File size: 9,363 Bytes
aca0aa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B3S1AZp6Kc0R"
      },
      "source": [
        "# Installing\n",
        "\n",
        "If running on Google Colab you will need a Colab Pro+ subscription.\n",
        "\n",
        "Change the runtime type to a high memory or connect your local machine to colab, launch a vm perhaps.\n",
        "\n",
        "First, you will need to obtain the LLaMA weights. \n",
        "\n",
        "You can sign up for the official weights here: https://huggingface.co/docs/transformers/main/model_doc/llama\n",
        "\n",
        "There are alternative models available on huggingface however. This guide will assume you do not have access to the official weights.\n",
        "\n",
        "If you do have access to the official weights skip to: Clone the delta weights"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8_MM5GNkKc0T"
      },
      "source": [
        "## Clone the LLaMA weights"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5TioBc7WKc0U"
      },
      "outputs": [],
      "source": [
        "# Setup git lfs\n",
        "!git lfs install --skip-smudge --force\n",
        "!git lfs env\n",
        "!git config filter.lfs.process \"git-lfs filter-process --skip\"\n",
        "!git config filter.lfs.smudge \"git-lfs smudge --skip -- %f\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "B5Q-x5yBKc0V"
      },
      "outputs": [],
      "source": [
        "# Cloning the 7b parameter model repo\n",
        "!git lfs clone https://huggingface.co/decapoda-research/llama-7b-hf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "d0r8HBUyKc0V"
      },
      "outputs": [],
      "source": [
        "# Cloning the 13b parameter model repo\n",
        "!git lfs clone https://huggingface.co/decapoda-research/llama-13b-hf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QeDIT-a4Kc0V"
      },
      "source": [
        "## Applying the Vicuna delta weights"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "p7BGGUICKc0W"
      },
      "source": [
        "### Install PyTorch with CUDA support\n",
        "\n",
        "If you already have this installed in your environment, you can skip this step."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "IfGE0ALrKc0W"
      },
      "outputs": [],
      "source": [
        "# First we need to upgrade setuptools, pip and wheel\n",
        "!pip install --upgrade setuptools pip wheel"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RpvAYAD8Kc0W"
      },
      "outputs": [],
      "source": [
        "# For CUDA 11.X:\n",
        "!pip install nvidia-cuda-runtime-cu11 --index-url https://pypi.ngc.nvidia.com"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1oEi1ggaKc0X"
      },
      "outputs": [],
      "source": [
        "# For CUDA 12.x\n",
        "!pip install nvidia-cuda-runtime-cu12 --index-url https://pypi.ngc.nvidia.com"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "fQYYrn4tKc0X"
      },
      "outputs": [],
      "source": [
        "# For PyTorch cu117\n",
        "!pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "G7lnb0sYKc0X"
      },
      "outputs": [],
      "source": [
        "# For PyTorch cu118\n",
        "!pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oBVyjHuWKc0Y"
      },
      "source": [
        "### Running the Fast-Chat apply delta script"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "a4b11Z8lKc0Y"
      },
      "outputs": [],
      "source": [
        "# Install FastChat\n",
        "!pip install fschat\n",
        "\n",
        "# Install the latest main branch of huggingface/transformers\n",
        "!pip install git+https://github.com/huggingface/transformers"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3vJTP1m0Kc0Y"
      },
      "outputs": [],
      "source": [
        "import json\n",
        "\n",
        "def convert_llama_names(model_name: str) -> None:\n",
        "    \"\"\"Convert LlamaForCausalLM to LlamaForCausalLM and LLaMATokenizer to LlamaTokenizer\"\"\"\n",
        "    with open(f\"{model_name}/config.json\", \"r\", encoding='utf-8') as f:\n",
        "        data = f.read()\n",
        "\n",
        "    config = json.loads(data)\n",
        "    config[\"architectures\"] = [\"LlamaForCausalLM\"]\n",
        "    with open(f\"{model_name}/config.json\", \"w\", encoding='utf-8') as f:\n",
        "        json.dump(config, f)\n",
        "\n",
        "\n",
        "    with open(f\"{model_name}/tokenizer_config.json\", \"r\", encoding='utf-8') as f:\n",
        "        data = f.read()\n",
        "\n",
        "    config = json.loads(data)\n",
        "    config[\"tokenizer_class\"] = \"LlamaTokenizer\"\n",
        "\n",
        "    with open(f\"{model_name}/tokenizer_config.json\", \"w\", encoding='utf-8') as f:\n",
        "        json.dump(config, f)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qyboxxkXKc0Y"
      },
      "outputs": [],
      "source": [
        "!git lfs clone https://huggingface.co/lmsys/vicuna-7b-delta-v1.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9tlvfvapKc0Z"
      },
      "outputs": [],
      "source": [
        "# 7b Model\n",
        "convert_llama_names(\"llama-7b-hf\")\n",
        "!python -m fastchat.model.apply_delta --base llama-7b-hf --target vicuna-7b --delta ./vicuna-7b-delta-v1.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5VzAdoySKc0Z"
      },
      "outputs": [],
      "source": [
        "!git lfs clone https://huggingface.co/lmsys/vicuna-13b-delta-v1.1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "jRBc7q5RKc0Z"
      },
      "outputs": [],
      "source": [
        "# 13b\n",
        "convert_llama_names(\"llama-13b-hf\")\n",
        "!python -m fastchat.model.apply_delta --base llama-13b-hf --target vicuna-13b --delta ./vicuna-13b-delta-v1.1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7n8Ug87RKc0Z"
      },
      "source": [
        "# Installing Auto-Vicuna\n",
        "\n",
        "Note that running this does not work in colab or the notebook, it is for demonstration purposes only."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gIlfwNlEKc0Z"
      },
      "outputs": [],
      "source": [
        "!pip install auto-vicuna"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0Z5NlQOXKc0Z"
      },
      "source": [
        "# Running Auto-Vicuna"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "sBJiQP7MKc0a"
      },
      "outputs": [],
      "source": [
        "!auto_vicuna --vicuna_weights vicuna-7b"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oqqFeF4zKc0a"
      },
      "source": [
        "You can also create a .env file with \n",
        "\n",
        "```\n",
        "VICUNA_WEIGHTS=vicuna-7b\n",
        "```\n",
        "\n",
        "To avoid passing the weights as an arugment."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jiW98C1KKc0a"
      },
      "source": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VE8DhmUrKc0a"
      },
      "source": [
        "## Known Issues\n",
        "\n",
        "If your model keeps talking about random news articles and suchs the `special_tokens_map.json` and `tokenizer_config.json` need to have to stop tokens populated most likely, you can find them in the repo's root dir."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".venv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.13"
    },
    "orig_nbformat": 4,
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}