{"id":55,"date":"2023-12-24T11:06:26","date_gmt":"2023-12-24T03:06:26","guid":{"rendered":"http:\/\/blog.thirdbody.cn\/?p=55"},"modified":"2023-12-24T11:11:43","modified_gmt":"2023-12-24T03:11:43","slug":"%e5%a6%82%e4%bd%95%e5%b0%86pytorch%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%88%b0open-neural-network-exchangeonnx%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"http:\/\/blog.thirdbody.cn\/index.php\/2023\/12\/24\/%e5%a6%82%e4%bd%95%e5%b0%86pytorch%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%88%b0open-neural-network-exchangeonnx%ef%bc%88%e4%ba%8c%ef%bc%89\/","title":{"rendered":"\u5982\u4f55\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u5230Open Neural Network Exchange(ONNX)\uff08\u4e8c\uff09"},"content":{"rendered":"<p><a href=\"http:\/\/blog.thirdbody.cn\/index.php\/2023\/12\/20\/%e5%a6%82%e4%bd%95%e5%b0%86pytorch%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%88%b0open-neural-network-exchangeonnx%ef%bc%88%e4%b8%80%ef%bc%89\/\">\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d<\/a>\u6211\u4eec\u4ecb\u7ecd\u4e86\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u5230ONNX(Open Neural Network Exchange)\u683c\u5f0f\u7684\u4e00\u822c\u6b65\u9aa4\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c06\u5f00\u59cb\u7cfb\u7edf\u4ecb\u7ecdtorch\u683c\u5f0f\u6a21\u578b\u8f6c\u6362\u6210ONNX\u7684\u539f\u7406\u3002<\/p>\n<h3>1. PyTorch\u6a21\u578b\u7684\u5e8f\u5217\u5316\u2014\u2014TorchScript<\/h3>\n<p>TorchScirpt\u662f\u4e00\u79cd\u5e8f\u5217\u5316\u548c\u4f18\u5316PyTorch\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u4f1a\u5c06\u4e00\u4e2a<code>torch.nn.Module<\/code>\u6a21\u578b\u8f6c\u6362\u4e3a<code>torch.jit.ScriptModel<\/code>\u683c\u5f0f\u7684\u6a21\u578b\uff0c<a href=\"http:\/\/blog.thirdbody.cn\/index.php\/2023\/12\/20\/%e5%a6%82%e4%bd%95%e5%b0%86pytorch%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%88%b0open-neural-network-exchangeonnx%ef%bc%88%e4%b8%80%ef%bc%89\/\">\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d<\/a>\u4ecb\u7ecd\u7684PyTorch\u5185\u90e8\u7684\u8f6c\u6362ONNX\u7684\u63a5\u53e3<code>torch.onnx.export<\/code>\u5b9e\u9645\u4e0a\u6240\u4f20\u5165\u7684\u5c31\u662f\u4e00\u4e2a<code>torch.jit.ScriptModel<\/code>\u683c\u5f0f\u7684\u6a21\u578b(\u4e0a\u4e00\u8282\u4e2d\u4e4b\u6240\u4ee5\u76f4\u63a5\u4f20\u5165<code>torch.nn.module<\/code>\uff0c\u662f\u56e0\u4e3a<code>torch.onnx.export<\/code>\u63a5\u53e3\u4f1a\u9ed8\u8ba4\u5c06<code>torch.nn.module<\/code>\u9690\u5f0f\u7684\u8f6c\u6362\u4e3a<code>torch.jit.ScriptModel<\/code>)\u3002<\/p>\n<p><strong>TorchScript\u6709\u4e24\u79cd\u6a21\u5f0f\uff1a<code>torch.jit.trace<\/code>\uff08\u8ddf\u8e2a\u6a21\u5f0f\uff09\u3001<code>torch.jit.script<\/code>(\u8bb0\u5f55\u6a21\u5f0f)\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u9700\u8981\u9009\u62e9\u5176\u4e2d\u4e00\u79cd<\/strong><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/onnx21.jpg\" alt=\"\" \/><\/p>\n<h4>1.1 torch.jit.trace<\/h4>\n<p>\u8ddf\u8e2a\u6a21\u5f0f\u662f<code>torch.onnx.export<\/code>\u7684\u9ed8\u8ba4\u6a21\u5f0f\uff0c\u5728\u4f7f\u7528\u8be5\u6a21\u5f0f\u65f6\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u8f93\u5165\u5bf9torch\u6a21\u578b\u8fdb\u884c\u4e00\u6b21\u63a8\u7406\uff0c\u5e76\u8bb0\u5f55\u6574\u4e2a\u7684\u8ba1\u7b97\u8fc7\u7a0b(\u4e3b\u8981\u5305\u542b\u7528\u5230\u7684\u7b97\u5b50\u3001\u8f93\u5165\u8f93\u51fa\u7684\u5f62\u72b6\u7b49\u4fe1\u606f)\u3002\u5373\u8ddf\u8e2a\u6a21\u5f0f\u5bfc\u51fa\u7684\u662f\u6a21\u578b\u7684\u9759\u6001\u56fe\uff0c\u65e0\u6cd5\u8ffd\u8e2a\u6a21\u578b\u7684\u63a7\u5236\u6d41(\u6761\u4ef6\u5206\u652f\u548c\u5faa\u73af)\uff0c\u5bf9\u4e8e\u6a21\u578b\u4e2d\u7684\u6761\u4ef6\u5206\u652f\uff0c\u8ddf\u8e2a\u6a21\u5f0f\u53ea\u80fd\u8bb0\u5f55\u7ed9\u5b9a\u7684\u8f93\u5165\u6240\u6267\u884c\u7684\u5206\u652f\uff0c\u5bf9\u4e8e\u5faa\u73af\u63a7\u5236\uff0c\u4e5f\u53ea\u80fd\u8bb0\u5f55\u7ed9\u5b9a\u7684\u8f93\u5165\u6267\u884c\u7684\u5faa\u73af\u6b21\u6570\u3002<\/p>\n<pre><code class=\"language-python\"># \u8ddf\u8e2a\u6a21\u5f0f\u5bfc\u51fa\u6a21\u578b\uff0c\u9700\u8981\u6267\u884c\u63a8\u7406\nmodel_trace = torch.jit.trace(lm, dummy_input)\ntorch.onnx.export(model_trace, dummy_input, &quot;.\/models\/model_trace.onnx&quot;, output_names=[&#039;output&#039;], input_names=[&#039;input&#039;])\n\ntrace_model_session = ort.InferenceSession(&quot;.\/models\/model_trace.onnx&quot;)\ntrace_ort_output = trace_model_session.run([&#039;output&#039;],\n                                      {&#039;input&#039;: dummy_input.numpy()})\n\nnp.allclose(dummy_output.detach().numpy(), trace_ort_output)<\/code><\/pre>\n<h4>1.2 torch.jit.script<\/h4>\n<p>\u8bb0\u5f55\u6a21\u5f0f\u4f1a\u5bf9\u7ed9\u5b9a\u7684torch\u6a21\u578b\u8fdb\u884c\u89e3\u6790\uff0c\u771f\u6b63<code>\u7406\u89e3<\/code>torch\u6a21\u578b\u7684\u63a7\u5236\u6d41\uff0c\u5b83\u5bfc\u51fa\u7684\u662f\u52a8\u6001\u56fe\uff0c\u6240\u4ee5\u5728\u5bfc\u51fa\u65f6\u4e0d\u9700\u8981\u5728torch\u6a21\u578b\u4e0a\u6267\u884c\u63a8\u7406\u3002\u4e0e<code>torch.jit.trace<\/code>\u76f8\u6bd4\uff0c\u52a8\u6001\u56fe\u80fd\u591f\u66f4\u597d\u7684\u652f\u6301\u5e26\u6709\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u7684\u6a21\u578b\uff0c\u4f46\u4ee3\u4ef7\u662f\u63a8\u7406\u901f\u5ea6\u76f8\u8f83\u4e8e\u524d\u8005\u6709\u6240\u964d\u4f4e\u3002\u5728\u5b9e\u9645\u7684\u6a21\u578b\u90e8\u7f72\u5de5\u4f5c\u4e2d\uff0c\u5c3d\u91cf\u4f7f\u7528\u8ffd\u8e2a\u6a21\u5f0f\uff0c\u5f53\u8ffd\u8e2a\u6a21\u5f0f\u89e3\u51b3\u4e0d\u4e86\u5b9e\u9645\u95ee\u9898\u65f6\uff0c\u518d\u8003\u8651\u4f7f\u7528\u8bb0\u5f55\u6a21\u5f0f\u3002<\/p>\n<pre><code class=\"language-python\"># \u8bb0\u5f55\u6a21\u5f0f\u5bfc\u51fa\u6a21\u578b\uff0c\u4e0d\u9700\u8981\u6267\u884c\u63a8\u7406\nmodel_script = torch.jit.script(lm)\ntorch.onnx.export(model_script, dummy_input, &quot;.\/models\/model_script.onnx&quot;, output_names=[&#039;output&#039;], input_names=[&#039;input&#039;])\n\nscript_model_session = ort.InferenceSession(&quot;.\/models\/model_script.onnx&quot;)\nscript_ort_output = script_model_session.run([&#039;output&#039;],\n                                      {&#039;input&#039;: dummy_input.numpy()})\n\nnp.allclose(dummy_output.detach().numpy(), script_ort_output)<\/code><\/pre>\n<h3>2. torch.onnx.export\u63a5\u53e3<\/h3>\n<p>\u4ee5\u4e0b\u662f<code>torch.onnx.export<\/code>\u51fd\u6570\u7684\u7b7e\u540d\uff1a<\/p>\n<pre><code class=\"language-python\">def export(model, args, f, export_params=True, verbose=False, training=TrainingMode.EVAL, \n           input_names=None, output_names=None, aten=False, export_raw_ir=False, \n           operator_export_type=None, opset_version=None, _retain_param_name=True, \n           do_constant_folding=True, example_outputs=None, strip_doc_string=True, \n           dynamic_axes=None, keep_initializers_as_inputs=None, custom_opsets=None, \n           enable_onnx_checker=True, use_external_data_format=False): <\/code><\/pre>\n<p>\u4e00\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u53c2\u6570\u4ecb\u7ecd\uff1a<\/p>\n<ul>\n<li>\n<p><code>model<\/code>\uff1a\u5f85\u8f6c\u6362\u7684<code>TorchScript<\/code>\u6a21\u578b\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165<code>torch.nn.Module<\/code>\uff0c\u8be5\u63a5\u53e3\u4f1a\u5c06\u5176\u9690\u5f0f\u8f6c\u6362\u4e3a<code>TorchScript<\/code>(<code>torch.jit.trace<\/code>\u6a21\u5f0f)<\/p>\n<\/li>\n<li>\n<p><code>args<\/code>\uff1a\u6a21\u578b\u7684\u8f93\u5165<\/p>\n<\/li>\n<li>\n<p><code>f<\/code>\uff1a\u8f6c\u6362\u540eONNX\u6a21\u578b\u7684\u5b58\u50a8\u8def\u5f84<\/p>\n<\/li>\n<li>\n<p><code>export_params<\/code>\uff1a\u662f\u5426\u5bfc\u51fa\u6a21\u578b\u53c2\u6570\uff0c\u82e5\u4e3a<code>False<\/code>\uff0c\u5219\u53ea\u5bfc\u51fa\u6a21\u578b\u7ed3\u6784\uff0c\u9ed8\u8ba4\u4e3a<code>True<\/code>\uff0c\u5f53\u5728\u4e0d\u540c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e2d\u4f20\u9012\u6a21\u578b\u65f6\uff0c\u53ef\u80fd\u53ea\u9700\u8981\u6a21\u578b\u7ed3\u6784\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u6b64\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>False<\/code><\/p>\n<\/li>\n<li>\n<p><code>input_names<\/code>\uff1a\u8f93\u5165\u7684\u53d8\u91cf\u540d\uff0c\u540e\u7eed\u63a8\u7406\u4ee5\u53ca\u4f7f\u7528\u63a8\u7406\u5f15\u64ce\u52a0\u8f7d\u65f6\u9700\u8981\u4f7f\u7528\uff0c\u5f53\u4e0d\u8bbe\u7f6e\u6b64\u53c2\u6570\u65f6\uff0cAPI\u4f1a\u81ea\u52a8\u751f\u6210\u7b80\u5355\u7684\u540d\u79f0<\/p>\n<\/li>\n<li>\n<p><code>output_names<\/code>\uff1a\u8f93\u51fa\u7684\u53d8\u91cf\u540d\uff0c\u4f5c\u7528\u540c\u4e0a<\/p>\n<\/li>\n<li>\n<p><code>opset_version<\/code>\uff1a\u4f7f\u7528\u7684ONNX\u7b97\u5b50\u96c6\u7248\u672c<\/p>\n<\/li>\n<li>\n<p><code>dynamic_axes<\/code>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cexport\u63a5\u53e3\u7684\u8f93\u5165\u8f93\u51fa\u7684\u7ef4\u5ea6\u3001\u5927\u5c0f\u90fd\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6709\u4e9b\u7ef4\u5ea6\u7684\u5927\u5c0f\u662f\u52a8\u6001\u7684\uff0c\u5982\uff1a\u6279\u6b21\u5927\u5c0f\u7ef4\u5ea6batch_size\uff0c\u6b64\u65f6\u5219\u9700\u8981\u6307\u5b9a\u6b64\u53c2\u6570\uff0c\u5177\u4f53\u7528\u6cd5\u5c06\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\u4ecb\u7ecd\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u5230ONNX(Open Neural Network Exchan [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":56,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,24],"tags":[27,26,28],"class_list":["post-55","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-model_deployment","category-deep_learning","tag-onnx","tag-pytorch","tag-28"],"_links":{"self":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":2,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":58,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/55\/revisions\/58"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media\/56"}],"wp:attachment":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}