Skip to content
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 11 commits into from
Feb 28, 2022

Conversation

zyfncg
Copy link
Contributor

@zyfncg zyfncg commented Feb 21, 2022

PR types

New features

PR changes

APIs

Describe

C++ API支持原Op体系下的Dispensable机制(即输入为可选),可通过yaml文件中optional项进行配置(如optiona : x, y,具体示例可参考matmul_double_grad配置)。

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

chenwhql
chenwhql previously approved these changes Feb 25, 2022
MetaTensor* dy,
MetaTensor* dz) {
if (dx) {
dx->share_meta(x);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

The actual behaviour is "copy_meta" in stead of so-called "share_meta", which is misleading. Strongly suggest that we should adjust this interface name in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里确实是有歧义,后续需要调整一下

@@ -31,6 +31,14 @@ inline std::shared_ptr<phi::DenseTensor> TensorToDenseTensor(
return std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
}

inline paddle::optional<const phi::DenseTensor&> TensorToDenseTensor(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle::optional<T&> doesn't seem to own the referenced object:
image

Which means the returned object "paddle::optional<const phi::DenseTensor&>" is under the risk of having dangling reference once the input "tensor" is destructed.

If this function is only used in generated C++ API, and we're confident about the lifetime management, can we at least add some comments to warn the risk of using this interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想了一下,这里直接返回std::shared_ptr<phi::DenseTensor>可能会更好一些,在形式上也更统一,是否为空的判断逻辑可以在自动生成的API内部代码里处理

@@ -49,12 +57,28 @@ inline std::shared_ptr<phi::SelectedRows> TensorToSelectedRows(
return std::dynamic_pointer_cast<phi::SelectedRows>(tensor.impl());
}

inline paddle::optional<const phi::SelectedRows&> TensorToSelectedRows(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

/* ----------------- for infer_meta --------------------- */

inline phi::MetaTensor MakeMetaTensor(const phi::DenseTensor& tensor) {
return phi::MetaTensor(tensor);
}

inline paddle::optional<phi::MetaTensor> MakeMetaTensor(
Copy link
Contributor

@jim19930609 jim19930609 Feb 25, 2022

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?

Copy link
Contributor Author

@zyfncg zyfncg Feb 26, 2022

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&>的形式

@@ -69,6 +93,14 @@ inline phi::MetaTensor MakeMetaTensor(const phi::SelectedRows& tensor) {
return phi::MetaTensor(tensor);
}

inline paddle::optional<phi::MetaTensor> MakeMetaTensor(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@zyfncg zyfncg merged commit aceb25e into PaddlePaddle:develop Feb 28, 2022
@zyfncg zyfncg deleted the api_optional branch February 28, 2022 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants