{"id":12649,"date":"2023-02-01T21:07:15","date_gmt":"2023-02-01T13:07:15","guid":{"rendered":"http:\/\/139.9.1.231\/?p=12649"},"modified":"2023-02-01T21:07:17","modified_gmt":"2023-02-01T13:07:17","slug":"pytorch-onnx-1","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2023\/02\/01\/pytorch-onnx-1\/","title":{"rendered":"PyTorch \u4e2d\u652f\u6301\u66f4\u591a ONNX \u7b97\u5b50"},"content":{"rendered":"\n<p>\u5b66\u4e60\u4e86 PyTorch \u8f6c ONNX \u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u53d1\u73b0 PyTorch \u5bf9 ONNX \u7684\u652f\u6301\u8fd8\u4e0d\u9519\u3002\u4f46\u5728\u5b9e\u9645\u7684\u90e8\u7f72\u8fc7\u7a0b\u4e2d\uff0c\u96be\u514d\u78b0\u5230\u6a21\u578b\u65e0\u6cd5\u7528\u539f\u751f PyTorch \u7b97\u5b50\u8868\u793a\u7684\u60c5\u51b5\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u5f97\u8003\u8651\u6269\u5145 PyTorch\uff0c\u5373\u5728 PyTorch \u4e2d\u652f\u6301\u66f4\u591a ONNX \u7b97\u5b50\u3002<\/p>\n\n\n\n<p>\u800c\u8981\u4f7f PyTorch \u7b97\u5b50\u987a\u5229\u8f6c\u6362\u5230 ONNX \uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u4ee5\u4e0b\u4e09\u4e2a\u73af\u8282\u90fd\u4e0d\u51fa\u9519\uff1a<\/p>\n\n\n\n<ul><li>\u7b97\u5b50\u5728 PyTorch \u4e2d\u6709\u5b9e\u73b0<\/li><li>\u6709\u628a\u8be5 PyTorch \u7b97\u5b50\u6620\u5c04\u6210\u4e00\u4e2a\u6216\u591a\u4e2a ONNX \u7b97\u5b50\u7684\u65b9\u6cd5<\/li><li>ONNX \u6709\u76f8\u5e94\u7684\u7b97\u5b50<\/li><\/ul>\n\n\n\n<p>\u53ef\u5728\u5b9e\u9645\u90e8\u7f72\u4e2d\uff0c\u8fd9\u4e09\u90e8\u5206\u7684\u5185\u5bb9\u90fd\u53ef\u80fd\u6709\u6240\u7f3a\u5931\u3002\u5176\u4e2d\u6700\u574f\u7684\u60c5\u51b5\u662f\uff1a\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u65b0\u7684\u7b97\u5b50\uff0c\u5b83\u4e0d\u4ec5\u7f3a\u5c11 PyTorch \u5b9e\u73b0\uff0c\u8fd8\u7f3a\u5c11 PyTorch \u5230 ONNX \u7684\u6620\u5c04\u5173\u7cfb\u3002\u4f46\u6240\u8c13\u8f66\u5230\u5c71\u524d\u5fc5\u6709\u8def\uff0c\u5bf9\u4e8e\u8fd9\u4e09\u4e2a\u73af\u8282\uff0c\u6211\u4eec\u4e5f\u5206\u522b\u90fd\u6709\u4ee5\u4e0b\u7684\u6dfb\u52a0\u652f\u6301\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul><li>PyTorch \u7b97\u5b50<ul><li>\u7ec4\u5408\u73b0\u6709\u7b97\u5b50<\/li><li>\u6dfb\u52a0 TorchScript \u7b97\u5b50<\/li><li>\u6dfb\u52a0\u666e\u901a C++ \u62d3\u5c55\u7b97\u5b50<\/li><\/ul><\/li><li>\u6620\u5c04\u65b9\u6cd5<ul><li>\u4e3a ATen \u7b97\u5b50\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570<\/li><li>\u4e3a TorchScript \u7b97\u5b50\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570<\/li><li>\u5c01\u88c5\u6210&nbsp;<code>torch.autograd.Function<\/code>&nbsp;\u5e76\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570<\/li><\/ul><\/li><li>ONNX \u7b97\u5b50<ul><li>\u4f7f\u7528\u73b0\u6709 ONNX \u7b97\u5b50<\/li><li>\u5b9a\u4e49\u65b0 ONNX \u7b97\u5b50<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>\u90a3\u4e48\u9762\u5bf9\u4e0d\u540c\u7684\u60c5\u51b5\u65f6\uff0c\u5c31\u9700\u8981\u6211\u4eec\u7075\u6d3b\u5730\u9009\u7528\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\u3002\u542c\u8d77\u6765\u662f\u4e0d\u662f\u5f88\u590d\u6742\uff1f\u522b\u62c5\u5fc3\uff0c\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u56f4\u7ed5\u7740\u4e09\u79cd\u7b97\u5b50<strong>\u6620\u5c04\u65b9\u6cd5<\/strong>\uff0c\u5b66\u4e60\u4e09\u4e2a\u6dfb\u52a0\u7b97\u5b50\u652f\u6301\u7684\u5b9e\u4f8b\uff0c\u6765\u7406\u6e05\u5982\u4f55\u5408\u9002\u5730\u4e3a PyTorch \u7b97\u5b50\u8f6c ONNX \u7b97\u5b50\u7684\u4e09\u4e2a\u73af\u8282\u6dfb\u52a0\u652f\u6301\u3002<\/p>\n\n\n\n<h2 id=\"h_513387413_0\"><strong>\u00a0<strong>\u652f\u6301 ATen\u00a0<\/strong>\u7b97\u5b50<\/strong><\/h2>\n\n\n\n<p>\u5b9e\u9645\u7684\u90e8\u7f72\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u90fd\u6709\u53ef\u80fd\u4f1a\u78b0\u5230\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u7b97\u5b50\u7f3a\u5931\u95ee\u9898\uff1a \u7b97\u5b50\u5728 ATen \u4e2d\u5df2\u7ecf\u5b9e\u73b0\u4e86\uff0cONNX \u4e2d\u4e5f\u6709\u76f8\u5173\u7b97\u5b50\u7684\u5b9a\u4e49\uff0c\u4f46\u662f\u76f8\u5173\u7b97\u5b50\u6620\u5c04\u6210 ONNX \u7684\u89c4\u5219\u6ca1\u6709\u5199\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981<strong>\u4e3a ATen \u7b97\u5b50\u8865\u5145\u63cf\u8ff0\u6620\u5c04\u89c4\u5219\u7684\u7b26\u53f7\u51fd\u6570<\/strong>\u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><a href=\"https:\/\/pytorch.org\/cppdocs\/#aten\" target=\"_blank\" rel=\"noreferrer noopener\">ATen<\/a>&nbsp;\u662f PyTorch \u5185\u7f6e\u7684 C++ \u5f20\u91cf\u8ba1\u7b97\u5e93\uff0cPyTorch \u7b97\u5b50\u5728\u5e95\u5c42\u7edd\u5927\u591a\u6570\u8ba1\u7b97\u90fd\u662f\u7528 ATen \u5b9e\u73b0\u7684\u3002<\/p><\/blockquote>\n\n\n\n<p>\u4e0a\u671f\u4e60\u9898\u4e2d\uff0c\u6211\u4eec\u66fe\u7ecf\u63d0\u5230\u4e86 ONNX \u7684&nbsp;<code>Asinh<\/code>&nbsp;\u7b97\u5b50\u3002\u8fd9\u4e2a\u7b97\u5b50\u5728 ATen \u4e2d\u6709\u5b9e\u73b0\uff0c\u5374\u7f3a\u5c11\u4e86\u6620\u5c04\u5230 ONNX \u7b97\u5b50\u7684\u7b26\u53f7\u51fd\u6570\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u4e3a\u5b83\u8865\u5145\u7b26\u53f7\u51fd\u6570\uff0c\u5e76\u5bfc\u51fa\u4e00\u4e2a\u5305\u542b\u8fd9\u4e2a\u7b97\u5b50\u7684 ONNX \u6a21\u578b\u3002<\/p>\n\n\n\n<h3 id=\"h_513387413_1\"><strong>\u83b7\u53d6 ATen \u4e2d\u7b97\u5b50\u63a5\u53e3\u5b9a\u4e49<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u7f16\u5199\u7b26\u53f7\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u83b7\u5f97&nbsp;<code>asinh<\/code>&nbsp;\u63a8\u7406\u63a5\u53e3\u7684\u8f93\u5165\u53c2\u6570\u5b9a\u4e49\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u8981\u53bb&nbsp;<code>torch\/_C\/_VariableFunctions.pyi<\/code>&nbsp;\u548c&nbsp;<code>torch\/nn\/functional.pyi<\/code>&nbsp;\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u641c\u7d22\u6211\u4eec\u521a\u521a\u5f97\u5230\u7684\u8fd9\u4e2a\u7b97\u5b50\u540d\u3002\u8fd9\u4e24\u4e2a\u6587\u4ef6\u662f\u7f16\u8bd1 PyTorch \u65f6\u672c\u5730\u81ea\u52a8\u751f\u6210\u7684\u6587\u4ef6\uff0c\u91cc\u9762\u5305\u542b\u4e86 ATen \u7b97\u5b50\u7684 PyTorch \u8c03\u7528\u63a5\u53e3\u3002\u901a\u8fc7\u641c\u7d22\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053&nbsp;<code>asinh<\/code>&nbsp;\u5728\u6587\u4ef6&nbsp;<code>torch\/_C\/_VariableFunctions.pyi<\/code>&nbsp;\u4e2d\uff0c\u5176\u63a5\u53e3\u5b9a\u4e49\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def asinh(input: Tensor, *, out: Optional&#91;Tensor]=None) -&gt; Tensor: ... <\/code><\/pre>\n\n\n\n<p>\u7ecf\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u786e\u8ba4\u4e86\u7f3a\u5931\u7684\u7b97\u5b50\u540d\u4e3a&nbsp;<code>asinh<\/code>\uff0c\u5b83\u662f\u4e00\u4e2a\u6709\u5b9e\u73b0\u7684 ATen \u7b97\u5b50\u3002\u6211\u4eec\u8fd8\u8bb0\u4e0b\u4e86&nbsp;<code>asinh<\/code>&nbsp;\u7684\u8c03\u7528\u63a5\u53e3\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8981\u4e3a\u5b83\u8865\u5145\u7b26\u53f7\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u8f6c\u6362\u6210 ONNX \u6a21\u578b\u65f6\u4e0d\u518d\u62a5\u9519\u3002<\/p>\n\n\n\n<h3 id=\"h_513387413_2\"><strong>\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u591a\u6b21\u63a5\u89e6\u4e86\u5b9a\u4e49 PyTorch \u5230 ONNX \u6620\u5c04\u89c4\u5219\u7684\u7b26\u53f7\u51fd\u6570\u4e86\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5411\u5927\u5bb6\u6b63\u5f0f\u4ecb\u7ecd\u4e00\u4e0b\u7b26\u53f7\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u7b26\u53f7\u51fd\u6570\uff0c\u53ef\u4ee5\u770b\u6210\u662f PyTorch \u7b97\u5b50\u7c7b\u7684\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u3002\u5728\u628a PyTorch \u6a21\u578b\u8f6c\u6362\u6210 ONNX \u6a21\u578b\u65f6\uff0c\u5404\u4e2a PyTorch \u7b97\u5b50\u7684\u7b26\u53f7\u51fd\u6570\u4f1a\u88ab\u4f9d\u6b21\u8c03\u7528\uff0c\u4ee5\u5b8c\u6210 PyTorch \u7b97\u5b50\u5230 ONNX \u7b97\u5b50\u7684\u8f6c\u6362\u3002\u7b26\u53f7\u51fd\u6570\u7684\u5b9a\u4e49\u4e00\u822c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def symbolic(g: torch._C.Graph, input_0: torch._C.Value, input_1: torch._C.Value, ...): <\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c<code>torch._C.Graph<\/code>&nbsp;\u548c&nbsp;<code>torch._C.Value<\/code>&nbsp;\u90fd\u5bf9\u5e94 PyTorch \u7684 C++ \u5b9e\u73b0\u91cc\u7684\u4e00\u4e9b\u7c7b\u3002\u6211\u4eec\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e0d\u6df1\u7a76\u5b83\u4eec\u7684\u7ec6\u8282\uff08\u611f\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u53c2\u8003\u6211\u4eec\u7684 TorchScript \u7cfb\u5217\u6587\u7ae0\u4e2d\u5bf9&nbsp;<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/489090393\">trace \u673a\u5236\u7684\u89e3\u8bfb<\/a>\uff09\uff0c\u53ea\u9700\u8981\u77e5\u9053\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u56fa\u5b9a\u53eb&nbsp;<code>g<\/code>\uff0c\u5b83\u8868\u793a\u548c\u8ba1\u7b97\u56fe\u76f8\u5173\u7684\u5185\u5bb9\uff1b\u540e\u9762\u7684\u6bcf\u4e2a\u53c2\u6570\u90fd\u8868\u793a\u7b97\u5b50\u7684\u8f93\u5165\uff0c\u9700\u8981\u548c\u7b97\u5b50\u7684\u524d\u5411\u63a8\u7406\u63a5\u53e3\u7684\u8f93\u5165\u76f8\u540c\u3002\u5bf9\u4e8e ATen \u7b97\u5b50\u6765\u8bf4\uff0c\u5b83\u4eec\u7684\u524d\u5411\u63a8\u7406\u63a5\u53e3\u5c31\u662f\u4e0a\u8ff0\u4e24\u4e2a&nbsp;<code>.pyi<\/code>&nbsp;\u6587\u4ef6\u91cc\u7684\u51fd\u6570\u63a5\u53e3\u3002<\/p>\n\n\n\n<p><code>g<\/code>&nbsp;\u6709\u4e00\u4e2a\u65b9\u6cd5&nbsp;<code>op<\/code>\u3002\u5728\u628a PyTorch \u7b97\u5b50\u8f6c\u6362\u6210 ONNX \u7b97\u5b50\u65f6\uff0c\u9700\u8981\u5728\u7b26\u53f7\u51fd\u6570\u4e2d\u8c03\u7528\u6b64\u65b9\u6cd5\u6765\u4e3a\u6700\u7ec8\u7684\u8ba1\u7b97\u56fe\u6dfb\u52a0\u4e00\u4e2a ONNX \u7b97\u5b50\u3002\u5176\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def op(name: str, input_0: torch._C.Value, input_1: torch._C.Value, ...) <\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7b97\u5b50\u540d\u79f0\u3002\u5982\u679c\u8be5\u7b97\u5b50\u662f\u666e\u901a\u7684 ONNX \u7b97\u5b50\uff0c\u53ea\u9700\u8981\u628a\u5b83\u5728 ONNX \u5b98\u65b9\u6587\u6863\u91cc\u7684\u540d\u79f0\u586b\u8fdb\u53bb\u5373\u53ef\uff08\u6211\u4eec\u7a0d\u540e\u518d\u8bb2\u5176\u4ed6\u60c5\u51b5\uff09\u3002<\/p>\n\n\n\n<p>\u5728\u6700\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u8981\u628a PyTorch \u7b97\u5b50\u7684\u8f93\u5165\u7528<code>g.op()<\/code>\u4e00\u4e00\u5bf9\u5e94\u5230 ONNX \u7b97\u5b50\u4e0a\u5373\u53ef\uff0c\u5e76\u628a<code>g.op()<\/code>\u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u7b26\u53f7\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u5728\u60c5\u51b5\u66f4\u590d\u6742\u65f6\uff0c\u6211\u4eec\u8f6c\u6362\u4e00\u4e2a PyTorch \u7b97\u5b50\u53ef\u80fd\u8981\u65b0\u5efa\u82e5\u5e72\u4e2a ONNX \u7b97\u5b50\u3002<\/p>\n\n\n\n<p>\u8865\u5145\u5b8c\u4e86\u80cc\u666f\u77e5\u8bc6\uff0c\u8ba9\u6211\u4eec\u56de\u5230&nbsp;<code>asinh<\/code>&nbsp;\u7b97\u5b50\u4e0a\uff0c\u6765\u4e3a\u5b83\u7f16\u5199\u7b26\u53f7\u51fd\u6570\u3002\u6211\u4eec\u5148\u53bb\u7ffb\u9605\u4e00\u4e0b ONNX \u7b97\u5b50\u6587\u6863\uff0c\u5b66\u4e60\u4e00\u4e0b\u6211\u4eec\u5728\u7b26\u53f7\u51fd\u6570\u91cc\u7684\u6620\u5c04\u5173\u7cfb&nbsp;<code>g.op()<\/code>&nbsp;\u91cc\u5e94\u8be5\u600e\u4e48\u5199\u3002<code>Asinh<\/code>&nbsp;\u7684<a href=\"https:\/\/github.com\/onnx\/onnx\/blob\/main\/docs\/Operators.md#asinh\" target=\"_blank\" rel=\"noreferrer noopener\">\u6587\u6863<\/a>\u5199\u9053\uff1a\u8be5\u7b97\u5b50\u6709\u4e00\u4e2a\u8f93\u5165&nbsp;<code>input<\/code>\uff0c\u4e00\u4e2a\u8f93\u51fa&nbsp;<code>output<\/code>\uff0c\u4e8c\u8005\u7684\u7c7b\u578b\u90fd\u4e3a\u5f20\u91cf\u3002<\/p>\n\n\n\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4fe1\u606f\u6536\u96c6\u73af\u8282\u3002\u6211\u4eec\u5728\u4e0a\u4e00\u5c0f\u8282\u5f97\u77e5\u4e86&nbsp;<code>asinh<\/code>&nbsp;\u7684\u63a8\u7406\u63a5\u53e3\u5b9a\u4e49\uff0c\u5728\u8fd9\u4e00\u5c0f\u8282\u91cc\u6536\u96c6\u4e86 ONNX \u7b97\u5b50&nbsp;<code>Asinh<\/code>&nbsp;\u7684\u5b9a\u4e49\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4ee3\u7801\u6765\u8865\u5145\u8fd9\u4e8c\u8005\u7684\u6620\u5c04\u5173\u7cfb\u4e86\u3002\u5728\u521a\u521a\u5bfc\u51fa&nbsp;<code>asinh<\/code>&nbsp;\u7b97\u5b50\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.onnx.symbolic_registry import register_op \n \ndef asinh_symbolic(g, input, *, out=None): \n    return g.op(\"Asinh\", input) \n \nregister_op('asinh', asinh_symbolic, '', 9)  <\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684<code>asinh_symbolic<\/code>\u5c31\u662f<code>asinh<\/code>\u7684\u7b26\u53f7\u51fd\u6570\u3002\u4ece\u9664<code>g<\/code>\u4ee5\u5916\u7684\u7b2c\u4e8c\u4e2a\u8f93\u5165\u53c2\u6570\u5f00\u59cb\uff0c\u5176\u8f93\u5165\u53c2\u6570\u5e94\u8be5\u4e25\u683c\u5bf9\u5e94\u5b83\u5728 ATen \u4e2d\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def asinh(input: Tensor, *, out: Optional&#91;Tensor]=None) -&gt; Tensor: ... <\/code><\/pre>\n\n\n\n<p>\u5728\u7b26\u53f7\u51fd\u6570\u7684\u51fd\u6570\u4f53\u4e2d\uff0c<code>g.op(\"Asinh\", input)<\/code>\u5219\u5b8c\u6210\u4e86 ONNX \u7b97\u5b50\u7684\u5b9a\u4e49\u3002\u5176\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570<code>\"Asinh\"<\/code>\u662f\u7b97\u5b50\u5728 ONNX \u4e2d\u7684\u540d\u79f0\u3002\u81f3\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570&nbsp;<code>input<\/code>\uff0c\u5982\u6211\u4eec\u521a\u521a\u5728\u6587\u6863\u91cc\u6240\u89c1\uff0c\u8fd9\u4e2a\u7b97\u5b50\u53ea\u6709\u4e00\u4e2a\u8f93\u5165\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u628a\u7b26\u53f7\u51fd\u6570\u7684\u8f93\u5165\u53c2\u6570&nbsp;<code>input<\/code>&nbsp;\u5bf9\u5e94\u8fc7\u53bb\u5c31\u884c\u3002ONNX \u7684&nbsp;<code>Asinh<\/code>&nbsp;\u7684\u8f93\u51fa\u548c ATen \u7684&nbsp;<code>asinh<\/code>&nbsp;\u7684\u8f93\u51fa\u662f\u4e00\u81f4\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u76f4\u63a5\u628a&nbsp;<code>g.op()<\/code>&nbsp;\u7684\u7ed3\u679c\u8fd4\u56de\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u5b8c\u7b26\u53f7\u51fd\u6570\u540e\uff0c\u6211\u4eec\u8981\u628a\u8fd9\u4e2a\u7b26\u53f7\u51fd\u6570\u548c\u539f\u6765\u7684 ATen \u7b97\u5b50\u201c\u7ed1\u5b9a\u201d\u8d77\u6765\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u8981\u7528\u5230&nbsp;<code>register_op<\/code>&nbsp;\u8fd9\u4e2a PyTorch API \u6765\u5b8c\u6210\u7ed1\u5b9a\u3002\u5982\u793a\u4f8b\u6240\u793a\uff0c\u53ea\u9700\u8981\u4e00\u884c\u7b80\u5355\u7684\u4ee3\u7801\u5373\u53ef\u628a\u7b26\u53f7\u51fd\u6570&nbsp;<code>asinh_symbolic<\/code>&nbsp;\u7ed1\u5b9a\u5230\u7b97\u5b50&nbsp;<code>asinh<\/code>&nbsp;\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>register_op('asinh', asinh_symbolic, '', 9) <\/code><\/pre>\n\n\n\n<p><code>register_op<\/code>\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u76ee\u6807 ATen \u7b97\u5b50\u540d\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8981\u6ce8\u518c\u7684\u7b26\u53f7\u51fd\u6570\uff0c\u8fd9\u4e24\u4e2a\u53c2\u6570\u5f88\u597d\u7406\u89e3\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u7b97\u5b50\u7684\u201c\u57df\u201d\uff0c\u5bf9\u4e8e\u666e\u901a ONNX \u7b97\u5b50\uff0c\u76f4\u63a5\u586b\u7a7a\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u7b2c\u56db\u4e2a\u53c2\u6570\u8868\u793a\u5411\u54ea\u4e2a\u7b97\u5b50\u96c6\u7248\u672c\u6ce8\u518c\u3002\u6211\u4eec\u9075\u7167 ONNX \u6807\u51c6\uff0c\u5411\u7b2c 9 \u53f7\u7b97\u5b50\u96c6\u6ce8\u518c\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u5411\u7b2c 9 \u53f7\u7b97\u5b50\u96c6\u6ce8\u518c\uff0c\u4e0d\u4ee3\u8868\u8f83\u65b0\u7684\u7b97\u5b50\u96c6\uff08\u7b2c 10 \u53f7\u3001\u7b2c 11 \u53f7\u2026\u2026\uff09\u90fd\u5f97\u5230\u4e86\u6ce8\u518c\u3002\u5728\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5148\u53ea\u5411\u7b2c 9 \u53f7\u7b97\u5b50\u96c6\u6ce8\u518c\u3002<\/p>\n\n\n\n<p>\u6574\u7406\u4e00\u4e0b\uff0c\u6211\u4eec\u6700\u7ec8\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch \n \nclass Model(torch.nn.Module): \n    def __init__(self): \n        super().__init__() \n \n    def forward(self, x): \n        return torch.asinh(x) \n \nfrom torch.onnx.symbolic_registry import register_op \n \ndef asinh_symbolic(g, input, *, out=None): \n    return g.op(\"Asinh\", input) \n \nregister_op('asinh', asinh_symbolic, '', 9) \n \nmodel = Model() \ninput = torch.rand(1, 3, 10, 10) \ntorch.onnx.export(model, input, 'asinh.onnx') \n <\/code><\/pre>\n\n\n\n<p>\u6210\u529f\u5bfc\u51fa\u7684\u8bdd\uff0c<code>asinh.onnx<\/code>&nbsp;\u5e94\u8be5\u957f\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic4.zhimg.com\/v2-2094d724daf7438c9cfde42734e61f67_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<h3 id=\"h_513387413_3\"><strong>\u6d4b\u8bd5\u7b97\u5b50<\/strong><\/h3>\n\n\n\n<p>\u5728\u5b8c\u6210\u4e86\u4e00\u4efd\u81ea\u5b9a\u4e49\u7b97\u5b50\u540e\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u6d4b\u8bd5\u4e00\u4e0b\u7b97\u5b50\u7684\u6b63\u786e\u6027\u3002\u4e00\u822c\u6211\u4eec\u8981\u7528 PyTorch \u8fd0\u884c\u4e00\u904d\u539f\u7b97\u5b50\uff0c\u518d\u7528\u63a8\u7406\u5f15\u64ce\uff08\u6bd4\u5982 ONNX Runtime\uff09\u8fd0\u884c\u4e00\u4e0b ONNX \u7b97\u5b50\uff0c\u6700\u540e\u6bd4\u5bf9\u4e24\u6b21\u7684\u8fd0\u884c\u7ed3\u679c\u3002\u5bf9\u4e8e\u6211\u4eec\u521a\u521a\u5f97\u5230\u7684&nbsp;<code>asinh.onnx<\/code>\uff0c\u53ef\u4ee5\u7528\u5982\u4e0b\u4ee3\u7801\u6765\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnxruntime \nimport torch \nimport numpy as np \n \nclass Model(torch.nn.Module): \n    def __init__(self): \n        super().__init__() \n \n    def forward(self, x): \n        return torch.asinh(x) \n \nmodel = Model() \ninput = torch.rand(1, 3, 10, 10) \ntorch_output = model(input).detach().numpy() \n \nsess = onnxruntime.InferenceSession('asinh.onnx') \nort_output = sess.run(None, {'0': input.numpy()})&#91;0] \n \nassert np.allclose(torch_output, ort_output) <\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4efd\u4ee3\u7801\u91cc\uff0c\u6211\u4eec\u7528 PyTorch \u505a\u4e86\u4e00\u904d\u63a8\u7406\uff0c\u5e76\u628a\u7ed3\u679c\u8f6c\u6210\u4e86 numpy \u683c\u5f0f\u3002\u4e4b\u540e\uff0c\u6211\u4eec\u53c8\u7528 ONNX Runtime \u5bf9 onnx \u6587\u4ef6\u505a\u4e86\u4e00\u6b21\u63a8\u7406\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5fd8\u4e86 ONNX Runtime \u7684\u8c03\u7528\u65b9\u6cd5\u7684\u8bdd\uff0c\u6b22\u8fce\u56de\u987e<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/477743341\">\u7b2c\u4e00\u7bc7\u6559\u7a0b<\/a>~<\/p><\/blockquote>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528&nbsp;<code>np.allclose<\/code>&nbsp;\u6765\u4fdd\u8bc1\u4e24\u4e2a\u7ed3\u679c\u5f20\u91cf\u7684\u8bef\u5dee\u5728\u4e00\u4e2a\u53ef\u4ee5\u5141\u8bb8\u7684\u8303\u56f4\u5185\u3002\u4e00\u5207\u6b63\u5e38\u7684\u8bdd\uff0c\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c<code>assert<\/code>&nbsp;\u6240\u5728\u884c\u4e0d\u4f1a\u62a5\u9519\uff0c\u7a0b\u5e8f\u5e94\u8be5\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u3002<\/p>\n\n\n\n<h2 id=\"h_513387413_4\"><strong>\u652f\u6301 TorchScript \u7b97\u5b50<\/strong><\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u8fd0\u7b97\uff0c\u4ec5\u4f7f\u7528 PyTorch \u539f\u751f\u7b97\u5b50\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u5c31\u8981\u8003\u8651\u81ea\u5b9a\u4e49\u4e00\u4e2a PyTorch \u7b97\u5b50\uff0c\u518d\u628a\u5b83\u8f6c\u6362\u5230 ONNX \u4e2d\u4e86\u3002\u65b0\u589e PyTorch \u7b97\u5b50\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0cPyTorch \u5b98\u65b9\u6bd4\u8f83\u63a8\u8350\u7684\u4e00\u79cd\u505a\u6cd5\u662f\u6dfb\u52a0&nbsp;<a href=\"https:\/\/pytorch.org\/tutorials\/advanced\/torch_script_custom_ops.html\" target=\"_blank\" rel=\"noreferrer noopener\">TorchScript \u7b97\u5b50<\/a>&nbsp;\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u6dfb\u52a0\u7b97\u5b50\u7684\u65b9\u6cd5\u8f83\u7e41\u7410\uff0c\u6211\u4eec\u4eca\u5929\u8df3\u8fc7\u65b0\u589e TorchScript \u7b97\u5b50\u7684\u5185\u5bb9\uff0c\u4ee5\u53ef\u53d8\u5f62\u5377\u79ef\uff08Deformable Convolution\uff09\u7b97\u5b50\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u4e3a\u73b0\u6709 TorchScript \u7b97\u5b50\u6dfb\u52a0 ONNX \u652f\u6301\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u53ef\u53d8\u5f62\u5377\u79ef\uff08Deformable Convolution\uff09\u662f\u5728 Torchvision \u4e2d\u5b9e\u73b0\u7684 TorchScript \u7b97\u5b50\uff0c\u867d\u7136\u5c1a\u672a\u5f97\u5230\u5e7f\u6cdb\u652f\u6301\uff0c\u4f46\u662f\u51fa\u73b0\u5728\u8bb8\u591a\u6a21\u578b\u4e2d\u3002<\/p><\/blockquote>\n\n\n\n<p>\u6709\u4e86\u652f\u6301 ATen \u7b97\u5b50\u7684\u7ecf\u9a8c\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u4e3a\u7b97\u5b50\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570\u4e00\u822c\u8981\u7ecf\u8fc7\u4ee5\u4e0b\u51e0\u6b65\uff1a<\/p>\n\n\n\n<ol><li>\u83b7\u53d6\u539f\u7b97\u5b50\u7684\u524d\u5411\u63a8\u7406\u63a5\u53e3\u3002<\/li><li>\u83b7\u53d6\u76ee\u6807 ONNX \u7b97\u5b50\u7684\u5b9a\u4e49\u3002<\/li><li>\u7f16\u5199\u7b26\u53f7\u51fd\u6570\u5e76\u7ed1\u5b9a\u3002<\/li><\/ol>\n\n\n\n<p>\u5728\u4e3a\u53ef\u53d8\u5f62\u5377\u79ef\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c1d\u8bd5\u8d70\u4e00\u904d\u8fd9\u4e2a\u6d41\u7a0b\u3002<\/p>\n\n\n\n<h3 id=\"h_513387413_5\"><strong>\u4f7f\u7528 TorchScript \u7b97\u5b50<\/strong><\/h3>\n\n\n\n<p>\u548c\u4e4b\u524d\u4e00\u6837\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e86\u7b97\u5b50\u7684\u6a21\u578b\uff0c\u4e3a\u4e4b\u540e\u8f6c\u6362 ONNX \u6a21\u578b\u505a\u51c6\u5907\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch \nimport torchvision \n \nclass Model(torch.nn.Module): \n    def __init__(self): \n        super().__init__() \n        self.conv1 = torch.nn.Conv2d(3, 18, 3) \n        self.conv2 = torchvision.ops.DeformConv2d(3, 3, 3) \n \n    def forward(self, x): \n        return self.conv2(x, self.conv1(x)) <\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c<code>torchvision.ops.DeformConv2d<\/code>&nbsp;\u5c31\u662f Torchvision \u4e2d\u7684\u53ef\u53d8\u5f62\u5377\u79ef\u5c42\u3002\u76f8\u6bd4\u4e8e\u666e\u901a\u5377\u79ef\uff0c\u53ef\u53d8\u5f62\u5377\u79ef\u7684\u5176\u4ed6\u53c2\u6570\u90fd\u5927\u81f4\u76f8\u540c\uff0c\u552f\u4e00\u7684\u533a\u522b\u5c31\u662f\u5728\u63a8\u7406\u65f6\u9700\u8981\u591a\u8f93\u5165\u4e00\u4e2a\u8868\u793a\u504f\u79fb\u91cf\u7684\u5f20\u91cf\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u67e5\u8be2\u7b97\u5b50\u7684\u524d\u5411\u63a8\u7406\u63a5\u53e3\u3002<code>DeformConv2d<\/code>&nbsp;\u5c42\u6700\u7ec8\u4f1a\u8c03\u7528&nbsp;<code>deform_conv2d<\/code>&nbsp;\u8fd9\u4e2a\u7b97\u5b50\u3002\u6211\u4eec\u53ef\u4ee5\u5728&nbsp;<code>torchvision\/csrc\/ops\/deform_conv2d.cpp<\/code>&nbsp;\u4e2d\u67e5\u5230\u8be5\u7b97\u5b50\u7684\u8c03\u7528\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>m.def(TORCH_SELECTIVE_SCHEMA( \n      \"torchvision::deform_conv2d(Tensor input,  \n      Tensor weight,  \n      Tensor offset,  \n      ...... \n      bool use_mask) -&gt; Tensor\")); <\/code><\/pre>\n\n\n\n<p>\u90a3\u4e48\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u4e4b\u524d\u7684\u7ecf\u9a8c\uff0c\u6211\u4eec\u5c31\u662f\u8981\u53bb ONNX \u5b98\u65b9\u6587\u6863\u4e2d\u67e5\u627e\u7b97\u5b50\u7684\u5b9a\u4e49\u4e86\u3002<\/p>\n\n\n\n<h3 id=\"h_513387413_6\"><strong>\u81ea\u5b9a\u4e49 ONNX \u7b97\u5b50<\/strong><\/h3>\n\n\n\n<p>\u5f88\u9057\u61be\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u53bb ONNX \u7684\u5b98\u65b9\u7b97\u5b50\u9875\u9762\u641c\u7d22 &#8220;deform&#8221;\uff0c\u5c06\u641c\u4e0d\u51fa\u4efb\u4f55\u5185\u5bb9\u3002\u76ee\u524d\uff0cONNX \u8fd8\u6ca1\u6709\u63d0\u4f9b\u53ef\u53d8\u5f62\u5377\u79ef\u7684\u7b97\u5b50\uff0c\u6211\u4eec\u8981\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a ONNX \u7b97\u5b50\u4e86\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u524d\u9762\u8bb2\u8fc7\uff0c<code>g.op()<\/code>&nbsp;\u662f\u7528\u6765\u5b9a\u4e49 ONNX \u7b97\u5b50\u7684\u51fd\u6570\u3002\u5bf9\u4e8e ONNX \u5b98\u65b9\u5b9a\u4e49\u7684\u7b97\u5b50\uff0c<code>g.op()<\/code>&nbsp;\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u8be5\u7b97\u5b50\u7684\u540d\u79f0\u3002\u800c\u5bf9\u4e8e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7b97\u5b50\uff0c<code>g.op()<\/code>&nbsp;\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5e26\u547d\u540d\u7a7a\u95f4\u7684\u7b97\u5b50\u540d\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g.op(\"custom::deform_conv2d, ...) <\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c&#8221;::&#8221;\u524d\u9762\u7684\u5185\u5bb9\u5c31\u662f\u6211\u4eec\u7684\u547d\u540d\u7a7a\u95f4\u3002\u8be5\u6982\u5ff5\u548c C++ \u7684\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\uff0c\u662f\u4e3a\u4e86\u9632\u6b62\u547d\u540d\u51b2\u7a81\u800c\u8bbe\u5b9a\u7684\u3002\u5982\u679c\u5728&nbsp;<code>g.op()<\/code>&nbsp;\u91cc\u4e0d\u52a0\u524d\u9762\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u5219\u7b97\u5b50\u4f1a\u88ab\u9ed8\u8ba4\u6210 ONNX \u7684\u5b98\u65b9\u7b97\u5b50\u3002<\/p>\n\n\n\n<p>PyTorch \u5728\u8fd0\u884c&nbsp;<code>g.op()<\/code>&nbsp;\u65f6\u4f1a\u5bf9\u5b98\u65b9\u7684\u7b97\u5b50\u505a\u68c0\u67e5\uff0c\u5982\u679c\u7b97\u5b50\u540d\u6709\u8bef\uff0c\u6216\u8005\u7b97\u5b50\u7684\u8f93\u5165\u7c7b\u578b\u4e0d\u6b63\u786e\uff0c&nbsp;<code>g.op()<\/code>&nbsp;\u5c31\u4f1a\u62a5\u9519\u3002\u4e3a\u4e86\u8ba9\u6211\u4eec\u968f\u5fc3\u6240\u6b32\u5730\u5b9a\u4e49\u65b0 ONNX \u7b97\u5b50\uff0c\u6211\u4eec\u5fc5\u987b\u8bbe\u5b9a\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u7ed9\u7b97\u5b50\u53d6\u4e2a\u540d\uff0c\u518d\u5b9a\u4e49\u81ea\u5df1\u7684\u7b97\u5b50\u3002<\/p>\n\n\n\n<p>\u6211\u4eec<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzI4MDcxNTY2MQ==&amp;mid=2247488952&amp;idx=1&amp;sn=880d3ad47a8fb3eab56514135f0e643b&amp;chksm=ebb51d5adcc2944c276af19e8cff5e73c934f8811706be0a94c5f47f9e767c902939903e6b95&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noreferrer noopener\">\u5728\u7b2c\u4e00\u7bc7\u6559\u7a0b<\/a>\u8bb2\u8fc7\uff1aONNX \u662f\u4e00\u5957\u6807\u51c6\uff0c\u672c\u8eab\u4e0d\u5305\u62ec\u5b9e\u73b0\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u7b80\u7565\u5730\u5b9a\u4e49\u4e00\u4e2a ONNX \u53ef\u53d8\u5f62\u5377\u79ef\u7b97\u5b50\uff0c\u800c\u4e0d\u53bb\u5199\u5b83\u5728\u67d0\u4e2a\u63a8\u7406\u5f15\u64ce\u4e0a\u7684\u5b9e\u73b0\u3002\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u518d\u4ecb\u7ecd\u5728\u5404\u4e2a\u63a8\u7406\u5f15\u64ce\u4e2d\u6dfb\u52a0\u65b0 ONNX \u7b97\u5b50\u652f\u6301\u7684\u65b9\u6cd5\u3002\u6b64\u5904\uff0c\u6211\u4eec\u53ea\u5173\u5fc3\u5982\u4f55\u5bfc\u51fa\u4e00\u4e2a\u5305\u542b\u65b0 ONNX \u7b97\u5b50\u8282\u70b9\u7684 onnx \u6587\u4ef6\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u65b0\u7b97\u5b50\u7f16\u5199\u5982\u4e0b\u7b80\u5355\u7684\u7b26\u53f7\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@parse_args(\"v\", \"v\", \"v\", \"v\", \"v\", \"i\", \"i\", \"i\", \"i\", \"i\", \"i\", \"i\", \"i\", \"none\") \ndef symbolic(g,  \n        input, \n        weight, \n        offset, \n        mask, \n        bias, \n        stride_h, stride_w, \n        pad_h, pad_w, \n        dil_h, dil_w, \n        n_weight_grps, \n        n_offset_grps, \n        use_mask): \n    return g.op(\"custom::deform_conv2d\", input, offset) \n <\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u7b26\u53f7\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4ee5\u521a\u521a\u641c\u7d22\u5230\u7684\u7b97\u5b50\u8f93\u5165\u53c2\u6570\u4f5c\u4e3a\u7b26\u53f7\u51fd\u6570\u7684\u8f93\u5165\u53c2\u6570\uff0c\u5e76\u53ea\u7528&nbsp;<code>input<\/code>&nbsp;\u548c&nbsp;<code>offset<\/code>&nbsp;\u6765\u6784\u9020\u4e00\u4e2a\u7b80\u5355\u7684 ONNX \u7b97\u5b50\u3002<\/p>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6700\u4ee4\u4eba\u7591\u60d1\u7684\u5c31\u662f\u88c5\u9970\u5668&nbsp;<code>@parse_args<\/code>&nbsp;\u4e86\u3002\u7b80\u5355\u6765\u8bf4\uff0cTorchScript \u7b97\u5b50\u7684\u7b26\u53f7\u51fd\u6570\u8981\u6c42\u6807\u6ce8\u51fa\u6bcf\u4e00\u4e2a\u8f93\u5165\u53c2\u6570\u7684\u7c7b\u578b\u3002\u6bd4\u5982&#8221;v&#8221;\u8868\u793a Torch \u5e93\u91cc\u7684&nbsp;<code>value<\/code>&nbsp;\u7c7b\u578b\uff0c\u4e00\u822c\u7528\u4e8e\u6807\u6ce8\u5f20\u91cf\uff0c\u800c&#8221;i&#8221;\u8868\u793a int \u7c7b\u578b\uff0c&#8221;f&#8221;\u8868\u793a float \u7c7b\u578b\uff0c&#8221;none&#8221;\u8868\u793a\u8be5\u53c2\u6570\u4e3a\u7a7a\u3002\u5177\u4f53\u7684\u7c7b\u578b\u542b\u4e49\u53ef\u4ee5\u5728&nbsp;<code>torch.onnx.symbolic_helper.py<\/code>&nbsp;(<a href=\"https:\/\/github.com\/pytorch\/pytorch\/blob\/master\/torch\/onnx\/symbolic_helper.py\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/pytorch\/pytorch\/blob\/master\/torch\/onnx\/symbolic_helper.py<\/a>)\u4e2d\u67e5\u770b\u3002\u8fd9\u91cc\u8f93\u5165\u53c2\u6570\u4e2d\u7684&nbsp;<code>input, weight, offset, mask, bias<\/code>&nbsp;\u90fd\u662f\u5f20\u91cf\uff0c\u6240\u4ee5\u7528&#8221;v&#8221;\u8868\u793a\u3002\u540e\u9762\u7684\u5176\u4ed6\u53c2\u6570\u540c\u7406\u3002\u6211\u4eec\u4e0d\u5fc5\u7ea0\u7ed3\u4e8e&nbsp;<code>@parse_args<\/code>&nbsp;\u7684\u539f\u7406\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5bf9\u7b26\u53f7\u51fd\u6570\u7684\u53c2\u6570\u6807\u6ce8\u7c7b\u578b\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u6709\u4e86\u7b26\u53f7\u51fd\u6570\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u5f0f\u6ce8\u518c\u7b26\u53f7\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>register_custom_op_symbolic(\"torchvision::deform_conv2d\", symbolic, 9) <\/code><\/pre>\n\n\n\n<p>\u548c\u524d\u9762\u7684&nbsp;<code>register_op<\/code>&nbsp;\u7c7b\u4f3c\uff0c\u6ce8\u518c\u7b26\u53f7\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u8981\u8f93\u5165\u7b97\u5b50\u540d\u3001\u7b26\u53f7\u51fd\u6570\u3001\u7b97\u5b50\u96c6\u7248\u672c\u3002\u4e0e\u524d\u9762\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u91cc\u7684\u7b97\u5b50\u96c6\u7248\u672c\u662f\u6700\u65e9\u751f\u6548\u7248\u672c\uff0c\u5728\u8fd9\u91cc\u8bbe\u5b9a\u7248\u672c 9\uff0c\u610f\u5473\u7740\u4e4b\u540e\u7684\u7b2c 10 \u53f7\u3001\u7b2c 11 \u53f7\u2026\u2026\u7248\u672c\u96c6\u90fd\u80fd\u4f7f\u7528\u8fd9\u4e2a\u65b0\u7b97\u5b50\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5b8c\u6574\u7684\u6a21\u578b\u5bfc\u51fa\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch \nimport torchvision \n \nclass Model(torch.nn.Module): \n    def __init__(self): \n        super().__init__() \n        self.conv1 = torch.nn.Conv2d(3, 18, 3) \n        self.conv2 = torchvision.ops.DeformConv2d(3, 3, 3) \n \n    def forward(self, x): \n        return self.conv2(x, self.conv1(x)) \n \nfrom torch.onnx import register_custom_op_symbolic \nfrom torch.onnx.symbolic_helper import parse_args \n \n@parse_args(\"v\", \"v\", \"v\", \"v\", \"v\", \"i\", \"i\", \"i\", \"i\", \"i\", \"i\", \"i\", \"i\", \"none\") \ndef symbolic(g,  \n        input, \n        weight, \n        offset, \n        mask, \n        bias, \n        stride_h, stride_w, \n        pad_h, pad_w, \n        dil_h, dil_w, \n        n_weight_grps, \n        n_offset_grps, \n        use_mask): \n    return g.op(\"custom::deform_conv2d\", input, offset) \n \nregister_custom_op_symbolic(\"torchvision::deform_conv2d\", symbolic, 9) \n \nmodel = Model() \ninput = torch.rand(1, 3, 10, 10) \ntorch.onnx.export(model, input, 'dcn.onnx') \n <\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u6210\u529f\u8fd0\u884c\u7684\u8bdd\uff0c\u6211\u4eec\u5e94\u8be5\u80fd\u5f97\u5230\u5982\u4e0b\u7684 ONNX \u6a21\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic1.zhimg.com\/v2-8356402568ef65eca91e4b11b6d8dc78_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u7684 ONNX \u7b97\u5b50&nbsp;<code>deform_conv2d<\/code>&nbsp;\u5305\u542b\u4e86\u4e24\u4e2a\u8f93\u5165\uff0c\u4e00\u4e2a\u8f93\u51fa\uff0c\u548c\u6211\u4eec\u9884\u60f3\u5f97\u4e00\u6837\u3002<\/p>\n\n\n\n<h2 id=\"h_513387413_7\"><strong>\u4f7f\u7528&nbsp;<code>torch.autograd.Function<\/code><\/strong><\/h2>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u6765\u5b66\u4e60\u4e00\u79cd\u7b80\u5355\u7684\u4e3a PyTorch \u6dfb\u52a0 C++ \u7b97\u5b50\u5b9e\u73b0\u7684\u65b9\u6cd5\uff0c\u6765\u4ee3\u66ff\u8f83\u4e3a\u590d\u6742\u7684\u65b0\u589e TorchScript \u7b97\u5b50\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4f1a\u7528&nbsp;<code>torch.autograd.Function<\/code>&nbsp;\u5c01\u88c5\u8fd9\u4e2a\u65b0\u7b97\u5b50\u3002<code>torch.autograd.Function<\/code>&nbsp;\u80fd\u5b8c\u6210\u7b97\u5b50\u5b9e\u73b0\u548c\u7b97\u5b50\u8c03\u7528\u7684\u9694\u79bb\u3002\u4e0d\u7ba1\u7b97\u5b50\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u5b83\u5c01\u88c5\u540e\u7684\u4f7f\u7528\u4f53\u9a8c\u4ee5\u53ca ONNX \u5bfc\u51fa\u65b9\u6cd5\u4f1a\u548c\u539f\u751f\u7684 PyTorch \u7b97\u5b50\u4e00\u6837\u3002\u8fd9\u662f\u6211\u4eec\u6bd4\u8f83\u63a8\u8350\u7684\u4e3a\u7b97\u5b50\u6dfb\u52a0 ONNX \u652f\u6301\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u5e94\u5bf9\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u6765\u81ea\u5b9a\u4e49\u4e00\u4e2a\u5947\u602a\u7684&nbsp;<code>my_add<\/code>&nbsp;\u7b97\u5b50\u3002\u8fd9\u4e2a\u7b97\u5b50\u7684\u8f93\u5165\u5f20\u91cf&nbsp;<code>a, b<\/code>&nbsp;\uff0c\u8f93\u51fa&nbsp;<code>2a + b<\/code>&nbsp;\u7684\u503c\u3002\u6211\u4eec\u4f1a\u5148\u628a\u5b83\u5728 PyTorch \u4e2d\u5b9e\u73b0\uff0c\u518d\u628a\u5b83\u5bfc\u51fa\u5230 ONNX \u4e2d\u3002<\/p>\n\n\n\n<h3 id=\"h_513387413_8\"><strong>\u4e3a PyTorch \u6dfb\u52a0 C++ \u62d3\u5c55<\/strong><\/h3>\n\n\n\n<p>\u4e3a PyTorch \u6dfb\u52a0\u7b80\u5355\u7684 C++ \u62d3\u5c55\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\u3002\u5bf9\u4e8e\u6211\u4eec\u5b9a\u4e49\u7684&nbsp;<code>my_add<\/code>&nbsp;\u7b97\u5b50\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u7684 C++ \u6e90\u6587\u4ef6\u6765\u5b9e\u73b0\u3002\u6211\u4eec\u628a\u8be5\u6587\u4ef6\u547d\u540d\u4e3a &#8220;my_add.cpp&#8221;\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/ my_add.cpp \n<\/em><em><\/em> \n#include &lt;torch\/torch.h&gt; \n \ntorch::Tensor my_add(torch::Tensor a, torch::Tensor b) \n{ \n    return 2 * a + b; \n} \n \nPYBIND11_MODULE(my_lib, m) \n{ \n    m.def(\"my_add\", my_add); \n} \n<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e\u5728 PyTorch \u4e2d\u6dfb\u52a0 C++ \u62d3\u5c55\u548c\u6a21\u578b\u90e8\u7f72\u5173\u7cfb\u4e0d\u5927\uff0c\u8fd9\u91cc\u6211\u4eec\u4ec5\u7ed9\u51fa\u8fd9\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5e76\u4e0d\u5bf9\u5176\u539f\u7406\u505a\u8fc7\u591a\u8bb2\u89e3\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>torch::Tensor<\/code>&nbsp;\u5c31\u662f C++ \u4e2d torch \u7684\u5f20\u91cf\u7c7b\u578b\uff0c\u5b83\u7684\u52a0\u6cd5\u548c\u4e58\u6cd5\u7b49\u8fd0\u7b97\u7b26\u5747\u5df2\u91cd\u8f7d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u5bf9\u666e\u901a\u6807\u91cf\u4e00\u6837\u5bf9\u5f20\u91cf\u505a\u52a0\u6cd5\u548c\u4e58\u6cd5\u3002<\/p>\n\n\n\n<p>\u8f7b\u677e\u5730\u5b8c\u6210\u4e86\u7b97\u5b50\u7684\u5b9e\u73b0\u540e\uff0c\u6211\u4eec\u7528&nbsp;<code>PYBIND11_MODULE<\/code>&nbsp;\u6765\u4e3a C++ \u51fd\u6570\u63d0\u4f9b Python \u8c03\u7528\u63a5\u53e3\u3002\u8fd9\u91cc\u7684&nbsp;<code>my_lib<\/code>&nbsp;\u662f\u6211\u4eec\u672a\u6765\u8981\u5728 Python \u91cc\u5bfc\u5165\u7684\u6a21\u5757\u540d\u3002\u53cc\u5f15\u53f7\u4e2d\u7684&nbsp;<code>my_add<\/code>&nbsp;\u662f Python \u8c03\u7528\u63a5\u53e3\u7684\u540d\u79f0\uff0c\u8fd9\u91cc\u6211\u4eec\u5bf9\u9f50 C++ \u51fd\u6570\u7684\u540d\u79f0\uff0c\u4f9d\u7136\u7528 &#8220;my_add&#8221;\u8fd9\u4e2a\u540d\u5b57\u3002<\/p>\n\n\n\n<p>\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u5982\u4e0b\u7684 Python \u4ee3\u7801\u5e76\u547d\u540d\u4e3a &#8220;setup.py&#8221;\uff0c\u6765\u7f16\u8bd1\u521a\u521a\u7684 C++ \u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from setuptools import setup \nfrom torch.utils import cpp_extension \n \nsetup(name='my_add', \n      ext_modules=&#91;cpp_extension.CppExtension('my_lib', &#91;'my_add.cpp'])], \n      cmdclass={'build_ext': cpp_extension.BuildExtension}) <\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u4e86 Python \u7684 setuptools \u7f16\u8bd1\u529f\u80fd\u548c PyTorch \u7684 C++ \u62d3\u5c55\u5de5\u5177\u51fd\u6570\uff0c\u53ef\u4ee5\u7f16\u8bd1\u5305\u542b\u4e86 torch \u5e93\u7684 C++ \u6e90\u6587\u4ef6\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u586b\u5199\u7684\u53ea\u6709\u6a21\u5757\u540d\u548c\u6a21\u5757\u4e2d\u7684\u6e90\u6587\u4ef6\u540d\u3002\u6211\u4eec\u521a\u521a\u628a\u6a21\u5757\u547d\u540d\u4e3a&nbsp;<code>my_lib<\/code>\uff0c\u800c\u6e90\u6587\u4ef6\u53ea\u6709\u4e00\u4e2a&nbsp;<code>my_add.cpp<\/code>\uff0c\u56e0\u6b64\u62d3\u5c55\u6a21\u5757\u90a3\u4e00\u884c\u8981\u5199\u6210&nbsp;<code>ext_modules=[cpp_extension.CppExtension('my_lib', ['my_add.cpp'])],<\/code>\u3002<\/p>\n\n\n\n<p>\u4e4b\u540e\uff0c\u50cf\u5904\u7406\u666e\u901a\u7684 Python \u5305\u4e00\u6837\u6267\u884c\u5b89\u88c5\u547d\u4ee4\uff0c\u6211\u4eec\u7684 C++ \u4ee3\u7801\u5c31\u4f1a\u81ea\u52a8\u7f16\u8bd1\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python setup.py develop \n<\/code><\/pre>\n\n\n\n<h3 id=\"h_513387413_9\"><strong>\u7528&nbsp;<code>torch.autograd.Function<\/code>&nbsp;\u5c01\u88c5<\/strong><\/h3>\n\n\n\n<p>\u76f4\u63a5\u7528 Python \u63a5\u53e3\u8c03\u7528 C++ \u51fd\u6570\u4e0d\u592a\u201c\u7f8e\u89c2\u201d\uff0c\u4e00\u79cd\u6bd4\u8f83\u4f18\u96c5\u7684\u505a\u6cd5\u662f\u628a\u8fd9\u4e2a\u8c03\u7528\u63a5\u53e3\u5c01\u88c5\u8d77\u6765\u3002\u8fd9\u91cc\u6211\u4eec\u7528&nbsp;<code>torch.autograd.Function<\/code>&nbsp;\u6765\u5c01\u88c5\u7b97\u5b50\u7684\u5e95\u5c42\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch \nimport my_lib \nclass MyAddFunction(torch.autograd.Function): \n \n    @staticmethod \n    def forward(ctx, a, b): \n        return my_lib.my_add(a, b) \n \n    @staticmethod \n    def symbolic(g, a, b): \n        two = g.op(\"Constant\", value_t=torch.tensor(&#91;2])) \n        a = g.op('Mul', a, two) \n        return g.op('Add', a, b) <\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5728\u524d\u9762\u7684\u6559\u7a0b\u4e2d\u5df2\u7ecf\u89c1\u8fc7&nbsp;<code>torch.autograd.Function<\/code>\uff0c\u8fd9\u91cc\u6211\u4eec\u6b63\u5f0f\u5730\u5bf9\u5176\u505a\u4e00\u4e2a\u4ecb\u7ecd\u3002<code>Function<\/code>&nbsp;\u7c7b\u672c\u8eab\u8868\u793a PyTorch \u7684\u4e00\u4e2a\u53ef\u5bfc\u51fd\u6570\uff0c\u53ea\u8981\u4e3a\u5176\u5b9a\u4e49\u4e86\u524d\u5411\u63a8\u7406\u548c\u53cd\u5411\u4f20\u64ad\u7684\u5b9e\u73b0\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u5b83\u5f53\u6210\u4e00\u4e2a\u666e\u901a PyTorch \u51fd\u6570\u6765\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>PyTorch \u4f1a\u81ea\u52a8\u8c03\u5ea6\u8be5\u51fd\u6570\uff0c\u5408\u9002\u5730\u6267\u884c\u524d\u5411\u548c\u53cd\u5411\u8ba1\u7b97\u3002\u5bf9\u6a21\u578b\u90e8\u7f72\u6765\u8bf4\uff0c<code>Function<\/code>&nbsp;\u7c7b\u6709\u4e00\u4e2a\u5f88\u597d\u7684\u6027\u8d28\uff1a\u5982\u679c\u5b83\u5b9a\u4e49\u4e86&nbsp;<code>symbolic<\/code>&nbsp;\u9759\u6001\u65b9\u6cd5\uff0c\u8be5&nbsp;<code>Function<\/code>&nbsp;\u5728\u6267\u884c&nbsp;<code>torch.onnx.export()<\/code>&nbsp;\u65f6\u5c31\u53ef\u4ee5\u6839\u636e&nbsp;<code>symbolic<\/code>&nbsp;\u4e2d\u5b9a\u4e49\u7684\u89c4\u5219\u8f6c\u6362\u6210 ONNX \u7b97\u5b50\u3002\u8fd9\u4e2a&nbsp;<code>symbolic<\/code>&nbsp;\u5c31\u662f\u524d\u9762\u63d0\u5230\u7684\u7b26\u53f7\u51fd\u6570\uff0c\u53ea\u662f\u5b83\u7684\u540d\u79f0\u5fc5\u987b\u662f&nbsp;<code>symbolic<\/code>&nbsp;\u800c\u5df2\u3002<\/p>\n\n\n\n<p>\u5728&nbsp;<code>forward<\/code>&nbsp;\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u7528&nbsp;<code>my_lib.my_add(a, b)<\/code>&nbsp;\u5c31\u53ef\u4ee5\u8c03\u7528\u4e4b\u524d\u5199\u7684C++\u51fd\u6570\u4e86\u3002\u8fd9\u91cc&nbsp;<code>my_lib<\/code>&nbsp;\u662f\u5e93\u540d\uff0c<code>my_add<\/code>&nbsp;\u662f\u51fd\u6570\u540d\uff0c\u8fd9\u4e24\u4e2a\u540d\u5b57\u662f\u5728\u524d\u9762C++\u7684&nbsp;<code>PYBIND11_MODULE<\/code>&nbsp;\u4e2d\u5b9a\u4e49\u7684\u3002<\/p>\n\n\n\n<p>\u5728&nbsp;<code>symbolic<\/code>&nbsp;\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u7528&nbsp;<code>g.op()<\/code>&nbsp;\u5b9a\u4e49\u4e86\u4e09\u4e2a\u7b97\u5b50\uff1a\u5e38\u91cf\u3001\u4e58\u6cd5\u3001\u52a0\u6cd5\u3002\u8fd9\u91cc\u4e58\u6cd5\u548c\u52a0\u6cd5\u7684\u7528\u6cd5\u548c\u524d\u9762\u63d0\u5230\u7684&nbsp;<code>asinh<\/code>&nbsp;\u4e00\u6837\uff0c\u53ea\u9700\u8981\u6839\u636e ONNX \u7b97\u5b50\u5b9a\u4e49\u89c4\u5219\u628a\u8f93\u5165\u53c2\u6570\u586b\u5165\u5373\u53ef\u3002\u800c\u5728\u5b9a\u4e49\u5e38\u91cf\u7b97\u5b50\u65f6\uff0c\u6211\u4eec\u8981\u628a PyTorch \u5f20\u91cf\u7684\u503c\u4f20\u5165&nbsp;<code>value_t<\/code>&nbsp;\u53c2\u6570\u4e2d\u3002<\/p>\n\n\n\n<p>\u5728 ONNX \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u628a\u65b0\u5efa\u5e38\u91cf\u5f53\u6210\u4e00\u4e2a\u7b97\u5b50\u6765\u770b\u5f85\uff0c\u5c3d\u7ba1\u8fd9\u4e2a\u7b97\u5b50\u5e76\u4e0d\u4f1a\u4ee5\u8282\u70b9\u7684\u5f62\u5f0f\u51fa\u73b0\u5728 ONNX \u6a21\u578b\u7684\u53ef\u89c6\u5316\u7ed3\u679c\u91cc\u3002<\/p>\n\n\n\n<p>\u628a\u7b97\u5b50\u5c01\u88c5\u6210&nbsp;<code>Function<\/code>&nbsp;\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u628a&nbsp;<code>my_add<\/code>\u7b97\u5b50\u7528\u8d77\u6765\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_add = MyAddFunction.apply \n \nclass MyAdd(torch.nn.Module): \n    def __init__(self): \n        super().__init__() \n \n    def forward(self, a, b): \n        return my_add(a, b) <\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4efd\u4ee3\u7801\u91cc\uff0c\u6211\u4eec\u5148\u7528&nbsp;<code>my_add = MyAddFunction.apply<\/code>&nbsp;\u83b7\u53d6\u4e86\u4e00\u4e2a\u5947\u602a\u7684\u53d8\u91cf\u3002\u8fd9\u4e2a\u53d8\u91cf\u662f\u7528\u6765\u505a\u4ec0\u4e48\u7684\u5462\uff1f\u5176\u5b9e\uff0c<code>apply<\/code>\u662f<code>torch.autograd.Function<\/code>&nbsp;\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5b8c\u6210\u4e86&nbsp;<code>Function<\/code>&nbsp;\u5728\u524d\u5411\u63a8\u7406\u6216\u8005\u53cd\u5411\u4f20\u64ad\u65f6\u7684\u8c03\u5ea6\u3002\u6211\u4eec\u5728\u4f7f\u7528&nbsp;<code>Function<\/code>&nbsp;\u7684\u6d3e\u751f\u7c7b\u505a\u63a8\u7406\u65f6\uff0c\u4e0d\u5e94\u8be5\u663e\u5f0f\u5730\u8c03\u7528&nbsp;<code>forward()<\/code>\uff0c\u800c\u5e94\u8be5\u8c03\u7528\u5176&nbsp;<code>apply<\/code>&nbsp;\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528&nbsp;<code>my_add = MyAddFunction.apply<\/code>&nbsp;\u628a\u8fd9\u4e2a\u8c03\u7528\u65b9\u6cd5\u53d6\u4e86\u4e00\u4e2a\u66f4\u7b80\u77ed\u7684\u522b\u540d&nbsp;<code>my_add<\/code>\u3002\u4ee5\u540e\u5728\u4f7f\u7528&nbsp;<code>my_add<\/code>&nbsp;\u7b97\u5b50\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u5ffd\u7565&nbsp;<code>MyAddFunction<\/code>&nbsp;\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u800c\u53ea\u901a\u8fc7&nbsp;<code>my_add<\/code>&nbsp;\u8fd9\u4e2a\u63a5\u53e3\u6765\u8bbf\u95ee\u7b97\u5b50\u3002\u8fd9\u91cc&nbsp;<code>my_add<\/code>&nbsp;\u7684\u5730\u4f4d\uff0c\u548c PyTorch \u7684&nbsp;<code>asinh<\/code>,&nbsp;<code>interpolate<\/code>,&nbsp;<code>conv2d<\/code>\u7b49\u539f\u751f\u51fd\u6570\u662f\u7c7b\u4f3c\u7684\u3002<\/p>\n\n\n\n<p>\u6709\u4e86\u8bbf\u95ee\u65b0\u7b97\u5b50\u7684\u63a5\u53e3\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u628a\u7b97\u5b50\u5c01\u88c5\u6210\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u4e2d\u7684\u8ba1\u7b97\u5c42\u3002\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u53eb\u505a\u7684&nbsp;<code>MyAdd<\/code>&nbsp;\u7684&nbsp;<code>torch.nn.Module<\/code>\uff0c\u5b83\u5c01\u88c5\u4e86<code>my_add<\/code>\uff0c\u5c31\u548c\u5c01\u88c5\u4e86<code>conv2d<\/code>&nbsp;\u7684&nbsp;<code>torch.nn.Conv2d<\/code>&nbsp;\u4e00\u6837\u3002<\/p>\n\n\n\n<h3 id=\"h_513387413_10\"><strong>\u6d4b\u8bd5\u7b97\u5b50<\/strong><\/h3>\n\n\n\n<p>\u8d39\u4e86\u597d\u5927\u7684\u529f\u592b\u6765\u201c\u5305\u88c5\u201d\u6211\u4eec\u7684\u65b0\u7b97\u5b50\u540e\uff0c\u6211\u4eec\u7ec8\u4e8e\u53ef\u4ee5\u6765\u4f7f\u7528\u5b83\u4e86\u3002\u548c\u4e4b\u524d\u7684\u6d4b\u8bd5\u6d41\u7a0b\u4e00\u6837\uff0c\u8ba9\u6211\u4eec\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u5bfc\u51fa\u4e00\u4e2a\u5305\u542b\u65b0\u7b97\u5b50\u7684 ONNX \u6a21\u578b\uff0c\u5e76\u9a8c\u8bc1\u4e00\u4e0b\u5b83\u662f\u5426\u6b63\u786e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = MyAdd() \ninput = torch.rand(1, 3, 10, 10) \ntorch.onnx.export(model, (input, input), 'my_add.onnx') \ntorch_output = model(input, input).detach().numpy() \n \nimport onnxruntime \nimport numpy as np \nsess = onnxruntime.InferenceSession('my_add.onnx') \nort_output = sess.run(None, {'a': input.numpy(), 'b': input.numpy()})&#91;0] \n \nassert np.allclose(torch_output, ort_output) <\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4efd\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u628a&nbsp;<code>MyAdd<\/code>&nbsp;\u4f5c\u4e3a\u8981\u5bfc\u51fa\u7684\u6a21\u578b\u3002\u6211\u4eec\u8ba1\u7b97\u4e86\u4e00\u4e2a PyTorch \u6a21\u578b\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u53c8\u5bfc\u51fa ONNX \u6a21\u578b\uff0c\u8ba1\u7b97\u4e86 ONNX \u6a21\u578b\u5728 ONNX Runtime \u4e0a\u7684\u8fd0\u7b97\u7ed3\u679c\u3002\u5982\u679c\u4e00\u5207\u6b63\u5e38\u7684\u8bdd\uff0c\u8fd9\u4e24\u4e2a\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u4efd\u4ee3\u7801\u4e0d\u4f1a\u62a5\u4efb\u4f55\u9519\u8bef\uff0c\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic2.zhimg.com\/80\/v2-b835abf0dabff9f7fe67db77839e1745_720w.webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u53ef\u89c6\u5316\u4e00\u4e0b&nbsp;<code>my_add.onnx<\/code>\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c\u548c\u6211\u4eec\u8bbe\u8ba1\u5f97\u4e00\u6837\uff0c<code>my_add<\/code>&nbsp;\u7b97\u5b50\u88ab\u7ffb\u8bd1\u6210\u4e86\u4e24\u4e2a ONNX \u7b97\u5b50\u8282\u70b9\uff08\u5176\u4e2d\u5e38\u91cf\u7b97\u5b50\u88ab\u653e\u5165\u4e86&nbsp;<code>Mul<\/code>&nbsp;\u7684\u53c2\u6570\u4e2d\uff09\u3002<\/p>\n\n\n\n<p>\u6574\u7406\u4e00\u4e0b\uff0c\u6574\u4e2a\u6d41\u7a0b\u7684 Python \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch \nimport my_lib \nclass MyAddFunction(torch.autograd.Function): \n \n    @staticmethod \n    def forward(ctx, a, b): \n        return my_lib.my_add(a, b) \n \n    @staticmethod \n    def symbolic(g, a, b): \n        two = g.op(\"Constant\", value_t=torch.tensor(&#91;2])) \n        a = g.op('Mul', a, two) \n        return g.op('Add', a, b) \n \nmy_add = MyAddFunction.apply \n \nclass MyAdd(torch.nn.Module): \n    def __init__(self): \n        super().__init__() \n \n    def forward(self, a, b): \n        return my_add(a, b) \n \nmodel = MyAdd() \ninput = torch.rand(1, 3, 10, 10) \ntorch.onnx.export(model, (input, input), 'my_add.onnx') \ntorch_output = model(input, input).detach().numpy() \n \nimport onnxruntime \nimport numpy as np \nsess = onnxruntime.InferenceSession('my_add.onnx') \nort_output = sess.run(None, {'a': input.numpy(), 'b': input.numpy()})&#91;0] \n \nassert np.allclose(torch_output, ort_output) <\/code><\/pre>\n\n\n\n<h2 id=\"h_513387413_11\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5728\u8fd9\u7bc7\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u56f4\u7ed5\u201c\u4e3a ATen \u7b97\u5b50\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570\u201d\u3001\u201c\u4e3a TorchScript \u7b97\u5b50\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570\u201d\u3001\u201c\u5c01\u88c5\u6210&nbsp;<code>torch.autograd.Function<\/code>&nbsp;\u5e76\u6dfb\u52a0\u7b26\u53f7\u51fd\u6570\u201d\u8fd9\u4e09\u79cd\u6dfb\u52a0\u6620\u5c04\u5173\u7cfb\u7684\u65b9\u6cd5\uff0c\u8bb2\u89e3\u4e86 3 \u4e2a\u4e3a PyTorch \u548c ONNX \u6dfb\u52a0\u652f\u6301\u7684\u5b9e\u4f8b\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b66\u5230\u4e86\u5f88\u591a\u96f6\u6563\u7684\u77e5\u8bc6\uff0c\u6765\u603b\u7ed3\u4e00\u4e0b\u5427\u3002<\/p>\n\n\n\n<ul><li>ATen \u662f PyTorch \u7684 C++ \u5f20\u91cf\u8fd0\u7b97\u5e93\u3002\u901a\u8fc7\u67e5\u8be2&nbsp;<code>torch\/_C\/_VariableFunctions.pyi<\/code>&nbsp;\u548c&nbsp;<code>torch\/nn\/functional.pyi<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053 ATen \u7b97\u5b50\u7684 Python \u63a5\u53e3\u5b9a\u4e49\u3002<\/li><li>\u7528&nbsp;<code>register_op<\/code>&nbsp;\u53ef\u4ee5\u4e3a ATen \u7b97\u5b50\u8865\u5145\u6ce8\u518c\u7b26\u53f7\u51fd\u6570<\/li><li>\u7528&nbsp;<code>register_custom_op_symbolic<\/code>&nbsp;\u53ef\u4ee5\u4e3a TorchScript \u7b97\u5b50\u8865\u5145\u6ce8\u518c\u7b26\u53f7\u51fd\u6570<\/li><li>\u5982\u4f55\u5728 PyTorch \u91cc\u6dfb\u52a0 C++ \u62d3\u5c55<\/li><li>\u5982\u4f55\u7528&nbsp;<code>torch.autograd.Function<\/code>&nbsp;\u5c01\u88c5\u4e00\u4e2a\u81ea\u5b9a\u4e49 PyTorch \u7b97\u5b50<\/li><li>\u5982\u4f55\u7f16\u5199\u7b26\u53f7\u51fd\u6570&nbsp;<code>symbolic(g, ...)<\/code>\u3002<\/li><li>\u5982\u4f55\u7528&nbsp;<code>g.op()<\/code>&nbsp;\u628a\u4e00\u4e2a PyTorch \u7b97\u5b50\u6620\u5c04\u6210\u4e00\u4e2a\u6216\u591a\u4e2a ONNX \u7b97\u5b50\uff0c\u6216\u8005\u662f\u81ea\u5b9a\u4e49\u7684 ONNX \u7b97\u5b50\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u4e86 PyTorch \u8f6c ONNX \u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u53d1\u73b0 PyTorch \u5bf9 ONNX \u7684\u652f\u6301\u8fd8\u4e0d\u9519\u3002\u4f46\u5728\u5b9e\u9645\u7684 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2023\/02\/01\/pytorch-onnx-1\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">PyTorch \u4e2d\u652f\u6301\u66f4\u591a ONNX \u7b97\u5b50<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,4,26],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/12649"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=12649"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/12649\/revisions"}],"predecessor-version":[{"id":12652,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/12649\/revisions\/12652"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=12649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=12649"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=12649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}