Spaces:
Build error
Build error
jijivski
commited on
Commit
•
0bf42ca
1
Parent(s):
52d0c82
move to sribd
Browse files- app.py +10 -5
- get_loss/get_loss.py +53 -50
- get_loss/get_loss_hf.py +288 -0
- get_loss/{my_geyt.py → my_get_logit.py} +0 -0
app.py
CHANGED
@@ -1,10 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
os.system('
|
|
|
|
|
7 |
# 第一个功能:基于输入文本和对应的损失值对文本进行着色展示
|
|
|
8 |
def color_text(text_list=["hi", "FreshEval"], loss_list=[0.1,0.7]):
|
9 |
"""
|
10 |
根据损失值为文本着色。
|
@@ -45,6 +48,8 @@ def color_pipeline(text=["hi", "FreshEval"], model=None):
|
|
45 |
"""
|
46 |
给定一个文本,返回其对应的着色文本。
|
47 |
"""
|
|
|
|
|
48 |
tokenizer=None # get tokenizer
|
49 |
ids, loss = get_ids_loss(text, tokenizer, model)
|
50 |
text = get_text(ids, tokenizer)
|
@@ -61,7 +66,7 @@ with gr.Blocks() as demo:
|
|
61 |
# TODO craw and drop the file
|
62 |
|
63 |
# loss_input = gr.Number(label="loss")
|
64 |
-
model_input = gr.Textbox(label="model name", placeholder="input your model name here...")
|
65 |
# TODO select models that can be used online
|
66 |
# TODO maybe add our own models
|
67 |
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
from .get_loss.get_loss_hf import run_get_loss
|
5 |
+
|
6 |
+
# os.system('git clone https://github.com/EleutherAI/lm-evaluation-harness')
|
7 |
+
# os.system('cd lm-evaluation-harness')
|
8 |
+
# os.system('pip install -e .')
|
9 |
# 第一个功能:基于输入文本和对应的损失值对文本进行着色展示
|
10 |
+
|
11 |
def color_text(text_list=["hi", "FreshEval"], loss_list=[0.1,0.7]):
|
12 |
"""
|
13 |
根据损失值为文本着色。
|
|
|
48 |
"""
|
49 |
给定一个文本,返回其对应的着色文本。
|
50 |
"""
|
51 |
+
# rtn_dic=run_get_loss()
|
52 |
+
# {'logit':logit,'input_ids':input_chunk,'tokenizer':tokenizer,'neg_log_prob_temp':neg_log_prob_temp}
|
53 |
tokenizer=None # get tokenizer
|
54 |
ids, loss = get_ids_loss(text, tokenizer, model)
|
55 |
text = get_text(ids, tokenizer)
|
|
|
66 |
# TODO craw and drop the file
|
67 |
|
68 |
# loss_input = gr.Number(label="loss")
|
69 |
+
model_input = gr.Textbox(label="model name", placeholder="input your model name here... now I am trying phi-2...")
|
70 |
# TODO select models that can be used online
|
71 |
# TODO maybe add our own models
|
72 |
|
get_loss/get_loss.py
CHANGED
@@ -9,6 +9,8 @@ import gc
|
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
from datetime import datetime
|
11 |
import argparse
|
|
|
|
|
12 |
|
13 |
|
14 |
RWKV4_TOKENIZER_FILE = "./support/20B_tokenizer.json"
|
@@ -142,13 +144,13 @@ def eval_rwkv(model, tokenizer, texts, chunk_size, v4pile=False):
|
|
142 |
input_length = len(input_seq)
|
143 |
|
144 |
neg_log_prob_temp = 0
|
145 |
-
for begin in range(0, input_length, chunk_size):
|
146 |
-
|
147 |
|
148 |
-
|
149 |
|
150 |
-
|
151 |
-
|
152 |
|
153 |
# log_sum = calculate_log_sum(logit, torch.tensor(input_chunk).cuda())
|
154 |
|
@@ -165,7 +167,7 @@ def eval_rwkv(model, tokenizer, texts, chunk_size, v4pile=False):
|
|
165 |
# print(f'log probability sum: {sum(rwkv_test_data) / len(rwkv_test_data):.2f}')
|
166 |
# print(f'avg tokens: {sum(rwkv_token_length_list) / len(rwkv_token_length_list):.0f}')
|
167 |
|
168 |
-
return logit
|
169 |
|
170 |
|
171 |
def eval_hf_model(model, tokenizer, texts, chunk_size):
|
@@ -183,9 +185,9 @@ def eval_hf_model(model, tokenizer, texts, chunk_size):
|
|
183 |
|
184 |
neg_log_prob_temp = 0
|
185 |
# for begin in range(0, seq_length, chunk_size):
|
186 |
-
|
187 |
|
188 |
-
|
189 |
|
190 |
# log_sum = calculate_log_sum(logit, input_chunk.squeeze(0))
|
191 |
# neg_log_prob_temp += log_sum
|
@@ -201,7 +203,7 @@ def eval_hf_model(model, tokenizer, texts, chunk_size):
|
|
201 |
# print(f'log probability sum: {sum(data) / len(data):.2f}')
|
202 |
# print(f'avg tokens: {sum(token_length_list) / len(token_length_list):.0f}')
|
203 |
|
204 |
-
return logit
|
205 |
|
206 |
|
207 |
# if __name__ == '__main__':
|
@@ -236,11 +238,11 @@ def run_get_loss(args):
|
|
236 |
|
237 |
# eval
|
238 |
if args.model_type in ['hf', 'mamba']:
|
239 |
-
|
240 |
elif args.model_type == 'rwkv':
|
241 |
-
|
242 |
elif args.model_type == 'rwkv4pile':
|
243 |
-
|
244 |
else:
|
245 |
raise NotImplementedError
|
246 |
|
@@ -252,43 +254,44 @@ def run_get_loss(args):
|
|
252 |
|
253 |
# print(json.dumps(results, indent=4, ensure_ascii=False))
|
254 |
|
255 |
-
|
256 |
|
257 |
if __name__ == '__main__':
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
from datetime import datetime
|
11 |
import argparse
|
12 |
+
import mamba_ssm
|
13 |
+
import rwkv
|
14 |
|
15 |
|
16 |
RWKV4_TOKENIZER_FILE = "./support/20B_tokenizer.json"
|
|
|
144 |
input_length = len(input_seq)
|
145 |
|
146 |
neg_log_prob_temp = 0
|
147 |
+
# for begin in range(0, input_length, chunk_size):
|
148 |
+
input_chunk = input_seq[:chunk_size]
|
149 |
|
150 |
+
logit = model.forward(input_chunk, None, full_output=True)[0]
|
151 |
|
152 |
+
if len(input_chunk) == 1:
|
153 |
+
logit = logit.unsqueeze(0)
|
154 |
|
155 |
# log_sum = calculate_log_sum(logit, torch.tensor(input_chunk).cuda())
|
156 |
|
|
|
167 |
# print(f'log probability sum: {sum(rwkv_test_data) / len(rwkv_test_data):.2f}')
|
168 |
# print(f'avg tokens: {sum(rwkv_token_length_list) / len(rwkv_token_length_list):.0f}')
|
169 |
|
170 |
+
return logit,logit,input_chunk,tokenizer
|
171 |
|
172 |
|
173 |
def eval_hf_model(model, tokenizer, texts, chunk_size):
|
|
|
185 |
|
186 |
neg_log_prob_temp = 0
|
187 |
# for begin in range(0, seq_length, chunk_size):
|
188 |
+
input_chunk = inputs['input_ids'][:, :chunk_size]
|
189 |
|
190 |
+
logit = model.forward(input_ids=input_chunk).logits[0, :, :]
|
191 |
|
192 |
# log_sum = calculate_log_sum(logit, input_chunk.squeeze(0))
|
193 |
# neg_log_prob_temp += log_sum
|
|
|
203 |
# print(f'log probability sum: {sum(data) / len(data):.2f}')
|
204 |
# print(f'avg tokens: {sum(token_length_list) / len(token_length_list):.0f}')
|
205 |
|
206 |
+
return logit,input_chunk,tokenizer
|
207 |
|
208 |
|
209 |
# if __name__ == '__main__':
|
|
|
238 |
|
239 |
# eval
|
240 |
if args.model_type in ['hf', 'mamba']:
|
241 |
+
return eval_hf_model(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size)
|
242 |
elif args.model_type == 'rwkv':
|
243 |
+
return eval_rwkv(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size)
|
244 |
elif args.model_type == 'rwkv4pile':
|
245 |
+
return eval_rwkv(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size, v4pile=True)
|
246 |
else:
|
247 |
raise NotImplementedError
|
248 |
|
|
|
254 |
|
255 |
# print(json.dumps(results, indent=4, ensure_ascii=False))
|
256 |
|
257 |
+
from types import SimpleNamespace
|
258 |
|
259 |
if __name__ == '__main__':
|
260 |
+
args=SimpleNamespace(model='microsft/phi-2',model_type='hf',data='data.json',log_path='./logs/',model_cache=None,chunk_size=1024)
|
261 |
+
|
262 |
+
|
263 |
+
|
264 |
+
# def run_get_loss(input_string, model_type):
|
265 |
+
# # load data
|
266 |
+
# texts = [input_string]
|
267 |
+
# print(f'data size: {len(texts)}')
|
268 |
+
|
269 |
+
# # load model
|
270 |
+
# if model_type == 'hf':
|
271 |
+
# model, tokenizer = load_hf_model(args.model, args.model_cache)# tokenzier path, model path
|
272 |
+
# elif model_type == 'rwkv':
|
273 |
+
# model, tokenizer = load_rwkv(args.model)
|
274 |
+
# elif model_type == 'mamba':
|
275 |
+
# model, tokenizer = load_mamba(args.model)
|
276 |
+
# elif model_type == 'rwkv4pile':
|
277 |
+
# model, tokenizer = load_rwkv4pile(args.model)
|
278 |
+
# else:
|
279 |
+
# raise NotImplementedError
|
280 |
+
|
281 |
+
# # eval
|
282 |
+
# if model_type in ['hf', 'mamba']:
|
283 |
+
# results = eval_hf_model(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size)
|
284 |
+
# elif model_type == 'rwkv':
|
285 |
+
# results = eval_rwkv(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size)
|
286 |
+
# elif model_type == 'rwkv4pile':
|
287 |
+
# results = eval_rwkv(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size, v4pile=True)
|
288 |
+
# else:
|
289 |
+
# raise NotImplementedError
|
290 |
+
|
291 |
+
# results['model_name_or_path'] = args.model
|
292 |
+
# results['data_path'] = args.data
|
293 |
+
# results['chunk_size'] = args.chunk_size
|
294 |
+
|
295 |
+
# make_log(results, args.log_path)
|
296 |
+
|
297 |
+
# print(json.dumps(results, indent=4, ensure_ascii=False))
|
get_loss/get_loss_hf.py
ADDED
@@ -0,0 +1,288 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import packages
|
2 |
+
import os
|
3 |
+
# from tqdm import tqdm
|
4 |
+
# import warnings
|
5 |
+
import json
|
6 |
+
import torch.nn.functional as F
|
7 |
+
import torch
|
8 |
+
import gc
|
9 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
+
from datetime import datetime
|
11 |
+
import argparse
|
12 |
+
from types import SimpleNamespace
|
13 |
+
|
14 |
+
# import mamba_ssm
|
15 |
+
# import rwkv
|
16 |
+
|
17 |
+
|
18 |
+
# RWKV4_TOKENIZER_FILE = "./support/20B_tokenizer.json"
|
19 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
20 |
+
|
21 |
+
def load_list_from_json(file_path):
|
22 |
+
"""
|
23 |
+
Loads a list of strings from a JSON file.
|
24 |
+
|
25 |
+
:param file_path: Path of the JSON file to be loaded.
|
26 |
+
:return: List of strings loaded from the JSON file.
|
27 |
+
"""
|
28 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
29 |
+
return json.load(file)
|
30 |
+
|
31 |
+
|
32 |
+
def calculate_loss(logits, target_token_ids):
|
33 |
+
# shifted_logits = logits[:-1, :]
|
34 |
+
# shifted_targets = target_token_ids[1:]
|
35 |
+
|
36 |
+
# log_probs = F.log_softmax(shifted_logits, dim=-1)
|
37 |
+
loss = torch.nn.functional.cross_entropy(logits[:-1, :].view(-1, logits.shape[-1]),
|
38 |
+
target_token_ids[1:].view(-1), reduction='none')
|
39 |
+
|
40 |
+
|
41 |
+
# target_log_probs = -log_probs.gather(1, shifted_targets.unsqueeze(1)).squeeze()
|
42 |
+
# # print(target_log_probs)
|
43 |
+
|
44 |
+
# log_sum = torch.sum(target_log_probs, dim=-1)
|
45 |
+
# print(perplexity_sum)
|
46 |
+
|
47 |
+
return loss.item()
|
48 |
+
|
49 |
+
|
50 |
+
def calculate_log_sum(logits, target_token_ids):
|
51 |
+
shifted_logits = logits[:-1, :]
|
52 |
+
shifted_targets = target_token_ids[1:]
|
53 |
+
|
54 |
+
log_probs = F.log_softmax(shifted_logits, dim=-1)
|
55 |
+
|
56 |
+
target_log_probs = -log_probs.gather(1, shifted_targets.unsqueeze(1)).squeeze()
|
57 |
+
# print(target_log_probs)
|
58 |
+
|
59 |
+
log_sum = torch.sum(target_log_probs, dim=-1)
|
60 |
+
# print(perplexity_sum)
|
61 |
+
|
62 |
+
return log_sum.item()
|
63 |
+
|
64 |
+
|
65 |
+
def print_model_parameters_in_billions(model):
|
66 |
+
total_params = sum(p.numel() for p in model.parameters())
|
67 |
+
|
68 |
+
total_params_billion = total_params / 1e9
|
69 |
+
|
70 |
+
print(f"Model parameters: {total_params_billion:.3f} billion")
|
71 |
+
|
72 |
+
|
73 |
+
# def make_log(data_dict, folder_path):
|
74 |
+
# if not os.path.exists(folder_path):
|
75 |
+
# try:
|
76 |
+
# os.makedirs(folder_path)
|
77 |
+
# print(f"Directory created at {folder_path}")
|
78 |
+
# except Exception as e:
|
79 |
+
# print(f"Error creating directory: {e}")
|
80 |
+
# return
|
81 |
+
|
82 |
+
# timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
83 |
+
# file_name = f"{timestamp}.json"
|
84 |
+
# file_path = os.path.join(folder_path, file_name)
|
85 |
+
|
86 |
+
# try:
|
87 |
+
# with open(file_path, 'w') as file:
|
88 |
+
# json.dump(data_dict, file, indent=4)
|
89 |
+
# print(f"Dictionary saved successfully to {file_path}")
|
90 |
+
# except Exception as e:
|
91 |
+
# print(f"Error saving dictionary: {e}")
|
92 |
+
|
93 |
+
|
94 |
+
# def load_rwkv(path):
|
95 |
+
# os.environ['RWKV_JIT_ON'] = '1'
|
96 |
+
# os.environ["RWKV_CUDA_ON"] = '1'
|
97 |
+
|
98 |
+
# from rwkv.model import RWKV
|
99 |
+
# from rwkv.utils import PIPELINE
|
100 |
+
|
101 |
+
# rwkv_model = RWKV(model=path, strategy='cuda fp16')
|
102 |
+
# rwkv_pipeline = PIPELINE(rwkv_model, r"rwkv_vocab_v20230424")
|
103 |
+
# rwkv_tokenizer = rwkv_pipeline.tokenizer
|
104 |
+
|
105 |
+
# return rwkv_model, rwkv_tokenizer
|
106 |
+
|
107 |
+
|
108 |
+
# def load_rwkv4pile(path):
|
109 |
+
# os.environ['RWKV_JIT_ON'] = '1'
|
110 |
+
# os.environ["RWKV_CUDA_ON"] = '1'
|
111 |
+
|
112 |
+
# from rwkv.model import RWKV
|
113 |
+
# from rwkv.utils import PIPELINE
|
114 |
+
|
115 |
+
# rwkv_model = RWKV(model=path, strategy='cuda fp16')
|
116 |
+
# rwkv_pipeline = PIPELINE(rwkv_model, RWKV4_TOKENIZER_FILE)
|
117 |
+
# rwkv_tokenizer = rwkv_pipeline.tokenizer
|
118 |
+
|
119 |
+
# return rwkv_model, rwkv_tokenizer
|
120 |
+
|
121 |
+
|
122 |
+
def load_hf_model(path, cache_path):
|
123 |
+
hf_tokenizer = AutoTokenizer.from_pretrained(path)
|
124 |
+
if cache_path is not None:
|
125 |
+
hf_model = AutoModelForCausalLM.from_pretrained(path,
|
126 |
+
device_map=device,
|
127 |
+
trust_remote_code=True,
|
128 |
+
cache_dir=cache_path).eval()
|
129 |
+
else:
|
130 |
+
hf_model = AutoModelForCausalLM.from_pretrained(path,
|
131 |
+
device_map=device,
|
132 |
+
trust_remote_code=True).eval()
|
133 |
+
|
134 |
+
print_model_parameters_in_billions(hf_model)
|
135 |
+
|
136 |
+
return hf_model, hf_tokenizer
|
137 |
+
|
138 |
+
|
139 |
+
# def load_mamba(path):
|
140 |
+
# from mamba_ssm.models.mixer_seq_simple import MambaLMHeadModel
|
141 |
+
|
142 |
+
# mamba_tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b")
|
143 |
+
# mamba_model = MambaLMHeadModel.from_pretrained(path, device="cuda", dtype=torch.float16)
|
144 |
+
# mamba_model.device = torch.device('cuda')
|
145 |
+
|
146 |
+
# print_model_parameters_in_billions(mamba_model)
|
147 |
+
|
148 |
+
# return mamba_model, mamba_tokenizer
|
149 |
+
|
150 |
+
|
151 |
+
# def eval_rwkv(model, tokenizer, texts, chunk_size, v4pile=False):
|
152 |
+
# rwkv_test_data = []
|
153 |
+
# rwkv_token_length_list = []
|
154 |
+
|
155 |
+
# for idx, sample in tqdm(enumerate(texts), total=len(texts)):
|
156 |
+
|
157 |
+
# with torch.no_grad():
|
158 |
+
|
159 |
+
# if v4pile:
|
160 |
+
# input_seq = tokenizer.encode(sample).ids # v4
|
161 |
+
# else:
|
162 |
+
# input_seq = tokenizer.encode(sample)
|
163 |
+
|
164 |
+
# input_length = len(input_seq)
|
165 |
+
|
166 |
+
# neg_log_prob_temp = 0
|
167 |
+
# # for begin in range(0, input_length, chunk_size):
|
168 |
+
# input_chunk = input_seq[:chunk_size]
|
169 |
+
|
170 |
+
# logit = model.forward(input_chunk, None, full_output=True)[0]
|
171 |
+
|
172 |
+
# if len(input_chunk) == 1:
|
173 |
+
# logit = logit.unsqueeze(0)
|
174 |
+
|
175 |
+
# log_sum = calculate_log_sum(logit, torch.tensor(input_chunk).cuda())
|
176 |
+
|
177 |
+
# neg_log_prob_temp += log_sum
|
178 |
+
|
179 |
+
# rwkv_token_length_list.append(input_length)
|
180 |
+
# rwkv_test_data.append(neg_log_prob_temp)
|
181 |
+
|
182 |
+
# data_dict = {
|
183 |
+
# 'neg_log_prob_sum': sum(rwkv_test_data) / len(rwkv_test_data),
|
184 |
+
# 'avg tokens': sum(rwkv_token_length_list) / len(rwkv_token_length_list),
|
185 |
+
# }
|
186 |
+
|
187 |
+
# print(f'log probability sum: {sum(rwkv_test_data) / len(rwkv_test_data):.2f}')
|
188 |
+
# print(f'avg tokens: {sum(rwkv_token_length_list) / len(rwkv_token_length_list):.0f}')
|
189 |
+
|
190 |
+
return logit,logit,input_chunk,tokenizer
|
191 |
+
|
192 |
+
|
193 |
+
def eval_hf_model(model, tokenizer, texts, chunk_size):
|
194 |
+
data = []
|
195 |
+
token_length_list = []
|
196 |
+
|
197 |
+
# for idx, sample in tqdm(enumerate(texts), total=len(texts)):#TODO deleta the forloop
|
198 |
+
with torch.no_grad():
|
199 |
+
|
200 |
+
inputs = tokenizer(texts, return_tensors='pt')
|
201 |
+
inputs = inputs.to(model.device)
|
202 |
+
|
203 |
+
seq_length = inputs['input_ids'].shape[-1]
|
204 |
+
|
205 |
+
neg_log_prob_temp = 0
|
206 |
+
# for begin in range(0, seq_length, chunk_size):
|
207 |
+
input_chunk = inputs['input_ids'][:, :chunk_size]
|
208 |
+
|
209 |
+
logit = model.forward(input_ids=input_chunk).logits[0, :, :]
|
210 |
+
|
211 |
+
log_sum = calculate_log_sum(logit, input_chunk.squeeze(0))# suppose shape of logit is (seq_length, vocab_size),shape of input_chunk is (,seq_length)
|
212 |
+
neg_log_prob_temp += log_sum
|
213 |
+
|
214 |
+
loss = calculate_loss(logit, input_chunk.squeeze(0))
|
215 |
+
neg_log_prob_temp += log_sum
|
216 |
+
|
217 |
+
# token_length_list.append(seq_length)
|
218 |
+
# data.append(neg_log_prob_temp)
|
219 |
+
|
220 |
+
# data_dict = {
|
221 |
+
# 'neg_log_prob_sum': sum(data) / len(data),
|
222 |
+
# 'avg tokens': sum(token_length_list) / len(token_length_list),
|
223 |
+
# }
|
224 |
+
|
225 |
+
# print(f'log probability sum: {sum(data) / len(data):.2f}')
|
226 |
+
# print(f'avg tokens: {sum(token_length_list) / len(token_length_list):.0f}')
|
227 |
+
rtn_dic={'logit':logit,'input_ids':input_chunk,'loss':loss,'tokenizer':tokenizer,'neg_log_prob_temp':neg_log_prob_temp}
|
228 |
+
return rtn_dic
|
229 |
+
|
230 |
+
|
231 |
+
# if __name__ == '__main__':
|
232 |
+
# parser = argparse.ArgumentParser()
|
233 |
+
|
234 |
+
# parser.add_argument('--model', type=str, required=True, help='model name or path')
|
235 |
+
# parser.add_argument('--model_type', choices=['hf', 'rwkv', 'mamba', 'rwkv4pile'], required=True, help='model type')
|
236 |
+
# parser.add_argument('--data', type=str, required=True, help='data path (json file)')
|
237 |
+
# parser.add_argument('--log_path', type=str, default='./logs/', help='log file path')
|
238 |
+
# parser.add_argument('--model_cache', type=str, help='hugging face model cache')
|
239 |
+
# parser.add_argument('--chunk_size', type=int, default=1024, help='chunk size')
|
240 |
+
|
241 |
+
|
242 |
+
def run_get_loss(args):
|
243 |
+
if args is None:
|
244 |
+
args=SimpleNamespace(model='microsoft/phi-2',texts='Hello FreshBench !',model_type='hf',model_cache=None,chunk_size=1024)
|
245 |
+
|
246 |
+
# args = parser.parse_args()
|
247 |
+
|
248 |
+
# load data
|
249 |
+
# texts = load_list_from_json(args.data)
|
250 |
+
texts=args.texts
|
251 |
+
print(f'data size: {len(texts)}')
|
252 |
+
|
253 |
+
# load model
|
254 |
+
if args.model_type == 'hf':
|
255 |
+
model, tokenizer = load_hf_model(args.model, args.model_cache)# tokenzier path, model path
|
256 |
+
# elif args.model_type == 'rwkv':
|
257 |
+
# model, tokenizer = load_rwkv(args.model)
|
258 |
+
# elif args.model_type == 'mamba':
|
259 |
+
# model, tokenizer = load_mamba(args.model)
|
260 |
+
# elif args.model_type == 'rwkv4pile':
|
261 |
+
# model, tokenizer = load_rwkv4pile(args.model)
|
262 |
+
else:
|
263 |
+
raise NotImplementedError
|
264 |
+
|
265 |
+
# eval
|
266 |
+
if args.model_type in ['hf', 'mamba']:
|
267 |
+
return eval_hf_model(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size)
|
268 |
+
# elif args.model_type == 'rwkv':
|
269 |
+
# return eval_rwkv(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size)
|
270 |
+
# elif args.model_type == 'rwkv4pile':
|
271 |
+
# return eval_rwkv(model=model, tokenizer=tokenizer, texts=texts, chunk_size=args.chunk_size, v4pile=True)
|
272 |
+
else:
|
273 |
+
raise NotImplementedError
|
274 |
+
|
275 |
+
# results['model_name_or_path'] = args.model
|
276 |
+
# results['data_path'] = args.data
|
277 |
+
# results['chunk_size'] = args.chunk_size
|
278 |
+
|
279 |
+
# make_log(results, args.log_path)
|
280 |
+
|
281 |
+
# print(json.dumps(results, indent=4, ensure_ascii=False))
|
282 |
+
|
283 |
+
|
284 |
+
if __name__ == '__main__':
|
285 |
+
args=SimpleNamespace(model='microsoft/phi-2',texts='Hello FreshBench !',model_type='hf',model_cache=None,chunk_size=1024)
|
286 |
+
run_get_loss(args)
|
287 |
+
|
288 |
+
# run_get_loss(args)
|
get_loss/{my_geyt.py → my_get_logit.py}
RENAMED
File without changes
|