-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
74 lines (47 loc) · 2.13 KB
/
sample.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
"""example code for sampling using SpecDec++"""
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
import time
import torch
from hf_generation import my_generate
from wrap_model import AcceptancePredictionHead
device = "cuda"
def set_up():
checkpoint = "meta-llama/Llama-2-70b-chat-hf"
assistant_checkpoint = "meta-llama/Llama-2-7b-chat-hf"
assist_acc_head_dir = "hacky/acchead-llama2-chat-7bx70b"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint, torch_dtype=torch.bfloat16, device_map='cuda:0')
assist_acc_head = AcceptancePredictionHead.from_pretrained(assist_acc_head_dir).to('cuda:0')
model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map='auto')
return tokenizer, model, assistant_model, assist_acc_head
def format_prompt(prompt):
"""
wrap the prompt in llama-2-chat format.
"""
B_INST, E_INST = "[INST]", "[/INST]"
return f"{B_INST} {prompt.strip()} {E_INST}"
def main(prompt):
### load target/draft/Acceptance Head and set generation config
tokenizer, model, assistant_model, assist_acc_head = set_up()
stop_threshold = 0.7
bound = (2, 20)
max_length = 512
before=time.time()
### format and tokenize prompt
prompt = format_prompt(prompt)
inputs = tokenizer(prompt, return_tensors="pt").to(device)
outputs, mismatched_tokens, LM_call = my_generate(model=model, **inputs, assistant_model=assistant_model, \
max_length=max_length, num_assistant_tokens_schedule='ada', \
do_sample=True, \
assist_acc_head=assist_acc_head, \
stop_threshold=stop_threshold, bound=bound)
after = time.time()
assisted_time = (after - before)
print(tokenizer.decode(outputs[0]))
print("assisted time: {:.2f}".format(assisted_time))
print("# mismatched_tokens: {:.2f}".format(mismatched_tokens))
print("# LM_call: {:.2f}".format(LM_call))
return outputs
if __name__ == "__main__":
prompt = "List 10 methods to be a successful PHD."
main(prompt)