-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsample_generator.py
184 lines (157 loc) · 7.37 KB
/
sample_generator.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import time
#import bitsandbytes #not strictly necessary, used for 8bit inference
import sys
from drugs.nice_imports import efficiency_stuff
import torch
from transformers import AutoTokenizer, TextStreamer, AutoModelForCausalLM
from drugs.dgenerate import DRUGS
from sample_generations.to_samples_text import template_stringed_init
import os
current_script_dir = os.path.dirname(os.path.abspath(__file__))
#model_id = "cognitivecomputations/dolphin-2.2.1-mistral-7b"
#model_id = "TheBloke/30B-Epsilon-AWQ"
#efficiency_stuff['load_in_8bit'] = False #when testing AWQ models
seed_start=420
gen_path = "sample_generations"
completions_path = os.path.join(current_script_dir, gen_path, "completed.json")
def runtest(experiment_name, mod_params, mod_type, mod_name, drug_type, precision, filename, base_content, chat_input):
basepath = os.path.join(current_script_dir, gen_path, experiment_name, mod_params, mod_type, mod_name, precision, drug_type)
path = os.path.join(basepath, filename)
i = 2
while os.path.exists(path):
path = os.path.join(basepath, f'v{i}__{filename}')
i +=1
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'a') as file:
file.write(base_content)
with torch.no_grad():
for i in range(5):
seed = seed_start + i
print("Using Seed: ", seed)
torch.manual_seed(seed)
generated_tokens = model.Dgenerate(
past_key_values = None,
input_ids = chat_input,
min_new_tokens = 5,
max_new_tokens = 800,
streamer = streamer,
)
output = drugs.decode(generated_tokens)
print(f"####\n###\n##\n#\nWRITING TO{path}\n#\n##\n###\n####")
content_to_append = f"\nSeed: {seed}\n```\n{output}\n```\n"
with open(path, 'a') as file:
file.write(content_to_append)
bit_configs = {
"16bit" : dict(torch_dtype=torch.float16, device_map="auto"),
"8bit" : dict(load_in_8bit=True, device_map="auto"),
"8bit_flash" : dict(load_in_8bit=True, attn_implementation="flash_attention_2", device_map="auto"),
"16bit_flash" : dict(torch_dtype=torch.float16, attn_implementation="flash_attention_2", device_map="auto"),
"4bit": dict(device_map="auto"),
"4bit_flash": dict(attn_implementation="flash_attention_2", device_map="auto")
}
model_param_map = {
"NousResearch/Llama-2-7b-chat-hf": {'type': "Llama2", "params": "7B"},
"cognitivecomputations/dolphin-2.2.1-mistral-7b": {'type': "mistral", "params": "7B", "ignore_tests": ["Alan_Watts"]},
"TheBloke/30B-Epsilon-AWQ": {'type': "Llama2", "params": "30B"},
}
model_precisions_map = {
"NousResearch/Llama-2-7b-chat-hf": ["16bit_flash", "8bit_flash", "8bit", "16bit"],
"TheBloke/30B-Epsilon-AWQ": ["4bit"],#, "4bit_flash"],
"cognitivecomputations/dolphin-2.2.1-mistral-7b": ["16bit", "8bit"],
}
model_id = "NousResearch/Llama-2-7b-chat-hf"
initial_input = 'Hello'
doses= [0.01, 0.05, 0.1, 0.15, 0.2, 0.35, 0.5, 0.8, 1]
experiments = {
"Reason_then_create": [
{'role': 'system',
'content': 'Answer the question, fulfill the instruction.'},
{'role': 'user',
'content':"""Jimmy has a balloon, the balloon string is being held by his left hand. Jimmy also has scissors in his right hand and uses the scissors to cut the balloon string slightly above his left hand, what happens to the balloon? I want you to answer this question in 2 steps, first answer what happens to the balloon, and then incorporate that into a creative story about the situation."""}
],
"Alan_Watts" : [
{'role': 'system',
'content': 'Respond as Alan Watts would.'},
{'role': 'user',
'content': "Hello."}
],
"Alan_Watts_Meaning": [
{'role': 'system',
'content': 'Respond as Alan Watts would.'},
{'role': 'user',
'content': 'What does it men to "mean"?'}
],
}
import json
def get_completion_state(val, get_from):
"return a completion object, possibly creating a new one and adding it to get_from if it doesn't exist"
if f'{val}' in get_from:
current_obj = get_from[f'{val}']
else:
current_obj = {"state": "in_progress"}
get_from[f'{val}'] = current_obj
return current_obj, current_obj
try:
with open(completions_path, 'r') as json_file:
completed_dict = json.load(json_file)
except:
completed_dict = {}
def update_completion_state(obj, state):
obj["state"] = state
with open(completions_path, 'w') as json_file: # Open the file in write mode
json.dump(completed_dict, json_file, indent=2)
#tracks completed tests so we can resume in case of error
base = "sample_generations"
drugs = None
model = None
for experiment_name, obj in experiments.items():
chat_input = obj
completion_obj, cpar1 = get_completion_state(experiment_name, completed_dict)
if completion_obj['state'] == 'finished':
continue
for model_id, mod_precisions in model_precisions_map.items():
completion_obj, cpar2 = get_completion_state(model_id, cpar1)
if completion_obj['state'] == 'finished':
continue
mod_name = model_id.split("/")[1]
modinfo = model_param_map[model_id]
if "ignore_tests" in modinfo and experiment_name in modinfo["ignore_tests"]:
break
mod_type = modinfo["type"]
mod_params = modinfo["params"]
for precision in mod_precisions:
completion_obj, cpar3 = get_completion_state(precision, cpar2)
if completion_obj['state'] == 'finished':
continue
if drugs is not None:
del model
model=None
drugs.clear_cuda_caches()
bit_cfg = bit_configs[precision]
for drug_type in ['Q', 'K', 'V', 'A', 'H']:
completion_obj, cpar4 = get_completion_state(drug_type, cpar3)
if completion_obj['state'] == 'finished':
continue
for dose in doses:
completion_obj, cpar5 = get_completion_state(dose, cpar4)
if completion_obj['state'] == 'finished':
continue
model, tokenizer, streamer, drugs, filename, result_string = template_stringed_init(
model_id=model_id,
chatinput=chat_input,
drug_type=drug_type,
dose_theta=dose,
injection_depth=0.4,
spread=5/32,
model=model,
**bit_cfg
)
tokenized_start = tokenizer.apply_chat_template(chat_input, return_tensors='pt')
filename += f"{precision}.md"
runtest(experiment_name, mod_params, mod_type, mod_name, drug_type, precision, filename, result_string, tokenized_start)
update_completion_state(completion_obj, "finished")
update_completion_state(cpar4, "finished")
update_completion_state(cpar3, "finished")
update_completion_state(cpar2, "finished")
update_completion_state(cpar1, "finished")
#str(input("\bAsk Something:"))