-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathutils.py
63 lines (56 loc) · 2.33 KB
/
utils.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
import torch
from transformers import StoppingCriteriaList
from transformers.generation.stopping_criteria import (
EosTokenCriteria,
MaxLengthCriteria,
)
def export_llm(model, inputs, min_seq_len=1, max_seq_len=16):
"""
Exports the LLM model into an ExportedProgram with dynamic shapes.
In the case of guard failures due to some PyTorch kernel implements, we also
try to re-export the graph by expressing them as runtime assert nodes
"""
with torch.no_grad():
# max=1024 has contraint violation error. https://github.com/pytorch/pytorch/issues/125604
seq_len = torch.export.Dim("seq_len", min=min_seq_len, max=max_seq_len)
try:
print("Trying to export the model using torch.export.export()..")
# strict=False only enables aotautograd tracing and excludes dynamo.
ep = torch.export.export(
model, (inputs,), dynamic_shapes=({1: seq_len},), strict=False
)
except:
print(
"Trying torch.export._trace._export to trace the graph since torch.export.export() failed"
)
# This API is used to express the constraint violation guards as asserts in the graph.
ep = torch.export._trace._export(
model,
(inputs,),
dynamic_shapes=({1: seq_len},),
strict=False,
allow_complex_guards_as_runtime_asserts=True,
)
return ep
def generate(model, input_seq, max_tokens, eos_token_id):
"""
Greedy decoding of the model. This generates up to max_tokens.
"""
# Max length of output seq = current input_seq length + max_tokens allowed to generate
max_output_seq_length = input_seq.shape[1] + max_tokens
stopping_criteria = StoppingCriteriaList(
[
MaxLengthCriteria(max_length=max_output_seq_length),
EosTokenCriteria(eos_token_id=eos_token_id),
]
)
while True:
outputs = model(input_seq)
logits = outputs.logits
next_token_logits = logits[:, -1, :]
next_tokens = torch.argmax(next_token_logits, dim=-1)
input_seq = torch.cat([input_seq, next_tokens[:, None]], dim=-1)
# TODO: Handle batch in this check
if stopping_criteria(input_seq, logits).item():
break
return input_seq