{"id":10020976,"date":"2025-08-27T21:23:00","date_gmt":"2025-08-27T13:23:00","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020976"},"modified":"2025-08-27T21:23:00","modified_gmt":"2025-08-27T13:23:00","slug":"%e3%80%90tvm-%e6%95%99%e7%a8%8b%e3%80%91%e5%90%91-tvm-%e4%b8%ad%e6%b7%bb%e5%8a%a0-codegen","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020976","title":{"rendered":"\u3010TVM \u6559\u7a0b\u3011\u5411 TVM \u4e2d\u6dfb\u52a0 Codegen"},"content":{"rendered":"<p>Apache TVM \u662f\u4e00\u4e2a\u6df1\u5ea6\u7684\u6df1\u5ea6\u5b66\u4e60\u7f16\u8bd1\u6846\u67b6\uff0c\u9002\u7528\u4e8e CPU\u3001GPU \u548c\u5404\u79cd\u673a\u5668\u5b66\u4e60\u52a0\u901f\u82af\u7247\u3002\u66f4\u591a TVM \u4e2d\u6587\u6587\u6863\u53ef\u8bbf\u95ee \u2192<a href=\"https:\/\/link.segmentfault.com\/?enc=pxFusIZ37yFviSrpOTBMQA%3D%3D.HIJ49hp5QfhHTUZXZxoS7u%2BlhsYRCjjg2yN9D4Z7BmY%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/tvm.hyper.ai\/<\/a><\/p>\n<p>\u968f\u7740\u6df1\u5ea6\u5b66\u4e60\u5de5\u4f5c\u8d1f\u8f7d\u6240\u9488\u5bf9\u7684\u786c\u4ef6\u8bbe\u5907\u6570\u91cf\u4e0d\u65ad\u589e\u52a0\uff0c\u7528\u6237\u5728\u5404\u79cd\u8bbe\u5907\u4e0a\u5b9e\u73b0\u9ad8\u6027\u80fd\u6240\u9700\u7684\u77e5\u8bc6\u4e5f\u5728\u4e0d\u65ad\u589e\u52a0\u3002\u4e3a\u4e86\u8ba9\u6570\u636e\u79d1\u5b66\u5bb6\u5728\u5f00\u53d1\u65b0\u6a21\u578b\u65f6\u4e0d\u5fc5\u62c5\u5fc3\u6027\u80fd\u95ee\u9898\uff0c\u786c\u4ef6\u5382\u5546\u6216\u662f\u57fa\u4e8e\u4e00\u4e9b\u5e38\u89c1\u7684\u6df1\u5ea6\u5b66\u4e60\u7b97\u5b50\uff0c\u63d0\u4f9b MKLDNN \u6216 cuDNN \u7b49\u5e93\uff0c\u6216\u662f\u63d0\u4f9b TensorRT \u7b49\u6846\u67b6\uff0c\u8ba9\u7528\u6237\u6309\u7167\u67d0\u79cd\u65b9\u5f0f\u63cf\u8ff0\u6a21\u578b\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<p>\u7136\u800c\uff0c\u7528\u6237\u5728\u5c1d\u8bd5\u4f7f\u7528\u65b0\u7684\u5e93\u6216\u8bbe\u5907\u65f6\uff0c\u5fc5\u987b\u5b66\u4e60\u65b0\u7684\u7f16\u7a0b\u63a5\u53e3\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u7edf\u4e00\u7684\u7f16\u7a0b\u63a5\u53e3\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\uff1a1\uff09\u8ba9\u6240\u6709\u7528\u6237\u53ca\u786c\u4ef6\u5382\u5546\u4fe1\u606f\u540c\u6b65\uff0c2\uff09\u63d0\u4f9b\u4e00\u4e2a\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u8ba9\u7279\u5b9a\u786c\u4ef6\u6216\u5e93\u53ea\u652f\u6301\u5177\u6709\u6781\u9ad8\u6027\u80fd\u7684\u3001\u5e7f\u6cdb\u4f7f\u7528\u7684\u7b97\u5b50\uff0c\u4e0d\u53d7\u652f\u6301\u7684\u7b97\u5b50\u5219\u56de\u9000\u5230 CPU\/GPU \u7b49\u901a\u7528\u8bbe\u5907\u3002<\/p>\n<p>\u672c\u5f00\u53d1\u624b\u518c\u6f14\u793a\u4e86\u786c\u4ef6\u5382\u5546\u5982\u4f55\u8f7b\u677e\u5b9e\u73b0\u81ea\u5df1\u7684 Codegen\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u4e3a Relay \u540e\u7aef\u7f16\u8bd1\u5668\uff0c\u4ece\u800c\u652f\u6301\u81ea\u5df1\u7684\u786c\u4ef6\u8bbe\u5907\/\u5e93\u3002\u672c\u624b\u518c\u6db5\u76d6\u4e86\u4e24\u79cd\u57fa\u4e8e\u4e0d\u540c\u8ba1\u7b97\u56fe\u7684 codegen\uff1a<\/p>\n<p><strong>1. \u5e0c\u671b\u751f\u6210 C \u4ee3\u7801\u3002<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u7684\u786c\u4ef6\u5df2\u7ecf\u5177\u5907\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4f18\u5316\u7684 C\/C++ \u5e93\uff0c\u5982\u5bf9\u4e8e CPU \u800c\u8a00\u7684 Intel CBLAS\/MKL \u5e93\uff0c\u6216\u9488\u5bf9 GPU \u800c\u8a00\u7684 NVIDIA CUBLAS \u5e93\uff0c\u90a3\u4e48\u672c\u8282\u5185\u5bb9\u975e\u5e38\u9002\u5408\u4f60\u3002\u5e78\u8fd0\u7684\u662f\uff0cC \u6e90\u4ee3\u7801\u6a21\u5757\u4e0e TVM runtime \u6a21\u5757\u5b8c\u5168\u517c\u5bb9\uff0c\u8fd9\u610f\u5473\u7740\u751f\u6210\u7684\u4ee3\u7801\u53ef\u4ee5\u7531\u4efb\u4f55\u5177\u6709\u9002\u5f53\u7f16\u8bd1\u6807\u5fd7\u7684 C\/C++ \u7f16\u8bd1\u5668\u7f16\u8bd1\uff0c\u56e0\u6b64\u7528\u6237\u53ea\u9700\u5b9e\u73b0\u4e00\u4e2a\u80fd\u4e3a\u5b50\u56fe\u751f\u6210 C \u4ee3\u7801\u7684 codegen\uff0c\u5e76\u5c06 C \u6e90\u4ee3\u7801\u6a21\u5757\u96c6\u6210\u5230 TVM runtime \u6a21\u5757\u4e2d\u3002\u4e0b\u4e00\u8282\u5185\u5bb9\u8bb2\u8be6\u7ec6\u6f14\u793a\u5982\u4f55\u4e3a\u786c\u4ef6\u5b9e\u73b0 C codegen\u3002<\/p>\n<p><strong>2. \u5e0c\u671b\u751f\u6210\u4efb\u610f\u8ba1\u7b97\u56fe\u3002<\/strong><\/p>\n<p>\u6709\u65f6\u5019\uff0c\u786c\u4ef6\u53ef\u80fd\u9700\u8981\u5176\u4ed6\u5f62\u5f0f\u7684\u8ba1\u7b97\u56fe\u5982 JSON\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u4e0d\u4ec5\u8981\u5b9e\u73b0\u4e00\u4e2a codegen\uff0c\u8fd8\u8981\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 TVM runtime \u6a21\u5757\uff0c\u4ece\u800c\u4f7f\u5f97 TVM runtime \u77e5\u9053\u5982\u4f55\u6267\u884c\u8fd9\u4e2a\u8ba1\u7b97\u56fe\u3002\u5982\u679c\u4f60\u7684\u786c\u4ef6\u5df2\u7ecf\u62e5\u6709\u5b8c\u6574\u7684\u8ba1\u7b97\u56fe\u6267\u884c\u5f15\u64ce\uff08graph execution engine\uff09\uff0c\u5982\u9002\u7528\u4e8e GPU \u7684 TensorRT\uff0c\u90a3\u4e48\u8be5\u89e3\u51b3\u65b9\u6848\u5bf9\u4f60\u800c\u8a00\u975e\u5e38\u5177\u6709\u53c2\u8003\u4ef7\u503c\u3002<\/p>\n<p>\u5b8c\u6210 codegen \u548c runtime \u540e\uff0c\u53ef\u4ee5\u8ba9\u5ba2\u6237\u501f\u52a9\u4f60\u7684\u81ea\u5b9a\u4e49\u6807\u7b7e\uff0c\u5bf9\u6a21\u578b\u8fdb\u884c\u6ce8\u91ca\u5e76\u52a0\u4ee5\u5229\u7528\u3002\u7ec8\u7aef\u7528\u6237\u5982\u4f55\u6ce8\u91ca\u548c\u542f\u52a8\u7279\u5b9a codegen \u7684\u6559\u7a0b\uff0c\u5c06\u5728\u540e\u7eed\u8fdb\u884c\u8865\u5145\u3002<\/p>\n<h2>\u5b9e\u73b0 C Codegen<\/h2>\n<p>\u5728\u8fd9\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u6f14\u793a\u5982\u4f55\u501f\u52a9\u9884\u5b9e\u73b0\u7684\u7b97\u5b50\u51fd\u6570\uff0c\u751f\u6210 C \u4ee3\u7801\u7684 codegen\u3002\u7b80\u5355\u8d77\u89c1\uff0c\u672c\u793a\u4f8b codegen \u4e0d\u4f9d\u8d56\u4e8e\u7b2c\u4e09\u65b9\u5e93\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u5728 C \u4e2d\u624b\u52a8\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5b8f\uff1a<\/p>\n<pre><code>#define CSOURCE_BINARY_OP_1D(p_ID_, p_OP_, p_DIM1_)         \/\n    extern &quot;C&quot; void p_ID_(float* a, float* b, float* out) { \/\n        for (int64_t i = 0; i &lt; p_DIM1_; ++i) {             \/\n            out[i] = a[i] p_OP_ b[i];                       \/\n        }                                                   \/\n    }\n\n#define CSOURCE_BINARY_OP_2D(p_ID_, p_OP_, p_DIM1_, p_DIM2_)  \/\n    extern &quot;C&quot; void p_ID_(float* a, float* b, float* out) {   \/\n        for (int64_t i = 0; i &lt; p_DIM1_; ++i) {               \/\n            for (int64_t j = 0; j &lt; p_DIM2_; ++j) {           \/\n                int64_t k = i * p_DIM2_ + j;                  \/\n                out[k] = a[k] p_OP_ b[k];                     \/\n            }                                                 \/\n        }                                                     \/\n    }<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5b8f\uff0c\u53ef\u4ee5\u4e3a\u4e00\u7ef4\u548c\u4e8c\u7ef4\u5f20\u91cf\u751f\u6210\u4e8c\u5143\u7b97\u5b50\uff08binary operator\uff09\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5982\u4e0b\u6240\u793a\u7684\u5b50\u56fe\uff0c\u5047\u8bbe\u6240\u6709\u8f93\u5165\u90fd\u662f shape \u4e3a\uff0810, 10\uff09\u7684\u4e8c\u7ef4\u5f20\u91cf\uff1a<\/p>\n<pre><code>c_compiler_input0\n       |\n      add &lt;-- c_compiler_input1\n       |\n    subtract &lt;-- c_compiler_input2\n       |\n    multiply &lt;-- c_compiler_input3\n       |\n      out<\/code><\/pre>\n<p>\u6211\u4eec\u7684\u76ee\u6807\u662f\u751f\u6210\u4ee5\u4e0b\u53ef\u7f16\u8bd1\u4ee3\u7801\u6765\u6267\u884c\u5b50\u56fe\uff1a<\/p>\n<pre><code>#include &lt;tvm\/runtime\/c_runtime_api.h&gt;\n#include &lt;tvm\/runtime\/packed_func.h&gt;\n#include &lt;dlpack\/dlpack.h&gt;\n#include &lt;cstdint&gt;\n#include &lt;cstring&gt;\n#include &lt;iostream&gt;\n\n#define GCC_BINARY_OP_1D(p_ID_, p_OP_, p_DIM1_)           \/\n  extern &quot;C&quot; void p_ID_(float* a, float* b, float* out) { \/\n    for (int64_t i = 0; i &lt; p_DIM1_; ++i) {               \/\n      out[i] = a[i] p_OP_ b[i];                           \/\n    }                                                     \/\n  }\n\n#define GCC_BINARY_OP_2D(p_ID_, p_OP_, p_DIM1_, p_DIM2_)  \/\n  extern &quot;C&quot; void p_ID_(float* a, float* b, float* out) { \/\n    for (int64_t i = 0; i &lt; p_DIM1_; ++i) {               \/\n      for (int64_t j = 0; j &lt; p_DIM2_; ++j) {             \/\n        int64_t k = i * p_DIM2_ + j;                      \/\n        out[k] = a[k] p_OP_ b[k];                         \/\n      }                                                   \/\n    }                                                     \/\n  }\n\n\/\/ \u6ce8 1\nGCC_BINARY_OP_2D(gcc_0_0, *, 10, 10);\nGCC_BINARY_OP_2D(gcc_0_1, -, 10, 10);\nGCC_BINARY_OP_2D(gcc_0_2, +, 10, 10);\n\n\/\/ \u6ce8 2\nextern &quot;C&quot; void gcc_0_(float* gcc_input0, float* gcc_input1,\n                       float* gcc_input2, float* gcc_input3, float* out) {\n  float* buf_0 = (float*)malloc(4 * 100);\n  float* buf_1 = (float*)malloc(4 * 100);\n  gcc_0_2(gcc_input0, gcc_input1, buf_0);\n  gcc_0_1(buf_0, gcc_input2, buf_1);\n  gcc_0_0(buf_1, gcc_input3, out);\n  free(buf_0);\n  free(buf_1);\n}\n\n\/\/ \u6ce8 3\nextern &quot;C&quot; int gcc_0_wrapper(DLTensor* arg0, DLTensor* arg1, DLTensor* arg2,\n                             DLTensor* arg3, DLTensor* out) {\n  gcc_0_(static_cast&lt;float*&gt;(arg0-&gt;data), static_cast&lt;float*&gt;(arg1-&gt;data),\n         static_cast&lt;float*&gt;(arg2-&gt;data), static_cast&lt;float*&gt;(arg3-&gt;data),\n         static_cast&lt;float*&gt;(out-&gt;data));\n  return 0;\n}\nTVM_DLL_EXPORT_TYPED_FUNC(gcc_0, gcc_0_wrapper);<\/code><\/pre>\n<p>\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u4e0a\u9762\u4ee3\u7801\u91cc\u7684\u6ce8\u91ca\uff1a<\/p>\n<ul>\n<li><strong>\u6ce81<\/strong>\uff1a\u5b50\u56fe\u4e2d\u4e09\u4e2a\u8282\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u6ce82<\/strong>\uff1a\u901a\u8fc7\u5206\u914d\u4e2d\u95f4\u6570\u7ec4\uff08intermediate buffer\uff09\u5e76\u8c03\u7528\u76f8\u5e94\u51fd\u6570\u6765\u6267\u884c\u5b50\u56fe\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>\u6ce83<\/strong>\uff1aTVM runtime \u517c\u5bb9\u7684\u5305\u88c5\u51fd\u6570\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u8f93\u5165\u5f20\u91cf\u5217\u8868\u548c\u4e00\u4e2a\u8f93\u51fa\u5f20\u91cf\uff08\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff09\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6b63\u786e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8c03\u7528\u6ce82 \u4e2d\u63cf\u8ff0\u7684\u5b50\u56fe\u51fd\u6570\u3002\u6b64\u5916\uff0c<code>TVM_DLL_EXPORT_TYPED_FUNC<\/code>\u00a0\u662f\u4e00\u4e2a TVM \u5b8f\uff0c\u5b83\u901a\u8fc7\u5c06\u6240\u6709\u5f20\u91cf\u6253\u5305\u5230\u00a0<code>TVMArgs<\/code>\u00a0\u6765\u751f\u6210\u53e6\u4e00\u4e2a\u51fd\u6570\u00a0<code>gcc_0<\/code>\uff0c\u8be5\u51fd\u6570\u5177\u6709\u7edf\u4e00\u7684\u51fd\u6570\u53c2\u6570\u3002\u56e0\u6b64\uff0cTVM runtime \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u00a0<code>gcc_0<\/code>\u00a0\u6765\u6267\u884c\u5b50\u56fe\uff0c\u65e0\u9700\u5176\u4ed6\u64cd\u4f5c\u3002\u751f\u6210\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0cTVM \u80fd\u591f\u5c06\u5176\u4e0e\u8ba1\u7b97\u56fe\u7684\u5176\u4f59\u90e8\u5206\u4e00\u8d77\u7f16\u8bd1\u5e76\u5bfc\u51fa\u5355\u4e2a\u5e93\u4ee5\u8fdb\u884c\u90e8\u7f72\u3002<\/li>\n<\/ul>\n<p>\u5728\u672c\u8282\u7684\u5176\u4f59\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u521b\u5efa\u4e00\u4e2a codegen\uff0c\u6765\u5b9e\u73b0\u4e0a\u8ff0\u4ee3\u7801\u3002\u4f60\u7684 codegen \u5fc5\u987b\u4f4d\u4e8e\u00a0<code>src\/relay\/backend\/contrib\/&lt;your-codegen-name&gt;\/<\/code>\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 codegen \u547d\u540d\u4e3a &#8220;codegen_c&#8221;\uff0c\u5e76\u5c06\u5176\u653e\u5728\u00a0<a href=\"https:\/\/link.segmentfault.com\/?enc=Mncx0cxwld9%2FUlCJachuGA%3D%3D.WOyzE9dWv1J17ZJhsC5yKUx1Qy7GSyb%2F65eDS%2FOb6ZhoNzccZ2vAzGD7G0rbn2DiNkTx7fkkvvEBqDY3yXJP4KgnvZXJQxqD8jEm%2Fl2nCvRg6Q1geG%2BkwZ4R4I%2Fs%2BjSW\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\/src\/relay\/backend\/contrib\/codegen_c\/<\/a>\u00a0\u76ee\u5f55\u4e0b\u3002\u4f60\u53ef\u4ee5\u968f\u65f6\u67e5\u770b\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4e86\u89e3\u5b8c\u6574\u7684\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5c06\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5b9e\u73b0\u4e24\u4e2a\u7c7b\uff0c\u4e24\u4e2a\u7c7b\u7684\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n<pre><code>            subgraph                                subgraph\nTVM backend -----------------------------&gt; CSourceCodegen -------------&gt; CodegenC\n       ^                                       |    ^                       |\n       |                                       |    |                       |\n       ----------------------------------------      ------------------------\n          generated C source runtime module              generated C code<\/code><\/pre>\n<p>\u5f53 TVM \u540e\u7aef\u53d1\u73b0 Relay \u8ba1\u7b97\u56fe\u4e2d\u7684\u51fd\u6570\uff08\u5b50\u56fe\uff09\uff0c\u7528\u6ce8\u518c\u7684\u7f16\u8bd1\u5668\u6807\u7b7e\uff08\u672c\u4f8b\u4e2d\u4e3a\u00a0<code>ccompiler<\/code>\uff09\u8fdb\u884c\u4e86\u6ce8\u91ca\u65f6\uff0cTVM \u540e\u7aef\u5c31\u4f1a\u8c03\u7528\u00a0<code>CSourceCodegen<\/code>\u00a0\u5e76\u4f20\u9012\u5b50\u56fe\u3002\u00a0<code>CSourceCodegen<\/code>\u00a0\u7684\u6210\u5458\u51fd\u6570\u00a0<code>CreateCSourceModule<\/code>\u00a0\u5c06\uff1a<\/p>\n<p>1\uff09\u4e3a\u5b50\u56fe\u751f\u6210 C \u4ee3\u7801\uff1b<\/p>\n<p>2\uff09\u5c06\u751f\u6210\u7684 C \u4ee3\u7801\u5305\u88c5\u5230 C source runtime \u6a21\u5757\u4e2d\uff0c\u4ee5\u4fbf TVM \u540e\u7aef\u8fdb\u884c\u7f16\u8bd1\u548c\u90e8\u7f72\u3002<\/p>\n<p>\u7279\u522b\u662f\uff0cC codegen \u5bf9\u00a0<code>CodegenC<\/code>\u00a0\u7c7b\u662f\u900f\u660e\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u5b9e\u7528\u7a0b\u5e8f\u6765\u7b80\u5316 codegen \u5b9e\u73b0\u3002\u4e0b\u9762\u7684\u7ae0\u8282\u5c06\u81ea\u4e0b\u800c\u4e0a\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u7c7b\u3002<\/p>\n<h3>\u5b9e\u73b0 CodegenC<\/h3>\n<p>\u5728\u00a0<code>src\/relay\/backend\/contrib\/codegen_c\/codegen.cc<\/code>\u00a0\u4e2d\uff0c\u9996\u5148\u5728\u00a0<code>tvm.relay.contrib<\/code>\u00a0\u7684\u547d\u540d\u7a7a\u95f4\u4e0b\u521b\u5efa\u4e00\u4e2a codegen \u7c7b\u9aa8\u67b6\uff1a<\/p>\n<pre><code>#include &lt;tvm\/relay\/expr_functor.h&gt;\n#include &lt;tvm\/relay\/transform.h&gt;\n#include &lt;tvm\/relay\/type.h&gt;\n#include &lt;tvm\/runtime\/module.h&gt;\n#include &lt;tvm\/runtime\/object.h&gt;\n\n#include &lt;fstream&gt;\n#include &lt;sstream&gt;\n\n#include &quot;codegen_c.h&quot;\n\nnamespace tvm {\nnamespace relay {\nnamespace contrib {\n\nclass CodegenC : public ExprVisitor, public CodegenCBase {\n  public:\n    explicit CodegenC(const std::string&amp; id) { this-&gt;ext_func_id_ = id; }\n\n    void VisitExpr_(const VarNode* node) { ; }\n    void VisitExpr_(const CallNode* call) final { ; }\n    std::string JIT() { ; }\n\n  private:\n    \/*! \/brief The function id that represents a C source function. *\/\n    std::string ext_func_id_ = &quot;&quot;;\n    \/*! \/brief The index of a wrapped C function. *\/\n    int func_idx = 0;\n    \/*! \/brief The index of allocated buffers. *\/\n    int buf_idx_ = 0;\n    \/*! \/brief The arguments of a C compiler compatible function. *\/\n    std::vector&lt;std::string&gt; ext_func_args_;\n    \/*! \/brief The statements of a C compiler compatible function. *\/\n    std::vector&lt;std::string&gt; ext_func_body;\n    \/*! \/brief The declaration statements of a C compiler compatible function. *\/\n    std::vector&lt;std::string&gt; func_decl_;\n    \/*! \/brief The declaration statements of buffers. *\/\n    std::vector&lt;std::string&gt; buf_decl_;\n    \/*! \/brief The name and index pairs for output. *\/\n    std::vector&lt;std::pair&lt;std::string, int&gt;&gt; out_;\n}<\/code><\/pre>\n<p><code>CodegenC<\/code>\u00a0\u7c7b\u7ee7\u627f\u4e86\u4e24\u4e2a\u7c7b\uff1a\u00a0<code>ExprVisitor<\/code>\u00a0\u63d0\u4f9b\u904d\u5386\u5b50\u56fe\u7684\u80fd\u529b\uff0c\u7136\u540e\u6536\u96c6\u6240\u9700\u7684\u4fe1\u606f\u5e76\u751f\u6210\u5b50\u56fe\u51fd\u6570\uff0c\u4f8b\u5982\u00a0<code>gcc_0_<\/code>\u3002<\/p>\n<p><code>CodegenCBase<\/code>\u00a0\u63d0\u4f9b\u4e86\u751f\u6210\u5305\u88c5\u51fd\u6570\u7684\u80fd\u529b\u548c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\u4e0a\u4f8b\u4e2d\u7684\u00a0<code>gcc_0<\/code>\u3002\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u8fd9\u4e2a codegen \u7c7b\u4e2d\u5b9e\u73b0\u4e09\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h4>\u7b97\u5b50\u7684\u4ee3\u7801\u751f\u6210<\/h4>\n<p>\u9996\u5148\u5b9e\u73b0\u00a0<code>VisitExpr_(const CallNode* call)<\/code>\u3002\u8be5\u51fd\u6570\u5728\u904d\u5386\u5b50\u56fe\u65f6\u4f1a\u8bbf\u95ee\u6240\u6709\u8c03\u7528\u8282\u70b9\u3002\u6bcf\u4e2a\u8c03\u7528\u8282\u70b9\u90fd\u5305\u542b\u4e00\u4e2a\u6211\u4eec\u60f3\u8981\u5378\u8f7d\uff08offload\uff09\u5230\u786c\u4ef6\u4e2d\u7684\u7b97\u5b50\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6309\u7167\u62d3\u6251\u987a\u5e8f\u751f\u6210\u5177\u6709\u6b63\u786e\u7b97\u5b50\u7684\u76f8\u5e94 C \u4ee3\u7801\u3002\u5b8c\u6574\u5b9e\u73b0\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<h5>1. \u751f\u6210\u51fd\u6570\u58f0\u660e<\/h5>\n<p>\u793a\u4f8b\u7ed3\u679c\uff1a<code>GCC_BINARY_OP_2D(gcc_0_0, *, 10, 10);<\/code><\/p>\n<p>\u8981\u751f\u6210\u51fd\u6570\u58f0\u660e\uff0c\u5982\u4e0a\u6240\u793a\uff0c\u6211\u4eec\u9700\u8981\uff1a<\/p>\n<p>1\uff09\u51fd\u6570\u540d\uff08\u4f8b\u5982\u00a0<code>gcc_0_0<\/code>\uff09<\/p>\n<p>2\uff09\u7b97\u5b50\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u00a0<code>*<\/code>\u00a0\uff09<\/p>\n<p>3\uff09\u8f93\u5165\u5f20\u91cf shape\uff08\u4f8b\u5982\u00a0<code>(10, 10)<\/code>\u00a0\uff09<\/p>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u4ece\u00a0<code>CallNode<\/code>\u00a0\u8f7b\u677e\u83b7\u53d6\uff1a<\/p>\n<pre><code>std::ostringstream macro_stream;\nstd::ostringstream decl_stream;\nstd::ostringstream buf_stream;\n\n\/\/ Generate a unique function name you like.\nstd::string func_name = ext_func_id_ + &quot;_&quot; + std::to_string(func_idx++);\n\n\/\/ Make function declaration string.\nmacro_stream &lt;&lt; &quot;CSOURCE_BINARY_OP_&quot; &lt;&lt; call-&gt;args.size() &lt;&lt; &quot;D(&quot; &lt;&lt; func_name &lt;&lt; &quot;, &quot;;\n\n\/\/ Check the operator type.\nif (IsOp(call, &quot;add&quot;)) {\n  macro_stream &lt;&lt; &quot;+&quot;;\n} else if (IsOp(call, &quot;subtract&quot;)) {\n  macro_stream &lt;&lt; &quot;-&quot;;\n} else if (IsOp(call, &quot;multiply&quot;)) {\n  macro_stream &lt;&lt; &quot;*&quot;;\n} else {\n  LOG(FATAL) &lt;&lt; &quot;Unrecognized op&quot;;\n}\n\n\/\/ Extract the input tensor shape.\nauto in_shape = GetShape(call-&gt;args[0]-&gt;checked_type());\nfor (size_t i = 0; i &lt; in_shape.size(); ++i) {\n  macro_stream &lt;&lt; &quot;, &quot; &lt;&lt; in_shape[i];\n}\nmacro_stream &lt;&lt; &quot;);&quot;;\nfunc_decl_.push_back(macro_stream.str());<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u5c06\u751f\u6210\u7684\u4ee3\u7801\u63a8\u9001\u5230\u7c7b\u6210\u5458\u53d8\u91cf\u00a0<code>func_decl_<\/code>\u00a0\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u5728\u6211\u4eec\u5b8c\u6210\u904d\u5386\u6574\u4e2a\u5b50\u56fe\u4e4b\u540e\uff0c\u6211\u4eec\u5df2\u7ecf\u6536\u96c6\u4e86\u6240\u6709\u5fc5\u9700\u7684\u51fd\u6570\u58f0\u660e\uff0c\u6211\u4eec\u552f\u4e00\u9700\u8981\u505a\u7684\u5c31\u662f\u7528 GCC \u7f16\u8bd1\u5b83\u4eec\u3002\u00a0<code>VisitExpr_(const CallNode* call)<\/code>\u00a0\u7684\u5176\u4f59\u5b9e\u73b0\u4e5f\u9075\u5faa\u8fd9\u4e2a\u6982\u5ff5\u3002<\/p>\n<h5>2. \u751f\u6210\u51fd\u6570\u8c03\u7528<\/h5>\n<p>\u793a\u4f8b\u7ed3\u679c\uff1a<code>gcc_0_0(buf_1, gcc_input3, out);<\/code><\/p>\n<p>\u751f\u6210\u51fd\u6570\u58f0\u660e\u540e\uff0c\u6211\u4eec\u9700\u8981\u751f\u6210\u4e00\u4e2a\u5177\u6709\u6b63\u786e\u8f93\u5165\u548c\u8f93\u51fa\u7684\u51fd\u6570\u8c03\u7528\u3002\u8981\u60f3\u77e5\u9053\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\u5e94\u8be5\u653e\u7f6e\u54ea\u4e9b\u8f93\u5165\u6216\u6570\u7ec4\uff0c\u5fc5\u987b\u8bbf\u95ee\u5b83\u7684\u53c2\u6570\uff1a<\/p>\n<pre><code>bool first = true;\ndecl_stream &lt;&lt; func_name &lt;&lt; &quot;(&quot;;\nfor (size_t i = 0; i &lt; call-&gt;args.size(); ++i) {\n  VisitExpr(call-&gt;args[i]); \/\/ \u6ce8 1\n  for (auto out : out_) {\n    if (!first) {\n      decl_stream &lt;&lt; &quot;, &quot;;\n    }\n    first = false;\n    decl_stream &lt;&lt; out.first;\n  }\n}\n\/\/ \u6ce8 2<\/code><\/pre>\n<p>\u540c\u6837\uff0c\u91cd\u70b9\u4ecb\u7ecd\u4e00\u4e0b\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684\u6ce8\u91ca\uff1a<\/p>\n<p><strong>\u6ce81<\/strong>\uff1a<code>VisitExpr(call-&gt;args[i])<\/code>\u00a0\u662f\u8bbf\u95ee\u5f53\u524d\u51fd\u6570\u53c2\u6570\u7684\u9012\u5f52\u8c03\u7528\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u8282\u70b9\u7684\u8f93\u51fa\u6216\u8f93\u5165\u5f20\u91cf\u3002\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u9700\u8981\u786e\u4fdd\u6bcf\u4e2a\u8282\u70b9\u5728\u79bb\u5f00\u8bbf\u95ee\u5668\u4e4b\u524d\uff0c\u90fd\u66f4\u65b0\u4e00\u4e2a\u7c7b\u53d8\u91cf\u00a0<code>out_<\/code>\u3002\u56fe\u89e3\u5982\u4e0b\uff1a<\/p>\n<pre><code> arg_node                 arg_node &lt;- Visit arg (Note 1)       arg_node\n     |                        |                                    |\n curr_node &lt;- Process      curr_node                            curr_node &lt;- Put &quot;buf_0&quot; as an input buffer\n\n(a) out_ = {}            (b) out_ = {}                   (c) out_ = {(&quot;buf_0&quot;, 20)}<\/code><\/pre>\n<p>\u4ece\u4e0a\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u7c7b\u53d8\u91cf\u00a0<code>out_<\/code>\u00a0\u5728\u8bbf\u95ee\u53c2\u6570\u8282\u70b9\u524d\u662f\u7a7a\u7684\uff0c\u5b83\u88ab\u586b\u5145\u4e86\u00a0<code>arg_node<\/code>\u00a0\u8f93\u51fa\u6570\u7ec4\u7684\u540d\u79f0\u548c\u5927\u5c0f\u3002\u56e0\u6b64\u5728\u5b8c\u6210\u5bf9\u53c2\u6570\u8282\u70b9\u7684\u8bbf\u95ee\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u00a0<code>out_<\/code>\u00a0\u5f97\u77e5\u5e94\u8be5\u653e\u7f6e\u7684\u6b63\u786e\u8f93\u5165\u6570\u7ec4\u3002\u672c\u8282\u672b\u5c3e\u4ee5\u53ca\u4e0b\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u66f4\u65b0\u00a0<code>out_<\/code>\u3002<\/p>\n<p><strong>\u6ce82<\/strong>\uff1a\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\uff0c\u6211\u4eec\u5728\u8fd9\u4e00\u6b65\u6ca1\u6709\u5173\u95ed\u51fd\u6570\u8c03\u7528\u5b57\u7b26\u4e32\u3002\u5f53\u524d\u51fd\u6570\u8c03\u7528\u5b57\u7b26\u4e32\u770b\u8d77\u6765\u50cf\uff1a<code>gcc_0_0(buf_1, gcc_input3<\/code>\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u5c06\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff08\u5982 output\uff09\u653e\u5165\u6b64\u8c03\u7528\u4e2d\u3002\u51fd\u6570\u8c03\u7528\u7684\u8f93\u51fa\u53ef\u4ee5\u662f\u5206\u914d\u7684\u4e34\u65f6\u6570\u7ec4\u6216\u5b50\u56fe\u8f93\u51fa\u5f20\u91cf\u3002\u7b80\u5355\u8d77\u89c1\uff0c\u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u4e3a\u6bcf\u4e2a\u8c03\u7528\u8282\u70b9\u90fd\u5206\u914d\u8001\u4e00\u4e2a\u8f93\u51fa\u6570\u7ec4\uff08\u4e0b\u4e00\u6b65\uff09\uff0c\u5e76\u5c06\u6700\u540e\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u7ed3\u679c\u590d\u5236\u5230\u4e86\u8f93\u51fa\u5f20\u91cf\u3002<\/p>\n<h5>3. \u751f\u6210\u8f93\u51fa\u6570\u7ec4\uff08output buffer\uff09<\/h5>\n<p>\u793a\u4f8b\u7ed3\u679c\uff1a<code>float buf_0 = (float)malloc(4 * 100);<\/code><\/p>\n<p>\u5982\u4e0a\u4e00\u6b65\u6240\u8ff0\uff0c\u9664\u4e86\u5b50\u56fe\u8f93\u5165\u548c\u8f93\u51fa\u5f20\u91cf\u5916\uff0c\u8fd8\u9700\u8981\u6570\u7ec4\u6765\u4fdd\u5b58\u4e2d\u95f4\u7ed3\u679c\u3002\u4e3a\u4e86\u751f\u6210\u6570\u7ec4\uff0c\u6211\u4eec\u63d0\u53d6 shape \u4fe1\u606f\uff0c\u4ee5\u786e\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\u548c\u5927\u5c0f\uff1a<\/p>\n<pre><code>\/\/ \u8fd9\u4e2a\u4f8b\u5b50\u4ec5\u652f\u6301\u5355\u4e2a\u8f93\u51fa\u3002\nauto type_node = call-&gt;checked_type().as&lt;TensorTypeNode&gt;();\nICHECK(type_node != nullptr &amp;&amp; runtime::TypeMatch(type_node-&gt;dtype, kDLFloat, 32))\n      &lt;&lt; &quot;Only support single output tensor with float type&quot;;\n\n\/\/ \u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u6570\u7ec4\u540d\u5b57\u3002\nstd::string out = &quot;buf_&quot; + std::to_string(buf_idx_++);\n\n\/\/ \u63d0\u53d6 shape \u4f5c\u4e3a\u6570\u7ec4\u5927\u5c0f\u3002\nauto out_shape = GetShape(call-&gt;checked_type());\nint out_size = 1;\nfor (size_t i = 0; i &lt; out_shape.size(); ++i) {\n  out_size *= out_shape[i];\n}\n\n\/\/ \u5206\u914d\u6570\u7ec4\u5e76\u63a8\u9001\u81f3\u6570\u7ec4\u58f0\u660e\nbuf_stream &lt;&lt; &quot;float* &quot; &lt;&lt; out &lt;&lt; &quot; = (float*)std::malloc(4 * &quot; &lt;&lt; out_size &lt;&lt; &quot;);&quot;;\nbuf_decl_.push_back(buf_stream.str());<\/code><\/pre>\n<p>\u5206\u914d\u4e86\u8f93\u51fa\u6570\u7ec4\u4e4b\u540e\uff0c\u73b0\u5728\u53ef\u4ee5\u5173\u95ed\u51fd\u6570\u8c03\u7528\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u751f\u6210\u7684\u51fd\u6570\u8c03\u7528\u63a8\u9001\u5230\u7c7b\u53d8\u91cf\u00a0<code>ext_func_body<\/code>\u3002<\/p>\n<pre><code>decl_stream &lt;&lt; &quot;, &quot; &lt;&lt; out &lt;&lt; &quot;);&quot;;\next_func_body.push_back(decl_stream.str());<\/code><\/pre>\n<h5>4. \u66f4\u65b0\u8f93\u51fa\u6570\u7ec4<\/h5>\n<p>\u4e3a\u4e86\u4f7f\u5f97\u4e0b\u4e00\u4e2a\u8282\u70b9\uff08\u63a5\u53d7\u5f53\u524d\u8c03\u7528\u8282\u70b9\u7684\u8f93\u51fa\uff0c\u4f5c\u4e3a\u5176\u8f93\u5165\uff09\u77e5\u9053\u5b83\u5e94\u8be5\u4f7f\u7528\u54ea\u4e2a\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u5728\u79bb\u5f00\u8fd9\u4e2a\u8bbf\u95ee\u51fd\u6570\u4e4b\u524d\u66f4\u65b0\u7c7b\u53d8\u91cf\u00a0<code>out_<\/code>\uff1a<\/p>\n<pre><code>out_.clear();\nout_.push_back({out, out_size});<\/code><\/pre>\n<p>\u606d\u559c\uff01\u5230\u8fd9\u4e00\u6b65\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u8fd9\u4e2a\u7c7b\u4e2d\u6700\u56f0\u96be\u7684\u51fd\u6570\u3002\u63a5\u4e0b\u6765\u7684\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u5b8c\u5584\u8fd9\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u3002<\/p>\n<h4>\u8f93\u5165\u53d8\u91cf\u7684\u4ee3\u7801\u751f\u6210<\/h4>\n<p>\u56de\u60f3\u4e00\u4e0b\uff0c\u6211\u4eec\u901a\u8fc7\u8bbf\u95ee\u8c03\u7528\u8282\u70b9\u7684\u53c2\u6570\uff08\u4e0a\u4e00\u8282\u4e2d\u7684\u7b2c 2 \u6b65\uff09\u6536\u96c6\u4e86\u8f93\u5165\u6570\u7ec4\u4fe1\u606f\uff0c\u5e76\u5904\u7406\u4e86\u53c2\u6570\u662f\u53e6\u4e00\u4e2a\u8c03\u7528\u8282\u70b9\u7684\u60c5\u51b5\uff08\u7b2c 4 \u6b65\uff09\u3002\u672c\u8282\u6211\u4eec\u5c06\u4ee5\u00a0<code>VarNode<\/code>\u00a0\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5904\u7406\u5176\u4ed6\u8282\u70b9\u3002<\/p>\n<p><code>VarNode<\/code>\u00a0\u8868\u793a\u6a21\u578b\u4e2d\u7684\u8f93\u5165\u5f20\u91cf\u3002\u5b83\u975e\u5e38\u91cd\u8981\u7684\u4e00\u70b9\u5c31\u662f\u540d\u79f0\u63d0\u793a\uff08\u4f8b\u5982\uff0c<code>data<\/code>\u3001<code>weight<\/code>\u00a0\u7b49\uff09\u3002\u8bbf\u95ee\u00a0<code>VarNode<\/code>\u00a0\u65f6\uff0c\u53ea\u9700\u66f4\u65b0\u7c7b\u53d8\u91cf\u00a0<code>out_<\/code>\u00a0\u4f20\u9012\u540d\u79f0\u63d0\u793a\uff0c\u540e\u4ee3\uff08descendant\uff09\u8c03\u7528\u8282\u70b9\u5c31\u53ef\u4ee5\u751f\u6210\u6b63\u786e\u7684\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<pre><code>void VisitExpr_(const VarNode* node) {\n  ext_func_args_.push_back(node-&gt;name_hint());\n  out_.clear();\n  out_.push_back({node-&gt;name_hint(), 0});\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u8981\u5378\u8f7d\u7684\u5b50\u56fe\u53ea\u6709\u8c03\u7528\u8282\u70b9\u548c\u53d8\u91cf\u8282\u70b9\u3002\u5982\u679c\u5b50\u56fe\u5305\u542b\u5176\u4ed6\u7c7b\u578b\u7684\u8282\u70b9\uff0c\u5982\u00a0<code>TupleNode<\/code>\uff0c\u90a3\u4e48\u4f60\u4e5f\u9700\u8981\u8bbf\u95ee\u5b83\u4eec\u5e76\u7ed5\u8fc7\u8f93\u51fa\u6570\u7ec4\u4fe1\u606f\u3002<\/p>\n<h4>Code Emitting<\/h4>\n<p>Codegen Class \u7684\u6700\u540e\u4e00\u90e8\u5206\u662f\u00a0<code>JIT<\/code>\u00a0\u51fd\u6570\uff0c\u5b83\u4e3a\u5b50\u56fe emit \u4e00\u4e2a C \u51fd\u6570\uff0c\u5e76\u5c06\u521a\u751f\u6210\u7684 C \u4ee3\u7801\u4f5c\u4e3a\u51fd\u6570\u4f53\u3002\u6ce8\u610f\uff0c\u9664\u4e86\u5728\u524d\u51e0\u8282\u4e2d\u751f\u6210\u7684\u5b50\u56fe\u51fd\u6570\u5916\uff0c\u8fd8\u9700\u8981\u4e00\u4e2a\u5177\u6709\u7edf\u4e00\u53c2\u6570\u7684 wrapper \u51fd\u6570\uff0c\u4f9b TVM runtime \u8c03\u7528\u548c\u4f20\u9012\u6570\u636e\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u7ee7\u627f\u7684\u57fa\u7c7b\u5df2\u7ecf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b9e\u73b0\uff0c\u5373\u00a0<code>JitImpl<\/code>\uff0c\u6765\u751f\u6210\u8be5\u51fd\u6570\u3002\u8c03\u7528\u00a0<code>JitImpl<\/code>\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>JitImpl(&quot;gcc_0&quot; \/* Subgraph symbol (ID) *\/,\n        {&quot;gcc_input0&quot;, &quot;gcc_input1&quot;, &quot;gcc_input2&quot;, &quot;gcc_input3&quot;} \/* Input arguments *\/,\n        {&quot;float *buf_0 = (float*)malloc(4 * 20)&quot;, ...} \/* Buffer allocations *\/,\n        {&quot;gcc_0_2(gcc_input0, gcc_input1, buf_0);&quot;} \/* Function body *\/,\n        {&quot;out&quot;} \/* Output *\/);<\/code><\/pre>\n<p>\u4e0a\u8ff0\u8c03\u7528\u5c06\u751f\u6210\u4e09\u4e2a\u51fd\u6570\uff08\u4e00\u4e2a\u6765\u81ea TVM wrapper \u5b8f\uff09\uff1a<\/p>\n<ol>\n<li>\u5b50\u56fe\u51fd\u6570\u00a0<code>gcc_0_<\/code>\uff08\u51fd\u6570\u540d\u672b\u5c3e\u591a\u4e86\u4e00\u4e2a\u4e0b\u5212\u7ebf\uff09\u4ee5\u53ca\u4e3a\u6267\u884c\u5b50\u56fe\u800c\u751f\u6210\u7684\u6240\u6709 C \u4ee3\u7801\uff1b<\/li>\n<li>\u5e26\u6709\u00a0<code>DLTensor<\/code>\u00a0\u53c2\u6570\u5217\u8868\u7684 wrapper \u51fd\u6570\u00a0<code>gcc_0__wrapper_<\/code>\u00a0\uff0c\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6b63\u786e\u7684\u7c7b\u578b\u5e76\u8c03\u7528\u00a0<code>gcc_0_<\/code><\/li>\n<li>TVM runtime \u517c\u5bb9\u51fd\u6570\u00a0<code>gcc_0<\/code>\u00a0\u5177\u6709 TVM \u7edf\u4e00\u51fd\u6570\u53c2\u6570\uff0c\u53ef\u89e3\u5305 TVM \u6253\u5305\u5f20\u91cf\u5e76\u8c03\u7528\u00a0<code>gcc_0__wrapper_<\/code><\/li>\n<\/ol>\n<p>\u56e0\u6b64\uff0c\u5728\u00a0<code>JIT<\/code>\u00a0\u5b9e\u73b0\u4e2d\u552f\u4e00\u8981\u505a\u7684\uff0c\u5c31\u662f\u5c06\u751f\u6210\u7684\u6240\u6709\u5b50\u56fe\u51fd\u6570\u4ee3\u7801\u4f20\u9012\u7ed9\u00a0<code>JitImpl<\/code>\uff1a<\/p>\n<pre><code>std::string JIT() {\n  \/\/ Write function macros\n  for (auto decl : func_decl_) {\n    code_stream_ &lt;&lt; decl &lt;&lt; &quot;\/n&quot;;\n  }\n  return JitImpl(ext_func_id_, ext_func_args_, buf_decl_, ext_func_body, out_);\n}<\/code><\/pre>\n<p>\u4f20\u9012\u7684\u6240\u6709\u53d8\u91cf\uff08<code>ext_func_id<\/code>\u00a0\u7b49\uff09\u90fd\u662f\u7c7b\u53d8\u91cf\uff0c\u5e76\u5728\u904d\u5386\u5b50\u56fe\u65f6\u88ab\u586b\u5145\u3002<\/p>\n<h5>\u5b9e\u73b0 CSourceCodegen<\/h5>\n<p>\u521b\u5efa\u4e00\u4e2a\u7c7b\u5e76\u5b9e\u73b0\u6240\u9700\u529f\u80fd\uff0c\u6ce8\u610f\uff1a\u9700\u8981\u7ee7\u627f\u81ea\u00a0<code>CSourceModuleCodegenBase<\/code>\uff1a<\/p>\n<pre><code>class CSourceCodegen : public CSourceModuleCodegenBase {\n public:\n  \/\/ \u4f20\u9012\u4e00\u4e2a\u5b50\u56fe\u51fd\u6570, \u5e76\u751f\u6210 C \u4ee3\u7801\u3002\n  void GenCFunc(const Function&amp; func) { ; }\n\n  \/\/ \u4f7f\u7528 GenCFunc \u6765\u751f\u6210 C \u4ee3\u7801\u5e76\u5c06\u5b83\u5305\u88c5\u6210\u4e00\u4e2a C \u6e90\u6a21\u5757\u3002\n  runtime::Module CreateCSourceModule(const NodeRef&amp; ref) override { ; }\n\n private:\n  std::ostringstream code_stream_;\n};<\/code><\/pre>\n<h5>\u5b9e\u73b0 GenCFunc<\/h5>\n<p><code>GenCFunc<\/code>\u00a0\u53ea\u662f\u7b80\u5355\u5730\u4f7f\u7528\u6211\u4eec\u521a\u521a\u5b9e\u73b0\u7684\u00a0<code>CodegenC<\/code>\u00a0\u6765\u904d\u5386\u4e00\u4e2a Relay \u51fd\u6570\uff08\u5b50\u56fe\uff09\uff0c\u5f97\u5230\u751f\u6210\u7684 C \u4ee3\u7801\u3002\u5185\u7f6e\u51fd\u6570\u00a0<code>GetExtSymbol<\/code>\u00a0\u5728 Relay \u51fd\u6570\u4e2d\u68c0\u7d22\u552f\u4e00\u7684\u7b26\u53f7\u540d\u79f0\uff08\u4f8b\u5982\u00a0<code>gcc_0<\/code>\uff09\uff0c\u6ce8\u610f\uff1a<strong>\u5fc5\u987b<\/strong>\u5c06\u5176\u7528\u4f5c C \u51fd\u6570\u540d\u79f0\uff0c\u56e0\u4e3a\u8be5\u7b26\u53f7\u5c06\u7528\u4e8e DSO \u8fd0\u884c\u67e5\u627e\u3002<\/p>\n<pre><code>void GenCFunc(const Function&amp; func) {\n  ICHECK(func.defined()) &lt;&lt; &quot;Input error: expect a Relay function.&quot;;\n\n  \/\/ \u8bb0\u5f55\u8fd0\u884c\u67e5\u627e\u7684\u5916\u90e8\u7b26\u53f7\u3002\n  auto sid = GetExtSymbol(func);\n\n  CodeGenC builder(sid);\n  builder.VisitExpr(func-&gt;body);\n  code_stream_ &lt;&lt; builder.JIT();\n}<\/code><\/pre>\n<h5>\u5b9e\u73b0 CreateCSourceModule<\/h5>\n<p>\u6b64\u51fd\u6570\u4e3a\u5916\u90e8\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a runtime \u6a21\u5757\u3002\u672c\u4e8b\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u53ef\u4ee5\u76f4\u63a5\u88ab\u7f16\u8bd1\u5e76\u4e0e TVM \u751f\u6210\u7684 DSOModule \u94fe\u63a5\u5728\u4e00\u8d77\u7684 CSourceModule\u3002<code>CodegenC<\/code>\u00a0\u5b9e\u73b0\u4e4b\u540e\uff0c\u518d\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u5c31\u6bd4\u8f83\u7b80\u5355\u4e86\uff1a<\/p>\n<pre><code>runtime::Module CreateCSourceModule(const NodeRef&amp; ref) override {\n  \/\/ \u521b\u5efa\u5934\u6587\u4ef6\n  code_stream_ &lt;&lt; &quot;#include &lt;cstdint&gt;\/n&quot;;\n  code_stream_ &lt;&lt; &quot;#include &lt;iostream&gt;\/n&quot;;\n  code_stream_ &lt;&lt; &quot;#include &lt;cstdlib&gt;\/n&quot;;\n  code_stream_ &lt;&lt; &quot;#include &lt;stdio.h&gt;\/n&quot;;\n  code_stream_ &lt;&lt; &quot;#include &lt;cstring&gt;\/n&quot;;\n  code_stream_ &lt;&lt; &quot;#include &lt;tvm\/runtime\/c_runtime_api.h&gt;\/n&quot;;\n  code_stream_ &lt;&lt; &quot;#include &lt;dlpack\/dlpack.h&gt;\/n&quot;;\n\n  \/\/ \u4e3a\u7b97\u5b50\u5b9a\u4e49\u6dfb\u52a0\u4e00\u4e9b\u516c\u5171\u5b8f\u3002\n  const char* operator_macro = R&quot;op_macro(\n  #define CSOURCE_BINARY_OP_1D(p_ID_, p_OP_, p_DIM1_)       \/\n    extern &quot;C&quot; void p_ID_(float* a, float* b, float* out) { \/\n      for (int64_t i = 0; i &lt; p_DIM1_; ++i) {               \/\n        out[i] = a[i] p_OP_ b[i];                           \/\n      }                                                     \/\n    }\n\n  #define CSOURCE_BINARY_OP_2D(p_ID_, p_OP_, p_DIM1_, p_DIM2_)  \/\n    extern &quot;C&quot; void p_ID_(float* a, float* b, float* out) {     \/\n      for (int64_t i = 0; i &lt; p_DIM1_; ++i) {                   \/\n        for (int64_t j = 0; j &lt; p_DIM2_; ++j) {                 \/\n          int64_t k = i * p_DIM2_ + j;                          \/\n          out[k] = a[k] p_OP_ b[k];                             \/\n        }                                                       \/\n      }                                                         \/\n    }\n  )op_macro&quot;;\n\n  code_stream_ &lt;&lt; operator_macro &lt;&lt; &quot;\/n\/n&quot;;\n\n  \/\/ \u4e3a\u5b50\u56fe\u751f\u6210 C \u4ee3\u7801\u3002\n  if (ref-&gt;IsInstance&lt;FunctionNode&gt;()) {\n    GenCFunc(Downcast&lt;Function&gt;(ref));\n  } else if (ref-&gt;IsInstance&lt;relay::ModuleNode&gt;()) {\n    relay::Module mod = Downcast&lt;relay::Module&gt;(ref);\n    for (const auto&amp; it : mod-&gt;functions) {\n      GenCFunc(Downcast&lt;Function&gt;(it.second));\n    }\n  } else {\n    LOG(FATAL) &lt;&lt; &quot;The input ref is expected to be a Relay function or module&quot;\n               &lt;&lt; &quot;\/n&quot;;\n  }\n\n  \/\/ \u521b\u5efa\u4e00\u4e2a CSourceModule\n  const auto* pf = runtime::Registry::Get(&quot;module.csource_module_create&quot;);\n  ICHECK(pf != nullptr) &lt;&lt; &quot;Cannot find csource module to create the external runtime module&quot;;\n  return (*pf)(code_stream_.str(), &quot;cc&quot;);\n}<\/code><\/pre>\n<h4>\u6ce8\u518c CodegenC<\/h4>\n<p>\u6700\u540e\u4e00\u6b65\u662f\u5c06 codegen \u6ce8\u518c\u5230 TVM \u540e\u7aef\u3002\u9996\u5148\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u8c03\u7528 codegen \u5e76\u751f\u6210\u4e00\u4e2a runtime \u6a21\u5757\uff1a<\/p>\n<pre><code>runtime::Module CCompiler(const NodeRef&amp; ref) {\n  CSourceCodegen csource;\n  return csource.CreateCSourceModule(ref);\n}<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u5c06\u6b64\u51fd\u6570\u6ce8\u518c\u5230 TVM \u540e\u7aef\uff1a<\/p>\n<pre><code>TVM_REGISTER_GLOBAL(&quot;relay.ext.ccompiler&quot;).set_body_typed(CCompiler);<\/code><\/pre>\n<p>\u5176\u4e2d\u00a0<code>ccompiler<\/code>\u00a0\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6807\u7b7e\uff0c\u5b83\u544a\u77e5 TVM \u8fd9\u662f\u7528\u00a0<code>ccompiler<\/code>\u00a0\u6ce8\u91ca\u5b50\u56fe\u65f6\uff0c\u5e94\u8be5\u7528\u6765\u751f\u6210\u548c\u5378\u8f7d\u5b50\u56fe\u7684 codegen\u3002<\/p>\n<p>\u6700\u540e\uff0c\u8bbe\u7f6e\u4e00\u4e2a CMake \u914d\u7f6e\u6807\u5fd7\uff0c\u53ea\u5305\u542b\u5ba2\u6237\u7684\u7f16\u8bd1\u5668\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2a cmake \u6587\u4ef6\uff1a<code>cmake\/modules\/contrib\/CODEGENC.cmake<\/code>\uff1a<\/p>\n<pre><code>if(USE_CODEGENC)\n  file(GLOB CSOURCE_RELAY_CONTRIB_SRC src\/relay\/backend\/contrib\/codegen_c\/codegen.cc)\n  list(APPEND COMPILER_SRCS ${CSOURCE_RELAY_CONTRIB_SRC})\nendif(USE_CODEGENC)<\/code><\/pre>\n<p>\u7528\u6237\u5728\u4f7f\u7528\u00a0<code>config.cmake<\/code>\u00a0\u914d\u7f6e TVM \u65f6\uff0c\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a\u662f\u5426\u914d\u7f6e\u7f16\u8bd1\u5668\uff1a<\/p>\n<pre><code>set(USE_CODEGENC ON)<\/code><\/pre>\n<h3>\u4e3a\u8868\u5f81\uff08Representation\uff09\u5b9e\u73b0 Codegen<\/h3>\n<p>\u5c3d\u7ba1\u6211\u4eec\u5df2\u7ecf\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0 C codegen\uff0c\u4f46\u7528\u6237\u786c\u4ef6\u53ef\u80fd\u8fd8\u9700\u8981\u5176\u4ed6\u5f62\u5f0f\u7684\u8ba1\u7b97\u56fe\u8868\u5f81\uff08Graph Representation\uff09\uff0c\u5982 JSON\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u00a0<code>CodegenC<\/code>\u00a0\u7c7b\uff0c\u751f\u6210\u81ea\u5df1\u7684\u8ba1\u7b97\u56fe\u8868\u5f81\uff0c\u5e76\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\uff0c\u544a\u8bc9 TVM runtime \u5982\u4f55\u6267\u884c\u8fd9\u4e2a\u8ba1\u7b97\u56fe\u8868\u5f81\u3002<\/p>\n<p>\u7b80\u5355\u8d77\u89c1\uff0c\u672c\u6307\u5357\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a &#8220;ExampleJSON&#8221; \u7684\u8ba1\u7b97\u56fe\u8868\u5f81\u3002 ExampleJSON \u5e76\u4e0d\u662f JSON\uff0c\u800c\u662f\u6ca1\u6709\u63a7\u5236\u6d41\u7684\u8ba1\u7b97\u56fe\u7684\u7b80\u5355\u8868\u793a\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4ee5\u4e0b\u540d\u4e3a\u00a0<code>subgraph_0<\/code>\u00a0\u7684\u5b50\u56fe\uff1a<\/p>\n<pre><code>input0\n   |\n  add &lt;-- input1\n   |\nsubtract &lt;-- input2\n   |\nmultiply &lt;-- input3\n   |\n  out<\/code><\/pre>\n<p>\u90a3\u4e48\u8fd9\u4e2a\u5b50\u56fe\u7684 ExampleJON \u770b\u8d77\u6765\u7c7b\u4f3c\uff1a<\/p>\n<pre><code>subgraph_0\n  input 0 10 10\n  input 1 10 10\n  input 2 10 10\n  input 3 10 10\n  add 4 inputs: 0 1 shape: 10 10\n  sub 5 inputs: 4 2 shape: 10 10\n  mul 6 inputs: 5 3 shape: 10 10<\/code><\/pre>\n<p><code>input<\/code>\u00a0\u5173\u952e\u5b57\u58f0\u660e\u4e00\u4e2a\u8f93\u5165\u5f20\u91cf\u53ca\u5176 ID \u548c shape\uff1b\u5176\u4ed6\u8bed\u53e5\u7528\u00a0<code>&lt;op&gt; &lt;output ID&gt; inputs: [input ID] shape: [shape]<\/code>\u00a0\u8bed\u6cd5\u63cf\u8ff0\u4e86\u5176\u8ba1\u7b97\u8fc7\u7a0b\u3002<\/p>\n<p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u8bd5\u56fe\u5b9e\u73b0\u4ee5\u4e0b\u81ea\u5b9a\u4e49 TVM runtime \u6a21\u5757\uff0c\u6765\u6267\u884c ExampleJSON \u8ba1\u7b97\u56fe\u3002<\/p>\n<pre><code>runtime::Module ExampleJsonCompiler(const NodeRef&amp; ref) {\n    ExampleJsonCodeGen codegen(ref);\n    std::string code = codegen.gen(); \/\/ \u6ce8 1\n    const auto* pf = runtime::Registry::Get(&quot;module.examplejson_module_create&quot;); \/\/ \u6ce8 2\n    ICHECK(pf != nullptr) &lt;&lt; &quot;Cannot find ExampleJson module to create the external runtime module&quot;;\n    return (*pf)(code);\n}\nTVM_REGISTER_GLOBAL(&quot;relay.ext.examplejsoncompiler&quot;).set_body_typed(ExampleJsonCompiler);<\/code><\/pre>\n<p><strong>\u6ce81<\/strong>\uff1a\u7a0d\u540e\u6211\u4eec\u5c06\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 codegen\uff0c\u901a\u8fc7\u53d6\u4e00\u4e2a\u5b50\u56fe\u6765\u751f\u6210\u4e00\u4e2a ExampleJSON \u4ee3\u7801\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u6ce82<\/strong>\uff1a\u6b64\u884c\u83b7\u53d6\u4e86\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u7684\u51fd\u6570\u7684\u6307\u9488\u3002\u53ef\u4ee5\u770b\u5230\u5b83\u91c7\u7528\u521a\u521a\u751f\u6210\u7684 ExampleJSON \u683c\u5f0f\u7684\u5b50\u56fe\u4ee3\u7801\uff0c\u5e76\u5bf9\u4e00\u4e2a runtime \u6a21\u5757\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u3002<\/p>\n<p>\u540e\u7eed\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd 1\uff09\u5982\u4f55\u5b9e\u73b0\u00a0<code>ExampleJsonCodeGen<\/code>\u00a0\u548c 2\uff09\u5982\u4f55\u5b9e\u73b0\u548c\u6ce8\u518c\u00a0<code>examplejson_module_create<\/code>\u3002<\/p>\n<h4>\u5b9e\u73b0 ExampleJsonCodeGen<\/h4>\n<p>\u4e0e C codegen \u7c7b\u4f3c\uff0c\u4ece\u00a0<code>ExprVisitor<\/code>\u00a0\u6d3e\u751f\u00a0<code>ExampleJsonCodeGen<\/code>\u00a0\u4ee5\u8bbf\u95ee\u5668\u6a21\u5f0f\u8fdb\u884c\u5b50\u56fe\u904d\u5386\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u56e0\u4e3a\u4e0d\u4f1a\u7528\u5230 TVM C++ wrapper\uff0c\u6240\u4ee5\u4e0d\u5fc5\u7ee7\u627f\u00a0<code>CodegenCBase<\/code>\u3002 codegen \u7c7b\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code>#include &lt;tvm\/relay\/expr_functor.h&gt;\n#include &lt;tvm\/relay\/transform.h&gt;\n#include &lt;tvm\/relay\/type.h&gt;\n#include &lt;tvm\/runtime\/module.h&gt;\n#include &lt;tvm\/runtime\/object.h&gt;\n\n#include &lt;fstream&gt;\n#include &lt;sstream&gt;\n\nnamespace tvm {\nnamespace relay {\nnamespace contrib {\n\nclass ExampleJsonCodeGen : public ExprVisitor {\n  public:\n    explicit ExampleJsonCodeGen();\n\n    \/\/ \u6ce8 1\n    void VisitExpr_(const VarNode* node) { \/* Skip in this example. *\/ }\n    void VisitExpr_(const CallNode* call) final { \/* Skip in this example. *\/ }\n\n    \/\/ \u6ce8 2\n    std::string gen(NodeRef&amp; ref) {\n        this-&gt;code = &quot;&quot;;\n        if (ref-&gt;IsInstance&lt;FunctionNode&gt;()) {\n            this-&gt;visit(Downcast&lt;Function&gt;(ref));\n        } else if (ref-&gt;IsInstance&lt;relay::ModuleNode&gt;()) {\n            relay::Module mod = Downcast&lt;relay::Module&gt;(ref);\n            for (const auto&amp; it : mod-&gt;functions) {\n                this-&gt;visit(Downcast&lt;Function&gt;(it.second));\n            }\n        } else {\n            LOG(FATAL) &lt;&lt; &quot;The input ref is expected to be a Relay function or module&quot;;\n        }\n        return this-&gt;code;\n    }\n\n  private:\n      \/*! \/brief The function id that represents a C source function. *\/\n     std::string code;\n}<\/code><\/pre>\n<p><strong>\u6ce81<\/strong>\uff1a\u518d\u6b21\u5b9e\u73b0\u76f8\u5e94\u7684 visitor \u51fd\u6570\uff0c\u4ee5\u751f\u6210 ExampleJSON \u4ee3\u7801\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u7c7b\u53d8\u91cf\u00a0<code>code<\/code>\u00a0\u4e2d\uff08\u7531\u4e8e\u4e0e C codegen \u57fa\u672c\u4e00\u81f4\uff0c\u8fd9\u91cc\u8df3\u8fc7\u4e86 visitor \u51fd\u6570\u7684\u5b9e\u73b0\uff09\u3002\u5b8c\u6210\u8ba1\u7b97\u56fe\u8bbf\u95ee\u540e\uff0c\u5728\u00a0<code>code<\/code>\u00a0\u4e2d\u4f1a\u751f\u6210\u4e00\u4e2a ExampleJSON \u8ba1\u7b97\u56fe\u3002<\/p>\n<p><strong>\u6ce82<\/strong>\uff1a\u5b9a\u4e49\u5185\u90e8 API\u00a0<code>gen<\/code>\u00a0\u6765\u83b7\u53d6\u5b50\u56fe\uff0c\u5e76\u751f\u6210 ExampleJSON \u4ee3\u7801\u3002\u7528\u6237\u53ef\u4ee5\u4f9d\u636e\u4e2a\u4eba\u559c\u597d\uff0c\u4e3a\u8fd9\u4e2a API \u547d\u540d\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 runtime\uff0c\u6765\u5229\u7528\u00a0<code>ExampleJsonCodeGen<\/code>\u00a0\u7684\u8f93\u51fa\u3002<\/p>\n<h4>\u5b9e\u73b0\u81ea\u5b9a\u4e49 runtime<\/h4>\n<p>\u672c\u8282\u5c06\u9010\u6b65\u6f14\u793a\u5982\u4f55\u81ea\u5b9a\u4e49 TVM runtime\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u5230 TVM runtime \u6a21\u5757\u3002\u81ea\u5b9a\u4e49 runtime \u5e94\u4f4d\u4e8e\u00a0<code>src\/runtime\/contrib\/&lt;your-runtime-name&gt;\/<\/code>\u3002\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06 runtime \u547d\u540d\u4e3a &#8220;example_ext_runtime&#8221;\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5982\u4e0b\u6240\u793a\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49 runtime \u7c7b\u3002\u6ce8\u610f\uff1a\u8fd9\u4e2a\u7c7b\u5fc5\u987b\u7531 TVM\u00a0<code>ModuleNode<\/code>\u00a0\u6d3e\u751f\uff0c\u4ee5\u4fdd\u8bc1\u4e0e\u5176\u4ed6 TVM runtime \u6a21\u5757\u517c\u5bb9\u3002<\/p>\n<pre><code>#include &lt;dmlc\/logging.h&gt;\n#include &lt;tvm\/runtime\/c_runtime_api.h&gt;\n#include &lt;tvm\/runtime\/memory.h&gt;\n#include &lt;tvm\/runtime\/module.h&gt;\n#include &lt;tvm\/runtime\/ndarray.h&gt;\n#include &lt;tvm\/runtime\/object.h&gt;\n#include &lt;tvm\/runtime\/packed_func.h&gt;\n#include &lt;tvm\/runtime\/registry.h&gt;\n\n#include &lt;fstream&gt;\n#include &lt;cmath&gt;\n#include &lt;map&gt;\n#include &lt;sstream&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n\nnamespace tvm {\nnamespace runtime {\nclass ExampleJsonModule : public ModuleNode {\n public:\n  explicit ExampleJsonModule(std::string graph_json);\n\n  PackedFunc GetFunction(const std::string&amp; name,\n                         const ObjectPtr&lt;Object&gt;&amp; sptr_to_self) final;\n\n  const char* type_key() const { return &quot;examplejson&quot;; }\n\n  void SaveToBinary(dmlc::Stream* stream) final;\n\n  static Module LoadFromBinary(void* strm);\n\n  static Module Create(const std::string&amp; path);\n\n  std::string GetSource(const std::string&amp; format = &quot;&quot;);\n\n  void Run(int id, const std::vector&lt;int&gt;&amp; inputs, int output);\n\n  void ParseJson(const std::string&amp; json);\n\n private:\n  \/* \/brief \u4ee3\u8868\u8ba1\u7b97\u56fe\u7684 json \u5b57\u7b26\u4e32\u3002 *\/\n  std::string graph_json_;\n  \/* \/brief \u6b63\u5728\u88ab\u5904\u7406\u7684\u5b50\u56fe\u3002 *\/\n  std::string curr_subgraph_;\n  \/*! \/brief \u7531\u5b50\u56fe id \u5230\u8282\u70b9\u6761\u76ee\u7684\u7b80\u5355\u56fe\u3002 *\/\n  std::map&lt;std::string, std::vector&lt;NodeEntry&gt; &gt; graph_;\n  \/* \/brief \u5305\u542b\u56fe\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u7684\u5f20\u91cf\u7684\u7b80\u5355\u6c60\u3002 *\/\n  std::vector&lt;NDArray&gt; data_entry_;\n  \/* \/brief \u4ece\u8282\u70b9 id \u5230\u7b97\u5b50\u540d\u5b57\u7684\u6620\u5c04\u3002 *\/\n  std::vector&lt;std::string&gt; op_id_;\n};<\/code><\/pre>\n<p>\u4ee5\u4e0b\u8fd9\u4e9b\u4ece\u00a0<code>ModuleNode<\/code>\u00a0\u6d3e\u751f\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u5728\u00a0<code>ExampleJsonModule<\/code>\u00a0\u4e2d\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li>\u6784\u9020\u51fd\u6570\uff1a\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5e94\u8be5\u63a5\u6536\u4e00\u4e2a\u8868\u5f81\u4e2d\u7684\u5b50\u56fe\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a\u5904\u7406\u548c\u5b58\u50a8\u7684\u683c\u5f0f\u3002\u4fdd\u5b58\u7684\u5b50\u56fe\u53ef\u4ee5\u88ab\u4ee5\u4e0b\u4e24\u4e2a\u51fd\u6570\u4f7f\u7528\u3002<\/li>\n<li><code>GetFunction<\/code>\uff1a\u8fd9\u662f\u8fd9\u4e2a\u7c7b\u4e2d\u6700\u91cd\u8981\u7684\u51fd\u6570\u3002\u5f53 TVM runtime \u8981\u4f7f\u7528\u7f16\u8bd1\u5668\u6807\u7b7e\uff08compiler tag\uff09\u6267\u884c\u5b50\u56fe\u65f6\uff0c\u5b83\u4f1a\u4ece\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u4e2d\u8c03\u7528\u6b64\u51fd\u6570\u3002\u5b83\u63d0\u4f9b\u51fd\u6570\u540d\u53ca runtime \u53c2\u6570\uff0c<code>GetFunction<\/code>\u00a0\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6253\u5305\u7684\u51fd\u6570\u5b9e\u73b0\uff0c\u4ee5\u4f9b TVM runtime \u6267\u884c\u3002<\/li>\n<li><code>SaveToBinary<\/code>\u00a0\u548c\u00a0<code>LoadFromBinary<\/code>\uff1a<code>SaveToBinary<\/code>\u00a0\u5c06 runtime \u6a21\u5757\u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u4ee5\u4f9b\u540e\u7eed\u90e8\u7f72\u3002\u7528\u6237\u4f7f\u7528\u00a0<code>export_library<\/code>\u00a0API \u65f6\uff0cTVM \u4f1a\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u7531\u4e8e\u7528\u6237\u8fd9\u65f6\u4f7f\u7528\u7684\u662f\u81ea\u5df1\u7684\u8ba1\u7b97\u56fe\u8868\u5f81\uff0c\u56e0\u6b64\u5fc5\u987b\u786e\u4fdd\u00a0<code>LoadFromBinary<\/code>\u00a0\u80fd\u591f\u91c7\u7528<code>SaveToBinary<\/code>\u00a0\u751f\u6210\u7684\u5e8f\u5217\u5316\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6765\u6784\u9020\u76f8\u540c\u7684 runtime \u6a21\u5757\u3002<\/li>\n<li><code>GetSource<\/code>\uff08\u53ef\u9009\uff09\uff1a\u5982\u679c\u60f3\u67e5\u770b\u751f\u6210\u7684 ExampleJSON \u4ee3\u7801\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u51fd\u6570\u6765\u8f6c\u5b58\uff1b\u5426\u5219\u5219\u53ef\u4ee5\u8df3\u8fc7\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u73b0\u6784\u9020\u51fd\u6570<\/h5>\n<pre><code>explicit ExampleJsonModule(std::string graph_json) {\n  this-&gt;graph_json_ = graph_json;\n  ParseJson(this-&gt;graph_json_);\n}<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u5b9e\u73b0\u00a0<code>ParseJson<\/code>\u00a0\u6765\u89e3\u6790 ExampleJSON \u683c\u5f0f\u7684\u5b50\u56fe\uff0c\u5e76\u5728\u5185\u5b58\u4e2d\u6784\u9020\u4e00\u4e2a\u8ba1\u7b97\u56fe\u4f9b\u540e\u7eed\u4f7f\u7528\u3002\u7531\u4e8e\u672c\u793a\u4f8b\u4e0d\u652f\u6301\u5e26\u6709\u5206\u652f\u7684\u5b50\u56fe\uff0c\u56e0\u6b64\u53ea\u9700\u7528\u4e00\u4e2a\u6570\u7ec4\uff0c\u6309\u987a\u5e8f\u5b58\u50a8\u5b50\u56fe\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u3002<\/p>\n<pre><code>void ParseJson(const std::string&amp; json) {\n  std::string line;\n  std::string curr_subgraph;\n  std::stringstream ss(json);\n\n  while (std::getline(ss, line, '\/n')) {\n    std::stringstream ss2(line);\n    std::string token;\n    int id = 0;\n\n    ss2 &gt;&gt; token;\n    if (token.find(&quot;subgraph_&quot;) != std::string::npos) {\n      curr_subgraph = token;\n      continue;\n    }\n\n    ss2 &gt;&gt; id;\n    if (op_id_.size() &lt;= static_cast&lt;size_t&gt;(id)) {\n      op_id_.resize(id + 1);\n      data_entry_.resize(id + 1);\n    }\n\n    int64_t total_elements = 1;\n    std::vector&lt;int64_t&gt; shape;\n    if (token == &quot;input&quot;) {\n      int64_t size = 0;\n      while (ss2 &gt;&gt; size) {\n        total_elements *= size;\n        shape.push_back(size);\n      }\n    } else {\n      op_id_[id] = token; \/\/ \u6ce8 1\n      bool shape_data = false;\n      NodeEntry entry;\n      while (ss2 &gt;&gt; token) {\n        if (token == &quot;shape:&quot;) {\n          shape_data = true;\n        } else if (shape_data) {\n          total_elements *= std::stoll(token);\n          shape.push_back(std::stoll(token));\n        } else if (token != &quot;inputs:&quot;) {\n          entry.inputs.push_back(std::stoi(token));\n        }\n      }\n      entry.id = id;\n      entry.output = id;\n      graph_[curr_subgraph].push_back(entry); \/\/ \u6ce8 2\n    }\n    DLDevice dev;\n    dev.device_type = static_cast&lt;DLDeviceType&gt;(1);\n    dev.device_id = 0;\n    data_entry_[id] = NDArray::Empty(shape, DLDataType{kDLFloat, 32, 1}, dev); \/\/ \u6ce8 3\n  }\n}<\/code><\/pre>\n<p><strong>\u6ce81<\/strong>\uff1a\u4f7f\u7528\u7c7b\u53d8\u91cf\u00a0<code>op_id_<\/code>\u00a0\u5c06\u5b50\u56fe\u8282\u70b9 ID \u6620\u5c04\u5230\u7b97\u5b50\u540d\u79f0\uff08\u4f8b\u5982\u00a0<code>add<\/code>\uff09\uff0c\u4ee5\u4fbf\u5728 runtime \u4e2d\u8c03\u7528\u76f8\u5e94\u7684\u7b97\u5b50\u51fd\u6570\u3002<\/p>\n<p><strong>\u6ce82<\/strong>\uff1a\u4f7f\u7528\u7c7b\u53d8\u91cf\u00a0<code>graph_<\/code>\u00a0\u4ece\u5b50\u56fe\u540d\u79f0\u6620\u5c04\u5230\u8282\u70b9\u6570\u7ec4\u3002<code>GetFunction<\/code>\u00a0\u5c06\u5728 runtime \u901a\u8fc7\u5b50\u56fe ID \u67e5\u8be2\u8ba1\u7b97\u56fe\u8282\u70b9\u3002<\/p>\n<p><strong>\u6ce83<\/strong>\uff1a\u4f7f\u7528\u7c7b\u53d8\u91cf\u00a0<em>data_entry_<\/em> \u00a0\u5c06\u5b50\u56fe\u8282\u70b9 ID \u6620\u5c04\u5230\u5f20\u91cf\u6570\u636e\u5360\u4f4d\u7b26\u3002\u5c06\u8f93\u5165\u548c\u8f93\u51fa\u653e\u5165 runtime \u4e2d\u5bf9\u5e94\u7684\u6570\u636e\u6761\u76ee\u4e2d\u3002<\/p>\n<h5>\u5b9e\u73b0 GetFunction<\/h5>\n<p>\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u540e\uff0c\u4ee5\u4e0a\u7c7b\u53d8\u91cf\u51c6\u5907\u5c31\u7eea\u3002\u63a5\u4e0b\u6765\u5b9e\u73b0\u00a0<code>GetFunction<\/code>\u00a0\u4e3a TVM runtime \u63d0\u4f9b\u53ef\u6267\u884c\u7684\u5b50\u56fe\u51fd\u6570\uff1a<\/p>\n<pre><code>PackedFunc GetFunction(const std::string&amp; name,\n                       const ObjectPtr&lt;Object&gt;&amp; sptr_to_self) final {\n  if (this-&gt;graph_.find(name) != this-&gt;graph_.end()) {\n    this-&gt;curr_subgraph_ = name;\n    return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {\n\n      \/\/ Copy input tensors to corresponding data entries.\n      for (auto i = 0; i &lt; args.size(); ++i) {\n        ICHECK(args[i].type_code() == kNDArrayContainer || args[i].type_code() == kArrayHandle)\n            &lt;&lt; &quot;Expect NDArray or DLTensor as inputs\/n&quot;;\n        if (args[i].type_code() == kArrayHandle) {\n          DLTensor* arg = args[i];\n          this-&gt;data_entry_[i].CopyFrom(arg);\n        } else {\n          NDArray arg = args[i];\n          this-&gt;data_entry_[i].CopyFrom(arg);\n        }\n      }\n\n      \/\/ Execute the subgraph.\n      for (const auto&amp; it : this-&gt;graph_[this-&gt;curr_subgraph_]) {\n        this-&gt;Run(it.id, it.inputs, it.output);\n      }\n      ICHECK_GT(graph_.count(this-&gt;curr_subgraph_), 0U);\n\n      \/\/ Copy the output from a data entry back to TVM runtime argument.\n      auto out_idx = graph_[this-&gt;curr_subgraph_].back().output;\n      if (args[args.size() - 1].type_code() == kArrayHandle) {\n        DLTensor* arg = args[args.size() - 1];\n        this-&gt;data_entry_[out_idx].CopyTo(arg);\n      } else {\n        NDArray arg = args[args.size() - 1];\n        this-&gt;data_entry_[out_idx].CopyTo(arg);\n      }\n      *rv = data_entry_.back();\n    });\n  } else {\n    LOG(FATAL) &lt;&lt; &quot;Unknown subgraph: &quot; &lt;&lt; name &lt;&lt; &quot;\/n&quot;;\n    return PackedFunc();\n  }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c<code>GetFunction<\/code>\u00a0\u7531\u4e09\u4e2a\u4e3b\u8981\u90e8\u5206\u7ec4\u6210\u3002\u7b2c\u4e00\u90e8\u5206\u5c06\u6570\u636e\u4ece TVM runtime \u53c2\u6570\uff0c\u590d\u5236\u5230\u6784\u9020\u51fd\u6570\u4e2d\u6307\u5b9a\u7684\u5bf9\u5e94\u6570\u636e\u6761\u76ee\u3002\u7b2c\u4e8c\u90e8\u5206\u4f7f\u7528\u00a0<code>Run<\/code>\u00a0\u51fd\u6570\u6267\u884c\u5b50\u56fe\uff08\u5e76\u7a0d\u540e\u5b9e\u73b0\uff09\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u53e6\u4e00\u4e2a\u6570\u636e\u6761\u76ee\u3002\u7b2c\u4e09\u90e8\u5206\u5c06\u8f93\u51fa\u6570\u636e\u6761\u76ee\u4e2d\u7684\u7ed3\u679c\uff0c\u590d\u5236\u56de\u5bf9\u5e94\u7684 TVM runtime \u53c2\u6570\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n<h5>\u5b9e\u73b0 Run<\/h5>\n<p><code>Run<\/code>\u00a0\u51fd\u6570\u63a5\u6536 1\uff09\u5b50\u56fe ID\uff0c2\uff09\u8f93\u5165\u6570\u636e\u6761\u76ee\u7d22\u5f15\u5217\u8868\u548c 3\uff09\u8f93\u51fa\u6570\u636e\u6761\u76ee\u7d22\u5f15\u3002<\/p>\n<pre><code>void Run(int id, const std::vector&lt;int&gt;&amp; inputs, int output) {\n  \/\/ Make a list data entry indexs.\n  std::vector&lt;int&gt; args(inputs.begin(), inputs.end());\n  args.push_back(output);\n\n  \/\/ Initialize data holders.\n  std::vector&lt;TVMValue&gt; values(args.size());\n  std::vector&lt;int&gt; type_codes(args.size());\n\n  \/\/ Initialize a TVM arg setter with TVMValue and its type code.\n  TVMArgsSetter setter(values.data(), type_codes.data());\n\n  \/\/ Set each argument to its corresponding data entry.\n  if (op_id_[id] == &quot;add&quot; || op_id_[id] == &quot;sub&quot; || op_id_[id] == &quot;mul&quot;) {\n    for (size_t i = 0; i &lt; args.size(); i++) {\n      setter(i, data_entry_[args[i]]);\n    }\n  }\n\n  \/\/ Invoke the corresponding operator function.\n  if (op_id_[id] == &quot;add&quot;) {\n    Add(values.data(), type_codes.data(), args.size());\n  } else if (op_id_[id] == &quot;sub&quot;) {\n    Sub(values.data(), type_codes.data(), args.size());\n  } else if (op_id_[id] == &quot;mul&quot;) {\n    Mul(values.data(), type_codes.data(), args.size());\n  } else {\n    LOG(FATAL) &lt;&lt; &quot;Unknown op: &quot; &lt;&lt; op_id_[id] &lt;&lt; &quot;\/n&quot;;\n  }\n}<\/code><\/pre>\n<p><code>Run<\/code>\u00a0\u51fd\u6570\u4e3b\u8981\u5305\u62ec\u4e24\u90e8\u5206\u3002\u7b2c\u4e00\u90e8\u5206\u8d1f\u8d23\u5206\u914d\u00a0<code>TVMValue<\/code>\u00a0\u5217\u8868\uff0c\u5e76\u6620\u5c04\u76f8\u5e94\u7684\u6570\u636e\u8f93\u5165\u5757\u3002\u8fd9\u4e5f\u4f1a\u6210\u4e3a\u7b97\u5b50\u51fd\u6570\u7684\u53c2\u6570\u3002\u7b2c\u4e8c\u90e8\u5206\u8c03\u7528\u7b97\u5b50\u51fd\u6570\u3002\u5c3d\u7ba1\u4f7f\u7528\u7684 C \u51fd\u6570\u4e0e\u4e0a\u4e00\u4e2a\u793a\u4f8b\u76f8\u540c\uff0c\u4f46\u7528\u6237\u53ef\u4ee5\u5c06\u00a0<code>Add<\/code>\u3001<code>Sub<\/code>\u00a0\u548c\u00a0<code>Mul<\/code>\u00a0\u66ff\u6362\u4e3a\u81ea\u5df1\u7684\u5f15\u64ce\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u9700\u8981\u786e\u4fdd\u5f15\u64ce\u5c06\u7ed3\u679c\u5b58\u50a8\u5230\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff0c\u4ece\u800c\u4f7f\u5f97\u5b83\u4eec\u53ef\u4ee5\u4f20\u8f93\u56de TVM runtime\u3002<\/p>\n<p>\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u540e\uff0c\u7528\u6237\u81ea\u5b9a\u4e49\u7684 codegen \u548c runtime \u5c31\u53ef\u4ee5\u6267\u884c\u5b50\u56fe\u4e86\u3002\u6700\u540e\u4e00\u6b65\u662f\u6ce8\u518c\u4e00\u4e2a API\uff08<code>examplejson_module_create<\/code>\uff09\u6765\u521b\u5efa\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<pre><code>TVM_REGISTER_GLOBAL(&quot;module.examplejson_module_create&quot;)\n.set_body_typed([](std::string code){\n    auto n = make_object&lt;ExampleJsonModule&gt;(code);\n    return runtime::Module(n);\n});<\/code><\/pre>\n<h5>\u5b9e\u73b0 SaveToBinary \u548c LoadFromBinary<\/h5>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u4e0e\u5176\u4ed6 TVM runtime \u7528\u6cd5\u4e00\u81f4\u7684\u81ea\u5b9a\u4e49 runtime \u7684\u4e3b\u8981\u529f\u80fd\u3002\u4f46\u662f\uff0c\u5f53\u7528\u6237\u60f3\u8981\u5c06\u6784\u5efa\u7684 runtime \u4fdd\u5b58\u5230\u78c1\u76d8\u4ee5\u8fdb\u884c\u90e8\u7f72\u65f6\uff0cTVM \u4e0d\u77e5\u9053\u5982\u4f55\u4fdd\u5b58\u3002\u8fd9\u5c31\u662f\u5b9e\u73b0\u00a0<code>SaveToBinary<\/code>\u00a0\u548c\u00a0<code>LoadFromBinary<\/code>\u00a0\u7684\u539f\u56e0\uff0c\u5b83\u4eec\u4f1a\u544a\u8bc9 TVM \u8fd9\u4e2a\u81ea\u5b9a\u4e49 runtime \u5982\u4f55\u6301\u4e45\u5316\u548c\u590d\u539f\u3002<\/p>\n<p>\u9996\u5148\u5b9e\u73b0\u00a0<code>SaveToBinary<\/code>\u00a0\u51fd\u6570\uff0c\u5141\u8bb8\u7528\u6237\u5c06\u6b64\u6a21\u5757\u4fdd\u5b58\u5728\u78c1\u76d8\u4e2d\u3002<\/p>\n<pre><code>void SaveToBinary(dmlc::Stream* stream) final {\n    stream-&gt;Write(this-&gt;graph_json_);\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u7684\u552f\u4e00\u53c2\u6570\u662f\u4e00\u4e2a\u5b50\u56fe\u8868\u5f81\uff08subgraph representation\uff09\u3002\u4e5f\u5c31\u662f\u8bf4\u53ea\u9700\u4e00\u4e2a\u5b50\u56fe\u8868\u5f81\u6765\u6784\u9020\/\u6062\u590d\u8fd9\u4e2a\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u3002<code>SaveToBinary<\/code>\u00a0\u53ea\u662f\u5c06\u5b50\u56fe\u5199\u5230\u4e00\u4e2a\u8f93\u51fa\u7684 DMLC \u6d41\u4e2d\uff0c\u5f53\u7528\u6237\u4f7f\u7528\u00a0<code>export_library<\/code>\u00a0API \u8f93\u51fa\u6a21\u5757\u65f6\uff0c\u81ea\u5b9a\u4e49\u6a21\u5757\u5c06\u662f\u4e00\u4e2a\u5b50\u56fe\u7684 ExampleJSON \u6d41\u3002<\/p>\n<p><code>LoadFromBinary<\/code>\u00a0\u8bfb\u53d6\u5b50\u56fe\u6d41\u5e76\u91cd\u65b0\u6784\u5efa\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u7684\u6d41\u7a0b\u4e0e\u6b64\u7c7b\u4f3c\uff1a<\/p>\n<pre><code>static Module LoadFromBinary(void* strm) {\n  dmlc::Stream* stream = static_cast&lt;dmlc::Stream*&gt;(strm);\n  std::string graph_json;\n  stream-&gt;Read(&amp;graph_json);\n  auto n = tvm::runtime::make_object&lt;ExampleJsonModule&gt;(graph_json);\n  return Module(n);\n}<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u518c\u4ee5\u4e0b\u51fd\u6570\uff0c\u542f\u7528\u76f8\u5e94\u7684 Python API\uff1a<\/p>\n<pre><code>TVM_REGISTER_GLOBAL(&quot;module.loadbinary_examplejson&quot;)\n.set_body_typed(ExampleJsonModule::LoadFromBinary);<\/code><\/pre>\n<p>\u4e0a\u8ff0\u6ce8\u518c\u610f\u5473\u7740\u5f53\u7528\u6237\u8c03\u7528\u00a0<code>tvm.runtime.load_module(lib_path)<\/code>\u00a0API\uff0c\u5e76\u4e14\u5bfc\u51fa\u5e93\u6709\u4e00\u4e2a ExampleJSON \u6d41\u65f6\uff0c<code>LoadFromBinary<\/code>\u00a0\u5c06\u88ab\u8c03\u7528\u4ee5\u521b\u5efa\u76f8\u540c\u7684\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u60f3\u652f\u6301\u76f4\u63a5\u4ece ExampleJSON \u6587\u4ef6\u521b\u5efa\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5e76\u6ce8\u518c\u4e00\u4e2a Python API\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>static Module Create(const std::string&amp; path) {\n    std::ifstream filep;\n    filep.open(path, std::ios::in);\n    std::string graph_json;\n    std::string line;\n    while (std::getline(filep, line)) {\n        graph_json += line;\n        graph_json += &quot;\/n&quot;;\n    }\n    filep.close();\n    auto n = tvm::runtime::make_object&lt;ExampleJsonModule&gt;(graph_json);\n    return Module(n);\n}\n\nTVM_REGISTER_GLOBAL(&quot;module.loadfile_examplejson&quot;)\n.set_body([](TVMArgs args, TVMRetValue* rv) {\n    *rv = ExampleJsonModule::Create(args[0]);\n});<\/code><\/pre>\n<p>\u8fd9\u610f\u5473\u7740\u7528\u6237\u53ef\u4ee5\u624b\u52a8\u7f16\u5199\/\u4fee\u6539 ExampleJSON \u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 Python API\u00a0<code>tvm.runtime.load_module(&quot;mysubgraph.examplejson&quot;, &quot;examplejson&quot;)<\/code>\u00a0\u6784\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u6c47\u603b\u524d\u6587\u91cd\u70b9\uff1a<\/p>\n<ul>\n<li>\n<p>\u4ece\u00a0<code>ExprVisitor<\/code>\u00a0\u548c\u00a0<code>CodegenCBase<\/code>\uff08\u4ec5\u9002\u7528\u4e8e C codegen\uff09\u6d3e\u751f\u7684 codegen \u7c7b\uff0c\u5177\u6709\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><code>VisitExpr_(const CallNode* call)<\/code>\u00a0\u6536\u96c6\u8c03\u7528\u8282\u70b9\u4fe1\u606f\u3002<\/li>\n<li>\u6536\u96c6\u5b50\u56fe\u4fe1\u606f\u6240\u9700\u7684\u5176\u4ed6 visitor \u51fd\u6570\u3002<\/li>\n<li><code>JIT<\/code>\u00a0\u751f\u6210\u5b50\u56fe\u4ee3\u7801\u3002<\/li>\n<li>\u6ce8\u518c codegen\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u521b\u5efa\u00a0<code>CSourceModule<\/code>\u00a0\u7684\u51fd\u6570\uff08\u7528\u4e8e C codegen\uff09\u3002<\/li>\n<li>\n<p>\u4ece\u00a0<code>ModuleNode<\/code>\u00a0\u6d3e\u751f\u7684 runtime \u6a21\u5757\u7c7b\uff0c\u5177\u6709\u4ee5\u4e0b\u529f\u80fd\uff08\u7528\u4e8e\u8ba1\u7b97\u56fe\u8868\u5f81\uff09\u3002<\/p>\n<ul>\n<li>\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li><code>GetFunction<\/code>\u00a0\u751f\u6210\u4e0e TVM runtime \u517c\u5bb9\u7684\u00a0<code>PackedFunc<\/code>\u3002<\/li>\n<li><code>Run<\/code>\u00a0\u6267\u884c\u5b50\u56fe\u3002<\/li>\n<li>\u6ce8\u518c runtime creation API\u3002<\/li>\n<li><code>SaveToBinary<\/code>\u00a0\u548c\u00a0<code>LoadFromBinary<\/code>\u00a0\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u3002<\/li>\n<li>\u6ce8\u518c\u00a0<code>LoadFromBinary<\/code>\u00a0API \u4e3a<code>tvm.runtime.load_module(your_module_lib_path)<\/code>\u63d0\u4f9b\u652f\u6301\u3002<\/li>\n<li>\uff08\u53ef\u9009\uff09<code>Create<\/code>\u00a0\u652f\u6301\u4ece\u8868\u5f81\u7684\u5b50\u56fe\u6587\u4ef6\uff0c\u6784\u5efa\u81ea\u5b9a\u4e49 runtime \u6a21\u5757\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e00\u4e2a\u6ce8\u91ca\u5668\uff0c\u7528\u4e8e\u6ce8\u91ca\u7528\u6237 Relay \u7a0b\u5e8f\uff0c\u5229\u7528\u7f16\u8bd1\u5668\u548c runtime\uff08\u5f85\u5b9a\uff09\u3002<\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047201566\">https:\/\/segmentfault.com\/a\/1190000047201566<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache TVM \u662f\u4e00\u4e2a\u6df1\u5ea6\u7684\u6df1\u5ea6\u5b66\u4e60\u7f16\u8bd1\u6846\u67b6\uff0c\u9002\u7528\u4e8e CPU\u3001GPU \u548c\u5404\u79cd\u673a\u5668\u5b66\u4e60\u52a0\u901f\u82af\u7247\u3002\u66f4\u591a TV [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020976","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020976"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020976\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}