-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathllama.py
More file actions
452 lines (399 loc) · 13.4 KB
/
Copy pathllama.py
File metadata and controls
452 lines (399 loc) · 13.4 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Copyright The Marin Authors
# SPDX-License-Identifier: Apache-2.0
"""
Specifies a sequence of Llama 3 models from small to large.
"""
from fray.cluster import ResourceConfig
from levanter.data.text import ChatLmDatasetFormat
from levanter.layers.rotary import Llama3RotaryEmbeddingsConfig
from levanter.models.llama import LlamaConfig
from levanter.utils.activation import ActivationFunctionEnum
from experiments.simple_train_config import SimpleTrainConfig
llama3_tokenizer = "meta-llama/Meta-Llama-3.1-8B"
llama3_tokenizer_vocab_size = 128_256
llama3_instruct_tokenizer = "meta-llama/Meta-Llama-3.1-8B-Instruct"
# Llama 3 chat stop token IDs for generation_config.json.
# The chat template ends every turn (user, assistant, system) with <|eot_id|> (128009),
# but the tokenizer's eos_token is <|end_of_text|> (128001), which is the pre-training
# document boundary. Both must be listed as stop tokens so vLLM stops on either.
# Determined by running: tokenizer.apply_chat_template([...], tokenize=True)
# and observing the last token of the assistant turn is 128009.
LLAMA3_CHAT_STOP_TOKEN_IDS = [128001, 128009]
# Llama3 instruct trainable chat template
# Slight modification of https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct/blob/main/tokenizer_config.json
# to add {% generation %} so we can create the assistant_mask
llama3_instruct_trainable_chat_template = """{{- bos_token }}
{%- if custom_tools is defined %}
{%- set tools = custom_tools %}
{%- endif %}
{%- if not tools_in_user_message is defined %}
{%- set tools_in_user_message = true %}
{%- endif %}
{%- if not date_string is defined %}
{%- set date_string = "26 Jul 2024" %}
{%- endif %}
{%- if not tools is defined %}
{%- set tools = none %}
{%- endif %}
{#- This block extracts the system message, so we can slot it into the right place. #}
{%- if messages[0]['role'] == 'system' %}
{%- set system_message = messages[0]['content']|trim %}
{%- set messages = messages[1:] %}
{%- else %}
{%- set system_message = "" %}
{%- endif %}
{#- System message + builtin tools #}
{{- "<|start_header_id|>system<|end_header_id|>\\n\\n" }}
{%- if builtin_tools is defined or tools is not none %}
{{- "Environment: ipython\\n" }}
{%- endif %}
{%- if builtin_tools is defined %}
{{- "Tools: " + builtin_tools | reject('equalto', 'code_interpreter') | join(", ") + "\\n\\n"}}
{%- endif %}
{{- "Cutting Knowledge Date: December 2023\\n" }}
{{- "Today Date: " + date_string + "\\n\\n" }}
{%- if tools is not none and not tools_in_user_message %}
{{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
{{- "Do not use variables.\\n\\n" }}
{%- for t in tools %}
{{- t | tojson(indent=4) }}
{{- "\\n\\n" }}
{%- endfor %}
{%- endif %}
{{- system_message }}
{{- "<|eot_id|>" }}
{#- Custom tools are passed in a user message with some extra guidance #}
{%- if tools_in_user_message and not tools is none %}
{#- Extract the first user message so we can plug it in here #}
{%- if messages | length != 0 %}
{%- set first_user_message = messages[0]['content']|trim %}
{%- set messages = messages[1:] %}
{%- else %}
{{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
{%- endif %}
{{- '<|start_header_id|>user<|end_header_id|}\\n\\n' -}}
{{- "Given the following functions, please respond with a JSON for a function call " }}
{{- "with its proper arguments that best answers the given prompt.\\n\\n" }}
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
{{- "Do not use variables.\\n\\n" }}
{%- for t in tools %}
{{- t | tojson(indent=4) }}
{{- "\\n\\n" }}
{%- endfor %}
{{- first_user_message + "<|eot_id|>" }}
{%- endif %}
{%- for message in messages %}
{%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
{%- if message.role == "assistant" %}
{{- "<|start_header_id|>assistant<|end_header_id|>\\n\\n" }}
{%- generation %}
{{- message['content'] | trim }}
{%- endgeneration %}
{{- "<|eot_id|>" }}
{%- else %}
{{- "<|start_header_id|>" + message['role'] + "<|end_header_id|>\\n\\n" + message['content'] | trim + "<|eot_id|>" }}
{%- endif %}
{%- elif 'tool_calls' in message %}
{%- if not message.tool_calls|length == 1 %}
{{- raise_exception("This model only supports single tool-calls at once!") }}
{%- endif %}
{%- set tool_call = message.tool_calls[0].function %}
{%- if builtin_tools is defined and tool_call.name in builtin_tools %}
{{- "<|start_header_id|>assistant<|end_header_id|>\\n\\n" -}}
{{- "<|python_tag|>" + tool_call.name + ".call(" }}
{%- for arg_name, arg_val in tool_call.arguments | items %}
{{- arg_name + '="' + arg_val + '"' }}
{%- if not loop.last %}
{{- ", " }}
{%- endif %}
{%- endfor %}
{{- ")" }}
{%- else %}
{{- "<|start_header_id|>assistant<|end_header_id|>\\n\\n" -}}
{{- '{"name": "' + tool_call.name + '", ' }}
{{- '"parameters": ' }}
{{- tool_call.arguments | tojson }}
{{- "}" }}
{%- endif %}
{%- if builtin_tools is defined %}
{{- "<|eom_id|>" }}
{%- else %}
{{- "<|eot_id|>" }}
{%- endif %}
{%- elif message.role == "tool" or message.role == "ipython" %}
{{- "<|start_header_id|>ipython<|end_header_id|>\\n\\n" }}
{%- if message.content is mapping or message.content is iterable %}
{{- message.content | tojson }}
{%- else %}
{{- message.content }}
{%- endif %}
{{- "<|eot_id|>" }}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- "<|start_header_id|>assistant<|end_header_id|>\\n\\n" }}
{%- endif %}""" # noqa: E501
# Chat format compatible with the llama3 instruct tokenizer and Levanter's chat format
llama3_instruct_chat_format = ChatLmDatasetFormat(
messages_field="messages",
chat_template=llama3_instruct_trainable_chat_template,
pack=True,
mask_user_turns=True,
)
llama_nano = LlamaConfig(
max_seq_len=512,
hidden_dim=32,
intermediate_dim=128,
num_heads=2,
num_kv_heads=2,
num_layers=2,
)
llama_30m = LlamaConfig(
max_seq_len=1024,
hidden_dim=128,
intermediate_dim=448,
num_heads=2,
num_kv_heads=2,
num_layers=4,
)
llama_50m = LlamaConfig(
max_seq_len=1024,
hidden_dim=192,
intermediate_dim=448,
num_heads=2,
num_kv_heads=2,
num_layers=4,
)
llama_75m = LlamaConfig(
max_seq_len=1024,
hidden_dim=256,
intermediate_dim=896,
num_heads=4,
num_kv_heads=4,
num_layers=8,
)
llama_150m = LlamaConfig(
max_seq_len=1024,
hidden_dim=512,
intermediate_dim=1792,
num_heads=8,
num_kv_heads=8,
num_layers=6,
)
llama_300m = LlamaConfig(
max_seq_len=1024,
hidden_dim=768,
intermediate_dim=2688,
num_heads=12,
num_kv_heads=12,
num_layers=12,
)
llama_600m = LlamaConfig(
max_seq_len=1024,
hidden_dim=1024,
intermediate_dim=3584,
num_heads=16,
num_kv_heads=8,
num_layers=24,
)
llama_1_4b = LlamaConfig(
max_seq_len=4096,
hidden_dim=2048,
intermediate_dim=7168,
num_heads=16,
num_kv_heads=8,
num_layers=16,
)
llama_1_9b = LlamaConfig(
max_seq_len=4096,
hidden_dim=2048,
intermediate_dim=7168,
num_heads=16,
num_kv_heads=8,
num_layers=24,
)
# Llama-3.2-1B
llama_3_2_1b = LlamaConfig(
max_seq_len=4096,
hidden_dim=2048,
intermediate_dim=8192,
num_heads=32,
num_kv_heads=8,
num_layers=16,
rope=Llama3RotaryEmbeddingsConfig(),
tie_word_embeddings=True,
)
# Llama-3.1-8B
llama_3_1_8b_tokenizer = "meta-llama/Llama-3.1-8B"
llama_3_1_8b = LlamaConfig(
# Matching defaults in https://huggingface.co/meta-llama/Llama-3.1-8B/blob/main/config.json
max_seq_len=4096,
hidden_dim=4096,
intermediate_dim=14336,
num_heads=32,
num_kv_heads=8,
num_layers=32,
activation_function=ActivationFunctionEnum.silu,
initializer_range=0.02,
layer_norm_epsilon=1e-5,
reference_checkpoint="meta-llama/Llama-3.1-8B",
rope=Llama3RotaryEmbeddingsConfig(
theta=500000.0,
factor=8.0,
high_freq_factor=4.0,
low_freq_factor=1.0,
original_max_position_embeddings=8192,
),
tie_word_embeddings=False,
)
# Llama-3.1-8B-Instruct
llama_3_1_8b_instruct_tokenizer = (
"meta-llama/Llama-3.1-8B-Instruct" # NOTE: Instruct and base eos_token_id values are different
)
llama_3_1_8b_instruct = LlamaConfig(
# Matching defaults in https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/blob/main/config.json
max_seq_len=4096,
hidden_dim=4096,
intermediate_dim=14336,
num_heads=32,
num_kv_heads=8,
num_layers=32,
activation_function=ActivationFunctionEnum.silu,
initializer_range=0.02,
layer_norm_epsilon=1e-5,
reference_checkpoint="meta-llama/Llama-3.1-8B-Instruct",
rope=Llama3RotaryEmbeddingsConfig(
theta=500000.0,
factor=8.0,
high_freq_factor=4.0,
low_freq_factor=1.0,
original_max_position_embeddings=8192,
),
tie_word_embeddings=False,
)
llama_3_5b = LlamaConfig(
max_seq_len=4096,
hidden_dim=2560,
intermediate_dim=8960,
num_heads=20,
num_kv_heads=10,
num_layers=32,
)
llama_8b = LlamaConfig(
max_seq_len=4096,
hidden_dim=4096,
intermediate_dim=14336,
num_heads=32,
num_kv_heads=8,
num_layers=32,
rope=Llama3RotaryEmbeddingsConfig(),
)
llama_8b_old_rotary = LlamaConfig(
max_seq_len=4096,
hidden_dim=4096,
intermediate_dim=14336,
num_heads=32,
num_kv_heads=8,
num_layers=32,
# Levanter defaults to Llama2 rotary
# rope=Llama3RotaryEmbeddingsConfig(),
)
llama_13b = LlamaConfig(
max_seq_len=4096,
hidden_dim=5120,
intermediate_dim=13824,
num_heads=40,
num_kv_heads=8,
num_layers=40,
rope=Llama3RotaryEmbeddingsConfig(),
)
# With Llama 3 tokenizer, this is 24B
llama_24b = LlamaConfig(
max_seq_len=4096,
hidden_dim=6144,
intermediate_dim=16384,
num_heads=48,
num_kv_heads=16,
num_layers=56,
rope=Llama3RotaryEmbeddingsConfig(),
)
# same as olmo 32b
llama_32b = LlamaConfig(
max_seq_len=4096,
hidden_dim=5120,
intermediate_dim=27648,
num_heads=40,
num_kv_heads=8,
num_layers=64,
rope=Llama3RotaryEmbeddingsConfig(),
)
llama_56b = LlamaConfig(
max_seq_len=4096,
hidden_dim=8192,
intermediate_dim=28672,
num_heads=64,
num_kv_heads=8,
num_layers=64,
rope=Llama3RotaryEmbeddingsConfig(),
)
llama_70b = LlamaConfig(
max_seq_len=4096,
hidden_dim=8192,
intermediate_dim=28672,
num_heads=64,
num_kv_heads=8,
num_layers=80,
rope=Llama3RotaryEmbeddingsConfig(),
)
llama_150m_train_config = SimpleTrainConfig(
resources=ResourceConfig.with_tpu("v4-32"),
train_batch_size=512,
num_train_steps=20000, # 1024 * 1024 * 20000 = 20B tokens
learning_rate=3e-3,
weight_decay=0.1,
)
# (18B is way overtrained, but...)
llama_300m_train_config = SimpleTrainConfig(
resources=ResourceConfig.with_tpu("v4-64"),
train_batch_size=1024,
num_train_steps=18000, # 1024 * 1024 * 18000 = 18B tokens
learning_rate=3e-3,
weight_decay=0.1,
)
# (18B is way overtrained, but...)
llama_1_4b_train_config = SimpleTrainConfig(
resources=ResourceConfig.with_tpu("v4-128"),
train_batch_size=1024,
num_train_steps=10000, # 4096 * 1024 * 10000 = 42B tokens
learning_rate=3e-4,
weight_decay=0.1,
)
llama_8b_train_config = SimpleTrainConfig(
resources=ResourceConfig.with_tpu("v4-128", slice_count=4),
train_batch_size=1024,
num_train_steps=40000, # 4096 * 1024 * 40000 = 167B tokens
# these hypers from Table 12 in https://arxiv.org/html/2406.11794v1#A6
learning_rate=2e-3,
weight_decay=0.05,
)
def compute_num_parameters(config: LlamaConfig, vocab_size: int) -> int:
head_size = config.hidden_dim // config.num_heads
q_params = config.num_heads * head_size * config.hidden_dim
k_params = config.num_kv_heads * head_size * config.hidden_dim
v_params = config.num_kv_heads * head_size * config.hidden_dim
o_params = config.num_heads * head_size * config.hidden_dim
attention_params = q_params + k_params + v_params + o_params
layer_norm_params = 2 * config.hidden_dim
gate_params = config.hidden_dim * config.intermediate_dim
up_params = config.hidden_dim * config.intermediate_dim
down_params = config.intermediate_dim * config.hidden_dim
mlp_params = gate_params + up_params + down_params
nonembedding_params = config.num_layers * (attention_params + mlp_params + layer_norm_params)
embedding_params = 2 * vocab_size * config.hidden_dim
return nonembedding_params + embedding_params
# For scaling laws
scaling_llamas = [llama_30m, llama_50m, llama_150m, llama_300m, llama_600m, llama_1_4b, llama_1_9b, llama_3_5b, llama_8b]
if __name__ == "__main__":
for llama in scaling_llamas:
print(f"{compute_num_parameters(llama, llama3_tokenizer_vocab_size):,}")