/
model.py
363 lines (301 loc) · 15.9 KB
/
model.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
363
# Copyright Amazon Web Services and its Affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import torch
import os
from transformers_neuronx import decoder
from transformers_neuronx import module
from transformers_neuronx import ops
from transformers_neuronx import sampling
from transformers_neuronx import utils
from transformers_neuronx import bucket
from transformers_neuronx import base
from transformers_neuronx.llama.config import LlamaConfig
from transformers_neuronx.llama.modules import LlamaForCausalLM
from transformers_neuronx.llama.hlo import LlamaForSamplingNoEmbeddingHlo
class LlamaForSampling(module.WrappingCheckpointCompatibleModel, base.NeuronModelBase):
def __init__(self, config, *, n_positions=2048, batch_size=1, amp='f32', tp_degree=2,
context_length_estimate=None, context_unroll=None, unroll=None,
neuron_config=None, prefixed_length=0, **kwargs):
config = LlamaConfig(config, n_positions, batch_size, amp, tp_degree)
super().__init__(LlamaForCausalLM, config)
self.config = config
self.neuron_config = neuron_config
self.prefixed_length = prefixed_length
if context_unroll is None:
context_unroll = config.num_hidden_layers
self.context_unroll = context_unroll
if unroll is None:
unroll = config.num_hidden_layers
self.token_buckets = bucket.token_sizes(n_positions)
self.context_buckets = bucket.context_sizes(context_length_estimate, self.token_buckets)
if prefixed_length:
if prefixed_length not in self.context_buckets:
self.context_buckets.append(prefixed_length)
self.context_buckets = sorted(self.context_buckets)
self.max_positions = self.token_buckets[-1]
self.decoder_lm_head = decoder.DecoderLmHeadForSamplingNoEmbedding(
tp_degree, self.token_buckets, 1, batch_size, config.attention_head_size, amp,
config.num_hidden_layers, unroll, neuron_config=neuron_config, allow_pad=True,
)
hlo_builder = LlamaForSamplingNoEmbeddingHlo(config, neuron_config=neuron_config)
self.decoder_lm_head.add_inputs_builder(hlo_builder.inputs)
self.decoder_lm_head.add_layer_builder(hlo_builder.layer)
self.decoder_lm_head.add_ln_lm_head_builder(hlo_builder.ln_lm_head)
self.decoder_lm_head_for_context = None
def _save_compiled_artifacts(self, directory):
if os.path.isfile(directory):
raise FileExistsError(
f'Artifacts should be saved to a directory. '
f'Found existing file: {directory}'
)
os.makedirs(directory, exist_ok=True)
self.decoder_lm_head.save_compiler_artifacts(os.path.join(directory, 'neuron-program.pkl'))
def _load_compiled_artifacts(self, directory):
if not os.path.isdir(directory):
raise FileNotFoundError(f'Did not find directory: {directory}')
program_filename = os.path.join(directory, 'neuron-program.pkl')
if os.path.exists(program_filename):
self.decoder_lm_head.load_compiler_artifacts_after_build(program_filename)
def to_neuron(self):
# Materialize the embedding to CPU
self.chkpt_model.model.embed_tokens.materialize()
ops.init()
for layer in self.chkpt_model.model.layers:
layer.materialize()
attn = layer.self_attn
mlp = layer.mlp
new_layer = self.decoder_lm_head.new_layer()
new_layer.add_pre_attention_layer_norm(layer.input_layernorm.weight.detach(), None)
new_layer.add_attention_query(attn.q_proj.weight.detach().T, None)
new_layer.add_attention_key(attn.k_proj.weight.detach().T, None)
new_layer.add_attention_value(attn.v_proj.weight.detach().T, None)
new_layer.add_attention_output(attn.o_proj.weight.detach(), None, sharding=1, transposed=False)
new_layer.add_pre_mlp_layer_norm(layer.post_attention_layernorm.weight.detach(), None)
# Note: Automatic MLP padding is safe since zeros are *only* introduced to intermediary state
new_layer.add_parameter(mlp.gate_proj.weight.T, sharding=1, allow_pad=True, allow_quantize=True)
new_layer.add_parameter(mlp.up_proj.weight.T, sharding=1, allow_pad=True, allow_quantize=True)
new_layer.add_parameter(mlp.down_proj.weight, sharding=1, allow_pad=True, allow_quantize=True, out_feature_dim=0)
new_layer.to_neuron()
layer.nullify()
ln_f = self.chkpt_model.model.norm
ln_f.materialize()
self.decoder_lm_head.add_final_layer_norm(ln_f.weight.detach(), None)
lm_head = self.chkpt_model.lm_head
lm_head.materialize()
self.decoder_lm_head.add_lm_head(lm_head.weight.detach().T)
lm_head.nullify()
self.decoder_lm_head.to_neuron()
self.decoder_lm_head.enable_executor()
if self.context_buckets:
self.decoder_lm_head_for_context = {}
for context_length_estimate in self.context_buckets:
model = self.decoder_lm_head.build_weight_shared(
n_positions_list=[context_length_estimate],
n_active_tokens=context_length_estimate,
unroll=self.context_unroll,
share_caches=True,
)
# PERF: No latency improvement seen in multi-layer models from executor
if self.context_unroll == self.config.num_hidden_layers:
model.enable_executor()
self.decoder_lm_head_for_context[context_length_estimate] = model
def reset(self):
self.decoder_lm_head.reset()
def context(self, hidden, cache_ids, start_ids):
context_length = hidden.shape[1]
current = 0
estimate = bucket.find(self.context_buckets, context_length)
if estimate is not None:
hidden_context = hidden
cache_context = cache_ids
# Slice context that when it is too large
if context_length > estimate:
current = estimate
hidden_context = hidden[:, :estimate]
cache_context = cache_ids[:estimate]
# Cannot use context encoding for a context that is too small. This
# is because the caller must be aware of the cache-ids/start-ids
# used.
elif context_length < estimate:
current = 0
# Directly pass input to the context network when exactly sized
else:
current = estimate
if current == estimate:
model = self.decoder_lm_head_for_context[estimate]
logits = model(hidden_context, cache_context, start_ids)
for i in range(current, context_length):
cache_ids = torch.as_tensor([i], dtype=torch.int32)
hidden_slice = hidden[:, i:i+1].contiguous()
logits = self.decoder_lm_head(hidden_slice, cache_ids, start_ids)
return logits
def set_prefixed(self, input_ids):
self.prefixed_input_ids = input_ids[:, :self.prefixed_length]
prefixed_length = self.prefixed_length
self.prefixed_length = 0
self.forward(self.prefixed_input_ids)
self.prefixed_length = prefixed_length
def forward(self, input_ids, cache_ids=None, start_ids=None):
batch_size, context_length = input_ids.shape
if start_ids is None:
start_ids = torch.zeros(batch_size, dtype=torch.int32)
if cache_ids is None:
cache_ids = torch.arange(context_length, dtype=torch.int32)
if self.prefixed_length:
cache_ids += self.prefixed_length
hidden = self.chkpt_model.model.embed_tokens(input_ids)
hidden = hidden.transpose(0, -1).contiguous()
if context_length > 1:
logits = self.context(hidden, cache_ids, start_ids)
else:
logits = self.decoder_lm_head(hidden, cache_ids, start_ids)
logits = logits.to(torch.float32)
logits = logits[:self.config.vocab_size, -1, :]
logits = logits.transpose(0, 1)
return logits
def sample(self, input_ids, sequence_length, start_ids=None,
top_k=50, top_p=1.0, eos_token_override=None, temperature=1.0, streamer=None):
# To enable optimized context encoding network, we must pad
# up to the context length estimate or we will not correctly
# select the final context logits (See: layers/transformer.py).
# This also means we need to shift the start_ids over to correct
# for padding.
offset = 0
batch_size, context_length = input_ids.shape
prefixed_length = self.prefixed_length
if context_length < prefixed_length:
self.prefixed_length = 0
else:
input_ids = input_ids[:, prefixed_length:]
context_length -= prefixed_length
sequence_length -= prefixed_length
estimate = bucket.find(self.context_buckets, context_length)
if estimate:
if context_length < estimate:
input_ids = utils.pad(input_ids, 1, estimate, left=True)
offset = estimate - context_length
if not prefixed_length:
if start_ids is None:
start_ids = torch.zeros(batch_size, dtype=torch.int32)
start_ids += offset
sequence_length += offset
# Sequence length cannot be greater than n_positions
sequence_length = min(sequence_length, self.max_positions)
result = sampling.sample_llama(
self, input_ids, start_ids, sequence_length,
eos_token_id=self.config.eos_token_id if eos_token_override is None else eos_token_override,
top_k=top_k, top_p=top_p, temperature=temperature, streamer=streamer
)
if offset != 0:
result = result[:, offset:]
return result
class FIDLlamaForSampling(LlamaForSampling):
def __init__(self, config, *, n_positions=2048, batch_size=1, amp='f32', tp_degree=2,
context_length_estimate=None, context_unroll=None, unroll=None,
neuron_config=None, **kwargs):
# Force batch_size=1 in NEFF
super().__init__(config, n_positions=n_positions, batch_size=1, amp=amp,
tp_degree=tp_degree, context_length_estimate=context_length_estimate,
context_unroll=context_unroll, unroll=unroll, neuron_config=neuron_config,
**kwargs)
self.batch_size = batch_size
self.bos_token_id = self.config.bos_token_id
def context(self, hidden, cache_ids, start_ids):
# Fusion-In-Decoder context encoding
fused_context_length = hidden.shape[1]
context_length = fused_context_length // self.batch_size
current = 0
estimate = bucket.find(self.context_buckets, context_length)
if estimate is not None:
hidden_context = hidden
cache_context = cache_ids
# Slice context when it is too large
if context_length > estimate:
current = estimate
hidden_context = hidden[:, :estimate]
cache_context = cache_ids[:estimate]
# Cannot use context encoding for a context that is too small. This
# is because the caller must be aware of the cache-ids/start-ids
# used.
elif context_length < estimate:
current = 0
# Directly pass input to the context network when exactly sized
else:
current = estimate
if current == estimate:
model = self.decoder_lm_head_for_context[estimate]
# Run each context separately in-place
for j in range(self.batch_size):
single_context_slice = slice(j * context_length, (j+1) * context_length)
logits = model(hidden_context[:, single_context_slice, :], cache_context[single_context_slice], start_ids)
for i in range(current, context_length):
cache_ids = torch.as_tensor([i], dtype=torch.int32)
hidden_slice = hidden[:, i:i+1].contiguous()
logits = self.decoder_lm_head(hidden_slice, cache_ids, start_ids)
logits[:] = float('-inf')
logits[self.bos_token_id] = 1.0
return logits
def _save_compiled_artifacts(self, directory):
if os.path.isfile(directory):
raise FileExistsError(
f'Artifacts should be saved to a directory. '
f'Found existing file: {directory}'
)
os.makedirs(directory, exist_ok=True)
self.decoder_lm_head.save_compiler_artifacts(os.path.join(directory, 'neuron-program.pkl'))
def _load_compiled_artifacts(self, directory):
if not os.path.isdir(directory):
raise FileNotFoundError(f'Did not find directory: {directory}')
program_filename = os.path.join(directory, 'neuron-program.pkl')
if os.path.exists(program_filename):
self.decoder_lm_head.load_compiler_artifacts_after_build(program_filename)
def sample(self, input_ids, sequence_length, start_ids=None, top_k=50, streamer=None):
""" Sample function
input_ids: shape [batch_size, context_length]
input_ids of different batch index represent single (context + query).
They will be mixed and generate a single output sequence.
"""
# In FID-Llama, first, context encoding is done w/ generating any output token for context
# Here batch-size are different context+queries of single run
offset = 0
fused_batch_size = 1
batch_size, context_length = input_ids.shape
# The context length estimate is chosen based on single (context+query)
estimate = bucket.find(self.context_buckets, context_length)
if batch_size * context_length >= sequence_length:
raise ValueError(f"sequence_length [{sequence_length}] should be larger than fused input contexts [{context_length} x {batch_size}]")
if batch_size * estimate >= sequence_length:
raise ValueError(f"sequence_length [{sequence_length}] should be larger than fused input context estimates [{estimate} x {batch_size}]")
if estimate:
if context_length < estimate:
input_ids = utils.pad(input_ids, 1, estimate, left=True)
offset = estimate - context_length
if start_ids is None:
start_ids = torch.zeros(fused_batch_size, dtype=torch.int32)
start_ids += offset
sequence_length += offset
# Sequence length cannot be greater than n_positions
sequence_length = min(sequence_length, self.max_positions)
context_length = estimate
# Flatten input_ids
context_length = batch_size * context_length
input_ids = input_ids.reshape(fused_batch_size, context_length)
# Run the model
result = sampling.sample_llama(self, input_ids, start_ids, sequence_length,
eos_token_id=self.config.eos_token_id, top_k=top_k, streamer=streamer)
if offset != 0:
# Offset by offset * batch_size
result = result[:, context_length:]
return result