forked from ML-GSAI/LLaDA
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.py
More file actions
74 lines (61 loc) · 2.01 KB
/
chat.py
File metadata and controls
74 lines (61 loc) · 2.01 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
import torch
from generate import generate
from transformers import AutoTokenizer, AutoModel
import argparse
def chat(verbose: bool = False):
device = "cuda"
model = (
AutoModel.from_pretrained(
"GSAI-ML/LLaDA-8B-Instruct",
trust_remote_code=True,
torch_dtype=torch.bfloat16,
)
.to(device)
.eval()
)
tokenizer = AutoTokenizer.from_pretrained(
"GSAI-ML/LLaDA-8B-Instruct", trust_remote_code=True
)
gen_length = 128
steps = 128
print("*" * 66)
print(f"** Answer Length: {gen_length} | Sampling Steps: {steps} **")
print("*" * 66)
conversation_num = 0
while True:
user_input = input("Enter your question: ")
m = [{"role": "user", "content": user_input}]
user_input = tokenizer.apply_chat_template(
m, add_generation_prompt=True, tokenize=False
)
input_ids = tokenizer(user_input)["input_ids"]
input_ids = torch.tensor(input_ids).to(device).unsqueeze(0)
if conversation_num == 0:
prompt = input_ids
else:
prompt = torch.cat([prompt, input_ids[:, 1:]], dim=1)
out = generate(
model,
prompt,
steps=steps,
gen_length=gen_length,
block_length=32,
temperature=0.0,
cfg_scale=0.0,
remasking="low_confidence",
verbose=verbose,
tokenizer=tokenizer,
)
answer = tokenizer.batch_decode(
out[:, prompt.shape[1] :], skip_special_tokens=True
)[0]
print(f"Bot's reply: {answer}")
# remove the <EOS>
prompt = out[out != 126081].unsqueeze(0)
conversation_num += 1
print("-----------------------------------------------------------------------")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", "-v", action="store_true")
args = parser.parse_args()
chat(verbose=args.verbose)