Upload Export Recipe.ipynb
Browse files- Export Recipe.ipynb +191 -0
Export Recipe.ipynb
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "Z97NsWqYepIS"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"## **Step 1: Setting Up ExecuTorch**\n",
|
10 |
+
"\n",
|
11 |
+
"* If using a Google colab notebook then please get a High-RAM instance to run this notebook.\n"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": null,
|
17 |
+
"metadata": {
|
18 |
+
"collapsed": true,
|
19 |
+
"id": "cAsZThj3dFo7"
|
20 |
+
},
|
21 |
+
"outputs": [],
|
22 |
+
"source": [
|
23 |
+
"! touch /content/executorch; rm -rf /content/executorch\n",
|
24 |
+
"! git clone https://github.com/pytorch/executorch ; cd /content/executorch; git submodule sync ; git submodule update --init"
|
25 |
+
]
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"cell_type": "code",
|
29 |
+
"execution_count": 2,
|
30 |
+
"metadata": {
|
31 |
+
"id": "J58Rbuptspfj"
|
32 |
+
},
|
33 |
+
"outputs": [],
|
34 |
+
"source": [
|
35 |
+
"import sys\n",
|
36 |
+
"# This is a workaround for now\n",
|
37 |
+
"!mkdir -p /usr/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages/torchgen/packaged/ATen/native/\n",
|
38 |
+
"!cp /usr/local/lib/python{sys.version_info.major}.{sys.version_info.minor}/dist-packages/torchgen/packaged/ATen/native/* /usr/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages/torchgen/packaged/ATen/native/"
|
39 |
+
]
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"cell_type": "code",
|
43 |
+
"execution_count": null,
|
44 |
+
"metadata": {
|
45 |
+
"id": "N1WgIQyqd5ra"
|
46 |
+
},
|
47 |
+
"outputs": [],
|
48 |
+
"source": [
|
49 |
+
"import sysconfig; lib_path = sysconfig.get_paths()[\"purelib\"]\n",
|
50 |
+
"! cd /content/executorch; CMAKE_PREFIX_PATH={lib_path} EXECUTORCH_BUILD_XNNPACK=ON bash ./install_executorch.sh"
|
51 |
+
]
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"execution_count": null,
|
56 |
+
"metadata": {
|
57 |
+
"id": "xEcuuYbIfE3L"
|
58 |
+
},
|
59 |
+
"outputs": [],
|
60 |
+
"source": [
|
61 |
+
"!cd /content/executorch; examples/models/llama/install_requirements.sh"
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "markdown",
|
66 |
+
"metadata": {
|
67 |
+
"id": "UX-ZS052fP6D"
|
68 |
+
},
|
69 |
+
"source": [
|
70 |
+
"## **Step 2. Download DeepSeek-R1-Distill-Llama-8B models**"
|
71 |
+
]
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"cell_type": "markdown",
|
75 |
+
"metadata": {
|
76 |
+
"id": "tWK81UfzlxLr"
|
77 |
+
},
|
78 |
+
"source": []
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"cell_type": "code",
|
82 |
+
"execution_count": null,
|
83 |
+
"metadata": {
|
84 |
+
"id": "BsL1yxiUfi01"
|
85 |
+
},
|
86 |
+
"outputs": [],
|
87 |
+
"source": [
|
88 |
+
"!huggingface-cli download deepseek-ai/DeepSeek-R1-Distill-Llama-8B --local-dir /content/models/DeepSeek-R1-Distill-Llama-8B --local-dir-use-symlinks False"
|
89 |
+
]
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"cell_type": "markdown",
|
93 |
+
"metadata": {
|
94 |
+
"id": "6QE-6XPWr4j9"
|
95 |
+
},
|
96 |
+
"source": [
|
97 |
+
"## **Step 3: Export to ExecuTorch**"
|
98 |
+
]
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"cell_type": "code",
|
102 |
+
"source": [
|
103 |
+
"!pip install torchtune"
|
104 |
+
],
|
105 |
+
"metadata": {
|
106 |
+
"id": "YrQLa1ST-uCP"
|
107 |
+
},
|
108 |
+
"execution_count": null,
|
109 |
+
"outputs": []
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"cell_type": "code",
|
113 |
+
"source": [
|
114 |
+
"from torchtune.models import convert_weights\n",
|
115 |
+
"from torchtune.training import FullModelHFCheckpointer\n",
|
116 |
+
"import torch\n",
|
117 |
+
"\n",
|
118 |
+
"# Convert from safetensors to TorchTune. Suppose the model has been downloaded from Hugging Face\n",
|
119 |
+
"checkpointer = FullModelHFCheckpointer(\n",
|
120 |
+
" checkpoint_dir='/content/models/DeepSeek-R1-Distill-Llama-8B',\n",
|
121 |
+
" checkpoint_files=['model-00001-of-000002.safetensors', 'model-00002-of-000002.safetensors'],\n",
|
122 |
+
" output_dir='/tmp/deepseek-ai/DeepSeek-R1-Distill-Llama-8B/' ,\n",
|
123 |
+
" model_type='LLAMA3' # or other types that TorchTune supports\n",
|
124 |
+
")\n",
|
125 |
+
"\n",
|
126 |
+
"print(\"loading checkpoint\")\n",
|
127 |
+
"sd = checkpointer.load_checkpoint()\n",
|
128 |
+
"\n",
|
129 |
+
"# Convert from TorchTune to Meta (PyTorch native)\n",
|
130 |
+
"sd = convert_weights.tune_to_meta(sd['model'])\n",
|
131 |
+
"\n",
|
132 |
+
"print(\"saving checkpoint\")\n",
|
133 |
+
"torch.save(sd, \"/tmp/deepseek-ai/DeepSeek-R1-Distill-Llama-8B/checkpoint.pth\")"
|
134 |
+
],
|
135 |
+
"metadata": {
|
136 |
+
"id": "Zphh3FVu-2Wa"
|
137 |
+
},
|
138 |
+
"execution_count": null,
|
139 |
+
"outputs": []
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"cell_type": "code",
|
143 |
+
"source": [
|
144 |
+
"# Download https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/blob/main/original/params.json and place it in /tmp/params.json"
|
145 |
+
],
|
146 |
+
"metadata": {
|
147 |
+
"id": "UBWLe4Gu_OTK"
|
148 |
+
},
|
149 |
+
"execution_count": null,
|
150 |
+
"outputs": []
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"cell_type": "code",
|
154 |
+
"execution_count": null,
|
155 |
+
"metadata": {
|
156 |
+
"collapsed": true,
|
157 |
+
"id": "hGkyrU5lnNop"
|
158 |
+
},
|
159 |
+
"outputs": [],
|
160 |
+
"source": [
|
161 |
+
"!cd /content/executorch; python -m examples.models.llama.export_llama \\\n",
|
162 |
+
" --checkpoint /tmp/deepseek-ai/DeepSeek-R1-Distill-Llama-8B/checkpoint.pth \\\n",
|
163 |
+
"\t-p /tmp/params.json \\\n",
|
164 |
+
"\t-kv \\\n",
|
165 |
+
"\t--use_sdpa_with_kv_cache \\\n",
|
166 |
+
"\t-X \\\n",
|
167 |
+
"\t-qmode 8da4w \\\n",
|
168 |
+
"\t--group_size 128 \\\n",
|
169 |
+
"\t-d fp16 \\\n",
|
170 |
+
"\t--metadata '{\"get_bos_id\":128000, \"get_eos_ids\":[128009, 128001]}' \\\n",
|
171 |
+
"\t--embedding-quantize 4,32 \\\n",
|
172 |
+
"\t--output_name=\"DeepSeek-R1-Distill-Llama-8B.pte\""
|
173 |
+
]
|
174 |
+
}
|
175 |
+
],
|
176 |
+
"metadata": {
|
177 |
+
"colab": {
|
178 |
+
"provenance": [],
|
179 |
+
"machine_shape": "hm"
|
180 |
+
},
|
181 |
+
"kernelspec": {
|
182 |
+
"display_name": "Python 3",
|
183 |
+
"name": "python3"
|
184 |
+
},
|
185 |
+
"language_info": {
|
186 |
+
"name": "python"
|
187 |
+
}
|
188 |
+
},
|
189 |
+
"nbformat": 4,
|
190 |
+
"nbformat_minor": 0
|
191 |
+
}
|