-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfocused_complexity_benchmark.py
More file actions
356 lines (276 loc) · 13.6 KB
/
focused_complexity_benchmark.py
File metadata and controls
356 lines (276 loc) · 13.6 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
#!/usr/bin/env python3
"""
Focused Complexity Benchmark
Tests prefilled-json across different JSON complexity scenarios with conservative settings
to avoid CUDA compilation issues while demonstrating the library's capabilities.
"""
import time
import json
import statistics
import sys
import os
from typing import Dict, List, Any
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def run_single_test(driver, fields, description, max_runs=2):
"""Run a single test scenario with error handling."""
print(f"\nTesting {description}:")
print(f"Fields: {fields}")
times = []
valid_count = 0
field_accuracy = []
for run in range(max_runs):
start = time.time()
try:
result = driver.generate_json(fields)
elapsed = time.time() - start
times.append(elapsed)
# Validate JSON
parsed = json.loads(result)
valid_count += 1
# Check field accuracy
expected_fields = set()
for field in fields:
if isinstance(field, dict):
for key, value in field.items():
if isinstance(value, dict):
# Nested object
expected_fields.add(key)
else:
# Simple field
expected_fields.add(key)
actual_fields = set(parsed.keys())
accuracy = len(expected_fields & actual_fields) / len(expected_fields) if expected_fields else 1.0
field_accuracy.append(accuracy)
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid JSON, {accuracy:.1%} field accuracy")
print(f" Result: {result}")
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: {str(e)[:50]}...")
return {
"avg_time": statistics.mean(times) if times else 0,
"validity_rate": valid_count / len(times) if times else 0,
"avg_accuracy": statistics.mean(field_accuracy) if field_accuracy else 0,
"times": times
}
def test_complexity_scenarios():
"""Test different JSON complexity scenarios."""
try:
from vllm import LLM, SamplingParams
from driver.stop_token_json_driver import StopTokenJsonDriver, STOP_TOKEN_EXCELLENT
print("🔬 Focused Complexity Benchmark")
print("Testing prefilled-json across different JSON complexity scenarios")
print("=" * 80)
# Use conservative settings to avoid CUDA issues
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
max_model_len=128, # Very small
gpu_memory_utilization=0.4, # Conservative
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=10, # Small to avoid issues
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 scenarios with increasing complexity
test_scenarios = [
# Scenario 1: Simple fields (few parameters)
{
"name": "Simple Fields",
"tests": [
([{"name": "string"}], "1 field"),
([{"name": "string"}, {"age": "number"}], "2 fields"),
([{"id": "number"}, {"email": "string"}, {"active": "string"}], "3 fields")
]
},
# Scenario 2: Complicated field names
{
"name": "Complicated Field Names",
"tests": [
([{"first_name": "string"}, {"last_name": "string"}], "snake_case"),
([{"firstName": "string"}, {"lastName": "string"}], "camelCase"),
([{"user_id": "number"}, {"api_key": "string"}], "mixed_naming")
]
},
# Scenario 3: Nested objects (simplified)
{
"name": "Nested Objects",
"tests": [
([{"user": {"name": "string"}}], "1-level nested"),
([{"profile": {"id": "number", "name": "string"}}], "1-level multi-field")
]
}
]
all_results = {}
for scenario in test_scenarios:
print(f"\n🧪 SCENARIO: {scenario['name'].upper()}")
print("=" * 60)
scenario_results = {}
for fields, description in scenario["tests"]:
try:
result = run_single_test(driver, fields, description)
scenario_results[description] = result
except Exception as e:
print(f"❌ Test {description} failed completely: {e}")
scenario_results[description] = {"avg_time": 0, "validity_rate": 0, "avg_accuracy": 0}
all_results[scenario["name"]] = scenario_results
return all_results
except Exception as e:
print(f"❌ Complexity test failed: {e}")
return {}
def test_simple_prompting_comparison():
"""Compare with simple prompting approach."""
try:
from vllm import LLM, SamplingParams
print("\n🧪 SIMPLE PROMPTING COMPARISON")
print("=" * 60)
llm = LLM(
model="Qwen/Qwen2-1.5B-Instruct",
max_model_len=128,
gpu_memory_utilization=0.4,
enable_prefix_caching=True,
trust_remote_code=True
)
# Test simple prompting on basic scenarios
test_cases = [
("Generate JSON with name (string):", "simple"),
("Generate JSON with name and age:", "two_fields"),
("Generate JSON with user_id and email:", "naming_test")
]
simple_results = {}
for prompt, test_name in test_cases:
print(f"\nTesting {test_name}: {prompt}")
params = SamplingParams(temperature=0.3, max_tokens=30, skip_special_tokens=True)
times = []
valid_count = 0
for run in range(2): # Conservative number of runs
try:
start = time.time()
outputs = llm.generate([prompt], params)
elapsed = time.time() - start
result = outputs[0].outputs[0].text.strip()
times.append(elapsed)
# Try to parse as JSON
json.loads(result)
valid_count += 1
print(f" Run {run+1}: {elapsed:.3f}s ✅ Valid JSON")
print(f" Result: {result}")
except json.JSONDecodeError:
print(f" Run {run+1}: {elapsed:.3f}s ❌ Invalid JSON")
print(f" Result: {result[:50]}...")
except Exception as e:
elapsed = time.time() - start if 'start' in locals() else 0
times.append(elapsed)
print(f" Run {run+1}: {elapsed:.3f}s ❌ Error: {str(e)[:30]}...")
simple_results[test_name] = {
"avg_time": statistics.mean(times) if times else 0,
"validity_rate": valid_count / len(times) if times else 0
}
return simple_results
except Exception as e:
print(f"❌ Simple prompting comparison failed: {e}")
return {}
def analyze_results(prefilled_results, simple_results):
"""Analyze and compare results across scenarios."""
print("\n📊 COMPREHENSIVE ANALYSIS")
print("=" * 80)
# Aggregate prefilled-json results
all_prefilled_times = []
all_prefilled_validity = []
all_prefilled_accuracy = []
print("\nPrefilled-JSON Results by Scenario:")
for scenario_name, scenario_data in prefilled_results.items():
if scenario_data:
print(f"\n{scenario_name}:")
for test_name, data in scenario_data.items():
validity = data.get("validity_rate", 0)
avg_time = data.get("avg_time", 0)
accuracy = data.get("avg_accuracy", 0)
if avg_time > 0: # Only include successful tests
all_prefilled_times.append(avg_time)
all_prefilled_validity.append(validity)
all_prefilled_accuracy.append(accuracy)
print(f" {test_name}: {avg_time:.3f}s, {validity:.1%} valid, {accuracy:.1%} accurate")
# Simple prompting results
if simple_results:
print(f"\nSimple Prompting Results:")
simple_times = []
simple_validity = []
for test_name, data in simple_results.items():
validity = data.get("validity_rate", 0)
avg_time = data.get("avg_time", 0)
if avg_time > 0:
simple_times.append(avg_time)
simple_validity.append(validity)
print(f" {test_name}: {avg_time:.3f}s, {validity:.1%} valid")
# Overall comparison
if all_prefilled_times and simple_times:
print(f"\n🎯 OVERALL COMPARISON:")
prefilled_avg_time = statistics.mean(all_prefilled_times)
prefilled_avg_validity = statistics.mean(all_prefilled_validity)
prefilled_avg_accuracy = statistics.mean(all_prefilled_accuracy)
simple_avg_time = statistics.mean(simple_times)
simple_avg_validity = statistics.mean(simple_validity)
speed_ratio = simple_avg_time / prefilled_avg_time if prefilled_avg_time > 0 else 1.0
print(f" Prefilled-JSON: {prefilled_avg_time:.3f}s avg, {prefilled_avg_validity:.1%} valid, {prefilled_avg_accuracy:.1%} accurate")
print(f" Simple Prompting: {simple_avg_time:.3f}s avg, {simple_avg_validity:.1%} valid")
print(f" Speed Advantage: {speed_ratio:.1f}x {'faster' if speed_ratio > 1 else 'slower'}")
print(f" Reliability Advantage: {prefilled_avg_validity - simple_avg_validity:+.1%}")
# Complexity analysis
if prefilled_results:
print(f"\n🔧 COMPLEXITY ANALYSIS:")
# Check if performance degrades with complexity
if "Simple Fields" in prefilled_results and "Complicated Field Names" in prefilled_results:
simple_avg = statistics.mean([d["avg_time"] for d in prefilled_results["Simple Fields"].values() if d["avg_time"] > 0])
complex_avg = statistics.mean([d["avg_time"] for d in prefilled_results["Complicated Field Names"].values() if d["avg_time"] > 0])
if simple_avg > 0 and complex_avg > 0:
complexity_overhead = (complex_avg - simple_avg) / simple_avg * 100
print(f" Complexity overhead: {complexity_overhead:+.1f}% (complex vs simple fields)")
# Check nested object performance
if "Nested Objects" in prefilled_results:
nested_results = [d for d in prefilled_results["Nested Objects"].values() if d["validity_rate"] > 0]
if nested_results:
nested_success_rate = len(nested_results) / len(prefilled_results["Nested Objects"])
print(f" Nested object success rate: {nested_success_rate:.1%}")
# Final recommendations
print(f"\n💡 RECOMMENDATIONS:")
if all_prefilled_validity and statistics.mean(all_prefilled_validity) > 0.8:
print(f" ✅ Prefilled-JSON shows high reliability across complexity levels")
if simple_times and all_prefilled_times:
speed_advantage = statistics.mean(simple_times) / statistics.mean(all_prefilled_times)
if speed_advantage > 1.2:
print(f" ⚡ Prefilled-JSON is significantly faster than simple prompting")
elif speed_advantage > 1.0:
print(f" 🏃 Prefilled-JSON has speed advantages")
print(f" 🎯 Best use case: Structured JSON APIs requiring guaranteed field compliance")
print(f" 🔧 Works well with: Qwen2-1.5B-Instruct + stop tokens + prefix caching")
def main():
print("🔬 Focused Complexity Benchmark")
print("Testing prefilled-json with conservative settings to avoid CUDA issues")
print("=" * 80)
# Run main complexity tests
prefilled_results = test_complexity_scenarios()
# Run simple prompting comparison
simple_results = test_simple_prompting_comparison()
# Analyze results
if prefilled_results or simple_results:
analyze_results(prefilled_results, simple_results)
else:
print("❌ All tests failed - check VLLM setup and GPU memory")
print(f"\n🎉 Benchmark complete! Check results above for performance insights.")
if __name__ == "__main__":
main()