-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama2-70b.py
54 lines (45 loc) · 2.06 KB
/
llama2-70b.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
import torch
from transformers import LlamaForCausalLM, LlamaTokenizer
# Load the Llama 2 70B model and tokenizer
model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-70b-chat-hf")
tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-70b-chat-hf")
# Move the model to the GPU
model.cuda()
# Set the model to evaluation mode
model.eval()
# Define the input prompt
prompt = "Hello, how are you today?"
# Encode the prompt
input_ids = tokenizer.encode(prompt, return_tensors="pt").cuda()
# Measure the first token latency
with torch.no_grad():
start_time = torch.cuda.Event(enable_timing=True)
end_time = torch.cuda.Event(enable_timing=True)
start_time.record()
output = model.generate(input_ids, max_length=input_ids.size(-1) + 1, do_sample=True, top_k=50, top_p=0.95, num_return_sequences=1)
end_time.record()
torch.cuda.synchronize()
first_token_latency = start_time.elapsed_time(end_time) / 1000 # in seconds
# Measure the second token latency
with torch.no_grad():
start_time = torch.cuda.Event(enable_timing=True)
end_time = torch.cuda.Event(enable_timing=True)
start_time.record()
output = model.generate(input_ids, max_length=input_ids.size(-1) + 2, do_sample=True, top_k=50, top_p=0.95, num_return_sequences=1)
end_time.record()
torch.cuda.synchronize()
second_token_latency = start_time.elapsed_time(end_time) / 1000 # in seconds
# Measure the throughput
num_tokens = 100
with torch.no_grad():
start_time = torch.cuda.Event(enable_timing=True)
end_time = torch.cuda.Event(enable_timing=True)
start_time.record()
output = model.generate(input_ids, max_length=input_ids.size(-1) + num_tokens, do_sample=True, top_k=50, top_p=0.95, num_return_sequences=1)
end_time.record()
torch.cuda.synchronize()
throughput = num_tokens / (end_time.elapsed_time(start_time) / 1000) # tokens per second
# Print the results
print(f"First token latency: {first_token_latency:.4f} seconds")
print(f"Second token latency: {second_token_latency:.4f} seconds")
print(f"Throughput: {throughput:.2f} tokens/second")