{"id":10006872,"date":"2022-11-18T17:41:03","date_gmt":"2022-11-18T09:41:03","guid":{"rendered":"https:\/\/ityet.com\/?p=10006872"},"modified":"2022-11-18T17:41:03","modified_gmt":"2022-11-18T09:41:03","slug":"dive-into-tensorflow%e7%b3%bb%e5%88%97%ef%bc%883%ef%bc%89-%e6%8f%ad%e5%bc%80tensor%e7%9a%84%e7%a5%9e%e7%a7%98%e9%9d%a2%e7%ba%b1","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006872","title":{"rendered":"Dive into TensorFlow\u7cfb\u5217\uff083\uff09- \u63ed\u5f00Tensor\u7684\u795e\u79d8\u9762\u7eb1"},"content":{"rendered":"<p>TensorFlow\u8ba1\u7b97\u56fe\u662f\u7531op\u548ctensor\u7ec4\u6210\uff0c\u90a3\u4e48tensor\u4e00\u822c\u90fd\u7528\u6765\u4ee3\u8868\u4ec0\u4e48\u5462\uff1f\u663e\u7136\uff0c\u50cf\u6a21\u578b\u7684\u8f93\u5165\u6570\u636e\u3001\u7f51\u7edc\u6743\u91cd\u3001\u8f93\u5165\u6570\u636e\u7ecfop\u5904\u7406\u540e\u7684\u8f93\u51fa\u7ed3\u679c\u90fd\u9700\u8981\u7528\u5f20\u91cf\u6216\u7279\u6b8a\u5f20\u91cf\u8fdb\u884c\u8868\u8fbe\u3002\u65e2\u7136tensor\u5728TensorFlow\u4f53\u7cfb\u67b6\u6784\u4e2d\u5982\u6b64\u91cd\u8981\uff0c\u56e0\u6b64\u672c\u6587\u5c06\u5e26\u9886\u5927\u5bb6\u7531\u6d45\u5165\u6df1\u5730\u5b66\u4e60tensor\u7684\u4e09\u4e2a\u8bdd\u9898\uff1a\u7528\u6237\u773c\u4e2d\u7684tensor\u3001TensorFlow\u7cfb\u7edf\u4e2d\u7684tensor\u3001tensor\u9ad8\u9636\u7528\u6cd5DLPack\uff08\u8de8\u6846\u67b6\u7f16\u7a0b\uff0c\u5982\uff1aTensorFlow+PyTorch\uff09\u3002<\/p>\n<p><strong>\u6ce8\uff1a\u672c\u6587\u57fa\u4e8eTensorFlow v1.15.5\u8fdb\u884c\u7f16\u5199\u3002<\/strong><\/p>\n<h2>\u4e00\u3001\u5c0f\u767d\u773c\u4e2d\u7684Tensor<\/h2>\n<h3>1.1 Tensor HelloWorld<\/h3>\n<p>\u5b9a\u4e49\u4e24\u4e2a\u5f20\u91cf\uff0c\u7136\u540e\u5bf9\u5176\u6c42\u52a0\u6cd5\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code># segment 1\na = tf.constant(3.0, dtype=tf.float32)\nb = tf.constant(4.0) # also tf.float32 implicitly\ntotal = a + b\nprint(a)\nprint(b)\nprint(total)\n\n### \u4e09\u4e2aprint\u7684\u8f93\u51fa\u5982\u4e0b\uff1a\n&quot;&quot;&quot;\nTensor(&quot;Const:0&quot;, shape=(), dtype=float32)\nTensor(&quot;Const_1:0&quot;, shape=(), dtype=float32)\nTensor(&quot;add:0&quot;, shape=(), dtype=float32)\n&quot;&quot;&quot;\n# \u8bf4\u660e\uff1a\u6b64\u65f6\u7684Tenosr\u5c1a\u4e0d\u80fd\u4ea7\u751f\u771f\u6b63\u7684\u7ed3\u679c\u3002\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u8ba1\u7b97\u56fe\uff0cTensor\u53ea\u662f\u4ee3\u8868op\u8fd0\u884c\u7684\u7ed3\u679c(\u4f46\u6b64\u65f6op\u672a\u8fd0\u884c)\u3002\n<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u770b\u5230\u6700\u7ec8total\u7684\u8ba1\u7b97\u7ed3\u679c\uff0c\u5219\u5e94\u8be5\u521b\u5efaSession\u5bf9\u8c61\u5e76\u8fd0\u884c\u8ba1\u7b97\u56fe\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff08\u5728segment1\u57fa\u7840\u4e0a\u589e\u52a0\u4ee3\u7801\uff09\uff1a<\/p>\n<pre><code>with tf.Session() as sess:\n    result = sess.run(total)\n    print(result, type(result), type(total))\n# \u8f93\u51fa\u7ed3\u679c= 7.0 &lt;class 'numpy.float32'&gt; &lt;class 'tensorflow.python.framework.ops.Tensor'&gt;\n<\/code><\/pre>\n<p>\u7531\u6b64\u53ef\u89c1\uff0cTensor\u4ee3\u8868\u5c1a\u672a\u6267\u884c\u7684\u7ed3\u679c\u8868\u793a\uff0c\u521b\u5efaSession\u5bf9\u8c61\u5e76\u8fd0\u884c\u8ba1\u7b97\u56fe\u53ef\u5f97total\u7ed3\u679c7.0\uff0c\u5e76\u4e14\u7ed3\u679c\u7684\u6570\u636e\u7c7b\u578b\u5df2\u53d8\u4e3anumpy\u3002\u6700\u540e\u8bf4\u660e\u4e00\u4e0b\uff0c\u672c\u5c0f\u8282\u4ee3\u7801\u8f93\u51fa\u7684Tensor\u662f\u6307tf.Tensor\uff0c\u5bf9\u5e94\u7684\u4ee3\u7801\u5b9e\u73b0\u662f<strong>tensorflow.python.framework.ops.Tensor<\/strong>\u3002<\/p>\n<h3>1.2 \u5f20\u91cf\u5c5e\u6027\u53ca\u7279\u6b8a\u5f20\u91cf<\/h3>\n<p>\u4ece\u7528\u6237\u89c6\u89d2\u770btf.Tensor\u4e3b\u8981\u6709\u4e09\u4e2a\u5c5e\u6027\uff1aname\u3001dtype\u3001shape\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u4e09\u4e2a\u5c5e\u6027\u6bd4\u8f83\u91cd\u8981\uff08\u4e0d\u5e38\u7528\u6216\u8005\u4e0d\u76f4\u63a5\u53ef\u89c1\uff09\uff1aop\u3001graph\u3001device\u3002\u5176\u4e2dop\u5c5e\u6027\u8bb0\u5f55\u4ea7\u751f\u6b64Tensor\u7684\u64cd\u4f5c\u540d\u79f0\uff0cgraph\u5c5e\u6027\u8bb0\u5f55\u5305\u542b\u6b64Tensor\u7684\u8ba1\u7b97\u56fe\uff0cdevice\u5c5e\u6027\u8bb0\u5f55\u4ea7\u751f\u6b64Tensor\u7684\u8bbe\u5907\u540d\u79f0\u3002<\/p>\n<p>\u5728TensorFlow\u4f53\u7cfb\u4e2d\u6709\u56db\u79cd\u7279\u6b8a\u7684\u5f20\u91cf\uff08\u6b64\u5904\u6682\u4e0d\u4e25\u683c\u533a\u5206Tensor\u4e0e\u4ea7\u751f\u6b64Tensor\u7684op\uff09\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<p>\u2022 tf.Variable: \u5b9a\u4e49\u5185\u5bb9\u53ef\u53d8\u7684\u5f20\u91cf\uff0c\u4e00\u822c\u7528\u6765\u5b9a\u4e49\u6a21\u578b\u6743\u91cd\u3002<\/p>\n<p>\u2022 tf.constant: \u4e00\u822c\u6765\u8bf4\uff0c\u5f20\u91cf\u5185\u5bb9\u4e0d\u53ef\u53d8\uff0c\u6b64API\u53ef\u7528\u6765\u5b9a\u4e49\u5e38\u89c4\u5f20\u91cf\u3002<\/p>\n<p>\u2022 tf.placeholder: \u5360\u4f4d\u7b26\u5f20\u91cf\uff0c\u7528\u4e8e\u63cf\u8ff0\u9759\u6001\u56fe\u8f93\u5165\u89c4\u683c\u3002\u9759\u6001\u56fe\u91c7\u7528\u5148\u7f16\u8bd1\u540e\u6267\u884c\u7684\u65b9\u5f0f\uff0c\u56e0\u6b64\u5728\u5b9a\u4e49\u8ba1\u7b97\u56fe\u65f6\u8981\u77e5\u9053\u8f93\u5165\u89c4\u683c\u3002<\/p>\n<p>\u2022 tf.SparseTensor: \u4e3a\u7a00\u758f\u6570\u636e\u5b9a\u5236\u7684\u5f20\u91cf\u7ed3\u6784\u3002<\/p>\n<h3>1.3 Tensor\u4e0eop\u7684\u5173\u7cfb<\/h3>\n<p>\u6211\u4eec\u591a\u6b21\u63d0\u5230\uff0cTensor\u53ef\u4ee5\u4f5c\u4e3aop\u7684\u8f93\u5165\uff0c\u7ecfop\u4e00\u7cfb\u5217\u5904\u7406\u540e\u4ea7\u751f\u65b0\u7684Tensor\u4f5c\u4e3a\u8f93\u51fa\u3002\u4e3a\u4e86\u6df1\u5165\u7406\u89e3\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u56de\u5934\u91cd\u65b0\u5ba1\u89c6segment1\u4e2d\u7684\u4ee3\u7801\u7247\u6bb5\uff08\u8bf7\u5927\u5bb6\u6ce8\u610fTensor\u7684\u547d\u540d\uff09\uff1a<\/p>\n<pre><code># segment 1\na = tf.constant(3.0, dtype=tf.float32)\nb = tf.constant(4.0) # also tf.float32 implicitly\ntotal = a + b\nprint(a)\nprint(b)\nprint(total)\n\n### \u4e09\u4e2aprint\u7684\u8f93\u51fa\u5982\u4e0b\uff1a\n&quot;&quot;&quot;\nTensor(&quot;Const:0&quot;, shape=(), dtype=float32)\nTensor(&quot;Const_1:0&quot;, shape=(), dtype=float32)\nTensor(&quot;add:0&quot;, shape=(), dtype=float32)\n&quot;&quot;&quot;\n# \u8bf4\u660e\uff1a\u6b64\u65f6\u7684Tenosr\u5c1a\u4e0d\u80fd\u4ea7\u751f\u771f\u6b63\u7684\u7ed3\u679c\u3002\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u8ba1\u7b97\u56fe\uff0cTensor\u53ea\u662f\u4ee3\u8868op\u8fd0\u884c\u7684\u7ed3\u679c(\u4f46\u6b64\u65f6op\u672a\u8fd0\u884c)\u3002\n<\/code><\/pre>\n<p>\u9488\u5bf9\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u5148\u6765\u770b\u770b\u54ea\u4e9b\u662fTensor\uff0c\u54ea\u4e9b\u662fop\uff0c\u7136\u540e\u57fa\u4e8e\u6b64\u5206\u522b\u63cf\u8ff0\u6bcf\u4e00\u4e2a\u64cd\u4f5c\u7684\u6267\u884c\u8fc7\u7a0b\u3002\u4e3a\u56de\u7b54\u7b2c\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u5148\u770b\u4e00\u6bb5TensorFlow\u5b98\u65b9\u6ce8\u91ca\uff1a<\/p>\n<pre><code>&quot;&quot;&quot;\n`tf.constant` creates a `Const` node in the computation graph with the\n \nexact value at graph construction time.\n&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\u7531\u6b64\u53ef\u89c1\uff0csegment1\u7684\u4ee3\u7801\u4e2d\u6709\u4e24\u79cdop\uff0c\u5206\u522b\u4e3aConst\u548cadd\uff0c\u524d\u8005\u51fa\u73b0\u4e86\u4e24\u6b21\uff0c\u800c\u540e\u80051\u6b21\u3002\u57fa\u4e8e\u6b64\uff0c\u6211\u4eec\u5f97\u77e5segment1\u4f9d\u6b21\u5411\u8ba1\u7b97\u56fe\u4e2d\u6dfb\u52a0\u4e86\u4e09\u4e2aop\uff0c\u4e0e\u6b64\u540c\u65f6\u4e5f\u53ef\u4ee5\u56de\u7b54\u7b2c\u4e8c\u4e2a\u95ee\u9898\uff0c\u5373\u6bcf\u4e2a\u64cd\u4f5c\u7684\u8fc7\u7a0b\u3002\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre><code>### \u4e09\u4e2aprint\u7684\u8f93\u51fa\u5982\u4e0b(a,b,total)\uff1a\n&quot;&quot;&quot;\nTensor(&quot;Const:0&quot;, shape=(), dtype=float32)\nTensor(&quot;Const_1:0&quot;, shape=(), dtype=float32)\nTensor(&quot;add:0&quot;, shape=(), dtype=float32)\n&quot;&quot;&quot;\n# \u5411\u8ba1\u7b97\u56fe\u6dfb\u52a0\u7b2c\u4e00\u4e2aop(Const),\u8f93\u5165\u662f\u4e00\u4e2a\u6807\u91cf,\u8f93\u51fa\u662fTensor a,\u5176\u540d\u79f0\u7531\u4e24\u90e8\u5206\u7ec4\u6210,\u5373op\u540d\u79f0:a\u5728op\u8f93\u51fa\u7684\u7d22\u5f15\u4f4d\u7f6e.\n# \u5411\u8ba1\u7b97\u56fe\u6dfb\u52a0\u7b2c\u4e8c\u4e2aop(Const_1,\u56e0\u4e3aop\u540d\u79f0\u8981\u552f\u4e00),\u8f93\u5165\u6807\u91cf,\u8f93\u51faTensor b,\u5176\u547d\u540d\u89c4\u5219\u540c\u4e0a.\n# \u5411\u8ba1\u7b97\u56fe\u6dfb\u52a0\u7b2c\u4e09\u4e2aop(add),\u8f93\u5165\u662fTensor a\u548cb,\u8f93\u51faTensor total,\u5176\u547d\u540d\u89c4\u5219\u540c\u4e0a.\n<\/code><\/pre>\n<h2>\u4e8c\u3001\u4e00\u63a2tensor\u7a76\u7adf<\/h2>\n<h3>2.1 \u524d\u540e\u7aefTensor\u6620\u5c04<\/h3>\n<p>\u5728TensorFlow\u7684\u767d\u76ae\u4e66[7]\u4e2d\u63d0\u5230C API\u662f\u8fde\u63a5\u524d\u7aef\u7528\u6237\u4ee3\u7801\u548c\u540e\u7aef\u6267\u884c\u5f15\u64ce\u7684\u6865\u6881\uff0c\u4e3a\u6df1\u5165\u7406\u89e3\u8fd9\u4e2a\u6982\u5ff5\uff0c\u5efa\u8bae\u8bfb\u8005\u53c2\u7167TensorFlow\u5b98\u7f51\u4ece\u5934\u7f16\u8bd1\u6e90\u4ee3\u7801\u3002TensorFlow v1.15.5\u57fa\u4e8eBazel\u8fdb\u884c\u7f16\u8bd1\uff0c\u524d\u7aefpython\u4e0e\u540e\u7aefC++\u901a\u8fc7SWIG\u8fdb\u884c\u4ea4\u4e92\u3002\u5b9e\u9645\u4e0a\u5728\u7cfb\u7edf\u7f16\u8bd1\u4e4b\u524d\u4f1a\u5148\u542f\u52a8SWIG\u4ee3\u7801\u751f\u6210\u8fc7\u7a0b\uff0c\u901a\u8fc7\u89e3\u6790tensorflow.i\u81ea\u52a8\u751f\u6210\u4e24\u4e2awrapper\u6587\u4ef6\uff1apywrap\/_tensorflow\/_internal.py\u548cpywrap\/_tensorflow\/_internal.cc\uff0c\u524d\u8005\u5bf9\u63a5\u524d\u7aefpython\u8c03\u7528\uff0c\u540e\u8005\u5bf9\u63a5\u540e\u7aefC API\u8c03\u7528\u3002\u5927\u5bb6\u5b89\u88c5tensorflow\u5b98\u65b9\u4e8c\u8fdb\u5236\u5305\u540e\uff0c\u53ea\u80fd\u770b\u5230py\u6587\u4ef6\u800c\u6ca1\u6709cc\u6587\u4ef6\u3002\u5982\u679c\u81ea\u5df1\u7f16\u8bd1TensorFlow\u6e90\u7801\uff0c\u53ef\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684bazel-bin\u4e2d\u627e\u5230\u76f8\u5e94\u7684py\u548ccc\u6587\u4ef6\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838111\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0a\u56fe\u7ea2\u6846\u4e2d\u7684so\u6587\u4ef6\u662f\u7531cc\u6587\u4ef6\u7f16\u8bd1\u5f97\u5230\uff0c\u9ec4\u6846\u4e2d\u7684py\u6a21\u5757\u9996\u6b21\u88ab\u5bfc\u5165\u65f6\uff0c\u4f1a\u81ea\u52a8\u52a0\u8f7dso\u52a8\u6001\u94fe\u63a5\u5e93\u3002\u800c\u5728so\u5bf9\u5e94\u7684cc\u6587\u4ef6\u4e2d\uff0c\u9759\u6001\u6ce8\u518c\u4e86\u4e00\u4e2a\u51fd\u6570\u6620\u5c04\u8868\uff0c\u5b9e\u73b0python\u51fd\u6570\u5230C\u51fd\u6570\u7684\u6620\u5c04\u3002\u6b64\u6620\u5c04\u8868\u7ed3\u6784\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<pre><code>static PyMethodDef SwigMethods[] = {\n          { (char *)&quot;SWIG_PyInstanceMethod_New&quot;, (PyCFunction)SWIG_PyInstanceMethod_New, METH_O, NULL},\n          { (char *)&quot;TF_OK_swigconstant&quot;, TF_OK_swigconstant, METH_VARARGS, NULL},\n          { (char *)&quot;TF_CANCELLED_swigconstant&quot;, TF_CANCELLED_swigconstant, METH_VARARGS, NULL},\n          { (char *)&quot;TF_UNKNOWN_swigconstant&quot;, TF_UNKNOWN_swigconstant, METH_VARARGS, NULL},\n          { (char *)&quot;TF_INVALID_ARGUMENT_swigconstant&quot;, TF_INVALID_ARGUMENT_swigconstant, METH_VARARGS, NULL},\n          \/\/ \u6b64\u5904\u7701\u7565\u8bb8\u591a\u4ee3\u7801\n};\n<\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u4eb2\u8eab\u5b9e\u8df5\uff0c\u4e0a\u9762\u8fd9\u4e9b\u6587\u5b57\u8bfb\u8d77\u6765\u591a\u5c11\u6709\u4e9b\u5403\u529b\u3002\u4e3a\u4fbf\u4e8e\u5927\u5bb6\u7406\u89e3\uff0c\u6211\u4eec\u628a\u4e0a\u8ff0\u6587\u5b57\u7528\u5982\u4e0b\u7b80\u56fe\u8fdb\u884c\u603b\u7ed3\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838112\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6709\u4e9b\u597d\u5947\u5b9d\u5b9d\u53ef\u80fd\u4f1a\u8bf4\uff1a\u4e0a\u9762\u8bb2\u7684\u592a\u5b8f\u89c2\uff0c\u597d\u50cf\u61c2\u4e86\uff0c\u53c8\u597d\u50cf\u6ca1\u61c2\u3002\u6ca1\u5173\u7cfb\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4ee5\u9759\u6001\u56fe\u7684\u8fd0\u884c\u63a5\u53e3session.run()\u4e3a\u4f8b\uff0c\u7ed3\u5408TensorFlow\u6e90\u7801\u8be6\u7ec6\u68b3\u7406\u4e00\u4e0b\u524d\u540e\u7aef\u7684\u6620\u5c04\u8fc7\u7a0b\uff0c\u5177\u4f53\u8fc7\u7a0b\u89c1\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838113\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7531\u4e0a\u56fe\u6211\u4eec\u53ef\u6e05\u6670\u770b\u5230C API\u5c42\u628a\u524d\u540e\u7aef\u7ed9\u9694\u79bb\u5f00\u4e86\uff0c\u5f53\u7136C API\u5c42\u5305\u62ecpywrap\/_tensorflow\/_internal.h\/cc\u3001tf\/_session\/_helper.h\/cc\u3001c_api.h\/cc\u3002\u81f3\u6b64session.run()\u4ece\u524d\u7aef\u6620\u5c04\u5230\u540e\u7aef\u7684\u6d41\u7a0b\u8bb2\u5b8c\u4e86\uff0c\u90a3\u63a5\u4e0b\u6765\u56de\u7b54\u524d\u7aeftensor\u5982\u4f55\u6620\u5c04\u81f3\u540e\u7aefTensor\uff0c\u8bf7\u770b\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>\/\/ tf_session_helper.cc    line351\nvoid TF_SessionRun_wrapper_helper(TF_Session* session, const char* handle,\n                                  const TF_Buffer* run_options,\n                                  const std::vector&lt;TF_Output&gt;&amp; inputs,\n                                  const std::vector&lt;PyObject*&gt;&amp; input_ndarrays,\n                                  const std::vector&lt;TF_Output&gt;&amp; outputs,\n                                  const std::vector&lt;TF_Operation*&gt;&amp; targets,\n                                  TF_Buffer* run_metadata,\n                                  TF_Status* out_status,\n                                  std::vector&lt;PyObject*&gt;* py_outputs) {\n  DCHECK_EQ(inputs.size(), input_ndarrays.size());\n  DCHECK(py_outputs != nullptr);\n  DCHECK(py_outputs-&gt;empty());\n  Status s;\n\n  \/\/ Convert input ndarray PyObjects to TF_Tensors. We maintain a continuous\n  \/\/ array of TF_Tensor*s as well as scoped containers to make sure they're\n  \/\/ cleaned up properly.\n  \/\/ \u7701\u7565\u4e86\u5f88\u591a\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u6b64\u5904\u628a\u524d\u7aef\u7c7bndarray\u7684\u5bf9\u8c61\u8f6c\u5316\u6210\u4e86TF_Tensors\u3002\n}\n\n\/\/ c_api.cc  line2274\nvoid TF_SessionRun(TF_Session* session, const TF_Buffer* run_options,\n                   const TF_Output* inputs, TF_Tensor* const* input_values,\n                   int ninputs, const TF_Output* outputs,\n                   TF_Tensor** output_values, int noutputs,\n                   const TF_Operation* const* target_opers, int ntargets,\n                   TF_Buffer* run_metadata, TF_Status* status) {\n  \/\/ TODO(josh11b,mrry): Change Session to be able to use a Graph*\n  \/\/ directly, instead of requiring us to serialize to a GraphDef and\n  \/\/ call Session::Extend().\n  if (session-&gt;extend_before_run &amp;&amp;\n      !ExtendSessionGraphHelper(session, status)) {\n    return;\n  }\n\n  TF_Run_Setup(noutputs, output_values, status);\n\n  \/\/ Convert from TF_Output and TF_Tensor to a string and Tensor.\n  \/\/ \u770b\u8fd9\u91cc\uff0c\u6b64\u5916TensorFlow\u628aTF_Tensor\u8f6c\u5316\u6210c++ Tensor\n  std::vector&lt;std::pair&lt;string, Tensor&gt;&gt; input_pairs(ninputs);\n  if (!TF_Run_Inputs(input_values, &amp;input_pairs, status)) return;\n  for (int i = 0; i &lt; ninputs; ++i) {\n    input_pairs[i].first = OutputName(inputs[i]);\n  }\n\n  \/\/ Convert from TF_Output to string names.\n  std::vector&lt;string&gt; output_names(noutputs);\n  for (int i = 0; i &lt; noutputs; ++i) {\n    output_names[i] = OutputName(outputs[i]);\n  }\n}\n<\/code><\/pre>\n<h3>2.2 C++ Tensor\u7c7b<\/h3>\n<p>\u67e5\u770b\u53c2\u8003\u6587\u732e5\uff0c\u6211\u4eec\u627e\u5230\u4e86C++ Tensor\u7c7b\u7684\u5b9a\u4e49\uff0c\u5176\u91cd\u8981\u7247\u6bb5\uff08seg1\uff09\u5982\u4e0b\uff1a<\/p>\n<pre><code>class Tensor{\n  public:\n    \/\/ Tensor\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u76f8\u5173,\u57282.3\u8282\u8be6\u7ec6\u4ecb\u7ecd\n    bool FromProto(const TensorProto&amp; other) TF_MUST_USE_RESULT;\n    void AsProtoField(TensorProto* proto) const;\n    void AsProtoTensorContent(TensorProto* proto) const;\n    \n    \/\/ Tensor\u5b9e\u9645\u4e3a\u5e95\u5c42\u6570\u636e\u7684\u4e00\u79cd\u89c6\u56fe,\u53ef\u7528vec\u6216matrix\u8fdb\u884c\u5c55\u793a\n    template &lt;typename T&gt;\n    typename TTypes&lt;T&gt;::Vec vec() {\n      return tensor&lt;T, 1&gt;();\n    }\n\n    template &lt;typename T&gt;\n    typename TTypes&lt;T&gt;::Matrix matrix() {\n      return tensor&lt;T, 2&gt;();\n    }\n\n    template &lt;typename T, size_t NDIMS&gt;\n    typename TTypes&lt;T, NDIMS&gt;::Tensor tensor();\n  \n  private:\n    TensorShape shape_;    \/\/ \u7ef4\u62a4Tensor\u7684\u5f62\u72b6\u548c\u6570\u636e\u7c7b\u578b\n    TensorBuffer buf_;     \/\/ \u5e95\u5c42\u6570\u636e\u7684\u6307\u9488\n}\n<\/code><\/pre>\n<p>\u6211\u4eec\u5148\u6765\u5206\u6790\u4e0b\u4e24\u4e2a\u79c1\u6709\u6210\u5458\u3002\u9996\u5148\u770b\u4e00\u4e0bTensorBuffer\u7c7b\uff0c\u5b83\u662f\u4e00\u4e2a\u7ee7\u627f\u5f15\u7528\u8ba1\u6570\u7c7b\u7684\u865a\u62df\u7c7b\uff0c\u4e0d\u5305\u542b\u4efb\u4f55\u5b9e\u73b0\u3002\u901a\u8fc7\u67e5\u770b\u53c2\u8003\u6587\u732e6\uff0c\u6211\u4eec\u5f97\u77e5BufferBase\u7ee7\u627fTensorBuffer\u7c7b\uff0c\u4e14\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5185\u5b58\u5206\u914d\u5668\u6307\u9488\u3002\u800cBuffer\u7c7b\u7ee7\u627fBufferBase\u7c7b\uff0c\u4e14\u7ef4\u62a4\u4e86\u6307\u5411\u5b9e\u9645\u6570\u636e\u7684\u6307\u9488data_\u548c\u5143\u7d20\u6570\u91cfelem_\u3002\u4e0a\u8ff0\u7c7b\u7684\u7ee7\u627f\u5173\u7cfb\u5982\u4e0b\u56fe\u6240\u793a\uff08\u4e3a\u4fbf\u4e8e\u7406\u89e3\u56fe\u4e2d\u7ed9\u51fa\u6210\u5458\u5b9a\u4e49\uff0c\u800c\u975e\u6807\u51c6\u7684UML\u56fe\uff09\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838114\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5206\u6790TensorShape\u7c7b\u3002\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u7c7b\u7ee7\u627f\u4f53\u7cfb\uff0c\u5176\u6838\u5fc3\u903b\u8f91\u5b9a\u4e49\u5728\u7236\u7c7bTensorShapeRep\u4e2d\uff0c\u76f8\u5173\u7684\u7c7b\u7ee7\u627f\u4f53\u7cfb\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838115\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e3a\u6df1\u5165\u7406\u89e3TensorShape\u7684\u4f5c\u7528\uff0c\u4ee5\u4e0b\u7ed3\u5408TensorShapeRep\u7684\u90e8\u5206\u4ee3\u7801\uff08seg2\uff09\u8fdb\u884c\u5206\u6790\uff1a<\/p>\n<pre><code>class TensorShapeRep{\n  private:\n    \/\/ \u5982\u4e0bbuf\u5171\u8ba116\u5b57\u8282\u8868\u793aTensorShape\uff0c\u5176\u4e2d\u524d12\u5b57\u8282\u7528\u6765\u5b58\u50a8\u5f62\u72b6\uff08Rep16\u3001Rep32\u3001Rep64\uff09\n    \/\/ \u7b2c13\u5b57\u8282\u4f5c\u7528\u4e0d\u6e05\u695a\uff0c\u7b2c14\u300115\u300116\u5b57\u8282\u5206\u522b\u8868\u793a\u6570\u636e\u7c7b\u578b\u7f16\u53f7\u3001\u5f20\u91cf\u7684\u7ef4\u5ea6\u6570\u76ee\u3001\u5f20\u91cf\u7ef4\u5ea6\u7684\u8868\u793a\u7c7b\u578b\n    union {\n      uint8 buf[16];\n\n      Rep64* unused_aligner;   \/\/ Force data to be aligned enough for a pointer.\n    } u_;\n  \n  public:\n    \/\/ \u7406\u8bba\u4e0a\u53ef\u5b9a\u4e49\u4efb\u610f\u7ef4\u7684\u5f20\u91cf\uff0c\u4f461\u7ef4\u30012\u7ef4\u30013\u7ef4\u5f20\u91cf\u6700\u5e38\u89c1\u3002\u6240\u4ee5\u7ed9\u51fa\u5982\u4e0b\u4e09\u79cd\u7ef4\u5ea6\u8868\u793a\u65b9\u6cd5\uff0812\u5b57\u8282\uff09\n    struct Rep16 {\n      uint16 dims_[6];    \/\/ \u6700\u591a\u53ef\u8868\u793a6\u7ef4\u7684\u5f20\u91cf\uff0c\u6bcf\u4e00\u7ef4\u7684\u957f\u5ea6\u4e0d\u8d85\u8fc72^16-1\n    };\n    struct Rep32 {\n      uint32 dims_[3];    \/\/ \u6700\u591a\u53ef\u8868\u793a3\u7ef4\u7684\u5f20\u91cf\uff0c\u6bcf\u4e00\u7ef4\u7684\u957f\u5ea6\u4e0d\u8d85\u8fc72^32-1\n    };\n    struct Rep64 {\n      gtl::InlinedVector&lt;int64, 4&gt;* dims_;  \/\/ \u652f\u6301\u4efb\u610f\u7ef4\u5ea6\u7684\u5f20\u91cf\n    };\n}\n<\/code><\/pre>\n<p>\u672c\u5c0f\u8282\u6700\u540e\uff0c\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0bTensor\u7c7b\u5b9a\u4e49\u4e2d\u7684vector()\u548cmatrix()\u3002\u67e5\u770b\u4e24\u4e2a\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u53d1\u73b0\u8c03\u7528\u4e86\u5171\u540c\u7684\u65b9\u6cd5tensor()\uff0c\u800ctensor()\u7684\u8fd4\u56de\u7c7b\u578b\u4e3aTTypes&lt;T\uff0cNDIMS&gt;::Tensor\uff0c\u800cTTypes\u6b63\u662f\u8854\u63a5TF Tensor\u4e0eEigen\u5e93\u7684\u5173\u952e\u3002\u8bf7\u770b\u5982\u4e0b\u4ee3\u7801\uff08seg3\uff09\uff1a<\/p>\n<pre><code>\/\/ tensorflow1.15.5\/tensorflow\/core\/framework\/tensor.h\nclass Tensor{\n  public:\n    \/\/ Returns the shape of the tensor.\n    const TensorShape&amp; shape() const { return shape_; }\n  \n    template &lt;typename T&gt;\n    typename TTypes&lt;T&gt;::Vec vec() {\n      return tensor&lt;T, 1&gt;();\n    }\n    \n\n    template &lt;typename T&gt;\n    typename TTypes&lt;T&gt;::Matrix matrix() {\n      return tensor&lt;T, 2&gt;();\n    }\n    \n\n    template &lt;typename T, size_t NDIMS&gt;\n    typename TTypes&lt;T, NDIMS&gt;::Tensor tensor();\n}\n\n\/\/ tensorflow1.15.5\/tensorflow\/core\/framework\/tensor_types.h\ntemplate &lt;typename T, int NDIMS = 1, typename IndexType = Eigen::DenseIndex&gt;\nstruct TTypes {\n  \/\/ Rank-&lt;NDIMS&gt; tensor of scalar type T.\n  typedef Eigen::TensorMap&lt;Eigen::Tensor&lt;T, NDIMS, Eigen::RowMajor, IndexType&gt;,Eigen::Aligned&gt; Tensor;\n  \/\/ \u7701\u7565\u4e86\u8bb8\u591a\u4ee3\u7801\n}\n\n\/\/ tensorflow1.15.5\/tensorflow\/core\/framework\/tensor.h\n\/\/ TF Tensor\u7684shape()\u8fd4\u56deTensorShape\u3002base()\u8fd4\u56de\u6307\u5411\u5b9e\u9645\u6570\u636e\u7684\u6307\u9488\u3002\ntemplate &lt;typename T, size_t NDIMS&gt;\ntypename TTypes&lt;T, NDIMS&gt;::Tensor Tensor::tensor() {\n  CheckTypeAndIsAligned(DataTypeToEnum&lt;T&gt;::v());\n  return typename TTypes&lt;T, NDIMS&gt;::Tensor(base&lt;T&gt;(),\n                                           shape().AsEigenDSizes&lt;NDIMS&gt;());\n}\n<\/code><\/pre>\n<p>\u7531\u4e0a\u8ff0\u4ee3\u7801\u53ef\u89c1\uff0c\u8c03\u7528tensor()\u662f\u628aTF Tensor\u8f6c\u5316\u6210\u4e86TTypes&lt;T,NDIMS&gt;::Tensor\uff0c\u800c\u540e\u8005\u672c\u8d28\u4e0a\u662fEigen::TensorMap\u3002\u81f3\u6b64\uff0c\u6211\u4eec\u641e\u6e05\u695a\u4e86TF Tensor\u4e0eEigen\u5e93\u7684\u5173\u7cfb\uff0c\u53ef\u4ee5\u8ba4\u4e3aTF C++ Tensor\u662f\u5bf9Eigen::TensorMap\u7684\u4e00\u79cd\u5c01\u88c5\u3002\u56e0\u4e3aEigen::TensorMap\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u6765\u81ea\u4e8eTF Tensor\u4e2d\u4fdd\u5b58\u7684\u4fe1\u606f\uff08base()\u548cshape()\u5bf9\u5e94\u7684\u4fe1\u606f\uff09\u3002<\/p>\n<h3>2.3 C++ Tensor\u5e8f\u5217\u5316<\/h3>\n<p>\u5728TensorFlow\u7684\u5206\u5e03\u5f0f\u8bad\u7ec3\u73af\u5883\u4e2d\u6d89\u53ca\u5927\u91cf\u7684\u8de8\u673a\u901a\u4fe1\uff0c\u901a\u4fe1\u7684\u5185\u5bb9\u5c31\u662f\u5e8f\u5217\u5316\u540e\u7684\u5f20\u91cf\uff08\u901a\u8fc7send\/recv op\u5bf9\u534f\u540c\u5de5\u4f5c\uff09\u3002\u672c\u5c0f\u8282\u6211\u4eec\u5c06\u4e00\u8d77\u5b66\u4e60Tensor\u7684\u5e8f\u5217\u5316\u673a\u5236\uff0c\u4ee5\u53caTensor\u4e0e\u5e8f\u5217\u5316\u5bf9\u8c61\u7684\u4e92\u7f16\u7a0b\u3002TensorFlow\u4e2dTensor\u5bf9\u5e94\u7684\u5e8f\u5217\u5316\u5bf9\u8c61\u53ebTensorProto\uff0c\u5b83\u662f\u7531\u5bf9\u5e94\u7684proto\u6587\u4ef6\u751f\u6210\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff08seg4\uff09\uff1a<\/p>\n<pre><code>\/\/ tensorflow1.15.5\/tensorflow\/core\/framework\/tensor.proto\nsyntax = &quot;proto3&quot;;\n\nmessage TensorProto {\n  DataType dtype = 1;\n\n  TensorShapeProto tensor_shape = 2;\n\n  int32 version_number = 3;\n\n  bytes tensor_content = 4;\n\n  repeated int32 half_val = 13 [packed = true];\n\n  \/\/ DT_FLOAT.\n  repeated float float_val = 5 [packed = true];\n\n  \/\/ DT_DOUBLE.\n  repeated double double_val = 6 [packed = true];\n\n  \/\/ DT_INT32, DT_INT16, DT_INT8, DT_UINT8.\n  repeated int32 int_val = 7 [packed = true];\n\n  \/\/ DT_STRING\n  repeated bytes string_val = 8;\n\n  \/\/ DT_COMPLEX64. scomplex_val(2*i) and scomplex_val(2*i+1) are real\n  \/\/ and imaginary parts of i-th single precision complex.\n  repeated float scomplex_val = 9 [packed = true];\n\n  \/\/ DT_INT64\n  repeated int64 int64_val = 10 [packed = true];\n\n  \/\/ DT_BOOL\n  repeated bool bool_val = 11 [packed = true];\n\n  \/\/ DT_COMPLEX128. dcomplex_val(2*i) and dcomplex_val(2*i+1) are real\n  \/\/ and imaginary parts of i-th double precision complex.\n  repeated double dcomplex_val = 12 [packed = true];\n\n  \/\/ DT_RESOURCE\n  repeated ResourceHandleProto resource_handle_val = 14;\n\n  \/\/ DT_VARIANT\n  repeated VariantTensorDataProto variant_val = 15;\n\n  \/\/ DT_UINT32\n  repeated uint32 uint32_val = 16 [packed = true];\n\n  \/\/ DT_UINT64\n  repeated uint64 uint64_val = 17 [packed = true];\n};\n<\/code><\/pre>\n<p>\u5927\u5bb6\u53ef\u7528protoc\u7f16\u8bd1\u5668\u6765\u7f16\u8bd1tensor.proto\u6587\u4ef6\uff0c\u7ed3\u679c\u751f\u6210tensor.pb.h\u548ctensor.pb.cc\u4e24\u4e2a\u6587\u4ef6\uff0c\u4ed6\u4eec\u5206\u522b\u58f0\u660e\u4e86TensorProto\u7c7b\u5b9a\u4e49\u3001TensorProto\u6210\u5458\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u6211\u4eec\u53ef\u4ee5\u7c97\u7565\u5730\u5c06TensorProto\u770b\u4f5cTensor\u7684\u4e8c\u8fdb\u5236\u5bf9\u8c61\uff0c\u57fa\u4e8e\u6b64\u5b83\u4eec\u76f8\u4e92\u4e4b\u95f4\u7684\u8f6c\u6362\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff08seg5\uff09\uff1a<\/p>\n<pre><code>\/\/ Tensor\u7684\u5e8f\u5217\u5316\u8fc7\u7a0b\nauto tensor_proto = new TensorProto();\n\/\/ Fills in `proto` with `*this` tensor's content.\n\/\/ `AsProtoField()` fills in the repeated field for `proto.dtype()`, \n\/\/ while `AsProtoTensorContent()` encodes the content in `proto.tensor_content()` in a compact form.\ntensor-&gt;AsProtoField(tensor_proto);\ntensor-&gt;AsProtoTensorContent(tensor_proto);\n  \n\/\/ Tensor\u7684\u53cd\u5e8f\u5217\u5316\u8fc7\u7a0b\nTensor tensor;\ntensor.FromProto(tensor_proto);\n<\/code><\/pre>\n<h2>\u4e09\u3001\u8de8\u6846\u67b6\u7f16\u7a0b-\u901a\u7528\u5185\u5b58\u5f20\u91cfDLPack<\/h2>\n<h3>3.1 \u4ec0\u4e48\u662fDLPack<\/h3>\n<p>DLPack\u662f\u4e00\u79cd\u5f00\u653e\u7684\u5185\u5b58\u5f20\u91cf\u7ed3\u6784\uff0c\u7528\u4e8e\u5728AI\u6846\u67b6\u4e4b\u95f4\u5171\u4eab\u5f20\u91cf\u3002\u591a\u6846\u67b6\u6574\u5408\u89e3\u51b3AI\u95ee\u9898\uff0c\u80fd\u5145\u5206\u53d1\u6325\u5404\u6846\u67b6\u4f18\u52bf\uff08\u4e00\u4e9b\u8fd0\u7b97\u5728\u67d0\u6846\u67b6\u4e2d\u652f\u6301\u66f4\u597d\uff09\uff0c\u5e76\u6700\u7ec8\u53d6\u5f97\u6574\u4f53\u6700\u4f73\u6027\u80fd\u3002\u4f46\u8fd9\u91cc\u6709\u4e00\u4e2a\u5173\u952e\u95ee\u9898\u8981\u89e3\u51b3\uff1a\u5982\u4f55\u5c06\u5185\u5b58\u4e2d\u7684\u5f20\u91cf\u4ece\u4e00\u4e2a\u6846\u67b6\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u6846\u67b6\uff0c\u800c\u4e0d\u53d1\u751f\u4efb\u4f55\u6570\u636e\u62f7\u8d1d\uff1f\u5e78\u8fd0\u7684\u662f\uff0c\u9648\u5929\u5947\u56e2\u961f\u7ed9\u51fa\u4e86DLPack\u8fd9\u4e2a\u7b54\u6848\u3002<\/p>\n<p>DLPack\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u5c3d\u53ef\u80fd\u7684\u8f7b\u91cf\u5316\uff0c\u5b83\u4e0d\u8003\u8651\u5185\u5b58\u5206\u914d\u3001\u8bbe\u5907API\uff0c\u4ec5\u4ec5\u5173\u6ce8\u5f20\u91cf\u6570\u636e\u7ed3\u6784\u3002\u5b83\u53ef\u4ee5\u8fd0\u884c\u5728\u591a\u4e2a\u786c\u4ef6\u5e73\u53f0\u4e0a\uff0c\u76ee\u524d\u652f\u6301\u7684\u6846\u67b6\u6709\uff1aNumPy\u3001CuPy\u3001PyTorch\u3001Tensorflow\u3001MXNet\u3001TVM\u3001mpi4py\u3002DLPack\u7684\u5f00\u53d1\u8005\u4e0d\u6253\u7b97\u5b9e\u73b0Tensor\u548cOps\uff0c\u800c\u662f\u5c06\u5176\u7528\u4f5c\u8de8\u6846\u67b6\u91cd\u7528\u5f20\u91cf\u548c\u64cd\u4f5c\u7684\u516c\u5171\u6865\u6881\u3002\u6df1\u5165\u7406\u89e3DLPack\uff0c\u8981\u638c\u63e1\u4e24\u5927\u6a21\u5757\uff1aC API\u4e0ePython API\u3002DLPack C API\u4f53\u7cfb\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838116\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0a\u56fe\u4e2d\u6df1\u84dd\u8272\u7684\u7ed3\u6784\u4f53\u5747\u5b9a\u4e49\u5728[13]\u4e2d\u3002DLTensor\u4ee3\u8868\u666e\u901aC Tensor\u5bf9\u8c61\uff0c\u4f46\u4e0d\u8d1f\u8d23\u5185\u5b58\u7ba1\u7406\u3002DLManagedTensor\u4e5f\u662f\u4e00\u4e2aC Tensor\u5bf9\u8c61\uff0c\u8d1f\u8d23DLTensor\u7684\u5185\u5b58\u7ba1\u7406\uff0c\u5b83\u88ab\u8bbe\u8ba1\u7528\u6765\u5e2e\u52a9\u5176\u4ed6\u6846\u67b6\u501f\u7528\u6b64DLTensor\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u76ee\u5149\u8f6c\u5411DLPack\u7684Python API\u3002<\/p>\n<p>DLPack Python\u63a5\u53e3\u662fPython array\u7684\u6807\u51c6API\u3002\u7528DLPack Python\u63a5\u53e3\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u7684\u63a5\u53e3\u6709\u4e24\u4e2a\uff1a<\/p>\n<p>\u2022 from\/_dlpack(x)\uff1a\u8f93\u5165\u4e00\u4e2a\u5305\u542b\/_\/_dlpack__\u65b9\u6cd5\u7684\u6570\u7ec4\u5bf9\u8c61\uff0c\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6784\u5efa\u4e00\u4e2a\u5305\u542bx\u6570\u636e\u57df\u7684\u65b0\u6570\u7ec4\u5bf9\u8c61\u3002<\/p>\n<p>\u2022 \/_\/_dlpack\/_\/_(self,stream=None) and \/_\/_dlpack\/_device__()\uff1a\u5728from_dlpack(x)\u5185\u90e8\u8c03\u7528x\u7684\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6x\u7684\u6570\u636e\u57df\u4ee5\u53ca\u5b9a\u4f4dx\u6570\u7ec4\u5bf9\u8c61\u5728\u54ea\u4e2a\u8bbe\u5907\u4e0a\u3002<\/p>\n<p>\u4ece\u8bed\u4e49\u5c42\u9762\u7406\u89e3y=from\/_dlpack(x)\u7684\u8bdd\uff0c\u751f\u6210x\u7684\u5e93\u53eb\u751f\u4ea7\u8005\uff0c\u5305\u542bfrom\/_dlpack()\u7684\u5e93\u53eb\u505a\u6d88\u8d39\u8005\u3002\u5176\u4e2d\u751f\u4ea7\u8005\u63d0\u4f9b\u4e86\u8bbf\u95eex\u6570\u636e\u57df\u7684\u9014\u5f84\uff0c\u901a\u5e38\u6765\u8bf4\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\u5173\u4e8e\u76f8\u5e94\u7684\u6570\u636e\u662f\u96f6\u62f7\u8d1d\u7684\uff0c\u4e5f\u5373y\u53ef\u89c6\u4e3ax\u7684\u89c6\u56fe\u3002\u5982\u679c\u6df1\u5165from\/_dlpack(x)\u5185\u90e8\uff0c\u5219x.\/_\/_dlpack__\u65b9\u6cd5\u751f\u6210\u5305\u542bDLManagedTensor\u7684PyCapsule\u5bf9\u8c61\uff08\u6216\u79f0capsule\uff09\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u53ea\u80fd\u88ab\u6d88\u8d39\u4e00\u6b21\u3002\u751f\u4ea7\u8005\u5fc5\u987b\u5c06PyCapsule\u5bf9\u8c61\u540d\u79f0\u8bbe\u4e3a&#8221;dltensor&#8221;\uff0c\u4ee5\u65b9\u4fbf\u6309\u540d\u79f0\u68c0\u7d22\uff1b\u540c\u65f6\u4e5f\u8981\u8bbe\u7f6eDLManagedTensor\u7684deleter\u65b9\u6cd5\u7ed9PyCapsule\/_Destructor\uff0c\u8fd9\u4e2a\u8bbe\u7f6e\u662f\u5f53\u540d\u4e3a&#8221;dltensor&#8221;\u7684capsule\u5bf9\u8c61\u4e0d\u518d\u9700\u8981\u65f6\u4f7f\u7528\u3002\u6d88\u8d39\u8005\u628aDLManagedTensor\u7684\u6240\u6709\u6743\u4ececapsule\u5bf9\u8c61\u8f6c\u79fb\u81f3\u81ea\u5df1\uff0c\u8fd9\u662f\u901a\u8fc7\u628acapsule\u5bf9\u8c61\u6539\u540d\u4e3a&#8221;used\/_dltensor&#8221;\u4ee5\u786e\u4fddPyCapsule_Destructor\u4e0d\u4f1a\u88ab\u8c03\u7528\u6765\u5b9e\u73b0\u7684\u3002\u4f46\u5f53capsule\u5bf9\u8c61\u628aDLManagedTensor\u6240\u6709\u6743\u8f6c\u79fb\u81f3\u6d88\u8d39\u8005\u5bf9\u8c61\u65f6\uff0c\u6d88\u8d39\u8005\u5bf9\u8c61\u7684destructor\u65b9\u6cd5\u4ecd\u7136\u53ef\u4ee5\u8c03\u7528DLManagedTensor\u7684deleter\u65b9\u6cd5\u3002<\/p>\n<h3>3.2 TensorFlow\u4e2d\u7684dlpack<\/h3>\n<p>\u7b14\u8005\u53d1\u73b0TensorFlow\u5bf9DLPack\u7684\u652f\u6301\u662f\u4ecev2.2.0\u5f00\u59cb\u7684\uff0c\u66f4\u65e9\u7684\u7248\u672c\u6ca1\u6709dlpack\u76f8\u5e94\u7684\u5e93\u3002TensorFlow\u7684dlpack\u63a5\u53e3\u4e0e3.1\u9075\u5b88\u76f8\u540c\u7684\u8bed\u4e49\u63cf\u8ff0\uff0c\u76f8\u5e94\u7684API\u6d4b\u8bd5\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre><code>import tensorflow as tf\n\nx = tf.constant(5)\nx                     \/\/ &lt;tf.Tensor: shape=(), dtype=int32, numpy=5&gt;\nr =tf.experimental.dlpack.to_dlpack(x)\nprint(r,type(r))      \/\/ &lt;capsule object &quot;dltensor&quot; at 0x7f55a0431c30&gt; &lt;class 'PyCapsule'&gt;\n\nx_other = tf.experimental.dlpack.from_dlpack(r)\nx_other               \/\/ &lt;tf.Tensor: shape=(), dtype=int32, numpy=5&gt;\n<\/code><\/pre>\n<h3>3.3 TVM\u4e0eDLPack\u7684\u5173\u7cfb<\/h3>\n<p>\u5982\u679c\u4f60\u60f3\u5f00\u53d1\u4e00\u6b3e\u8de8AI\u6846\u67b6\u7684\u6df1\u5ea6\u5b66\u4e60\u7f16\u8bd1\u5668\uff0cDLPack\u5c31\u662f\u4e00\u79cd\u53ef\u884c\u7684\u65b9\u6848\uff08TVM\u5c31\u662f\u8fd9\u6761\u6280\u672f\u8def\u7ebf\uff09\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u5728TVM\u4e2d\u58f0\u660e\u5e76\u7f16\u8bd1\u4e00\u4e2a\u77e9\u9635\u4e58\u6cd5\u7b97\u5b50\uff0c\u7136\u540e\u57fa\u4e8eDLPack\u8868\u793a\u6784\u5efa\u4e00\u4e2a\u5305\u88c5\u5668\uff0c\u8be5\u5305\u88c5\u5668\u80fd\u8ba9\u6b64\u77e9\u9635\u4e58\u6cd5\u7b97\u5b50\u652f\u6301PyTorch Tensor\u3002\u5bf9MxNet\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u7684\u64cd\u4f5c\u3002DLPack\u63d0\u4f9b\u5728AI\u6846\u67b6\u548cTVM\u4e4b\u95f4\u5171\u4eab\u7684\u4e2d\u95f4\u5305\u88c5\u5668\u7684\u539f\u7406\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042838117\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0a\u8ff0\u539f\u7406\u53ef\u4ee5\u53c2\u8003\u5982\u4e0b\u4ee3\u7801\u4e3e\u4f8b\uff1a<\/p>\n<pre><code>\/\/ \u524d\u63d0\u8bf4\u660e:\u5728PyTorch\u4e2d\u8ba1\u7b97\u77e9\u9635\u4e58\u6cd5\nimport torch\nx = torch.rand(56,56)\ny = torch.rand(56,56)\nz = x.mm(y)\n\n\/\/ \u7b2c\u4e00\u6b65\uff0c\u5b9a\u4e49\u5e76\u6784\u5efa\u4e00\u4e2aTVM\u77e9\u9635\u4e58\u6cd5\u7b97\u5b50\nn = tvm.convert(56)\nX = tvm.placeholder((n,n), name='X')\nY = tvm.placeholder((n,n), name='Y')\n\nk = tvm.reduce_axis((0, n), name='k')\nZ = tvm.compute((n,n), lambda i,j : tvm.sum(X[i,k]*Y[k,j], axis=k))\ns = tvm.create_schedule(Z.op)\nfmm = tvm.build(s, [X, Y, Z], target_host='llvm', name='fmm')\n    \n\/\/ \u7b2c\u4e8c\u6b65\uff0c\u5bf9TVM\u51fd\u6570\u8fdb\u884c\u5305\u88c5\u4ee5\u652f\u6301PyTorch Tensor,\u5e76\u9a8c\u8bc1\u7ed3\u679c\nfrom tvm.contrib.dlpack import to_pytorch_func\n# fmm is the previously built TVM function (Python function)\n# fmm is the wrapped TVM function (Python function)\nfmm_pytorch = to_pytorch_func(fmm)\nz2 = torch.empty(56,56)\nfmm_pytorch(x, y, z2)\nnp.testing.assert_allclose(z.numpy(), z2.numpy())\n    \n\/\/ \u7b2c\u4e09\u6b65\uff0c\u53c2\u7167\u7b2c\u4e8c\u6b65\u5bf9MxNet\u8fdb\u884c\u7c7b\u4f3c\u5305\u88c5\u5904\u7406\nimport mxnet\nfrom tvm.contrib.mxnet import to_mxnet_func\nctx = mxnet.cpu(0)\nx = mxnet.nd.uniform(shape=(56,56), ctx=ctx)\ny = mxnet.nd.uniform(shape=(56,56), ctx=ctx)\nz = mxnet.nd.empty(shape=(56,56), ctx=ctx)\nf = tvm.build(s, [X, Y, Z], target_host='llvm', name='f')\nf_mxnet = to_mxnet_func(f)\nf_mxnet(x, y, z)\nnp.testing.assert_allclose(z.asnumpy(), x.asnumpy().dot(y.asnumpy()))\n    \n\/\/ \u7b2c\u56db\u6b65\uff0cto_pytorch_func()\u7684\u8be6\u7ec6\u5b9a\u4e49\n\/\/ TVM\u63d0\u4f9b\u4e86dlpack tensor\u548cTVM NDArray\u4e92\u8f6c\u7684\u51fd\u6570.TVM\u51fd\u6570\u5728\u6700\u5e95\u5c42\u8c03\u7528\u7684\u662fTVM NDArray.\n\/\/ \u6b64\u5305\u88c5\u5668\u7684\u5927\u81f4\u6d41\u7a0b\u662f: AI Tensor -&gt; dlpack tensor -&gt; TVM NDArray -&gt; call TVM function\ndef convert_func(tvm_func, tensor_type, to_dlpack_func):\n    assert callable(tvm_func)\n\n    def _wrapper(*args):\n        args = tuple(ndarray.from_dlpack(to_dlpack_func(arg))\/\n            if isinstance(arg, tensor_type) else arg for arg in args)\n        return tvm_func(*args)\n\n    return _wrapper\n\ndef to_pytorch_func(tvm_func):\n    import torch\n    import torch.utils.dlpack\n    return convert_func(tvm_func, torch.Tensor, torch.utils.dlpack.to_dlpack)\n<\/code><\/pre>\n<h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u5185\u5bb9\u8f83\u591a\u4e14\u70e7\u8111\uff0c\u5efa\u8bae\u8bfb\u8005\u53cd\u590d\u9605\u8bfb\u51e0\u904d\uff0c\u5b9a\u80fd\u6709\u6240\u6536\u83b7\u3002\u6211\u4eec\u5728\u6b64\u5bf9\u901a\u7bc7\u5185\u5bb9\u4f5c\u4e2a\u603b\u7ed3\uff0c\u672c\u6587\u4e3b\u8981\u8bb2\u4e86\u4e09\u4e2a\u4e3b\u9898\uff1a<\/p>\n<p>\u2022 \u7b2c\u4e00\u90e8\u5206\u8bb2\u89e3\u5c0f\u767d\u773c\u4e2d\u7684Tensor\uff0c\u91cd\u70b9\u5206\u6790\u4e86Tensor\u7684\u5c5e\u6027\u548cOP\u7684\u5173\u7cfb\u3002<\/p>\n<p>\u2022 \u7b2c\u4e8c\u90e8\u5206\u8bb2\u89e3\u7cfb\u7edf\u5f00\u53d1\u8005\u773c\u4e2d\u7684Tensor\uff0c\u91cd\u70b9\u8bb2\u89e3Tensor\u524d\u540e\u7aef\u6620\u5c04\uff0c\u4ee5\u53caTensor\u7684C++\u5b9a\u4e49\u53ca\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u2022 \u7b2c\u4e09\u90e8\u5206\u8bb2\u89e3\u901a\u7528\u5185\u5b58\u5f20\u91cfDLPack\uff0c\u91cd\u70b9\u8bb2\u89e3\u4e86DLPack\u7684\u5b9a\u4e49\u53ca\u5728TensorFlow\u4e2d\u7684\u4f7f\u7528\uff0c\u4ee5\u53caDLPack\u5728TVM\u4e2d\u626e\u6f14\u7684\u89d2\u8272\u3002<\/p>\n<h2>\u53c2\u8003\u6587\u732e<\/h2>\n<p>1.TensorFlow Introduction\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=UrnLPL7EsVfVILqBoV2pPw%3D%3D.fHFdhs6sEgE9pnNZKAzSs9moAXufIxE%2FQVUmIYaaVLLV8CS2o7ZQTYhmgNt1GWEmu4DtX363SqlPbS6IxpoZUmEeN2WT%2Bg9J5dAmFtRTtE%2BuiBC4BgKQpRWs7aaF0LWk\" rel=\"nofollow\">https:\/\/github.com\/tensorflow\/docs\/blob\/master\/site\/en\/r1\/guide\/low\/_level\/_intro.md<\/a><\/p>\n<p>2.TensorFlow Tensors\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=p8M3nT2FHgcUDhmEKiE5Nw%3D%3D.%2BSDxaJcGc0bwMnope0JteQIuTqrviRO0N9yKIO5WNSUV8DEHGiXnPuwBOl6qUc70RbfEnp9BwZ0AMy8HFNVWS%2BO73mrYwJRBrXdAI%2FcFPDA%3D\" rel=\"nofollow\">https:\/\/github.com\/tensorflow\/docs\/blob\/master\/site\/en\/r1\/guide\/tensors.md<\/a><\/p>\n<p>3.tf.constant\u6e90\u7801\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=qm6ZZST9LbJ%2FsRac1g24Rg%3D%3D.UwqDxxSfMpFvtS9XRw1YswAKr0QR%2FNf2ev1Gar1ACn%2Bn%2FodKCOSbibvkfwamQZG6Envc1FD2y76t4b4ozsLi0CozS5Bl%2Fu1Ge2F7EI6zAngRSdt4eb%2Fqx1h2K7kgIX0Etjz%2B4fTxiZW9dC41sFI48A%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/tensorflow\/tensorflow\/blob\/v1.15.5\/tensorflow\/python\/framework\/constant_op.py#L165<\/a><\/p>\n<p>4.tensorflow\u6e90\u7801\u89e3\u6790\u4e4bframework-tensor\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=1dhp%2B5usMkh2Uo4T4%2FSmSQ%3D%3D.yw2Kh3X1z%2FafYwy4lglIZNjfri7wmSFkbgipzjT5uXpyAfhpV1gu9wEqtJfVGbDrLiiYQoXC9nwdWTSIFpT1qg%3D%3D\" rel=\"nofollow\">https:\/\/www.cnblogs.com\/jicanghai\/p\/9537282.html<\/a><\/p>\n<p>5.TensorFlow c++ Tensor source code\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=J9XM1%2FhXy1bw8h%2B9NW2AEw%3D%3D.LoLgN%2FxYVP2aefCnFRt6fYC25EXZs2pZIAcDMsNPZFl%2BhZJuROdNl9e6KfG5Ik7CwVBjEME6W8cOLdo033%2BF8%2Fd%2Fxz%2BLIFT6rizrTyu9lo522BaGY%2FzcsSJDgTCVx0WS\" rel=\"nofollow\">https:\/\/github.com\/tensorflow\/tensorflow\/blob\/v1.15.5\/tensorflow\/core\/framework\/tensor.h<\/a><\/p>\n<p>6.TensorFlow c++ Tensor source code\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=sVvHUevOU8WXNZVvYqY2Og%3D%3D.6tBHGnaN6Lar7hZCZRGyyGIpq0XeCpMd6TNDK4RYsBwKnZtfgJgoCWCQC1XPq8I9WaFzFOzK2a4mszYp%2BDcTEkdRORdotJfXY2dvvmuq81rK0KBJ07%2FgeMbBM7hilMsz\" rel=\"nofollow\">https:\/\/github.com\/tensorflow\/tensorflow\/blob\/v1.15.5\/tensorflow\/core\/framework\/tensor.cc<\/a><\/p>\n<p>7.\u300aTensorFlow: A System for Large-Scale Machine Learning\u300b\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=npDgXca5TWhxwKxUqdjfTw%3D%3D.ya6RuoKpXgQJIKznXfvRBqVNX6eRiVdq8A27KoZ8XTA0Ab2a43VXIZmT%2BXSMUS8zM0mEG8QJBX%2FBea9xxYF7mTdFGuU2Cvnj%2BQGNzi9OljA%3D\" rel=\"nofollow\">https:\/\/www.usenix.org\/system\/files\/conference\/osdi16\/osdi16-abadi.pdf<\/a><\/p>\n<p>8.tensorflow-internals.pdf\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=LgQLgxD3nbkA6bzUUAElKQ%3D%3D.PRt3GHC2p5ZCyCtvxuyhKU3amkXqUbrrAFfB8OdRVLfQzI5kL2PC10%2BvlPSWhZs2D0jgJ1ksGDPgIPArQm1QOQ%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/horance-liu\/tensorflow-internals<\/a><\/p>\n<p>9.DLPack doc\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=QorCoTV%2BTWzH3MkF8ygjUQ%3D%3D.LvPc5S%2F8Z3m%2Fl%2BqntweiGUJAGjpoOFrWoYvTaYwXdh85yUzp1XjYorighqd7E7qC\" rel=\"nofollow\">https:\/\/dmlc.github.io\/dlpack\/latest\/<\/a><\/p>\n<p>10.DLPack github\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=l8dXc1MBQtuzmrm7QMrVVA%3D%3D.sMiScimQjA41SK4c8AYSyp6iAN3wAi5nIqYXJODBuVk%3D\" rel=\"nofollow\">https:\/\/github.com\/dmlc\/dlpack<\/a><\/p>\n<p>11.DLPack CAPI\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=xax1YCsBuoXj31VAA05Gmg%3D%3D.A5zf7o7kh%2FGjsCGOH50PUIOZwDgu2wlb6fdQHwY6WUjLUsYYDA4yiSnHhYZtfqNP\" rel=\"nofollow\">https:\/\/dmlc.github.io\/dlpack\/latest\/c_api.html<\/a><\/p>\n<p>12.Python Specification for DLPack\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=r%2BOVZ8ey4yBwnccZ0ZUwCg%3D%3D.ZtRsQignAwYIkTTfh1RMScn89qWhseaeyo3WBPS6aFm7PB%2Bu3zsJ4o8wcbnKnxvQGBzHKquOfyaJuvPXK2v5FA%3D%3D\" rel=\"nofollow\">https:\/\/dmlc.github.io\/dlpack\/latest\/python_spec.html<\/a><\/p>\n<p>13.dlpack.h\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=D5BPXIYnxQi%2BFeZWnCn5yQ%3D%3D.CcxCO0QK%2FachM1QdlJjDJEEGTtn4fV%2Faz6uXHSOJVVNmzJ28vWXUHJ1nLiB3ug8khoo9LMYffBSIby0PpIxTg%2B9qhX5EeoXeoz%2FP%2FqCyW24%3D\" rel=\"nofollow\">https:\/\/github.com\/dmlc\/dlpack\/blob\/main\/include\/dlpack\/dlpack.h<\/a><\/p>\n<p>14.Building a Cross-Framework Deep Learning Compiler via DLPack\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=VAisUJsdSwhusLKeolGw8g%3D%3D.LGPcHvm6dNo7uuNnlX%2BZe%2FFayo2hjcYUZ1bOctalwDS4tFHjW7WyLi9ZElVrfaIA\" rel=\"nofollow\">https:\/\/tvm.apache.org\/2018\/08\/10\/DLPack-Bridge<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042838109\">https:\/\/segmentfault.com\/a\/1190000042838109<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TensorFlow\u8ba1\u7b97\u56fe\u662f\u7531op\u548ctensor\u7ec4\u6210\uff0c\u90a3\u4e48tensor\u4e00\u822c\u90fd\u7528\u6765\u4ee3\u8868\u4ec0\u4e48\u5462\uff1f\u663e\u7136\uff0c\u50cf\u6a21\u578b\u7684\u8f93\u5165 [&hellip;]<\/p>\n","protected":false},"author":1,"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-10006872","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\/10006872","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006872"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006872\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}