-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
567 lines (463 loc) · 20.7 KB
/
test.py
File metadata and controls
567 lines (463 loc) · 20.7 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
557
558
559
560
561
562
563
564
565
566
567
"""
Universal Testing Script for Optical Compression
Test trained adapter on ANY VLM with optical compression.
Supports custom texts, files, or LongBench v2 benchmark.
Usage:
# Test on custom text
python test.py --vlm_model_path /path/to/Qwen3-VL-2B-Instruct \\
--adapter_checkpoint adapters/qwen3_vl_2b.pth \\
--text "Your long document here..."
# Test on file
python test.py --vlm_model_path /path/to/model \\
--adapter_checkpoint adapters/adapter.pth \\
--file document.txt
# Test on LongBench v2 (quick test)
python test.py --vlm_model_path /path/to/model \\
--adapter_checkpoint adapters/adapter.pth \\
--benchmark longbench \\
--num_samples 20
# Full LongBench v2 evaluation
python test.py --vlm_model_path /path/to/model \\
--adapter_checkpoint adapters/adapter.pth \\
--benchmark longbench \\
--full
"""
import torch
import sys
import os
import argparse
import time
import json
import re
from tqdm import tqdm
# Add paths
script_dir = os.path.dirname(os.path.abspath(__file__))
examples_dir = os.path.dirname(script_dir)
deepseek_dir = os.path.join(examples_dir, "initial", "models", "DeepSeek-OCR")
sys.path.insert(0, deepseek_dir)
sys.path.insert(0, script_dir)
from optical_encoder import OpticalEncoder, render_text_to_pages
def load_vlm_model(vlm_model_path, vlm_type='auto', quantization=None, device='cuda'):
"""Load VLM model (auto-detects type)"""
print(f"\nLoading VLM from: {vlm_model_path}")
if quantization:
print(f"Using {quantization.upper()} quantization")
if vlm_type == 'auto':
if 'qwen' in vlm_model_path.lower():
vlm_type = 'qwen3'
elif 'llava' in vlm_model_path.lower():
vlm_type = 'llava'
else:
print("⚠️ Could not auto-detect VLM type, assuming Qwen3-VL")
vlm_type = 'qwen3'
if vlm_type == 'qwen3':
from transformers import Qwen3VLForConditionalGeneration, AutoProcessor, BitsAndBytesConfig
quantization_config = None
if quantization == 'int4':
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4"
)
elif quantization == 'int8':
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = Qwen3VLForConditionalGeneration.from_pretrained(
vlm_model_path,
torch_dtype=torch.float16 if not quantization else None,
quantization_config=quantization_config,
device_map=device if not quantization else "auto",
trust_remote_code=True,
).eval()
processor = AutoProcessor.from_pretrained(
vlm_model_path,
trust_remote_code=True,
)
print(f"✓ Loaded Qwen3-VL model")
elif vlm_type == 'llava':
from transformers import LlavaForConditionalGeneration, AutoProcessor
model = LlavaForConditionalGeneration.from_pretrained(
vlm_model_path,
torch_dtype=torch.float16,
device_map=device,
).eval()
processor = AutoProcessor.from_pretrained(vlm_model_path)
print(f"✓ Loaded LLaVA model")
else:
raise ValueError(f"Unsupported VLM type: {vlm_type}")
return model, processor
@torch.no_grad()
def test_optical_compression(encoder, text, vlm_model, processor, question=None, device='cuda', max_pages=200):
"""
Test with optical compression
Args:
encoder: OpticalEncoder instance
text: Input text
vlm_model: VLM model
processor: VLM processor
question: Optional question to ask (for Q&A)
device: Device
max_pages: Max pages to render
Returns:
dict with answer, tokens, time
"""
# Render text to images
images = render_text_to_pages(text, font_size=10, img_size=1024, max_pages=max_pages)
num_pages = len(images)
# Encode with optical encoder
start = time.time()
vision_tokens = encoder(images) # [1, num_pages*256, target_dim]
encode_time = time.time() - start
# Prepare prompt
if question:
prompt = f"Based on the document shown in the image, answer the following question:\n\nQuestion: {question}\n\nAnswer:"
else:
prompt = "Summarize the document shown in the image."
messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
text_prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = processor(text=[text_prompt], padding=True, return_tensors="pt").to(device)
# Combine vision + text embeddings
input_ids = inputs['input_ids']
text_embeds = vlm_model.language_model.embed_tokens(input_ids)
combined_embeds = torch.cat([vision_tokens, text_embeds], dim=1)
vision_mask = torch.ones((1, vision_tokens.shape[1]), dtype=inputs['attention_mask'].dtype, device=device)
combined_mask = torch.cat([vision_mask, inputs['attention_mask']], dim=1)
# Generate
start = time.time()
output_ids = vlm_model.generate(
inputs_embeds=combined_embeds,
attention_mask=combined_mask,
max_new_tokens=512,
do_sample=False,
)
gen_time = time.time() - start
answer = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
if "assistant" in answer:
answer = answer.split("assistant")[-1].strip()
return {
'answer': answer,
'vision_tokens': vision_tokens.shape[1],
'text_tokens': input_ids.shape[1],
'total_tokens': vision_tokens.shape[1] + input_ids.shape[1],
'num_pages': num_pages,
'encode_time': encode_time,
'gen_time': gen_time,
'total_time': encode_time + gen_time,
}
@torch.no_grad()
def test_native_text(text, vlm_model, processor, question=None, device='cuda', max_chars=500000):
"""
Test with native text tokenization
Args:
text: Input text
vlm_model: VLM model
processor: VLM processor
question: Optional question
device: Device
max_chars: Max characters (for truncation)
Returns:
dict with answer, tokens, time
"""
# Truncate if needed
truncated = False
if len(text) > max_chars:
text = text[:max_chars]
truncated = True
# Prepare prompt
if question:
prompt = f"Context: {text}\n\nQuestion: {question}\n\nAnswer:"
else:
prompt = f"Context: {text}\n\nSummarize the context."
messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
text_prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = processor(text=[text_prompt], padding=True, return_tensors="pt").to(device)
# Generate
start = time.time()
output_ids = vlm_model.generate(
**inputs,
max_new_tokens=512,
do_sample=False,
)
gen_time = time.time() - start
answer = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
if "assistant" in answer:
answer = answer.split("assistant")[-1].strip()
return {
'answer': answer,
'total_tokens': inputs['input_ids'].shape[1],
'gen_time': gen_time,
'truncated': truncated,
}
def evaluate_accuracy(prediction, ground_truth):
"""Evaluate multiple choice accuracy (for LongBench)"""
pred_lower = prediction.lower().strip()
gt_lower = ground_truth.lower().strip()
# Exact match
if pred_lower == gt_lower:
return 1.0
# Check if answer letter appears
pattern = rf'\b{gt_lower}\b|\({gt_lower}\)|{gt_lower}\)'
if re.search(pattern, pred_lower, re.IGNORECASE):
return 1.0
return 0.0
def test_longbench_v2(encoder, vlm_model, processor, device='cuda', num_samples=None, full=False):
"""
Test on LongBench v2 benchmark
Args:
encoder: OpticalEncoder
vlm_model: VLM model
processor: VLM processor
device: Device
num_samples: Number of samples (None for full)
full: Run full 503 samples
Returns:
dict with results
"""
from datasets import load_dataset
print("\n" + "="*80)
print("LOADING LONGBENCH V2 DATASET")
print("="*80)
dataset = load_dataset("THUDM/LongBench-v2", split="train")
print(f"✓ Loaded {len(dataset)} samples")
# Select samples
if full:
samples = list(dataset)
num_samples = len(samples)
print(f"✓ Running FULL benchmark: {num_samples} samples")
elif num_samples:
# Balanced sampling
short_samples = [s for s in dataset if s.get('length') == 'short']
medium_samples = [s for s in dataset if s.get('length') == 'medium']
long_samples = [s for s in dataset if s.get('length') == 'long']
samples_per_cat = num_samples // 3
samples = (
short_samples[:samples_per_cat] +
medium_samples[:samples_per_cat] +
long_samples[:num_samples - 2*samples_per_cat]
)
print(f"✓ Quick test: {len(samples)} balanced samples")
else:
samples = list(dataset)[:20]
print(f"✓ Default: 20 samples")
# Run benchmark
print("\n" + "="*80)
print("RUNNING BENCHMARK")
print("="*80)
results = {
'optical': {'correct': 0, 'total': 0, 'failed': 0, 'total_tokens': 0, 'total_time': 0},
'native': {'correct': 0, 'total': 0, 'failed': 0, 'total_tokens': 0, 'total_time': 0},
}
for idx, sample in enumerate(tqdm(samples, desc="Evaluating")):
try:
context = sample['context']
question = sample['question']
choices = f"A. {sample['choice_A']}\nB. {sample['choice_B']}\nC. {sample['choice_C']}\nD. {sample['choice_D']}"
full_question = f"{question}\n\n{choices}\n\nAnswer (A/B/C/D):"
ground_truth = sample['answer']
torch.cuda.empty_cache()
# Test optical
try:
result_optical = test_optical_compression(
encoder, context, vlm_model, processor, full_question, device, max_pages=200
)
accuracy = evaluate_accuracy(result_optical['answer'], ground_truth)
results['optical']['correct'] += accuracy
results['optical']['total'] += 1
results['optical']['total_tokens'] += result_optical['total_tokens']
results['optical']['total_time'] += result_optical['total_time']
except Exception as e:
print(f"\n Optical error on sample {idx}: {e}")
results['optical']['failed'] += 1
torch.cuda.empty_cache()
# Test native
try:
result_native = test_native_text(
context, vlm_model, processor, full_question, device
)
accuracy = evaluate_accuracy(result_native['answer'], ground_truth)
results['native']['correct'] += accuracy
results['native']['total'] += 1
results['native']['total_tokens'] += result_native['total_tokens']
results['native']['total_time'] += result_native['gen_time']
except Exception as e:
# Native OOM/context exceeded is expected on long contexts
results['native']['failed'] += 1
except Exception as e:
print(f"\n Error on sample {idx}: {e}")
continue
return results
def print_results(results):
"""Print comparison results"""
print("\n" + "="*80)
print("RESULTS")
print("="*80)
# Calculate total samples for each method
opt_total_samples = results['optical']['total'] + results['optical']['failed']
nat_total_samples = results['native']['total'] + results['native']['failed']
if results['optical']['total'] > 0:
opt_acc = 100 * results['optical']['correct'] / results['optical']['total']
opt_avg_tokens = results['optical']['total_tokens'] / results['optical']['total']
opt_avg_time = results['optical']['total_time'] / results['optical']['total']
opt_success_rate = 100 * results['optical']['total'] / opt_total_samples
opt_overall_score = 100 * results['optical']['correct'] / opt_total_samples
print(f"\nOptical Compression:")
print(f" Overall Score: {opt_overall_score:.1f}% ({results['optical']['correct']}/{opt_total_samples} samples)")
print(f" Success rate: {opt_success_rate:.1f}% ({results['optical']['total']}/{opt_total_samples} samples completed)")
print(f" Accuracy on completed: {opt_acc:.1f}% ({results['optical']['correct']:.1f}/{results['optical']['total']})")
print(f" Failed: {results['optical']['failed']} samples")
print(f" Avg tokens: {opt_avg_tokens:,.0f}")
print(f" Avg time: {opt_avg_time:.2f}s")
if results['native']['total'] > 0:
nat_acc = 100 * results['native']['correct'] / results['native']['total']
nat_avg_tokens = results['native']['total_tokens'] / results['native']['total']
nat_avg_time = results['native']['total_time'] / results['native']['total']
nat_success_rate = 100 * results['native']['total'] / nat_total_samples
nat_overall_score = 100 * results['native']['correct'] / nat_total_samples
print(f"\nNative Text:")
print(f" Overall Score: {nat_overall_score:.1f}% ({results['native']['correct']}/{nat_total_samples} samples)")
print(f" Success rate: {nat_success_rate:.1f}% ({results['native']['total']}/{nat_total_samples} samples completed)")
print(f" Accuracy on completed: {nat_acc:.1f}% ({results['native']['correct']:.1f}/{results['native']['total']})")
print(f" Failed: {results['native']['failed']} samples (OOM/context exceeded)")
print(f" Avg tokens: {nat_avg_tokens:,.0f}")
print(f" Avg time: {nat_avg_time:.2f}s")
if results['optical']['total'] > 0 and results['native']['total'] > 0:
compression = nat_avg_tokens / opt_avg_tokens if opt_avg_tokens > 0 else 0
savings = nat_avg_tokens - opt_avg_tokens
print(f"\nCompression:")
print(f" Token savings: {savings:,.0f} tokens")
print(f" Compression ratio: {compression:.1f}×")
# Compare overall scores (failures = wrong)
overall_diff = opt_overall_score - nat_overall_score
print(f"\nOverall Score Comparison (failures = wrong):")
if abs(overall_diff) < 2:
print(f" Comparable (Δ{overall_diff:.1f}%)")
elif overall_diff > 0:
print(f" Optical is {overall_diff:.1f}% better overall!")
print(f" (Optical handles long documents that cause native OOM/context exceeded)")
else:
print(f" Native is {-overall_diff:.1f}% better overall")
def main():
parser = argparse.ArgumentParser(description="Universal Optical Compression Testing")
# Model configuration
parser.add_argument('--vlm_model_path', type=str, required=True,
help='Path to VLM model')
parser.add_argument('--vlm_type', type=str, default='auto',
choices=['auto', 'qwen3', 'llava'],
help='VLM type')
parser.add_argument('--adapter_checkpoint', type=str, required=True,
help='Path to trained adapter checkpoint')
parser.add_argument('--deepencoder_path', type=str, default=None,
help='Path to DeepEncoder weights')
parser.add_argument('--quantization', type=str, default=None,
choices=['int4', 'int8'],
help='Quantization type for large models (int4 recommended for 8B on 12GB GPU)')
# Test mode
parser.add_argument('--text', type=str, default=None,
help='Test on custom text')
parser.add_argument('--file', type=str, default=None,
help='Test on text file')
parser.add_argument('--benchmark', type=str, default=None,
choices=['longbench'],
help='Run benchmark')
parser.add_argument('--num_samples', type=int, default=None,
help='Number of benchmark samples')
parser.add_argument('--full', action='store_true',
help='Run full benchmark (all samples)')
parser.add_argument('--question', type=str, default=None,
help='Question to ask about text/file')
# Settings
parser.add_argument('--max_pages', type=int, default=200,
help='Max pages to render')
parser.add_argument('--device', type=str, default='cuda',
help='Device')
parser.add_argument('--output', type=str, default=None,
help='Output file for results (JSON)')
args = parser.parse_args()
print("="*80)
print("UNIVERSAL OPTICAL COMPRESSION TESTING")
print("="*80)
# Load VLM
print("\n" + "="*80)
print("LOADING MODELS")
print("="*80)
vlm_model, processor = load_vlm_model(args.vlm_model_path, args.vlm_type, args.quantization, args.device)
# Load OpticalEncoder with trained adapter
encoder = OpticalEncoder.from_pretrained(
vlm_model=vlm_model,
adapter_checkpoint=args.adapter_checkpoint,
deepencoder_path=args.deepencoder_path,
max_pages=args.max_pages,
device=args.device
)
print(f"\n✓ OpticalEncoder loaded with trained adapter")
print(f" - Target dimension: {encoder.target_dim}")
print(f" - Adapter params: {encoder.adapter.get_num_params():,}")
# Run tests
if args.benchmark == 'longbench':
# LongBench v2 benchmark
results = test_longbench_v2(
encoder, vlm_model, processor, args.device,
num_samples=args.num_samples,
full=args.full
)
print_results(results)
if args.output:
with open(args.output, 'w') as f:
json.dump(results, f, indent=2)
print(f"\n✓ Results saved to: {args.output}")
elif args.text or args.file:
# Custom text test
print("\n" + "="*80)
print("TESTING CUSTOM TEXT")
print("="*80)
if args.file:
with open(args.file, 'r') as f:
text = f.read()
print(f"\n✓ Loaded text from: {args.file}")
else:
text = args.text
print(f" - Text length: {len(text):,} chars")
# Test optical
print("\n[1/2] Testing with optical compression...")
result_optical = test_optical_compression(
encoder, text, vlm_model, processor, args.question, args.device, args.max_pages
)
print(f"\nOptical Compression:")
print(f" Answer: {result_optical['answer'][:200]}...")
print(f" Vision tokens: {result_optical['vision_tokens']:,}")
print(f" Text tokens: {result_optical['text_tokens']:,}")
print(f" Total tokens: {result_optical['total_tokens']:,}")
print(f" Pages: {result_optical['num_pages']}")
print(f" Time: {result_optical['total_time']:.2f}s")
# Test native
print("\n[2/2] Testing with native text...")
try:
result_native = test_native_text(
text, vlm_model, processor, args.question, args.device
)
print(f"\nNative Text:")
print(f" Answer: {result_native['answer'][:200]}...")
print(f" Total tokens: {result_native['total_tokens']:,}")
print(f" Time: {result_native['gen_time']:.2f}s")
print(f" Truncated: {result_native['truncated']}")
# Comparison
compression = result_native['total_tokens'] / result_optical['total_tokens']
print(f"\nCompression: {compression:.1f}×")
except Exception as e:
print(f"\n⚠️ Native text failed (expected for long contexts): {e}")
else:
print("\n❌ ERROR: Must specify --text, --file, or --benchmark")
print("\nExamples:")
print(" # Custom text")
print(" python test.py --vlm_model_path /path/to/model \\")
print(" --adapter_checkpoint adapters/adapter.pth \\")
print(" --text 'Your text here'")
print("\n # LongBench v2")
print(" python test.py --vlm_model_path /path/to/model \\")
print(" --adapter_checkpoint adapters/adapter.pth \\")
print(" --benchmark longbench --num_samples 20")
print("\n" + "="*80)
print("✓ TESTING COMPLETE")
print("="*80)
if __name__ == "__main__":
main()