Skip to content

Conversation

ming1753
Copy link
Contributor

@ming1753 ming1753 commented Nov 14, 2023

PR types

Others

PR changes

Others

Description

Pcard-71501

本PR的目的是在写了自定义算子(custom opetator)后,只需提供尽可能少的代码就能使自定义算子进trt,即自动生成对应的trt plugin。

  • 自定义算子提供了kenel实现,可以直接在plugin中复用
  • getOutputDimensions通过SetTrtInferShapeFn指定
  • supportsFormatCombination通过配置生成
    • 格式:{"<dtype1>[:format1]+<dtype2>[:format2]+...", "<dtype3>[:format3]+<dtype4>[:format4]+..."}
    • 依次列出所有输入和输出的dtype和format,format可缺省,缺省时为LINEAR
    • 比如一个有2个输入1个输出,第1个输入支持fp32和fp16,第二个输入只支持int32的自定义算子,config可以写成:{"float32:LINEAR+int32:LINEAR+float32:LINEAR", "float16:LINEAR+int32:LINEAR+float16:LINEAR"}

Example:

PD_BUILD_OP(custom_op)
    .Inputs({"X"})
    .Outputs({"Out"})
    .SetKernelFn(PD_KERNEL(paddle_gap_forward))
    .Attrs({"test_attr1: std::vector<int>", "test_attr2: int"})
    .SetInferShapeFn(PD_INFER_SHAPE(InferShape))
    .SetInferDtypeFn(PD_INFER_DTYPE(InferDtype))
    .SetTrtInferShapeFn(PD_TRT_INFER_SHAPE(getOutputDimensions))
    .SetTrtSupportsFormatConfig({"float32:LINEAR+float32:LINEAR"});

Copy link

paddle-bot bot commented Nov 14, 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.

@@ -27,6 +27,10 @@ limitations under the License. */
#include "paddle/utils/none.h"
#include "paddle/utils/optional.h"

#ifdef PADDLE_WITH_TENSORRT
#include "NvInfer.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

这个.h在inference demo仓库编译用户模块的时候,能找到吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TensorRT的头文件,用户指定TENSORRT_ROOT后是可以找到的。这个问题会在demo的README.me中补充说明一下。

Comment on lines 1090 to 1102
// NOTE(HongyuJia): Used to be compatible with the 2.0.1 released
// interface, and will be deprecated in the future
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(const bool&);
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(const int&);
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(const float&);
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(const int64_t&);

// NOTE(HongyuJia): Used to be compatible with the 2.1 released
// interface, but not recommended
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(std::string);
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(std::vector<int>);
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(std::vector<float>);
PD_SPECIALIZE_TrtSupportsFormateCallHelper_FOR_ATTR(std::vector<std::string>);
Copy link
Contributor

Choose a reason for hiding this comment

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

这些为了兼容的,你这里是否有必要再加上?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我认为还是有必要的,不然在存量的自定义算子中,会出现为了使用trt还要把const int的属性改为int。

@yuanlehome
Copy link
Contributor

inference demo仓库里也需要提供demo示例

@yuanlehome
Copy link
Contributor

需要在PR描述里把所有不支持的情况以及TODO暴露一下,比如win上有bug之类的

XieYunshen
XieYunshen previously approved these changes Nov 23, 2023
Copy link
Contributor

@XieYunshen XieYunshen left a comment

Choose a reason for hiding this comment

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

LGTM for set_tests_properties(test_inference_gap_setup PROPERTIES TIMEOUT 180)

YuanRisheng
YuanRisheng previously approved these changes Nov 24, 2023
zhiqiu
zhiqiu previously approved these changes Nov 24, 2023
Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

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

LGTM for const_cast

Ligoml
Ligoml previously approved these changes Nov 24, 2023
yuanlehome
yuanlehome previously approved these changes Nov 24, 2023
Copy link
Contributor

@yuanlehome yuanlehome left a comment

Choose a reason for hiding this comment

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

LGTM

pos_nbInputs_nbOutputs, in_out, attrs, pargs..., arg); \
} catch (paddle::bad_any_cast&) { \
PD_THROW( \
"Attribute cast error in custom operator TrtSupportsFormate " \
Copy link
Contributor

Choose a reason for hiding this comment

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

这里换行有点奇怪

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里断行是和原自定义算子的SetKernelFn, SetInferDtypeFn, SetInferDtypeFn中保持一致写的


// end: base template
template <typename T>
struct TrtSupportsFormateCallHelper<TypeTag<T>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Formate -> format?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

return dimsOutput;
}

bool supportsFormatCombination(
Copy link
Contributor

Choose a reason for hiding this comment

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

这个函数写起来过于繁琐,增加用户理解成本,可以考虑使用代码生成:
https://github.com/NVIDIA-AI-IOT/tensorrt_plugin_generator/blob/main/tpg/generate.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@ming1753 ming1753 dismissed stale reviews from yuanlehome, Ligoml, zhiqiu, and YuanRisheng via fcfc024 November 25, 2023 03:53
Copy link
Contributor

@yuanlehome yuanlehome left a comment

Choose a reason for hiding this comment

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

LGTM

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 for const_cast

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@Wangzheee Wangzheee merged commit ad9c6ef into PaddlePaddle:develop Dec 4, 2023
SigureMo pushed a commit to gouzil/Paddle that referenced this pull request Dec 5, 2023
…addlePaddle#58976)

* [Paddle-TRT] custom operator support generating plugin automatically
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.

10 participants