[CustomOp] Support output as input argument of kernel func #39353
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.
PR types
New features
PR changes
Others
Describe
[CustomOp] Support output as input argument of kernel func
扩展支持自定义算子将输出Tensor以Tensor*形式作为Kernel函数输入参数的写法。
之前自定义算子限定返回值必须是
vector<Tensor>
,这在一些情况下是无法满足需求的,包括但不限于:因此,为了确保功能完备,扩展支持以下写法的自定义算子开发,即返回值作为函数输入参数的写法:
当输出以
paddle::Tensor*
的形式指定,并且自定义算子kernel内没有对内存进行重新设定的话,Tensor*对应的Holder在自定义算子运算时操作的是框架内传入Tensor的Holder,从而确保能够适配由框架指定的inplace和fuse的场景。但由于
paddle::Tensor
和framework::Variable
有些差别,实质上在准备自定义算子的输出Tensor时,其内部的DenseTensor也是拷贝创建的(meta拷贝,holder共享),因此在自定义算子kernel执行结束后,需要对meta信息进行同步,holder如果相同的话,不需要再Reset。同时,原先写法仍然是完全兼容的。