File size: 30,569 Bytes
987ed1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MIT License

# Copyright (c) 2025 ReinFlow Authors

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# MIT License

# Copyright (c) 2024 Kevin Frans

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# The description of ShortCutFlowMLP is translated from Kevin Fran's One Step Diffusion via Short Cut Models 
# and revised by ReinFlow Authors and Collaborators.
# The ShortCutFlowViT scipt is extended from Diffusion Policy Policy Optimization's implementation. 
# NoisyShortCutFlowMLP and NoisyShortCutFlowViT are composed by ReinFlow Authors.
  
import torch
import torch.nn as nn
import numpy as np
import logging
from copy import deepcopy
from torch import Tensor
from diffusion_policy.model.common.mlp import MLP, ResidualMLP
from diffusion_policy.model.diffusion.positional_embedding import SinusoidalPosEmb
from diffusion_policy.model.common.modules import SpatialEmb, RandomShiftsAug
from diffusion_policy.model.common.vit import VitEncoder
from diffusion_policy.model.flow.mlp_flow import NoisyFlowMLP, ExploreNoiseNet
log = logging.getLogger(__name__)
import einops
from typing import Tuple, List

class ShortCutFlowMLP(nn.Module):
    def __init__(
        self,
        horizon_steps,
        action_dim,
        cond_dim,
        td_emb_dim=16,  # Embedding dimension for time and step
        mlp_dims=[256, 256], # hidden layers of the velocity head.
        cond_mlp_dims=None, # the hidden dimensions and output dimension of condition embedder. 
        activation_type="Mish", # different from reflow (SiLU is better for dense nets)
        out_activation_type="Identity",
        use_layernorm=False,
        residual_style=False,
        embed_combination_type='add' #, multiply or concate.   cond_embed + td_embed or cond_embed x td_embed or [cond_embed, td_embed]
        ):
        super().__init__()
        self.td_emb_dim = td_emb_dim # for both time and step
        self.act_dim_total = action_dim * horizon_steps
        self.horizon_steps = horizon_steps
        self.action_dim = action_dim
        self.cond_dim=cond_dim
        self.activation_type=activation_type
        self.out_activation_type=out_activation_type
        self.time_embed_activation=nn.Mish() # nn.SiLU() maybe better but for fair comparison with reflow and diffusion we use Mish. 
        self.use_layernorm=use_layernorm
        self.residual_style=residual_style
        candidate_embed_combination_types=['add', 'multiply', 'concate']
        if embed_combination_type not in candidate_embed_combination_types:
            raise ValueError(f"embed_combination_type must be one of {candidate_embed_combination_types} but received {embed_combination_type}!")
        self.embed_combination_type=embed_combination_type
        
        # time t and step share an input embedding
        self.map_noise = SinusoidalPosEmb(td_emb_dim)
        # MLP to process concatenated t and step embeddings
        self.t_emb = nn.Sequential(
            nn.Linear(2 * td_emb_dim, td_emb_dim),
            self.time_embed_activation,
            nn.Linear(td_emb_dim, td_emb_dim)
        )
        
        # Condition embedding
        if cond_mlp_dims:
            self.cond_emb = MLP(
                [cond_dim] + cond_mlp_dims,
                activation_type=activation_type,
                out_activation_type="Identity",
            )
            self.cond_enc_dim = cond_mlp_dims[-1]
        else:
            self.cond_enc_dim = cond_dim
        if embed_combination_type in ['add', 'multiply'] and td_emb_dim !=self.cond_enc_dim: 
            raise ValueError(f"To add or multiply td_embed with cond_embed you must make td_emb_dim={td_emb_dim} == self.cond_enc_dim={self.cond_enc_dim}")
        
        
        # velocity head
        model = ResidualMLP if residual_style else MLP
        if self.embed_combination_type =='concate':
            input_dim = action_dim * horizon_steps + self.cond_enc_dim + td_emb_dim #(s,a, t-dt)
        elif self.embed_combination_type =='add' or 'multiply':
            input_dim = action_dim * horizon_steps + td_emb_dim
        else:
            raise ValueError(f"Unsupported embed_combination_type={self.embed_combination_type}")
        self.vel_head = model(
            [input_dim] + mlp_dims + [self.act_dim_total],
            activation_type=activation_type,
            out_activation_type=out_activation_type,
            use_layernorm=use_layernorm,
        )

    def forward(
        self,
        action: Tensor,
        time: Tensor,
        dt: Tensor,
        cond: dict,
        output_embedding=False
        ):
        """
        Inputs:
            action: (B, Ta, Da) - Current action trajectory
            time: (B,) - Current noise level t
            cond: (B, Do) - Condition (e.g., flattened state)
            dt: (B,) - Step size

        Outputs:
            velocity: (B, Ta, Da) - Predicted velocity
        """
        B, Ta, Da = action.shape

        # Flatten action
        action_flat = action.view(B, -1)
        
        # Embed time t and dt separately, then concatenate and feed to the same MLP to squeeze the dimension back to emb_dim
        t_emb = self.map_noise(time.view(B, 1)).view(B, self.td_emb_dim)
        dt_emb = self.map_noise(dt.view(B, 1)).view(B, self.td_emb_dim)
        td_emb = self.t_emb(torch.cat([t_emb, dt_emb], dim=1))

        # Embed condition and add to time-step embedding
        state = cond["state"].view(B, -1)
        cond_emb = self.cond_emb(state) if hasattr(self, "cond_emb") else state
            
        if self.embed_combination_type=='add':# we use add to reduce dimension
            emb = td_emb + cond_emb
        elif self.embed_combination_type=='multiply':# we use add to reduce dimension while preserving nonlinearity
            emb = td_emb * cond_emb
        elif self.embed_combination_type =='concate': # separate the influences of td_embd and cond_emb
            emb=torch.cat([td_emb, cond_emb], dim=-1)
        # Predict velocity
        vel_flat = self.vel_head(torch.cat([action_flat, emb], dim=-1))
        if output_embedding:
            return vel_flat.view(B, Ta, Da), td_emb, cond_emb
        return vel_flat.view(B, Ta, Da)
    
    def sample_action(self,cond:dict,inference_steps:int,clip_intermediate_actions:bool,act_range:List[float], z:Tensor=None,save_chains:bool=False):
        """
        simply return action via integration (Euler's method). the initial noise could be specified. 
        when `save_chains` is True, also return the denoising trajectory.
        """
        B = cond['state'].shape[0]
        device=cond['state'].device

        x_hat:Tensor=z if z is not None else torch.randn(B, self.horizon_steps, self.action_dim, device=device)
        if save_chains:
            x_chain=torch.zeros((B, inference_steps+1, self.horizon_steps, self.action_dim), device=device)
        dt = (1 / inference_steps) * torch.ones_like(x_hat, device=device)
        steps = torch.linspace(0, 1-1/inference_steps, inference_steps, device=device).repeat(B, 1)
        for i in range(inference_steps):
            t = steps[:, i]
            dt_batch = (1 / inference_steps) * torch.ones(B, device=device)
            vt = self.forward(action=x_hat, time=t, dt=dt_batch, cond=cond, output_embedding=False)
            x_hat += vt * dt
            if clip_intermediate_actions or i == inference_steps-1: # always clip the output action. appended by Tonghe on 04/25/2025
                x_hat = x_hat.clamp(*act_range)
            if save_chains:
                x_chain[:, i+1] = x_hat
        if save_chains:
            return x_hat, x_chain
        return x_hat


class ShortCutFlowViT(nn.Module):
    """With ViT backbone and Transformer-based shortcut flow
    
    
    **Architecture**:
    
    camera pixels -> aug-> backbone-> visual_feature - | 
                                                      cat->cond_embed->cond_embedding->|
    proprioception-> prop embedder -> prop_embedding - |                               |
                                                                                       + or * --> cond_td_embedding-|
            t     ->               -> t_embedding ->                                   |                            |
                        map_noise                     td_embed     -->   td_embedding->|                            |
           step   ->               -> dt_embedding->                                                           cat --> vel_head --> vel
                                                                                                                | 
          action  -->   (omitted)                                                         -->  act_embedding   -|
                        (projection + positional embedding)
    """
    def __init__(
        self,
        backbone:VitEncoder,    # VitEncoder instance
        action_dim,
        horizon_steps,
        prop_dim,               # proprioception dimension
        img_cond_steps=1,
        td_emb_dim=16,  # Embedding dimension for time and step
        # d_model=384,  # omitted
        # n_heads=6,
        # depth=12,
        mlp_dims=[256,256], # the hidden dimensions of output velocity head
        cond_mlp_dims=None, # the hidden dimensions and output dimension of condition embedder. 
        activation_type="Mish", # instead of SiLU() maybe better for deep nets.
        out_activation_type="Identity",
        use_layernorm=False,
        residual_style=False,
        dropout=0.0,
        visual_feature_dim=128, # overload spatial embed when specified.
        num_img=1,
        augment=False,
        spatial_emb=0,
        embed_combination_type='add'  # 'add', 'multiply', or 'concate'
    ):
        super().__init__()
        
        # Action chunk
        self.action_dim = action_dim
        self.horizon_steps = horizon_steps
        self.act_dim_total = action_dim * horizon_steps
        
        # Historical proprioception and visual inputs
        self.prop_dim = prop_dim
        self.img_cond_steps = img_cond_steps
        
        # How to combine timestep and condition embeddings.
        candidate_embed_combination_types = ['add', 'multiply', 'concate']
        if embed_combination_type not in candidate_embed_combination_types:
            raise ValueError(f"embed_combination_type must be one of {candidate_embed_combination_types}, got {embed_combination_type}")
        self.embed_combination_type = embed_combination_type
        
        # Transformer dimension (omitted)
        # self.d_model = d_model
        
        # Action embeddings: projection and action chunk positional embedding (omitted)
        # self.x_proj = nn.Linear(action_dim, d_model)
        # self.pos_emb = PositionalEmbedding(d_model)
        
        # Time-step embeddings
        self.td_emb_dim = td_emb_dim
        self.map_noise = SinusoidalPosEmb(td_emb_dim)   # Shared embedding for time t and step size d
        self.time_embed_activation=nn.Mish()            # nn.SiLU() maybe better but for fair comparison with reflow and diffusion we use Mish. 
        self.td_emb = nn.Sequential(
                nn.Linear(2 * td_emb_dim, td_emb_dim),
                self.time_embed_activation,
                nn.Linear(td_emb_dim, td_emb_dim)
            )
        
        # Condition embedding
        if cond_mlp_dims:# add transform to the state 
            self.prop_emb = MLP(
                [prop_dim] + cond_mlp_dims,
                activation_type=activation_type,
                out_activation_type="Identity",
            )
            self.prop_embed_dim = cond_mlp_dims[-1]
        else: # just us the state itself, without transforms. 
            self.prop_embed_dim = prop_dim
        
        # Visual backbone and augmentation
        self.backbone = backbone
        self.num_img = num_img
        self.augment = augment
        if augment:
            self.aug = RandomShiftsAug(pad=4)
        # Visual feature compression
        if spatial_emb > 0:
            assert spatial_emb > 1, "spatial_emb must be > 1"
            if num_img == 2:
                self.compress1 = SpatialEmb(
                    num_patch=self.backbone.num_patch,
                    patch_dim=self.backbone.patch_repr_dim,
                    prop_dim=prop_dim,
                    proj_dim=spatial_emb,
                    dropout=dropout,
                )
                self.compress2 = deepcopy(self.compress1)
            elif num_img == 1:
                self.compress = SpatialEmb(
                    num_patch=self.backbone.num_patch,
                    patch_dim=self.backbone.patch_repr_dim,
                    prop_dim=prop_dim,
                    proj_dim=spatial_emb,
                    dropout=dropout,
                )
            else:
                raise NotImplementedError(f"num_img={num_img} not supported (only 1 or 2)")
            self.visual_feature_dim = spatial_emb * num_img
        else:
            self.visual_feature_dim = visual_feature_dim
            self.compress = nn.Sequential(
                nn.Linear(self.backbone.repr_dim, visual_feature_dim),
                nn.LayerNorm(visual_feature_dim),
                nn.Dropout(dropout),
                nn.ReLU(),
            )
        self.visuomotor_feature_dim = self.visual_feature_dim + self.prop_embed_dim
        
        if embed_combination_type in ['add', 'multiply']: 
            # compress visuomotor information to the same size of time embedding.
            self.cond_embed=nn.Sequential(
                nn.Linear(self.visuomotor_feature_dim, td_emb_dim*2),
                nn.ReLU(),
                nn.Linear(td_emb_dim*2, td_emb_dim),
            )
            self.cond_enc_dim=td_emb_dim
        else:
            self.cond_enc_dim=self.visuomotor_feature_dim
        
        # Transformer middle blocks (omitted)
        # self.transformer_blocks = nn.ModuleList([
        #     ShortcutDiTBlock(d_model, n_heads, dropout) for _ in range(depth)
        # ])
        

        # velocity head
        vel_head_model = ResidualMLP if residual_style else MLP
        if self.embed_combination_type =='concate':
            input_dim = action_dim * horizon_steps + self.cond_enc_dim + td_emb_dim     #(s, a, t-dt)
        elif self.embed_combination_type =='add' or 'multiply':
            input_dim = action_dim * horizon_steps + self.cond_enc_dim                  
        else:
            raise ValueError(f"Unsupported embed_combination_type={self.embed_combination_type}")
        output_dim = action_dim * horizon_steps
        self.vel_head = vel_head_model(
            [input_dim] + mlp_dims + [output_dim],
            activation_type=activation_type,
            out_activation_type=out_activation_type,
            use_layernorm=use_layernorm,
        )
    
    def forward(
        self,
        action,
        time,
        d,
        cond,
        output_embedding=False,
    ):
        """
        Inputs:
            action: (B, Ta, Da) - Action trajectories
            time: (B,) or float - Flow time
            d: (B,) or float - Step size
            cond: dict with keys 'state' and 'rgb'
                state: (B, To, Do) - Proprioceptive states
                rgb: (B, To, C, H, W) - RGB images
            output_embedding: whether also return td_embedding and condition embedding
        Outputs:
            velocity: (B, Ta, Da) - Predicted velocities
        """
        B, Ta, Da = action.shape
        _, T_rgb, C, H, W = cond["rgb"].shape
        
        # flatten chunk
        action_embed = action.view(B, -1)
        # (action transform omitted)
        # Project action chunk and add positional embeddings
        # x = self.x_proj(action)  # (B, Ta, d_model)
        # pos_emb = self.pos_emb(torch.arange(Ta, device=device))
        # x = x + pos_emb[None, :]
        
        # Embed time t and dt separately, then concatenate and feed to the same MLP to squeeze the dimension back to `td_emb_dim`
        t_emb = self.map_noise(time.view(B, 1)).view(B, self.td_emb_dim)
        d_emb = self.map_noise(d.view(B, 1)).view(B, self.td_emb_dim)
        td_emb = self.td_emb(torch.cat([t_emb, d_emb], dim=1))
        
        # Embed proprioceptive states
        state = cond["state"].view(B, -1)
        prop_emb = self.prop_emb(state) if hasattr(self, "prop_emb") else state
        
        # Process visual inputs (augmentation + compression)
        rgb = cond["rgb"][:, -self.img_cond_steps:]
        if self.num_img > 1:
            rgb = rgb.reshape(B, T_rgb, self.num_img, 3, H, W)
            rgb = einops.rearrange(rgb, "b t n c h w -> b n (t c) h w")
        elif self.num_img == 1:
            rgb = einops.rearrange(rgb, "b t c h w -> b (t c) h w")
        else:
            raise ValueError(f"self.num_img={self.num_img} < 1")
        rgb = rgb.float()
        if self.num_img == 2:
            rgb1, rgb2 = rgb[:, 0], rgb[:, 1]
            if self.augment:
                rgb1 = self.aug(rgb1)
                rgb2 = self.aug(rgb2)
            visual_feat1 = self.backbone.forward(rgb1)
            visual_feat1 = self.compress1.forward(visual_feat1, cond["state"].view(B, -1)) if hasattr(self, 'compress1') else self.compress(visual_feat1.flatten(1, -1))
            visual_feat2 = self.backbone.forward(rgb2)
            visual_feat2 = self.compress2.forward(visual_feat2, cond["state"].view(B, -1)) if hasattr(self, 'compress2') else self.compress(visual_feat2.flatten(1, -1))
            visual_feat = torch.cat([visual_feat1, visual_feat2], dim=-1)
        elif self.num_img == 1:
            if self.augment:
                rgb = self.aug(rgb)
            visual_feat = self.backbone.forward(rgb)
            if isinstance(self.compress, SpatialEmb):
                visual_feat = self.compress.forward(visual_feat, cond["state"].view(B, -1))
            else:
                visual_feat = self.compress(visual_feat.flatten(1, -1))
        else:
            raise NotImplementedError(f"num_img={self.num_img} not supported")
        
        # Combine visual and proprioceptive visual_features
        if self.embed_combination_type == 'add' or 'multiply':
            cond_emb = self.cond_embed(torch.cat([visual_feat, prop_emb], dim=-1))
        else:
            cond_emb = torch.cat([visual_feat, prop_emb], dim=-1)
        
        # Combine embeddings based on embed_combination_type
        if self.embed_combination_type == 'add':
            td_cond_emb = td_emb + cond_emb
        elif self.embed_combination_type == 'multiply':
            td_cond_emb = td_emb * cond_emb
        elif self.embed_combination_type == 'concate':
            td_cond_emb = torch.cat([td_emb, cond_emb], dim=-1)
        
        emd=torch.cat([action_embed, td_cond_emb], dim=-1)
        
        # Pass through Transformer blocks
        # omitted
        
        # Final layer to predict velocities
        velocity = self.vel_head(emd)
        if output_embedding:
            return velocity.view(B, Ta, Da), td_emb, cond_emb
        return velocity.view(B, Ta, Da)
    
    def sample_action(self,cond:dict,inference_steps:int,clip_intermediate_actions:bool,act_range:List[float], z:Tensor=None,save_chains:bool=False):
        """
        simply return action via integration (Euler's method). the initial noise could be specified. 
        when `save_chains` is True, also return the denoising trajectory.
        """
        B = cond['state'].shape[0]
        device=cond['state'].device

        x_hat:Tensor=z if z is not None else torch.randn(B, self.horizon_steps, self.action_dim, device=device)
        if save_chains:
            x_chain=torch.zeros((B, inference_steps+1, self.horizon_steps, self.action_dim), device=device)
        dt = (1 / inference_steps) * torch.ones_like(x_hat, device=device)
        steps = torch.linspace(0, 1-1/inference_steps, inference_steps, device=device).repeat(B, 1)
        for i in range(inference_steps):
            t = steps[:, i]
            dt_batch=(1 / inference_steps)* torch.ones(B, device=device)
            vt = self.forward(action=x_hat, time=t, dt=dt_batch, cond=cond, output_embedding=False)
            x_hat += vt * dt
            if clip_intermediate_actions or i == inference_steps-1: # always clip the output action. appended by Tonghe on 04/25/2025
                x_hat = x_hat.clamp(*act_range)
            if save_chains:
                x_chain[:, i+1] = x_hat
        if save_chains:
            return x_hat, x_chain
        return x_hat


class NoisyShortCutFlowMLP(NoisyFlowMLP):
    def __init__(
        self,
        policy:ShortCutFlowMLP,
        denoising_steps:int,
        learn_explore_noise_from:int,
        inital_noise_scheduler_type:str,
        min_logprob_denoising_std:float,
        max_logprob_denoising_std:float,
        learn_explore_time_embedding:bool,
        time_dim_explore:int,
        use_time_independent_noise:bool,
        device,
        noise_hidden_dims=None,
        activation_type='Tanh',
    ):  
        super().__init__(
            policy,
            denoising_steps,
            learn_explore_noise_from,
            inital_noise_scheduler_type,
            min_logprob_denoising_std,
            max_logprob_denoising_std,
            learn_explore_time_embedding,
            time_dim_explore,
            use_time_independent_noise,
            device,
            noise_hidden_dims,
            activation_type
        )
        self.policy:ShortCutFlowMLP
    
    # overload to receive shortcut features 
    def init_exploration_noise_net(self):
        if self.use_time_independent_noise:
            # sigma(s)
            # input dims for the noisy net
            noise_input_dim = self.policy.cond_enc_dim
            # hidden dims for the noisy net
            if not self.noise_hidden_dims:
                self.noise_hidden_dims = [16]
        else:
            if self.learn_explore_time_embedding:
                noise_input_dim = self.time_dim_explore + self.policy.cond_enc_dim
                self.time_embedding_explore = nn.Embedding(num_embeddings=self.denoising_steps, 
                                                       embedding_dim = self.time_dim_explore, 
                                                       device=self.device)
            else:
                # sigma(s,t)
                # input dims for the noisy net
                noise_input_dim = self.policy.td_emb_dim + self.policy.cond_enc_dim
                # hidden dims for the noisy net
                if not self.noise_hidden_dims:
                    self.noise_hidden_dims = [int(np.sqrt(noise_input_dim**2 + self.policy.act_dim_total**2))]
        
        self.explore_noise_net=ExploreNoiseNet(in_dim=noise_input_dim, 
                                                out_dim=self.policy.act_dim_total,
                                                logprob_denoising_std_range=[self.min_logprob_denoising_std, self.max_logprob_denoising_std], 
                                                device=self.device,
                                                hidden_dims=self.noise_hidden_dims,
                                                activation_type=self.noise_activation_type)

    # overload
    def forward(
        self,
        action,
        time,
        dt,
        cond,
        learn_exploration_noise=False,
        step=-1,
        verbose=False,
        **kwargs,
    )->Tuple[Tensor, Tensor]:
        """
        inputs:
            x: (B, Ta, Da)
            time: (B,) floating point in {0,1/2,1/4,1/8,...1/2^n} shortcut flow time
            cond: dict with key state/rgb; more recent obs at the end
                state: (B, To, Do)
            step: (B,) torch.tensor, optional, flow matching denoising step, from 0 to denoising_steps-1
            *here, B is the n_envs
        outputs:
             vel                [B, Ta, Da]
             noise_std          [B, Ta x Da]
        """
        B = action.shape[0]
        # WARNING: here you must secure that dt and time matches: time must be a multiple of 1.0 / self.denoising_steps.
        vel, td_emb, cond_emb = self.policy.forward(action, time, dt, cond, output_embedding=True)
        
        # noise head (for exploration). allow gradient flow.
        if self.initial_noise_scheduler_type=='const' or step < self.learn_explore_noise_from:
            noise_std       = self.logprob_noise_levels[:, step].repeat(B,1)
        else:
            if self.use_time_independent_noise:
                noise_feature    = cond_emb
            else:
                if self.learn_explore_time_embedding:
                    step_ts = torch.tensor(step, device = self.device).repeat(B)
                    time_emb_explore = self.time_embedding_explore(step_ts)
                    noise_feature    = torch.cat([time_emb_explore, cond_emb], dim=-1)
                else:
                    noise_feature    = torch.cat([td_emb.detach(), cond_emb], dim=-1)
            
            noise_std = self.explore_noise_net.forward(noise_feature=noise_feature)
            
            if verbose:
                log.info(f"step={step}, learnable noise = {noise_std.mean()}")
        if verbose:
            log.info(f"step={step}, set to learn from {self.learn_explore_noise_from}, will learn exploration noise ? {step >= self.learn_explore_noise_from}, noise_std={noise_std.mean()}require_grad={noise_std.requires_grad}")
        
        return vel, noise_std if learn_exploration_noise else noise_std.detach()

class NoisyVisionShortCutFlowMLP(NoisyShortCutFlowMLP):
    def __init__(
        self,
        policy:ShortCutFlowViT,
        denoising_steps:int,
        learn_explore_noise_from:int,
        inital_noise_scheduler_type:str,
        min_logprob_denoising_std:float,
        max_logprob_denoising_std:float,
        learn_explore_time_embedding:bool,
        time_dim_explore:int,
        use_time_independent_noise:bool,
        device,
        noise_hidden_dims=None,
        activation_type='Tanh',
    ):  
        super().__init__(
            policy,
            denoising_steps,
            learn_explore_noise_from,
            inital_noise_scheduler_type,
            min_logprob_denoising_std,
            max_logprob_denoising_std,
            learn_explore_time_embedding,
            time_dim_explore,
            use_time_independent_noise,
            device,
            noise_hidden_dims,
            activation_type,
        )
        self.policy:ShortCutFlowViT
    # overload
    def forward(
        self,
        action,
        time,
        cond,
        learn_exploration_noise=False,
        step=-1,
        verbose=False,
        **kwargs,
    )->Tuple[Tensor, Tensor]:
        """
        inputs:
            x: (B, Ta, Da)
            time: (B,) floating point in {0,1/2,1/4,1/8,...1/2^n} shortcut flow time
            cond: dict with key state/rgb; more recent obs at the end
                state: (B, To, Do)
            step: (B,) torch.tensor, optional, flow matching inference step, from 0 to denoising_steps-1
            *here, B is the n_envs
        outputs:
             vel                [B, Ta, Da]
             noise_std          [B, Ta x Da]
        """
        B = action.shape[0]
        # this is new for shortcut flows:
        dt = torch.full((B,), 1.0 / self.denoising_stepss, device=self.device)
        # WARNING: here you must secure that dt and time matches: time must be a multiple of 1.0 / self.denoising_steps.
        vel, td_emb, cond_emb = self.policy.forward(action, time, dt, cond, output_embedding=True)
        
        # noise head (for exploration). allow gradient flow.
        if self.initial_noise_scheduler_type=='const' or step < self.learn_explore_noise_from:
            noise_std       = self.logprob_noise_levels[:, step].repeat(B,1)
        else:
            if self.use_time_independent_noise:
                noise_feature    = cond_emb
            else:
                if self.learn_explore_time_embedding:
                    step_ts = torch.tensor(step, device = self.device).repeat(B)
                    time_emb_explore = self.time_embedding_explore(step_ts)
                    noise_feature    = torch.cat([time_emb_explore, cond_emb], dim=-1)
                else:
                    noise_feature    = torch.cat([td_emb.detach(), cond_emb], dim=-1)
            noise_std = self.explore_noise_net.forward(noise_feature=noise_feature)
        return vel, noise_std if learn_exploration_noise else noise_std.detach()