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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# SGL-new

This repository is a cleaned, submission-oriented copy of the SGL codebase for TextVQA large-only experiments:

1. `InternVL2-2B` large-only
2. `InternVL2-8B` large-only
3. `InternVL2-26B` large-only
4. `2B vision + 1B mlp1 + 1B language model` hybrid checkpoint large-only
5. `2B vision + 8B mlp1 + 8B language model` hybrid checkpoint large-only
6. `2B vision + 26B mlp1 + 26B language model` hybrid checkpoint large-only

The repository does **not** include checkpoints or datasets. The intended workflow is:

1. create an environment
2. place checkpoints under `checkpoints/`
3. prepare TextVQA data under `data/`
4. optionally build the hybrid checkpoint
5. run one of the experiment launch scripts


## 1. Repository Structure

Main experiment scripts:

- `textvqa2B-largeonly.sh`
- `textvqa8B-largeonly.sh`
- `textvqa26B-largeonly.sh`
- `textvqaHybrid-2Bvision-1Bllm-largeonly.sh`
- `textvqaHybrid-2Bvision-8Bllm-largeonly.sh`
- `textvqaHybrid-2Bvision-26Bllm-largeonly.sh`
- `run_textvqa_three_largeonly.sh`
- `run_textvqa_five_largeonly.sh`
- `train_textvqaHybrid-2Bvision-26Bllm-mlp.sh`

Core evaluation code:

- `eval/vqa/run_single_model_native.py`

Native single-model helpers:

- `eval/vqa/run_single_model_native.py`
- `eval/vqa/run_full_textvqa_native.sh`

Utility scripts:

- `tools/prepare_textvqa_for_sgl.py`
- `tools/build_hybrid_checkpoint.py`
- `build_hybrid_checkpoint_2bvision_1bllm.sh`
- `tools/hybrid_single_infer.py`
- `tools/train_hybrid_textvqa_mlp.py`
- `build_hybrid_checkpoint_2bvision_26bllm.sh`

Environment helper:

- `setup_sgl_2b_env.sh`


## 2. Environment Setup

This repo expects Python 3.10 and a CUDA-enabled PyTorch installation.

### Option A: manual setup

```bash
conda create -y -n sgl-new python=3.10
conda activate sgl-new

pip install --upgrade pip

# Install torch/torchvision matching your CUDA version.
# Example for CUDA 12.1:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121

pip install -r requirements.txt
```

### Option B: helper script

```bash
bash setup_sgl_2b_env.sh sgl-new
conda activate sgl-new

# Then install torch/torchvision matching your CUDA version.
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
```

### Notes

- `flash-attn` is optional. The code can run without it, but may be slower.
- The large-only launchers now call Python directly and optionally shard a model with `device_map`.
- If `transformers` or `torch` versions are changed substantially, verify that `InternVL` remote-code loading still works.


## 3. Checkpoint Layout

Create a directory:

```bash
mkdir -p checkpoints
```

Place checkpoints under `checkpoints/` with these names:

- `checkpoints/models--OpenGVLab--InternVL2-1B`
- `checkpoints/models--OpenGVLab--InternVL2-2B`
- `checkpoints/models--OpenGVLab--InternVL2-8B`
- `checkpoints/models--OpenGVLab--InternVL2-26B`

The hybrid checkpoint will be created at:

- `checkpoints/InternVL2-1B_2Bvision_hybrid`
- `checkpoints/InternVL2-8B_2Bvision_hybrid`
- `checkpoints/InternVL2-26B_2Bvision_hybrid`

If you want to use a different checkpoint layout, override `CHECKPOINT_ROOT` or `CHECKPOINT` when launching.


## 4. TextVQA Data Preparation

This repo expects SGL-style TextVQA files under:

- `data/textvqa/textvqa_train.jsonl`
- `data/textvqa/textvqa_val.jsonl`
- `data/textvqa/textvqa_val_questions.json`
- `data/textvqa/textvqa_val_annotations.json`

The repo does **not** ship the dataset.

### 4.1 Download the official TextVQA data

Prepare:

- `TextVQA_0.5.1_train.json`
- `TextVQA_0.5.1_val.json`
- `TextVQA_0.5.1_test.json`
- training/validation images
- test images

Place them under:

```text
data/textvqa_official/
├── TextVQA_0.5.1_train.json
├── TextVQA_0.5.1_val.json
├── TextVQA_0.5.1_test.json
├── train_images/
└── test_images/
```

### 4.2 Convert official data to SGL format

From the repo root:

```bash
python tools/prepare_textvqa_for_sgl.py \
  --official-root data/textvqa_official \
  --output-root data/textvqa
```

This script:

- creates `data/textvqa/*.jsonl`
- creates `textvqa_val_questions.json`
- creates `textvqa_val_annotations.json`
- symlinks `train_images` and `test_images` into `data/textvqa/`


## 5. Building Hybrid Checkpoints

### 5.1 2B vision + 1B LLM hybrid

The hybrid experiment means:

- `vision_model` from `InternVL2-2B`
- `mlp1` from `InternVL2-1B`
- `language_model` from `InternVL2-1B`

Use the convenience wrapper:

```bash
bash build_hybrid_checkpoint_2bvision_1bllm.sh
```

Equivalent manual command:

```bash
python tools/build_hybrid_checkpoint.py \
  --base-checkpoint checkpoints/models--OpenGVLab--InternVL2-1B \
  --vision-checkpoint checkpoints/models--OpenGVLab--InternVL2-2B \
  --output-dir checkpoints/InternVL2-1B_2Bvision_hybrid
```

### 5.2 2B vision + 8B LLM hybrid

The hybrid experiment means:

- `vision_model` from `InternVL2-2B`
- `mlp1` from `InternVL2-8B`
- `language_model` from `InternVL2-8B`

In this repo, the reproducible builder is:

- `tools/build_hybrid_checkpoint.py`

Run:

```bash
python tools/build_hybrid_checkpoint.py \
  --base-checkpoint checkpoints/models--OpenGVLab--InternVL2-8B \
  --vision-checkpoint checkpoints/models--OpenGVLab--InternVL2-2B \
  --output-dir checkpoints/InternVL2-8B_2Bvision_hybrid
```

This script starts from the 8B checkpoint, replaces its `vision_model` weights with the 2B `vision_model`, and saves a new merged checkpoint.

### 5.3 2B vision + 26B LLM hybrid

Use the convenience wrapper:

```bash
bash build_hybrid_checkpoint_2bvision_26bllm.sh
```

Equivalent manual command:

```bash
python tools/build_hybrid_checkpoint.py \
  --base-checkpoint checkpoints/models--OpenGVLab--InternVL2-26B \
  --vision-checkpoint checkpoints/models--OpenGVLab--InternVL2-2B \
  --output-dir checkpoints/InternVL2-26B_2Bvision_hybrid
```


## 6. How the Experiments Map to Code

### 6.1 InternVL2-2B large-only

Launcher:

- `textvqa2B-largeonly.sh`

Core code path:

- `eval/vqa/run_single_model_native.py --mode textvqa_eval`

Default checkpoint:

- `checkpoints/models--OpenGVLab--InternVL2-2B`

Run:

```bash
bash textvqa2B-largeonly.sh
```

Optional overrides:

```bash
CHECKPOINT_ROOT=/path/to/checkpoints \
OUT_DIR=/path/to/output \
GPUS_PER_MODEL=1 \
bash textvqa2B-largeonly.sh
```


### 6.2 InternVL2-8B large-only

Launcher:

- `textvqa8B-largeonly.sh`

Core code path:

- `eval/vqa/run_single_model_native.py --mode textvqa_eval`

Default checkpoint:

- `checkpoints/models--OpenGVLab--InternVL2-8B`

Run:

```bash
bash textvqa8B-largeonly.sh
```

Optional overrides:

```bash
CHECKPOINT_ROOT=/path/to/checkpoints \
OUT_DIR=/path/to/output \
GPUS_PER_MODEL=1 \
bash textvqa8B-largeonly.sh
```

### 6.3 InternVL2-26B large-only

Launcher:

- `textvqa26B-largeonly.sh`

Core code path:

- `eval/vqa/run_single_model_native.py --mode textvqa_eval`

Default checkpoint:

- `checkpoints/models--OpenGVLab--InternVL2-26B`

Run:

```bash
bash textvqa26B-largeonly.sh
```

Optional overrides:

```bash
CUDA_VISIBLE_DEVICES=0,1 \
CHECKPOINT_ROOT=/path/to/checkpoints \
OUT_DIR=/path/to/output \
GPUS_PER_MODEL=2 \
bash textvqa26B-largeonly.sh
```

### 6.4 2B vision + 1B mlp1 + 1B language model large-only

Launcher:

- `textvqaHybrid-2Bvision-1Bllm-largeonly.sh`

Core code path:

- `eval/vqa/run_single_model_native.py --mode textvqa_eval`

Hybrid builder:

- `build_hybrid_checkpoint_2bvision_1bllm.sh`
- `tools/build_hybrid_checkpoint.py`

Default checkpoint:

- `checkpoints/InternVL2-1B_2Bvision_hybrid`

Run:

```bash
bash textvqaHybrid-2Bvision-1Bllm-largeonly.sh
```

Optional overrides:

```bash
CHECKPOINT_ROOT=/path/to/checkpoints \
OUT_DIR=/path/to/output \
GPUS_PER_MODEL=1 \
bash textvqaHybrid-2Bvision-1Bllm-largeonly.sh
```

### 6.5 2B vision + 8B mlp1 + 8B language model large-only

Launcher:

- `textvqaHybrid-2Bvision-8Bllm-largeonly.sh`

Core code path:

- `eval/vqa/run_single_model_native.py --mode textvqa_eval`

Hybrid builder:

- `tools/build_hybrid_checkpoint.py`

Default checkpoint:

- `checkpoints/InternVL2-8B_2Bvision_hybrid`

Run:

```bash
bash textvqaHybrid-2Bvision-8Bllm-largeonly.sh
```

Optional overrides:

```bash
CHECKPOINT_ROOT=/path/to/checkpoints \
OUT_DIR=/path/to/output \
GPUS_PER_MODEL=1 \
bash textvqaHybrid-2Bvision-8Bllm-largeonly.sh
```

### 6.6 2B vision + 26B mlp1 + 26B language model large-only

Launcher:

- `textvqaHybrid-2Bvision-26Bllm-largeonly.sh`

Core code path:

- `eval/vqa/run_single_model_native.py --mode textvqa_eval`

Hybrid builder:

- `build_hybrid_checkpoint_2bvision_26bllm.sh`
- `tools/build_hybrid_checkpoint.py`

Default checkpoint:

- `checkpoints/InternVL2-26B_2Bvision_hybrid`

Run:

```bash
bash textvqaHybrid-2Bvision-26Bllm-largeonly.sh
```

Optional overrides:

```bash
CUDA_VISIBLE_DEVICES=0,1 \
CHECKPOINT_ROOT=/path/to/checkpoints \
OUT_DIR=/path/to/output \
GPUS_PER_MODEL=2 \
bash textvqaHybrid-2Bvision-26Bllm-largeonly.sh
```

### 6.7 Optional CoT-style reasoning

The native and hybrid inference entry points now support optional reasoning modes:

- `--reasoning-mode none`: default single-pass decoding
- `--reasoning-mode prompt`: adds an internal "think step by step" instruction in one pass
- `--reasoning-mode two_pass`: first generates explicit reasoning, then compresses it into the final short answer

If you do not set `REASONING_MODE` or `--reasoning-mode`, the code stays on the original normal inference path.

For the hybrid TextVQA launchers, use environment variables:

```bash
REASONING_MODE=two_pass \
REASONING_MAX_NEW_TOKENS=64 \
SAVE_REASONING=1 \
bash textvqaHybrid-2Bvision-8Bllm-largeonly.sh
```

For the shared-vision launcher:

```bash
REASONING_MODE=two_pass \
REASONING_MAX_NEW_TOKENS=64 \
SAVE_REASONING=1 \
bash textvqaSharedVision-2Bguide-8Btext.sh
```

To let the small guide model produce a short text hint for the large decoder:

```bash
GUIDE_TEXT_MODE=short_rationale \
GUIDE_TEXT_MAX_NEW_TOKENS=12 \
bash textvqaSharedVision-2Bguide-8Btext.sh
```

To force a short CoT on the guide branch so its generation changes the visual-token attention scores:

```bash
GUIDE_REASONING_MODE=short_cot \
GUIDE_REASONING_MAX_NEW_TOKENS=1024 \
bash textvqaSharedVision-2Bguide-8Btext.sh
```

Both options can be enabled together.

For single-image hybrid debugging:

```bash
python tools/hybrid_single_infer.py \
  --vision-checkpoint checkpoints/models--OpenGVLab--InternVL2-2B \
  --language-checkpoint checkpoints/models--OpenGVLab--InternVL2-8B \
  --image-path /path/to/image.jpg \
  --prompt "What is the brand name on the sign?" \
  --reasoning-mode two_pass \
  --reasoning-max-new-tokens 64 \
  --answer-format-prompt "Answer the question using a single word or phrase."
```


## 7. Running Sequential Launchers

Use:

```bash
bash run_textvqa_three_largeonly.sh
```

Default output root:

- `outputs/textvqa_three_largeonly`

This script runs:

1. 2B
2. 8B
3. hybrid 2B-vision + 8B-LLM

each with its own output subdirectory and launcher log.

To run all five experiments, use:

```bash
bash run_textvqa_five_largeonly.sh
```

This script adds:

1. 26B
2. hybrid 2B-vision + 26B-LLM


## 8. Minimal Hybrid Fine-Tuning On TextVQA

For a lightweight experiment, this repo also includes a minimal script that:

1. builds `2B vision + 26B mlp1 + 26B language_model`
2. freezes everything except `mlp1`
3. trains on TextVQA jsonl
4. runs validation inference immediately after training

Launcher:

- `train_textvqaHybrid-2Bvision-26Bllm-mlp.sh`

Core code:

- `tools/train_hybrid_textvqa_mlp.py`

Default demo dataset:

- `/home/yf/snap/SGL_yf/data/textvqa_demo_backup/textvqa_train.jsonl`
- `/home/yf/snap/SGL_yf/data/textvqa_demo_backup/textvqa_val.jsonl`

Run:

```bash
bash train_textvqaHybrid-2Bvision-26Bllm-mlp.sh
```

Important assumptions:

- `UPSTREAM_SGL_ROOT` defaults to `/home/yf/snap/SGL` because this script reuses the upstream `internvl` package.
- The default launcher expects local checkpoints at:
  - `/root/model_ckpts/models--OpenGVLab--InternVL2-2B`
  - `/root/model_ckpts/models--OpenGVLab--InternVL2-26B`
- The minimal implementation currently supports `batch_size=1`.


## 9. Native Single-Model Inference Utilities

These are not required for the main large-only experiments, but they are included because they are useful for debugging and single-sample inspection.

### Single sample or single question

Code:

- `eval/vqa/run_single_model_native.py`

Example:

```bash
python eval/vqa/run_single_model_native.py \
  --checkpoint checkpoints/models--OpenGVLab--InternVL2-2B \
  --mode single \
  --image-path /path/to/image.jpg \
  --prompt "What is written on the sign?" \
  --max-new-tokens 32 \
  --dynamic
```

### Full TextVQA native evaluation for 2B and 8B

Code:

- `eval/vqa/run_full_textvqa_native.sh`

Example:

```bash
bash eval/vqa/run_full_textvqa_native.sh outputs/native_eval
```


## 10. Hybrid Single-Sample Debugging Utility

Code:

- `tools/hybrid_single_infer.py`

Example:

```bash
python tools/hybrid_single_infer.py \
  --vision-checkpoint checkpoints/models--OpenGVLab--InternVL2-2B \
  --language-checkpoint checkpoints/models--OpenGVLab--InternVL2-8B \
  --image-path /path/to/image.jpg \
  --prompt "What is written on the sign?" \
  --dynamic
```

This script does **not** require a saved hybrid checkpoint. It builds the hybrid model in memory for single-sample inspection.


## 11. Output Files

The large-only evaluation script writes outputs under the launcher-provided output directory.

Typical files include one JSON results file per run inside the launcher-provided output directory.


## 12. Minimal Reproduction Checklist

For someone receiving this repository, the minimal steps are:

1. create a Python environment
2. install `torch`, `torchvision`, and `requirements.txt`
3. download `InternVL2-2B`, `InternVL2-8B`, and optionally `InternVL2-26B` into `checkpoints/`
4. download official TextVQA into `data/textvqa_official/`
5. run `python tools/prepare_textvqa_for_sgl.py`
6. run `python tools/build_hybrid_checkpoint.py`
7. run one of:
   - `bash textvqa2B-largeonly.sh`
   - `bash textvqa8B-largeonly.sh`
   - `bash textvqa26B-largeonly.sh`
   - `bash textvqaHybrid-2Bvision-8Bllm-largeonly.sh`
   - `bash textvqaHybrid-2Bvision-26Bllm-largeonly.sh`


## 13. Important Assumptions

- The code assumes CUDA is available for model inference.
- The code assumes TextVQA data is prepared under `data/textvqa/`.
- The code assumes checkpoints are available under `checkpoints/` unless overridden.
- All large-only experiments use the same evaluation implementation:
  `eval/vqa/run_single_model_native.py --mode textvqa_eval`
- `InternVL2-26B` and the `2B vision + 26B LLM` hybrid usually require multiple visible GPUs.