File size: 50,575 Bytes
e3cb0cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
import torch
import logging
import os
import numpy as np
import json
import random
import argparse
from datasets import Dataset
from typing import List, Union
import math
from PIL import Image
import math

def get_args():
    parser = argparse.ArgumentParser()
    # ===== Basic arguments =====
    parser.add_argument("--load_model_path", type=str, default='./checkpoints/model_sft_stage1')
    parser.add_argument("--data_path", type=str, default='PathToJsonlData', nargs='+')
    parser.add_argument("--stage", type=str, default="sft_stage1", choices=['sft_stage1', 'sft_stage2', 'sft_stage3'])
    parser.add_argument("--task", type=str, default="mm-reasoning", choices=["mm-reasoning"])
    parser.add_argument("--save_model_path", type=str, default='./checkpoints/',help="Path to save the model checkpoints.")
    parser.add_argument("--resume_from_checkpoint", default=False, action="store_true")
    parser.add_argument("--dataset_root", type=str, default="", help="Root directory for the dataset.")
    parser.add_argument("--deepspeed", type=str, default="./deepspeed/ds_zero2_gpu.json",
                        help="Path to DeepSpeed config JSON file")
    parser.add_argument("--num_samples", default=-1, help="-1 means all data", type=int)
    parser.add_argument("--max_seq_len", type=int, default=4096, help="Maximum allowed sequence length after processing.")
    parser.add_argument("--image_resize", type=str, choices=["global", "clear_question_img"], default="global")
    parser.add_argument("--save_freq", type=int, default=250)
    parser.add_argument("--log_freq", default=10, type=int)
    parser.add_argument("--allow_no_observation", action='store_true', default=False)
    # ===== Basic training hyperparameters =====
    parser.add_argument("--lr", type=float, default=1e-5, help="Learning rate for training.")
    parser.add_argument("--bsz", type=int, default=1, help="Batch size for training.")
    parser.add_argument("--grad_accum_steps", type=int, default=4, help="Gradient accumulation steps.")
    parser.add_argument("--epochs", type=int, default=10)
    parser.add_argument("--weight_decay", type=float, default=0.01,
                        help="AdamW weight decay. Default 0.01 == the value previously hardcoded in main.py SFTConfig.")
    parser.add_argument("--max_steps", type=int, default=-1,
                        help="If > 0, total optimizer steps (overrides --epochs); wired to TrainingArguments.max_steps. Default -1 = epochs-based (previous behavior).")
    parser.add_argument("--lr_scheduler_type", type=str, default="linear",
                        help="TrainingArguments.lr_scheduler_type (e.g. linear, cosine, cosine_with_min_lr). Default 'linear' == previous implicit HF default.")
    parser.add_argument("--shuffle_train", action='store_true', default=False, help="Whether to shuffle the training dataset.")

    # ===== Monet arguments =====
    parser.add_argument("--alignment", type=str, default="observation_all", choices=["observation_end", "boxed_start", "observation_all"], help="The alignment strategy for Monet.")
    parser.add_argument("--latent_size", type=int, default=4)
    parser.add_argument("--ce_emphasize_factor", default=1.0, type=float)
    parser.add_argument("--only_predict_obs", action='store_true', default=False)
    parser.add_argument("--alignment_weight", default=1.0, help="Weight of the alignment loss in SFT Stage 2 and SFT Stage 3.", type=float)
    parser.add_argument("--alignment_layer", choices=["all_layers", "last_layer"])
    parser.add_argument("--emphasize_latent_weight", default=1.0, type=float, help="Weight of the loss that only flow through latents in SFT Stage 2.")
    parser.add_argument("--sft_stage2_align_poss", default='obs', choices=['obs', 'latent_end'])
    parser.add_argument("--sft_stage2_global_img_tokens", type=int, help="Maximum img pixels in a sequence will be sft_stage2_global_max_img_tokens*28*28", default=1500)
    parser.add_argument("--sft_stage2_per_img_tokens", type=int, help="Maximum pixels per img will be sft_stage2_global_max_img_tokens*28*28", default=1280)
    parser.add_argument("--sft_stage3_img_tokens", type=int, help="Maximum img pixels in a sequence will be sft_stage3_max_img_tokens*28*28", default=2000)
    parser.add_argument("--stage3_input_img_mask_ratio", type=float, default=0.0,
                        help="Stage 3 only. DISABLED BY DEFAULT (0.0): with the default the "
                             "input-image attention mask is OFF and Stage 3 behaves exactly like "
                             "the original Monet (no mask). Opt in by passing a value > 0. "
                             "STATIC path (used when --stage3_img_mask_curriculum is off): MAX "
                             "fraction of input-image patch tokens blocked from answer-token rows "
                             "in Forward 2. Each batch samples the effective ratio uniformly from "
                             "[0, this value]. ratio=0 batches match the inference distribution "
                             "(no mask), ratio>0 batches force the answer head to lean on the "
                             "latent. Forward 1 (latent generation) always sees the full image.")
    # ---- Stage-3 input-image attention-mask CURRICULUM (decay the ratio over training) ----
    # OFF BY DEFAULT: this is an opt-in store_true flag (default False). When it is NOT passed
    # (and --stage3_input_img_mask_ratio stays 0.0), NO input-image attention mask is applied
    # and Stage 3 reproduces the original Monet behavior. Only the dedicated launch script
    # `sft_no_text_stage3_curriculum_multinode.sh` turns it on; the standard scripts do not.
    # Rationale (when enabled): at inference the model CAN see the input image, so a fixed
    # train-time mask creates a train/inference distribution gap. A curriculum that starts high
    # (force the answer head onto the latent early) and decays to 0 (final steps match inference
    # exactly) closes that gap. Applied in the trainer (it sees global_step / max_steps), so it
    # is correct even when the dataloader uses worker processes.
    parser.add_argument("--stage3_img_mask_curriculum", action='store_true', default=False,
                        help="Stage 3. DISABLED BY DEFAULT (opt-in flag): when omitted, no "
                             "input-image attention mask is applied (same as original Monet). "
                             "When set, enable curriculum decay of the input-image attention-mask "
                             "ratio from --stage3_img_mask_start to --stage3_img_mask_end over "
                             "training. Overrides the static --stage3_input_img_mask_ratio path. "
                             "Still an attention-edge mask (answer rows -> input-image patches); "
                             "image tokens / Forward 1 are never touched.")
    parser.add_argument("--stage3_img_mask_start", type=float, default=0.7,
                        help="Curriculum ratio at training start (global_step 0). Default 0.7.")
    parser.add_argument("--stage3_img_mask_end", type=float, default=0.0,
                        help="Curriculum ratio at training end (global_step == max_steps). "
                             "Default 0.0 so the final steps match inference (no mask).")
    parser.add_argument("--stage3_img_mask_schedule", type=str, default='linear',
                        choices=['linear', 'cosine'],
                        help="Curriculum decay shape from start->end. 'linear' or 'cosine' "
                             "(cosine holds high a bit longer, then drops faster). Default linear.")

    # ===== Training record arguments =====
    parser.add_argument("--log_file", type=str, default='./log.txt')
    parser.add_argument("--wandb_name", default=None, help="Name for the Weights & Biases run. If None, no W&B logging is done.")

    # ==== Custom attention =====
    parser.add_argument("--not_use_4d", action='store_true', default=False)
    parser.add_argument("--not_mask_image", action='store_true', default=False)
    parser.add_argument("--mask_latent", action='store_true', default=False,
                        help="If set, make latent tokens (A_i) invisible to all subsequent tokens in build_additive_bias.")
    parser.add_argument("--observation_tokens_only_see_image_tokens", action='store_true', default=False)
    parser.add_argument("--observation_tokens_only_see_latent_tokens", action='store_true', default=False)
    parser.add_argument("--observation_tokens_cannot_see_question_image", action='store_true', default=False)
    parser.add_argument("--latent_can_see_all_previous", action='store_true', default=True)
    parser.add_argument("--observation_tokens_only_see_question_and_latent", action='store_true', default=False)
    parser.add_argument("--mask_question_image", action='store_true', default=False)
    # ===== Precomputed teacher latent loading =====
    parser.add_argument("--teacher_latent_dir", type=str, default=None,
                        help="Directory that stores precomputed teacher latents (files named latent_{sample_id:08d}.pt). If not set, defaults to {save_model_path or ./checkpoints}/teacher_latents.")
    parser.add_argument("--teacher_reps_dir", type=str, default=None)
    parser.add_argument("--online_teacher", action='store_true', default=False,
                        help="Compute teacher reps in-process each step instead of loading from disk. "
                             "Falls back to offline `load_offline_tensor` when not set.")
    parser.add_argument("--online_teacher_model_path", type=str, default=None,
                        help="Model used as the online teacher. Stage 2: base ckpt; Stage 3: Stage 2 ckpt. "
                             "Required when --online_teacher is set.")
    parser.add_argument("--attn_analysis", action='store_true', default=False)
    parser.add_argument("--output_latent_embeds", action='store_true', default=False)
    parser.add_argument("--output_hidden_states", action='store_true', default=False)
    parser.add_argument("--resume", action="store_true", default=False)


    return parser.parse_args()

def seed_everything(seed: int = 42):
    """
    Set seed for reproducibility across random, numpy, torch, and environment.
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)  # for multi-GPU
    # Ensure deterministic behavior
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

def load_jsonl_dataset(jsonl_path):
    with open(jsonl_path, "r", encoding="utf-8") as f:
        data = [json.loads(line) for line in f]
        data = data[:]
    return data

def load_json_dataset(json_path):
    with open(json_path, "r", encoding="utf-8") as f:
        data = json.load(f)
    return data

def replace_latent_placeholder_with_img_pad(text, image_pad="<|vision_start|><|image_pad|><|vision_end|>", latent_placeholder="<abs_vis_token></abs_vis_token>", sep_token="<|im_start|>assistant") -> str:
    text = text.split(sep_token)
    res_text = process_multiple_question_img(text[0])
    assistant_texts = text[1:]
    for text in assistant_texts:
        if latent_placeholder in text:
            text = text.replace(image_pad, "")
            text = text.replace(latent_placeholder, image_pad)
        res_text += sep_token + text
    return res_text

def remove_auxiliary_images(examples):
    new_examples = []
    for example in examples:
        # `example` is a list of turn dicts
        new_example = []
        for turn in example:
            # Create a shallow copy of the turn so we don't modify the original
            new_turn = dict(turn)
            if turn.get("role") == "assistant":
                # Filter out image-type content
                new_turn["content"] = [
                    item for item in turn.get("content", [])
                    if item.get("type") != "image"
                ]
            # Add the updated turn to this new example
            new_example.append(new_turn)
        new_examples.append(new_example)

    return new_examples

def process_multiple_question_img(question_str):
    if "<abs_vis_token></abs_vis_token>" in question_str:
        question_str = question_str.replace("<|vision_start|><|image_pad|><|vision_end|>", "").replace("<abs_vis_token></abs_vis_token>", "<|vision_start|><|image_pad|><|vision_end|>")
    return question_str

def replace_img_pad_with_latent_pad(texts, latent_size, latent_pad_str="<abs_vis_token_pad>"):
    update_texts = []
    latent_pad_strs = latent_pad_str*latent_size
    for i, text in enumerate(texts):
        turns = text.split("<|im_start|>assistant")
        upd_text = process_multiple_question_img(turns[0])
        for turn in turns[1:]:
            upd_text += "<|im_start|>assistant" + turn.replace("<|vision_start|><|image_pad|><|vision_end|>", f"<abs_vis_token>{latent_pad_strs}</abs_vis_token>")
        update_texts.append(upd_text)
    return update_texts

def add_latent_pad_after_auxiliary_img(texts, latent_size, latent_pad_str="<abs_vis_token_pad>"):
    update_texts = []
    latent_pad_strs = latent_pad_str*latent_size
    for i, text in enumerate(texts):
        turns = text.split("<|im_start|>assistant")
        upd_text = process_multiple_question_img(turns[0])
        for turn in turns[1:]:
            upd_text += "<|im_start|>assistant" + turn.replace("<|vision_start|><|image_pad|><|vision_end|>", f"<|vision_start|><|image_pad|><|vision_end|><abs_vis_token>{latent_pad_strs}</abs_vis_token>")
        update_texts.append(upd_text)
    return update_texts

def find_subsequence(row: torch.Tensor, pattern: Union[torch.Tensor, List[torch.Tensor]], start: int=0) -> int:

    seq_len = row.size(0)
    # Naive scan over all possible start positions
    if isinstance(pattern, torch.Tensor):
        max_pat_len = pattern.size(0)
    elif isinstance(pattern, list):
        max_pat_len = max(pat.size(0) for pat in pattern)

    for start_idx in range(start, seq_len - max_pat_len + 1):
        # Compare row[start_idx : start_idx + pat_len] to pattern
        if isinstance(pattern, torch.Tensor):
            pat_len = pattern.size(0)
            if torch.all(row[start_idx : start_idx + pat_len] == pattern):
                return start_idx
        elif isinstance(pattern, list):
            for pat in pattern:
                if isinstance(pat, torch.Tensor):
                    pat_len = pat.size(0)
                    if torch.all(row[start_idx : start_idx + pat_len] == pat):
                        return start_idx

    return -1

def find_ids_poss(input_ids: torch.Tensor, answer_start_token_pattern: torch.Tensor, ids_tensor_or_list: Union[torch.Tensor,List[torch.Tensor]]) -> List[List[int]]:
    batch_poss = []
    for i in range(input_ids.shape[0]):
        manipulation_result_poss = []
        start_idx = find_subsequence(input_ids[i], answer_start_token_pattern, 0)
        while start_idx != -1:
            start_idx = find_subsequence(input_ids[i], ids_tensor_or_list, start_idx+1)
            if start_idx != -1:
                manipulation_result_poss.append(start_idx)
        manipulation_result_poss = manipulation_result_poss[:]
        batch_poss.append(manipulation_result_poss)
    return batch_poss

def generate_labels_after_multi_token_start(
    input_ids: torch.Tensor,
    start_sequence: torch.Tensor,
    ignore_ids: List[int] = None
) -> torch.Tensor:
    """
    For each row in `input_ids`, find the *first* occurrence of `start_sequence`
    (a 1D tensor of multiple token IDs). Mask all tokens up to and including
    that entire sub-sequence (set them to -100), and also mask any padding tokens
    anywhere in the row. The remainder (tokens *after* the sub-sequence) are kept.

    Args:
      input_ids: 2D tensor [batch_size, seq_len].
      start_sequence: 1D tensor of shape [k], the multi-token "start" pattern.
      pad_token_id: which ID is used as padding (default=0).

    Returns:
      labels: a new 2D tensor [batch_size, seq_len], where tokens before (and
              including) the sub-sequence are -100, as well as any pad tokens,
              and tokens after the sub-sequence are kept as in `input_ids`.
    """
    batch_size, seq_len = input_ids.shape

    # Clone so we can modify in-place
    labels = input_ids.clone()

    for b in range(batch_size):
        row = labels[b]
        # Find first occurrence of the entire sub-sequence
        start_idx = find_subsequence(row, start_sequence)

        if start_idx == -1:
            # Sub-sequence not found -> mask everything
            logging.warning(f"Couldn't find the <|im_start|>assistant, all labels are -100")
            row[:] = -100
        else:
            # The sub-sequence length
            sub_len = start_sequence.size(0)
            end_of_subseq = start_idx + sub_len  # the position *after* the sub-sequence

            # Mask everything up to (and including) the sub-sequence
            row[:end_of_subseq] = -100

        for id in ignore_ids:
            # Mask specified tokens (<|endoftext|>, <|vision_start|>, <|image_pad|>, <|vision_end|>)
            row[row == id] = -100



    return labels

def generate_labels_after_multi_token_start_only_allow(
    input_ids: torch.Tensor,
    start_sequence: torch.Tensor,
    allowed_poss: List[List[int]] = None
) -> torch.Tensor:
    """
    For each row in `input_ids`, find the *first* occurrence of `start_sequence`
    (a 1D tensor of multiple token IDs). Mask all tokens up to and including
    that entire sub-sequence (set them to -100), and also mask any padding tokens
    anywhere in the row. The remainder (tokens *after* the sub-sequence) are kept.

    Args:
      input_ids: 2D tensor [batch_size, seq_len].
      start_sequence: 1D tensor of shape [k], the multi-token "start" pattern.
      pad_token_id: which ID is used as padding (default=0).

    Returns:
      labels: a new 2D tensor [batch_size, seq_len], where tokens before (and
              including) the sub-sequence are -100, as well as any pad tokens,
              and tokens after the sub-sequence are kept as in `input_ids`.
    """
    batch_size, seq_len = input_ids.shape

    # Clone so we can modify in-place
    labels = input_ids.clone()

    for b in range(batch_size):
        row = labels[b]
        # Find first occurrence of the entire sub-sequence
        start_idx = find_subsequence(row, start_sequence)

        if start_idx == -1:
            # Sub-sequence not found -> mask everything
            logging.warning(f"Couldn't find the <|im_start|>assistant, all labels are -100")
            row[:] = -100
        else:
            # The sub-sequence length
            sub_len = start_sequence.size(0)
            end_of_subseq = start_idx + sub_len  # the position *after* the sub-sequence

            # Mask everything up to (and including) the sub-sequence
            row[:end_of_subseq] = -100

        mask = torch.ones_like(row, dtype=torch.bool)
        allowed_pos = allowed_poss[b]
        mask[allowed_pos] = False
        row[mask] = -100

    return labels

def resize_by_token_budget(images,
                           global_max_pixels=2000*28*28,
                           per_img_max_pixels=1280*28*28,
                           divisor=28):
    '''Resuze images to fit within a global token budget and per-image token budget.'''
    total = sum(img.width * img.height for img in images)
    if total <= global_max_pixels:
        return images, None

    ratio = math.sqrt(global_max_pixels / total)

    processed = []
    new_sizes = []
    for img in images:
        w, h = int(img.width * ratio), int(img.height * ratio)
        w = max(divisor, (w // divisor) * divisor)
        h = max(divisor, (h // divisor) * divisor)

        if w * h > per_img_max_pixels:
            r = math.sqrt(per_img_max_pixels / (w * h))
            w = max(divisor, int(w * r) // divisor * divisor)
            h = max(divisor, int(h * r) // divisor * divisor)

        processed.append(img.resize((w, h), Image.BICUBIC))
        new_sizes.append((w, h))
    return processed, new_sizes

def resize_diff(images,
                question_img_max_pixels=1280*28*28,#2000*28*28,
                remain_global_max_pixels=800*28*28,#800*3*28*28,
                remain_per_img_max_pixels=800*28*28,#1280*28*28,
                divisor=28):
    processed = []
    new_sizes = []
    question_img_processed, question_img_new_sizes = resize_by_token_budget(
        [images[0]],
        global_max_pixels=question_img_max_pixels,
        per_img_max_pixels=question_img_max_pixels,
        divisor=divisor
    )
    processed.append(question_img_processed[0])
    new_sizes.append(question_img_new_sizes[0] if question_img_new_sizes is not None else None)
    remain_img_processed, remain_img_new_sizes = resize_by_token_budget(
        images[1:],
        global_max_pixels=remain_global_max_pixels,
        per_img_max_pixels=remain_per_img_max_pixels,
        divisor=divisor
    )
    processed.extend(remain_img_processed)
    new_sizes.extend(remain_img_new_sizes if remain_img_new_sizes is not None else [None]*len(remain_img_processed))
    return processed, new_sizes

def find_helper_img_segs(ids, token_ids):
    device = ids.device
    def between(start_pos, end_pos, wanted_id=None):
        s = start_pos + 1
        e = end_pos
        if s >= e:
            return torch.empty(0, dtype=torch.long, device=device)
        if wanted_id is None:
            return torch.arange(s, e, device=device, dtype=torch.long)
        mask = (ids[s:e] == wanted_id)
        return torch.nonzero(mask, as_tuple=False).squeeze(-1) + s
    v_starts = torch.nonzero(ids == token_ids['v_start'], as_tuple=False).squeeze(-1)
    v_ends   = torch.nonzero(ids == token_ids['v_end'],   as_tuple=False).squeeze(-1)
    v_ptr, e_ptr = 0, 0
    Vs, Ve = [], []
    while v_ptr < v_starts.numel() and e_ptr < v_ends.numel():
        if v_starts[v_ptr] < v_ends[e_ptr]:
            Vs.append(v_starts[v_ptr].item()); Ve.append(v_ends[e_ptr].item())
            v_ptr += 1; e_ptr += 1
        else:
            e_ptr += 1
    # drop the question image (first vision pair)
    Q_img_idx = between(Vs[0], Ve[0], wanted_id=token_ids['img_pad'])
    if len(Vs) > 0 and len(Ve) > 0:
        Vs = Vs[1:]
        Ve = Ve[1:]
    helper_img_segs = [[between(vs, ve, wanted_id=token_ids['img_pad'])] for vs, ve in zip(Vs, Ve)]
    return Q_img_idx, helper_img_segs

def find_aux_image_token_blocks(input_ids_row, token_ids):
    """
    Return one LongTensor of <|image_pad|> indices per auxiliary image.
    The first vision_start/end pair is the question image and is skipped.
    """
    v_starts = (input_ids_row == token_ids['v_start']).nonzero(as_tuple=False).squeeze(-1)
    v_ends = (input_ids_row == token_ids['v_end']).nonzero(as_tuple=False).squeeze(-1)
    pairs = list(zip(v_starts.tolist(), v_ends.tolist()))
    blocks = []
    for vs, ve in pairs[1:]:
        mask = input_ids_row[vs + 1:ve] == token_ids['img_pad']
        idx = mask.nonzero(as_tuple=False).squeeze(-1) + vs + 1
        blocks.append(idx)
    return blocks

def find_segments_1d(ids, token_ids):
    """
    ids: 1D LongTensor, shape [L]
    token_ids: dict with keys:
        'v_start', 'v_end', 'img_pad',
        'abs_start', 'abs_end', 'abs_pad',
        'obs_start', 'obs_end'

    Returns a list for each step S_i with:
        (I_idx: LongTensor, A_idx: LongTensor, O_blocks: List[LongTensor])

    Notes:
    - We assume the first <|vision_start|>...</|vision_end|> pair is the question image
      and is excluded from steps (i.e., only subsequent pairs are treated as I_i).
    - O can appear multiple times inside a single T_i; DO NOT merge them.
      O_blocks contains one LongTensor per <observation>...</observation> block.
    - No cross-step O is expected; we only pair O blocks fully inside each T_i.
    """

    L = ids.numel()
    device = ids.device

    # Helper to collect indices between two tags (exclusive) that match 'wanted_id' (or all if None)
    def between(start_pos, end_pos, wanted_id=None):
        s = start_pos + 1
        e = end_pos
        if s >= e:
            return torch.empty(0, dtype=torch.long, device=device)
        if wanted_id is None:
            return torch.arange(s, e, device=device, dtype=torch.long)
        mask = (ids[s:e] == wanted_id)
        return torch.nonzero(mask, as_tuple=False).squeeze(-1) + s

    # 1) Pair all I_i by <|vision_start|> ... <|vision_end|>
    v_starts = torch.nonzero(ids == token_ids['v_start'], as_tuple=False).squeeze(-1)
    v_ends   = torch.nonzero(ids == token_ids['v_end'],   as_tuple=False).squeeze(-1)
    v_ptr, e_ptr = 0, 0
    Vs, Ve = [], []
    while v_ptr < v_starts.numel() and e_ptr < v_ends.numel():
        if v_starts[v_ptr] < v_ends[e_ptr]:
            Vs.append(v_starts[v_ptr].item()); Ve.append(v_ends[e_ptr].item())
            v_ptr += 1; e_ptr += 1
        else:
            e_ptr += 1
    # drop the question image (first vision pair)
    Q_img_idx = between(Vs[0], Ve[0], wanted_id=token_ids['img_pad'])
    if len(Vs) > 0 and len(Ve) > 0:
        Vs = Vs[1:]
        Ve = Ve[1:]

    # 2) Pair all A_i by <abs_vis_token> ... </abs_vis_token>
    a_starts = torch.nonzero(ids == token_ids['abs_start'], as_tuple=False).squeeze(-1)
    a_ends   = torch.nonzero(ids == token_ids['abs_end'],   as_tuple=False).squeeze(-1)
    a_ptr, b_ptr = 0, 0
    As, Ae = [], []
    while a_ptr < a_starts.numel() and b_ptr < a_ends.numel():
        if a_starts[a_ptr] < a_ends[b_ptr]:
            As.append(a_starts[a_ptr].item()); Ae.append(a_ends[b_ptr].item())
            a_ptr += 1; b_ptr += 1
        else:
            b_ptr += 1

    # Precompute all observation tag positions to avoid repeated arange() inside loops
    obs_starts_all = torch.nonzero(ids == token_ids['obs_start'], as_tuple=False).squeeze(-1)
    obs_ends_all   = torch.nonzero(ids == token_ids['obs_end'],   as_tuple=False).squeeze(-1)

    S = []
    n_steps = min(len(Vs), len(As))
    for i in range(n_steps):
        vs, ve = Vs[i], Ve[i]
        as_, ae = As[i], Ae[i]

        # I_i and A_i indices (exclusive between their own tags)
        I_idx = between(vs, ve, wanted_id=token_ids['img_pad'])
        A_idx = between(as_, ae, wanted_id=token_ids['abs_pad'])

        # Text region T_i: from end of A_i to start of next vision, or to sequence end
        t_end = Vs[i + 1] if (i + 1) < len(Vs) else L

        # Restrict observation tag candidates to the current T_i window
        # Start tags: ae <= pos < t_end
        # End tags:   ae <  pos <= t_end
        # (Fully contained O will satisfy start < end and both within this window.)
        in_start_win = (obs_starts_all >= ae) & (obs_starts_all < t_end)
        in_end_win   = (obs_ends_all   >  ae) & (obs_ends_all   <= t_end)
        o_starts = obs_starts_all[in_start_win]
        o_ends   = obs_ends_all[in_end_win]

        # Greedy 1-1 pairing within T_i
        O_blocks = []
        p, q = 0, 0
        while p < o_starts.numel() and q < o_ends.numel():
            s_pos = o_starts[p].item()
            e_pos = o_ends[q].item()
            if s_pos < e_pos:
                O_idx = between(s_pos, e_pos, wanted_id=None)
                if O_idx.numel() > 0:
                    O_blocks.append(O_idx)
                p += 1
                q += 1
            else:
                q += 1

        S.append((I_idx, A_idx, O_blocks))

    return Q_img_idx, S

def build_4d_attn(
    input_ids,
    pad_mask,
    token_ids,
    large_neg: float = 1e-6,
    not_mask_image: bool = False,
    mask_latent: bool = False,
    observation_tokens_only_see_image_tokens: bool = False,
    observation_tokens_only_see_latent_tokens: bool = False,
    observation_tokens_cannot_see_question_image: bool = False,
    observation_tokens_only_see_question_and_latent: bool = False,
    latent_can_see_all_previous: bool = True,
    mask_question_image: bool = False,
    return_type: str = 'bool'
):
    """
    input_ids: LongTensor [B, L]
    pad_mask:  LongTensor/BoolTensor [B, L], 1/True for real tokens
    token_ids: dict of special token ids
    large_neg: float used as "negative infinity" added to logits (not applied here)
    Returns:
      allowed: BoolTensor [B, 1, L, L], True=allowed to attend, False=blocked
      (Includes causal mask and padding mask already.)
    Notes:
      - This version expects find_segments_1d to return O_blocks: List[LongTensor] per step.
      - We do NOT merge O blocks inside a T_i; each O block gets its own lower-tri self-visibility.
    """

    # Keep on CPU as in the original version; model can cast later if needed.
    input_ids = input_ids.cpu()
    pad_mask = pad_mask.cpu()

    B, L = input_ids.shape
    device = input_ids.device

    # Base causal mask (lower triangular, including diagonal)
    causal = torch.tril(torch.ones((L, L), dtype=torch.bool, device=device))

    # Valid tokens (both query and key must be valid)
    valid = pad_mask.bool()
    allowed = causal.unsqueeze(0).clone()   # [1, L, L]
    allowed = allowed.repeat(B, 1, 1)       # [B, L, L]
    for b in range(B):
        allowed[b] &= valid[b].unsqueeze(0)  # mask keys (columns)
        allowed[b] &= valid[b].unsqueeze(1)  # mask queries (rows)

    batch_segs=[]
    for b in range(B):
        Q_img_idx, segs = find_segments_1d(input_ids[b], token_ids)
        batch_segs.append(segs)
        if not segs:
            continue

        Lb = input_ids.shape[1]
        ids = input_ids[b]

        if mask_question_image:
            allowed[b][:, Q_img_idx] = False  # no one can see question image tokens

        for (I_idx, A_idx, O_blocks) in segs:
            # --- Latent segment A_i rules ---
            if A_idx.numel():
                # Clear all visibility for A_i queries first
                if not latent_can_see_all_previous:
                    allowed[b][A_idx, :] = False
                else:
                    if mask_question_image and Q_img_idx is not None and Q_img_idx.numel() > 0:
                        allowed[b][A_idx.unsqueeze(1), Q_img_idx] = True


                # (1) A_i can see its left I_i (image pads inside this vision pair)
                if I_idx.numel():
                    allowed[b][A_idx.unsqueeze(1), I_idx] = True

                # (Optional) A_i prefix self-visibility (lower-tri within A_i)
                n = A_idx.numel()
                ar = torch.arange(n, device=A_idx.device)
                tri = ar.unsqueeze(1) >= ar.unsqueeze(0)  # (n, n) bool
                rows = A_idx.unsqueeze(1).expand(n, n)
                cols = A_idx.unsqueeze(0).expand(n, n)
                allowed[b][rows, cols] = tri

                # Ensure only A_i (and not others) can see I_i
                if I_idx.numel() and not not_mask_image:
                    not_A = torch.ones(Lb, dtype=torch.bool, device=device)
                    not_A[A_idx] = False
                    not_A_idx = torch.nonzero(not_A, as_tuple=False).squeeze(-1)
                    if not_A_idx.numel():
                        allowed[b][not_A_idx[:, None], I_idx] = False

                # Optionally hide A_i from all subsequent non-A queries as keys
                if mask_latent:
                    r_idx = torch.arange(Lb, device=device)
                    rows_to_block = (r_idx.unsqueeze(0) > A_idx.unsqueeze(1)).any(dim=0)  # rows after any A
                    if rows_to_block.any():
                        allowed[b][rows_to_block.nonzero(as_tuple=False).squeeze(-1)[:, None], A_idx] = False



            # --- Observation blocks: treat each O block independently ---
            if O_blocks and A_idx.numel():
                # Locate the question image range once if needed
                if observation_tokens_cannot_see_question_image:
                    q_v_starts = torch.nonzero(ids == token_ids['v_start'], as_tuple=False).squeeze(-1)
                    q_v_ends   = torch.nonzero(ids == token_ids['v_end'],   as_tuple=False).squeeze(-1)
                    if q_v_starts.numel() > 0 and q_v_ends.numel() > 0:
                        question_img_start = q_v_starts[0].item()
                        question_img_end   = q_v_ends[0].item()
                        question_img_idx = torch.arange(question_img_start, question_img_end + 1, device=device)
                    else:
                        question_img_idx = None
                else:
                    question_img_idx = None

                # Precompute first answer start position for question segmentation (inline search)
                if 'ans_start' in token_ids:
                    pat = token_ids['ans_start']
                    if isinstance(pat, torch.Tensor):
                        k = int(pat.numel())
                        ans_start_pos = -1
                        if k == 1:
                            eq = torch.nonzero(ids == pat.item(), as_tuple=False).squeeze(-1)
                            ans_start_pos = int(eq[0].item()) if eq.numel() > 0 else -1
                        else:
                            Lb_local = int(ids.numel())
                            ans_start_pos = -1
                            for s in range(0, Lb_local - k + 1):
                                if torch.equal(ids[s:s+k], pat):
                                    ans_start_pos = s
                                    break
                    else:
                        ans_start_pos = -1
                else:
                    ans_start_pos = -1

                for O_idx in O_blocks:
                    if O_idx.numel() == 0:
                        continue

                    # Default: no extra rules for O beyond causal/padding and the I->only-A restriction
                    if observation_tokens_only_see_question_and_latent:
                        # O can ONLY see: (a) question tokens: positions < first ans_start that are NOT image tokens
                        #                 (b) all latent pad tokens that appear BEFORE this O block
                        allowed[b][O_idx, :] = False

                        Lb_local = ids.size(0)
                        ar = torch.arange(Lb_local, device=device)
                        # Question tokens: before answer start and not image tokens
                        if ans_start_pos != -1:
                            before_ans = ar < ans_start_pos
                        else:
                            # No answer pattern found: treat as no question tokens
                            before_ans = torch.zeros(Lb_local, dtype=torch.bool, device=device)
                        non_image = (ids != token_ids['img_pad']) & (ids != token_ids['v_start']) & (ids != token_ids['v_end'])
                        question_idx = torch.nonzero(before_ans & non_image, as_tuple=False).squeeze(-1)

                        # Latent tokens prior to this observation block (all abs_pad positions with index < first O position)
                        o_first = int(O_idx[0].item())
                        latent_before_mask = (ids == token_ids['abs_pad']) & (ar < o_first)
                        latent_before_idx = torch.nonzero(latent_before_mask, as_tuple=False).squeeze(-1)

                        if question_idx.numel():
                            allowed[b][O_idx.unsqueeze(1), question_idx] = True
                        if latent_before_idx.numel():
                            allowed[b][O_idx.unsqueeze(1), latent_before_idx] = True

                        # Each O block has its own lower-tri self-visibility
                        n_o = O_idx.numel()
                        ar_o = torch.arange(n_o, device=O_idx.device)
                        tri_o = ar_o.unsqueeze(1) >= ar_o.unsqueeze(0)
                        rows_o = O_idx.unsqueeze(1).expand(n_o, n_o)
                        cols_o = O_idx.unsqueeze(0).expand(n_o, n_o)
                        allowed[b][rows_o, cols_o] = tri_o
                        continue

                    if observation_tokens_only_see_image_tokens:
                        allowed[b][O_idx, :] = False
                        if I_idx.numel():
                            allowed[b][O_idx.unsqueeze(1), I_idx] = True

                    if observation_tokens_only_see_latent_tokens:
                        allowed[b][O_idx, :] = False
                        if not mask_latent and A_idx.numel():
                            allowed[b][O_idx.unsqueeze(1), A_idx] = True

                    if question_img_idx is not None:
                        allowed[b][O_idx.unsqueeze(1), question_img_idx] = False

                    # Each O block has its own lower-tri self-visibility
                    n_o = O_idx.numel()
                    ar_o = torch.arange(n_o, device=O_idx.device)
                    tri_o = ar_o.unsqueeze(1) >= ar_o.unsqueeze(0)
                    rows_o = O_idx.unsqueeze(1).expand(n_o, n_o)
                    cols_o = O_idx.unsqueeze(0).expand(n_o, n_o)
                    allowed[b][rows_o, cols_o] = tri_o

            # --- Vision tokens I_i as queries: restrict to identity (optional safety) ---
            '''if I_idx.numel():
                allowed[b][I_idx, :] = False
                allowed[b][I_idx, I_idx] = True'''

    # Keep return type consistent with the previous implementation (bool mask).
    # If you need an additive bias, convert with: bias = (~allowed).float() * large_neg.
    if return_type == 'bool':
        return allowed.unsqueeze(1), batch_segs  # [B, 1, L, L], bool
    elif return_type == 'additive':
        return (~allowed.unsqueeze(1)).float() * large_neg, batch_segs

def find_segments_1d_wo_helper_images(ids, token_ids):
    """
    ids: 1D LongTensor, shape [L]
    token_ids: dict with keys:
        'v_start', 'v_end', 'img_pad',
        'abs_start', 'abs_end', 'abs_pad',
        'obs_start', 'obs_end'
    Returns: list of tuples for each S_i:
        (I_idx: LongTensor, A_idx: LongTensor, O_idx: LongTensor)
        O_idx may be empty if no <observation>...</observation> in T_i
    """
    L = ids.numel()
    # Helper to collect indices between two tags (exclusive) that match 'wanted_id' (or all if None)
    def between(start_pos, end_pos, wanted_id=None):
        s = start_pos + 1
        e = end_pos
        if s >= e:
            return torch.empty(0, dtype=torch.long, device=ids.device)
        if wanted_id is None:
            idx = torch.arange(s, e, device=ids.device)
        else:
            mask = (ids[s:e] == wanted_id)
            idx = torch.nonzero(mask, as_tuple=False).squeeze(-1) + s
        return idx


    # 2) Parse all A_i by pairing <abs_vis_token> ... </abs_vis_token>
    a_starts = torch.nonzero(ids == token_ids['abs_start'], as_tuple=False).squeeze(-1)
    a_ends   = torch.nonzero(ids == token_ids['abs_end'],   as_tuple=False).squeeze(-1)
    As, Ae = [], []
    a_ptr, b_ptr = 0, 0
    while a_ptr < a_starts.numel() and b_ptr < a_ends.numel():
        if a_starts[a_ptr] < a_ends[b_ptr]:
            As.append(a_starts[a_ptr].item()); Ae.append(a_ends[b_ptr].item())
            a_ptr += 1; b_ptr += 1
        else:
            b_ptr += 1

    # 3) For each (I_i, A_i) in order, find O_i within T_i
    S = []
    for i in range(len(As)):
        as_, ae = As[i], Ae[i]

        A_idx = between(as_, ae, wanted_id=token_ids['abs_pad'])

        # T_i is from ae to next latent start (or end of sequence)
        t_end = As[i+1] if i + 1 < len(As) else L
        # Find all <observation>...</observation> fully inside T_i
        obs_starts = torch.nonzero((ids == token_ids['obs_start']) & (torch.arange(L, device=ids.device) >= ae) & (torch.arange(L, device=ids.device) < t_end), as_tuple=False).squeeze(-1)
        obs_ends   = torch.nonzero((ids == token_ids['obs_end'])   & (torch.arange(L, device=ids.device) >  ae) & (torch.arange(L, device=ids.device) <= t_end), as_tuple=False).squeeze(-1)

        # Pair obs tags in order
        O_all = []
        p, q = 0, 0
        while p < obs_starts.numel() and q < obs_ends.numel():
            if obs_starts[p] < obs_ends[q]:
                # tokens between the two tags (exclusive) belong to O_i
                O_idx = between(obs_starts[p].item(), obs_ends[q].item(), wanted_id=None)
                if O_idx.numel():
                    O_all.append(O_idx)
                p += 1; q += 1
            else:
                q += 1

        O_idx = torch.cat(O_all, dim=0) if len(O_all) else torch.empty(0, dtype=torch.long, device=ids.device)
        S.append((A_idx, O_idx))

    return S

def build_4d_attn_wo_helper_images(input_ids, pad_mask, token_ids, mask_latent: bool = False,
                                    input_img_mask_ratio: float = 0.0):
    """
    input_ids: LongTensor [B, L]
    pad_mask:  LongTensor/BoolTensor [B, L], 1/True for real tokens
    token_ids: dict of special token ids (see above)
    mask_latent: if True, hide latent A_i tokens from all subsequent rows
    input_img_mask_ratio: if > 0, sample ONE Bernoulli mask per input-image column
                          (per sample) and apply the SAME drop set to every answer
                          row (rows after the last `</abs_vis_token>`). Dropped cols
                          are hard-invisible to all answer rows -> image info can't
                          leak through the answer-to-answer causal path. The only
                          remaining route from input image to answer tokens is via
                          (a) the latent block (the bottleneck we want to force) and
                          (b) question hidden states (intentional context). Drawn
                          fresh per call.

    Returns:
      allowed: BoolTensor [B, 1, L, L], True = allowed to attend.
               Already includes causal + padding masks.
    """
    input_ids = input_ids.cpu()
    pad_mask = pad_mask.cpu()

    B, L = input_ids.shape
    device = input_ids.device

    # Base: causal visibility (lower-triangular including diagonal)
    causal = torch.tril(torch.ones((L, L), dtype=torch.bool, device=device))

    # Start from causal AND valid tokens (both query & key must be valid)
    valid = pad_mask.bool()
    allowed = causal.unsqueeze(0).clone()  # [1, L, L]
    allowed = allowed.repeat(B, 1, 1)      # [B, L, L]
    for b in range(B):
        allowed[b] &= valid[b].unsqueeze(0)  # mask keys
        allowed[b] &= valid[b].unsqueeze(1)  # mask queries

    # Apply per-segment constraints
    for b in range(B):
        segs = find_segments_1d_wo_helper_images(input_ids[b], token_ids)
        if not segs:
            continue

        Lb = input_ids.shape[1]
        for (A_idx, O_idx) in segs:
            if A_idx.numel():
                # Optional: make A_i invisible to all subsequent tokens (as keys)
                if mask_latent:
                    # rows r are considered "subsequent" if any a in A_idx satisfies a < r
                    r_idx = torch.arange(Lb, device=device)
                    rows_to_block = (r_idx.unsqueeze(0) >= A_idx.unsqueeze(1)).any(dim=0)  # [L]
                    if rows_to_block.any():
                        allowed[b][rows_to_block.nonzero(as_tuple=False).squeeze(-1)[:, None], A_idx] = False

    # Move to the canonical 4D [B, 1, L, L] form before any input-image edge masking.
    allowed = allowed.unsqueeze(1)

    # Static path: random partial mask of (answer_rows -> input_image_patches) edges.
    # The curriculum path leaves this at 0 here and applies the per-step ratio in the
    # trainer instead (see CustomTrainerSFT_STAGE3), since collate cannot see global_step.
    if input_img_mask_ratio and input_img_mask_ratio > 0.0:
        apply_input_img_attn_mask(allowed, input_ids, token_ids, input_img_mask_ratio)

    return allowed


def apply_input_img_attn_mask(allowed, input_ids, token_ids, ratio):
    """In-place: block answer-token rows from attending to a random ``ratio`` fraction of
    the input (question) image's patch columns, in the Stage-3 Forward-2 4D mask.

    This is a pure ATTENTION-edge mask: the image tokens themselves are never removed and
    Forward 1 (latent generation) is unaffected. ONE Bernoulli(ratio) draw per input-image
    column is shared across ALL answer rows of a sample, so image content cannot leak to the
    answer via the answer->answer causal path -- the only surviving input-image -> answer
    routes are (a) the latent block (the bottleneck we want to force) and (b) the question
    hidden states (intentional context).

    Args:
      allowed:   BoolTensor [B, 1, L, L], True = allowed to attend. Mutated in place. May
                 live on CPU (collate static path) or GPU (trainer curriculum path).
      input_ids: LongTensor [B, L].
      token_ids: dict of special token ids (needs v_start / v_end / img_pad / abs_end).
      ratio:     float in [0, 1]. <= 0 is a no-op.
    Returns:
      allowed (same tensor object, mutated).
    """
    if ratio is None or ratio <= 0.0:
        return allowed
    assert allowed.dim() == 4 and allowed.size(1) == 1, \
        f"apply_input_img_attn_mask expects [B,1,L,L], got {tuple(allowed.shape)}"
    dev = allowed.device
    ids_cpu = input_ids.detach().to('cpu')
    B, _, L, _ = allowed.shape
    for b in range(B):
        ids_b = ids_cpu[b]
        # First vision pair = input/question image (aux images are stripped in Stage 3).
        v_starts = (ids_b == token_ids['v_start']).nonzero(as_tuple=False).flatten()
        v_ends = (ids_b == token_ids['v_end']).nonzero(as_tuple=False).flatten()
        if v_starts.numel() == 0 or v_ends.numel() == 0:
            continue
        vs, ve = int(v_starts[0].item()), int(v_ends[0].item())
        if ve <= vs + 1:
            continue
        inside = ids_b[vs + 1:ve]
        img_pad_local = (inside == token_ids['img_pad']).nonzero(as_tuple=False).flatten()
        if img_pad_local.numel() == 0:
            continue
        input_img_cols = (img_pad_local + (vs + 1)).to(dev)

        # Answer rows: positions strictly after the last `</abs_vis_token>`.
        latent_ends = (ids_b == token_ids['abs_end']).nonzero(as_tuple=False).flatten()
        answer_start = int(latent_ends[-1].item()) + 1 if latent_ends.numel() > 0 else ve + 1
        if answer_start >= L:
            continue
        row_range = torch.arange(answer_start, L, device=dev)
        # Validity proxy: a real (non-pad) row can attend to itself on the causal diagonal.
        # (Padding + causal are already baked into `allowed`, so the diagonal == pad validity.)
        row_valid = allowed[b, 0, row_range, row_range]
        answer_rows = row_range[row_valid]
        if answer_rows.numel() == 0:
            continue

        # ONE Bernoulli(ratio) draw per input-image column, shared across all answer rows.
        n_c = input_img_cols.numel()
        col_drop = torch.rand(n_c, device=dev) < ratio
        if col_drop.any():
            dropped_cols = input_img_cols[col_drop]
            allowed[b, 0, answer_rows.unsqueeze(1), dropped_cols.unsqueeze(0)] = False
    return allowed


from typing import List, Tuple, Dict, Any

def _merge_spans(spans: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
    # Merge overlapping/adjacent [start, end) spans.
    if not spans:
        return []
    spans = sorted(spans)
    merged = [spans[0]]
    for s, e in spans[1:]:
        ps, pe = merged[-1]
        if s <= pe:  # overlap or adjacent
            merged[-1] = (ps, max(pe, e))
        else:
            merged.append((s, e))
    return merged

def strip_observation_and_track_retokenized(
    text: str,
    tokenizer,
    open_tag: str = "<observation>",
    close_tag: str = "</observation>",
) -> Dict[str, Any]:
    # 1) Build clean_text and record observation char spans in clean_text.
    clean_parts: List[str] = []
    obs_spans: List[Tuple[int, int]] = []

    i = 0
    depth = 0
    cur_start = None

    while i < len(text):
        next_open = text.find(open_tag, i)
        next_close = text.find(close_tag, i)

        # Choose the nearest tag (open or close).
        candidates = [(next_open, "open"), (next_close, "close")]
        candidates = [(pos, typ) for pos, typ in candidates if pos != -1]
        if not candidates:
            clean_parts.append(text[i:])
            break

        pos, typ = min(candidates, key=lambda x: x[0])
        # Append normal text before the tag.
        clean_parts.append(text[i:pos])
        i = pos + (len(open_tag) if typ == "open" else len(close_tag))

        # Update depth and span bookkeeping.
        clean_len = sum(len(p) for p in clean_parts)
        if typ == "open":
            depth += 1
            if depth == 1:
                cur_start = clean_len
        else:  # close
            if depth > 0:
                depth -= 1
                if depth == 0 and cur_start is not None:
                    obs_spans.append((cur_start, clean_len))
                    cur_start = None

    clean_text = "".join(clean_parts)

    # If unbalanced (text ends inside observation), close at end.
    if depth > 0 and cur_start is not None:
        obs_spans.append((cur_start, len(clean_text)))

    obs_spans = _merge_spans(obs_spans)

    # 2) Retokenize clean_text with offsets.
    if not getattr(tokenizer, "is_fast", False):
        raise ValueError("This method needs a fast tokenizer to get offset_mapping.")

    enc = tokenizer(
        clean_text,
        add_special_tokens=False,
        return_offsets_mapping=True,
    )

    clean_ids: List[int] = enc["input_ids"]
    offsets: List[Tuple[int, int]] = enc["offset_mapping"]

    # 3) Map observation char spans -> token positions.
    obs_token_positions: List[int] = []
    j = 0  # pointer over obs_spans

    for tidx, (s, e) in enumerate(offsets):
        # Skip empty offsets if any.
        if e <= s:
            continue
        # Advance span pointer until span end > token start.
        while j < len(obs_spans) and obs_spans[j][1] <= s:
            j += 1
        if j >= len(obs_spans):
            break
        span_s, span_e = obs_spans[j]
        # Overlap check: token intersects observation characters.
        if max(s, span_s) < min(e, span_e):
            obs_token_positions.append(tidx)

    return {
        "clean_text": clean_text,
        "clean_ids": clean_ids,
        "obs_token_positions": obs_token_positions,
    }



if __name__=="__main__":
    pass