-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[Pten] Support optional param for C++ API #39760
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
48e73b9
fix selected_rows bug in C++ API
zyfncg ae4247e
add optional for C++ APIO
zyfncg daa21e8
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg e68ea8e
data transform support optional
zyfncg 2a5f7de
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 3ad6a05
remove data transform for optional vector<Tensor>
zyfncg 8e13120
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg cf9f824
Merge branch 'api_optional' of github.com:zyfncg/Paddle into api_opti…
zyfncg 80b8393
adjust some format of funtcion
zyfncg d773761
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 98fa938
fix empyt bug
zyfncg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,14 @@ inline std::shared_ptr<phi::DenseTensor> TensorToDenseTensor( | |
return std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl()); | ||
} | ||
|
||
inline std::shared_ptr<phi::DenseTensor> TensorToDenseTensor( | ||
const paddle::optional<Tensor>& tensor) { | ||
if (tensor) { | ||
return std::dynamic_pointer_cast<phi::DenseTensor>(tensor->impl()); | ||
} | ||
return nullptr; | ||
} | ||
|
||
inline std::unique_ptr<std::vector<phi::DenseTensor>> TensorToDenseTensor( | ||
const std::vector<Tensor>& tensors) { | ||
auto pt_tensors = std::make_unique<std::vector<phi::DenseTensor>>(); | ||
|
@@ -49,12 +57,28 @@ inline std::shared_ptr<phi::SelectedRows> TensorToSelectedRows( | |
return std::dynamic_pointer_cast<phi::SelectedRows>(tensor.impl()); | ||
} | ||
|
||
inline std::shared_ptr<phi::SelectedRows> TensorToSelectedRows( | ||
const paddle::optional<Tensor>& tensor) { | ||
if (tensor) { | ||
return std::dynamic_pointer_cast<phi::SelectedRows>(tensor->impl()); | ||
} | ||
return nullptr; | ||
} | ||
|
||
/* ----------------- for infer_meta --------------------- */ | ||
|
||
inline phi::MetaTensor MakeMetaTensor(const phi::DenseTensor& tensor) { | ||
return phi::MetaTensor(tensor); | ||
} | ||
|
||
inline paddle::optional<phi::MetaTensor> MakeMetaTensor( | ||
const paddle::optional<const phi::DenseTensor&>& tensor) { | ||
if (tensor) { | ||
return {phi::MetaTensor(*tensor)}; | ||
} | ||
return {paddle::none}; | ||
} | ||
|
||
inline std::vector<phi::MetaTensor> MakeMetaTensor( | ||
const std::vector<phi::DenseTensor>& tensors) { | ||
std::vector<phi::MetaTensor> meta_tensors; | ||
|
@@ -69,6 +93,14 @@ inline phi::MetaTensor MakeMetaTensor(const phi::SelectedRows& tensor) { | |
return phi::MetaTensor(tensor); | ||
} | ||
|
||
inline paddle::optional<phi::MetaTensor> MakeMetaTensor( | ||
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. ditto |
||
const paddle::optional<const phi::SelectedRows&>& tensor) { | ||
if (tensor) { | ||
return {phi::MetaTensor(*tensor)}; | ||
} | ||
return {paddle::none}; | ||
} | ||
|
||
/* ------------------ for output ----------------------- */ | ||
|
||
inline phi::DenseTensor* SetKernelOutput(Backend backend, Tensor* out) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,23 @@ void GeneralBinaryGradInferMeta(const MetaTensor& x, | |
} | ||
} | ||
|
||
void GeneralTernaryGradInferMeta(const MetaTensor& x, | ||
const MetaTensor& y, | ||
const MetaTensor& z, | ||
MetaTensor* dx, | ||
MetaTensor* dy, | ||
MetaTensor* dz) { | ||
if (dx) { | ||
dx->share_meta(x); | ||
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. 这里确实是有歧义,后续需要调整一下 |
||
} | ||
if (dy) { | ||
dy->share_meta(y); | ||
} | ||
if (dz) { | ||
dz->share_meta(z); | ||
} | ||
} | ||
|
||
void GumbelSoftmaxGradInferMeta(const MetaTensor& out, | ||
const MetaTensor& dout, | ||
int axis, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems that sometimes the function returns "paddle::optional", while othertimes it returns "paddle::optional<T&>". May I ask what the design is, like what determines the return type?
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.
由于这里需要创建一个新的MetaTensor出来,如果直接返回
optional<const T&>
类型在函数退出后对象会被析构,如果直接返回对象,生成的代码在写法上存在问题,综合考虑这里目前返回optional<phi::MetaTensor>
最为合适。目前底层Kernel的参数形式为
optional<const DenseTensor&>
,InferMeta的参数形式需要与Kernel保持一致,所以在调用InferMeta和Kernel前都需要将参数统一转为paddle::optional<const T&>
的形式