forked from exo-explore/exo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
llama3.py
362 lines (305 loc) · 10.3 KB
/
llama3.py
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
"""
llama3 model
Written with pytorch using torchtune and other methods
"""
from typing import Optional, Any, Tuple, List, Union, Callable
import torch
import torch.nn as nn
import torchtune.modules as ttm
import torchtune.generation as ttg
from torchtune.models.llama3_1 import Llama3ScaledRoPE
from torchtune.modules.attention_utils import _MaskType
from exo.inference.shard import Shard
from exo.inference.torch.models.llm_utils import MultiLayerPreceptron, RMSNorm, get_torch_dtype
class ShardTransformerDecoder(ttm.TransformerDecoder):
"""
ShardTransformerDecorder
Custom version of torchtune TransformerDecoder to allow for
sharding of models and passing of hidden layers between shards
"""
def __init__(
self,
*,
shard: Shard,
tok_embeddings: nn.Embedding,
layers: Union[nn.Module, List[nn.Module], nn.ModuleList],
max_seq_len: int,
num_heads: int,
head_dim: int,
norm: nn.Module,
output: Union[nn.Linear, Callable],
num_layers: Optional[int] = None,
output_hidden_states: Optional[List[int]] = None,
):
super().__init__(
tok_embeddings=tok_embeddings,
layers=layers,
max_seq_len=max_seq_len,
num_heads=num_heads,
head_dim=head_dim,
norm=norm,
output=output,
num_layers=num_layers,
output_hidden_states=output_hidden_states,
)
self.shard = shard
def setup_caches(
self,
batch_size: int,
dtype: torch.dtype,
*,
encoder_max_seq_len: Optional[int] = None,
decoder_max_seq_len: Optional[int] = None,
):
"""
modified version for shard
assume just decoder layers
"""
if decoder_max_seq_len is not None:
self.decoder_max_cache_seq_len = decoder_max_seq_len
else:
self.decoder_max_cache_seq_len = self.max_seq_len
for layer in self.layers:
if layer is not None:
layer.setup_caches(
batch_size,
dtype,
encoder_max_seq_len=self.encoder_max_cache_seq_len,
decoder_max_seq_len=self.decoder_max_cache_seq_len,
)
def caches_are_enabled(self) -> bool:
"""
modified version for shard
"""
if self.layers[0] is not None:
return self.layers[0].caches_are_enabled()
else:
for layer in self.layers:
if layer is not None:
return layer.caches_are_enabled()
def forward(
self,
tokens: torch.Tensor,
*,
mask: Optional[_MaskType] = None,
encoder_input: Optional[torch.Tensor] = None,
encoder_mask: Optional[torch.Tensor] = None,
input_pos: Optional[torch.Tensor] = None,
) -> Union[torch.Tensor, List[torch.Tensor]]:
# Determine the type of input and shape
if tokens.ndim == 3:
h = tokens # Use directly as hidden states
else:
h = self.tok_embeddings(tokens) # Apply token tok_embeddings
seq_len = h.shape[1]
self._validate_inputs(
seq_len,
mask=mask,
encoder_input=encoder_input,
encoder_mask=encoder_mask,
input_pos=input_pos,
)
# Initialize a list to capture hidden states if requested
# for captured hidden states
hidden = []
for i in range(self.shard.start_layer, self.shard.end_layer + 1):
layer = self.layers[i]
print(f"\nhidden layer in H[{i}]\n{h}\nmask\n{mask}\ninput_pos\n{input_pos}\n{self.output_hidden_states}\n")
# Process through each transformer layer
with torch.no_grad():
h = layer(
h,
mask=mask,
encoder_input=encoder_input,
encoder_mask=encoder_mask,
input_pos=input_pos,
)
if i in self.output_hidden_states:
hidden.append(h)
print(f"\nhidden layer out H[{i}]->H[{i + 1}]\n{h}\n")
# Apply normalization
h = self.norm(h)
# Handle chunked output if needed
if self.num_output_chunks > 0:
output = self.chunked_output(h)
else:
output = self.output(h).float()
# Return list if hidden states are requested
output = [hidden[-1], output] if hidden else output
print(f"\n\noutput {output}\n\n")
return output
def LlamaModel(config: dict, shard: Shard):
"""
LlamaModel using torchtune
"""
# rope scaling config
if config["rope_scaling"] is not None:
scale_factor = config["rope_scaling"].get("factor", 32)
rope = Llama3ScaledRoPE(
dim=config["head_dim"],
max_seq_len=config["max_seq_len"],
base=config["rope_base"],
scale_factor=scale_factor,
)
# hack to align sharded weights with layers
# fill unused layer positions with None
layers = [None for _ in range(shard.n_layers)]
for i in range(shard.start_layer, shard.end_layer + 1):
self_attn = ttm.MultiHeadAttention(
embed_dim=config["embed_dim"],
num_heads=config["num_heads"],
num_kv_heads=config["num_kv_heads"],
head_dim=config["head_dim"],
q_proj=nn.Linear(
config["embed_dim"],
config["num_heads"] * config["head_dim"],
bias=config["attn_bias"],
),
k_proj=nn.Linear(
config["embed_dim"],
config["num_kv_heads"] * config["head_dim"],
bias=config["attn_bias"],
),
v_proj=nn.Linear(
config["embed_dim"],
config["num_kv_heads"] * config["head_dim"],
bias=config["attn_bias"],
),
output_proj=nn.Linear(
config["embed_dim"],
config["embed_dim"],
bias=config["attn_bias"],
),
max_seq_len=config["max_seq_len"],
attn_dropout=config["attn_dropout"],
pos_embeddings=rope,
)
mlp = MultiLayerPreceptron(config["embed_dim"], config["intermediate_dim"], config["hidden_act"])
layer = ttm.TransformerSelfAttentionLayer(
attn=self_attn,
mlp=mlp,
sa_norm=RMSNorm(config["embed_dim"], eps=config["norm_eps"]),
mlp_norm=RMSNorm(config["embed_dim"], eps=config["norm_eps"]),
)
layers[i] = layer
for i in range(len(layers)):
print(f"layers[{i}]: {layers[i]}")
layers = nn.ModuleList(layers)
tok_embeddings = nn.Embedding(config["vocab_size"], config["embed_dim"])
output_proj = ttm.TiedLinear(tok_embeddings)
# output_proj = nn.Linear(
# config["embed_dim"],
# config["vocab_size"],
# bias=config["attn_bias"],
# )
return ShardTransformerDecoder(
tok_embeddings=tok_embeddings,
shard=shard,
layers=layers,
max_seq_len=config["max_seq_len"],
num_heads=config["num_heads"],
head_dim=config["head_dim"],
norm=RMSNorm(config["embed_dim"], eps=config["norm_eps"]),
output=output_proj,
num_layers=config["num_layers"],
)
# return ttm.TransformerDecoder(
# tok_embeddings=tok_embeddings,
# layers=layers,
# max_seq_len=config["max_seq_len"],
# num_heads=config["num_heads"],
# head_dim=config["head_dim"],
# norm=RMSNorm(config["embed_dim"], eps=config["norm_eps"]),
# output=output_proj,
# num_layers=config["num_layers"],
# )
class ShardedLlamaModel(nn.Module):
def __init__(
self,
config: dict,
shard: Shard,
tokenizer: Any,
device: Optional[torch.device] = None,
max_new_tokens: Optional[int] = 10,
use_cache: Optional[bool] = False
):
super(ShardedLlamaModel, self).__init__()
self.tokenizer = tokenizer
self.shard = shard
self.config = config
self.dtype = get_torch_dtype(self.config["torch_dtype"]) if "torch_dtype" in self.config else torch.float
self.device = device if device is not None else torch.device("cpu")
self.use_cache = use_cache if use_cache else self.config.get("use_cache", False)
self.max_new_tokens = max_new_tokens
self.max_seq_len = self.config["max_seq_len"]
self.model = LlamaModel(config, self.shard).to(dtype=self.dtype, device=self.device)
def generate(
self,
tokens: torch.Tensor,
hidden_state: Optional[torch.Tensor] = None
) -> Tuple[Optional[List[torch.Tensor]], Optional[torch.Tensor]]:
"""
Generate logits and/or hidden_states from llama model
Args
tokens (torch.Tensor) - tokens from prompt tokenization and generation
hidden_state (torch.Tensor, optional) - hidden state from last activated hidden layer, if any
"""
if tokens.ndim == 1:
tokens = tokens.view(1, -1)
bsz, tokens_length = tokens.size()
# setup cache
if not self.model.caches_are_enabled() and self.use_cache:
with self.device:
self.model.setup_caches(
bsz,
self.dtype,
decoder_max_seq_len=tokens.numel() + self.max_new_tokens
)
if not self.shard.is_last_layer():
self.model.output_hidden_states = [self.shard.end_layer]
total_response_length = tokens_length + self.max_seq_len
resp_max_seq_len = total_response_length if not self.model.caches_are_enabled() else self.model.decoder_max_cache_seq_len
# clone tokens
generated_tokens = tokens.clone()
# masking for proper attention
padding_masks = generated_tokens != self.tokenizer.pad_id
if not padding_masks.all():
padding_masks = torch.nn.functional.pad(padding_masks, (0, self.max_seq_len), value=True)
masks = ttg.get_causal_mask_from_padding_mask(padding_masks, target_seq_len=resp_max_seq_len)
input_pos = ttg.get_position_ids_from_padding_mask(padding_masks)
else:
masks = torch.tril(
torch.ones(
total_response_length,
resp_max_seq_len if resp_max_seq_len is not None else total_response_length,
dtype=torch.bool,
device=tokens.device,
)
).unsqueeze(0)
input_pos = torch.arange(0, total_response_length, device=generated_tokens.device).unsqueeze(0)
if self.model.caches_are_enabled():
curr_masks = masks[:, :tokens_length]
else:
curr_masks = masks[:, :tokens_length, :tokens_length]
input_pos = input_pos[:, :tokens_length].squeeze()
if hidden_state is not None:
model_output = self.model(
tokens=hidden_state,
mask=curr_masks,
input_pos=input_pos,
)
else:
model_output = self.model(
tokens=tokens,
mask=curr_masks,
input_pos=input_pos,
)
print(f"\nmodel_output: {model_output}")
if isinstance(model_output, list):
model_logits = model_output[1]
model_output.pop() # remove logits
model_hs = model_output[0] # get last hidden state
else:
model_logits = model_output
model_hs = None
return model_hs, model_logits