-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathllama_model.py
More file actions
571 lines (513 loc) · 23.3 KB
/
Copy pathllama_model.py
File metadata and controls
571 lines (513 loc) · 23.3 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
"""
Implementation for Llama2 architecture.
"""
import dataclasses
from typing import Any, Dict, Optional # noqa: UP035
from tvm import tirx
from tvm.relax.frontend import nn
from tvm.relax.frontend.nn import Tensor, op
from mlc_llm import op as op_ext
from mlc_llm.model.model_utils import index_last_token
from mlc_llm.nn import PagedKVCache, RopeMode
from mlc_llm.support import logging
from mlc_llm.support import tensor_parallel as tp
from mlc_llm.support.config import ConfigBase
from mlc_llm.support.style import bold
logger = logging.getLogger(__name__)
@dataclasses.dataclass
class LlamaConfig(ConfigBase):
"""Configuration of the Llama model."""
hidden_size: int
intermediate_size: int
num_attention_heads: int
num_hidden_layers: int
rms_norm_eps: float
vocab_size: int
tie_word_embeddings: bool = False
position_embedding_base: int = 0
rope_scaling: Optional[Dict[str, Any]] = None # noqa: UP006
context_window_size: int = 0
prefill_chunk_size: int = 0
num_key_value_heads: int = 0
head_dim: int = 0
attention_bias: bool = False
mlp_bias: bool = False
tensor_parallel_shards: int = 1
pipeline_parallel_stages: int = 1
max_batch_size: int = 1
disaggregation: bool = False
kwargs: Dict[str, Any] = dataclasses.field(default_factory=dict) # noqa: UP006
def __post_init__(self):
if self.position_embedding_base == 0:
if "rope_theta" in self.kwargs:
self.position_embedding_base = self.kwargs.pop("rope_theta")
else:
self.position_embedding_base = 10000
if self.rope_scaling is not None:
if "rope_type" not in self.rope_scaling:
self.rope_scaling = None
else:
assert self.rope_scaling["rope_type"] == "llama3", (
f"Unsupported RoPE scaling type {self.rope_scaling['rope_type']} for Llama"
)
if self.context_window_size == 0:
for name in ["max_position_embeddings", "max_sequence_length"]:
if name in self.kwargs:
self.context_window_size = self.kwargs.pop(name)
logger.info(
"%s not found in config.json. Falling back to %s (%d)",
bold("context_window_size"),
bold(name),
self.context_window_size,
)
break
else:
raise ValueError(
"Unable to determine the maximum sequence length, because none of "
"`context_window_size`, `max_position_embeddings` or `max_sequence_length` is "
"provided in `config.json`."
)
if (
self.pipeline_parallel_stages <= 0
or self.pipeline_parallel_stages > self.num_hidden_layers
):
raise ValueError(
f'Invalid "pipeline_parallel_stages" value ({self.pipeline_parallel_stages}). '
)
if self.num_key_value_heads == 0:
self.num_key_value_heads = self.num_attention_heads
if self.head_dim == 0:
self.head_dim = self.hidden_size // self.num_attention_heads
assert self.num_attention_heads % self.num_key_value_heads == 0
if self.prefill_chunk_size == 0:
logger.info(
"%s defaults to %d",
bold("prefill_chunk_size"),
min(self.context_window_size, 8192),
)
self.prefill_chunk_size = min(self.context_window_size, 8192)
elif self.prefill_chunk_size > self.context_window_size:
logger.info(
"Overriding %s from %d to %d",
bold("prefill_chunk_size"),
self.prefill_chunk_size,
min(self.context_window_size, 8192),
)
self.prefill_chunk_size = min(self.context_window_size, 8192)
class LlamaFFN(nn.Module):
def __init__(self, config: LlamaConfig):
super().__init__()
if config.intermediate_size % config.tensor_parallel_shards != 0:
raise ValueError(
f"Cannot split MLP intermediate size {config.intermediate_size} "
f"evenly to {config.tensor_parallel_shards} GPUs."
)
self.intermediate_size = config.intermediate_size // config.tensor_parallel_shards
self.gate_up_proj = nn.Linear(
in_features=config.hidden_size,
out_features=2 * self.intermediate_size,
bias=config.mlp_bias,
)
self.down_proj = nn.Linear(
self.intermediate_size, config.hidden_size, bias=config.mlp_bias
)
def forward(self, x: Tensor):
concat_x1_x2 = self.gate_up_proj(x)
x1, x2 = op.split(concat_x1_x2, 2, axis=-1)
return self.down_proj(op.silu(x1) * x2)
class LlamaEmbedding(nn.Embedding):
"""The embedding module that can be shared with the final lm_head. From Qwen2Embedding."""
def lm_head_forward(self, x: nn.Tensor):
"""The lm_head forwarding, which transposes the weight and multiplies
with the input tensor.
"""
weight = nn.op.permute_dims(self.weight)
return nn.op.matmul(x, weight, out_dtype="float32")
class LlamaAttention(nn.Module):
def __init__(self, config: LlamaConfig):
self.head_dim = config.head_dim
self.num_q_heads = config.num_attention_heads // config.tensor_parallel_shards
assert config.num_key_value_heads % config.tensor_parallel_shards == 0, (
f"num_kv_heads({config.num_key_value_heads}) must be divisible by tensor_parallel_shards" # noqa: E501
)
assert config.num_key_value_heads >= config.tensor_parallel_shards, (
f"Too large tensor_parallel_shards, must be smaller than {config.num_key_value_heads}"
)
self.num_kv_heads = config.num_key_value_heads // config.tensor_parallel_shards
self.qkv_proj = nn.Linear(
in_features=config.hidden_size,
out_features=(self.num_q_heads + 2 * self.num_kv_heads) * self.head_dim,
bias=config.attention_bias,
)
self.o_proj = nn.Linear(
self.num_q_heads * self.head_dim, config.hidden_size, bias=config.attention_bias
)
def forward(self, hidden_states: Tensor, paged_kv_cache: PagedKVCache, layer_id: int):
d, h_q, h_kv = self.head_dim, self.num_q_heads, self.num_kv_heads
b, s, _ = hidden_states.shape
# QKV Projection
qkv = self.qkv_proj(hidden_states)
qkv = op.reshape(qkv, (b, s, h_q + h_kv + h_kv, d))
# Attention
output = op.reshape(
paged_kv_cache.attention_with_fused_qkv(
layer_id, qkv, self.num_q_heads, sm_scale=self.head_dim**-0.5
),
(b, s, h_q * d),
)
return self.o_proj(output)
class LlamaDecoderLayer(nn.Module):
def __init__(self, config: LlamaConfig):
rms_norm_eps = config.rms_norm_eps
self.self_attn = LlamaAttention(config)
self.mlp = LlamaFFN(config)
self.input_layernorm = nn.RMSNorm(config.hidden_size, -1, rms_norm_eps, bias=False)
self.post_attention_layernorm = nn.RMSNorm(config.hidden_size, -1, rms_norm_eps, bias=False)
def _set_tp():
def _set(layer, hint):
layer.weight.attrs["shard_strategy"] = hint
def _set_bias(layer, hint):
# Column-parallel layers (qkv_proj, gate_up_proj) shard their bias along the
# same dim as the weight's output dim. Row-parallel layers (o_proj, down_proj)
# keep a full (unsharded) bias and instead divide it by the shard count at
# forward time via tp.shard_bias, so the subsequent allreduce(sum) restores
# the original value — they don't get a shard_strategy here.
layer.bias.attrs["shard_strategy"] = hint
hd = config.head_dim
q = self.self_attn.num_q_heads * hd
k = self.self_attn.num_kv_heads * hd
v = self.self_attn.num_kv_heads * hd
i = self.mlp.intermediate_size
_set(
self.self_attn.qkv_proj,
tp.ShardSingleDim("_shard_qkv", segs=[q, k, v], dim=0),
)
_set(self.self_attn.o_proj, tp.ShardSingleDim("_shard_o", dim=1))
_set(
self.mlp.gate_up_proj,
tp.ShardSingleDim("_shard_mlp_up", segs=[i, i], dim=0),
)
_set(self.mlp.down_proj, tp.ShardSingleDim("_shard_mlp_down", dim=1))
if config.attention_bias:
_set_bias(
self.self_attn.qkv_proj,
tp.ShardSingleDim("_shard_qkv_bias", segs=[q, k, v], dim=0),
)
if config.mlp_bias:
_set_bias(
self.mlp.gate_up_proj,
tp.ShardSingleDim("_shard_mlp_up_bias", segs=[i, i], dim=0),
)
self.tensor_parallel_shards = config.tensor_parallel_shards
_set_tp()
def forward(self, hidden_states: Tensor, paged_kv_cache: PagedKVCache, layer_id: int):
# o_proj and down_proj are row-parallel: their bias must be divided by the shard
# count so the following ccl_allreduce(sum) in _apply_residual restores it (no-op
# when there is no bias or on a single device).
with tp.shard_bias(self.self_attn.o_proj, self.tensor_parallel_shards):
out = self.self_attn(self.input_layernorm(hidden_states), paged_kv_cache, layer_id)
hidden_states = self._apply_residual(out, residual=hidden_states)
with tp.shard_bias(self.mlp.down_proj, self.tensor_parallel_shards):
out = self.mlp(self.post_attention_layernorm(hidden_states))
hidden_states = self._apply_residual(out, residual=hidden_states)
return hidden_states
def _apply_residual(self, out, residual):
if self.tensor_parallel_shards > 1:
return op.ccl_allreduce(out, "sum") + residual
return out + residual
class LlamaModel(nn.Module):
def __init__(self, config: LlamaConfig):
assert config.hidden_size % config.num_attention_heads == 0
self.embed_tokens = LlamaEmbedding("vocab_size", config.hidden_size)
self.layers = nn.ModuleList(
[LlamaDecoderLayer(config) for _ in range(config.num_hidden_layers)]
)
self.norm = nn.RMSNorm(config.hidden_size, -1, config.rms_norm_eps, bias=False)
self.num_layers_per_stage = (
config.num_hidden_layers + config.pipeline_parallel_stages - 1
) // config.pipeline_parallel_stages
# Compute pipeline layer partition.
layers_per_stage = (
config.num_hidden_layers + config.pipeline_parallel_stages - 1
) // config.pipeline_parallel_stages
self.layer_partition = [
i * layers_per_stage for i in range(config.pipeline_parallel_stages)
] + [config.num_hidden_layers]
def forward(self, input_embed: Tensor, paged_kv_cache: PagedKVCache):
hidden_states = input_embed
for layer_id, layer in enumerate(self.layers):
if layer_id != 0 and layer_id in self.layer_partition:
hidden_states = op_ext.pipeline_stage_boundary(hidden_states)
hidden_states = layer(hidden_states, paged_kv_cache, layer_id)
hidden_states = self.norm(hidden_states)
return hidden_states
class LlamaForCausalLM(nn.Module):
def __init__(self, config: LlamaConfig):
self.model = LlamaModel(config)
self.tie_word_embeddings = config.tie_word_embeddings
if not config.tie_word_embeddings:
self.lm_head = nn.Linear(config.hidden_size, "vocab_size", bias=False)
self.num_hidden_layers = config.num_hidden_layers
self.num_attention_heads = config.num_attention_heads
self.num_key_value_heads = config.num_key_value_heads
self.head_dim = config.head_dim
self.hidden_size = config.hidden_size
self.vocab_size = config.vocab_size
self.rope_scaling = config.rope_scaling
self.rope_theta = config.position_embedding_base
self.tensor_parallel_shards = config.tensor_parallel_shards
self.disaggregation = config.disaggregation
self.dtype = "float32"
def _set_pp():
# hidden layers
for layer_id in range(config.num_hidden_layers):
stage = layer_id // (config.num_hidden_layers // config.pipeline_parallel_stages)
for _, param in self.model.layers[layer_id].named_parameters():
param.attrs["pipeline_stages"] = [stage]
# last stage
last_stage = config.pipeline_parallel_stages - 1
self.model.norm.weight.attrs["pipeline_stages"] = [last_stage]
# embedding table and lm_head is required by all stages
all_stages = list(range(config.pipeline_parallel_stages))
self.model.embed_tokens.weight.attrs["pipeline_stages"] = all_stages
if not config.tie_word_embeddings:
self.lm_head.weight.attrs["pipeline_stages"] = all_stages
_set_pp()
def to(self, dtype: Optional[str] = None):
super().to(dtype=dtype)
if dtype is not None:
self.dtype = dtype
def batch_forward(
self,
input_embeds: Tensor,
paged_kv_cache: PagedKVCache,
logit_positions: Optional[Tensor] = None,
):
op_ext.configure()
hidden_states = self.model(input_embeds, paged_kv_cache)
if logit_positions is not None:
if self.tensor_parallel_shards > 1:
logit_positions = op.ccl_broadcast_from_worker0(logit_positions)
hidden_states = op.take(hidden_states, logit_positions, axis=1)
return self.get_logits(hidden_states)
def batch_forward_to_last_hidden_states(
self,
input_embeds: Tensor,
paged_kv_cache: PagedKVCache,
):
op_ext.configure()
hidden_states = self.model(input_embeds, paged_kv_cache)
return hidden_states
def embed(self, input_ids: Tensor):
if self.tensor_parallel_shards > 1:
input_ids = op.ccl_broadcast_from_worker0(input_ids)
return self.model.embed_tokens(input_ids)
def get_logits(self, hidden_states: Tensor):
op_ext.configure()
if self.tie_word_embeddings:
logits = self.model.embed_tokens.lm_head_forward(hidden_states)
else:
logits = self.lm_head(hidden_states)
if logits.dtype != "float32":
logits = logits.astype("float32")
return logits
def batch_select_last_hidden_states(self, hidden_states: Tensor, logit_positions: Tensor):
op_ext.configure()
if self.tensor_parallel_shards > 1:
logit_positions = op.ccl_broadcast_from_worker0(logit_positions)
hidden_states = op.take(hidden_states, logit_positions, axis=0)
return hidden_states
def prefill(self, input_embed: Tensor, paged_kv_cache: PagedKVCache):
op_ext.configure()
hidden_states = self.model(input_embed, paged_kv_cache)
hidden_states = index_last_token(hidden_states)
logits = self.get_logits(hidden_states)
return logits, paged_kv_cache
def decode(self, input_embed: Tensor, paged_kv_cache: PagedKVCache):
op_ext.configure()
hidden_states = self.model(input_embed, paged_kv_cache)
logits = self.get_logits(hidden_states)
return logits, paged_kv_cache
def prefill_to_last_hidden_states(self, input_embed: Tensor, paged_kv_cache: PagedKVCache):
op_ext.configure()
hidden_states = self.model(input_embed, paged_kv_cache)
return hidden_states, paged_kv_cache
def decode_to_last_hidden_states(self, input_embed: Tensor, paged_kv_cache: PagedKVCache):
op_ext.configure()
hidden_states = self.model(input_embed, paged_kv_cache)
return hidden_states, paged_kv_cache
def batch_prefill(
self,
input_embeds: Tensor,
logit_positions: Tensor,
paged_kv_cache: PagedKVCache,
):
logits = self.batch_forward(input_embeds, paged_kv_cache, logit_positions)
return logits, paged_kv_cache
def batch_decode(self, input_embeds: Tensor, paged_kv_cache: PagedKVCache):
logits = self.batch_forward(input_embeds, paged_kv_cache)
return logits, paged_kv_cache
def batch_verify(self, input_embeds: Tensor, paged_kv_cache: PagedKVCache):
logits = self.batch_forward(input_embeds, paged_kv_cache)
return logits, paged_kv_cache
def batch_prefill_to_last_hidden_states(
self, input_embeds: Tensor, paged_kv_cache: PagedKVCache
):
hidden_states = self.batch_forward_to_last_hidden_states(input_embeds, paged_kv_cache)
return hidden_states, paged_kv_cache
def batch_decode_to_last_hidden_states(
self, input_embeds: Tensor, paged_kv_cache: PagedKVCache
):
hidden_states = self.batch_forward_to_last_hidden_states(input_embeds, paged_kv_cache)
return hidden_states, paged_kv_cache
def batch_verify_to_last_hidden_states(
self, input_embeds: Tensor, paged_kv_cache: PagedKVCache
):
hidden_states = self.batch_forward_to_last_hidden_states(input_embeds, paged_kv_cache)
return hidden_states, paged_kv_cache
def create_paged_kv_cache(
self,
max_batch_size: tirx.Var,
max_total_seq_len: tirx.Var,
prefill_chunk_size: tirx.Var,
page_size: tirx.Var,
support_sliding_window: tirx.Var,
) -> PagedKVCache:
return PagedKVCache.create_generic(
attn_kind="mha",
max_batch_size=max_batch_size,
max_total_seq_len=max_total_seq_len,
prefill_chunk_size=prefill_chunk_size,
page_size=page_size,
support_sliding_window=support_sliding_window,
num_hidden_layers=self.num_hidden_layers,
num_attention_heads=self.num_attention_heads // self.tensor_parallel_shards,
num_key_value_heads=self.num_key_value_heads // self.tensor_parallel_shards,
qk_head_dim=self.head_dim,
v_head_dim=self.head_dim,
rope_mode=RopeMode.NORMAL,
rope_scale=1,
rope_theta=self.rope_theta,
rope_scaling=self.rope_scaling,
layer_partition=self.model.layer_partition,
enable_disaggregation=self.disaggregation,
dtype=self.dtype,
)
def get_default_spec(self):
mod_spec = {
"embed": {
"input_ids": nn.spec.Tensor(["seq_len"], "int32"),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"get_logits": {
"hidden_states": nn.spec.Tensor(["seq_len", self.hidden_size], self.dtype),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_select_last_hidden_states": {
"hidden_states": nn.spec.Tensor(["seq_len", self.hidden_size], self.dtype),
"logit_positions": nn.spec.Tensor(["batch_size"], "int32"),
"$": {
"param_mode": "none",
"effect_mode": "none",
},
},
"prefill": {
"input_embed": nn.spec.Tensor([1, "seq_len", self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"decode": {
"input_embed": nn.spec.Tensor([1, 1, self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"prefill_to_last_hidden_states": {
"input_embed": nn.spec.Tensor([1, "seq_len", self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"decode_to_last_hidden_states": {
"input_embed": nn.spec.Tensor([1, 1, self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_prefill": {
"input_embeds": nn.spec.Tensor([1, "seq_len", self.hidden_size], self.dtype),
"logit_positions": nn.spec.Tensor(["batch_size"], "int32"),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_decode": {
"input_embeds": nn.spec.Tensor(["batch_size", 1, self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_verify": {
"input_embeds": nn.spec.Tensor([1, "seq_len", self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_prefill_to_last_hidden_states": {
"input_embeds": nn.spec.Tensor([1, "seq_len", self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_decode_to_last_hidden_states": {
"input_embeds": nn.spec.Tensor(["batch_size", 1, self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"batch_verify_to_last_hidden_states": {
"input_embeds": nn.spec.Tensor([1, "seq_len", self.hidden_size], self.dtype),
"paged_kv_cache": nn.spec.Object(object_type=PagedKVCache),
"$": {
"param_mode": "packed",
"effect_mode": "none",
},
},
"create_paged_kv_cache": {
"max_batch_size": int,
"max_total_seq_len": int,
"prefill_chunk_size": int,
"page_size": int,
"support_sliding_window": int,
"$": {
"param_mode": "none",
"effect_mode": "none",
},
},
}
return nn.spec.ModuleSpec.from_raw(mod_spec, self)