forked from zeroentropy-ai/llama-chunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathog_test.py
136 lines (116 loc) · 3.97 KB
/
og_test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from llama_cpp import Llama
import math
import random
import json
import numpy as np
prompt_format = """
<|start_header_id|>system<|end_header_id|>
{SYSTEM_MESSAGE}
<|eot_id|><|start_header_id|>user<|end_header_id|>
{EXAMPLE_INPUT}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>
{EXAMPLE_OUTPUT}
<|eot_id|><|start_header_id|>user<|end_header_id|>
{USER_MESSAGE}
<|eot_id|><|start_header_id|>assistant<|end_header_id|>
""".strip() + "\n\n"
llm = Llama(
model_path="./models/Meta-Llama-3.1-70B-Instruct-Q6_K_L-00001-of-00002.gguf",
#grammar=LlamaGrammar.from_file("./dictionary_clean.gbnf"),
logits_all=True,
n_ctx=8192,
n_gpu_layers=-1,
)
# Get vocab
vocab = []
for i in range(llm.n_vocab()):
token: str | None
try:
token = llm.detokenize([i]).decode("utf-8")
except UnicodeDecodeError:
token = None
vocab.append(token)
BIG_SPLIT_TOKEN = "\u6bb5"
SMALL_SPLIT_TOKEN = "\u987f"
BIG_SPLIT_TOKEN_INDEX = None
for i, vocab_element in enumerate(vocab):
if vocab_element == BIG_SPLIT_TOKEN:
BIG_SPLIT_TOKEN_INDEX = i
assert BIG_SPLIT_TOKEN_INDEX != None
def replace_split_tokens(text: str) -> str:
return text.replace("{SPLIT_TOKEN}", BIG_SPLIT_TOKEN)
return text.replace("{BIG_SPLIT_TOKEN}", BIG_SPLIT_TOKEN).replace("{SMALL_SPLIT_TOKEN}", SMALL_SPLIT_TOKEN)
SYSTEM_MESSAGE = replace_split_tokens(open("system.txt").read())
EXAMPLE_INPUT = open("example_input.txt").read()
EXAMPLE_OUTPUT = replace_split_tokens(open("example_output.txt").read())
USER_MESSAGE = open("user2.txt").read()
USER_MESSAGE = open("userhtml.txt").read().replace("\n", "")
USER_MESSAGE = open("userbenchmarksmall.txt").read()
USER_MESSAGE = open("userbenchmark.txt").read()
# null_state = llm.save_state()
input_string = prompt_format.format(
SYSTEM_MESSAGE=SYSTEM_MESSAGE,
EXAMPLE_INPUT=EXAMPLE_INPUT,
EXAMPLE_OUTPUT=BIG_SPLIT_TOKEN + EXAMPLE_OUTPUT,
USER_MESSAGE=USER_MESSAGE,
)
input_tokens = llm.tokenize(input_string.encode("utf-8"), special=True) + llm.tokenize(BIG_SPLIT_TOKEN.encode("utf-8"))
user_tokens = llm.tokenize(USER_MESSAGE.encode("utf-8"))
print(f"NUM PREFIX TOKENS: {len(input_tokens)}")
print("Evaluating prefix tokens...")
llm.eval(input_tokens)
start_n_tokens = llm.n_tokens
start_index = len(input_tokens)
print("Done!")
llm.eval(user_tokens)
user_split_by_token = []
for token in user_tokens:
try:
user_split_by_token.append(llm.detokenize([token]).decode("utf-8"))
except UnicodeDecodeError:
user_split_by_token.append("<COULD NOT DECODE>")
all_logprobs = []
for i in range(start_index, start_index+len(user_tokens)):
logprobs = llm.logits_to_logprobs(llm.scores[i])
top_100_indices = np.argsort(logprobs)[-100:][::-1]
top_100_logprobs = [(int(i), float(logprobs[i])) for i in top_100_indices]
all_logprobs.append(top_100_logprobs)
open("result.json", "w").write(
json.dumps({
"split_tokens": [
BIG_SPLIT_TOKEN,
# SMALL_SPLIT_TOKEN,
],
"vocab": vocab,
"user_tokens": user_split_by_token,
"all_logprobs": all_logprobs,
}, indent=4)
)
def get_result():
llm.load_state(root_state)
llm.set_seed(random.randint(1000, 100000))
max_tokens = 100
tokens = []
while len(tokens) < max_tokens:
token = llm.sample(
#temp=0.72 * (1.0 + 10.0 * math.exp(-len(tokens)-1)), # 0.72*(1+5*e^(-1))
temp=0,
top_k=40,
top_p=0.95,
repeat_penalty=1.1,
)
tokens.append(token)
current_string = llm.detokenize(tokens).decode('utf-8')
print("Current String:", current_string)
if token == llm.token_eos():
break
llm.eval([token])
return llm.detokenize(tokens).decode('utf-8')
def test_result():
results_file = open("logs/results.log", "a")
result = get_result()
print("RESULT:", result)
if result is not None:
print("VALID!!!!")
results_file.write(result + "\n")
results_file.flush()