-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverification_vllm.py
More file actions
356 lines (296 loc) · 11.5 KB
/
verification_vllm.py
File metadata and controls
356 lines (296 loc) · 11.5 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
"""
vLLM benchmark comparing standard inference vs speculative decoding.
Uses same evaluation metrics as verification_benchmark.py.
"""
import os
os.environ["VLLM_USE_V1"] = "0"
os.environ["CUDA_VISIBLE_DEVICES"] = "3" # Set GPU device
import argparse
import json
from typing import List, Dict, Tuple, Any, Optional
from pathlib import Path
from dataclasses import dataclass, asdict
import torch
from transformers import AutoTokenizer
# Import vLLM inference functions
from src.baselines.vllm_inference import (
run_vllm_standard_inference,
run_vllm_speculative_inference
)
from src.utils.data_loader import json_loader
@dataclass
class VerificationResult:
"""Result of comparing outputs against ground truth."""
method: str
model_pair: str
batch_size: int
exact_matches: int
partial_match_score: float # Average fraction of tokens matching up to first difference
total_prompts: int
@property
def exact_match_rate(self) -> float:
return self.exact_matches / self.total_prompts if self.total_prompts > 0 else 0.0
@property
def formatted_result(self) -> str:
"""Format as 'exact_count / partial_percentage' for table."""
return f"{self.exact_matches} / {self.partial_match_score:.1%}"
MODEL_CONFIGS = {
"qwen": {
"target": "Qwen/Qwen3-8B",
"draft": "Qwen/Qwen3-0.6B",
"name": "Qwen"
},
"vicuna": {
"target": "lmsys/vicuna-7b-v1.3",
"draft": "double7/vicuna-68m",
"name": "Vicuna"
},
"glm4": {
"target": "zai-org/GLM-4-9B-0414",
"draft": "jukofyork/GLM-4.5-DRAFT-0.6B-v3.0",
"name": "GLM4"
}
}
def load_prompts(input_file: str, num_prompts: Optional[int] = None) -> List[str]:
"""Load prompts from JSON file."""
data = json_loader(input_file)
prompts = []
for item in data:
if isinstance(item, dict):
if 'turns' in item and item['turns']:
# Use first turn as the prompt
prompts.append(item['turns'][0])
elif 'question' in item:
prompts.append(item['question'])
elif 'prompt' in item:
prompts.append(item['prompt'])
else:
prompts.append(str(item))
if num_prompts is not None:
prompts = prompts[:num_prompts]
print(f"Loaded {len(prompts)} prompts from {input_file}")
return prompts
def compare_outputs(
outputs: List[str],
ground_truth: List[str],
prompts: List[str],
tokenizer: Any
) -> Tuple[int, float]:
"""
Compare outputs against ground truth.
Returns (exact_matches, partial_match_score).
partial_match_score is the average fraction of matching tokens up to first difference.
"""
exact_matches = 0
partial_match_scores = []
for i, (output, gt_output) in enumerate(zip(outputs, ground_truth)):
# Check exact match
if output == gt_output:
exact_matches += 1
partial_match_scores.append(1.0) # Exact match is 100% partial match
else:
# Check partial match (up to first differing token)
# Tokenize both outputs
output_tokens = tokenizer.encode(output, add_special_tokens=False)
gt_tokens = tokenizer.encode(gt_output, add_special_tokens=False)
# Find first difference
min_len = min(len(output_tokens), len(gt_tokens))
matches_up_to = 0
for j in range(min_len):
if output_tokens[j] == gt_tokens[j]:
matches_up_to += 1
else:
break
# Calculate partial match score as fraction of ground truth tokens matched
if len(gt_tokens) > 0:
partial_score = matches_up_to / len(gt_tokens)
else:
partial_score = 0.0
partial_match_scores.append(partial_score)
# Calculate average partial match score
avg_partial_score = sum(partial_match_scores) / len(partial_match_scores) if partial_match_scores else 0.0
return exact_matches, avg_partial_score
def run_vllm_benchmark(
model_pair: str,
prompts: List[str],
max_new_tokens: int,
n_draft_tokens: int,
output_dir: str
) -> List[VerificationResult]:
"""Run vLLM benchmark for a specific model pair."""
config = MODEL_CONFIGS[model_pair]
print(f"\n{'='*60}")
print(f"Processing {config['name']} models")
print(f"{'='*60}")
print(f"Target: {config['target']}")
print(f"Draft: {config['draft']}")
# Load tokenizer for comparison
tokenizer = AutoTokenizer.from_pretrained(config['target'])
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
results = []
# Run vLLM WITHOUT speculative decoding (ground truth)
print(f"\n1. Running vLLM standard inference (ground truth)...")
outputs_standard, time_standard, tps_standard, _ = run_vllm_standard_inference(
model_name=config['target'],
prompts=prompts,
max_new_tokens=max_new_tokens,
temperature=0.0
)
print(f" Time: {time_standard:.2f}s ({tps_standard:.2f} tokens/sec)")
# Run vLLM WITH speculative decoding
print(f"\n2. Running vLLM with speculative decoding...")
outputs_speculative, time_speculative, tps_speculative, _ = run_vllm_speculative_inference(
target_model=config['target'],
draft_model=config['draft'],
prompts=prompts,
max_new_tokens=max_new_tokens,
n_draft_tokens=n_draft_tokens,
temperature=0.0
)
print(f" Time: {time_speculative:.2f}s ({tps_speculative:.2f} tokens/sec)")
# Compare outputs (speculative vs standard as ground truth)
exact_matches, partial_match_score = compare_outputs(
outputs_speculative,
outputs_standard,
prompts,
tokenizer
)
# Create verification result for speculative decoding
spec_result = VerificationResult(
method="vLLM-Spec",
model_pair=model_pair,
batch_size=len(prompts), # vLLM handles batching internally
exact_matches=exact_matches,
partial_match_score=partial_match_score,
total_prompts=len(prompts)
)
results.append(spec_result)
# Create baseline result (standard vLLM is the ground truth, so perfect match)
standard_result = VerificationResult(
method="vLLM-Standard",
model_pair=model_pair,
batch_size=len(prompts),
exact_matches=len(prompts),
partial_match_score=1.0,
total_prompts=len(prompts)
)
results.append(standard_result)
# Print comparison
print(f"\n{'='*50}")
print("CORRECTNESS COMPARISON")
print(f"{'='*50}")
print(f"vLLM-Standard (baseline): {standard_result.formatted_result}")
print(f"vLLM-Speculative: {spec_result.formatted_result}")
# Performance comparison
print(f"\n{'='*50}")
print("PERFORMANCE COMPARISON")
print(f"{'='*50}")
print(f"vLLM-Standard: {time_standard:.2f}s ({tps_standard:.2f} tokens/sec)")
print(f"vLLM-Speculative: {time_speculative:.2f}s ({tps_speculative:.2f} tokens/sec)")
if time_speculative < time_standard:
speedup = time_standard / time_speculative
print(f"✓ Speculative decoding is {speedup:.2f}x faster")
else:
slowdown = time_speculative / time_standard
print(f"⚠ Speculative decoding is {slowdown:.2f}x slower")
# Save outputs for analysis
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
outputs_file = output_path / f"vllm_outputs_{model_pair}.json"
with open(outputs_file, 'w') as f:
json.dump({
"model_config": config,
"num_prompts": len(prompts),
"max_new_tokens": max_new_tokens,
"n_draft_tokens": n_draft_tokens,
"outputs": {
"prompts": prompts,
"standard": outputs_standard,
"speculative": outputs_speculative
},
"performance": {
"standard": {"time": time_standard, "tokens_per_sec": tps_standard},
"speculative": {"time": time_speculative, "tokens_per_sec": tps_speculative},
"speedup": time_standard / time_speculative if time_speculative > 0 else 0
},
"correctness": {
"exact_matches": exact_matches,
"partial_match_score": partial_match_score,
"total_prompts": len(prompts)
}
}, f, indent=2)
print(f"\nOutputs saved to: {outputs_file}")
return results
def main():
parser = argparse.ArgumentParser(description="vLLM verification benchmark")
# Data configuration
parser.add_argument("--input_file", type=str, default="data/spec_bench/question.jsonl",
help="Path to input JSON file with prompts")
parser.add_argument("--num_prompts", type=int, default=480,
help="Number of prompts to use (None for all)")
# Model configuration
parser.add_argument("--models", nargs="+", choices=list(MODEL_CONFIGS.keys()),
# default=["vicuna", "qwen", "glm4"],
default=["glm4"],
help="Model pairs to evaluate")
# Generation configuration
parser.add_argument("--max_new_tokens", type=int, default=50,
help="Maximum new tokens to generate")
parser.add_argument("--n_draft_tokens", type=int, default=5,
help="Number of draft tokens for speculative decoding")
# Output configuration
parser.add_argument("--output_dir", type=str, default="vllm_verification_results",
help="Directory for output files")
args = parser.parse_args()
print("=" * 80)
print("vLLM VERIFICATION BENCHMARK")
print("=" * 80)
print(f"Input file: {args.input_file}")
print(f"Models: {', '.join(args.models)}")
print(f"Max new tokens: {args.max_new_tokens}")
print(f"Draft tokens: {args.n_draft_tokens}")
# Load prompts
prompts = load_prompts(args.input_file, args.num_prompts)
# Store all results
all_results = []
# Process each model pair
for model_pair in args.models:
results = run_vllm_benchmark(
model_pair=model_pair,
prompts=prompts,
max_new_tokens=args.max_new_tokens,
n_draft_tokens=args.n_draft_tokens,
output_dir=args.output_dir
)
all_results.extend(results)
# Generate summary
print("\n" + "=" * 80)
print("SUMMARY")
print("=" * 80)
for model_pair in args.models:
model_name = MODEL_CONFIGS[model_pair]['name']
print(f"\n{model_name} Results:")
print("-" * 40)
for result in all_results:
if result.model_pair == model_pair:
print(f" {result.method:15s}: {result.formatted_result:10s} "
f"(exact: {result.exact_match_rate:6.1%}, "
f"partial: {result.partial_match_score:6.1%})")
# Save detailed results
output_path = Path(args.output_dir)
output_path.mkdir(parents=True, exist_ok=True)
results_file = output_path / "vllm_verification_results.json"
with open(results_file, 'w') as f:
json.dump({
"args": vars(args),
"num_prompts": len(prompts),
"results": [asdict(r) for r in all_results]
}, f, indent=2)
print(f"\nDetailed results saved to: {results_file}")
print("\n" + "=" * 80)
print("VERIFICATION COMPLETE")
print("=" * 80)
if __name__ == "__main__":
main()