File size: 32,426 Bytes
d180a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

def get_timestep_embedding(timesteps, embedding_dim):
    """Build sinusoidal timestep embeddings.

    Args:
        timesteps (torch.Tensor): A 1-D Tensor of N timesteps.
        embedding_dim (int): The dimension of the embedding.

    Returns:
        torch.Tensor: N x embedding_dim Tensor of positional embeddings.
    """
    assert len(timesteps.shape) == 1

    half_dim = embedding_dim // 2
    emb = np.log(10000) / (half_dim - 1)
    emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb)
    emb = emb.cuda() # Move embedding to CUDA device
    emb = timesteps.float()[:, None] * emb[None, :]
    emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
    if embedding_dim % 2 == 1:  # Zero pad if embedding_dim is odd
        emb = torch.nn.functional.pad(emb, (0, 1, 0, 0))
    return emb

class Attention(nn.Module):
    """A simple attention layer to get weights for attributes."""
    def __init__(self, embedding_dim):
        super(Attention, self).__init__()
        self.fc = nn.Linear(embedding_dim, 1)

    def forward(self, x):
        # x shape: (batch_size, num_attributes, embedding_dim)
        weights = self.fc(x)  # shape: (batch_size, num_attributes, 1)
        # Apply softmax along the attributes dimension to get attention weights.
        weights = F.softmax(weights, dim=1)
        return weights
    
class WideAndDeep(nn.Module):
    """Network to combine attribute (start/end points) and prototype embeddings."""
    def __init__(self, in_channels, embedding_dim=512):
        super(WideAndDeep, self).__init__()
        
        # Process start point and end point independently
        self.start_fc1 = nn.Linear(in_channels, embedding_dim)
        self.start_fc2 = nn.Linear(embedding_dim, embedding_dim)
        
        self.end_fc1 = nn.Linear(in_channels, embedding_dim)
        self.end_fc2 = nn.Linear(embedding_dim, embedding_dim)
        
        # Process prototype features
        self.prototype_fc1 = nn.Linear(512, embedding_dim)
        self.prototype_fc2 = nn.Linear(embedding_dim, embedding_dim)
        
        self.relu = nn.ReLU()
        
    def forward(self, attr, prototype):
        # attr shape: (batch_size, num_features, traj_length)
        # prototype shape: (batch_size, prototype_embedding_dim) - assuming N_CLUSTER is handled before or it's single prototype
        start_point = attr[:, :, 0].float() # First point in trajectory features
        end_point = attr[:, :, -1].float()  # Last point in trajectory features
        
        # Process start point features
        start_x = self.start_fc1(start_point)
        start_x = self.relu(start_x)
        start_embed = self.start_fc2(start_x)
        
        # Process end point features
        end_x = self.end_fc1(end_point)
        end_x = self.relu(end_x)
        end_embed = self.end_fc2(end_x)
        
        # Combine the processed start and end point features
        attr_embed = start_embed + end_embed
        
        # Process prototype features
        proto_x = self.prototype_fc1(prototype)
        proto_x = self.relu(proto_x)
        proto_embed = self.prototype_fc2(proto_x)
        
        # Combine the processed attribute and prototype features
        combined_embed = attr_embed + proto_embed # Simple addition for combination
        
        return combined_embed


def nonlinearity(x):
    # Swish activation function (SiLU)
    return x * torch.sigmoid(x)

def Normalize(in_channels):
    """Group normalization."""
    return torch.nn.GroupNorm(num_groups=32,
                              num_channels=in_channels,
                              eps=1e-6,
                              affine=True)
    
class Upsample(nn.Module):
    """Upsampling layer, optionally with a 1D convolution."""
    def __init__(self, in_channels, with_conv=True):
        super().__init__()
        self.with_conv = with_conv
        if self.with_conv:
            self.conv = torch.nn.Conv1d(in_channels,
                                        in_channels,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)

    def forward(self, x):
        x = torch.nn.functional.interpolate(x,
                                            scale_factor=2.0,
                                            mode="nearest") # Upsample using nearest neighbor
        if self.with_conv:
            x = self.conv(x)
        return x


class Downsample(nn.Module):
    """Downsampling layer, optionally with a 1D convolution."""
    def __init__(self, in_channels, with_conv=True):
        super().__init__()
        self.with_conv = with_conv
        if self.with_conv:
            # No asymmetric padding in torch.nn.Conv1d, must do it ourselves via F.pad.
            self.conv = torch.nn.Conv1d(in_channels,
                                        in_channels,
                                        kernel_size=3,
                                        stride=2,
                                        padding=0)

    def forward(self, x):
        if self.with_conv:
            pad = (1, 1) # Padding for kernel_size=3, stride=2 to maintain roughly half size
            x = torch.nn.functional.pad(x, pad, mode="constant", value=0)
            x = self.conv(x)
        else:
            x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) # Avg pool if no conv
        return x


class ResnetBlock(nn.Module):
    """Residual block for the U-Net."""
    def __init__(self,
                 in_channels,
                 out_channels=None,
                 conv_shortcut=False,
                 dropout=0.1,
                 temb_channels=512):
        super().__init__()
        self.in_channels = in_channels
        out_channels = in_channels if out_channels is None else out_channels
        self.out_channels = out_channels
        self.use_conv_shortcut = conv_shortcut

        self.norm1 = Normalize(in_channels)
        self.conv1 = torch.nn.Conv1d(in_channels,
                                     out_channels,
                                     kernel_size=3,
                                     stride=1,
                                     padding=1)
        self.temb_proj = torch.nn.Linear(temb_channels, out_channels)
        self.norm2 = Normalize(out_channels)
        self.dropout = torch.nn.Dropout(dropout)
        self.conv2 = torch.nn.Conv1d(out_channels,
                                     out_channels,
                                     kernel_size=3,
                                     stride=1,
                                     padding=1)
        if self.in_channels != self.out_channels:
            if self.use_conv_shortcut:
                self.conv_shortcut = torch.nn.Conv1d(in_channels,
                                                     out_channels,
                                                     kernel_size=3, # Convolutional shortcut
                                                     stride=1,
                                                     padding=1)
            else:
                self.nin_shortcut = torch.nn.Conv1d(in_channels,
                                                    out_channels,
                                                    kernel_size=1, # 1x1 convolution (Network-in-Network) shortcut
                                                    stride=1,
                                                    padding=0)

    def forward(self, x, temb):
        h = x
        h = self.norm1(h)
        h = nonlinearity(h)
        h = self.conv1(h)
        h = h + self.temb_proj(nonlinearity(temb))[:, :, None]
        h = self.norm2(h)
        h = nonlinearity(h)
        h = self.dropout(h)
        h = self.conv2(h)

        if self.in_channels != self.out_channels:
            if self.use_conv_shortcut:
                x = self.conv_shortcut(x)
            else:
                x = self.nin_shortcut(x)

        return x + h


class AttnBlock(nn.Module):
    """Self-attention block for the U-Net."""
    def __init__(self, in_channels):
        super().__init__()
        self.in_channels = in_channels

        self.norm = Normalize(in_channels)
        self.q = torch.nn.Conv1d(in_channels,
                                 in_channels,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0)
        self.k = torch.nn.Conv1d(in_channels,
                                 in_channels,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0)
        self.v = torch.nn.Conv1d(in_channels,
                                 in_channels,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0)
        self.proj_out = torch.nn.Conv1d(in_channels,
                                        in_channels,
                                        kernel_size=1,
                                        stride=1,
                                        padding=0)

    def forward(self, x):
        h_ = x
        h_ = self.norm(h_)
        q = self.q(h_) # Query
        k = self.k(h_) # Key
        v = self.v(h_) # Value
        b, c, w = q.shape
        q = q.permute(0, 2, 1)  # b,w,c (sequence_length, channels)
        w_ = torch.bmm(q, k)  # b,w,w (attention scores: q @ k.T)
        w_ = w_ * (int(c)**(-0.5)) # Scale by sqrt(channel_dim)
        w_ = torch.nn.functional.softmax(w_, dim=2) # Softmax over scores
        # attend to values
        w_ = w_.permute(0, 2, 1)  # b,w,w (transpose back for v @ w_ if v is b,c,w)
        h_ = torch.bmm(v, w_) # Weighted sum of values
        h_ = h_.reshape(b, c, w)

        h_ = self.proj_out(h_)

        return x + h_ # Add residual connection
    
    
class Model(nn.Module):
    """The core U-Net model for the diffusion process."""
    def __init__(self, config):
        super(Model, self).__init__()
        self.config = config
        ch, out_ch, ch_mult = config.model.ch, config.model.out_ch, tuple(config.model.ch_mult)
        num_res_blocks = config.model.num_res_blocks
        attn_resolutions = config.model.attn_resolutions
        dropout = config.model.dropout
        in_channels = config.model.in_channels
        resolution = config.data.traj_length
        resamp_with_conv = config.model.resamp_with_conv
        num_timesteps = config.diffusion.num_diffusion_timesteps
        
        if config.model.type == 'bayesian':
            self.logvar = nn.Parameter(torch.zeros(num_timesteps))
            
        self.ch = ch
        self.temb_ch = self.ch * 4
        self.num_resolutions = len(ch_mult)
        self.num_res_blocks = num_res_blocks
        self.resolution = resolution
        self.in_channels = in_channels
        
        # timestep embedding
        self.temb = nn.Module()
        self.temb.dense = nn.ModuleList([
            torch.nn.Linear(self.ch, self.temb_ch),
            torch.nn.Linear(self.temb_ch, self.temb_ch),
        ])

        # downsampling
        self.conv_in = torch.nn.Conv1d(in_channels, # in_channels related to embedding_dim, not traj_length. Input format (batch_size, embedding_dim, traj_length)
                                       self.ch,
                                       kernel_size=3,
                                       stride=1,
                                       padding=1)

        curr_res = resolution
        in_ch_mult = (1, ) + ch_mult
        self.down = nn.ModuleList()
        block_in = None
        for i_level in range(self.num_resolutions):
            block = nn.ModuleList()
            attn = nn.ModuleList()
            block_in = ch * in_ch_mult[i_level]
            block_out = ch * ch_mult[i_level]
            for i_block in range(self.num_res_blocks):
                block.append(
                    ResnetBlock(in_channels=block_in,
                                out_channels=block_out,
                                temb_channels=self.temb_ch,
                                dropout=dropout))
                block_in = block_out
                if curr_res in attn_resolutions:
                    attn.append(AttnBlock(block_in))
            down = nn.Module()
            down.block = block
            down.attn = attn
            if i_level != self.num_resolutions - 1:
                down.downsample = Downsample(block_in, resamp_with_conv)
                curr_res = curr_res // 2
            self.down.append(down)

        # middle block
        self.mid = nn.Module()
        self.mid.block_1 = ResnetBlock(in_channels=block_in,
                                       out_channels=block_in,
                                       temb_channels=self.temb_ch,
                                       dropout=dropout)
        self.mid.attn_1 = AttnBlock(block_in)
        self.mid.block_2 = ResnetBlock(in_channels=block_in,
                                       out_channels=block_in,
                                       temb_channels=self.temb_ch,
                                       dropout=dropout)

        # upsampling
        self.up = nn.ModuleList()
        for i_level in reversed(range(self.num_resolutions)):
            block = nn.ModuleList()
            attn = nn.ModuleList()
            block_out = ch * ch_mult[i_level]
            skip_in = ch * ch_mult[i_level]
            for i_block in range(self.num_res_blocks + 1):
                if i_block == self.num_res_blocks:
                    skip_in = ch * in_ch_mult[i_level]
                block.append(
                    ResnetBlock(in_channels=block_in + skip_in,
                                out_channels=block_out,
                                temb_channels=self.temb_ch,
                                dropout=dropout))
                block_in = block_out
                if curr_res in attn_resolutions:
                    attn.append(AttnBlock(block_in))
            up = nn.Module()
            up.block = block
            up.attn = attn
            if i_level != 0:
                up.upsample = Upsample(block_in, resamp_with_conv)
                curr_res = curr_res * 2
            self.up.insert(0, up)  # Prepend to get consistent order for upsampling path

        # end
        self.norm_out = Normalize(block_in)
        self.conv_out = torch.nn.Conv1d(block_in,
                                        out_ch,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
        
    def forward(self, x, t, extra_embed=None):
        assert x.shape[2] == self.resolution # Ensure input trajectory length matches model resolution

        # timestep embedding
        temb = get_timestep_embedding(t, self.ch)
        temb = self.temb.dense[0](temb)
        temb = nonlinearity(temb)
        temb = self.temb.dense[1](temb)
        if extra_embed is not None:
            temb = temb + extra_embed

        # downsampling
        hs = [self.conv_in(x)] # List to store hidden states for skip connections
        # print(hs[-1].shape)
        for i_level in range(self.num_resolutions):
            for i_block in range(self.num_res_blocks):
                h = self.down[i_level].block[i_block](hs[-1], temb)
                # print(i_level, i_block, h.shape)
                if len(self.down[i_level].attn) > 0:
                    h = self.down[i_level].attn[i_block](h)
                hs.append(h)
            if i_level != self.num_resolutions - 1:
                hs.append(self.down[i_level].downsample(hs[-1]))

        # middle
        # print(hs[-1].shape)
        # print(len(hs))
        h = hs[-1]  # Last hidden state from downsampling path
        h = self.mid.block_1(h, temb)
        h = self.mid.attn_1(h)
        h = self.mid.block_2(h, temb)
        # print(h.shape)
        # upsampling
        for i_level in reversed(range(self.num_resolutions)):
            for i_block in range(self.num_res_blocks + 1):
                ht = hs.pop() # Get corresponding hidden state from downsampling path
                if ht.size(-1) != h.size(-1):
                    # Pad if spatial dimensions do not match (can happen with odd resolutions)
                    h = torch.nn.functional.pad(h,
                                                (0, ht.size(-1) - h.size(-1)))
                h = self.up[i_level].block[i_block](torch.cat([h, ht], dim=1), # Concatenate skip connection
                                                    temb)
                # print(i_level, i_block, h.shape)
                if len(self.up[i_level].attn) > 0:
                    h = self.up[i_level].attn[i_block](h)
            if i_level != 0:
                h = self.up[i_level].upsample(h)

        # end
        h = self.norm_out(h)
        h = nonlinearity(h)
        h = self.conv_out(h)
        return h
    
class Guide_UNet(nn.Module):
    """A U-Net model guided by attribute and prototype embeddings."""
    def __init__(self, config):
        super(Guide_UNet, self).__init__()
        self.config = config
        self.in_channels = config.model.in_channels
        self.ch = config.model.ch * 4
        self.attr_dim = config.model.attr_dim
        self.guidance_scale = config.model.guidance_scale
        self.unet = Model(config)
        self.guide_emb = WideAndDeep(self.in_channels, self.ch)
        self.place_emb = WideAndDeep(self.in_channels, self.ch)
        
    def forward(self, x, t, attr, prototype):
        guide_emb = self.guide_emb(attr, prototype) # Conditional embedding
        
        target_device = attr.device # Get device from an existing input tensor
        place_vector = torch.zeros(attr.shape, device=target_device)
        place_prototype = torch.zeros(prototype.shape, device=target_device)
        
        place_emb = self.place_emb(place_vector, place_prototype) # Unconditional embedding
        
        cond_noise = self.unet(x, t, guide_emb) # Conditioned UNet pass
        uncond_noise = self.unet(x, t, place_emb) # Unconditioned UNet pass (for classifier-free guidance)
        
        # Classifier-free guidance
        pred_noise = cond_noise + self.guidance_scale * (cond_noise -
                                                         uncond_noise)
        return pred_noise
    
    
class WeightedLoss(nn.Module):
    """Base class for weighted losses."""
    def __init__(self):
        super(WeightedLoss, self).__init__()
        
    def forward(self, pred, target, weighted=1.0):
        """ 
        pred, target:[batch_size, 2, traj_length]
        """
        loss = self._loss(pred, target)
        weightedLoss = (loss * weighted).mean() # Apply weights and average
        # loss = self._loss(weighted * pred, weighted * target)
        # weightedLoss = loss.mean()
        return weightedLoss
    
class WeightedL1(WeightedLoss):
    """Weighted L1 Loss (Mean Absolute Error)."""
    def _loss(self, pred, target):
        return torch.abs(pred - target)


class WeightedL2(WeightedLoss):
    """Weighted L2 Loss (Mean Squared Error)."""
    def _loss(self, pred, target):
        return F.mse_loss(pred, target, reduction='none')

class WeightedL3(WeightedLoss):
    """A custom weighted L3-like loss, where weights depend on the error magnitude."""
    def __init__(self, base_weight=1000.0, scale_factor=10000.0):
        super(WeightedL3, self).__init__()
        self.base_weight = base_weight
        self.scale_factor = scale_factor

    def _loss(self, pred, target):
        error = F.mse_loss(pred, target, reduction='none')
        weight = self.base_weight + self.scale_factor * error
        loss = weight * torch.abs(pred - target)
        return loss
Losses = {
    'l1': WeightedL1,
    'l2': WeightedL2,
    'l3': WeightedL3,
}

def extract(a, t, x_shape):
    """Extracts values from a (typically constants like alphas) at given timesteps t 
       and reshapes them to match the batch shape x_shape.
    """
    b, *_ = t.shape
    out = a.gather(-1, t)
    return out.reshape(b, *((1,) * (len(x_shape) - 1))) # Reshape to (b, 1, 1, ...) for broadcasting


class Diffusion(nn.Module):
    """Denoising Diffusion Probabilistic Model (DDPM).
    This class now also includes DDIM sampling capabilities.
    """
    def __init__(self, loss_type, config, clip_denoised=True, predict_epsilon=True, **kwargs):
        super(Diffusion, self).__init__()
        self.predict_epsilon = predict_epsilon
        self.T = config.diffusion.num_diffusion_timesteps
        self.model = Guide_UNet(config)
        self.beta_schedule = config.diffusion.beta_schedule
        self.beta_start = config.diffusion.beta_start
        self.beta_end = config.diffusion.beta_end
        
        if self.beta_schedule == "linear":
            betas = torch.linspace(self.beta_start, self.beta_end, self.T, dtype=torch.float32)
        elif self.beta_schedule == "cosine":
            # Implement cosine schedule
            pass
        else:
            raise ValueError(f"Unsupported beta_schedule: {self.beta_schedule}")
            
        alphas = 1.0 - betas
        alpha_cumprod = torch.cumprod(alphas, axis=0)
        alpha_cumprod_prev = torch.cat([torch.ones(1, device=betas.device), alpha_cumprod[:-1]])

        self.register_buffer("betas", betas)
        self.register_buffer("alphas", alphas)
        self.register_buffer("alpha_cumprod", alpha_cumprod)
        self.register_buffer("alpha_cumprod_prev", alpha_cumprod_prev)
        
        # Parameters for q(x_t | x_0) (forward process - DDPM & DDIM)
        self.register_buffer("sqrt_alphas_cumprod", torch.sqrt(alpha_cumprod))
        self.register_buffer("sqrt_one_minus_alphas_cumprod", torch.sqrt(1.0 - alpha_cumprod))
        
        # Parameters for DDPM reverse process posterior q(x_{t-1} | x_t, x_0)
        posterior_variance = betas * (1.0 - alpha_cumprod_prev) / (1.0 - alpha_cumprod)
        self.register_buffer("posterior_variance", posterior_variance)
        self.register_buffer("posterior_log_variance_clipped", torch.log(posterior_variance.clamp(min=1e-20)))
        self.register_buffer("posterior_mean_coef1", betas * torch.sqrt(alpha_cumprod_prev) / (1.0 - alpha_cumprod))
        self.register_buffer("posterior_mean_coef2", (1.0 - alpha_cumprod_prev) * torch.sqrt(alphas) / (1.0 - alpha_cumprod))
        
        # Parameters for computing x_0 from x_t and noise (used in DDPM prediction and DDIM sampling)
        self.register_buffer("sqrt_recip_alphas_cumprod", torch.sqrt(1.0 / alpha_cumprod))
        self.register_buffer("sqrt_recipminus_alphas_cumprod", torch.sqrt(1.0 / alpha_cumprod - 1))
        
        self.loss_fn = Losses[loss_type]()
        
    def q_posterior(self, x_start, x, t):
        """Compute the mean, variance, and log variance of the posterior q(x_{t-1} | x_t, x_0)."""
        posterior_mean = (
            extract(self.posterior_mean_coef1, t, x.shape) * x_start
            + extract(self.posterior_mean_coef2, t, x.shape) * x
        )
        posterior_variance = extract(self.posterior_variance, t, x.shape)
        posterior_log_variance = extract(self.posterior_log_variance_clipped, t, x.shape)
        return posterior_mean, posterior_variance, posterior_log_variance

    def predict_start_from_noise(self, x, t, pred_noise):
        """Compute x_0 from x_t and predicted noise epsilon_theta(x_t, t).
           Used by both DDPM and DDIM.
        """
        return (
            extract(self.sqrt_recip_alphas_cumprod, t, x.shape) * x
            - extract(self.sqrt_recipminus_alphas_cumprod, t, x.shape) * pred_noise
        )

    def p_mean_variance(self, x, t, attr, prototype):
        """Compute the mean and variance of the reverse process p_theta(x_{t-1} | x_t)."""
        pred_noise = self.model(x, t, attr, prototype)
        x_recon = self.predict_start_from_noise(x, t, pred_noise) # Predict x0
        model_mean, posterior_variance, posterior_log_variance = self.q_posterior(x_recon, x, t)
        return model_mean, posterior_log_variance

    def p_sample(self, x, t, attr, prototype, start_end_info):
        """Sample x_{t-1} from the model p_theta(x_{t-1} | x_t) (DDPM step)."""
        b = x.shape[0]
        model_mean, model_log_variance = self.p_mean_variance(x, t, attr, prototype)
        noise = torch.randn_like(x)

        nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1))) # No noise when t=0
        x = model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise

        # Fix the first and last point for trajectory interpolation
        x[:, :, 0] = start_end_info[:, :, 0]
        x[:, :, -1] = start_end_info[:, :, -1]
        return x

    def p_sample_loop(self, test_x0, attr, prototype, *args, **kwargs):
        """DDPM sampling loop to generate x_0 from x_T (noise)."""
        batch_size = attr.shape[0]
        device = attr.device # Assuming attr is on the correct device

        x = torch.randn(attr.shape, requires_grad=False, device=device) # Start with pure noise
        start_end_info = test_x0.clone() # Contains the ground truth start and end points
        
        # Fix the first and last point from the start
        x[:, :, 0] = start_end_info[:, :, 0]
        x[:, :, -1] = start_end_info[:, :, -1]
        
        for i in reversed(range(0, self.T)): # Iterate from T-1 down to 0
            t = torch.full((batch_size,), i, dtype=torch.long, device=device)
            x = self.p_sample(x, t, attr, prototype, start_end_info)
        return x

    # --------------------- DDIM Sampling Methods ---------------------
    def ddim_sample(self, x, t, t_prev, attr, prototype, start_end_info, eta=0.0):
        """
        DDIM sampling step from t to t_prev.
        eta: Controls stochasticity. 0 for DDIM (deterministic), 1 for DDPM-like (stochastic).
        """
        # Ensure model is on the same device as x
        self.model.to(x.device)
        
        pred_noise = self.model(x, t, attr, prototype)
        x_0_pred = self.predict_start_from_noise(x, t, pred_noise)
        
        x_0_pred[:, :, 0] = start_end_info[:, :, 0]
        x_0_pred[:, :, -1] = start_end_info[:, :, -1]
        
        alpha_cumprod_t = extract(self.alpha_cumprod, t, x.shape)
        alpha_cumprod_t_prev = extract(self.alpha_cumprod, t_prev, x.shape) if t_prev.all() >= 0 else torch.ones_like(alpha_cumprod_t)
        
        sigma_t = eta * torch.sqrt((1 - alpha_cumprod_t_prev) / (1 - alpha_cumprod_t) * (1 - alpha_cumprod_t / alpha_cumprod_t_prev))
        
        c1 = torch.sqrt(alpha_cumprod_t_prev)
        c2 = torch.sqrt(1 - alpha_cumprod_t_prev - sigma_t**2)
        
        noise_cond = torch.zeros_like(x)
        if eta > 0:
            noise_cond = torch.randn_like(x)
            noise_cond[:, :, 0] = 0
            noise_cond[:, :, -1] = 0
        
        x_prev = c1 * x_0_pred + c2 * pred_noise + sigma_t * noise_cond
        
        x_prev[:, :, 0] = start_end_info[:, :, 0]
        x_prev[:, :, -1] = start_end_info[:, :, -1]
        
        return x_prev
        
    def ddim_sample_loop(self, test_x0, attr, prototype, num_steps=50, eta=0.0):
        """
        DDIM sampling loop. Can use fewer steps than original diffusion process.
        num_steps: Number of sampling steps (can be less than self.T).
        eta: Controls stochasticity (0 for deterministic, 1 for fully stochastic).
        """
        batch_size = attr.shape[0]
        device = attr.device # Assuming attr is on the correct device
        
        x = torch.randn(attr.shape, requires_grad=False, device=device)
        start_end_info = test_x0.clone()
        
        x[:, :, 0] = start_end_info[:, :, 0]
        x[:, :, -1] = start_end_info[:, :, -1]
        
        times = torch.linspace(self.T - 1, 0, num_steps + 1, device=device).long() # Ensure times tensor is on the same device
        
        for i in range(num_steps):
            t = times[i]
            t_next = times[i + 1]
            # Create full tensors for t and t_next for batch processing
            t_tensor = torch.full((batch_size,), t.item(), dtype=torch.long, device=device)
            t_next_tensor = torch.full((batch_size,), t_next.item(), dtype=torch.long, device=device)
            
            x = self.ddim_sample(x, t_tensor, t_next_tensor, attr, prototype, start_end_info, eta)
            
        return x

    # --------------------- Unified Sampling Entry Point ---------------------
    def sample(self, test_x0, attr, prototype, sampling_type='ddpm', 
               ddim_num_steps=50, ddim_eta=0.0, *args, **kwargs):
        """Generate samples using either DDPM or DDIM.

        Args:
            test_x0 (torch.Tensor): Tensor containing ground truth data, primarily used for start/end points.
            attr (torch.Tensor): Attributes for conditioning.
            prototype (torch.Tensor): Prototypes for conditioning.
            sampling_type (str, optional): 'ddpm' or 'ddim'. Defaults to 'ddpm'.
            ddim_num_steps (int, optional): Number of steps for DDIM sampling. Defaults to 50.
            ddim_eta (float, optional): Eta for DDIM sampling. Defaults to 0.0.
        """
        self.model.eval() # Set model to evaluation mode for sampling
        with torch.no_grad():
            if sampling_type == 'ddpm':
                return self.p_sample_loop(test_x0, attr, prototype, *args, **kwargs)
            elif sampling_type == 'ddim':
                return self.ddim_sample_loop(test_x0, attr, prototype, 
                                             num_steps=ddim_num_steps, eta=ddim_eta)
            else:
                raise ValueError(f"Unsupported sampling_type: {sampling_type}. Choose 'ddpm' or 'ddim'.")

    #----------------------------------training----------------------------------#
    def q_sample(self, x_start, t, noise):
        """Sample x_t from x_0 using q(x_t | x_0) = sqrt(alpha_bar_t)x_0 + sqrt(1-alpha_bar_t)noise."""
        sample = (
            extract(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start +
            extract(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * noise
        )
        # Keep start and end points fixed during noising process as well (for interpolation task)
        sample[:, :, 0] = x_start[:, :, 0]
        sample[:, :, -1] = x_start[:, :, -1]
        return sample

    def p_losses(self, x_start, attr, prototype, t, weights=1.0):
        """Calculate the diffusion loss (typically MSE between predicted noise and actual noise).
           This is common for both DDPM and DDIM training.
        """
        noise = torch.randn_like(x_start)
        # For interpolation, noise is not added to the fixed start/end points
        noise[:, :, 0] = 0
        noise[:, :, -1] = 0
        
        x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise)

        x_recon = self.model(x_noisy, t, attr, prototype) # Model predicts noise or x0
        assert noise.shape == x_recon.shape

        if self.predict_epsilon:
            # Loss on the predicted noise, excluding start/end points
            loss = self.loss_fn(x_recon[:, :, 1:-1], noise[:, :, 1:-1], weights)
        else:
            # Loss on the predicted x0, excluding start/end points
            loss = self.loss_fn(x_recon[:, :, 1:-1], x_start[:, :, 1:-1], weights)

        return loss

    def trainer(self, x, attr, prototype, weights=1.0):
        """Performs a single training step. Common for DDPM and DDIM."""
        self.model.train() # Set model to training mode
        batch_size = len(x)
        t = torch.randint(0, self.T, (batch_size,), device=x.device).long() # Sample random timesteps on the same device as x
        return self.p_losses(x, attr, prototype, t, weights)

    def forward(self, test_x0, attr, prototype, sampling_type='ddpm', 
                ddim_num_steps=50, ddim_eta=0.0, *args, **kwargs):
        """Default forward pass calls the unified sampling method."""
        return self.sample(test_x0, attr, prototype, 
                           sampling_type=sampling_type, 
                           ddim_num_steps=ddim_num_steps, 
                           ddim_eta=ddim_eta, 
                           *args, **kwargs)