{"id":138,"date":"2026-10-06T20:58:52","date_gmt":"2026-10-06T12:58:52","guid":{"rendered":"https:\/\/luqimu.xyz\/?p=138"},"modified":"2026-10-06T20:58:53","modified_gmt":"2026-10-06T12:58:53","slug":"ai-%e6%a8%a1%e5%9e%8b%e6%a0%bc%e5%bc%8f%e4%b8%8e%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/luqimu.xyz\/index.php\/2026\/10\/06\/ai-%e6%a8%a1%e5%9e%8b%e6%a0%bc%e5%bc%8f%e4%b8%8e%e8%a7%a3%e6%9e%90\/","title":{"rendered":"AI \u6a21\u578b\u683c\u5f0f\u4e0e\u89e3\u6790"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00.\u5e38\u89c1ai\u6a21\u578b\u683c\u5f0f<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">1. GGUF<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u521d\u4e3a <strong>llama.cpp<\/strong> \u9879\u76ee\u5f00\u53d1\uff0c\u662f\u4e00\u79cd<strong>\u4e8c\u8fdb\u5236\u683c\u5f0f<\/strong>\uff0c\u76ee\u6807\u662f\u5feb\u901f\u52a0\u8f7d\/\u4fdd\u5b58\u4e14\u6613\u4e8e\u8bfb\u53d6\u3002\u6a21\u578b\u901a\u5e38\u5148\u7528 PyTorch \u7b49\u6846\u67b6\u5f00\u53d1\uff0c\u518d\u8f6c\u6210 GGUF \u914d\u5408 GGML \u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u4eca GGUF \u5df2\u6210\u5f00\u6e90\u793e\u533a\u6700\u6d41\u884c\u7684\u6a21\u578b\u5206\u4eab\u683c\u5f0f\u4e4b\u4e00\uff0c\u88ab\u4e3b\u6d41\u63a8\u7406\u8fd0\u884c\u65f6\u652f\u6301\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/ggml-org\/llama.cpp\">llama.cpp<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/ollama.com\/\">ollama<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/vllm-project\/vllm\">vLLM<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3b\u8981\u9762\u5411<strong>\u8bed\u8a00\u6a21\u578b<\/strong>\uff1b\u5176\u4ed6\u7c7b\u578b\uff08\u5982\u6269\u6563\u6a21\u578b\uff09\u53ef\u901a\u8fc7 <a href=\"https:\/\/github.com\/leejet\/stable-diffusion.cpp\">stable-diffusion.cpp<\/a> \u4f7f\u7528\uff0c\u4f46\u4e0d\u5982 LLM \u666e\u53ca\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6587\u4ef6\u7ed3\u6784<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5143\u6570\u636e\u6bb5<\/strong>\uff1a\u952e\u503c\u5bf9\u7ec4\u7ec7\uff0c\u542b\u67b6\u6784\u3001\u7248\u672c\u3001\u8d85\u53c2\u6570<\/li>\n\n\n\n<li><strong>\u5f20\u91cf\u5143\u6570\u636e\u6bb5<\/strong>\uff1a\u5f20\u91cf\u7684\u5f62\u72b6\u3001\u6570\u636e\u7c7b\u578b\u3001\u540d\u79f0<\/li>\n\n\n\n<li><strong>\u5f20\u91cf\u6570\u636e\u6bb5<\/strong>\uff1a\u5f20\u91cf\u672c\u4f53<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"https:\/\/luqimu.xyz\/wp-content\/uploads\/2026\/10\/gguf-file-structure.jpg\" alt=\"GGUF \u6587\u4ef6\u7ed3\u6784\uff1a\u6587\u4ef6\u5934\uff08magic number \/ version \/ tensor_count \/ metadata_kv_count + \u5143\u6570\u636e\u952e\u503c\u5bf9\uff09\u3001\u5f20\u91cf\u4fe1\u606f\u8868\u3001\u5f20\u91cf\u6570\u636e\u672c\u4f53\"><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6848<\/th><th>\u8bf4\u660e<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td><code>Q4_K_M<\/code><\/td><td>\u591a\u6570\u5f20\u91cf 4bit\uff0c\u90e8\u5206 6bit<\/td><td><strong>\u6700\u5e38\u7528<\/strong>\uff0c\u7cbe\u5ea6\u4e0e\u4f53\u79ef\u5e73\u8861<\/td><\/tr><tr><td><code>IQ4_XS<\/code><\/td><td>\u51e0\u4e4e\u6240\u6709\u5f20\u91cf 4bit + <strong>\u91cd\u8981\u6027\u77e9\u9635<\/strong>\u6821\u51c6<\/td><td>\u4fdd\u6301\u5b58\u50a8\u6548\u7387\u7684\u540c\u65f6\u63d0\u5347\u7cbe\u5ea6<\/td><\/tr><tr><td><code>IQ2_M<\/code><\/td><td>\u7c7b\u4f3c <code>IQ4_XS<\/code> \u4f46\u4e3a 2bit<\/td><td>\u6700\u6fc0\u8fdb\uff0c\u6781\u4f4e\u5185\u5b58\u786c\u4ef6\u53ef\u7528<\/td><\/tr><tr><td><code>Q8_0<\/code><\/td><td>\u5168\u90e8\u5f20\u91cf 8bit<\/td><td>\u6700\u4e0d\u6fc0\u8fdb\uff0c\u7cbe\u5ea6\u63a5\u8fd1\u539f\u6a21\u578b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"https:\/\/luqimu.xyz\/wp-content\/uploads\/2026\/10\/gguf-quantization-files.jpg\" alt=\"HF \u6587\u4ef6\u5217\u8868\u4e2d Meta-Llama-3.1-8B-Instruct \u7684\u5404\u91cf\u5316\u53d8\u4f53\uff1aQ4_0_8_8 \/ Q4_K_L \/ Q4_K_M \/ Q4_K_S \/ Q5_K_L \/ Q5_K_M \/ Q5_K_S \/ Q6_K \u53ca\u5bf9\u5e94\u4f53\u79ef\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b80\u5355\uff1a\u5355\u6587\u4ef6\uff0c\u6613\u5206\u4eab\u5206\u53d1<\/li>\n\n\n\n<li>\u5feb\u901f\uff1a\u517c\u5bb9 <code>mmap()<\/code>\uff0c\u52a0\u8f7d\/\u4fdd\u5b58\u5feb<\/li>\n\n\n\n<li>\u9ad8\u6548\uff1a\u91cf\u5316\u65b9\u6848\u7075\u6d3b<\/li>\n\n\n\n<li>\u4fbf\u643a\uff1a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u65e0\u9700\u7279\u5b9a\u5e93\u5373\u53ef\u8bfb<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u6570\u6a21\u578b\u9700\u4ece PyTorch \/ Safetensors <strong>\u8f6c\u6362<\/strong>\u800c\u6765<\/li>\n\n\n\n<li>\u5e76\u975e\u6240\u6709\u6a21\u578b\u90fd\u53ef\u8f6c\u6362\uff08\u90e8\u5206\u4e0d\u88ab llama.cpp \u652f\u6301\uff09<\/li>\n\n\n\n<li>\u8f6c\u6210 GGUF \u540e<strong>\u4e0d\u6613\u4fee\u6539\u6216\u5fae\u8c03<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u751f\u4ea7\u73af\u5883<strong>\u6a21\u578b\u670d\u52a1<\/strong>\uff08\u52a0\u8f7d\u901f\u5ea6\u5173\u952e\uff09+ \u5f00\u6e90\u793e\u533a<strong>\u6a21\u578b\u5171\u4eab<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. PyTorch\uff08.pt \/ .pth\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>.pt<\/code> \/ <code>.pth<\/code> \u662f PyTorch \u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u5b58\u50a8\u5305\u542b<strong>\u5b66\u4e60\u53c2\u6570<\/strong>\uff08\u6743\u91cd\u3001\u504f\u7f6e\uff09\u3001<strong>\u4f18\u5316\u5668\u72b6\u6001<\/strong>\u548c<strong>\u8bad\u7ec3\u5143\u6570\u636e<\/strong>\u7684\u6a21\u578b\u72b6\u6001\u5b57\u5178\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>.pt<\/code><\/strong>\uff1a\u4fdd\u5b58<strong>\u6574\u4e2a\u6a21\u578b<\/strong>\uff08\u67b6\u6784 + \u5b66\u4e60\u53c2\u6570\uff09<\/li>\n\n\n\n<li><strong><code>.pth<\/code><\/strong>\uff1a\u4ec5\u4fdd\u5b58<strong>\u72b6\u6001\u5b57\u5178<\/strong>\uff08\u5b66\u4e60\u53c2\u6570 + \u90e8\u5206\u5143\u6570\u636e\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e95\u5c42\u57fa\u4e8e Python \u7684 <a href=\"https:\/\/docs.python.org\/3\/library\/pickle.html\"><code>pickle<\/code><\/a> \u6a21\u5757\u505a\u5bf9\u8c61\u5e8f\u5217\u5316\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b80\u5355\uff1a\u5355\u6587\u4ef6\uff0c\u6613\u5206\u4eab\u5206\u53d1<\/li>\n\n\n\n<li>\u5feb\u901f\uff1a\u517c\u5bb9 <code>mmap()<\/code>\uff0c\u52a0\u8f7d\/\u4fdd\u5b58\u5feb<\/li>\n\n\n\n<li>\u9ad8\u6548\uff1a\u91cf\u5316\u65b9\u6848\u7075\u6d3b<\/li>\n\n\n\n<li>\u4fbf\u643a\uff1a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u65e0\u9700\u7279\u5b9a\u5e93\u5373\u53ef\u8bfb<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u6570\u6a21\u578b\u9700\u4ece PyTorch \/ Safetensors <strong>\u8f6c\u6362<\/strong>\u800c\u6765<\/li>\n\n\n\n<li>\u5e76\u975e\u6240\u6709\u6a21\u578b\u90fd\u53ef\u8f6c\u6362\uff08\u90e8\u5206\u4e0d\u88ab llama.cpp \u652f\u6301\uff09<\/li>\n\n\n\n<li>\u8f6c\u6210 GGUF \u540e<strong>\u4e0d\u6613\u4fee\u6539\u6216\u5fae\u8c03<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u751f\u4ea7\u73af\u5883<strong>\u6a21\u578b\u670d\u52a1<\/strong>\uff08\u52a0\u8f7d\u901f\u5ea6\u5173\u952e\uff09+ \u5f00\u6e90\u793e\u533a<strong>\u6a21\u578b\u5171\u4eab<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. PyTorch\uff08.pt \/ .pth\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>.pt<\/code> \/ <code>.pth<\/code> \u662f PyTorch \u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u5b58\u50a8\u5305\u542b<strong>\u5b66\u4e60\u53c2\u6570<\/strong>\uff08\u6743\u91cd\u3001\u504f\u7f6e\uff09\u3001<strong>\u4f18\u5316\u5668\u72b6\u6001<\/strong>\u548c<strong>\u8bad\u7ec3\u5143\u6570\u636e<\/strong>\u7684\u6a21\u578b\u72b6\u6001\u5b57\u5178\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>.pt<\/code><\/strong>\uff1a\u4fdd\u5b58<strong>\u6574\u4e2a\u6a21\u578b<\/strong>\uff08\u67b6\u6784 + \u5b66\u4e60\u53c2\u6570\uff09<\/li>\n\n\n\n<li><strong><code>.pth<\/code><\/strong>\uff1a\u4ec5\u4fdd\u5b58<strong>\u72b6\u6001\u5b57\u5178<\/strong>\uff08\u5b66\u4e60\u53c2\u6570 + \u90e8\u5206\u5143\u6570\u636e\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e95\u5c42\u57fa\u4e8e Python \u7684 <a href=\"https:\/\/docs.python.org\/3\/library\/pickle.html\"><code>pickle<\/code><\/a> \u6a21\u5757\u505a\u5bf9\u8c61\u5e8f\u5217\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pickle\nmodel_state_dict = {\"layer1\": \"hello\", \"layer2\": \"world\"}\npickle.dump(model_state_dict, open(\"model.pkl\", \"wb\"))\n\n# \u8bfb\u56de\nmodel_state_dict = pickle.load(open(\"model.pkl\", \"rb\"))\nprint(model_state_dict)  # {'layer1': 'hello', 'layer2': 'world'}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"https:\/\/luqimu.xyz\/wp-content\/uploads\/2026\/10\/pytorch-pickle-hex-view.jpg\" alt=\"model.pkl \u7684\u5341\u516d\u8fdb\u5236\u89c6\u56fe\uff0c\u53f3\u4fa7\u662f\u89e3\u7801\u540e\u7684\u6587\u672c layer1 \/ hello \/ layer2 \/ world\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">pickle \u7684\u4e09\u5927\u5c40\u9650<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u4efb\u4f55\u4e1c\u897f\u90fd\u80fd\u88ab pickle\uff0c<strong>\u5305\u62ec\u6076\u610f\u4ee3\u7801<\/strong>\u3002\u5e8f\u5217\u5316\u6570\u636e\u672a\u7ecf\u9a8c\u8bc1\u5373\u52a0\u8f7d\u4f1a\u9020\u6210\u5b89\u5168\u6f0f\u6d1e\u3002\u53c2\u89c1 <a href=\"https:\/\/snyk.io\/articles\/python-pickle-poisoning-and-backdooring-pth-files\/\">Snyk\uff1apickle \u6295\u6bd2\u4e0e .pth \u6587\u4ef6\u540e\u95e8<\/a><\/li>\n\n\n\n<li><strong>\u6548\u7387<\/strong>\uff1a\u4e0d\u652f\u6301\u5ef6\u8fdf\u52a0\u8f7d \/ \u90e8\u5206\u52a0\u8f7d\uff0c\u5927\u6a21\u578b<strong>\u52a0\u8f7d\u6162\u3001\u5185\u5b58\u5360\u7528\u9ad8<\/strong><\/li>\n\n\n\n<li><strong>\u53ef\u79fb\u690d\u6027<\/strong>\uff1aPython \u4e13\u5c5e\uff0c\u8de8\u8bed\u8a00\u5171\u4eab\u56f0\u96be<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u8bba<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u5728\u7eaf Python + PyTorch \u73af\u5883\u91cc\u5de5\u4f5c\u65f6\u53ef\u7528\u3002\u8fd1\u5e74\u793e\u533a\u5df2\u660e\u663e\u8f6c\u5411\u66f4\u9ad8\u6548\u5b89\u5168\u7684 GGUF \u548c Safetensors\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Safetensors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531 <strong>Hugging Face \u5f00\u53d1<\/strong>\uff0c\u7528\u4e8e\u89e3\u51b3 pickle \u7684\u5b89\u5168\u4e0e\u6548\u7387\u95ee\u9898\u3002\u91c7\u7528<strong>\u53d7\u9650\u7684\u53cd\u5e8f\u5217\u5316\u8fc7\u7a0b<\/strong>\u9632\u6b62\u4ee3\u7801\u6267\u884c\u6f0f\u6d1e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6587\u4ef6\u7ed3\u6784<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5143\u6570\u636e\u6bb5<\/strong>\uff08JSON \u683c\u5f0f\uff09\uff1a\u6240\u6709\u5f20\u91cf\u7684\u5f62\u72b6\u3001\u6570\u636e\u7c7b\u578b\u3001\u540d\u79f0\uff0c\u53ef\u9009\u81ea\u5b9a\u4e49\u5143\u6570\u636e<\/li>\n\n\n\n<li><strong>\u5f20\u91cf\u6570\u636e\u6bb5<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"https:\/\/luqimu.xyz\/wp-content\/uploads\/2026\/10\/safetensors-file-structure.jpg\" alt=\"Safetensors \u6587\u4ef6\u7ed3\u6784\uff1a8 \u5b57\u8282 header \u957f\u5ea6 + N \u5b57\u8282 JSON header\uff08\u5f20\u91cf\u540d\u3001dtype\u3001shape\u3001offsets\uff09+ \u5f20\u91cf\u6570\u636e\u672c\u4f53\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u5168<\/strong>\uff1a\u53d7\u9650\u53cd\u5e8f\u5217\u5316\uff0c\u9632\u4ee3\u7801\u6267\u884c\u6f0f\u6d1e<\/li>\n\n\n\n<li><strong>\u5feb\u901f<\/strong>\uff1a\u652f\u6301\u5ef6\u8fdf\u52a0\u8f7d\u4e0e\u90e8\u5206\u52a0\u8f7d\uff0c\u53ef <code>mmap()<\/code> \u6620\u5c04\u6587\u4ef6<\/li>\n\n\n\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u652f\u6301\u91cf\u5316\u5f20\u91cf<\/li>\n\n\n\n<li><strong>\u53ef\u79fb\u690d<\/strong>\uff1a\u8de8\u7f16\u7a0b\u8bed\u8a00\u8bbe\u8ba1<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u91cf\u5316\u65b9\u6848\u4e0d\u5982 GGUF \u7075\u6d3b\uff08\u4e3b\u56e0\u662f PyTorch \u91cf\u5316\u652f\u6301\u6709\u9650\uff09<\/li>\n\n\n\n<li>\u8bfb\u5143\u6570\u636e\u9700 JSON \u89e3\u6790\u5668\uff0c\u5728 C++ \u7b49\u5e95\u5c42\u8bed\u8a00\u4e2d\u8f83\u9ebb\u70e6<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5730\u4f4d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Safetensors \u662f Hugging Face <strong><code>transformers<\/code> \u5e93\u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u683c\u5f0f<\/strong>\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6a21\u578b\u7684<strong>\u5171\u4eab\u3001\u8bad\u7ec3\u3001\u5fae\u8c03\u548c\u90e8\u7f72<\/strong>\u3002HF \u4e0a\u65b0\u53d1\u5e03\u7684\u6a21\u578b\u90fd\u4ee5 safetensors \u5b58\u50a8\uff0c\u5305\u62ec <strong>Llama\u3001Gemma\u3001Phi\u3001Stable-Diffusion\u3001Flux<\/strong> \u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. ONNX<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Open Neural Network Exchange<\/strong>\uff08\u5f00\u653e\u795e\u7ecf\u7f51\u7edc\u4ea4\u6362\u683c\u5f0f\uff09\uff0c\u63d0\u4f9b<strong>\u4e0e\u4f9b\u5e94\u5546\u65e0\u5173<\/strong>\u7684\u6a21\u578b\u8868\u793a\u65b9\u6cd5\uff0c\u662f <a href=\"https:\/\/onnx.ai\/\">ONNX \u751f\u6001<\/a> \u7684\u4e00\u90e8\u5206\u2014\u2014\u5305\u542b PyTorch\u3001TensorFlow\u3001MXNet \u7b49\u6846\u67b6\u95f4\u7684\u4e92\u64cd\u4f5c\u5de5\u5177\u4e0e\u5e93\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5355\u6587\u4ef6 <code>.onnx<\/code> \u4fdd\u5b58\u3002\u4e0e GGUF \/ Safetensors \u7684\u5173\u952e\u5dee\u5f02\uff1aONNX <strong>\u4e0d\u4ec5\u5305\u542b\u5f20\u91cf\u548c\u5143\u6570\u636e\uff0c\u8fd8\u5305\u542b\u6a21\u578b\u7684\u300c\u8ba1\u7b97\u56fe\u300d<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4ef6\u91cc\u5e26\u8ba1\u7b97\u56fe = \u5904\u7406\u6a21\u578b\u65f6\u66f4\u7075\u6d3b\u3002\u4f8b\u5982\u65b0\u6a21\u578b\u53d1\u5e03\u65f6\u53ef\u76f4\u63a5\u8f6c\u4e3a ONNX\uff0c<strong>\u65e0\u9700\u5173\u5fc3\u5176\u67b6\u6784\u6216\u63a8\u7406\u4ee3\u7801<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"https:\/\/luqimu.xyz\/wp-content\/uploads\/2026\/10\/onnx-computation-graph.jpg\" alt=\"ONNX \u8ba1\u7b97\u56fe\u793a\u4f8b\uff08Netron \u751f\u6210\uff09\uff1aMatMulInteger \/ Cast \/ Mul \/ Add \/ Shape \/ Gather \/ Unsqueeze \/ Concat \/ Slice \/ Sqrt \u7b49\u7b97\u5b50\u53ca\u5176\u5f20\u91cf\u5f62\u72b6\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u6587\u4ef6\u5185\u81ea\u5e26\u8ba1\u7b97\u56fe\uff0c\u8de8\u6846\u67b6\u8f6c\u6362\u66f4\u7075\u6d3b<\/li>\n\n\n\n<li><strong>\u53ef\u79fb\u690d\u6027<\/strong>\uff1a\u4f9d\u6258 ONNX \u751f\u6001\uff0c\u53ef\u8f7b\u677e\u90e8\u7f72\u5230\u5404\u7c7b\u5e73\u53f0\u4e0e\u8bbe\u5907\uff0c\u542b\u79fb\u52a8\u7aef\u548c\u8fb9\u7f18\u8bbe\u5907<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9<strong>\u91cf\u5316\u5f20\u91cf\u652f\u6301\u6709\u9650<\/strong>\uff1aONNX \u672c\u8eab\u4e0d\u652f\u6301\u91cf\u5316\u5f20\u91cf\uff0c\u800c\u662f\u62c6\u6210\u6574\u6570\u5f20\u91cf + \u6bd4\u4f8b\u56e0\u5b50\u5f20\u91cf\uff0c\u5904\u7406\u91cf\u5316\u6a21\u578b\u65f6\u53ef\u80fd\u6389\u8d28\u91cf<\/li>\n\n\n\n<li><strong>\u590d\u6742\u67b6\u6784<\/strong>\uff1a\u4e0d\u652f\u6301\u7684\u5c42\u9700\u8981\u7b97\u5b50\u56de\u9000\u6216\u81ea\u5b9a\u4e49\u5b9e\u73b0\uff0c\u8f6c\u6362\u65f6\u53ef\u80fd\u5e26\u6765\u6027\u80fd\u635f\u5931<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u8bba<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5904\u7406<strong>\u79fb\u52a8\u8bbe\u5907\u6216\u6d4f\u89c8\u5668\u5185\u63a8\u7406<\/strong>\u65f6\uff0cONNX \u662f\u597d\u9009\u62e9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. HuggingFace \u6a21\u578b\u4ed3\u5e93\u7684\u6587\u4ef6\u683c\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 \u4e00\u4e2a\u5178\u578b LLM \u4ed3\u5e93\u7684\u76ee\u5f55\u7ed3\u6784<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Qwen\/Qwen2.5-7B-Instruct\/<br>\u251c\u2500\u2500 .gitattributes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # LFS \u89c4\u5219\uff1a\u54ea\u4e9b\u540e\u7f00\u8d70\u5927\u6587\u4ef6\u5b58\u50a8<br>\u251c\u2500\u2500 README.md &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Model Card\uff08YAML frontmatter + Markdown \u6b63\u6587\uff09<br>\u251c\u2500\u2500 config.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u6a21\u578b\u67b6\u6784\u914d\u7f6e\uff1a\u51b3\u5b9a transformers \u7528\u54ea\u4e2a\u6a21\u578b\u7c7b<br>\u251c\u2500\u2500 generation_config.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u751f\u6210\u53c2\u6570\u9ed8\u8ba4\u503c\uff08\u6e29\u5ea6\u3001top_p\u3001eos \u7b49\uff09<br>\u251c\u2500\u2500 model-00001-of-00004.safetensors  # \u6743\u91cd\u5206\u7247\uff08\u5355\u6587\u4ef6\u65f6\u53eb model.safetensors\uff09<br>\u251c\u2500\u2500 model-00002-of-00004.safetensors<br>\u251c\u2500\u2500 model-00003-of-00004.safetensors<br>\u251c\u2500\u2500 model-00004-of-00004.safetensors<br>\u251c\u2500\u2500 model.safetensors.index.json &nbsp; &nbsp;  # \u5206\u7247\u7d22\u5f15\uff1a\u53c2\u6570\u540d \u2192 \u5206\u7247\u6587\u4ef6<br>\u251c\u2500\u2500 tokenizer.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u5b8c\u6574 tokenizer\uff08\u5feb\uff0c\u63a8\u8350\uff09<br>\u251c\u2500\u2500 tokenizer_config.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # tokenizer \u7c7b\u4e0e\u53c2\u6570<br>\u251c\u2500\u2500 vocab.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # BPE \u8bcd\u8868\uff08\u65e7\u5f0f tokenizer \u624d\u5355\u72ec\u653e\uff09<br>\u251c\u2500\u2500 merges.txt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # BPE \u5408\u5e76\u89c4\u5219<br>\u251c\u2500\u2500 special_tokens_map.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u7279\u6b8a token \u6620\u5c04\uff08bos\/eos\/pad\/unk\uff09<br>\u251c\u2500\u2500 added_tokens.json &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u624b\u52a8\u8ffd\u52a0\u7684 token<br>\u251c\u2500\u2500 chat_template.jinja &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u5bf9\u8bdd\u6a21\u677f\uff08\u65b0\u7248\u72ec\u7acb\u6210\u6587\u4ef6\uff09<br>\u2514\u2500\u2500 preprocessor_config.json &nbsp; &nbsp; &nbsp; &nbsp;  # \u591a\u6a21\u6001\u6a21\u578b\u7684\u56fe\u50cf\/\u97f3\u9891\u9884\u5904\u7406\u914d\u7f6e<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 README.md<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6a21\u578b\u5361\u672c\u8d28\u662f\u4e00\u4e2a Markdown \u6587\u4ef6\uff0c<strong>\u9876\u90e8 <code>---<\/code> \u5305\u8d77\u6765\u7684\u4e00\u6bb5 YAML<\/strong> \u5c31\u662f\u5143\u6570\u636e\u3002\u8fd9\u90e8\u5206\u76f4\u63a5\u9a71\u52a8 HF \u7684\u7b5b\u9009\u5668\u3001\u6a21\u578b\u9875\u5c55\u793a\u3001\u4ee5\u53ca\u52a0\u8f7d\u5de5\u5177\u7684\u81ea\u52a8\u63a8\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">---<br>language:<br>  - zh<br>  - en<br>thumbnail: \"url to a thumbnail used in social sharing\"<br>tags:<br>  - text-generation<br>  - qwen2<br>license: apache-2.0<br>datasets:<br>  - stanfordnlp\/imdb<br>base_model: HuggingFaceH4\/zephyr-7b-beta<br>library_name: transformers<br>pipeline_tag: text-generation<br>---<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>language<\/code><\/td><td>\u8bed\u8a00\u4ee3\u7801\u5217\u8868\uff0c\u7528\u4e8e hub \u4e0a\u7684\u8bed\u8a00\u7b5b\u9009<\/td><\/tr><tr><td><code>tags<\/code><\/td><td>\u6807\u7b7e\uff0c\u51b3\u5b9a\u6a21\u578b\u9875\u663e\u793a\u7684 tag\uff1b\u53ef\u81ea\u5b9a\u4e49\uff08\u5982 <code>finance<\/code>\uff09<\/td><\/tr><tr><td><code>license<\/code><\/td><td>\u8bb8\u53ef\u8bc1\u6807\u8bc6\uff0c\u7528\u4e8e\u7b5b\u9009\u4e0e\u5c55\u793a\uff1b\u81ea\u5b9a\u4e49\u8bb8\u53ef\u8bc1\u5199 <code>license: other<\/code> + <code>license_name<\/code> + <code>license_link<\/code><\/td><\/tr><tr><td><code>datasets<\/code><\/td><td>\u8bad\u7ec3\u6240\u7528\u6570\u636e\u96c6 Hub ID\uff0c\u6a21\u578b\u9875\u4f1a\u663e\u793a \u201cDatasets used to train:\u201d \u5e76\u94fe\u63a5\u8fc7\u53bb<\/td><\/tr><tr><td><code>base_model<\/code><\/td><td>\u57fa\u5ea7\u6a21\u578b ID\uff1b\u53ef\u4ee5\u662f<strong>\u5217\u8868<\/strong>\uff08\u5408\u5e76\u591a\u4e2a\u6a21\u578b\u65f6\uff09<\/td><\/tr><tr><td><code>base_model_relation<\/code><\/td><td>\u4e0e\u57fa\u5ea7\u7684\u5173\u7cfb\uff1a<code>adapter<\/code> \/ <code>merge<\/code> \/ <code>quantized<\/code> \/ <code>finetune<\/code>\u3002Hub \u4f1a\u81ea\u52a8\u63a8\u65ad\uff0c\u4e5f\u53ef\u663e\u5f0f\u6307\u5b9a<\/td><\/tr><tr><td><code>library_name<\/code><\/td><td>\u652f\u6301\u8be5\u6a21\u578b\u7684\u5e93\uff08\u5982 <code>transformers<\/code>\u3001<code>flair<\/code>\uff09\u3002<strong>\u975e transformers \u6a21\u578b\u5f3a\u70c8\u5efa\u8bae\u663e\u5f0f\u5199<\/strong><\/td><\/tr><tr><td><code>pipeline_tag<\/code><\/td><td>\u4efb\u52a1\u7c7b\u578b\uff08\u5982 <code>text-generation<\/code>\uff09\uff0c\u51b3\u5b9a\u6a21\u578b\u9875\u7528\u54ea\u4e2a widget \u548c\u5e95\u5c42 API<\/td><\/tr><tr><td><code>buckets<\/code><\/td><td>\u5173\u8054\u7684\u5b58\u50a8\u6876 ID<\/td><\/tr><tr><td><code>thumbnail<\/code><\/td><td>\u793e\u4ea4\u5206\u4eab\u7f29\u7565\u56fe<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5.3 config.json \u2014\u2014 \u67b6\u6784\u914d\u7f6e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>transformers<\/code> \u9760\u5b83\u51b3\u5b9a\u52a0\u8f7d\u54ea\u4e2a\u6a21\u578b\u7c7b\u3001\u600e\u4e48\u642d\u7f51\u7edc\u3002\u5e38\u89c1\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td><code>architectures<\/code><\/td><td>\u67b6\u6784\u7c7b\u540d\uff0c\u5982 <code>[\"Qwen2ForCausalLM\"]<\/code><\/td><\/tr><tr><td><code>model_type<\/code><\/td><td>\u6a21\u578b\u7c7b\u578b\uff0c\u5982 <code>qwen2<\/code>\u3001<code>llama<\/code>\uff08\u65e7\u7248 transformers \u4f1a\u56e0\u6b64\u62a5 <code>KeyError<\/code>\uff09<\/td><\/tr><tr><td><code>hidden_size<\/code><\/td><td>\u9690\u85cf\u5c42\u7ef4\u5ea6<\/td><\/tr><tr><td><code>num_hidden_layers<\/code><\/td><td>\u5c42\u6570<\/td><\/tr><tr><td><code>num_attention_heads<\/code> \/ <code>num_key_value_heads<\/code><\/td><td>Q \u5934\u6570 \/ KV \u5934\u6570\uff08GQA \u65f6\u4e8c\u8005\u4e0d\u7b49\uff09<\/td><\/tr><tr><td><code>intermediate_size<\/code><\/td><td>FFN \u4e2d\u95f4\u7ef4\u5ea6<\/td><\/tr><tr><td><code>vocab_size<\/code><\/td><td>\u8bcd\u8868\u5927\u5c0f<\/td><\/tr><tr><td><code>max_position_embeddings<\/code><\/td><td>\u6700\u5927\u4f4d\u7f6e\u7f16\u7801\u957f\u5ea6<\/td><\/tr><tr><td><code>rope_scaling<\/code><\/td><td>\u957f\u4e0a\u4e0b\u6587\u5916\u63a8\u914d\u7f6e\uff0c\u5982 <code>{\"type\": \"yarn\", \"factor\": 4.0, \"original_max_position_embeddings\": 32768}<\/code><\/td><\/tr><tr><td><code>torch_dtype<\/code><\/td><td>\u6743\u91cd\u7cbe\u5ea6\uff0c\u5982 <code>bfloat16<\/code><\/td><\/tr><tr><td><code>tie_word_embeddings<\/code><\/td><td>\u662f\u5426\u5171\u4eab\u8f93\u5165\/\u8f93\u51fa embedding<\/td><\/tr><tr><td><code>quantization_config<\/code><\/td><td>\u91cf\u5316\u914d\u7f6e\uff08GPTQ \/ AWQ \/ bitsandbytes \u7b49\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5.4 generation_config.json \u2014\u2014 \u751f\u6210\u53c2\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>model.generate()<\/code> \u7684\u9ed8\u8ba4\u8d85\u53c2\uff0c\u52a0\u8f7d\u65f6\u4e00\u5e76\u8bfb\u5165\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<br>&#8220;bos_token_id&#8221;: 151643,<br>&#8220;eos_token_id&#8221;: 151645,<br>&#8220;max_new_tokens&#8221;: 2048,<br>&#8220;temperature&#8221;: 0.7,<br>&#8220;top_p&#8221;: 0.8,<br>&#8220;top_k&#8221;: 20,<br>&#8220;repetition_penalty&#8221;: 1.05,<br>&#8220;do_sample&#8221;: true<br>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.5 \u6743\u91cd\u6587\u4ef6\u4e0e\u5206\u7247<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6587\u4ef6<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>model.safetensors<\/code><\/td><td>\u5c0f\u6a21\u578b\u5355\u6587\u4ef6\uff0c\u63a8\u8350\u683c\u5f0f<\/td><\/tr><tr><td><code>model-0000X-of-0000N.safetensors<\/code><\/td><td>\u5927\u6a21\u578b\u5206\u7247\uff08sharded checkpoint\uff09<\/td><\/tr><tr><td><code>model.safetensors.index.json<\/code><\/td><td>\u5206\u7247\u7d22\u5f15<\/td><\/tr><tr><td><code>pytorch_model.bin<\/code><\/td><td>\u65e7\u5f0f PyTorch \u6743\u91cd\uff0c\u57fa\u4e8e pickle\uff0c<strong>\u5b58\u5728\u5b89\u5168\u98ce\u9669<\/strong>\uff0c\u5c3d\u91cf\u907f\u5f00<\/td><\/tr><tr><td><code>.gitattributes<\/code><\/td><td>\u58f0\u660e <code>*.safetensors<\/code> \u7b49\u8d70 Git LFS<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5206\u7247\u89c4\u5219<\/strong>\uff1a<code>save_pretrained()<\/code> \u4f1a\u81ea\u52a8\u628a<strong>\u8d85\u8fc7 50GB<\/strong> \u7684 checkpoint \u5206\u7247\uff0c\u53ef\u7528 <code>max_shard_size<\/code> \u8c03\u6574\u9608\u503c\u3002\u5206\u7247\u5e76\u884c\u52a0\u8f7d\uff0c\u5cf0\u503c\u5185\u5b58\u53ea\u53d6\u51b3\u4e8e\u6a21\u578b\u5927\u5c0f\uff08MoE \u4f8b\u5916\uff1a\u5185\u5b58 \u2248 \u6a21\u578b\u5927\u5c0f + \u5355\u5c42\u4e13\u5bb6\u6570\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7d22\u5f15\u6587\u4ef6 <code>model.safetensors.index.json<\/code> \u53ea\u6709\u4e24\u4e2a\u952e<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>metadata<\/code>\uff1a\u5b58\u603b\u5927\u5c0f\uff0c\u5982 <code>{\"total_size\": 28966928384}<\/code><\/li>\n\n\n\n<li><code>weight_map<\/code>\uff1a\u53c2\u6570\u540d \u2192 \u6240\u5c5e\u5206\u7247\u6587\u4ef6<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br>  \"metadata\": { \"total_size\": 28966928384 },<br>  \"weight_map\": {<br> &nbsp;  \"lm_head.weight\": \"model-00006-of-00006.safetensors\",<br> &nbsp;  \"model.embed_tokens.weight\": \"model-00001-of-00006.safetensors\",<br> &nbsp;  \"model.layers.0.input_layernorm.weight\": \"model-00001-of-00006.safetensors\",<br> &nbsp;  \"model.layers.0.mlp.down_proj.weight\": \"model-00001-of-00006.safetensors\"<br>  }<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.6 Tokenizer \u76f8\u5173\u6587\u4ef6<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6587\u4ef6<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>tokenizer.json<\/code><\/td><td><strong>\u5b8c\u6574<\/strong> tokenizer\uff08\u8bcd\u8868 + \u89c4\u5219\uff09\uff0c\u52a0\u8f7d\u6700\u5feb\uff0c\u65b0\u6a21\u578b\u57fa\u672c\u53ea\u7559\u8fd9\u4e2a<\/td><\/tr><tr><td><code>tokenizer_config.json<\/code><\/td><td>tokenizer \u7c7b\u540d\u4e0e\u53c2\u6570\uff0c\u5982 <code>model_max_length<\/code>\u3001<code>padding_side<\/code><\/td><\/tr><tr><td><code>vocab.json<\/code> + <code>merges.txt<\/code><\/td><td>\u65e7\u5f0f BPE tokenizer \u7684\u8bcd\u8868\u4e0e\u5408\u5e76\u89c4\u5219<\/td><\/tr><tr><td><code>tokenizer.model<\/code><\/td><td>SentencePiece\uff08LLaMA \u7cfb\uff09\u8bcd\u8868<\/td><\/tr><tr><td><code>special_tokens_map.json<\/code><\/td><td><code>bos<\/code> \/ <code>eos<\/code> \/ <code>pad<\/code> \/ <code>unk<\/code> \u7684\u6620\u5c04<\/td><\/tr><tr><td><code>added_tokens.json<\/code><\/td><td>\u624b\u52a8\u8ffd\u52a0\u7684\u7279\u6b8a token<\/td><\/tr><tr><td><code>chat_template.jinja<\/code><\/td><td>\u5bf9\u8bdd\u6a21\u677f\uff0c\u628a <code>[{role, content}]<\/code> \u6e32\u67d3\u6210\u6a21\u578b\u9700\u8981\u7684 prompt \u683c\u5f0f\uff1b\u65b0\u7248\u652f\u6301\u72ec\u7acb\u6210\u6587\u4ef6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u6a21\u578b\u6587\u4ef6\u89e3\u6790\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 \u89e3\u6790\u539f\u5219<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u7ba1\u4ec0\u4e48\u683c\u5f0f\uff0c\u6240\u6709\u89e3\u6790\u90fd\u9075\u5faa\u540c\u4e00\u4e2a\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u7b2c 1 \u6b65  \u770b\u9b54\u6570               \u2192  \u786e\u5b9a\u300c\u5bb9\u5668\u7c7b\u578b\u300d<br>\u7b2c 2 \u6b65  \u6309\u5bb9\u5668\u7ed3\u6784\u62c6\u5c42 &nbsp; &nbsp; &nbsp; &nbsp;  \u2192  \u627e\u5230\u300c\u5143\u6570\u636e\u533a\u300d\u548c\u300c\u5f20\u91cf\u6570\u636e\u533a\u300d<br>\u7b2c 3 \u6b65  \u8bfb\u5143\u6570\u636e \u2192 \u7d22\u5f15\u6570\u636e &nbsp; &nbsp; \u2192  \u62ff\u5230\u5f20\u91cf\u540d \/ \u5f62\u72b6 \/ dtype\uff0c\u518d\u6309\u9700\u5207\u7247<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e3a\u4ec0\u4e48\u5fc5\u987b\u5148\u770b\u9b54\u6570\uff1f<\/strong> \u56e0\u4e3a\u6269\u5c55\u540d\u4f1a\u9a97\u4eba\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.pth<\/code> \/ <code>.pt<\/code> \/ <code>.ckpt<\/code> \/ <code>.bin<\/code> \u7269\u7406\u4e0a\u53ef\u80fd\u662f <strong>pickle<\/strong>\uff0c\u4e5f\u53ef\u80fd\u662f <strong>zip<\/strong>\uff08\u53d6\u51b3\u4e8e PyTorch \u7248\u672c\uff09<\/li>\n\n\n\n<li><code>.pb<\/code> \u53ea\u662fprotobuf \u5e8f\u5217\u5316\u7684\u7ea6\u5b9a\u6269\u5c55\u540d\uff0c<strong>\u6ca1\u6709\u56fa\u5b9a\u9b54\u6570<\/strong>\uff0c\u5149\u770b\u6587\u4ef6\u5934\u5224\u4e0d\u51fa\u6765<\/li>\n\n\n\n<li><code>.h5<\/code> \u548c Keras 3 \u7684 <code>.keras<\/code> \u5b8c\u5168\u4e0d\u662f\u4e00\u4e2a\u4e1c\u897f \u2014\u2014 \u524d\u8005\u662f HDF5\uff0c\u540e\u8005\u662f zip<\/li>\n\n\n\n<li><code>.ckpt<\/code> \u53ea\u662f\u8bad\u7ec3\u811a\u672c\u7684\u547d\u540d\u4e60\u60ef\uff08Lightning \u7b49\u7231\u7528\uff09\uff0c\u7269\u7406\u683c\u5f0f\u53e6\u6709\u5176\u4e8b<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u547d\u4ee4<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xxd -l 32 model.pth &nbsp; &nbsp; # \u770b\u524d 32 \u5b57\u8282\u7684\u5341\u516d\u8fdb\u5236<br>file model.pth &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u7cfb\u7edf\u7ea7\u6587\u4ef6\u7c7b\u578b\u8bc6\u522b\uff08\u90e8\u5206\u73af\u5883\u652f\u6301\uff09<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 \u9b54\u6570\u901f\u67e5\u8868<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9b54\u6570\uff08\u5341\u516d\u8fdb\u5236 \/ ASCII\uff09<\/th><th>\u5bb9\u5668\u7c7b\u578b<\/th><th>\u5178\u578b\u6587\u4ef6<\/th><th>\u600e\u4e48\u8bfb<\/th><\/tr><\/thead><tbody><tr><td><code>50 4b 03 04<\/code>\uff08<code>PK..<\/code>\uff09<\/td><td>ZIP<\/td><td>torch \u65b0\u683c\u5f0f <code>.pt\/.pth<\/code>\u3001Keras3 <code>.keras<\/code>\u3001<code>.npz<\/code><\/td><td><code>zipfile<\/code> \/ <code>torch.load<\/code><\/td><\/tr><tr><td><code>80 02<\/code> \/ <code>80 04<\/code> \/ <code>80 05<\/code><\/td><td>Pickle<\/td><td>torch \u8001\u683c\u5f0f <code>.pt\/.pth\/.ckpt<\/code><\/td><td><code>torch.load<\/code><\/td><\/tr><tr><td><code>89 48 44 46 0d 0a 1a 0a<\/code>\uff08<code>\\x89HDF\\r\\n\\x1a\\n<\/code>\uff09<\/td><td>HDF5<\/td><td>Keras \/ TF <code>.h5<\/code> <code>.hdf5<\/code><\/td><td><code>h5py.File<\/code> \/ <code>keras<\/code><\/td><\/tr><tr><td><code>47 47 55 46<\/code>\uff08<code>GGUF<\/code>\uff09<\/td><td>GGUF<\/td><td>llama.cpp \/ ollama \u91cf\u5316\u6a21\u578b<\/td><td><code>gguf.GGUFReader<\/code><\/td><\/tr><tr><td><code>47 47 4d 4c<\/code>\uff08<code>GGML<\/code>\uff09<\/td><td>GGML<\/td><td>llama.cpp \u65e9\u671f\u683c\u5f0f\uff08\u5df2\u6dd8\u6c70\uff09<\/td><td>\u8f6c GGUF \u540e\u518d\u8bfb<\/td><\/tr><tr><td>\u504f\u79fb 4 \u5904\u4e3a <code>54 46 4c 33<\/code>\uff08<code>TFL3<\/code>\uff09<\/td><td>FlatBuffers<\/td><td>TFLite <code>.tflite<\/code><\/td><td><code>tf.lite.Interpreter<\/code><\/td><\/tr><tr><td><strong>\u65e0\u56fa\u5b9a\u9b54\u6570<\/strong><\/td><td>protobuf<\/td><td>TF <code>.pb<\/code>\u3001ONNX <code>.onnx<\/code><\/td><td>TF \/ onnx \u5e93 + netron<\/td><\/tr><tr><td><strong>\u524d 8 \u5b57\u8282\u4e3a\u5c0f\u7aef uint64 \u957f\u5ea6\uff0c\u5176\u540e\u7d27\u8ddf <code>{<\/code><\/strong><\/td><td>safetensors<\/td><td>HF \u6743\u91cd\u3001LLM \u5206\u7247<\/td><td><code>safetensors.safe_open<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">6.3 \u9010\u683c\u5f0f\u89e3\u6790<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.1 PyTorch <code>.pth<\/code> \/ <code>.pt<\/code> \/ <code>.ckpt<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u672c\u8d28<\/strong>\uff1a<code>torch.save<\/code> \u662f <strong>pickle \u5e8f\u5217\u5316<\/strong>\uff0c\u5b58\u7684\u662fPython \u5bf9\u8c61\u56fe\uff0c\u4e0d\u662f\u901a\u7528\u5f20\u91cf\u683c\u5f0f\u3002\u6240\u4ee5 <code>.pth<\/code> \u91cc\u53ef\u80fd\u88c5 <code>dict<\/code>\u3001<code>nn.Module<\/code>\uff0c\u751a\u81f3\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e24\u79cd\u7269\u7406\u683c\u5f0f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A. \u8001\u683c\u5f0f\uff08torch &lt; 1.6\uff0c\u6216 _use_new_zipfile_serialization=False\uff09<br> &nbsp; \u5f00\u5934: 80 02 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;- pickle protocol 2<br> &nbsp; \u7ed3\u6784: \u7eaf pickle \u6d41\uff0c\u5f20\u91cf\u5b57\u8282\u5185\u8054\u5728 pickle \u91cc<br>\u200b<br>B. \u65b0\u683c\u5f0f\uff08torch &gt;= 1.6 \u9ed8\u8ba4\uff09<br> &nbsp; \u5f00\u5934: 50 4b 03 04\uff08PK..\uff09 &nbsp; &nbsp; &nbsp; &lt;- zip \u5bb9\u5668<br> &nbsp; \u7ed3\u6784: &lt;prefix&gt;\/data.pkl &nbsp; &nbsp; &nbsp;  &lt;- \u5bf9\u8c61\u56fe\uff08pickle\uff09<br> &nbsp; &nbsp; &nbsp; &nbsp; &lt;prefix&gt;\/data\/0,1,2...  &lt;- \u6bcf\u4e2a\u5f20\u91cf\u7684\u88f8\u5b57\u8282<br> &nbsp; &nbsp; &nbsp; &nbsp; &lt;prefix&gt;\/version &nbsp; &nbsp; &nbsp;  &lt;- \u683c\u5f0f\u7248\u672c<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bfb\u53d6\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import torch, zipfile<br>\u200b<br># \u65b9\u5f0f\u4e00\uff1a\u5b98\u65b9\u52a0\u8f7d\uff08\u6700\u5e38\u7528\uff09<br>sd = torch.load(\"model.pth\", map_location=\"cpu\")<br>sd = sd.get(\"state_dict\", sd) &nbsp; &nbsp; &nbsp;# \u89e3\u5305 Lightning \/ \u81ea\u8bad\u811a\u672c\u7684\u5d4c\u5957<br>for k, v in sd.items():<br> &nbsp; &nbsp;print(k, tuple(v.shape), v.dtype)<br>\u200b<br># \u65b9\u5f0f\u4e8c\uff1a\u5148\u770b\u5b83\u5230\u5e95\u662f zip \u8fd8\u662f pickle<br>if zipfile.is_zipfile(\"model.pth\"):<br> &nbsp; &nbsp;with zipfile.ZipFile(\"model.pth\") as z:<br> &nbsp; &nbsp; &nbsp; &nbsp;names = z.namelist()<br> &nbsp; &nbsp; &nbsp; &nbsp;prefix = next(n[:-len(\"data.pkl\")] for n in names if n.endswith(\"\/data.pkl\"))<br> &nbsp; &nbsp; &nbsp; &nbsp;print(\"prefix =\", prefix, \"| \u5f20\u91cf\u5757\u6570 =\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum(1 for n in names if \"\/data\/\" in n and n.split(\"\/data\/\")[-1].isdigit()))<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.2 TensorFlow \/ Keras <code>.h5<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u672c\u8d28<\/strong>\uff1a<strong>HDF5<\/strong> \u2014\u2014 \u5c42\u7ea7\u5316\u7684\u79d1\u5b66\u6570\u636e\u5bb9\u5668\uff0c\u50cf\u6587\u4ef6\u7cfb\u7edf\u91cc\u7684\u6587\u4ef6\u7cfb\u7edf\u3002Keras \u628a\u6743\u91cd + \u6a21\u578b\u7ed3\u6784\uff08JSON \u5b57\u7b26\u4e32\uff09\u90fd\u585e\u8fdb HDF5 \u6811\u91cc\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u6784<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  attrs: keras_version, backend, model_config(JSON\u5b57\u7b26\u4e32)<br>  \/model_weights\/ &nbsp; &nbsp; &nbsp;  \u6bcf\u5c42\u6743\u91cd<br> &nbsp;  \/dense\/<br> &nbsp; &nbsp;  dense\/kernel:0 &nbsp; &nbsp; &lt;- Dataset\uff0c\u771f\u6b63\u7684 ndarray<br> &nbsp; &nbsp;  dense\/bias:0<br>  \/optimizer_weights\/ &nbsp;  \u4f18\u5316\u5668\u72b6\u6001\uff08\u5982\u679c\u6709\uff09<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bfb\u53d6\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import h5py<br>\u200b<br># \u65b9\u5f0f\u4e00\uff1a\u76f4\u63a5\u904d\u5386\uff08\u770b\u7ed3\u6784\u6700\u76f4\u89c2\uff09<br>with h5py.File(\"model.h5\", \"r\") as f:<br> &nbsp; &nbsp;print(\"\u6839\u5c5e\u6027:\", dict(f.attrs))<br> &nbsp; &nbsp;f.visititems(lambda n, o: print(<br> &nbsp; &nbsp; &nbsp;  (\"D \" if isinstance(o, h5py.Dataset) else \"G \") + n,<br> &nbsp; &nbsp; &nbsp;  (o.shape, o.dtype) if isinstance(o, h5py.Dataset) else \"\"))<br>\u200b<br># \u65b9\u5f0f\u4e8c\uff1a\u5b98\u65b9\u52a0\u8f7d\uff08\u9700\u8981\u7248\u672c\u517c\u5bb9\uff09<br># import tensorflow as tf<br># model = tf.keras.models.load_model(\"model.h5\")<br># model.summary()<br>\u200b<br># \u65b9\u5f0f\u4e09\uff1a\u53ea\u62a0\u6743\u91cd\u6210 numpy<br>weights = {}<br>with h5py.File(\"model.h5\", \"r\") as f:<br> &nbsp; &nbsp;f.visititems(lambda n, o: weights.update({n: o[()]})<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isinstance(o, h5py.Dataset) and n.startswith(\"model_weights\") else None)<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.3 TensorFlow <code>.pb<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u672c\u8d28<\/strong>\uff1a<code>.pb<\/code> = <strong>Protocol Buffers<\/strong> \u4e8c\u8fdb\u5236\uff0cTF \u7528\u5b83\u5e8f\u5217\u5316\u8ba1\u7b97\u56fe\uff08GraphDef\uff09\u3002<strong><code>.pb<\/code> \u6ca1\u6709\u56fa\u5b9a\u9b54\u6570<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u4e1c\u897f\u90fd\u53eb <code>.pb<\/code><\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A. \u51bb\u7ed3\u56fe frozen_graph.pb \u2014\u2014 \u5355\u4e2a\u6587\u4ef6<br> &nbsp; \u5185\u5bb9: GraphDef \u6d88\u606f\uff08\u8282\u70b9\u5b9a\u4e49 + \u6743\u91cd\uff0c\u6743\u91cd\u901a\u5e38\u88ab freeze \u6210 Const \u8282\u70b9\u5d4c\u8fdb\u53bb\uff09<br> &nbsp; \u8bfb\u53d6:<br> &nbsp; &nbsp; &nbsp; import tensorflow as tf<br> &nbsp; &nbsp; &nbsp; gd = tf.compat.v1.GraphDef()<br> &nbsp; &nbsp; &nbsp; gd.ParseFromString(open(\"frozen.pb\", \"rb\").read())<br> &nbsp; &nbsp; &nbsp; print(len(gd.node), \"\u4e2a\u8282\u70b9\")<br> &nbsp; &nbsp; &nbsp; for n in gd.node[:10]:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(n.name, n.op)<br>\u200b<br>B. SavedModel \u2014\u2014 \u4e00\u4e2a\u76ee\u5f55<br> &nbsp; saved_model.pb &nbsp; &nbsp;  &lt;- MetaGraphDef\uff08\u56fe + \u7b7e\u540d\uff09<br> &nbsp; variables\/ &nbsp; &nbsp; &nbsp; &nbsp;  &lt;- \u6743\u91cd\uff08variables.data-00000-of-00001 + .index\uff09<br> &nbsp; assets\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;- tokenizer \u7b49\u9644\u5c5e\u6587\u4ef6<br> &nbsp; \u8bfb\u53d6:<br> &nbsp; &nbsp; &nbsp; m = tf.saved_model.load(\"saved_model_dir\")<br> &nbsp; &nbsp; &nbsp; print(m.signatures.keys())<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u624b\u5de5\u626b protobuf \u5b57\u6bb5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># protobuf \u7684 wire format: [tag varint][payload]<br># tag = (field_number &lt;&lt; 3) | wire_type<br>def scan(buf):<br> &nbsp; &nbsp;i = 0<br> &nbsp; &nbsp;while i &lt; len(buf):<br> &nbsp; &nbsp; &nbsp; &nbsp;shift, val = 0, 0<br> &nbsp; &nbsp; &nbsp; &nbsp;while True: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u8bfb tag varint<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b = buf[i]; i += 1<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;val |= (b &amp; 0x7F) &lt;&lt; shift<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not (b &amp; 0x80): break<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shift += 7<br> &nbsp; &nbsp; &nbsp; &nbsp;field_no, wire_type = val &gt;&gt; 3, val &amp; 0x07<br> &nbsp; &nbsp; &nbsp; &nbsp;if wire_type == 2: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # length-delimited<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shift, ln = 0, 0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while True: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u8bfb\u957f\u5ea6 varint<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b = buf[i]; i += 1<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ln |= (b &amp; 0x7F) &lt;&lt; shift<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not (b &amp; 0x80): break<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shift += 7<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(f\"field#{field_no} len={ln} -&gt; {buf[i:i+ln][:40]!r}\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i += ln<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.4 ONNX <code>.onnx<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u672c\u8d28<\/strong>\uff1a\u4e5f\u662f <strong>protobuf<\/strong>\u3002\u9876\u5c42\u6d88\u606f <code>ModelProto<\/code>\uff0c\u5d4c\u5957\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ModelProto<br>  ir_version, producer_name, opset_import[]<br>  graph: GraphProto<br> &nbsp; &nbsp;  node[] &nbsp; &nbsp; &nbsp; &nbsp;  &lt;- \u7b97\u5b50\u8282\u70b9\uff08op_type, input[], output[]\uff09<br> &nbsp; &nbsp;  initializer[] &nbsp; &lt;- \u6743\u91cd\u5e38\u91cf TensorProto\uff08\u771f\u6b63\u7684\u6743\u91cd\u5728\u8fd9\uff09<br> &nbsp; &nbsp;  input[] \/ output[]<br>  metadata_props[]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bfb\u53d6\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import onnx<br>from onnx import numpy_helper<br>\u200b<br>m = onnx.load(\"model.onnx\")<br>onnx.checker.check_model(m)<br>\u200b<br>print(\"ir_version:\", m.ir_version, \"| producer:\", m.producer_name)<br>print(\"opset:\", [(o.domain or \"ai.onnx\", o.version) for o in m.opset_import])<br>\u200b<br># \u7b97\u5b50 + \u8fde\u63a5\u5173\u7cfb<br>print(onnx.helper.printable_graph(m.graph))<br>\u200b<br># \u6743\u91cd\u6e05\u5355<br>for t in m.graph.initializer:<br> &nbsp; &nbsp;arr = numpy_helper.to_array(t)<br> &nbsp; &nbsp;print(t.name, arr.shape, arr.dtype)<br>\u200b<br># \u8f93\u5165\u8f93\u51fa\u7b7e\u540d<br>print(\"\u8f93\u5165:\", [i.name for i in m.graph.input])<br>print(\"\u8f93\u51fa:\", [o.name for o in m.graph.output])<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>printable_graph()<\/code> \u7684\u8f93\u51fa\u662f\u6587\u672c\u7248\u8ba1\u7b97\u56fe \u2014\u2014 \u4e0d\u7528\u5f00 netron \u4e5f\u80fd\u770b\u6e05\u7b97\u5b50\u8fde\u63a5\uff0c\u5f62\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">graph demo (%X[FLOAT, 1x3]) initializers (%W[FLOAT, 3x4] %B[FLOAT, 4]) {<br>  %XW = MatMul(%X, %W)<br>  %Y = Add(%XW, %B)<br>  return %Y<br>}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9a8c\u8bc1 \/ \u63a8\u7406<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import onnxruntime as ort<br>sess = ort.InferenceSession(\"model.onnx\")<br>print(\"\u8f93\u5165:\", [(i.name, i.shape) for i in sess.get_inputs()])<br>print(\"\u8f93\u51fa:\", [(o.name, o.shape) for o in sess.get_outputs()])<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.5 safetensors<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">from safetensors import safe_open<br>\u200b<br># \u65b9\u5f0f\u4e00\uff1a\u61d2\u52a0\u8f7d\uff08\u4e0d\u8bfb\u5168\u91cf\uff09<br>with safe_open(\"model.safetensors\", framework=\"pt\", device=\"cpu\") as f:<br> &nbsp; &nbsp;print(list(f.keys()))<br> &nbsp; &nbsp;t = f.get_tensor(\"model.embed_tokens.weight\")<br> &nbsp; &nbsp;print(t.shape, t.dtype)<br>\u200b<br># \u65b9\u5f0f\u4e8c\uff1anumpy \u5168\u91cf<br>from safetensors.numpy import load_file<br>sd = load_file(\"model.safetensors\")<br>\u200b<br># \u65b9\u5f0f\u4e09\uff1a\u624b\u6495\u5934\u90e8\uff08\u7406\u89e3\u683c\u5f0f\uff0c\u4e0d\u4f9d\u8d56\u5e93\uff09<br>import struct, json<br>with open(\"model.safetensors\", \"rb\") as f:<br> &nbsp; &nbsp;n = struct.unpack(\"&lt;Q\", f.read(8))[0]<br> &nbsp; &nbsp;header = json.loads(f.read(n))<br>for k, v in header.items():<br> &nbsp; &nbsp;if k != \"__metadata__\":<br> &nbsp; &nbsp; &nbsp; &nbsp;print(k, v[\"dtype\"], v[\"shape\"], v[\"data_offsets\"])<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.3.6 GGUF<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u6790\u5165\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from gguf import GGUFReader<br>\u200b<br>r = GGUFReader(\"model.gguf\")<br>print(\"\u5b57\u6bb5\u6570:\", len(r.fields), \"| \u5f20\u91cf\u6570:\", len(r.tensors))<br>\u200b<br># \u5143\u6570\u636e\uff08\u67b6\u6784\u3001\u540d\u79f0\u3001\u91cf\u5316\u7248\u672c\u3001\u8d85\u53c2\uff09<br>for k in (\"general.architecture\", \"general.name\", \"general.file_type\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"llama.block_count\", \"llama.embedding_length\", \"tokenizer.ggml.model\"):<br> &nbsp; &nbsp;if k in r.fields:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(k, \"=\", r.fields[k].contents())<br>\u200b<br># \u5f20\u91cf + \u91cf\u5316\u7c7b\u578b\uff08Q4_K \/ Q6_K \/ F32 ...\uff09<br>for t in r.tensors[:5]:<br> &nbsp; &nbsp;print(t.name, list(t.shape), t.tensor_type.name)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53ea\u9760\u5934\u90e8\u3001\u4e0d\u88c5 <code>gguf<\/code> \u5e93\u4e5f\u80fd\u8bfb\u57fa\u672c\u4fe1\u606f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import struct<br>with open(\"model.gguf\", \"rb\") as f:<br> &nbsp; &nbsp;assert f.read(4) == b\"GGUF\"<br> &nbsp; &nbsp;version = struct.unpack(\"&lt;I\", f.read(4))[0]<br> &nbsp; &nbsp;n_tensors = struct.unpack(\"&lt;Q\", f.read(8))[0]<br> &nbsp; &nbsp;n_kv = struct.unpack(\"&lt;Q\", f.read(8))[0]<br>print(version, n_tensors, n_kv)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u91cf\u5316\u547d\u540d\u89c4\u5219<\/strong>\uff1a<code>Q4_K_M<\/code> \u91cc\u7684 <code>Q4<\/code> = 4-bit\uff1b\u540e\u7f00 <code>_K<\/code> \u662f k-quant \u7cfb\u5217\uff0c<code>_M \/ _S \/ _L<\/code> \u8868\u793a medium \/ small \/ large \u6df7\u5408\u7cbe\u5ea6\u7b49\u7ea7\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.4 \u5927\u6a21\u578b\u5206\u7247\u600e\u4e48\u8fd8\u539f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">HF \u683c\u5f0f\u7684\u5927\u6a21\u578b\u5e38\u88ab\u5207\u6210\u591a\u7247\uff0c\u9760 <code>model.safetensors.index.json<\/code> \u7d22\u5f15\u5173\u8054\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fd8\u539f\u601d\u8def<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from collections import defaultdict<br>\u200b<br>idx = json.load(open(\"model.safetensors.index.json\"))<br>wmap = idx[\"weight_map\"]<br>\u200b<br># 1. \u6bcf\u4e2a\u5206\u7247\u8d1f\u8d23\u54ea\u4e9b\u5f20\u91cf<br>per_shard = defaultdict(list)<br>for name, shard in wmap.items():<br> &nbsp; &nbsp;per_shard[shard].append(name)<br>\u200b<br># 2. \u6309\u9700\u53ea\u52a0\u8f7d\u67d0\u4e2a\u5f20\u91cf<br>import struct<br>def load_one(shard, tensor):<br> &nbsp; &nbsp;import numpy as np<br> &nbsp; &nbsp;with open(shard, \"rb\") as f:<br> &nbsp; &nbsp; &nbsp; &nbsp;n = struct.unpack(\"&lt;Q\", f.read(8))[0]<br> &nbsp; &nbsp; &nbsp; &nbsp;header = json.loads(f.read(n))<br> &nbsp; &nbsp; &nbsp; &nbsp;base = 8 + n<br> &nbsp; &nbsp; &nbsp; &nbsp;s, e = header[tensor][\"data_offsets\"]<br> &nbsp; &nbsp; &nbsp; &nbsp;f.seek(base + s)<br> &nbsp; &nbsp; &nbsp; &nbsp;raw = f.read(e - s)<br> &nbsp; &nbsp;shape = header[tensor][\"shape\"]<br> &nbsp; &nbsp;return np.frombuffer(raw, dtype=np.float16).reshape(shape)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>load_one()<\/code> \u5c31\u662f\u96f6\u62f7\u8d1d\u53ea\u8bfb\u4e00\u7247\u7684\u5b9e\u9645\u7528\u6cd5 \u2014\u2014 \u53ea\u60f3\u770b\u67d0\u4e00\u5c42\u6743\u91cd\u65f6\uff0c\u5b8c\u5168\u6ca1\u5fc5\u8981\u628a\u6574\u4efd 15GB \u8bfb\u8fdb\u5185\u5b58\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>GGUF \u5206\u7247<\/strong>\u66f4\u7b80\u5355\uff1a<code>llama-cli -m &lt;\u7b2c\u4e00\u4e2a\u5206\u7247\u8def\u5f84&gt;<\/code>\uff0cllama.cpp \u4f1a\u81ea\u52a8\u5408\u5e76\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u5e38\u89c1ai\u6a21\u578b\u683c\u5f0f 1. GGUF \u6700\u521d\u4e3a llama.cpp \u9879\u76ee\u5f00\u53d1\uff0c\u662f\u4e00\u79cd\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u76ee\u6807\u662f\u5feb\u901f\u52a0\u8f7d\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":1,"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/luqimu.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}