{"id":57807,"date":"2025-08-15T20:09:57","date_gmt":"2025-08-15T12:09:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/57807.html"},"modified":"2025-08-15T20:09:57","modified_gmt":"2025-08-15T12:09:57","slug":"jax%e5%bf%ab%e9%80%9f%e4%b8%8a%e6%89%8b%ef%bc%9a%e4%bb%8enumpy%e5%88%b0gpu%e5%8a%a0%e9%80%9f%e7%9a%84python%e9%ab%98%e6%80%a7%e8%83%bd%e8%ae%a1%e7%ae%97%e5%ba%93%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/57807.html","title":{"rendered":"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b"},"content":{"rendered":"<p>NumPy\u4f5c\u4e3aPython\u6570\u503c\u8ba1\u7b97\u9886\u57df\u7684\u57fa\u7840\u6846\u67b6&#xff0c;\u51ed\u501f\u5176\u5f3a\u5927\u7684N\u7ef4\u6570\u7ec4\u7ed3\u6784\u548c\u4e30\u5bcc\u7684\u51fd\u6570\u751f\u6001\u7cfb\u7edf&#xff0c;\u6210\u4e3a\u79d1\u5b66\u5bb6\u3001\u5de5\u7a0b\u5e08\u548c\u6570\u636e\u5206\u6790\u5e08\u7684\u6838\u5fc3\u5de5\u5177\u3002\u7136\u800c&#xff0c;\u968f\u7740\u8ba1\u7b97\u9700\u6c42\u7684\u5feb\u901f\u589e\u957f&#xff0c;\u7279\u522b\u662f\u5728\u673a\u5668\u5b66\u4e60\u548c\u5927\u89c4\u6a21\u79d1\u5b66\u6a21\u62df\u9886\u57df&#xff0c;NumPy\u57fa\u4e8eCPU\u7684\u6267\u884c\u6a21\u5f0f\u4ee5\u53ca\u7f3a\u4e4f\u5185\u7f6e\u81ea\u52a8\u5fae\u5206\u529f\u80fd\u7684\u9650\u5236\u6108\u53d1\u660e\u663e\u3002<\/p>\n<p>JAX\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u800c\u8bbe\u8ba1\u7684\u3002\u4f5c\u4e3aGoogle Research\u5f00\u53d1\u7684\u6570\u503c\u8ba1\u7b97\u5e93&#xff0c;JAX\u81f4\u529b\u4e8e\u5c06NumPy\u5f15\u5165\u73b0\u4ee3\u786c\u4ef6\u52a0\u901f\u5668\u548c\u57fa\u4e8e\u68af\u5ea6\u4f18\u5316\u7684\u8ba1\u7b97\u8303\u5f0f\u4e2d\u3002\u9700\u8981\u8bf4\u660e\u7684\u662f&#xff0c;JAX\u76ee\u524d\u4ecd\u662f\u4e00\u4e2a\u7814\u7a76\u9879\u76ee&#xff0c;\u800c\u975eGoogle\u7684\u5b98\u65b9\u4ea7\u54c1&#xff0c;\u56e0\u6b64\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u4e00\u4e9b\u4e0d\u7a33\u5b9a\u56e0\u7d20\u548c\u6f5c\u5728\u95ee\u9898\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250815120953-689f23914c2a0.png\" alt=\"\" \/><\/p>\n<h3>NumPy\u7684\u6838\u5fc3\u7279\u6027\u4e0e\u5c40\u9650\u6027<\/h3>\n<p>\u5bf9\u4e8e\u4ece\u4e8b\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5f00\u53d1\u8005\u800c\u8a00&#xff0c;NumPy\u51e0\u4e4e\u662f\u5fc5\u5907\u5de5\u5177\u3002\u5176\u6838\u5fc3ndarray\u5bf9\u8c61\u4e3a\u9ad8\u6548\u5b58\u50a8\u548c\u64cd\u4f5c\u5bc6\u96c6\u6570\u503c\u6570\u7ec4\u63d0\u4f9b\u4e86\u57fa\u7840&#xff0c;\u914d\u5408\u5927\u91cf\u7ecf\u8fc7\u4f18\u5316\u7684\u6570\u5b66\u51fd\u6570\u5e93&#xff08;\u901a\u5e38\u901a\u8fc7C\u3001C&#043;&#043;\u6216Fortran\u4ee3\u7801\u5b9e\u73b0&#xff09;&#xff0c;\u5728\u5904\u7406\u6570\u7ec4\u8fd0\u7b97\u65f6\u8fdc\u8d85\u7eafPython\u5faa\u73af\u7684\u6027\u80fd\u3002<\/p>\n<p>NumPy\u6784\u6210\u4e86\u6574\u4e2a\u79d1\u5b66Python\u751f\u6001\u7cfb\u7edf\u7684\u57fa\u77f3&#xff0c;\u5305\u62ecSciPy\u3001Pandas\u3001Scikit-learn\u3001Matplotlib\u7b49\u91cd\u8981\u5e93\u90fd\u5efa\u7acb\u5728\u5176\u4e4b\u4e0a\u3002\u7136\u800c&#xff0c;\u5176\u8bbe\u8ba1\u4e3b\u8981\u9762\u5411CPU\u6267\u884c\u73af\u5883&#xff0c;\u4e14\u672c\u8eab\u4e0d\u5177\u5907\u81ea\u52a8\u68af\u5ea6\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<p>\u68af\u5ea6\u8ba1\u7b97\u7684\u91cd\u8981\u6027\u4e0d\u5bb9\u5ffd\u89c6\u3002\u68af\u5ea6\u8861\u91cf\u51fd\u6570\u8f93\u51fa\u76f8\u5bf9\u4e8e\u8f93\u5165\u7684\u53d8\u5316\u7387&#xff0c;\u6307\u793a\u51fd\u6570\u503c\u589e\u51cf\u6700\u5feb\u7684\u65b9\u5411\u3002\u8fd9\u4e00\u4fe1\u606f\u5bf9\u4f18\u5316\u7b97\u6cd5\u81f3\u5173\u91cd\u8981&#xff0c;\u7279\u522b\u662f\u5728\u673a\u5668\u5b66\u4e60\u9886\u57df&#xff0c;\u68af\u5ea6\u6307\u5bfc\u6a21\u578b\u53c2\u6570\u7684\u8c03\u6574\u8fc7\u7a0b&#xff0c;\u4ee5\u5728\u8bad\u7ec3\u671f\u95f4\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u3002\u9ad8\u6548\u7684\u68af\u5ea6\u8ba1\u7b97\u80fd\u529b\u4f7f\u6a21\u578b\u80fd\u591f\u4ece\u6570\u636e\u4e2d\u5b66\u4e60\u590d\u6742\u7684\u6a21\u5f0f\u3002<\/p>\n<h3>JAX\u6280\u672f\u67b6\u6784\u4e0e\u6838\u5fc3\u529f\u80fd<\/h3>\n<p>JAX\u662fGoogle\u5f00\u53d1\u7684\u9ad8\u6027\u80fdPython\u6570\u503c\u8ba1\u7b97\u5e93&#xff0c;\u5c06\u7c7b\u4f3cNumPy\u7684API\u4e0e\u81ea\u52a8\u5fae\u5206&#xff08;autodiff&#xff09;\u548c\u52a0\u901f\u786c\u4ef6\u6267\u884c&#xff08;GPU\/TPU&#xff09;\u76f8\u7ed3\u5408\u3002<\/p>\n<p>JAX\u7684\u4e3b\u8981\u6280\u672f\u7279\u6027\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762&#xff1a;<\/p>\n<p>jax.numpy<\/p>\n<p>\u6a21\u5757\u63d0\u4f9b\u4e86NumPy\u7684\u76f4\u63a5\u66ff\u4ee3\u65b9\u6848&#xff0c;\u4fdd\u6301\u76f8\u540c\u7684API\u63a5\u53e3\u4f46\u652f\u6301GPU\/TPU\u52a0\u901f&#xff1b;<\/p>\n<p>jax.grad<\/p>\n<p>\u6a21\u5757\u5b9e\u73b0\u51fd\u6570\u7684\u81ea\u52a8\u5fae\u5206\u529f\u80fd&#xff0c;\u7c7b\u4f3c\u4e8eTensorFlow\u6216PyTorch\u7684\u68af\u5ea6\u8ba1\u7b97\u673a\u5236&#xff1b;<\/p>\n<p>jax.jit<\/p>\n<p>\u6a21\u5757\u901a\u8fc7\u52a0\u901f\u7ebf\u6027\u4ee3\u6570&#xff08;XLA&#xff09;\u5e93\u63d0\u4f9b\u5373\u65f6\u7f16\u8bd1\u529f\u80fd&#xff0c;\u5b9e\u73b0\u6781\u9ad8\u7684\u6267\u884c\u6548\u7387&#xff1b;<\/p>\n<p>jax.vmap<\/p>\n<p>\u548c<\/p>\n<p>jax.pmap<\/p>\n<p>\u6a21\u5757\u652f\u6301\u81ea\u52a8\u5411\u91cf\u5316\u548c\u5e76\u884c\u5316\u5904\u7406&#xff1b;\u6b64\u5916JAX\u5728GPU\/TPU\u4e0a\u65e0\u7f1d\u8fd0\u884c&#xff0c;\u65e0\u9700\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u3002<\/p>\n<h3>JAX\u7684\u5e94\u7528\u573a\u666f\u5206\u6790<\/h3>\n<p>JAX\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\u8868\u73b0\u51fa\u663e\u8457\u4f18\u52bf&#xff1a;\u5f53\u9700\u8981\u901a\u8fc7GPU\u6216TPU\u663e\u8457\u52a0\u901f\u7c7b\u4f3cNumPy\u7684\u8ba1\u7b97\u4efb\u52a1\u65f6&#xff1b;\u5f53\u9700\u8981\u81ea\u52a8\u8ba1\u7b97\u6570\u503cPython\u51fd\u6570\u7684\u68af\u5ea6\u4ee5\u8fdb\u884c\u4f18\u5316\u65f6&#xff08;\u5982\u673a\u5668\u5b66\u4e60\u3001\u7269\u7406\u6a21\u62df\u7b49&#xff09;&#xff1b;\u5f53\u9700\u8981\u901a\u8fc7JIT\u7f16\u8bd1\u5173\u952ePython\u4ee3\u7801\u6bb5\u4ee5\u83b7\u5f97\u8fdb\u4e00\u6b65\u52a0\u901f\u65f6&#xff1b;\u5f53\u9700\u8981\u8f7b\u677e\u5b9e\u73b0\u51fd\u6570\u5411\u91cf\u5316\u4ee5\u5904\u7406\u6570\u636e\u6279\u6b21\u6216\u5728\u591a\u4e2a\u52a0\u901f\u5668\u8bbe\u5907\u4e0a\u5e76\u884c\u5316\u8ba1\u7b97\u65f6\u3002<\/p>\n<p>\u5728\u9009\u62e9JAX\u66ff\u4ee3NumPy\u4e4b\u524d&#xff0c;\u9700\u8981\u4e86\u89e3\u4e24\u4e2a\u5e93\u4e4b\u95f4\u7684\u5173\u952e\u5dee\u5f02\u3002\u867d\u7136<\/p>\n<p>jax.numpy<\/p>\n<p>\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u6a21\u4effNumPy API&#xff0c;\u4f46\u5728\u4ee5\u4e0b\u65b9\u9762\u5b58\u5728\u663e\u8457\u533a\u522b&#xff1a;<\/p>\n<p>\u5728\u6267\u884c\u540e\u7aef\u548c\u7f16\u8bd1\u65b9\u9762&#xff0c;NumPy\u5728CPU\u4e0a\u91c7\u7528\u5373\u65f6\u6267\u884c\u6a21\u5f0f&#xff0c;\u901a\u5e38\u4f7f\u7528\u9884\u7f16\u8bd1\u7684C\u3001C&#043;&#043;\u6216Fortran\u6269\u5c55\u4ee5\u53ca\u4f18\u5316\u7684\u7ebf\u6027\u4ee3\u6570\u5e93&#xff08;\u5982OpenBLAS&#xff09;\u3002\u76f8\u6bd4\u4e4b\u4e0bJAX\u4f7f\u7528XLA\u7f16\u8bd1\u5668\u5c06\u4ee3\u7801\u8f6c\u6362\u4e3a\u9488\u5bf9CPU\u3001GPU\u6216TPU\u4f18\u5316\u7684\u673a\u5668\u4ee3\u7801&#xff0c;\u652f\u6301\u901a\u8fc7<\/p>\n<p>jax.jit<\/p>\n<p>\u8fdb\u884c\u5373\u65f6\u7f16\u8bd1&#xff0c;\u5e76\u901a\u5e38\u91c7\u7528\u5f02\u6b65\u5206\u6d3e\u65b9\u5f0f\u3002<\/p>\n<p>\u5728\u6267\u884c\u6a21\u578b\u65b9\u9762&#xff0c;NumPy\u64cd\u4f5c\u901a\u5e38\u540c\u6b65\u6267\u884c&#xff0c;Python\u89e3\u91ca\u5668\u7b49\u5f85\u64cd\u4f5c\u5b8c\u6210\u540e\u7ee7\u7eed\u6267\u884c\u3002\u800cJAX\u64cd\u4f5c\u5f02\u6b65\u5206\u6d3e\u5230\u52a0\u901f\u5668&#xff0c;Python\u4ee3\u7801\u53ef\u80fd\u5728\u8ba1\u7b97\u8fdb\u884c\u65f6\u7ee7\u7eed\u8fd0\u884c\u3002\u56e0\u6b64&#xff0c;\u901a\u5e38\u9700\u8981\u4f7f\u7528<\/p>\n<p>result.block_until_ready()<\/p>\n<p>\u6765\u786e\u4fdd\u51c6\u786e\u8ba1\u65f6\u6216\u5728\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u7ed3\u679c\u4e4b\u524d\u786e\u4fdd\u7ed3\u679c\u53ef\u7528\u3002<\/p>\n<p>\u5728\u6570\u636e\u53ef\u53d8\u6027\u65b9\u9762&#xff0c;NumPy\u6570\u7ec4&#xff08;ndarray&#xff09;\u662f\u53ef\u53d8\u7684&#xff0c;\u5141\u8bb8\u5c31\u5730\u4fee\u6539\u5143\u7d20\u3002JAX\u6570\u7ec4\u5219\u662f\u4e0d\u53ef\u53d8\u7684&#xff0c;\u4e0d\u5141\u8bb8\u5c31\u5730\u66f4\u65b0\u3002\u8fd9\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u65b9\u6cd5\u786e\u4fddJAX\u7684\u8f6c\u6362\u529f\u80fd\u80fd\u591f\u53ef\u9760\u5de5\u4f5c\u800c\u4e0d\u4ea7\u751f\u526f\u4f5c\u7528&#xff0c;\u66f4\u65b0\u64cd\u4f5c\u9700\u8981\u4f7f\u7528\u7d22\u5f15\u66f4\u65b0\u8bed\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\u3002<\/p>\n<p>\u5728\u968f\u673a\u6570\u751f\u6210\u65b9\u9762&#xff0c;NumPy\u4f7f\u7528\u5168\u5c40\u968f\u673a\u6570\u751f\u6210\u5668\u72b6\u6001&#xff0c;\u8fd9\u5728\u5e76\u884c\u6216\u8f6c\u6362\u4ee3\u7801\u4e2d\u53ef\u80fd\u5f71\u54cd\u53ef\u91cd\u73b0\u6027\u3002JAX\u5219\u9700\u8981\u663e\u5f0f\u5904\u7406\u968f\u673a\u5bc6\u94a5&#xff0c;\u5fc5\u987b\u624b\u52a8\u7ba1\u7406\u548c\u5206\u5272\u5bc6\u94a5\u4ee5\u786e\u4fdd\u968f\u673a\u6027\u7684\u53ef\u91cd\u73b0\u6027\u3002<\/p>\n<p>\u5728API\u8986\u76d6\u8303\u56f4\u65b9\u9762&#xff0c;NumPy\u63d0\u4f9b\u6db5\u76d6\u6570\u503c\u8ba1\u7b97\u5404\u4e2a\u65b9\u9762\u7684\u5168\u9762API&#xff0c;\u800cJAX\u8986\u76d6\u6700\u5e38\u89c1NumPy API\u7684\u5927\u90e8\u5206\u5b50\u96c6\u4e14\u5728\u4e0d\u65ad\u6269\u5c55&#xff0c;\u4f46\u5e76\u975e100%\u7684\u76f4\u63a5\u66ff\u4ee3\u54c1\u3002\u4e00\u4e9b\u4e0d\u5e38\u89c1\u7684\u51fd\u6570\u3001\u7279\u5b9a\u6570\u636e\u7c7b\u578b&#xff08;\u5982\u5bf9\u8c61\u6570\u7ec4&#xff09;\u6216\u7279\u5b9a\u884c\u4e3a\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\u6216\u7f3a\u5931\u3002<\/p>\n<h3>\u73af\u5883\u914d\u7f6e\u4e0e\u5b89\u88c5<\/h3>\n<p>\u672c\u6587\u7684\u4ee3\u7801\u793a\u4f8b\u57fa\u4e8eWSL2 Ubuntu for Windows\u5f00\u53d1\u73af\u5883\u3002\u5bf9\u4e8e\u62e5\u6709Nvidia GPU\u7684\u7cfb\u7edf&#xff0c;\u53ef\u4ee5\u5145\u5206\u5229\u7528GPU\u52a0\u901f\u529f\u80fd\u3002\u5373\u4f7f\u6ca1\u6709GPU&#xff0c;JAX\u4ecd\u80fd\u5728CPU\u4e0a\u63d0\u4f9b\u76f8\u6bd4NumPy\u66f4\u597d\u7684\u6027\u80fd\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684GPU\u54c1\u724c\u6216\u914d\u7f6e&#xff0c;\u5efa\u8bae\u53c2\u8003\u5b98\u65b9\u6587\u6863\u83b7\u53d6\u8be6\u7ec6\u7684\u5b89\u88c5\u8bf4\u660e\u3002<\/p>\n<p>\u9996\u5148\u521b\u5efa\u4e13\u7528\u7684\u5f00\u53d1\u73af\u5883&#xff0c;\u8fd9\u91cc\u4f7f\u7528conda\u8fdb\u884c\u73af\u5883\u7ba1\u7406&#xff1a;<\/p>\n<p> conda create -n jax_test python&#061;3.13 -y <\/p>\n<p>\u6fc0\u6d3b\u73af\u5883\u5e76\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u5bf9\u4e8eNVIDIA GPU&#xff0c;\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u9002\u5f53\u7684NVIDIA\u9a71\u52a8\u7a0b\u5e8f\u548cCUDA\u73af\u5883&#xff08;\u5982CUDA 11\u6216CUDA 12&#xff09;&#xff1a;<\/p>\n<p> conda activate jax_test<br \/>\n pip install jupyter numpy &#034;jax[cuda12]&#034; matplotlib pillow<\/p>\n<p>JAX\u7684\u5b89\u88c5\u8fc7\u7a0b\u53ef\u80fd\u8f83\u4e3a\u8017\u65f6&#xff0c;\u5b8c\u6210\u540e\u53ef\u4ee5\u542f\u52a8Jupyter notebook\u3002\u5982\u679c\u6d4f\u89c8\u5668\u672a\u81ea\u52a8\u6253\u5f00&#xff0c;\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u8f93\u51fa\u4e2d\u627e\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u683c\u5f0f\u7684URL&#xff1a;<\/p>\n<p> http:\/\/127.0.0.1:8888\/tree?token&#061;3b9f7bd07b6966b41b68e2350721b2d0b6f388d248cc69d<\/p>\n<h3>1\u3001\u719f\u6089API\u4e0eJIT\u7f16\u8bd1\u4f18\u5316<\/h3>\n<p>\u7b2c\u4e00\u4e2a\u6848\u4f8b\u5c55\u793a\u4e86JAX\u5982\u4f55\u901a\u8fc7\u5373\u65f6\u7f16\u8bd1\u6280\u672f\u63d0\u5347NumPy\u7684\u6027\u80fd\u3002\u8fd9\u4e2a\u6848\u4f8b\u5b9e\u73b0\u4e86\u5e94\u7528\u4e8e10,000 x 10,000\u6570\u7ec4\u7684SELU&#xff08;Scaled Exponential Linear Unit&#xff09;\u51fd\u6570\u3002SELU\u662f\u81ea\u5f52\u4e00\u5316\u795e\u7ecf\u7f51\u7edc\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u6fc0\u6d3b\u51fd\u6570&#xff0c;\u5176\u6570\u5b66\u5b9a\u4e49\u5982\u4e0b\u56fe\u6240\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250815120954-689f2392ee1eb.jpg\" alt=\"\" \/><\/p>\n<p>\u8be5\u51fd\u6570\u901a\u8fc7<\/p>\n<p>np.where<\/p>\n<p>&#xff08;NumPy&#xff09;\u6216<\/p>\n<p>jnp.where<\/p>\n<p>&#xff08;JAX&#xff09;\u5b9e\u73b0&#xff0c;\u6839\u636e\u8f93\u5165\u503c<\/p>\n<p>x<\/p>\n<p>\u7684\u6b63\u8d1f\u6027\u9009\u62e9\u4e0d\u540c\u7684\u8ba1\u7b97\u516c\u5f0f\u3002<\/p>\n<p>\u4ee3\u7801\u5b9e\u73b0\u5305\u542b\u4e09\u79cd\u7248\u672c&#xff1a;<\/p>\n<p>selu_numpy(x)<\/p>\n<p>\u4e3a\u6807\u51c6NumPy\u5b9e\u73b0&#xff1b;<\/p>\n<p>selu_jax(x)<\/p>\n<p>\u4e3aJAX\u7248\u672c&#xff0c;\u4ee3\u7801\u7ed3\u6784\u76f8\u540c\u4f46\u4f7f\u7528JAX\u6570\u7ec4&#xff1b;<\/p>\n<p>selu_jax_jit(x)<\/p>\n<p>\u5728JAX\u7248\u672c\u57fa\u7840\u4e0a\u6dfb\u52a0<\/p>\n<p>&#064;jax.jit<\/p>\n<p>\u88c5\u9970\u5668\u4ee5\u542f\u7528\u7f16\u8bd1\u4f18\u5316\u3002<\/p>\n<p>\u6d4b\u8bd5\u4f7f\u7528\u4e00\u4e2a10,000 x 10,000\u7684\u968f\u673a\u6570\u6570\u7ec4\u4f5c\u4e3a\u8f93\u5165\u6570\u636e&#xff0c;\u5206\u522b\u6d4b\u91cf\u5404\u5b9e\u73b0\u7684\u6267\u884c\u65f6\u95f4\u3002\u5176\u4e2d&#xff0c;<\/p>\n<p>selu_numpy<\/p>\n<p>\u5728CPU\u4e0a\u76f4\u63a5\u8fd0\u884c&#xff1b;<\/p>\n<p>selu_jax<\/p>\n<p>\u5728\u6ca1\u6709JIT\u4f18\u5316\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c&#xff0c;\u7531\u4e8e\u9700\u8981\u89e3\u91ca\u6267\u884c\u800c\u76f8\u5bf9\u8f83\u6162&#xff1b;<\/p>\n<p>selu_jax_jit<\/p>\n<p>\u9996\u6b21\u8fd0\u884c\u65f6\u5305\u542b\u7f16\u8bd1\u65f6\u95f4&#xff0c;\u4f46\u5728\u540e\u7eed\u8fd0\u884c\u4e2d\u91cd\u7528\u7f16\u8bd1\u540e\u7684\u51fd\u6570&#xff0c;\u6267\u884c\u901f\u5ea6\u663e\u8457\u63d0\u5347\u3002<\/p>\n<p>\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f&#xff0c;\u7531\u4e8eJAX\u91c7\u7528\u5f02\u6b65\u6267\u884c\u6a21\u5f0f&#xff0c;\u9700\u8981\u4f7f\u7528<\/p>\n<p>block_until_ready()<\/p>\n<p>\u7b49\u5f85\u64cd\u4f5c\u5b8c\u6210\u4ee5\u83b7\u5f97\u51c6\u786e\u7684\u6027\u80fd\u6d4b\u91cf\u7ed3\u679c\u3002\u9996\u6b21JIT\u8fd0\u884c\u5305\u542b\u7f16\u8bd1\u5f00\u9500&#xff0c;\u800c\u7b2c\u4e8c\u6b21\u8fd0\u884c\u5219\u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\u7684\u7f16\u8bd1\u7ed3\u679c&#xff0c;\u5b9e\u73b0\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<p> import numpy as np<br \/>\nimport jax<br \/>\nimport jax.numpy as jnp<br \/>\nfrom timeit import default_timer as timer  <\/p>\n<p># \u4e3aSELU\u5b9a\u4e49\u5e38\u6570<br \/>\nalpha &#061; 1.6732632423543772848170429916717<br \/>\nscale &#061; 1.0507009873554804934193349852946  <\/p>\n<p># &#8212; NumPy\u7248\u672c &#8212;<br \/>\ndef selu_numpy(x):<br \/>\n  return scale * np.where(x &gt; 0, x, alpha * np.exp(x) &#8211; alpha)  <\/p>\n<p># &#8212; JAX\u7248\u672c &#8212;<br \/>\ndef selu_jax(x):<br \/>\n  return scale * jnp.where(x &gt; 0, x, alpha * jnp.exp(x) &#8211; alpha)  <\/p>\n<p># &#8212; JIT\u7f16\u8bd1\u7684JAX\u7248\u672c &#8212;<br \/>\n# \u5e94\u7528&#064;jax.jit\u88c5\u9970\u5668<br \/>\n&#064;jax.jit<br \/>\ndef selu_jax_jit(x):<br \/>\n  return scale * jnp.where(x &gt; 0, x, alpha * jnp.exp(x) &#8211; alpha)  <\/p>\n<p># \u751f\u6210\u6d4b\u8bd5\u6570\u636e<br \/>\nx_np &#061; np.random.rand(10000, 10000).astype(np.float32)<br \/>\n# \u4f7f\u7528JAX\u7684\u968f\u673a\u6570\u751f\u6210&#xff08;\u9700\u8981\u663e\u5f0f\u5bc6\u94a5\u7ba1\u7406&#xff09;<br \/>\nkey &#061; jax.random.PRNGKey(0)<br \/>\nx_jax &#061; jax.random.normal(key, (10000, 10000), dtype&#061;jnp.float32)  <\/p>\n<p>print(&#034;\u6267\u884c\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5&#8230;&#034;)  <\/p>\n<p># &#8212; \u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 &#8212;  <\/p>\n<p># NumPy\u57fa\u51c6\u6d4b\u8bd5<br \/>\nstart &#061; timer()<br \/>\nresult_np &#061; selu_numpy(x_np)<br \/>\n# NumPy\u5728CPU\u4e0a\u540c\u6b65\u6267\u884c&#xff0c;\u65e0\u9700\u7b49\u5f85<br \/>\nprint(f&#034;NumPy\u6267\u884c\u65f6\u95f4: {timer()-start:.6f} \u79d2&#034;)  <\/p>\n<p># JAX&#xff08;\u65e0JIT&#xff09;\u57fa\u51c6\u6d4b\u8bd5<br \/>\nstart &#061; timer()<br \/>\nresult_jax &#061; selu_jax(x_jax)<br \/>\nresult_jax.block_until_ready() # \u5173\u952e\u6b65\u9aa4&#xff1a;\u7b49\u5f85JAX\u8ba1\u7b97\u5b8c\u6210<br \/>\nprint(f&#034;JAX (\u65e0JIT)\u6267\u884c\u65f6\u95f4: {timer()-start:.6f} \u79d2&#034;)  <\/p>\n<p># JAX&#xff08;JIT&#xff09;\u9996\u6b21\u8fd0\u884c&#xff08;\u5305\u542b\u7f16\u8bd1\u65f6\u95f4&#xff09;<br \/>\nstart &#061; timer()<br \/>\nresult_jax_jit &#061; selu_jax_jit(x_jax)<br \/>\nresult_jax_jit.block_until_ready()<br \/>\nprint(f&#034;JAX (JIT)\u9996\u6b21\u8fd0\u884c\u65f6\u95f4&#xff08;\u542b\u7f16\u8bd1&#xff09;: {timer()-start:.6f} \u79d2&#034;)  <\/p>\n<p># JAX&#xff08;JIT&#xff09;\u7b2c\u4e8c\u6b21\u8fd0\u884c&#xff08;\u4f7f\u7528\u7f13\u5b58\u7f16\u8bd1\u7ed3\u679c&#xff09;<br \/>\nstart &#061; timer()<br \/>\nresult_jax_jit_2 &#061; selu_jax_jit(x_jax)<br \/>\nresult_jax_jit_2.block_until_ready()<br \/>\nprint(f&#034;JAX (JIT)\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u95f4: {timer()-start:.6f} \u79d2&#034;)  <\/p>\n<p># \u9a8c\u8bc1\u8ba1\u7b97\u7ed3\u679c\u7684\u4e00\u81f4\u6027<br \/>\n print(np.allclose(selu_numpy(np.array(x_jax)), result_jax_jit_2, atol&#061;1e-6))<\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u663e\u793a&#xff1a;<\/p>\n<p> \u6267\u884c\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5&#8230;<br \/>\n NumPy\u6267\u884c\u65f6\u95f4: 0.357104 \u79d2<br \/>\n JAX (\u65e0JIT)\u6267\u884c\u65f6\u95f4: 0.108734 \u79d2<br \/>\n JAX (JIT)\u9996\u6b21\u8fd0\u884c\u65f6\u95f4&#xff08;\u542b\u7f16\u8bd1&#xff09;: 0.026956 \u79d2<br \/>\n JAX (JIT)\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u95f4: 0.002400 \u79d2<br \/>\n True<\/p>\n<p>\u7ed3\u679c\u8868\u660e&#xff0c;JAX\u7684JIT\u7f16\u8bd1\u7248\u672c\u5728\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u76f8\u6bd4NumPy\u5b9e\u73b0\u4e86\u8d85\u8fc7100\u500d\u7684\u6027\u80fd\u63d0\u5347&#xff0c;\u5373\u4f7f\u4e0d\u4f7f\u7528JIT\u4f18\u5316&#xff0c;JAX\u7684\u6027\u80fd\u4e5f\u6bd4NumPy\u63d0\u5347\u7ea63\u500d\u3002<\/p>\n<h3>2\u3001\u81ea\u52a8\u5fae\u5206\u529f\u80fd\u6f14\u793a<\/h3>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u6848\u4f8b\u5c06\u5c55\u793a\u4e86JAX\u5f3a\u5927\u7684\u81ea\u52a8\u5fae\u5206\u80fd\u529b&#xff0c;\u8fd9\u662f\u5176\u533a\u522b\u4e8eNumPy\u7684\u6838\u5fc3\u7279\u6027\u4e4b\u4e00\u3002<\/p>\n<p> import jax<br \/>\nimport jax.numpy as jnp  <\/p>\n<p># \u4f7f\u7528jax.numpy\u5b9a\u4e49\u76ee\u6807\u51fd\u6570<br \/>\ndef cubic_sum(x):<br \/>\n  return jnp.sum(x**3)  <\/p>\n<p># \u901a\u8fc7jax.grad\u83b7\u53d6\u68af\u5ea6\u51fd\u6570<br \/>\ngrad_cubic_sum &#061; jax.grad(cubic_sum)  <\/p>\n<p># \u521b\u5efa\u8f93\u5165\u6570\u636e<br \/>\nx_input &#061; jnp.arange(1.0, 5.0)  <\/p>\n<p># \u8ba1\u7b97\u68af\u5ea6<br \/>\ngradient &#061; grad_cubic_sum(x_input)<br \/>\nprint(f&#034;\\\\n&#8212; \u81ea\u52a8\u5fae\u5206\u793a\u4f8b &#8212;&#034;)<br \/>\nprint(f&#034;\u539f\u59cb\u51fd\u6570\u8f93\u5165: {x_input}&#034;)<br \/>\nprint(f&#034;\u51fd\u6570\u8f93\u51fa f(x): {cubic_sum(x_input)}&#034;)<br \/>\n print(f&#034;\u68af\u5ea6 df\/dx: {gradient}&#034;)<\/p>\n<p>\u6267\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p> &#8212; \u81ea\u52a8\u5fae\u5206\u793a\u4f8b &#8212;<br \/>\n \u539f\u59cb\u51fd\u6570\u8f93\u5165: [1. 2. 3. 4.]<br \/>\n \u51fd\u6570\u8f93\u51fa f(x): 100.0<br \/>\n \u68af\u5ea6 df\/dx: [ 3. 12. 27. 48.]<\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d&#xff0c;\u76ee\u6807\u51fd\u6570\u4e3ax\u00b3&#xff0c;\u5176\u5bfc\u6570\u4e3a3x\u00b2\u3002\u5bf9\u4e8e\u8f93\u5165\u5e8f\u5217[1, 2, 3, 4]&#xff0c;\u76f8\u5e94\u7684\u68af\u5ea6\u8ba1\u7b97\u4e3a&#xff1a;<\/p>\n<ul>\n<li>3 \u00d7 1\u00b2 &#061; 3<\/li>\n<li>3 \u00d7 2\u00b2 &#061; 12<\/li>\n<li>3 \u00d7 3\u00b2 &#061; 27<\/li>\n<li>3 \u00d7 4\u00b2 &#061; 48<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a\u4e86JAX\u81ea\u52a8\u5fae\u5206\u7684\u5f3a\u5927\u529f\u80fd&#xff0c;\u65e0\u9700\u624b\u52a8\u63a8\u5bfc\u548c\u5b9e\u73b0\u68af\u5ea6\u8ba1\u7b97&#xff0c;JAX\u80fd\u591f\u81ea\u52a8\u5904\u7406\u590d\u6742\u51fd\u6570\u7684\u5fae\u5206\u8fd0\u7b97\u3002<\/p>\n<h3>3\u3001\u5411\u91cf\u5316\u77e9\u9635\u4e58\u6cd5\u6027\u80fd\u5206\u6790<\/h3>\n<p>\u6211\u4eec\u8fd8\u901a\u8fc7\u5411\u91cf\u5316\u64cd\u4f5c\u5c55\u793a\u4e86JAX\u7684\u6027\u80fd\u4f18\u52bf&#xff0c;\u5177\u4f53\u5b9e\u73b0\u4e86\u5c06\u4e00\u4e2a10,000\u5143\u7d20\u77e9\u9635\u4e0e128\u4e2a10,000\u5143\u7d20\u5411\u91cf\u6279\u6b21\u76f8\u4e58\u7684\u8ba1\u7b97\u4efb\u52a1\u3002<\/p>\n<p> import numpy as np<br \/>\nimport jax<br \/>\nimport jax.numpy as jnp<br \/>\nfrom timeit import default_timer as timer  <\/p>\n<p># &#8212; \u5355\u4e2a\u6570\u636e\u70b9\u7684\u57fa\u7840\u51fd\u6570 &#8212;<br \/>\ndef mat_vec_product(matrix, vector):<br \/>\n    &#034;&#034;&#034;\u8ba1\u7b97\u77e9\u9635-\u5411\u91cf\u4e58\u79ef&#034;&#034;&#034;<br \/>\n    return jnp.dot(matrix, vector)  <\/p>\n<p># &#8212; \u4f7f\u7528vmap\u521b\u5efa\u6279\u5904\u7406\u7248\u672c &#8212;<br \/>\n# \u76ee\u6807\u662f\u5c06mat_vec_product\u5e94\u7528\u5230\u6279\u6b21\u4e2d\u7684\u6bcf\u4e2a\u5411\u91cf<br \/>\n# \u77e9\u9635\u5bf9\u6279\u6b21\u4e2d\u7684\u6240\u6709\u5411\u91cf\u4fdd\u6301\u4e0d\u53d8<br \/>\n# in_axes&#061;(None, 0)\u7684\u542b\u4e49&#xff1a;<br \/>\n#   None: \u4e0d\u5bf9\u7b2c\u4e00\u4e2a\u53c2\u6570(matrix)\u8fdb\u884c\u6620\u5c04&#xff0c;\u91c7\u7528\u5e7f\u64ad\u65b9\u5f0f<br \/>\n#   0: \u5bf9\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u7b2c0\u8f74(\u5411\u91cf\u6279\u6b21)\u8fdb\u884c\u6620\u5c04<br \/>\nbatched_mat_vec &#061; jax.vmap(mat_vec_product, in_axes&#061;(None, 0))  <\/p>\n<p># &#8212; JIT\u7f16\u8bd1\u7684\u5411\u91cf\u5316\u51fd\u6570 &#8212;<br \/>\n&#064;jax.jit<br \/>\ndef batched_mat_vec_jit(matrix, vectors):<br \/>\n    &#034;&#034;&#034;JIT\u7f16\u8bd1\u7684\u6279\u5904\u7406\u77e9\u9635-\u5411\u91cf\u4e58\u6cd5&#034;&#034;&#034;<br \/>\n    return jax.vmap(mat_vec_product, in_axes&#061;(None, 0))(matrix, vectors)  <\/p>\n<p># &#8212; \u6570\u636e\u914d\u7f6e &#8212;<br \/>\nmatrix_size &#061; 10000<br \/>\nvector_size &#061; 10000<br \/>\nbatch_size &#061; 128<br \/>\ndtype &#061; jnp.float32  <\/p>\n<p># JAX\u968f\u673a\u6570\u751f\u6210\u9700\u8981\u663e\u5f0f\u5bc6\u94a5\u7ba1\u7406<br \/>\nkey &#061; jax.random.PRNGKey(0)<br \/>\nkey, subkey1, subkey2 &#061; jax.random.split(key, 3)  <\/p>\n<p># \u751f\u6210\u6d4b\u8bd5\u6570\u636e<br \/>\nmatrix_jax &#061; jax.random.normal(subkey1, (matrix_size, vector_size), dtype&#061;dtype)<br \/>\nvectors_jax &#061; jax.random.normal(subkey2, (batch_size, vector_size), dtype&#061;dtype)  <\/p>\n<p># \u8f6c\u6362\u4e3aNumPy\u683c\u5f0f\u4ee5\u8fdb\u884c\u5bf9\u6bd4\u6d4b\u8bd5<br \/>\nmatrix_np &#061; np.array(matrix_jax)<br \/>\nvectors_np &#061; np.array(vectors_jax)  <\/p>\n<p>print(f&#034;\\\\n&#8212; vmap\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 (\u77e9\u9635: {matrix_size}x{vector_size}, \u6279\u6b21\u5927\u5c0f: {batch_size}) &#8212;&#034;)<br \/>\nprint(f&#034;\u53ef\u7528JAX\u8bbe\u5907: {jax.devices()}&#034;)  <\/p>\n<p># &#8212; \u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 &#8212;  <\/p>\n<p># NumPy\u65b9\u6cd51&#xff1a;Python\u5faa\u73af\u5b9e\u73b0&#xff08;\u4ec5\u4f5c\u6f14\u793a&#xff0c;\u901a\u5e38\u6027\u80fd\u8f83\u5dee&#xff09;<br \/>\nstart_np_loop &#061; timer()<br \/>\noutput_np_loop &#061; np.array([np.dot(matrix_np, v) for v in vectors_np])<br \/>\nend_np_loop &#061; timer()<br \/>\nprint(f&#034;NumPy (Python\u5faa\u73af)\u6267\u884c\u65f6\u95f4: {end_np_loop &#8211; start_np_loop:.6f} \u79d2&#034;)  <\/p>\n<p># NumPy\u65b9\u6cd52&#xff1a;\u77e9\u9635\u4e58\u6cd5\u4e0e\u8f6c\u7f6e\u64cd\u4f5c&#xff08;\u9ad8\u6548\u5b9e\u73b0\u65b9\u5f0f&#xff09;<br \/>\nstart_np_matmul &#061; timer()<br \/>\n# \u77e9\u9635\u4e58\u6cd5\u8981\u6c42vectors_np\u7684\u5f62\u72b6\u4e3a(vector_size, batch_size)<br \/>\noutput_np_matmul &#061; (matrix_np &#064; vectors_np.T).T<br \/>\nend_np_matmul &#061; timer()<br \/>\nprint(f&#034;NumPy (\u77e9\u9635\u4e58\u6cd5&#064;)\u6267\u884c\u65f6\u95f4: {end_np_matmul &#8211; start_np_matmul:.6f} \u79d2&#034;)  <\/p>\n<p># JAX vmap&#xff08;\u65e0JIT\u4f18\u5316&#xff09;<br \/>\nstart_jax_vmap &#061; timer()<br \/>\noutput_jax_vmap &#061; batched_mat_vec(matrix_jax, vectors_jax)<br \/>\noutput_jax_vmap.block_until_ready()<br \/>\nend_jax_vmap &#061; timer()<br \/>\nprint(f&#034;JAX (vmap, \u65e0JIT)\u6267\u884c\u65f6\u95f4: {end_jax_vmap &#8211; start_jax_vmap:.6f} \u79d2&#034;)  <\/p>\n<p># JAX vmap&#xff08;JIT\u4f18\u5316&#xff09;\u9996\u6b21\u8fd0\u884c&#xff08;\u5305\u542b\u7f16\u8bd1\u5f00\u9500&#xff09;<br \/>\nstart_jax_vmap_jit_compile &#061; timer()<br \/>\noutput_jax_vmap_jit_compile &#061; batched_mat_vec_jit(matrix_jax, vectors_jax)<br \/>\noutput_jax_vmap_jit_compile.block_until_ready()<br \/>\nend_jax_vmap_jit_compile &#061; timer()<br \/>\nprint(f&#034;JAX (vmap&#043;JIT)\u9996\u6b21\u8fd0\u884c\u65f6\u95f4&#xff08;\u542b\u7f16\u8bd1&#xff09;: {end_jax_vmap_jit_compile &#8211; start_jax_vmap_jit_compile:.6f} \u79d2&#034;)  <\/p>\n<p># JAX vmap&#xff08;JIT\u4f18\u5316&#xff09;\u7b2c\u4e8c\u6b21\u8fd0\u884c<br \/>\nstart_jax_vmap_jit &#061; timer()<br \/>\noutput_jax_vmap_jit &#061; batched_mat_vec_jit(matrix_jax, vectors_jax)<br \/>\noutput_jax_vmap_jit.block_until_ready()<br \/>\nend_jax_vmap_jit &#061; timer()<br \/>\n print(f&#034;JAX (vmap&#043;JIT)\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u95f4: {end_jax_vmap_jit &#8211; start_jax_vmap_jit:.6f} \u79d2&#034;)<\/p>\n<p>\u6027\u80fd\u6d4b\u8bd5\u7ed3\u679c&#xff1a;<\/p>\n<p> &#8212; vmap\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 (\u77e9\u9635: 10000&#215;10000, \u6279\u6b21\u5927\u5c0f: 128) &#8212;<br \/>\n \u53ef\u7528JAX\u8bbe\u5907: [CudaDevice(id&#061;0)]<br \/>\n NumPy (Python\u5faa\u73af)\u6267\u884c\u65f6\u95f4: 1.129315 \u79d2<br \/>\n NumPy (\u77e9\u9635\u4e58\u6cd5&#064;)\u6267\u884c\u65f6\u95f4: 0.029319 \u79d2<br \/>\n JAX (vmap, \u65e0JIT)\u6267\u884c\u65f6\u95f4: 0.901569 \u79d2<br \/>\n JAX (vmap&#043;JIT)\u9996\u6b21\u8fd0\u884c\u65f6\u95f4&#xff08;\u542b\u7f16\u8bd1&#xff09;: 0.539354 \u79d2<br \/>\n JAX (vmap&#043;JIT)\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u95f4: 0.001776 \u79d2<\/p>\n<p>\u867d\u7136\u9996\u6b21JIT\u7f16\u8bd1\u9700\u8981\u76f8\u5bf9\u8f83\u957f\u7684\u65f6\u95f4&#xff0c;\u4f46\u540e\u7eed\u8fd0\u884c\u7684\u6027\u80fd\u63d0\u5347\u6781\u4e3a\u663e\u8457&#xff0c;\u4f53\u73b0\u4e86JAX\u7f16\u8bd1\u4f18\u5316\u7684\u5f3a\u5927\u6548\u679c\u3002<\/p>\n<h3>4\u3001\u56fe\u50cf\u5377\u79ef\u5904\u7406\u5b9e\u73b0<\/h3>\n<p>\u6700\u540e\u4e00\u4e2a\u6848\u4f8b\u4ee5\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u9ad8\u65af\u6a21\u7cca\u4e3a\u4f8b&#xff0c;\u5c55\u793a\u4e86JAX\u5728\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e2d\u7684\u6027\u80fd\u8868\u73b0\u3002\u5377\u79ef\u662f\u56fe\u50cf\u5904\u7406\u7684\u57fa\u7840\u64cd\u4f5c&#xff0c;\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6a21\u7cca\u3001\u9510\u5316\u548c\u8fb9\u7f18\u68c0\u6d4b\u7b49\u4efb\u52a1\u3002\u8be5\u64cd\u4f5c\u6d89\u53ca\u5728\u56fe\u50cf\u4e0a\u6ed1\u52a8\u5c0f\u77e9\u9635&#xff08;\u5377\u79ef\u6838&#xff09;&#xff0c;\u5e76\u5728\u6bcf\u4e2a\u4f4d\u7f6e\u8ba1\u7b97\u6838\u4e0b\u50cf\u7d20\u7684\u52a0\u6743\u548c\u3002\u672c\u6848\u4f8b\u901a\u8fc7\u6570\u7ec4\u5207\u7247\u548c\u9010\u5143\u7d20\u64cd\u4f5c\u5b9e\u73b0\u9ad8\u65af\u6a21\u7cca\u7684\u57fa\u672c\u7248\u672c&#xff0c;\u4ee5\u8bc4\u4f30jax.jit\u5bf9\u6b64\u7c7b\u64cd\u4f5c\u5e8f\u5217\u7684\u4f18\u5316\u6548\u679c\u3002<\/p>\n<p>\u8f93\u5165\u56fe\u50cf\u793a\u4f8b&#xff1a; [\u5916\u94fe\u56fe\u7247\u8f6c\u5b58\u4e2d\u2026(img-b59nsNpi-1755002028476)]<\/p>\n<p>\u539f\u59cb\u56fe\u50cf\u7531Yury Taranik\u63d0\u4f9b<\/p>\n<p> import numpy as np<br \/>\nimport jax<br \/>\nimport jax.numpy as jnp<br \/>\nfrom timeit import default_timer as timer<br \/>\nfrom PIL import Image<br \/>\nimport matplotlib.pyplot as plt<br \/>\nimport os  <\/p>\n<p># &#8212; \u914d\u7f6e\u53c2\u6570 &#8212;<br \/>\nimage_path &#061; &#034;\/mnt\/d\/images\/taj_mahal.png&#034;<br \/>\nkernel_size &#061; 9  # \u589e\u5927\u5377\u79ef\u6838\u4ee5\u83b7\u5f97\u66f4\u660e\u663e\u7684\u6a21\u7cca\u6548\u679c<br \/>\nsigma &#061; 2.5<br \/>\ndtype &#061; jnp.float32  <\/p>\n<p># &#8212; \u56fe\u50cf\u6587\u4ef6\u68c0\u67e5 &#8212;<br \/>\nif not os.path.exists(image_path):<br \/>\n    print(f&#034;\u9519\u8bef&#xff1a;\u5728\u8def\u5f84 &#039;{image_path}&#039; \u672a\u627e\u5230\u56fe\u50cf\u6587\u4ef6&#034;)<br \/>\n    print(&#034;\u8bf7\u66f4\u65b0\u811a\u672c\u4e2d\u7684 &#039;image_path&#039; \u53d8\u91cf&#034;)<br \/>\n    exit()  <\/p>\n<p># &#8212; \u56fe\u50cf\u52a0\u8f7d\u548c\u9884\u5904\u7406 &#8212;<br \/>\nprint(f&#034;\u4ece\u4ee5\u4e0b\u8def\u5f84\u52a0\u8f7d\u56fe\u50cf: {image_path}&#034;)<br \/>\ntry:<br \/>\n    # \u6253\u5f00\u56fe\u50cf&#xff0c;\u8f6c\u6362\u4e3a\u7070\u5ea6\u6a21\u5f0f&#xff0c;\u7136\u540e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<br \/>\n    with Image.open(image_path) as img:<br \/>\n        image_np_uint8 &#061; np.array(img.convert(&#039;L&#039;))  <\/p>\n<p>    # \u5f52\u4e00\u5316\u4e3a0.0\u52301.0\u8303\u56f4\u7684float32\u7c7b\u578b<br \/>\n    image_np &#061; image_np_uint8.astype(np.float32) \/ 255.0<br \/>\n    image_jax &#061; jnp.array(image_np)  <\/p>\n<p>    image_size_h, image_size_w &#061; image_np.shape<br \/>\n    print(f&#034;\u56fe\u50cf\u52a0\u8f7d\u6210\u529f ({image_size_h}x{image_size_w})&#034;)  <\/p>\n<p>except Exception as e:<br \/>\n    print(f&#034;\u9519\u8bef&#xff1a;\u65e0\u6cd5\u52a0\u8f7d\u6216\u5904\u7406\u56fe\u50cf &#039;{image_path}&#039;\u3002\u9519\u8bef\u4fe1\u606f: {e}&#034;)<br \/>\n    exit()  <\/p>\n<p># &#8212; \u9ad8\u65af\u5377\u79ef\u6838\u751f\u6210 &#8212;<br \/>\ndef gaussian_kernel(size, sigma&#061;1.0):<br \/>\n    &#034;&#034;&#034;\u751f\u62102D\u9ad8\u65af\u5377\u79ef\u6838&#034;&#034;&#034;<br \/>\n    ax &#061; jnp.arange(-size \/\/ 2 &#043; 1., size \/\/ 2 &#043; 1.)<br \/>\n    xx, yy &#061; jnp.meshgrid(ax, ax)<br \/>\n    kernel &#061; jnp.exp(-(xx**2 &#043; yy**2) \/ (2. * sigma**2))<br \/>\n    return (kernel \/ jnp.sum(kernel)).astype(dtype)  <\/p>\n<p># &#8212; \u624b\u52a8\u5377\u79ef\u5b9e\u73b0 &#8212;<br \/>\ndef convolve_2d_manual(image, kernel):<br \/>\n    &#034;&#034;&#034;\u4f7f\u7528\u57fa\u672c\u6570\u7ec4\u64cd\u4f5c\u5b9e\u73b02D\u5377\u79ef&#034;&#034;&#034;<br \/>\n    im_h, im_w &#061; image.shape<br \/>\n    ker_h, ker_w &#061; kernel.shape<br \/>\n    pad_h, pad_w &#061; ker_h \/\/ 2, ker_w \/\/ 2<br \/>\n    padded_image &#061; jnp.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), mode&#061;&#039;edge&#039;)<br \/>\n    output &#061; jnp.zeros_like(image)<br \/>\n    for i in range(ker_h):<br \/>\n        for j in range(ker_w):<br \/>\n            # \u4f7f\u7528dynamic_slice\u786e\u4fdd\u4e0eJIT\u517c\u5bb9<br \/>\n            image_slice &#061; jax.lax.dynamic_slice(padded_image, (i, j), (im_h, im_w))<br \/>\n            output &#043;&#061; kernel[i, j] * image_slice<br \/>\n    return output  <\/p>\n<p># &#8212; JIT\u7f16\u8bd1\u7248\u672c &#8212;<br \/>\n&#064;jax.jit<br \/>\ndef convolve_2d_manual_jit(image, kernel):<br \/>\n    &#034;&#034;&#034;JIT\u7f16\u8bd1\u76842D\u5377\u79ef\u5b9e\u73b0&#034;&#034;&#034;<br \/>\n    im_h, im_w &#061; image.shape<br \/>\n    ker_h, ker_w &#061; kernel.shape<br \/>\n    pad_h, pad_w &#061; ker_h \/\/ 2, ker_w \/\/ 2<br \/>\n    padded_image &#061; jnp.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), mode&#061;&#039;edge&#039;)<br \/>\n    output &#061; jnp.zeros_like(image)<br \/>\n    for i in range(ker_h):<br \/>\n        for j in range(ker_w):<br \/>\n            # \u5f53\u5207\u7247\u5927\u5c0f\u52a8\u6001\u53d8\u5316\u65f6&#xff0c;\u4f7f\u7528jax.lax.dynamic_slice\u786e\u4fddJIT\u517c\u5bb9\u6027<br \/>\n            image_slice &#061; jax.lax.dynamic_slice(padded_image, (i, j), (im_h, im_w))<br \/>\n            output &#043;&#061; kernel[i, j] * image_slice<br \/>\n    return output  <\/p>\n<p># &#8212; NumPy\u5bf9\u7167\u5b9e\u73b0 &#8212;<br \/>\ndef convolve_2d_manual_np(image, kernel):<br \/>\n    &#034;&#034;&#034;NumPy\u7248\u672c\u76842D\u5377\u79ef\u5b9e\u73b0&#034;&#034;&#034;<br \/>\n    im_h, im_w &#061; image.shape<br \/>\n    ker_h, ker_w &#061; kernel.shape<br \/>\n    pad_h, pad_w &#061; ker_h \/\/ 2, ker_w \/\/ 2<br \/>\n    padded_image &#061; np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), mode&#061;&#039;edge&#039;)<br \/>\n    output &#061; np.zeros_like(image)<br \/>\n    for i in range(ker_h):<br \/>\n        for j in range(ker_w):<br \/>\n            image_slice &#061; padded_image[i:i &#043; im_h, j:j &#043; im_w]<br \/>\n            output &#043;&#061; kernel[i, j] * image_slice<br \/>\n    return output  <\/p>\n<p># &#8212; \u5377\u79ef\u6838\u51c6\u5907 &#8212;<br \/>\nkernel_jax &#061; gaussian_kernel(kernel_size, sigma&#061;sigma)<br \/>\nkernel_np &#061; np.array(kernel_jax)  <\/p>\n<p>print(f&#034;\\\\n&#8212; \u5377\u79ef\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 (\u56fe\u50cf: {image_size_h}x{image_size_w}, \u5377\u79ef\u6838: {kernel_size}x{kernel_size}) &#8212;&#034;)<br \/>\nprint(f&#034;\u53ef\u7528JAX\u8bbe\u5907: {jax.devices()}&#034;)  <\/p>\n<p># &#8212; \u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 &#8212;  <\/p>\n<p># NumPy&#xff08;CPU&#xff09;\u5b9e\u73b0<br \/>\nstart_np &#061; timer()<br \/>\noutput_np &#061; convolve_2d_manual_np(image_np, kernel_np)<br \/>\nend_np &#061; timer()<br \/>\nprint(f&#034;NumPy (\u624b\u52a8\u5377\u79ef)\u6267\u884c\u65f6\u95f4: {end_np &#8211; start_np:.6f} \u79d2&#034;)  <\/p>\n<p># JAX&#xff08;\u65e0JIT&#xff09;\u5b9e\u73b0<br \/>\nstart_jax &#061; timer()<br \/>\noutput_jax &#061; convolve_2d_manual(image_jax, kernel_jax)<br \/>\noutput_jax.block_until_ready()<br \/>\nend_jax &#061; timer()<br \/>\nprint(f&#034;JAX (\u65e0JIT, \u624b\u52a8\u5377\u79ef)\u6267\u884c\u65f6\u95f4: {end_jax &#8211; start_jax:.6f} \u79d2&#034;)  <\/p>\n<p># JAX&#xff08;JIT&#xff09;\u9996\u6b21\u8fd0\u884c&#xff08;\u5305\u542b\u7f16\u8bd1\u65f6\u95f4&#xff09;<br \/>\nstart_jax_compile &#061; timer()<br \/>\noutput_jax_jit_compile &#061; convolve_2d_manual_jit(image_jax, kernel_jax)<br \/>\noutput_jax_jit_compile.block_until_ready()<br \/>\nend_jax_compile &#061; timer()<br \/>\nprint(f&#034;JAX (JIT, \u624b\u52a8\u5377\u79ef)\u9996\u6b21\u8fd0\u884c\u65f6\u95f4&#xff08;\u542b\u7f16\u8bd1&#xff09;: {end_jax_compile &#8211; start_jax_compile:.6f} \u79d2&#034;)  <\/p>\n<p># JAX&#xff08;JIT&#xff09;\u7b2c\u4e8c\u6b21\u8fd0\u884c<br \/>\nstart_jax_jit &#061; timer()<br \/>\noutput_jax_jit &#061; convolve_2d_manual_jit(image_jax, kernel_jax)<br \/>\noutput_jax_jit.block_until_ready()<br \/>\nend_jax_jit &#061; timer()<br \/>\nprint(f&#034;JAX (JIT, \u624b\u52a8\u5377\u79ef)\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u95f4: {end_jax_jit &#8211; start_jax_jit:.6f} \u79d2&#034;)  <\/p>\n<p># \u7ed3\u679c\u9a8c\u8bc1&#xff08;\u8003\u8651float32\u7d2f\u79ef\u8bef\u5dee&#xff09;<br \/>\nmax_diff_conv &#061; np.max(np.abs(output_np &#8211; output_jax_jit))<br \/>\nprint(f&#034;\u5377\u79ef\u7ed3\u679c\u6700\u5927\u7edd\u5bf9\u5dee\u503c: {max_diff_conv:.6f}&#034;)<br \/>\nprint(f&#034;\u5377\u79ef\u7ed3\u679c\u8fd1\u4f3c\u7a0b\u5ea6 (atol&#061;1e-3, rtol&#061;1e-3): {np.allclose(output_np, output_jax_jit, atol&#061;1e-3, rtol&#061;1e-3)}&#034;)  <\/p>\n<p># &#8212; \u7ed3\u679c\u53ef\u89c6\u5316 &#8212;<br \/>\nprint(&#034;\\\\n&#8212; \u8f93\u5165\u8f93\u51fa\u56fe\u50cf\u53ef\u89c6\u5316 &#8212;&#034;)  <\/p>\n<p>fig, axes &#061; plt.subplots(1, 2, figsize&#061;(12, 6))  <\/p>\n<p># \u663e\u793a\u539f\u59cb\u7070\u5ea6\u56fe\u50cf<br \/>\naxes[0].imshow(image_np, cmap&#061;&#039;gray&#039;, vmin&#061;0, vmax&#061;1)<br \/>\naxes[0].set_title(&#039;\u539f\u59cb\u7070\u5ea6\u56fe\u50cf (\u8f93\u5165)&#039;)<br \/>\naxes[0].axis(&#039;off&#039;)  <\/p>\n<p># \u663e\u793a\u6a21\u7cca\u5904\u7406\u540e\u7684\u56fe\u50cf<br \/>\naxes[1].imshow(output_np, cmap&#061;&#039;gray&#039;, vmin&#061;0, vmax&#061;1)<br \/>\naxes[1].set_title(f&#039;\u6a21\u7cca\u5904\u7406\u56fe\u50cf (\u8f93\u51fa, \u5377\u79ef\u6838\u5927\u5c0f&#061;{kernel_size})&#039;)<br \/>\naxes[1].axis(&#039;off&#039;)  <\/p>\n<p>plt.tight_layout()<br \/>\n plt.show()<\/p>\n<p>\u6027\u80fd\u6d4b\u8bd5\u7ed3\u679c&#xff1a;<\/p>\n<p> \u4ece\u4ee5\u4e0b\u8def\u5f84\u52a0\u8f7d\u56fe\u50cf: \/mnt\/d\/images\/taj_mahal.png<br \/>\n\u56fe\u50cf\u52a0\u8f7d\u6210\u529f (473&#215;716)  <\/p>\n<p>&#8212; \u5377\u79ef\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 (\u56fe\u50cf: 473&#215;716, \u5377\u79ef\u6838: 9&#215;9) &#8212;<br \/>\n\u53ef\u7528JAX\u8bbe\u5907: [CudaDevice(id&#061;0)]<br \/>\nNumPy (\u624b\u52a8\u5377\u79ef)\u6267\u884c\u65f6\u95f4: 0.025815 \u79d2<br \/>\nJAX (\u65e0JIT, \u624b\u52a8\u5377\u79ef)\u6267\u884c\u65f6\u95f4: 0.234791 \u79d2<br \/>\nJAX (JIT, \u624b\u52a8\u5377\u79ef)\u9996\u6b21\u8fd0\u884c\u65f6\u95f4&#xff08;\u542b\u7f16\u8bd1&#xff09;: 0.366345 \u79d2<br \/>\nJAX (JIT, \u624b\u52a8\u5377\u79ef)\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u95f4: 0.000238 \u79d2<br \/>\n\u5377\u79ef\u7ed3\u679c\u6700\u5927\u7edd\u5bf9\u5dee\u503c: 0.000000<br \/>\n \u5377\u79ef\u7ed3\u679c\u8fd1\u4f3c\u7a0b\u5ea6 (atol&#061;1e-3, rtol&#061;1e-3): True<\/p>\n<p>\u518d\u6b21\u9a8c\u8bc1\u4e86JAX JIT\u7f16\u8bd1\u5728\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u5b9e\u73b0\u7ea6100\u500d\u7684\u6027\u80fd\u63d0\u5347&#xff0c;\u5c55\u73b0\u4e86\u5176\u5728\u56fe\u50cf\u5904\u7406\u7b49\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u7684\u5de8\u5927\u6f5c\u529b\u3002<\/p>\n<p>\u5904\u7406\u7ed3\u679c\u56fe\u50cf&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250815120955-689f2393926b0.jpg\" alt=\"\" \/><\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>JAX\u4ee3\u8868\u4e86Python\u9ad8\u6027\u80fd\u6570\u503c\u8ba1\u7b97\u9886\u57df\u7684\u91cd\u8981\u8fdb\u5c55\u3002\u901a\u8fc7\u63d0\u4f9b\u4e0eNumPy\u517c\u5bb9\u7684\u63a5\u53e3&#xff0c;\u7ed3\u5408\u5f3a\u5927\u7684\u51fd\u6570\u8f6c\u6362\u80fd\u529b&#xff08;\u5305\u62ecgrad\u3001jit\u3001vmap\u7b49&#xff09;\u4ee5\u53ca\u57fa\u4e8e\u52a0\u901f\u7ebf\u6027\u4ee3\u6570&#xff08;XLA&#xff09;\u7684\u9ad8\u6548\u786c\u4ef6\u6267\u884c&#xff0c;JAX\u4e3a\u73b0\u4ee3\u673a\u5668\u5b66\u4e60\u548c\u5927\u89c4\u6a21\u79d1\u5b66\u8ba1\u7b97\u63d0\u4f9b\u4e86\u5fc5\u8981\u7684\u6280\u672f\u652f\u6491\u3002<\/p>\n<p>JAX\u7684\u663e\u8457\u6027\u80fd\u63d0\u5347\u548c\u5185\u7f6e\u81ea\u52a8\u5fae\u5206\u529f\u80fd\u4f7f\u5176\u6210\u4e3a\u8ba1\u7b97\u79d1\u5b66\u7814\u7a76\u4eba\u5458\u548c\u5de5\u7a0b\u5e08\u7684\u91cd\u8981\u5de5\u5177&#xff0c;\u7279\u522b\u9002\u7528\u4e8e\u5b58\u5728\u6027\u80fd\u74f6\u9888\u6216\u9700\u8981\u68af\u5ea6\u8ba1\u7b97\u7684NumPy\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p>\u4f5c\u4e3aGoogle\u7684\u5b9e\u9a8c\u6027\u9879\u76ee&#xff0c;JAX\u7684\u672a\u6765\u53d1\u5c55\u4ecd\u5b58\u5728\u4e0d\u786e\u5b9a\u6027\u3002\u867d\u7136\u5176\u6280\u672f\u4f18\u52bf\u660e\u663e&#xff0c;\u4f46\u8003\u8651\u5230\u73b0\u6709NumPy\u4ee3\u7801\u7684\u5e9e\u5927\u57fa\u6570&#xff0c;\u5373\u4f7fJAX\u83b7\u5f97\u5e7f\u6cdb\u91c7\u7528&#xff0c;\u5b8c\u5168\u66ff\u4ee3NumPy\u4ecd\u9700\u8981\u76f8\u5f53\u957f\u7684\u65f6\u95f4\u3002\u4e0e\u6b64\u540c\u65f6&#xff0c;NumPy\u5f00\u53d1\u56e2\u961f\u4e5f\u5728\u6301\u7eed\u6539\u8fdb\u5176\u529f\u80fd\u548c\u6027\u80fd&#xff0c;\u8fd9\u79cd\u826f\u6027\u7ade\u4e89\u5c06\u63a8\u52a8\u6574\u4e2a\u6570\u503c\u8ba1\u7b97\u751f\u6001\u7cfb\u7edf\u7684\u53d1\u5c55\u3002<\/p>\n<p>\u4ece\u6280\u672f\u53d1\u5c55\u8d8b\u52bf\u6765\u770b&#xff0c;JAX\u4ee3\u8868\u4e86\u6570\u503c\u8ba1\u7b97\u5e93\u5411\u786c\u4ef6\u52a0\u901f\u3001\u81ea\u52a8\u5fae\u5206\u548c\u7f16\u8bd1\u4f18\u5316\u65b9\u5411\u6f14\u8fdb\u7684\u91cd\u8981\u91cc\u7a0b\u7891&#xff0c;\u4e3aPython\u5728\u9ad8\u6027\u80fd\u8ba1\u7b97\u9886\u57df\u7684\u5e94\u7528\u5f00\u8f9f\u4e86\u65b0\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<p>https:\/\/avoid.overfit.cn\/post\/c4a32ba84033446cace5fed8a7d32e62<\/p>\n<p>\u4f5c\u8005&#xff1a;Thomas Reid<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e32\u6b21\uff0c\u6536\u85cf8\u6b21\u3002JAX\u901a\u8fc7GPU\u52a0\u901f\u548cJIT\u7f16\u8bd1\u663e\u8457\u63d0\u5347\u6570\u503c\u8ba1\u7b97\u6027\u80fd\u3002\u76f8\u6bd4NumPy\u7684\u540c\u6b65CPU\u6267\u884c\uff0cJAX\u5728\u65e0JIT\u4f18\u5316\u65f6\u5df2\u83b7\u5f973\u500d\u52a0\u901f\uff0c\u7ecfJIT\u7f16\u8bd1\u540e\u6027\u80fd\u63d0\u5347\u66f4\u4e3a\u663e\u8457\u3002<\/p>\n","protected":false},"author":2,"featured_media":57804,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6073,1818,81,50,86],"topic":[],"class_list":["post-57807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-jax","tag-numpy","tag-python","tag-50","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/57807.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e32\u6b21\uff0c\u6536\u85cf8\u6b21\u3002JAX\u901a\u8fc7GPU\u52a0\u901f\u548cJIT\u7f16\u8bd1\u663e\u8457\u63d0\u5347\u6570\u503c\u8ba1\u7b97\u6027\u80fd\u3002\u76f8\u6bd4NumPy\u7684\u540c\u6b65CPU\u6267\u884c\uff0cJAX\u5728\u65e0JIT\u4f18\u5316\u65f6\u5df2\u83b7\u5f973\u500d\u52a0\u901f\uff0c\u7ecfJIT\u7f16\u8bd1\u540e\u6027\u80fd\u63d0\u5347\u66f4\u4e3a\u663e\u8457\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/57807.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-15T12:09:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250815120953-689f23914c2a0.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/57807.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/57807.html\",\"name\":\"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-15T12:09:57+00:00\",\"dateModified\":\"2025-08-15T12:09:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/57807.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/57807.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/57807.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/57807.html","og_locale":"zh_CN","og_type":"article","og_title":"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e32\u6b21\uff0c\u6536\u85cf8\u6b21\u3002JAX\u901a\u8fc7GPU\u52a0\u901f\u548cJIT\u7f16\u8bd1\u663e\u8457\u63d0\u5347\u6570\u503c\u8ba1\u7b97\u6027\u80fd\u3002\u76f8\u6bd4NumPy\u7684\u540c\u6b65CPU\u6267\u884c\uff0cJAX\u5728\u65e0JIT\u4f18\u5316\u65f6\u5df2\u83b7\u5f973\u500d\u52a0\u901f\uff0c\u7ecfJIT\u7f16\u8bd1\u540e\u6027\u80fd\u63d0\u5347\u66f4\u4e3a\u663e\u8457\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/57807.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-15T12:09:57+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250815120953-689f23914c2a0.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/57807.html","url":"https:\/\/www.wsisp.com\/helps\/57807.html","name":"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-15T12:09:57+00:00","dateModified":"2025-08-15T12:09:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/57807.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/57807.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/57807.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"JAX\u5feb\u901f\u4e0a\u624b\uff1a\u4eceNumPy\u5230GPU\u52a0\u901f\u7684Python\u9ad8\u6027\u80fd\u8ba1\u7b97\u5e93\u5165\u95e8\u6559\u7a0b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/57807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=57807"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/57807\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/57804"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=57807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=57807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=57807"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=57807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}