-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcllm_llama_modeling.py
414 lines (351 loc) · 17.9 KB
/
cllm_llama_modeling.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
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
from dataclasses import dataclass, field
import json
import math
import pathlib
import functools
from typing import Dict, Optional, Sequence, List, Tuple
import random
from tqdm import tqdm
import torch.nn.functional as F
import sqlite3
import time
import numpy as np
import torch
from torch.utils.data import Dataset
import transformers
from transformers.trainer_pt_utils import LabelSmoother, get_module_class_from_name
from fastchat.model.model_adapter import get_conversation_template
from transformers.cache_utils import Cache, DynamicCache
from transformers.modeling_attn_mask_utils import (
_prepare_4d_causal_attention_mask,
_prepare_4d_causal_attention_mask_for_sdpa,
)
import torch.nn.functional as F
from transformers import LlamaModel,LlamaForCausalLM
import argparse
def delete_false_key_value(
self,
num_of_false_tokens,
) -> Tuple[torch.Tensor, torch.Tensor]:
for layer_idx in range(len(self.key_cache)):
self.key_cache[layer_idx] = self.key_cache[layer_idx][..., :-num_of_false_tokens, :]
self.value_cache[layer_idx] = self.value_cache[layer_idx][..., :-num_of_false_tokens, :]
@torch.inference_mode()
def jacobi_forward(
self,
input_ids: torch.LongTensor = None,
tokenizer=None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
use_cache: Optional[bool] = None,
max_new_tokens: Optional[int] = None,
prefill_phase: Optional[bool] = False,
chat: Optional[bool] = False,
):
assert use_cache == True
if input_ids is not None:
batch_size, seq_length = input_ids.shape[:2]
else:
raise ValueError("You have to specify either input_ids or inputs_embeds")
if prefill_phase: # prefill phase, just compute the keys & values of prompt
# self.model is the instance of class LlamaModel
inputs_embeds = self.model.embed_tokens(input_ids)
past_key_values_length = 0
if use_cache:
use_legacy_cache = not isinstance(past_key_values, Cache)
if use_legacy_cache:
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
past_key_values_length = past_key_values.get_usable_length(seq_length)
if position_ids is None:
device = input_ids.device if input_ids is not None else inputs_embeds.device
position_ids = torch.arange(
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
)
position_ids = position_ids.unsqueeze(0)
if self.model._use_flash_attention_2:
# 2d mask is passed through the layers
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
elif self.model._use_sdpa :
# output_attentions=True can not be supported when using SDPA, and we fall back on
# the manual implementation that requires a 4D causal mask in all cases.
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
attention_mask,
(batch_size, seq_length),
inputs_embeds,
past_key_values_length,
)
else:
# 4d mask is passed through the layers
attention_mask = _prepare_4d_causal_attention_mask(
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
)
# embed positions
hidden_states = inputs_embeds
# decoder layers
for decoder_layer in self.model.layers:
layer_outputs = decoder_layer(
hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_values,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
if use_cache:
next_decoder_cache = layer_outputs[1]
hidden_states = self.model.norm(hidden_states)
if self.config.pretraining_tp > 1:
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
logits = torch.cat(logits, dim=-1)
else:
logits = self.lm_head(hidden_states)
logits = logits.float()
predict_next_tokens = torch.argmax(torch.nn.functional.softmax(logits, dim=-1) / 0.001, dim=-1)
first_correct_token = predict_next_tokens[:, -1]
return next_decoder_cache, first_correct_token
else: # generation phase, input as random_initilized point and output as fixed point
jacobian_trajectory = []
accurate_n_gram = torch.zeros_like(input_ids).to(input_ids.device)
accurate_length = 0
next_point = input_ids
jacobian_trajectory.append(next_point)
iter_counter = 0
prev_len = 0
while True:
current_point = next_point
inputs_embeds = self.model.embed_tokens(current_point)
attention_mask = None
position_ids = None
seq_length = current_point.shape[1]
if use_cache:
use_legacy_cache = not isinstance(past_key_values, Cache)
if use_legacy_cache:
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
past_key_values_length = past_key_values.get_usable_length(seq_length)
# print(past_key_values_length) # return previous_seq_length
if position_ids is None:
device = input_ids.device if input_ids is not None else inputs_embeds.device
position_ids = torch.arange(
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
)
position_ids = position_ids.unsqueeze(0)
if self.model._use_flash_attention_2:
# 2d mask is passed through the layers
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
elif self.model._use_sdpa :
# output_attentions=True can not be supported when using SDPA, and we fall back on
# the manual implementation that requires a 4D causal mask in all cases.
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
attention_mask,
(batch_size, seq_length),
inputs_embeds,
past_key_values_length,
)
else:
# 4d mask is passed through the layers
attention_mask = _prepare_4d_causal_attention_mask(
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
)
# embed positions
hidden_states = inputs_embeds
# decoder layers
for decoder_layer in self.model.layers:
layer_outputs = decoder_layer(
hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_values,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
hidden_states = self.model.norm(hidden_states)
if self.config.pretraining_tp > 1:
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
logits = torch.cat(logits, dim=-1)
else:
logits = self.lm_head(hidden_states)
logits = logits.float()
all_shift_one_token = torch.argmax(torch.nn.functional.softmax(logits, dim=-1) / 0.001, dim=-1)
next_point = torch.cat((current_point[0, 0].view(1,-1), all_shift_one_token[0, :seq_length-1].view(1,-1)), dim=-1)
first_false_index = torch.where(torch.eq(current_point[0], next_point[0]) == False)[0]
jacobian_trajectory.append(next_point)
if len(first_false_index) > 0:
fast_forward_cnt = first_false_index[0].item()
past_key_values.delete_false_key_value(seq_length - fast_forward_cnt) # delete the false keys & values
else:
fast_forward_cnt = torch.sum(torch.eq(current_point, next_point)).item()
accurate_n_gram[0, accurate_length : accurate_length + fast_forward_cnt] = next_point[0, :fast_forward_cnt]
first_correct_token = all_shift_one_token[:,-1]
if chat:
if tokenizer.eos_token_id in accurate_n_gram[0, :accurate_length + fast_forward_cnt]:
eos_positions = torch.where(accurate_n_gram[0]==tokenizer.eos_token_id)[0]
eos_position = eos_positions[0]
generated_str = tokenizer.decode(accurate_n_gram[0, :eos_position], skip_special_tokens=True)
else:
generated_str = tokenizer.decode(accurate_n_gram[0, :accurate_length + fast_forward_cnt], skip_special_tokens=True)
print(generated_str[prev_len:], flush=True, end="")
prev_len = len(generated_str)
break
accurate_n_gram[0, accurate_length : accurate_length + fast_forward_cnt] = next_point[0, :fast_forward_cnt]
accurate_length += fast_forward_cnt
next_point = next_point[0, fast_forward_cnt:].view(1,-1) # only false tokens should be re-generated
if chat:
if tokenizer.eos_token_id in accurate_n_gram[0, :accurate_length]:
eos_positions = torch.where(accurate_n_gram[0]==tokenizer.eos_token_id)[0]
eos_position = eos_positions[0]
generated_str = tokenizer.decode(accurate_n_gram[0, :eos_position], skip_special_tokens=True)
else:
generated_str = tokenizer.decode(accurate_n_gram[0, :accurate_length], skip_special_tokens=True)
print(generated_str[prev_len:], flush=True, end="")
prev_len = len(generated_str)
iter_counter += 1
return accurate_n_gram, first_correct_token, iter_counter, accurate_length
@torch.inference_mode()
def jacobi_forward_profiling(
self,
input_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
use_cache: Optional[bool] = None,
max_new_tokens: Optional[int] = None,
prefill_phase: Optional[bool] = False,
):
assert use_cache == True
if input_ids is not None:
batch_size, seq_length = input_ids.shape[:2]
else:
raise ValueError("You have to specify either input_ids or inputs_embeds")
if prefill_phase: # prefill phase, just compute the keys & values of prompt
# self.model is the instance of class LlamaModel
inputs_embeds = self.model.embed_tokens(input_ids)
past_key_values_length = 0
if use_cache:
use_legacy_cache = not isinstance(past_key_values, Cache)
if use_legacy_cache:
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
past_key_values_length = past_key_values.get_usable_length(seq_length)
if position_ids is None:
device = input_ids.device if input_ids is not None else inputs_embeds.device
position_ids = torch.arange(
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
)
position_ids = position_ids.unsqueeze(0)
if self.model._use_flash_attention_2:
# 2d mask is passed through the layers
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
elif self.model._use_sdpa :
# output_attentions=True can not be supported when using SDPA, and we fall back on
# the manual implementation that requires a 4D causal mask in all cases.
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
attention_mask,
(batch_size, seq_length),
inputs_embeds,
past_key_values_length,
)
else:
# 4d mask is passed through the layers
attention_mask = _prepare_4d_causal_attention_mask(
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
)
# embed positions
hidden_states = inputs_embeds
# decoder layers
for decoder_layer in self.model.layers:
layer_outputs = decoder_layer(
hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_values,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
if use_cache:
next_decoder_cache = layer_outputs[1]
hidden_states = self.model.norm(hidden_states)
if self.config.pretraining_tp > 1:
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
logits = torch.cat(logits, dim=-1)
else:
logits = self.lm_head(hidden_states)
logits = logits.float()
predict_next_tokens = torch.argmax(torch.nn.functional.softmax(logits, dim=-1), dim=-1)
first_correct_token = predict_next_tokens[:, -1]
return next_decoder_cache, first_correct_token
else: # generation phase, input as random_initilized point and output as fixed point
jacobian_trajectory = []
accurate_n_gram = torch.zeros_like(input_ids).to(input_ids.device)
accurate_length = 0
next_point = input_ids
jacobian_trajectory.append(next_point)
iter_counter = 0
while True:
current_point = next_point
inputs_embeds = self.model.embed_tokens(current_point)
attention_mask = None
position_ids = None
seq_length = current_point.shape[1]
if use_cache:
use_legacy_cache = not isinstance(past_key_values, Cache)
if use_legacy_cache:
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
past_key_values_length = past_key_values.get_usable_length(seq_length)
# print(past_key_values_length) # return previous_seq_length
if position_ids is None:
device = input_ids.device if input_ids is not None else inputs_embeds.device
position_ids = torch.arange(
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
)
position_ids = position_ids.unsqueeze(0)
if self.model._use_flash_attention_2:
# 2d mask is passed through the layers
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
elif self.model._use_sdpa :
# output_attentions=True can not be supported when using SDPA, and we fall back on
# the manual implementation that requires a 4D causal mask in all cases.
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
attention_mask,
(batch_size, seq_length),
inputs_embeds,
past_key_values_length,
)
else:
# 4d mask is passed through the layers
attention_mask = _prepare_4d_causal_attention_mask(
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
)
# embed positions
hidden_states = inputs_embeds
# decoder layers
for decoder_layer in self.model.layers:
layer_outputs = decoder_layer(
hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_values,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
hidden_states = self.model.norm(hidden_states)
if self.config.pretraining_tp > 1:
lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
logits = torch.cat(logits, dim=-1)
else:
logits = self.lm_head(hidden_states)
logits = logits.float()
all_shift_one_token = torch.argmax(torch.nn.functional.softmax(logits, dim=-1) / 0.01, dim=-1)
next_point= torch.cat((current_point[0, 0].view(1,-1), all_shift_one_token[0, :seq_length-1].view(1,-1)), dim=-1)
jacobian_trajectory.append(next_point)
if torch.all(torch.eq(current_point, next_point)).item():
#print('Successfully break!')
#print(next_point)
first_correct_token = torch.argmax(torch.nn.functional.softmax(logits, dim=-1), dim=-1)[:,-1]
break
past_key_values.delete_false_key_value(seq_length)
iter_counter += 1
return jacobian_trajectory[:-1], next_point, first_correct_token, iter_counter