-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
474 lines (388 loc) · 17.9 KB
/
Copy pathutils.py
File metadata and controls
474 lines (388 loc) · 17.9 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
import torch
import torch.nn.functional as F
TOPK=10 # topk for sparse tree (10 is a placeholder and it is sufficient)
def pad_path(path, length, pad_value=-2):
"""
Pad the given path list with a specific value up to a specified length.
Parameters:
- path (list): The original list that needs padding.
- length (int): The desired length of the padded list.
- pad_value (optional, default=-2): The value to use for padding.
Returns:
- list: A new list based on the original path but padded to the desired length.
Example:
>>> pad_path([1,2,3], 5)
[1, 2, 3, -2, -2]
Note:
If the given path is already longer than the specified length,
then no padding occurs, and the original path is returned.
"""
return path + [pad_value] * (length - len(path))
def generate_medusa_buffers(medusa_choices, device="cuda"):
"""
Args:
medusa_choices (List[Tuple[int]]):
A list of tuples, each representing one branch (sequence of predicted token indices)
in the decoding tree. Shorter sequences are higher-level branches.
Example:
medusa_choices = [(1,), (2,), (1, 3), (1, 4), (2, 5)]
Conceptually, these represent continuations of a hypothetical prompt, e.g.:
Prompt: "Read this"
Root → token(1): "paper"
Root → token(2): "book"
Root → token(1) → token(3): "paper carefully"
Root → token(1) → token(4): "paper now"
Root → token(2) → token(5): "book quickly"
Resulting tree structure:
[Root: "Read this"]
├── (1) "paper"
│ ├── (3) "carefully"
│ └── (4) "now"
└── (2) "book"
└── (5) "quickly"
device (str):
The device ("cuda" or "cpu") on which tensors are allocated.
"""
sorted_medusa_choices = sorted(medusa_choices, key=lambda x: (len(x), x))
medusa_len = len(sorted_medusa_choices) + 1
depth_counts = []
prev_depth = 0
for path in sorted_medusa_choices:
depth = len(path)
if depth != prev_depth:
depth_counts.append(0)
depth_counts[depth - 1] += 1
prev_depth = depth
medusa_attn_mask = torch.eye(medusa_len, medusa_len)
medusa_attn_mask[:, 0] = 1
start = 0
for i in range(len(depth_counts)):
for j in range(depth_counts[i]):
cur_medusa_choice = sorted_medusa_choices[start + j]
if len(cur_medusa_choice) == 1:
continue
ancestor_idx = []
for c in range(len(cur_medusa_choice) - 1):
ancestor_idx.append(sorted_medusa_choices.index(cur_medusa_choice[:c+1]) + 1)
medusa_attn_mask[j + start + 1, ancestor_idx] = 1
start += depth_counts[i]
medusa_tree_indices = torch.zeros(medusa_len, dtype=torch.long)
medusa_tree_indices[0] = 0
start = 0
for i in range(len(depth_counts)):
for j in range(depth_counts[i]):
cur_medusa_choice = sorted_medusa_choices[start + j]
medusa_tree_indices[start + j + 1] = cur_medusa_choice[-1] + TOPK * i + 1
start += depth_counts[i]
medusa_position_ids = torch.zeros(medusa_len, dtype=torch.long)
start = 0
for i in range(len(depth_counts)):
medusa_position_ids[start + 1: start + depth_counts[i] + 1] = i + 1
start += depth_counts[i]
retrieve_indices_nest = []
retrieve_paths = []
for i in range(len(sorted_medusa_choices)):
cur_medusa_choice = sorted_medusa_choices[-i-1]
retrieve_indice = []
if cur_medusa_choice in retrieve_paths:
continue
else:
for c in range(len(cur_medusa_choice)):
retrieve_indice.append(sorted_medusa_choices.index(cur_medusa_choice[:c+1]))
retrieve_paths.append(cur_medusa_choice[:c+1])
retrieve_indices_nest.append(retrieve_indice)
max_length = max([len(x) for x in retrieve_indices_nest])
retrieve_indices = [pad_path(path, max_length) for path in retrieve_indices_nest]
retrieve_indices = torch.tensor(retrieve_indices, dtype=torch.long)
retrieve_indices = retrieve_indices + 1
retrieve_indices = torch.cat([torch.zeros((retrieve_indices.shape[0], 1), dtype=torch.long), retrieve_indices], dim=1)
medusa_buffers = {
"medusa_attn_mask": medusa_attn_mask.unsqueeze(0).unsqueeze(0),
"tree_indices": medusa_tree_indices,
"medusa_position_ids": medusa_position_ids,
"retrieve_indices": retrieve_indices,
}
medusa_buffers = {
k: v.clone().to(device)
if isinstance(v, torch.Tensor)
else torch.tensor(v, device=device)
for k, v in medusa_buffers.items()
}
return medusa_buffers
def initialize_medusa(input_ids, model, medusa_attn_mask, past_key_values):
"""
Initializes Medusa inference by a forward pass and setting attention masks.
Steps:
1. Computes logits from Medusa decoding heads and the original base model.
2. Sets the tree-based Medusa attention mask in the base model for structured parallel decoding.
Args:
input_ids (torch.Tensor): Input token IDs.
model (MedusaLMHead): The model with Medusa heads and base transformer.
medusa_attn_mask (torch.Tensor): Attention mask enforcing tree-based decoding structure.
past_key_values (List[torch.Tensor]): Cached past hidden states for efficient decoding.
Returns:
- medusa_logits
- logits
"""
medusa_logits, outputs, logits = model(
input_ids, past_key_values=past_key_values, output_orig=True, medusa_forward=True
)
model.base_model.model.medusa_mask = medusa_attn_mask
return medusa_logits, logits
def reset_medusa_mode(
model,
):
model.base_model.model.medusa_mask = None
model.base_model.model.medusa_mode = None
def reset_past_key_values(passed_key_values):
for i in range(len(passed_key_values)):
for j in range(2):
passed_key_values[i][j].current_length.fill_(0)
return passed_key_values
def get_nucleus_one_token(logit, temperature, top_p):
"""
Performs token sampling based on the nucleus (top-p) sampling method.
This function selects a token from a given logit distribution using the nucleus sampling strategy.
It allows for more controlled and diverse generation compared to traditional top-k sampling.
"""
if top_p >= 1:
return torch.multinomial(F.softmax(logit / temperature, dim=-1), 1)
logit = logit / temperature
probs = torch.softmax(logit, dim=-1)
sorted_logits, sorted_indices = torch.sort(probs, descending=True)
cum_probs = torch.cumsum(sorted_logits, dim=-1)
sorted_indices_to_remove = cum_probs > top_p
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices_to_remove.scatter(dim=1, index=sorted_indices, src=sorted_indices_to_remove)
logit[indices_to_remove] = float('-inf')
sampled_tokens = torch.multinomial(F.softmax(logit, dim=-1), 1)
return sampled_tokens
def get_typical_one_token(logit, temperature, posterior_threshold, posterior_alpha):
"""
Implements token sampling based on the typical sampling method.
This function selects a token from a given logit distribution using the typical sampling strategy,
aiming to balance between diversity and likelihood in a more nuanced way compared to traditional methods.
Returns:
torch.Tensor: A tensor containing the indices of the sampled tokens.
"""
logit = logit / temperature
probs = torch.softmax(logit, dim=-1)
entropy = -torch.sum(
probs * torch.log(probs + 1e-5), dim=-1
)
threshold = torch.minimum(
torch.ones_like(entropy) * posterior_threshold,
torch.exp(-entropy) * posterior_alpha,
)
indices_to_remove = probs < threshold.unsqueeze(-1)
logit[indices_to_remove] = float('-inf')
sampled_tokens = torch.multinomial(F.softmax(logit, dim=-1), 1)
return sampled_tokens
def generate_candidates(medusa_logits, logits, tree_indices, retrieve_indices, temperature = 0, posterior_threshold=0.3, posterior_alpha = 0.09, top_p=0.8, sampling = 'typical', fast = False):
"""
Generate candidates based on provided logits and indices.
Parameters:
-- **args
Returns:
- tuple(tensor, tensor)
1. Cartesian candidates derived from the combined original and Medusa logits.
2. Tree candidates mapped from the Cartesian candidates using tree indices.
Example:
Input prompt: "Read this"
Original logits select next token "paper"
Medusa logits suggest top tokens ["carefully", "now", "quickly"]
Resulting candidates (cartesian):
["paper carefully", "paper now", "paper quickly"]
"""
if temperature == 0 or fast:
candidates_logit = torch.argmax(logits[:, -1]).unsqueeze(0)
else:
if sampling == 'typical':
candidates_logit = get_typical_one_token(logits[:, -1], temperature, posterior_threshold, posterior_alpha).squeeze(0)
elif sampling == 'nucleus':
candidates_logit = get_nucleus_one_token(logits[:, -1], temperature, top_p).squeeze(0)
else:
raise NotImplementedError
candidates_medusa_logits = torch.topk(medusa_logits[:, 0, -1], TOPK, dim = -1).indices
candidates = torch.cat([candidates_logit, candidates_medusa_logits.view(-1)], dim=-1)
tree_candidates = candidates[tree_indices]
tree_candidates_ext = torch.cat([tree_candidates, torch.zeros((1), dtype=torch.long, device=tree_candidates.device)], dim=0)
cart_candidates = tree_candidates_ext[retrieve_indices]
tree_candidates = tree_candidates.unsqueeze(0)
return cart_candidates, tree_candidates
def tree_decoding(
model,
tree_candidates,
past_key_values,
medusa_position_ids,
input_ids,
retrieve_indices,
):
"""
Decode the tree candidates using the provided model and reorganize the logits.
Parameters:
**kwargs
Returns:
- tuple: Returns medusa logits, regular logits, and other outputs from the model.
"""
position_ids = medusa_position_ids + input_ids.shape[1]
tree_medusa_logits, outputs, tree_logits = model(
tree_candidates,
output_orig=True,
past_key_values=past_key_values,
position_ids=position_ids,
medusa_forward=True,
)
logits = tree_logits[0, retrieve_indices]
medusa_logits = tree_medusa_logits[:, 0, retrieve_indices]
return medusa_logits, logits, outputs
def get_nucleus_posterior_mask(logits, candidates, temperature, top_p):
"""
Generates a posterior mask for token candidates using nucleus (top-p) sampling.
This function applies nucleus sampling to a set of logits, and then generates a mask indicating
which candidate tokens are selected. It adapts the sampling strategy to accommodate for
temperature scaling and cumulative probability thresholding.
Args:
**kwargs
Returns:
torch.Tensor: A posterior mask indicating which candidate tokens match the sampled tokens.
"""
logits = logits[:, :-1] / temperature
n_samples, n_tokens = logits.shape[0], logits.shape[1]
logits = logits.view(n_samples*n_tokens, -1)
if top_p >= 1:
sampled_tokens = torch.multinomial(F.softmax(logits, dim=-1), 1)
sampled_tokens = sampled_tokens.view(n_samples, n_tokens)
posterior_mask = (candidates[:, 1:] == sampled_tokens).int()
return posterior_mask
probs = F.softmax(logits, dim=-1)
sorted_logits, sorted_indices = torch.sort(probs, descending=True)
cum_probs = torch.cumsum(sorted_logits, dim=-1)
sorted_indices_to_remove = cum_probs > top_p
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices_to_remove.scatter(dim=1, index=sorted_indices, src=sorted_indices_to_remove)
logits[indices_to_remove] = float('-inf')
sampled_tokens = torch.multinomial(F.softmax(logits, dim=-1), 1)
sampled_tokens = sampled_tokens.view(n_samples, n_tokens)
posterior_mask = (candidates[:, 1:] == sampled_tokens).int()
return posterior_mask
def get_typical_posterior_mask(logits, candidates, temperature, posterior_threshold, posterior_alpha):
logits = logits[:, :-1] / temperature
n_samples, n_tokens = logits.shape[0], logits.shape[1]
logits = logits.view(n_samples*n_tokens, -1)
probs = F.softmax(logits, dim=-1)
entropy = -torch.sum(
probs * torch.log(probs + 1e-5), dim=-1
)
threshold = torch.minimum(
torch.ones_like(entropy) * posterior_threshold,
torch.exp(-entropy) * posterior_alpha,
)
indices_to_remove = probs < threshold.unsqueeze(-1)
logits[indices_to_remove] = float('-inf')
sampled_tokens = torch.multinomial(F.softmax(logits, dim=-1), 1)
sampled_tokens = sampled_tokens.view(n_samples, n_tokens)
posterior_mask = (candidates[:, 1:] == sampled_tokens).int()
return posterior_mask
def evaluate_posterior(
logits, candidates, temperature, posterior_threshold=0.3, posterior_alpha = 0.09, top_p=0.8, sampling = 'typical', fast = True
):
"""
Evaluate the posterior probabilities of the candidates based on the provided logits and choose the best candidate.
Depending on the temperature value, the function either uses greedy decoding or evaluates posterior
probabilities to select the best candidate.
Args:
-- **args
Returns:
- tuple(tensor, int)
- best_candidate (torch.Tensor): Index of the chosen best candidate.
- accept_length (int): Length of the accepted candidate sequence.
"""
if temperature == 0:
posterior_mask = (
candidates[:, 1:] == torch.argmax(logits[:, :-1], dim=-1)
).int()
candidates_accept_length = (torch.cumprod(posterior_mask, dim=1)).sum(dim=1)
accept_length = candidates_accept_length.max()
if accept_length == 0:
best_candidate = torch.tensor(0, dtype=torch.long, device=candidates.device)
else:
best_candidate = torch.argmax(candidates_accept_length).to(torch.long)
return best_candidate, accept_length
if sampling == 'typical':
if fast:
posterior_prob = torch.softmax(logits[:, :-1] / temperature, dim=-1)
candidates_prob = torch.gather(
posterior_prob, dim=-1, index=candidates[:, 1:].unsqueeze(-1)
).squeeze(-1)
posterior_entropy = -torch.sum(
posterior_prob * torch.log(posterior_prob + 1e-5), dim=-1
)
threshold = torch.minimum(
torch.ones_like(posterior_entropy) * posterior_threshold,
torch.exp(-posterior_entropy) * posterior_alpha,
)
posterior_mask = candidates_prob > threshold
candidates_accept_length = (torch.cumprod(posterior_mask, dim=1)).sum(dim=1)
accept_length = candidates_accept_length.max()
if accept_length == 0:
best_candidate = torch.tensor(0, dtype=torch.long, device=candidates.device)
else:
best_candidates = torch.where(candidates_accept_length == accept_length)[0]
likelihood = torch.sum(
torch.log(candidates_prob[best_candidates, :accept_length]), dim=-1
)
best_candidate = best_candidates[torch.argmax(likelihood)]
return best_candidate, accept_length
posterior_mask = get_typical_posterior_mask(logits, candidates, temperature, posterior_threshold, posterior_alpha, fast)
candidates_accept_length = (torch.cumprod(posterior_mask, dim=1)).sum(dim=1)
accept_length = candidates_accept_length.max()
if accept_length == 0:
best_candidate = torch.tensor(0, dtype=torch.long, device=candidates.device)
else:
best_candidate = torch.argmax(candidates_accept_length).to(torch.long)
return best_candidate, accept_length
if sampling == 'nucleus':
assert top_p < 1.0 + 1e-6, "top_p should between 0 and 1"
posterior_mask = get_nucleus_posterior_mask(logits, candidates, temperature, top_p)
candidates_accept_length = (torch.cumprod(posterior_mask, dim=1)).sum(dim=1)
accept_length = candidates_accept_length.max()
if accept_length == 0:
best_candidate = torch.tensor(0, dtype=torch.long, device=candidates.device)
else:
best_candidate = torch.argmax(candidates_accept_length).to(torch.long)
return best_candidate, accept_length
else:
raise NotImplementedError
def update_inference_inputs(
input_ids,
candidates,
best_candidate,
accept_length,
retrieve_indices,
outputs,
logits,
medusa_logits,
new_token,
past_key_values_data,
current_length_data,
):
prev_input_len = input_ids.shape[1]
select_indices = (
retrieve_indices[best_candidate, : accept_length + 1] + prev_input_len
)
input_ids = torch.cat(
[input_ids, candidates[None, best_candidate, : accept_length + 1]], dim=-1
)
tgt = past_key_values_data[..., select_indices, :]
dst = past_key_values_data[..., prev_input_len : prev_input_len + tgt.shape[-2], :]
dst.copy_(tgt, non_blocking=True)
current_length_data.fill_(prev_input_len + tgt.shape[-2])
logits = logits[None, best_candidate, accept_length : accept_length + 1]
medusa_logits = medusa_logits[
:, None, best_candidate, accept_length : accept_length + 1
]
new_token += accept_length + 1
return input_ids, logits, medusa_logits, new_token