-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample_chat.py
More file actions
48 lines (42 loc) · 1.49 KB
/
Copy pathexample_chat.py
File metadata and controls
48 lines (42 loc) · 1.49 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
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("evabyte/EvaByte-SFT", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("evabyte/EvaByte-SFT", torch_dtype=torch.bfloat16, trust_remote_code=True).eval().to("cuda")
# Prepare input messages
messages = [
{"role": "user", "content": "Write me an English pangram."}
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt",
).to("cuda")
# Byte-by-byte generation (default)
generation_output = model.generate(
input_ids=input_ids,
max_new_tokens=256
)
response = tokenizer.decode(
generation_output[0][input_ids.shape[1]:],
skip_special_tokens=False,
clean_up_tokenization_spaces=False
)
print(f"User: {messages[0]['content']}\n")
print("===========================================")
print("[Byte-by-byte generation] via model.generate():")
print(f"> Assistant: {response}")
# Multibyte generation (faster alternative)
generation_output = model.multi_byte_generate(
input_ids=input_ids,
max_new_tokens=256
)
response = tokenizer.decode(
generation_output[0][input_ids.shape[1]:],
skip_special_tokens=False,
clean_up_tokenization_spaces=False
)
print("===========================================")
print("[Multibyte generation] via model.multi_byte_generate():")
print(f"> Assistant: {response}")
print("===========================================")