File size: 37,807 Bytes
c5a2786
 
 
 
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
a25598f
c5a2786
 
 
 
a25598f
c5a2786
 
 
 
a25598f
c5a2786
 
 
 
 
a25598f
 
 
 
c5a2786
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
a25598f
 
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
 
 
 
a25598f
 
 
c5a2786
 
 
 
a25598f
 
c5a2786
 
 
a25598f
 
c5a2786
a25598f
c5a2786
 
 
 
 
a25598f
 
c5a2786
 
 
a25598f
c5a2786
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
 
a25598f
 
 
 
c5a2786
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
a25598f
 
 
 
 
c5a2786
 
 
 
 
 
 
 
 
 
a25598f
 
 
c5a2786
 
 
a25598f
 
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
a25598f
c5a2786
a25598f
 
c5a2786
a25598f
 
c5a2786
a25598f
 
c5a2786
a25598f
c5a2786
 
 
a25598f
c5a2786
 
 
a25598f
c5a2786
a25598f
c5a2786
a25598f
c5a2786
a25598f
c5a2786
 
 
 
a25598f
 
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
a25598f
c5a2786
a25598f
c5a2786
a25598f
c5a2786
a25598f
 
c5a2786
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
a25598f
c5a2786
 
 
 
a25598f
 
c5a2786
 
a25598f
 
c5a2786
a25598f
c5a2786
a25598f
c5a2786
 
a25598f
c5a2786
a25598f
c5a2786
a25598f
 
c5a2786
 
 
 
 
 
a25598f
c5a2786
a25598f
c5a2786
a25598f
c5a2786
a25598f
c5a2786
 
 
 
a25598f
 
c5a2786
 
 
a25598f
 
c5a2786
a25598f
c5a2786
a25598f
c5a2786
 
 
 
 
a25598f
 
c5a2786
 
a25598f
 
c5a2786
 
 
 
 
 
 
a25598f
 
 
 
 
c5a2786
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
 
 
 
 
c5a2786
 
 
 
a25598f
c5a2786
 
 
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
a25598f
c5a2786
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
 
 
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25598f
 
 
 
c5a2786
 
 
 
 
a25598f
c5a2786
 
 
 
a25598f
c5a2786
 
 
 
a25598f
c5a2786
 
a25598f
c5a2786
 
 
 
 
 
a25598f
c5a2786
 
a25598f
c5a2786
 
 
 
a25598f
 
 
 
 
 
 
 
c5a2786
 
 
 
 
 
 
 
 
 
a25598f
 
c5a2786
 
 
 
 
 
 
 
a25598f
c5a2786
a25598f
c5a2786
 
 
 
 
 
 
a25598f
c5a2786
 
a25598f
c5a2786
 
 
 
a25598f
 
 
 
 
c5a2786
 
 
 
 
a25598f
 
 
 
c5a2786
 
a25598f
c5a2786
 
 
 
 
 
 
 
 
 
a25598f
c5a2786
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
# Copyright 2025 starVLA community. All rights reserved.
# Licensed under the MIT License, Version 1.0 (the "License");
"""
Cosmos-Predict2.5-Perceiver with future-image prediction.

This framework keeps the existing Cosmos hidden-state -> Perceiver action
path, and additionally runs the Cosmos2.5 generation/decoder path during
inference to return the image predicted after the action chunk horizon.
"""

from dataclasses import dataclass, field
from starVLA.model.modules.world_model.latent_contract import validate_generation_length
from starVLA.model.modules.world_model.reproducibility import isolated_visualization, seeded_inference
from typing import List, Optional

import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image

from deployment.model_server.tools.image_tools import to_pil_preserve
from starVLA.model.framework.base_framework import baseframework
from starVLA.model.framework.share_tools import merge_framework_config
from starVLA.model.modules.action_model.PerceiverHead import FlowmatchingActionHead, get_action_model
from starVLA.model.modules.world_model import get_world_model
from starVLA.model.tools import FRAMEWORK_REGISTRY
from starVLA.training.trainer_utils.trainer_tools import resize_images

from starVLA.model.framework.WAM_VJEPA.CosmoPredict25Perceiver import (
    CosmoPredict25_Perceiver,
    CosmoPredict25PerceiverDefaultConfig,
)


@dataclass
class CosmoPredict25PerceiverVJEPA2ACDefaultConfig(CosmoPredict25PerceiverDefaultConfig):
    """Cosmos-Predict2.5-Perceiver + VJEPA2-AC defaults."""

    name: str = "CosmoPredict25PerceiverFutureImage"

    future_image_generation: dict = field(default_factory=lambda: {
        "enabled": True,
        "num_frames": 5,
        "num_inference_steps": 36,
        "guidance_scale": 7.0,
        "output_type": "pil",
        "height": 704,
        "width": 1280,
        "max_sequence_length": 512,
        "conditional_frame_timestep": 0.1,
        "num_latent_conditional_frames": 1,
        "conditioning_mode": "auto",
        "max_samples": 1,
        "return_full_video": False,
        "future_frame_index": -1,
        "negative_prompt": None,
    })

    future_image_training: dict = field(default_factory=lambda: {
        "enabled": True,
        "loss_weight": 1.0,
        "train_time_distribution": "logitnormal",
        "shift": 5.0,
    })


@FRAMEWORK_REGISTRY.register("CosmoPredict25PerceiverVJEPA2AC")
class CosmoPredict25_Perceiver_VJEPA2AC(CosmoPredict25_Perceiver):

    def __init__(self, config: Optional[dict] = None, **kwargs) -> None:

        baseframework.__init__(self)
        self.config = merge_framework_config(CosmoPredict25PerceiverVJEPA2ACDefaultConfig, config)

        # 1. 加载预训练的CosmoPredictr2.5类作为self.backbone
        # WM4A/starVLA/model/modules/world_model/CosmoPredict25.py
        self.backbone = get_world_model(config=self.config)


        wm_hidden = self.backbone.model.config.hidden_size
        self.config.framework.qwenvl.vl_hidden_dim = wm_hidden
        self.config.framework.action_model.diffusion_model_cfg.cross_attention_dim = wm_hidden


        # 2. 初始化FlowmatchingActionHead作为self.action_model
        # WM4A/starVLA/model/modules/action_model/PerceiverHead.py
        self.action_model: FlowmatchingActionHead = get_action_model(config=self.config)


        self.future_action_window_size = self.config.framework.action_model.future_action_window_size
        self.past_action_window_size = self.config.framework.action_model.past_action_window_size
        self.chunk_len = self.past_action_window_size + 1 + self.future_action_window_size
        self.state_dim = self.config.framework.action_model.get("state_dim", None)





        # 3. 加载vjepa_encoder和vjepa_decoder,并将其权重固定为bf16
        vjepa2_ac_cfg = self.config.framework.get("vjepa2_ac")
        vjepa2_ac_repo_dir = vjepa2_ac_cfg.get("repo_dir")
        vjepa2_ac_pretrained = vjepa2_ac_cfg.get("pretrained")


        self.vjepa_encoder, self.vjepa_predictor = torch.hub.load(
            vjepa2_ac_repo_dir,
            "vjepa2_ac_vit_giant",
            source="local",
            pretrained=vjepa2_ac_pretrained,
        )
        self.vjepa_encoder = self.vjepa_encoder.to(torch.bfloat16).cuda()
        self.vjepa_predictor = self.vjepa_predictor.to(torch.bfloat16).cuda()
        for p in self.vjepa_encoder.parameters():
            p.requires_grad = False

        # 冻结predictor内部不再使用的encoder——它们在forward_for_WAM_VJEPA2AC中从未被调用,
        # 但作为nn.Linear子模块仍被注册为可训练参数,DeepSpeed ZeRO-2会为它们分配
        # fp32 master weight和优化器状态,由于它们永远不参与前向计算,梯度始终为None,
        # 在optimizer.step()或fp32 master同步时可能产生异常值,最终污染backbone权重
        for p in self.vjepa_predictor.action_encoder.parameters():
            p.requires_grad = False
        for p in self.vjepa_predictor.state_encoder.parameters():
            p.requires_grad = False
        for p in self.vjepa_predictor.extrinsics_encoder.parameters():
            p.requires_grad = False

        for name, p in self.vjepa_encoder.named_parameters():
            assert p.dtype == torch.bfloat16, f"vjepa_encoder.{name} dtype={p.dtype}, expected bfloat16"
        for name, p in self.vjepa_predictor.named_parameters():
            assert p.dtype == torch.bfloat16, f"vjepa_predictor.{name} dtype={p.dtype}, expected bfloat16"




        # 初始化VJEPA2-AC的图像预处理transform,与官方energy_landscape_example.ipynb保持一致
        from starVLA.facebookresearch_vjepa2_main.app.vjepa_droid.transforms import make_transforms as vjepa2_make_transforms
        self._vjepa2_transform = vjepa2_make_transforms(
            random_horizontal_flip=False,
            random_resize_aspect_ratio=(1., 1.),
            random_resize_scale=(1., 1.),
            reprob=0.,
            auto_augment=False,
            motion_shift=False,
            crop_size=256,
        )

        self.state_projector = torch.nn.Linear(
            self.state_dim,
            self.vjepa_predictor.predictor_embed.out_features,
            bias=True
        ).to(torch.bfloat16)


        self.action_projector = torch.nn.Linear(
            self.action_model.model.config.output_dim,
            self.vjepa_predictor.predictor_embed.out_features,
            bias=True
        ).to(torch.bfloat16)




    @staticmethod
    def _as_sequence(images):
        return list(images) if isinstance(images, (list, tuple)) else [images]

    def _align_state_dim(self, state: torch.Tensor) -> torch.Tensor:
        if state is None or self.state_dim is None:
            return state
        target_dim = int(self.state_dim)
        current_dim = state.shape[-1]
        if current_dim == target_dim:
            return state
        if current_dim > target_dim:
            return state[..., :target_dim]

        pad_shape = (*state.shape[:-1], target_dim - current_dim)
        padding = state.new_zeros(pad_shape)
        return torch.cat([state, padding], dim=-1)

    @staticmethod
    def _as_bool(value) -> bool:
        if isinstance(value, str):
            return value.lower() in {"1", "true", "yes", "on"}
        return bool(value)

    def _future_image_training_cfg(self) -> dict:
        return dict(self.config.framework.get("future_image_training", {}))

    def _future_latent_training_cfg(self) -> dict:
        return dict(self.config.framework.get("future_latent_training", {}))

    def _sample_train_times(self, batch_size: int, device: torch.device, distribution: str) -> torch.Tensor:
        distribution = str(distribution).lower()
        if distribution == "logitnormal":
            return torch.sigmoid(torch.randn(batch_size, device=device, dtype=torch.float32))
        if distribution == "uniform":
            return torch.rand(batch_size, device=device, dtype=torch.float32)
        raise ValueError(f"Unsupported future_image_training.train_time_distribution={distribution!r}")

    @staticmethod
    def _shift_time(time: torch.Tensor, shift: float) -> torch.Tensor:
        return shift * time / (1.0 + (shift - 1.0) * time)

    def _latent_frame_count(self, raw_frame_count: int) -> int:
        temporal_factor = int(getattr(self.backbone, "vae_scale_factor_temporal", 4))
        return (int(raw_frame_count) - 1) // temporal_factor + 1

    def _build_future_video_sequences(self, batch_images: List, future_images: List) -> tuple[List[List], List[int], List[int]]:

        temporal_factor = int(getattr(self.backbone, "vae_scale_factor_temporal", 4))
        raw_sequences = []
        condition_counts = []
        sample_counts = []




        for images, future_image in zip(batch_images, future_images):
            current_sequence = self._as_sequence(images)
            future_sequence = self._as_sequence(future_image)


            if not current_sequence or not future_sequence:
                raise ValueError("future image training needs both current image and future_image.")

            if len(future_sequence) != 1:
                raise ValueError("This image objective expects one future target, not an ignored multi-frame sequence")
            future_frame = future_sequence[0]

            raw_sequence = current_sequence + [future_frame] * temporal_factor
            raw_sequences.append(raw_sequence)

            condition_count = self._latent_frame_count(len(current_sequence))
            sample_count = self._latent_frame_count(len(raw_sequence))


            condition_counts.append(condition_count)
            sample_counts.append(sample_count)


        return raw_sequences, condition_counts, sample_counts

    # 训练时
    # future_image支路,self.backbone.transformer()
    def _cosmos25_future_image_loss(self, examples: List[dict]) -> Optional[torch.Tensor]:

        if "future_image" not in examples[0]:
            return None
        training_cfg = self._future_image_training_cfg()
        if not self._as_bool(training_cfg.get("enabled", True)):
            return None

        # 1. 将输入数据【当前观测】和【未来观测】预处理为可以被CosmosPredict接受的形状
        # batch_size=8时,batch_images、future_images和instructions都是长度为8的列表,分别包含当前观测图像、未来观测图像和文本prompt;
        batch_images = [to_pil_preserve(example["image"]) for example in examples]
        future_images = [to_pil_preserve(example["future_image"]) for example in examples]
        instructions = [example["lang"] for example in examples]





        # 我们将每个样本对应的当前和未来图像组装为raw_sequence=concat[obs,future_obs,future_obs,future_obs,future_obs]
        # 而raw_sequences也是由8个这样的raw_sequence组成的列表,
        train_obs_image_size = getattr(self.config.framework, "obs_image_size", None)

        if train_obs_image_size:
            batch_images = resize_images(batch_images, target_size=train_obs_image_size)
            future_images = resize_images(future_images, target_size=train_obs_image_size)
        raw_sequences, condition_counts, sample_counts = self._build_future_video_sequences(
            batch_images=batch_images,
            future_images=future_images,
        )






        # 2. 调用CosmosPredict的文本和视觉tokenizer,
        # 将每个样本的prompt编码为prompt_emb(512,100352),将raw_sequence编码为clean_latents(16,2,28,28),
        # 从mask上来看,1个image和4个future_image分别被编码为了两个(16,28,28),分别记为obs_tokens和future_obs_tokens,二者拼起来就是clean_latents
        prompt_embeds = self.backbone._encode_text(instructions)
        clean_latents, _, _ = self.backbone._encode_images(raw_sequences)
        dtype = self.backbone.transformer.dtype
        clean_latents = clean_latents.to(dtype)
        batch_size, channels, num_latents, height, width = clean_latents.shape
        device = clean_latents.device



        condition_mask = clean_latents.new_zeros((batch_size, 1, num_latents, height, width))
        target_mask = clean_latents.new_zeros((batch_size, 1, num_latents, 1, 1), dtype=torch.float32)
        cond_indicator = clean_latents.new_zeros((batch_size, 1, num_latents, 1, 1))



        for batch_idx, (condition_count, sample_count) in enumerate(zip(condition_counts, sample_counts)):
            condition_count = max(1, min(int(condition_count), num_latents))
            sample_count = min(max(condition_count + 1, int(sample_count)), num_latents)
            condition_mask[batch_idx, :, :condition_count] = 1.0
            cond_indicator[batch_idx, :, :condition_count] = 1.0
            target_mask[batch_idx, :, condition_count:sample_count] = 1.0

        # 3. 在clean_latents中,对obs_tokens部分保持不变、对future_obs_tokens部分加噪
        train_time = self._sample_train_times(
            batch_size=batch_size,
            device=device,
            distribution=training_cfg.get("train_time_distribution", "logitnormal"),
        )

        flow_time = self._shift_time(train_time, shift=float(training_cfg.get("shift", 5.0)))

        target_time = flow_time.view(batch_size, 1, 1, 1, 1).to(device=device, dtype=clean_latents.dtype)


        noise = torch.randn_like(clean_latents)


        noisy_latents = clean_latents * (1.0 - target_time) + noise * target_time


        hidden_states = torch.where(target_mask.to(dtype=torch.bool), noisy_latents, clean_latents)


        # 4. 对future_obs_tokens部分,计算其去噪目标target_velocity
        target_velocity = noise.float() - clean_latents.float()


        # 5. 计算timestep和padding_mask
        cond_timestep = float(getattr(self.backbone, "_conditional_frame_timestep", 0.1))

        timestep = clean_latents.new_zeros((batch_size, 1, num_latents, 1, 1))

        timestep = timestep + cond_indicator.to(dtype=clean_latents.dtype) * cond_timestep

        timestep = torch.where(target_mask.to(dtype=torch.bool), target_time.expand_as(timestep), timestep)


        padding_height = int(height * getattr(self.backbone, "vae_scale_factor_spatial", 16))
        padding_width = int(width * getattr(self.backbone, "vae_scale_factor_spatial", 16))
        padding_mask = clean_latents.new_zeros((1, 1, padding_height, padding_width), dtype=dtype)




        # 7. CosmosPredict前向传播
        # 以prompt_emb(8,512,100352)和clean_latents(8,16,2,28,28)作为输入,
        # 其中clean_latents包含【当前观测的tokens1】和【未来观测加噪后的tokens2】这两部分,CosmosPredict采用了一个经典的DiT架构,其中,
        # self-attn使得tokens2能够看到tokens1中真实的当前观测中是视觉信息、cross-attn使得tokens2能够理解prompt_emb中的人类指令信息,
        # 这两部分信息一起作为tokens2部分的降噪条件,得益于loss_mask,最终参与计算future_image_loss的只有tokens2部分,
        if hasattr(self.backbone, "_intermediate_features"):
            self.backbone._intermediate_features.clear()
        with torch.autocast("cuda", dtype=torch.bfloat16):
            pred = self.backbone.transformer(
                hidden_states=hidden_states,
                timestep=timestep,
                encoder_hidden_states=prompt_embeds.to(device=device, dtype=dtype),
                condition_mask=condition_mask.to(dtype),
                padding_mask=padding_mask,
                return_dict=False,
            )
        pred = pred[0] if isinstance(pred, tuple) else pred
        pred = pred.sample if hasattr(pred, "sample") else pred
        if hasattr(self.backbone, "_intermediate_features"):
            self.backbone._intermediate_features.clear()



        # 8. 计算future-image-loss

        loss_mask = target_mask.expand(batch_size, channels, num_latents, height, width)

        future_loss = F.mse_loss(pred.float(), target_velocity, reduction="none")

        future_image_loss = (future_loss * loss_mask).sum() / loss_mask.sum().clamp_min(1.0)


        return future_image_loss

    # 训练时
    # future_latent支路,self.vjepa_encoder + self.vjepa_predictor
    def _vjepa2ac_future_latent_loss(self, examples: List[dict], target_emb: torch.Tensor) -> Optional[torch.Tensor]:

        if "future_image" not in examples[0]:
            return None

        # 2. 从examples中取出当前图像和未来图像,并使用VJEPA2-AC官方的transform对图像做预处理
        batch_current_images = [to_pil_preserve(example["image"]) for example in examples]
        batch_future_images = [to_pil_preserve(example["future_image"]) for example in examples]

        clips_list = []
        for current_images, future_images in zip(batch_current_images, batch_future_images):
            current_seq = self._as_sequence(current_images)
            future_seq = self._as_sequence(future_images)


            current_np = np.array(current_seq[0])
            future_np = np.array(future_seq[0])


            clip_np = np.stack([current_np, future_np], axis=0)

            clip_tensor = self._vjepa2_transform(clip_np)

            clips_list.append(clip_tensor)
        clips = torch.stack(clips_list, dim=0).to(self.vjepa_encoder.patch_embed.proj.weight.device)

        B, C, T, H, W = clips.size()

        c = clips.permute(0, 2, 1, 3, 4).flatten(0, 1).unsqueeze(2).repeat(1, 1, 2, 1, 1).to(torch.bfloat16)


        # 3. self.vjepa_encoder推理时使用b16
        # 这是仿照self._cosmos25_future_image_loss中,当self.backbone作为编码器前向推理时使用bf16
        crop_size = 256
        tokens_per_frame = int((crop_size // self.vjepa_encoder.patch_size) ** 2)
        # 仿照原始代码对self.backbone的处理,用bf16 autocast包裹vjepa_encoder
        with torch.no_grad(), torch.autocast("cuda", dtype=torch.bfloat16):

            h = self.vjepa_encoder(c)

            h = h.view(B, T, -1, h.size(-1)).flatten(1, 2)

            h = F.layer_norm(h, (h.size(-1),))


        # 4. 从h中分离出current_tokens和future_tokens
        current_tokens = h[:, :tokens_per_frame]
        future_tokens = h[:, tokens_per_frame:]



        # 5. 从examples中取出current_state,参考predict_action()中的实现
        current_state = [example["state"] for example in examples] if "state" in examples[0] else None


        current_state = (torch.from_numpy(np.array(current_state)).to(current_tokens.device, dtype=current_tokens.dtype))

        current_state = self._align_state_dim(current_state)


        # 6. self.vjepa_predictor推理时使用fp32
        # 这是仿照CosmoPredict2Perceiver.forward()中,当self.action_model生成action前向推理时使用fp32
        with torch.autocast("cuda", dtype=torch.float32):
            # 通过projector映射维度


            current_state_emb = self.state_projector(current_state)
            target_emb_proj = self.action_projector(target_emb)



            # 7. 调用self.vjepa_predictor预测future_tokens
            future_tokens_pred = self.vjepa_predictor.forward_for_WAM_VJEPA2AC(
                x=current_tokens,
                target_emb=target_emb_proj,
                state_emb=current_state_emb,
            )






            # 8. 计算future_latent_loss
            future_latent_loss = F.smooth_l1_loss(future_tokens_pred, future_tokens)



        return future_latent_loss

    # 训练时
    # 生成future_image、future_latent和action
    # 最终返回的是total_loss=action_loss+future_image_loss*loss_weight+future_latent_loss*loss_weight
    def forward(self, examples: List[dict] = None, **kwargs) -> dict:

        output = {}
        future_image_cfg = self._future_image_training_cfg()
        future_latent_cfg = self._future_latent_training_cfg()

        # 1. action支路(必定执行)
        perceiver_action_loss, target_emb = super().forward(examples=examples, **kwargs)
        output["perceiver_action_loss"] = perceiver_action_loss.detach()
        total_loss = perceiver_action_loss
        # 仿照原始代码对action_model输出target_emb的处理,不强制转换精度
        # target_emb在float32 autocast内产生,后续_vjepa2ac_future_latent_loss也在float32 autocast内使用


        # 2. future_image支路(根据enabled配置决定是否执行)


        if self._as_bool(future_image_cfg.get("enabled")):
            future_image_loss = self._cosmos25_future_image_loss(examples)
            if future_image_loss is not None:
                future_image_loss_weight = float(future_image_cfg.get("loss_weight"))
                output["future_image_loss"] = future_image_loss.detach()
                total_loss = total_loss + future_image_loss * future_image_loss_weight

        # 3. future_latent支路(根据enabled配置决定是否执行)


        if self._as_bool(future_latent_cfg.get("enabled")):
            future_latent_loss = self._vjepa2ac_future_latent_loss(examples, target_emb)
            if future_latent_loss is not None:
                future_latent_loss_weight = float(future_latent_cfg.get("loss_weight"))
                output["future_latent_loss"] = future_latent_loss.detach()
                total_loss = total_loss + future_latent_loss * future_latent_loss_weight

        output["action_loss"] = total_loss



        return output

    def _get_future_image_generation_cfg(self, kwargs: dict) -> dict:
        cfg = dict(self.config.framework.get("future_image_generation", {}))
        override = kwargs.get("future_image_generation", None)
        if override:
            cfg.update(dict(override))

        if "decode_future_images" in kwargs:
            cfg["enabled"] = kwargs["decode_future_images"]
        if "num_ddim_steps" in kwargs and "num_inference_steps" not in kwargs:
            cfg["num_inference_steps"] = kwargs["num_ddim_steps"]

        for key in (
            "enabled",
            "num_frames",
            "allow_temporal_extrapolation",
            "num_inference_steps",
            "guidance_scale",
            "output_type",
            "height",
            "width",
            "max_sequence_length",
            "conditional_frame_timestep",
            "num_latent_conditional_frames",
            "conditioning_mode",
            "max_samples",
            "return_full_video",
            "future_frame_index",
            "negative_prompt",
            "generator",
            "latents",
            "prompt_embeds",
            "negative_prompt_embeds",
        ):
            if key in kwargs:
                cfg[key] = kwargs[key]
        return cfg

    def _build_generation_inputs(self, batch_images: List, instructions: List[str], cfg: dict) -> dict:
        mode = str(cfg.pop("conditioning_mode", "auto")).lower()
        height = cfg.pop("height", getattr(self.backbone, "_height", 704))
        width = cfg.pop("width", getattr(self.backbone, "_width", 1280))
        heights = self._resolve_generation_size_values(batch_images, height, width)
        trained_frames = max(len(self._as_sequence(images)) for images in batch_images) + int(self.backbone.vae_scale_factor_temporal)
        num_frames = validate_generation_length(
            cfg.pop("num_frames", trained_frames), trained_frames,
            self._as_bool(cfg.pop("allow_temporal_extrapolation", False)),
        )
        generation_inputs = {
            "prompt": instructions,
            "height": heights[0],
            "width": heights[1],
            "num_frames": num_frames,
            "num_inference_steps": cfg.pop("num_inference_steps", 36),
            "guidance_scale": cfg.pop("guidance_scale", 7.0),
            "output_type": cfg.pop("output_type", "pil"),
            "return_dict": True,
            "max_sequence_length": cfg.pop("max_sequence_length", 512),
            "conditional_frame_timestep": cfg.pop("conditional_frame_timestep", 0.1),
            "num_latent_conditional_frames": cfg.pop("num_latent_conditional_frames", 1),
        }

        negative_prompt = cfg.pop("negative_prompt", None)
        if negative_prompt is not None:
            generation_inputs["negative_prompt"] = negative_prompt

        if mode == "image":
            generation_inputs["image"] = [self._as_sequence(images)[-1] for images in batch_images]
        elif mode == "video":
            generation_inputs["video"] = [self._as_sequence(images) for images in batch_images]
        elif mode == "auto":
            if any(len(self._as_sequence(images)) > 1 for images in batch_images):
                generation_inputs["video"] = [self._as_sequence(images) for images in batch_images]
            else:
                generation_inputs["image"] = [self._as_sequence(images)[0] for images in batch_images]
        else:
            raise ValueError(
                "future_image_generation.conditioning_mode must be one of "
                f"'auto', 'image', or 'video', got {mode!r}."
            )

        passthrough_keys = (
            "generator",
            "latents",
            "prompt_embeds",
            "negative_prompt_embeds",
            "num_videos_per_prompt",
            "callback_on_step_end",
            "callback_on_step_end_tensor_inputs",
        )
        for key in passthrough_keys:
            if key in cfg and cfg[key] is not None:
                generation_inputs[key] = cfg.pop(key)

        generation_inputs.update({key: value for key, value in cfg.items() if value is not None})
        return generation_inputs

    @staticmethod
    def _is_auto_size(value) -> bool:
        return isinstance(value, str) and value.lower() == "auto"

    @staticmethod
    def _round_up_to_multiple(value: int, multiple: int) -> int:
        return int(((max(1, value) + multiple - 1) // multiple) * multiple)

    @staticmethod
    def _image_size(image) -> tuple[int, int]:
        size = getattr(image, "size", None)
        if isinstance(size, (list, tuple)) and len(size) >= 2:
            return int(size[0]), int(size[1])
        if torch.is_tensor(image):
            shape = tuple(image.shape)
            if len(shape) >= 3 and shape[0] in (1, 3, 4):
                return int(shape[-1]), int(shape[-2])
            if len(shape) >= 2:
                return int(shape[1]), int(shape[0])
        array = np.asarray(image)
        if array.ndim < 2:
            raise ValueError(f"Cannot infer image size from shape {array.shape}")
        return int(array.shape[1]), int(array.shape[0])

    def _resolve_generation_size_values(self, batch_images: List, height, width) -> tuple:
        if not self._is_auto_size(height) and not self._is_auto_size(width):
            return int(height), int(width)

        resolved_heights = []
        resolved_widths = []
        spatial_multiple = int(getattr(self.backbone, "vae_scale_factor_spatial", 16))
        for images in batch_images:
            image_sequence = self._as_sequence(images)
            frame_sizes = [self._image_size(image) for image in image_sequence]
            if not frame_sizes:
                raise ValueError("Cannot infer auto generation size from empty image sequence.")

            sample_height = max(frame_height for _, frame_height in frame_sizes) if self._is_auto_size(height) else int(height)
            if self._is_auto_size(width):
                max_aspect = max(frame_width / max(1, frame_height) for frame_width, frame_height in frame_sizes)
                sample_width = self._round_up_to_multiple(round(sample_height * max_aspect), spatial_multiple)
            else:
                sample_width = int(width)
            resolved_heights.append(int(sample_height))
            resolved_widths.append(int(sample_width))

        height_value = resolved_heights if len(set(resolved_heights)) > 1 else resolved_heights[0]
        width_value = resolved_widths if len(set(resolved_widths)) > 1 else resolved_widths[0]
        return height_value, width_value

    @staticmethod
    def _extract_videos(generation_output):
        if hasattr(generation_output, "frames"):
            return generation_output.frames
        if hasattr(generation_output, "videos"):
            return generation_output.videos
        if hasattr(generation_output, "images"):
            return generation_output.images
        if isinstance(generation_output, tuple):
            return generation_output[0]
        return generation_output

    def _select_future_images(self, generation_output, future_frame_index: int, return_full_video: bool):
        videos = self._extract_videos(generation_output)
        if return_full_video:
            return videos

        if isinstance(videos, (list, tuple)) and videos and isinstance(videos[0], Image.Image):
            return [videos[future_frame_index]]

        future_images = []
        for sample in videos:
            if torch.is_tensor(sample):
                if sample.ndim >= 4:
                    future_images.append(sample[future_frame_index])
                else:
                    future_images.append(sample)
            elif isinstance(sample, np.ndarray):
                if sample.ndim >= 4:
                    future_images.append(sample[future_frame_index])
                else:
                    future_images.append(sample)
            elif isinstance(sample, (list, tuple)):
                future_images.append(sample[future_frame_index])
            else:
                future_images.append(sample)
        return future_images

    @staticmethod
    def _slice_generation_value(key: str, value, index: int, batch_size: int):
        if value is None:
            return None
        if key in {"height", "width"} and isinstance(value, (list, tuple)) and len(value) == batch_size:
            return value[index]
        if key in {"image", "video"} and isinstance(value, (list, tuple)) and len(value) == batch_size:
            return value[index]
        if torch.is_tensor(value) and value.shape[0] == batch_size:
            return value[index:index + 1]
        if isinstance(value, np.ndarray) and value.shape[0] == batch_size:
            return value[index:index + 1]
        if isinstance(value, list) and len(value) == batch_size:
            return [value[index]]
        if isinstance(value, tuple) and len(value) == batch_size:
            return (value[index],)
        return value

    # 推理时
    # future_image支路,self.backbone.generate()
    @isolated_visualization
    def _generate_future_images(self, generation_inputs: dict, batch_size: int, max_samples: int, future_frame_index: int, return_full_video: bool):

        if max_samples is None or max_samples < 0:
            sample_count = batch_size
        else:
            sample_count = min(batch_size, max(0, int(max_samples)))



        """
        {
            'prompt': [
                'Subtask: unlocked_waist: pick the bell pepper from the plate and place it in the cardboard box.', 
                'Subtask: unlocked_waist: pick the tomato from the tray and place it in the cardboard box.', 
                'Subtask: unlocked_waist: pick up the milk, place it into the microwave and close the microwave.', 
                'Subtask: unlocked_waist: pick the bell pepper from the cutting board and place it in the pot.', 
                'Subtask: unlocked_waist: pick up the potato, place it into the microwave and close the microwave.', 
                'Subtask: unlocked_waist: pick the tomato from the placemat and place it in the tiered shelf.', 
                'Subtask: unlocked_waist: pick the can from the cutting board and place it in the basket.', 
                'Subtask: unlocked_waist: pick up the wine, place it into the cabinet and close the cabinet.'
            ], 
            'height': 224, 
            'width': 224, 
            'num_frames': 93, 
            'num_inference_steps': 20, 
            'guidance_scale': 7.0, 
            'output_type': 'pil', 
            'return_dict': True, 
            'max_sequence_length': 512, 
            'conditional_frame_timestep': 0.1, 
            'num_latent_conditional_frames': 2, 
            'image': [
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D7487370>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D74872B0>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D7487280>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D74872E0>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D74877F0>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D7487850>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D7487F70>, 
                <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D7487580>
            ]
        }
        """
        pred_future_images = []
        for sample_idx in range(sample_count):

            # 1. CosmosPredict:构建输入

            single_inputs = {
                key: self._slice_generation_value(key, value, sample_idx, batch_size)
                for key, value in generation_inputs.items()
            }

            """
            {
                'prompt': ['Subtask: unlocked_waist: pick the bell pepper from the plate and place it in the cardboard box.'], 
                'height': 224, 
                'width': 224, 
                'num_frames': 93, 
                'num_inference_steps': 20, 
                'guidance_scale': 7.0, 
                'output_type': 'pil', 
                'return_dict': True, 
                'max_sequence_length': 512, 
                'conditional_frame_timestep': 0.1, 
                'num_latent_conditional_frames': 2, 
                'image': <PIL.Image.Image image mode=RGB size=224x224 at 0x7F96D7487370>
            }
            """

            # 2. CosmosPredict:前向推理
            generation_output = self.backbone.generate(**single_inputs)




            sample_images = self._select_future_images(
                generation_output,
                future_frame_index=future_frame_index,
                return_full_video=return_full_video,
            )

            if isinstance(sample_images, (list, tuple)):
                pred_future_images.extend(sample_images)
            else:
                pred_future_images.append(sample_images)

        return pred_future_images

    # 推理时
    # action支路,self.backbone.build_inputs()、self.backbone.forward()、self.action_model.predict_action()
    @seeded_inference
    @torch.inference_mode()
    def predict_action(self, examples: List[dict], **kwargs) -> dict:

        if type(examples) is not list:
            examples = [examples]

        # 1. CosmosPredict:构建输入
        batch_images = [to_pil_preserve(example["image"]) for example in examples]
        instructions = [example["lang"] for example in examples]


        train_obs_image_size = getattr(self.config.framework, "obs_image_size", None)

        if train_obs_image_size:
            batch_images = resize_images(batch_images, target_size=train_obs_image_size)

        wm_inputs = self.backbone.build_inputs(images=batch_images, instructions=instructions)









        # 2. CosmosPredict:前向推理
        # 这里的CosmosPredict起到了一个编码器的作用,将输入的当前观测和prompt编码为vl-tokens
        with torch.autocast("cuda", dtype=torch.bfloat16):
            wm_outputs = self.backbone(
                **wm_inputs,
                output_hidden_states=True,
                return_dict=True,
            )
            last_hidden = wm_outputs.hidden_states[-1]



        # 3. ActionHead:构建输入
        state = [example["state"] for example in examples] if "state" in examples[0] else None
        state = (
            torch.from_numpy(np.array(state)).to(last_hidden.device, dtype=last_hidden.dtype)
            if state is not None
            else None
        )

        state = self._align_state_dim(state)


        embodiment_tag = [example["embodiment_tag"] for example in examples] if "embodiment_tag" in examples[0] else None
        embodiment_tag = (
            torch.from_numpy(np.array(embodiment_tag)).to(last_hidden.device, dtype=torch.int64)
            if embodiment_tag is not None
            else None
        )

        if embodiment_tag is not None:
            embodiment_tag = embodiment_tag.view(-1)


        # 4. ActionHead:前向推理
        with torch.autocast("cuda", dtype=torch.float32):
            pred_actions = self.action_model.predict_action(last_hidden, state, embodiment_tag)





        output = {"normalized_actions": pred_actions.detach().cpu().numpy()}

        # 5. 若enabled为True,只生成action即可返回;若enabled为False,则需要
        generation_cfg = self._get_future_image_generation_cfg(kwargs)
        is_enabled = generation_cfg.pop("enabled", True)

        if not self._as_bool(is_enabled):


            return output

        return_full_video = self._as_bool(generation_cfg.pop("return_full_video", False))
        future_frame_index = int(generation_cfg.pop("future_frame_index", -1))
        max_samples = int(generation_cfg.pop("max_samples", 1))
        generation_inputs = self._build_generation_inputs(batch_images, instructions, generation_cfg)
        output["pred_future_images"] = self._generate_future_images(
            generation_inputs,
            batch_size=len(examples),
            max_samples=max_samples,
            future_frame_index=future_frame_index,
            return_full_video=return_full_video,
        )

        return output