-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomprehensive_json_benchmark.py
More file actions
556 lines (432 loc) · 20.2 KB
/
Copy pathcomprehensive_json_benchmark.py
File metadata and controls
556 lines (432 loc) · 20.2 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/env python3
"""
Comprehensive JSON Generation Benchmark
Tests prefilled-json stop token approach across different JSON complexity scenarios:
1. Simple fields (few parameters)
2. Nested objects
3. Complicated field names
4. Mixed scenarios
Tests both speed and accuracy with Qwen2-1.5B-Instruct and attempts quantized Gemma.
"""
import time
import json
import statistics
import sys
import os
from typing import Dict, List, Any, Tuple, Optional
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def test_simple_fields_scenario():
"""Test 1: Simple fields - just a few basic parameters."""
try:
from vllm import LLM, SamplingParams
from driver.stop_token_json_driver import StopTokenJsonDriver, STOP_TOKEN_EXCELLENT
print("🧪 SCENARIO 1: SIMPLE FIELDS (Few Parameters)")
print("=" * 70)
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
max_model_len=256,
gpu_memory_utilization=0.5,
enable_prefix_caching=True,
trust_remote_code=True
)
def vllm_generate_func(prompt: str, stop_token: str = None) -> str:
stop_list = [stop_token] if stop_token else None
params = SamplingParams(
temperature=0.3,
max_tokens=15,
stop=stop_list,
skip_special_tokens=True
)
outputs = llm.generate([prompt], params)
return outputs[0].outputs[0].text.strip()
model_config = STOP_TOKEN_EXCELLENT.get("Qwen/Qwen2-1.5B-Instruct", {
"stop_tokens": [",", "}", "\n"],
"stop_reliable": True
})
driver = StopTokenJsonDriver(vllm_generate_func, model_config)
# Test different simple field scenarios
test_cases = [
# Very simple - 2 fields
([{"name": "string"}, {"age": "number"}], "2 fields"),
# Simple - 3 fields
([{"id": "number"}, {"email": "string"}, {"active": "string"}], "3 fields"),
# Medium simple - 4 fields
([{"username": "string"}, {"score": "number"}, {"level": "string"}, {"points": "number"}], "4 fields")
]
results = {}
for fields, description in test_cases:
print(f"\nTesting {description}: {fields}")
times = []
valid_count = 0
field_accuracy = []
for run in range(3):
start = time.time()
try:
result = driver.generate_json(fields)
elapsed = time.time() - start
times.append(elapsed)
# Validate JSON and field accuracy
parsed = json.loads(result)
valid_count += 1
expected_fields = set(list(field.keys())[0] for field in fields)
actual_fields = set(parsed.keys())
accuracy = len(expected_fields & actual_fields) / len(expected_fields)
field_accuracy.append(accuracy)
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid JSON, {accuracy:.1%} field accuracy")
except Exception as e:
elapsed = time.time() - start
times.append(elapsed)
field_accuracy.append(0.0)
print(f" Run {run+1}: {elapsed:.3f}s ❌ Error: {e}")
results[description] = {
"avg_time": statistics.mean(times),
"validity_rate": valid_count / len(times),
"avg_accuracy": statistics.mean(field_accuracy),
"times": times
}
return results
except Exception as e:
print(f"❌ Simple fields test failed: {e}")
return {}
def test_nested_objects_scenario():
"""Test 2: Nested objects - complex hierarchical structures."""
try:
from vllm import LLM, SamplingParams
from driver.stop_token_json_driver import StopTokenJsonDriver, STOP_TOKEN_EXCELLENT
print("\n🧪 SCENARIO 2: NESTED OBJECTS (Complex Hierarchy)")
print("=" * 70)
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
max_model_len=512, # Larger for nested structures
gpu_memory_utilization=0.5,
enable_prefix_caching=True,
trust_remote_code=True
)
def vllm_generate_func(prompt: str, stop_token: str = None) -> str:
stop_list = [stop_token] if stop_token else None
params = SamplingParams(
temperature=0.3,
max_tokens=20,
stop=stop_list,
skip_special_tokens=True
)
outputs = llm.generate([prompt], params)
return outputs[0].outputs[0].text.strip()
model_config = STOP_TOKEN_EXCELLENT.get("Qwen/Qwen2-1.5B-Instruct", {})
driver = StopTokenJsonDriver(vllm_generate_func, model_config)
# Test different nested scenarios
test_cases = [
# Simple nesting - 1 level
([
{"user": {"name": "string", "id": "number"}},
{"status": "string"}
], "1-level nesting"),
# Medium nesting - 2 levels
([
{"user": {
"profile": {"name": "string", "age": "number"},
"settings": {"theme": "string"}
}},
{"timestamp": "string"}
], "2-level nesting"),
# Complex nesting - multiple objects
([
{"customer": {"name": "string", "tier": "string"}},
{"order": {"id": "number", "total": "number"}},
{"metadata": {"created": "string", "source": "string"}}
], "multiple nested")
]
results = {}
for fields, description in test_cases:
print(f"\nTesting {description}:")
print(f"Structure: {fields}")
times = []
valid_count = 0
for run in range(3):
start = time.time()
try:
result = driver.generate_json(fields)
elapsed = time.time() - start
times.append(elapsed)
# Validate nested JSON structure
parsed = json.loads(result)
valid_count += 1
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid nested JSON")
print(f" Keys: {list(parsed.keys())}")
except Exception as e:
elapsed = time.time() - start
times.append(elapsed)
print(f" Run {run+1}: {elapsed:.3f}s ❌ Error: {e}")
results[description] = {
"avg_time": statistics.mean(times),
"validity_rate": valid_count / len(times),
"times": times
}
return results
except Exception as e:
print(f"❌ Nested objects test failed: {e}")
return {}
def test_complicated_field_names_scenario():
"""Test 3: Complicated field names - edge cases and difficult naming."""
try:
from vllm import LLM, SamplingParams
from driver.stop_token_json_driver import StopTokenJsonDriver, STOP_TOKEN_EXCELLENT
print("\n🧪 SCENARIO 3: COMPLICATED FIELD NAMES")
print("=" * 70)
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
max_model_len=256,
gpu_memory_utilization=0.5,
enable_prefix_caching=True,
trust_remote_code=True
)
def vllm_generate_func(prompt: str, stop_token: str = None) -> str:
stop_list = [stop_token] if stop_token else None
params = SamplingParams(
temperature=0.3,
max_tokens=15,
stop=stop_list,
skip_special_tokens=True
)
outputs = llm.generate([prompt], params)
return outputs[0].outputs[0].text.strip()
model_config = STOP_TOKEN_EXCELLENT.get("Qwen/Qwen2-1.5B-Instruct", {})
driver = StopTokenJsonDriver(vllm_generate_func, model_config)
# Test different complicated field naming scenarios
test_cases = [
# Snake case
([{"first_name": "string"}, {"last_name": "string"}, {"phone_number": "string"}], "snake_case"),
# CamelCase
([{"firstName": "string"}, {"lastName": "string"}, {"phoneNumber": "string"}], "camelCase"),
# Long descriptive names
([{"customerSupportTicketId": "number"}, {"priorityClassificationLevel": "string"}, {"issueResolutionTimestamp": "string"}], "long descriptive"),
# Mixed conventions and numbers
([{"user_id_v2": "number"}, {"apiKey": "string"}, {"last_updated_timestamp": "string"}], "mixed conventions"),
# Abbreviations and acronyms
([{"uuid": "string"}, {"api_endpoint_url": "string"}, {"http_status_code": "number"}], "abbreviations")
]
results = {}
for fields, description in test_cases:
print(f"\nTesting {description}: {[list(f.keys())[0] for f in fields]}")
times = []
valid_count = 0
field_accuracy = []
for run in range(3):
start = time.time()
try:
result = driver.generate_json(fields)
elapsed = time.time() - start
times.append(elapsed)
# Validate JSON and check field name accuracy
parsed = json.loads(result)
valid_count += 1
expected_fields = set(list(field.keys())[0] for field in fields)
actual_fields = set(parsed.keys())
# Check exact field name match
exact_match = expected_fields == actual_fields
field_accuracy.append(1.0 if exact_match else 0.0)
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid JSON, exact fields: {'✅' if exact_match else '❌'}")
if not exact_match:
print(f" Expected: {expected_fields}")
print(f" Actual: {actual_fields}")
except Exception as e:
elapsed = time.time() - start
times.append(elapsed)
field_accuracy.append(0.0)
print(f" Run {run+1}: {elapsed:.3f}s ❌ Error: {e}")
results[description] = {
"avg_time": statistics.mean(times),
"validity_rate": valid_count / len(times),
"exact_field_accuracy": statistics.mean(field_accuracy),
"times": times
}
return results
except Exception as e:
print(f"❌ Complicated field names test failed: {e}")
return {}
def test_quantized_gemma_model():
"""Test 4: Try quantized Gemma model if available."""
print("\n🧪 SCENARIO 4: QUANTIZED GEMMA MODEL")
print("=" * 70)
# Try different Gemma model variants
gemma_models = [
"google/gemma-2b",
"google/gemma-2b-it",
"unsloth/gemma-2b-bnb-4bit",
"unsloth/gemma-2b-it-bnb-4bit"
]
for model_name in gemma_models:
print(f"\nTrying {model_name}...")
try:
from vllm import LLM, SamplingParams
from driver.stop_token_json_driver import StopTokenJsonDriver
llm = LLM(
model=model_name,
max_model_len=256,
gpu_memory_utilization=0.4, # Conservative for quantized
enable_prefix_caching=True,
trust_remote_code=True
)
print(f"✅ {model_name} loaded successfully!")
def vllm_generate_func(prompt: str, stop_token: str = None) -> str:
stop_list = [stop_token] if stop_token else None
params = SamplingParams(
temperature=0.3,
max_tokens=10,
stop=stop_list,
skip_special_tokens=True
)
outputs = llm.generate([prompt], params)
return outputs[0].outputs[0].text.strip()
# Test with a simple JSON
model_config = {"stop_tokens": [",", "}", "\n"], "stop_reliable": True}
driver = StopTokenJsonDriver(vllm_generate_func, model_config)
test_fields = [{"name": "string"}, {"age": "number"}]
times = []
valid_count = 0
for run in range(3):
start = time.time()
try:
result = driver.generate_json(test_fields)
elapsed = time.time() - start
times.append(elapsed)
parsed = json.loads(result)
valid_count += 1
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid JSON: {result}")
except Exception as e:
elapsed = time.time() - start
times.append(elapsed)
print(f" Run {run+1}: {elapsed:.3f}s ❌ Error: {e}")
avg_time = statistics.mean(times)
validity_rate = valid_count / len(times)
print(f"📊 {model_name} Results:")
print(f" Average time: {avg_time:.3f}s")
print(f" Validity rate: {validity_rate:.1%}")
if validity_rate > 0.5:
print(f"🎉 {model_name} works with stop tokens!")
return {
"model": model_name,
"avg_time": avg_time,
"validity_rate": validity_rate,
"working": True
}
else:
print(f"⚠️ {model_name} has reliability issues")
except Exception as e:
print(f"❌ {model_name} failed: {e}")
continue
print("❌ No working quantized Gemma models found")
return {"working": False}
def compare_scenarios_with_simple_prompting():
"""Compare our scenarios against simple prompting approach."""
print("\n🧪 COMPARISON: PREFILLED-JSON vs SIMPLE PROMPTING")
print("=" * 70)
try:
from vllm import LLM, SamplingParams
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
max_model_len=256,
gpu_memory_utilization=0.5,
enable_prefix_caching=True,
trust_remote_code=True
)
# Test simple prompting on our test cases
simple_prompts = [
# Simple fields
"Generate JSON with name (string) and age (number):",
# Nested
"Generate JSON with user object containing name and id, plus status:",
# Complicated fields
"Generate JSON with first_name, last_name, and phone_number:"
]
print("Simple prompting results:")
simple_results = []
for i, prompt in enumerate(simple_prompts):
print(f"\nPrompt {i+1}: {prompt}")
params = SamplingParams(temperature=0.3, max_tokens=50, skip_special_tokens=True)
times = []
valid_count = 0
for run in range(3):
start = time.time()
outputs = llm.generate([prompt], params)
elapsed = time.time() - start
result = outputs[0].outputs[0].text.strip()
times.append(elapsed)
try:
json.loads(result)
valid_count += 1
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid")
except:
print(f" Run {run+1}: {elapsed:.3f}s ❌ Invalid JSON")
simple_results.append({
"avg_time": statistics.mean(times),
"validity_rate": valid_count / len(times)
})
return simple_results
except Exception as e:
print(f"❌ Simple prompting comparison failed: {e}")
return []
def main():
print("🔬 Comprehensive JSON Generation Benchmark")
print("Testing prefilled-json across different complexity scenarios")
print("=" * 80)
# Run all test scenarios
simple_results = test_simple_fields_scenario()
nested_results = test_nested_objects_scenario()
complicated_results = test_complicated_field_names_scenario()
# Try quantized Gemma
gemma_results = test_quantized_gemma_model()
# Compare with simple prompting
simple_prompting_results = compare_scenarios_with_simple_prompting()
# Final analysis
print("\n📊 COMPREHENSIVE ANALYSIS")
print("=" * 80)
# Aggregate prefilled-json results
all_prefilled_times = []
all_prefilled_validity = []
for scenario_name, results in [
("Simple Fields", simple_results),
("Nested Objects", nested_results),
("Complicated Names", complicated_results)
]:
if results:
print(f"\n{scenario_name}:")
for test_name, data in results.items():
validity = data.get("validity_rate", 0)
avg_time = data.get("avg_time", 0)
all_prefilled_times.append(avg_time)
all_prefilled_validity.append(validity)
print(f" {test_name}: {avg_time:.3f}s, {validity:.1%} valid")
# Overall metrics
if all_prefilled_times and all_prefilled_validity:
overall_prefilled_time = statistics.mean(all_prefilled_times)
overall_prefilled_validity = statistics.mean(all_prefilled_validity)
print(f"\n🎯 OVERALL PREFILLED-JSON PERFORMANCE:")
print(f" Average time: {overall_prefilled_time:.3f}s")
print(f" Average validity: {overall_prefilled_validity:.1%}")
# Compare with simple prompting if available
if simple_prompting_results:
simple_avg_time = statistics.mean([r["avg_time"] for r in simple_prompting_results])
simple_avg_validity = statistics.mean([r["validity_rate"] for r in simple_prompting_results])
speed_ratio = simple_avg_time / overall_prefilled_time
print(f"\n📈 vs SIMPLE PROMPTING:")
print(f" Speed advantage: {speed_ratio:.1f}x faster")
print(f" Reliability advantage: {overall_prefilled_validity - simple_avg_validity:+.1%}")
# Gemma results
if gemma_results.get("working"):
print(f"\n🎉 GEMMA MODEL SUCCESS:")
print(f" Model: {gemma_results['model']}")
print(f" Performance: {gemma_results['avg_time']:.3f}s, {gemma_results['validity_rate']:.1%} valid")
else:
print(f"\n❌ GEMMA MODELS: Not available or incompatible")
# Final recommendations
print(f"\n💡 RECOMMENDATIONS:")
if overall_prefilled_validity > 0.8:
print(f" ✅ Prefilled-JSON is highly reliable across all scenarios")
if len(all_prefilled_times) > 0 and max(all_prefilled_times) - min(all_prefilled_times) < 0.1:
print(f" ✅ Performance is consistent across complexity levels")
print(f" 🎯 Best for: Production JSON APIs requiring guaranteed structure")
print(f" ⚡ Works well with: Qwen2-1.5B-Instruct and stop tokens")
if __name__ == "__main__":
main()