-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[PHI]Add new Tensor type and migrate save_combine kernel #47856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
60f8c2a
60e3717
ebbee9a
a87999d
9929a28
c4a66e6
ee11ac6
01e976c
bec4990
4010a6f
4ec7569
c4cca54
9bbd512
e552774
059e85f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#pragma once | ||
|
||
#include <unordered_map> | ||
|
||
#include "paddle/phi/core/extended_tensor.h" | ||
#include "paddle/utils/any.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
/// \brief Fluid Kernel and PHI Kernel will be unified in the future. | ||
/// So, we need a class in PHI that can represent the RAW type in Fluid. | ||
/// The RawTensor is for PHI Kernel that has RAW type arguments. | ||
class RawTensor : public phi::ExtendedTensor, | ||
public phi::TypeInfoTraits<phi::TensorBase, RawTensor> { | ||
public: | ||
RawTensor() = default; | ||
|
||
RawTensor(RawTensor&& other) = default; | ||
|
||
RawTensor(const RawTensor& other) = default; | ||
|
||
RawTensor& operator=(RawTensor&& other) = default; | ||
|
||
/// \brief Destroy the RawTensor and release exclusive resources. | ||
virtual ~RawTensor() = default; | ||
|
||
public: | ||
/// \brief Returns the name of the class for type traits. | ||
/// \return The name of the class. | ||
static const char* name() { return "RawTensor"; } | ||
|
||
template <typename T> | ||
T* GetMutable() { | ||
if (!data_.empty()) { | ||
try { | ||
return paddle::any_cast<T*>(data_); | ||
} catch (paddle::bad_any_cast&) { | ||
PADDLE_THROW(phi::errors::InvalidArgument( | ||
"Invalid data type error, expected %s, actual %s.", | ||
typeid(T).name(), | ||
data_type_.name())); | ||
} | ||
} | ||
T* created_data = new T(); | ||
data_ = created_data; | ||
data_deleter_ = [created_data]() { delete created_data; }; | ||
data_type_ = std::type_index(typeid(T)); | ||
return created_data; | ||
} | ||
|
||
// template <typename T> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里注释还有用吗?需要移除吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 谢谢,下个PR进行移除 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修复 |
||
// T* Get() { | ||
// if (!data_.empty()) { | ||
// try { | ||
// return paddle::any_cast<T*>(data_); | ||
// } catch (paddle::bad_any_cast&) { | ||
// PADDLE_THROW(phi::errors::InvalidArgument( | ||
// "Invalid data type error, expected %s, actual %s.", | ||
// typeid(T).name(), | ||
// data_type_.name())); | ||
// } | ||
// } else { | ||
// PADDLE_THROW( | ||
// phi::errors::Unavailable("RawTensor is not initialized, if you want | ||
// " | ||
// "to create data, you can use | ||
// GetMutable.")); | ||
// } | ||
// } | ||
|
||
template <typename T> | ||
bool IsType() const { | ||
return std::type_index(typeid(T)) == data_type_; | ||
} | ||
|
||
private: | ||
paddle::any data_; | ||
std::function<void(void)> data_deleter_; | ||
std::type_index data_type_ = std::type_index(typeid(void)); | ||
}; | ||
|
||
} // namespace framework | ||
} // namespace paddle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fluid 下的
RawType
是一个数据类型,然而在 phi 下变成了 "Tensor",这个概念是有差异的:enum DataType
做扩展?(phi 下如何表示 RawTensor 的数据类型?)RawType
如何对应到 phi 下的数据类型?(是否会扩展TransToProtoVarType
函数?)RawType
in the fluid isdatatype
. However, it corresponds to "Tensor" in phi. The concepts betweenRawType
andRawTensor
have differences:enum DataType
in the future? (How to represent the datatype ofRawTensor
? )RawType
matches the datatype under phi?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里RawTensor命名后边可以更改为Raw,可能理解上就更准确一点。
准确意义上来说,RawType在fluid下不是一个具体的数据类型,我的理解把它作为一个标志更为合理一点,类似于DataType里的Undefined。对于RawType的具体类型(可以是任意类型)是在执行Kernel的时候确定,Fluid由于是结构体类型的Kernel,其对于RawType的具体类型处理可以通过传入的参数「ExecutionContext」来展开进行。但是对于PHI Kernel,由于是函数式的,其函数参数类型必须明确,所以这里必须存在一个数据结构能表示不确定的数据类型。所以对于以上提出的俩点:
1,DataType里不需要扩展,从上边可以看出,Raw类型的在PHI下必须是一个类类型,DataType里存放的都是基础数据类型,所以显然是不合适的。
2,PHI下也不需要类型转换,因为RawType在Fluid下就不是一个具体的类型