Skip to content

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Dec 6, 2023

PR types

Bug fixes

PR changes

Others

Description

PIR API 与老 IR 相比并没有统一的 append_op 入口,因此可能有 EagerTensor 传入到 _C_ops 的情况,因此我们会在 C API 处将 Tensor 转为 Value

具体实现如下:

添加 set_static_op_arg_pre_cast_hook 接口,用于设置一个 hook,在将 PyObject cast 为 Value 之前调用,该 hook 将会在输入是一个 EagerTensor 的时候转换为 Value(利用 ParameterRecorder)

在进入动转静时,会同时设置该 hook,在离开动转静时会恢复(时机同 _to_static_mode_guard_

Pcard-67164

Copy link

paddle-bot bot commented Dec 6, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@SigureMo SigureMo changed the title [WIP][PIR][Dy2St] Cast Tensor to Value in PIR bind API [PIR][Dy2St] Cast Tensor to Value in PIR bind API Dec 13, 2023
2742195759
2742195759 previously approved these changes Dec 14, 2023
Copy link
Contributor

@2742195759 2742195759 left a comment

Choose a reason for hiding this comment

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

LGTM

pir::Value CastPyArg2Value(PyObject* obj,
const std::string& op_type,
size_t arg_pos) {
obj = CastPyArg2ValuePreHook(obj);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里obj可能会返回nullptr,此时会走到函数里的else分支,在报错文案里直接使用->会段错误吧,这里有潜在风险

Copy link
Member Author

Choose a reason for hiding this comment

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

已 ENFORCE~

PyObject* result = PyObject_CallFunction(hook, "O", obj);
if (result == nullptr) {
Py_DECREF(obj);
return nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

或者这里是不是不应该允许返回nullptr,应该ENFORCE下

@SigureMo SigureMo changed the title [PIR][Dy2St] Cast Tensor to Value in PIR bind API [PIR][Dy2St] Convert Tensor to Value in PIR bind API Dec 14, 2023
Copy link
Contributor

@Aurelius84 Aurelius84 left a comment

Choose a reason for hiding this comment

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

LGTM

@SigureMo SigureMo merged commit 1266da1 into PaddlePaddle:develop Dec 15, 2023
@SigureMo SigureMo deleted the pir/cast-tensor-to-value-in-pir-bind-api branch December 15, 2023 02:20
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.

3 participants