-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_models.py
executable file
·320 lines (259 loc) · 11.5 KB
/
test_models.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
import time
import json
import sys
import requests
import argparse
import subprocess
import traceback
from datauri import DataURI
from openai import OpenAI
import torch
# tests are configured with model_conf_tests.json
all_results = []
urls = {
'tree': 'https://images.freeimages.com/images/large-previews/e59/autumn-tree-1408307.jpg',
'waterfall': 'https://images.freeimages.com/images/large-previews/242/waterfall-1537490.jpg',
'horse': 'https://images.freeimages.com/images/large-previews/5fa/attenborough-nature-reserve-1398791.jpg',
'leaf': 'https://images.freeimages.com/images/large-previews/cd7/gingko-biloba-1058537.jpg',
}
quality_urls = {
'98.21': ('What is the total bill?', 'https://ocr.space/Content/Images/receipt-ocr-original.webp'),
'walmart': ('What store is the receipt from?', 'https://ocr.space/Content/Images/receipt-ocr-original.webp'),
}
no_image = {
'5': 'In the integer sequence: 1, 2, 3, 4, ... What number comes next after 4?'
}
green_pass = '\033[92mpass\033[0m✅'
red_fail = '\033[91mfail\033[0m❌'
def data_url_from_url(img_url: str) -> str:
response = requests.get(img_url)
img_data = response.content
content_type = response.headers['content-type']
return str(DataURI.make(mimetype=content_type, charset='utf-8', base64=True, data=img_data))
def ready():
client = OpenAI(base_url='http://localhost:5006/v1', api_key='skip')
try:
return len(client.models.list(timeout=1.0).data) > 0
except:
return False
def record_result(cmd_args, results, t, mem, note):
# update all_results with the test data
all_results.extend([{
'args': cmd_args,
'results': results,
'time': t,
'mem': mem,
'note': note
}])
result = all(results)
print(f"#CLI_COMMAND=\"python vision.py -m {' '.join(cmd_args)}\" # test {green_pass if result else red_fail}, time: {t:.1f}s, mem: {mem:.1f}GB, {note}")
torch_memory_baseline = 0
def get_total_gpu_mem_used():
device_count = torch.cuda.device_count()
total_mem_used = 0.0
for i in range(device_count):
allocated_memory, total_memory, = torch.cuda.mem_get_info(device=i)
total_mem_used += total_memory - allocated_memory
return total_mem_used / (1024 ** 3) - torch_memory_baseline # convert bytes to gigabytes
torch_memory_baseline = get_total_gpu_mem_used()
print(f"Baseline CUDA memory: {torch_memory_baseline:.1f}GB")
def test(cmd_args: list[str]) -> int:
print(f"### Test start")
print("Launching server", end='', flush=True)
proc = subprocess.Popen(['python', 'vision.py', '--log-level', args.log_level, '-m'] + cmd_args,
stdout=subprocess.DEVNULL if args.quiet else sys.stdout,
stderr=subprocess.DEVNULL if args.quiet else sys.stderr)
note = ''
results = []
timeout = time.time() + 600
while not ready():
try:
proc.communicate(timeout=0)
except:
pass
if proc.returncode is not None:
note = 'Error: Server failed to start (exit).'
record_result(cmd_args, [False], -1, -1, note)
print(f"\n{note}\nResult: fail\n\n### Test end")
return -1
elif time.time() > timeout:
print("Startup Timeout, killing server.", end='', flush=True)
note = 'Error: Server failed to start (timeout).'
record_result(cmd_args, [False], -1, -1, note)
proc.kill()
proc.wait()
print(f"\n{note}\nResult: fail\n\n### Test end")
return -1
print(".", end='', flush=True)
time.sleep(1)
print("Server Alive, starting test.\n\n###")
t = time.time()
try:
results, timing = single_round()
except Exception as e:
traceback.print_exc()
note = f'Test failed with Exception: {e}'
print(f"{note}")
results = [False]
timing = []
t = time.time() - t
mem = get_total_gpu_mem_used()
result = all(results)
if not note:
note = f'{results.count(True)}/{len(results)} tests passed'
if timing:
tok_total, tim_total = 0, 0.0
for tok, tim in timing:
if tok > 1 and tim > 0:
tok_total += tok
tim_total += tim
if tim_total > 0.0:
note += f', ({tok_total}/{tim_total:0.1f}s) {tok_total/tim_total:0.1f} T/s'
print(f"\n\n###\n\nTest complete.\nResult: {green_pass if result else red_fail}, time: {t:.1f}s")
record_result(cmd_args, results, t, mem, note)
print("Stopping server", end='', flush=True)
try:
proc.communicate(timeout=0)
except:
pass
proc.kill()
proc.wait()
while proc.returncode is None:
print(".", end='', flush=True)
time.sleep(1)
print(f"\n### Test end")
return 0 if result else 1
if __name__ == '__main__':
# Initialize argparse
parser = argparse.ArgumentParser(description='Test vision using OpenAI')
parser.add_argument('-s', '--system-prompt', type=str, default=None)
parser.add_argument('-m', '--max-tokens', type=int, default=None)
parser.add_argument('-t', '--temperature', type=float, default=None)
parser.add_argument('-p', '--top_p', type=float, default=None)
parser.add_argument('-v', '--verbose', action='store_true', help="Verbose")
parser.add_argument('--abort-on-fail', action='store_true', help="Abort testing on fail.")
parser.add_argument('--quiet', action='store_true', help="Less test noise.")
parser.add_argument('-L', '--log-level', default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], help="Set the log level")
args = parser.parse_args()
client = OpenAI(base_url='http://localhost:5006/v1', api_key='skip')
params = {}
if args.max_tokens is not None:
params['max_tokens'] = args.max_tokens
if args.temperature is not None:
params['temperature'] = args.temperature
if args.top_p is not None:
params['top_p'] = args.top_p
def generate_response(image_url, prompt):
messages = [{ "role": "system", "content": [{ 'type': 'text', 'text': args.system_prompt }] }] if args.system_prompt else []
messages.extend([
{ "role": "user", "content": [
{ "type": "image_url", "image_url": { "url": image_url } },
{ "type": "text", "text": prompt },
]}])
response = client.chat.completions.create(model="gpt-4-vision-preview", messages=messages, **params)
answer = response.choices[0].message.content
tok = response.usage.completion_tokens
return answer, tok
def generate_stream_response(image_url, prompt):
messages = [{ "role": "system", "content": [{ 'type': 'text', 'text': args.system_prompt }] }] if args.system_prompt else []
messages.extend([
{ "role": "user", "content": [
{ "type": "image_url", "image_url": { "url": image_url } },
{ "type": "text", "text": prompt },
]}])
response = client.chat.completions.create(model="gpt-4-vision-preview", messages=messages, **params, stream=True)
answer = ''
completion_tokens = 0
for chunk in response:
if chunk.choices[0].delta.content:
answer += chunk.choices[0].delta.content
if chunk.usage:
completion_tokens = chunk.usage.completion_tokens
return answer, completion_tokens
def single_round():
# XXX TODO: timeout
results = []
### Single round
timing = []
def single_test(url, question, label, generator=generate_response):
tps_time = time.time()
answer, tok = generator(url, question)
tps_time = time.time() - tps_time
correct = name in answer.lower()
results.extend([correct])
if not correct:
print(f"{name}[{label}]: fail, got: {answer}")
#if args.abort_on_fail:
# break
else:
print(f"{name}[{label}]: pass{', got: ' + answer if args.verbose else ''}")
if tok > 1:
timing.extend([(tok, tps_time)])
# url tests
for name, url in urls.items():
single_test(url, "What is the subject of the image?", "url", generate_response)
data_url = data_url_from_url(url)
single_test(data_url, "What is the subject of the image?", "data", generate_response)
single_test(data_url, "What is the subject of the image?", "data_stream", generate_stream_response)
"""
## OCR tests
quality_urls = {
'98.21': ('What is the total bill?', 'https://ocr.space/Content/Images/receipt-ocr-original.webp'),
'walmart': ('What store is the receipt from?', 'https://ocr.space/Content/Images/receipt-ocr-original.webp'),
}
for name, question in quality_urls.items():
prompt, data_url = question
single_test(data_url, prompt, "quality", generate_stream_response)
"""
# No image tests
no_image = {
'5': 'In the sequence of numbers: 1, 2, 3, 4, ... What number comes next after 4?'
}
def no_image_response(prompt):
messages = [{ "role": "system", "content": [{ 'type': 'text', 'text': args.system_prompt }] }] if args.system_prompt else []
messages.extend([{ "role": "user", "content": prompt }])
response = client.chat.completions.create(model="gpt-4-vision-preview", messages=messages, **params, max_tokens=5)
answer = response.choices[0].message.content
return answer
for name, prompt in no_image.items():
answer = no_image_response(prompt)
correct = True #name in answer.lower() # - no exceptions is enough.
results.extend([correct])
if not correct:
print(f"{name}[no_img]: fail, got: {answer}")
if args.abort_on_fail:
break
else:
print(f"{name}[no_img]: pass{', got: ' + answer if args.verbose else ''}")
return results, timing
with open('model_conf_tests.json') as f:
model_tests = json.load(f)
print(f"### Begin tests. test count: {len(model_tests)}")
try:
for i, cmd_args in enumerate(model_tests):
print(f"### Test {i+1}/{len(model_tests)}: {cmd_args}")
ret = test(cmd_args)
if ret != 0 and args.abort_on_fail:
print(f"### Test {i+1}/{len(model_tests)} Failed.")
break
except:
import traceback
traceback.print_exc()
print(f"### Aborting due to Exception at test {len(all_results) + 1}/{len(model_tests)}")
print(f"### End tests.")
fname = f"sample.env-{time.time()}"
with open(fname,'w') as results_file:
print("""# This sample env file can be used to set environment variables for the docker-compose.yml
# Copy this file to vision.env and uncomment the model of your choice.
HF_HOME=hf_home
HF_HUB_ENABLE_HF_TRANSFER=1
#HF_TOKEN=hf-...
#CUDA_VISIBLE_DEVICES=1,0
#OPENEDAI_DEVICE_MAP="sequential"
""", file=results_file)
for r in all_results:
cmdl = ' '.join(r['args'])
result = all(r['results'])
print(f"#CLI_COMMAND=\"python vision.py -m {cmdl}\" # test {green_pass if result else red_fail}, time: {r['time']:.1f}s, mem: {r['mem']:.1f}GB, {r['note']}", file=results_file)
print(open(fname,'r').read())