Skip to content

Samplers

This module contains functionality related to samplers.

DDMSampler

Bases: Sampler

Class for sampling from diffusion models using the DDPM/DDIM sampler.

Source code in src/diffusionlab/samplers.py
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
class DDMSampler(Sampler):
    """
    Class for sampling from diffusion models using the DDPM/DDIM sampler.
    """

    def _convert_to_x0(
        self,
        x: torch.Tensor,
        t: torch.Tensor,
        fx: torch.Tensor,
        fx_type: VectorFieldType,
    ):
        x0 = convert_vector_field_type(
            x,
            fx,
            self.diffusion_process.alpha(t),
            self.diffusion_process.sigma(t),
            self.diffusion_process.alpha_prime(t),
            self.diffusion_process.sigma_prime(t),
            fx_type,
            VectorFieldType.X0,
        )
        return x0

    def _ddpm_step_x0_tensor(
        self,
        x0: torch.Tensor,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        t1 = pad_shape_back(ts[idx + 1], x.shape)
        alpha_t = self.diffusion_process.alpha(t)
        sigma_t = self.diffusion_process.sigma(t)
        alpha_t1 = self.diffusion_process.alpha(t1)
        sigma_t1 = self.diffusion_process.sigma(t1)

        r11 = (alpha_t / alpha_t1) * (sigma_t1 / sigma_t)
        r12 = r11 * (sigma_t1 / sigma_t)
        r22 = r12 * (alpha_t / alpha_t1)

        mean = r12 * x + alpha_t1 * (1 - r22) * x0
        std = sigma_t1 * (1 - r11**2) ** (1 / 2)
        return mean + std * zs[idx]

    def _ddim_step_x0_tensor(
        self,
        x0: torch.Tensor,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        t1 = pad_shape_back(ts[idx + 1], x.shape)
        alpha_t = self.diffusion_process.alpha(t)
        sigma_t = self.diffusion_process.sigma(t)
        alpha_t1 = self.diffusion_process.alpha(t1)
        sigma_t1 = self.diffusion_process.sigma(t1)

        r01 = sigma_t1 / sigma_t
        r11 = (alpha_t / alpha_t1) * r01

        mean = r01 * x + alpha_t1 * (1 - r11) * x0
        return mean

    def sample_step_deterministic_x0(
        self,
        x0: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        x0_value = x0(x, self._fix_t_shape(x, ts[idx]))
        return self._ddim_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_stochastic_x0(
        self,
        x0: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        x0_value = x0(x, self._fix_t_shape(x, ts[idx]))
        return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_deterministic_score(
        self,
        score: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        score_value = score(x, self._fix_t_shape(x, t))
        x0_value = self._convert_to_x0(x, t, score_value, VectorFieldType.SCORE)
        return self._ddim_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_stochastic_score(
        self,
        score: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        score_value = score(x, self._fix_t_shape(x, t))
        x0_value = self._convert_to_x0(x, t, score_value, VectorFieldType.SCORE)
        return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_deterministic_eps(
        self,
        eps: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        eps_value = eps(x, self._fix_t_shape(x, t))
        x0_value = self._convert_to_x0(x, t, eps_value, VectorFieldType.EPS)
        return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_stochastic_eps(
        self,
        eps: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        eps_value = eps(x, self._fix_t_shape(x, t))
        x0_value = self._convert_to_x0(x, t, eps_value, VectorFieldType.EPS)
        return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_deterministic_v(
        self,
        v: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        v_value = v(x, self._fix_t_shape(x, t))
        x0_value = self._convert_to_x0(x, t, v_value, VectorFieldType.V)
        return self._ddim_step_x0_tensor(x0_value, x, zs, idx, ts)

    def sample_step_stochastic_v(
        self,
        v: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        t = pad_shape_back(ts[idx], x.shape)
        v_value = v(x, self._fix_t_shape(x, t))
        x0_value = self._convert_to_x0(x, t, v_value, VectorFieldType.V)
        return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_deterministic_eps(eps, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
886
887
888
889
890
891
892
893
894
895
896
897
def sample_step_deterministic_eps(
    self,
    eps: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    t = pad_shape_back(ts[idx], x.shape)
    eps_value = eps(x, self._fix_t_shape(x, t))
    x0_value = self._convert_to_x0(x, t, eps_value, VectorFieldType.EPS)
    return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_deterministic_score(score, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
860
861
862
863
864
865
866
867
868
869
870
871
def sample_step_deterministic_score(
    self,
    score: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    t = pad_shape_back(ts[idx], x.shape)
    score_value = score(x, self._fix_t_shape(x, t))
    x0_value = self._convert_to_x0(x, t, score_value, VectorFieldType.SCORE)
    return self._ddim_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_deterministic_v(v, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
912
913
914
915
916
917
918
919
920
921
922
923
def sample_step_deterministic_v(
    self,
    v: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    t = pad_shape_back(ts[idx], x.shape)
    v_value = v(x, self._fix_t_shape(x, t))
    x0_value = self._convert_to_x0(x, t, v_value, VectorFieldType.V)
    return self._ddim_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_deterministic_x0(x0, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
838
839
840
841
842
843
844
845
846
847
def sample_step_deterministic_x0(
    self,
    x0: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    x0_value = x0(x, self._fix_t_shape(x, ts[idx]))
    return self._ddim_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_stochastic_eps(eps, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
899
900
901
902
903
904
905
906
907
908
909
910
def sample_step_stochastic_eps(
    self,
    eps: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    t = pad_shape_back(ts[idx], x.shape)
    eps_value = eps(x, self._fix_t_shape(x, t))
    x0_value = self._convert_to_x0(x, t, eps_value, VectorFieldType.EPS)
    return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_stochastic_score(score, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
873
874
875
876
877
878
879
880
881
882
883
884
def sample_step_stochastic_score(
    self,
    score: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    t = pad_shape_back(ts[idx], x.shape)
    score_value = score(x, self._fix_t_shape(x, t))
    x0_value = self._convert_to_x0(x, t, score_value, VectorFieldType.SCORE)
    return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_stochastic_v(v, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
925
926
927
928
929
930
931
932
933
934
935
936
def sample_step_stochastic_v(
    self,
    v: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    t = pad_shape_back(ts[idx], x.shape)
    v_value = v(x, self._fix_t_shape(x, t))
    x0_value = self._convert_to_x0(x, t, v_value, VectorFieldType.V)
    return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

sample_step_stochastic_x0(x0, x, zs, idx, ts)

Source code in src/diffusionlab/samplers.py
849
850
851
852
853
854
855
856
857
858
def sample_step_stochastic_x0(
    self,
    x0: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    x0_value = x0(x, self._fix_t_shape(x, ts[idx]))
    return self._ddpm_step_x0_tensor(x0_value, x, zs, idx, ts)

EulerMaruyamaSampler

Bases: Sampler

Source code in src/diffusionlab/samplers.py
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
class EulerMaruyamaSampler(Sampler):
    def _get_step_quantities(
        self, zs: torch.Tensor, idx: int, ts: torch.Tensor
    ) -> Tuple[
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
        torch.Tensor,
    ]:
        """
        Calculate various quantities needed for a sampling step.

        This helper method computes time-dependent quantities used in the sampling
        step functions.

        Args:
            zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
                where L is the number of time steps, N is the batch size, and D represents the data dimensions
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)
                where L is the number of time steps

        Returns:
            Tuple: A tuple containing various time-dependent quantities:
                  - t (torch.Tensor): Current time, of shape (1*), where 1* is a tuple with the same number of dimensions as (N, D*)
                  - t1 (torch.Tensor): Next time, of shape (1*)
                  - alpha_t (torch.Tensor): Alpha at current time, of shape (1*)
                  - sigma_t (torch.Tensor): Sigma at current time, of shape (1*)
                  - alpha_prime_t (torch.Tensor): Derivative of alpha at current time, of shape (1*)
                  - sigma_prime_t (torch.Tensor): Derivative of sigma at current time, of shape (1*)
                  - dt (torch.Tensor): Time difference, of shape (1*)
                  - dwt (torch.Tensor): Scaled noise, of shape (N, *D)
                  - alpha_ratio_t (torch.Tensor): alpha_prime_t / alpha_t, of shape (1*)
                  - sigma_ratio_t (torch.Tensor): sigma_prime_t / sigma_t, of shape (1*)
                  - diff_ratio_t (torch.Tensor): sigma_ratio_t - alpha_ratio_t, of shape (1*)
        """
        x_shape = zs.shape[1:]
        t = pad_shape_back(ts[idx], x_shape)
        t1 = pad_shape_back(ts[idx + 1], x_shape)
        dt = t1 - t
        dwt = zs[idx] * torch.sqrt(-dt)

        alpha_t = pad_shape_back(self.diffusion_process.alpha(ts[idx]), x_shape)
        sigma_t = pad_shape_back(self.diffusion_process.sigma(ts[idx]), x_shape)
        alpha_prime_t = pad_shape_back(
            self.diffusion_process.alpha_prime(ts[idx]), x_shape
        )
        sigma_prime_t = pad_shape_back(
            self.diffusion_process.sigma_prime(ts[idx]), x_shape
        )
        alpha_ratio_t = alpha_prime_t / alpha_t
        sigma_ratio_t = sigma_prime_t / sigma_t
        diff_ratio_t = sigma_ratio_t - alpha_ratio_t
        return (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        )

    def sample_step_deterministic_score(
        self,
        score: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the score vector field.

        Args:
            score (VectorField): The score vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = alpha_ratio_t * x - (sigma_t**2) * diff_ratio_t * score(
            x, self._fix_t_shape(x, t)
        )
        return x + drift_t * dt

    def sample_step_stochastic_score(
        self,
        score: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform a stochastic sampling step using the score vector field.

        This implements the stochastic reverse SDE for score-based models using the
        Euler-Maruyama discretization method.

        Args:
            score (VectorField): The score vector field model
            x (torch.Tensor): The current state tensor, of shape (N, *D)
                where N is the batch size and D represents the data dimensions
            zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
                where L is the number of time steps
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)
                where L is the number of time steps

        Returns:
            torch.Tensor: The next state tensor, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)

        # Compute score at current state
        score_x_t = score(x, ts[idx])

        # Compute drift and diffusion terms
        drift = alpha_prime_t * x / alpha_t - sigma_t * sigma_prime_t * score_x_t
        diffusion = sigma_prime_t * dwt

        # Update state using Euler-Maruyama method
        x_next = x + drift * dt + diffusion

        return x_next

    def sample_step_deterministic_x0(
        self,
        x0: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the x0 vector field.

        Args:
            x0 (VectorField): The x0 vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = sigma_ratio_t * x - alpha_t * diff_ratio_t * x0(
            x, self._fix_t_shape(x, t)
        )
        return x + drift_t * dt

    def sample_step_stochastic_x0(
        self,
        x0: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of stochastic sampling using the x0 vector field.

        Args:
            x0 (VectorField): The x0 vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = (
            alpha_ratio_t + 2 * diff_ratio_t
        ) * x - 2 * alpha_t * diff_ratio_t * x0(x, self._fix_t_shape(x, t))
        diffusion_t = torch.sqrt(2 * diff_ratio_t) * sigma_t
        return x + drift_t * dt + diffusion_t * dwt

    def sample_step_deterministic_eps(
        self,
        eps: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the eps vector field.

        Args:
            eps (VectorField): The eps vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = alpha_ratio_t * x + sigma_t * diff_ratio_t * eps(
            x, self._fix_t_shape(x, t)
        )
        return x + drift_t * dt

    def sample_step_stochastic_eps(
        self,
        eps: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of stochastic sampling using the eps vector field.

        Args:
            eps (VectorField): The eps vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = alpha_ratio_t * x + 2 * sigma_t * diff_ratio_t * eps(
            x, self._fix_t_shape(x, t)
        )
        diffusion_t = torch.sqrt(2 * diff_ratio_t) * sigma_t
        return x + drift_t * dt + diffusion_t * dwt

    def sample_step_deterministic_v(
        self,
        v: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the v vector field.

        Args:
            v (VectorField): The velocity vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = v(x, self._fix_t_shape(x, t))
        return x + drift_t * dt

    def sample_step_stochastic_v(
        self,
        v: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of stochastic sampling using the v vector field.

        Args:
            v (VectorField): The velocity vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        (
            t,
            t1,
            alpha_t,
            sigma_t,
            alpha_prime_t,
            sigma_prime_t,
            dt,
            dwt,
            alpha_ratio_t,
            sigma_ratio_t,
            diff_ratio_t,
        ) = self._get_step_quantities(zs, idx, ts)
        drift_t = -alpha_ratio_t * x + v(x, self._fix_t_shape(x, t))
        diffusion_t = torch.sqrt(2 * diff_ratio_t) * sigma_t
        return x + drift_t * dt + diffusion_t * dwt

sample_step_deterministic_eps(eps, x, zs, idx, ts)

Perform one step of deterministic sampling using the eps vector field.

Parameters:

Name Type Description Default
eps VectorField

The eps vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_deterministic_eps(
    self,
    eps: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the eps vector field.

    Args:
        eps (VectorField): The eps vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = alpha_ratio_t * x + sigma_t * diff_ratio_t * eps(
        x, self._fix_t_shape(x, t)
    )
    return x + drift_t * dt

sample_step_deterministic_score(score, x, zs, idx, ts)

Perform one step of deterministic sampling using the score vector field.

Parameters:

Name Type Description Default
score VectorField

The score vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_deterministic_score(
    self,
    score: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the score vector field.

    Args:
        score (VectorField): The score vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = alpha_ratio_t * x - (sigma_t**2) * diff_ratio_t * score(
        x, self._fix_t_shape(x, t)
    )
    return x + drift_t * dt

sample_step_deterministic_v(v, x, zs, idx, ts)

Perform one step of deterministic sampling using the v vector field.

Parameters:

Name Type Description Default
v VectorField

The velocity vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_deterministic_v(
    self,
    v: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the v vector field.

    Args:
        v (VectorField): The velocity vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = v(x, self._fix_t_shape(x, t))
    return x + drift_t * dt

sample_step_deterministic_x0(x0, x, zs, idx, ts)

Perform one step of deterministic sampling using the x0 vector field.

Parameters:

Name Type Description Default
x0 VectorField

The x0 vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_deterministic_x0(
    self,
    x0: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the x0 vector field.

    Args:
        x0 (VectorField): The x0 vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = sigma_ratio_t * x - alpha_t * diff_ratio_t * x0(
        x, self._fix_t_shape(x, t)
    )
    return x + drift_t * dt

sample_step_stochastic_eps(eps, x, zs, idx, ts)

Perform one step of stochastic sampling using the eps vector field.

Parameters:

Name Type Description Default
eps VectorField

The eps vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors, of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_stochastic_eps(
    self,
    eps: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of stochastic sampling using the eps vector field.

    Args:
        eps (VectorField): The eps vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = alpha_ratio_t * x + 2 * sigma_t * diff_ratio_t * eps(
        x, self._fix_t_shape(x, t)
    )
    diffusion_t = torch.sqrt(2 * diff_ratio_t) * sigma_t
    return x + drift_t * dt + diffusion_t * dwt

sample_step_stochastic_score(score, x, zs, idx, ts)

Perform a stochastic sampling step using the score vector field.

This implements the stochastic reverse SDE for score-based models using the Euler-Maruyama discretization method.

Parameters:

Name Type Description Default
score VectorField

The score vector field model

required
x Tensor

The current state tensor, of shape (N, *D) where N is the batch size and D represents the data dimensions

required
zs Tensor

The noise tensors for stochastic sampling, of shape (L-1, N, *D) where L is the number of time steps

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,) where L is the number of time steps

required

Returns:

Type Description
Tensor

torch.Tensor: The next state tensor, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_stochastic_score(
    self,
    score: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform a stochastic sampling step using the score vector field.

    This implements the stochastic reverse SDE for score-based models using the
    Euler-Maruyama discretization method.

    Args:
        score (VectorField): The score vector field model
        x (torch.Tensor): The current state tensor, of shape (N, *D)
            where N is the batch size and D represents the data dimensions
        zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
            where L is the number of time steps
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)
            where L is the number of time steps

    Returns:
        torch.Tensor: The next state tensor, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)

    # Compute score at current state
    score_x_t = score(x, ts[idx])

    # Compute drift and diffusion terms
    drift = alpha_prime_t * x / alpha_t - sigma_t * sigma_prime_t * score_x_t
    diffusion = sigma_prime_t * dwt

    # Update state using Euler-Maruyama method
    x_next = x + drift * dt + diffusion

    return x_next

sample_step_stochastic_v(v, x, zs, idx, ts)

Perform one step of stochastic sampling using the v vector field.

Parameters:

Name Type Description Default
v VectorField

The velocity vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors, of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_stochastic_v(
    self,
    v: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of stochastic sampling using the v vector field.

    Args:
        v (VectorField): The velocity vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = -alpha_ratio_t * x + v(x, self._fix_t_shape(x, t))
    diffusion_t = torch.sqrt(2 * diff_ratio_t) * sigma_t
    return x + drift_t * dt + diffusion_t * dwt

sample_step_stochastic_x0(x0, x, zs, idx, ts)

Perform one step of stochastic sampling using the x0 vector field.

Parameters:

Name Type Description Default
x0 VectorField

The x0 vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors, of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_stochastic_x0(
    self,
    x0: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of stochastic sampling using the x0 vector field.

    Args:
        x0 (VectorField): The x0 vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    (
        t,
        t1,
        alpha_t,
        sigma_t,
        alpha_prime_t,
        sigma_prime_t,
        dt,
        dwt,
        alpha_ratio_t,
        sigma_ratio_t,
        diff_ratio_t,
    ) = self._get_step_quantities(zs, idx, ts)
    drift_t = (
        alpha_ratio_t + 2 * diff_ratio_t
    ) * x - 2 * alpha_t * diff_ratio_t * x0(x, self._fix_t_shape(x, t))
    diffusion_t = torch.sqrt(2 * diff_ratio_t) * sigma_t
    return x + drift_t * dt + diffusion_t * dwt

Sampler

Class for sampling from diffusion models using various vector field types.

A Sampler combines a diffusion process and a scheduler to generate samples from a trained diffusion model. It handles both the forward process (adding noise) and the reverse process (denoising/sampling).

The sampler supports different vector field types (SCORE, X0, EPS, V) and can perform both stochastic and deterministic sampling.

Attributes:

Name Type Description
diffusion_process DiffusionProcess

The diffusion process defining the forward and reverse dynamics

is_stochastic bool

Whether the reverse process is stochastic or deterministic

Source code in src/diffusionlab/samplers.py
 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
class Sampler:
    """
    Class for sampling from diffusion models using various vector field types.

    A Sampler combines a diffusion process and a scheduler to generate samples from
    a trained diffusion model. It handles both the forward process (adding noise) and
    the reverse process (denoising/sampling).

    The sampler supports different vector field types (SCORE, X0, EPS, V) and can perform
    both stochastic and deterministic sampling.

    Attributes:
        diffusion_process (DiffusionProcess): The diffusion process defining the forward and reverse dynamics
        is_stochastic (bool): Whether the reverse process is stochastic or deterministic
    """

    def __init__(
        self,
        diffusion_process: DiffusionProcess,
        is_stochastic: bool,
    ):
        """
        Initialize a sampler with a diffusion process and sampling strategy.

        Args:
            diffusion_process (DiffusionProcess): The diffusion process to use for sampling
            is_stochastic (bool): Whether the reverse process should be stochastic
        """
        self.diffusion_process: DiffusionProcess = diffusion_process
        self.is_stochastic: bool = is_stochastic

    def sample(
        self,
        vector_field: VectorField,
        x_init: torch.Tensor,
        zs: torch.Tensor,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Sample from the model using the reverse diffusion process.

        This method generates a sample by iteratively applying the appropriate sampling step
        function based on the vector field type.

        Args:
            vector_field (VectorField): The vector field model to use for sampling
            x_init (torch.Tensor): The initial noisy tensor to start sampling from, of shape (N, *D) where N is the batch size and D represents the data dimensions
            zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
                where L is the number of time steps
            ts (torch.Tensor): The time schedule for sampling, of shape (L,)
                where L is the number of time steps

        Returns:
            torch.Tensor: The generated sample, of shape (N, *D)
        """
        sample_step_function = self.get_sample_step_function(
            vector_field.vector_field_type
        )
        x = x_init
        for i in range(ts.shape[0] - 1):
            x = sample_step_function(vector_field, x, zs, i, ts)
        return x

    def sample_trajectory(
        self,
        vector_field: VectorField,
        x_init: torch.Tensor,
        zs: torch.Tensor,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Sample a trajectory from the model using the reverse diffusion process.

        This method is similar to sample() but returns the entire trajectory of
        intermediate samples rather than just the final sample.

        Args:
            vector_field (VectorField): The vector field model to use for sampling
            x_init (torch.Tensor): The initial noisy tensor to start sampling from, of shape (N, *D)
                where N is the batch size and D represents the data dimensions
            zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
                where L is the number of time steps
            ts (torch.Tensor): The time schedule for sampling, of shape (L,)
                where L is the number of time steps

        Returns:
            torch.Tensor: The generated trajectory, of shape (L, N, *D)
                where L is the number of time steps
        """
        sample_step_function = self.get_sample_step_function(
            vector_field.vector_field_type
        )
        xs = [x_init]
        x = x_init
        for i in range(ts.shape[0] - 1):
            x = sample_step_function(vector_field, x, zs, i, ts)
            xs.append(x)
        return torch.stack(xs)

    def get_sample_step_function(
        self, vector_field_type: VectorFieldType
    ) -> Callable[
        [VectorField, torch.Tensor, torch.Tensor, int, torch.Tensor], torch.Tensor
    ]:
        """
        Get the appropriate sampling step function based on the vector field type.

        This method selects the correct sampling function based on the vector field type
        and whether sampling is stochastic or deterministic.

        Args:
            vector_field_type (VectorFieldType): The type of vector field being used
                                                (SCORE, X0, EPS, or V)

        Returns:
            Callable: A function that performs one step of the sampling process with signature:
                     (vector_field, x, zs, idx, ts) -> next_x
                     where:
                     - vector_field is the model
                     - x is the current state tensor of shape (N, *D)
                       where N is the batch size and D represents the data dimensions
                     - zs is the noise tensors of shape (L-1, N, *D)
                       where L is the number of time steps
                     - idx is the current step index
                     - ts is the time steps tensor of shape (L,)
                       where L is the number of time steps
                     - next_x is the next state tensor of shape (N, *D)
        """
        f = None
        if self.is_stochastic:
            if vector_field_type == VectorFieldType.SCORE:
                f = self.sample_step_stochastic_score
            elif vector_field_type == VectorFieldType.X0:
                f = self.sample_step_stochastic_x0
            elif vector_field_type == VectorFieldType.EPS:
                f = self.sample_step_stochastic_eps
            elif vector_field_type == VectorFieldType.V:
                f = self.sample_step_stochastic_v
        else:
            if vector_field_type == VectorFieldType.SCORE:
                f = self.sample_step_deterministic_score
            elif vector_field_type == VectorFieldType.X0:
                f = self.sample_step_deterministic_x0
            elif vector_field_type == VectorFieldType.EPS:
                f = self.sample_step_deterministic_eps
            elif vector_field_type == VectorFieldType.V:
                f = self.sample_step_deterministic_v
        return f

    def _fix_t_shape(self, x: torch.Tensor, t: torch.Tensor) -> torch.Tensor:
        """
        Reshape the time tensor to be compatible with the batch dimension of x.

        Args:
            x (torch.Tensor): The data tensor of shape (N, *D)
                where N is the batch size and D represents the data dimensions
            t (torch.Tensor): The time tensor to reshape, of shape (1, 1, ..., 1)
                or any shape that can be broadcast to match the batch size

        Returns:
            torch.Tensor: The reshaped time tensor of shape (N,)
                where N is the batch size of x
        """
        t = t.view((1,)).expand(x.shape[0])
        return t

    def sample_step_stochastic_score(
        self,
        score: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform a stochastic sampling step using the score vector field.

        This method implements one step of the stochastic reverse process using the score function.

        Args:
            score (VectorField): The score vector field model
            x (torch.Tensor): The current state tensor, of shape (N, *D)
                where N is the batch size and D represents the data dimensions
            zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
                where L is the number of time steps
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)
                where L is the number of time steps

        Returns:
            torch.Tensor: The next state tensor, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_deterministic_score(
        self,
        score: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the score vector field.

        Args:
            score (VectorField): The score vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_stochastic_x0(
        self,
        x0: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of stochastic sampling using the x0 vector field.

        Args:
            x0 (VectorField): The x0 vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_deterministic_x0(
        self,
        x0: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the x0 vector field.

        Args:
            x0 (VectorField): The x0 vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_stochastic_eps(
        self,
        eps: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of stochastic sampling using the eps vector field.

        Args:
            eps (VectorField): The eps vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_deterministic_eps(
        self,
        eps: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the eps vector field.

        Args:
            eps (VectorField): The eps vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_stochastic_v(
        self,
        v: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of stochastic sampling using the v vector field.

        Args:
            v (VectorField): The velocity vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

    def sample_step_deterministic_v(
        self,
        v: VectorField,
        x: torch.Tensor,
        zs: torch.Tensor,
        idx: int,
        ts: torch.Tensor,
    ) -> torch.Tensor:
        """
        Perform one step of deterministic sampling using the v vector field.

        Args:
            v (VectorField): The velocity vector field model
            x (torch.Tensor): The current state, of shape (N, *D)
            zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
            idx (int): The current step index
            ts (torch.Tensor): The time steps tensor, of shape (L,)

        Returns:
            torch.Tensor: The next state after one sampling step, of shape (N, *D)
        """
        raise NotImplementedError

diffusion_process = diffusion_process instance-attribute

is_stochastic = is_stochastic instance-attribute

__init__(diffusion_process, is_stochastic)

Initialize a sampler with a diffusion process and sampling strategy.

Parameters:

Name Type Description Default
diffusion_process DiffusionProcess

The diffusion process to use for sampling

required
is_stochastic bool

Whether the reverse process should be stochastic

required
Source code in src/diffusionlab/samplers.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def __init__(
    self,
    diffusion_process: DiffusionProcess,
    is_stochastic: bool,
):
    """
    Initialize a sampler with a diffusion process and sampling strategy.

    Args:
        diffusion_process (DiffusionProcess): The diffusion process to use for sampling
        is_stochastic (bool): Whether the reverse process should be stochastic
    """
    self.diffusion_process: DiffusionProcess = diffusion_process
    self.is_stochastic: bool = is_stochastic

get_sample_step_function(vector_field_type)

Get the appropriate sampling step function based on the vector field type.

This method selects the correct sampling function based on the vector field type and whether sampling is stochastic or deterministic.

Parameters:

Name Type Description Default
vector_field_type VectorFieldType

The type of vector field being used (SCORE, X0, EPS, or V)

required

Returns:

Name Type Description
Callable Callable[[VectorField, Tensor, Tensor, int, Tensor], Tensor]

A function that performs one step of the sampling process with signature: (vector_field, x, zs, idx, ts) -> next_x where: - vector_field is the model - x is the current state tensor of shape (N, D) where N is the batch size and D represents the data dimensions - zs is the noise tensors of shape (L-1, N, D) where L is the number of time steps - idx is the current step index - ts is the time steps tensor of shape (L,) where L is the number of time steps - next_x is the next state tensor of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def get_sample_step_function(
    self, vector_field_type: VectorFieldType
) -> Callable[
    [VectorField, torch.Tensor, torch.Tensor, int, torch.Tensor], torch.Tensor
]:
    """
    Get the appropriate sampling step function based on the vector field type.

    This method selects the correct sampling function based on the vector field type
    and whether sampling is stochastic or deterministic.

    Args:
        vector_field_type (VectorFieldType): The type of vector field being used
                                            (SCORE, X0, EPS, or V)

    Returns:
        Callable: A function that performs one step of the sampling process with signature:
                 (vector_field, x, zs, idx, ts) -> next_x
                 where:
                 - vector_field is the model
                 - x is the current state tensor of shape (N, *D)
                   where N is the batch size and D represents the data dimensions
                 - zs is the noise tensors of shape (L-1, N, *D)
                   where L is the number of time steps
                 - idx is the current step index
                 - ts is the time steps tensor of shape (L,)
                   where L is the number of time steps
                 - next_x is the next state tensor of shape (N, *D)
    """
    f = None
    if self.is_stochastic:
        if vector_field_type == VectorFieldType.SCORE:
            f = self.sample_step_stochastic_score
        elif vector_field_type == VectorFieldType.X0:
            f = self.sample_step_stochastic_x0
        elif vector_field_type == VectorFieldType.EPS:
            f = self.sample_step_stochastic_eps
        elif vector_field_type == VectorFieldType.V:
            f = self.sample_step_stochastic_v
    else:
        if vector_field_type == VectorFieldType.SCORE:
            f = self.sample_step_deterministic_score
        elif vector_field_type == VectorFieldType.X0:
            f = self.sample_step_deterministic_x0
        elif vector_field_type == VectorFieldType.EPS:
            f = self.sample_step_deterministic_eps
        elif vector_field_type == VectorFieldType.V:
            f = self.sample_step_deterministic_v
    return f

sample(vector_field, x_init, zs, ts)

Sample from the model using the reverse diffusion process.

This method generates a sample by iteratively applying the appropriate sampling step function based on the vector field type.

Parameters:

Name Type Description Default
vector_field VectorField

The vector field model to use for sampling

required
x_init Tensor

The initial noisy tensor to start sampling from, of shape (N, *D) where N is the batch size and D represents the data dimensions

required
zs Tensor

The noise tensors for stochastic sampling, of shape (L-1, N, *D) where L is the number of time steps

required
ts Tensor

The time schedule for sampling, of shape (L,) where L is the number of time steps

required

Returns:

Type Description
Tensor

torch.Tensor: The generated sample, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample(
    self,
    vector_field: VectorField,
    x_init: torch.Tensor,
    zs: torch.Tensor,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Sample from the model using the reverse diffusion process.

    This method generates a sample by iteratively applying the appropriate sampling step
    function based on the vector field type.

    Args:
        vector_field (VectorField): The vector field model to use for sampling
        x_init (torch.Tensor): The initial noisy tensor to start sampling from, of shape (N, *D) where N is the batch size and D represents the data dimensions
        zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
            where L is the number of time steps
        ts (torch.Tensor): The time schedule for sampling, of shape (L,)
            where L is the number of time steps

    Returns:
        torch.Tensor: The generated sample, of shape (N, *D)
    """
    sample_step_function = self.get_sample_step_function(
        vector_field.vector_field_type
    )
    x = x_init
    for i in range(ts.shape[0] - 1):
        x = sample_step_function(vector_field, x, zs, i, ts)
    return x

sample_step_deterministic_eps(eps, x, zs, idx, ts)

Perform one step of deterministic sampling using the eps vector field.

Parameters:

Name Type Description Default
eps VectorField

The eps vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
def sample_step_deterministic_eps(
    self,
    eps: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the eps vector field.

    Args:
        eps (VectorField): The eps vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_deterministic_score(score, x, zs, idx, ts)

Perform one step of deterministic sampling using the score vector field.

Parameters:

Name Type Description Default
score VectorField

The score vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def sample_step_deterministic_score(
    self,
    score: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the score vector field.

    Args:
        score (VectorField): The score vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_deterministic_v(v, x, zs, idx, ts)

Perform one step of deterministic sampling using the v vector field.

Parameters:

Name Type Description Default
v VectorField

The velocity vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
def sample_step_deterministic_v(
    self,
    v: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the v vector field.

    Args:
        v (VectorField): The velocity vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_deterministic_x0(x0, x, zs, idx, ts)

Perform one step of deterministic sampling using the x0 vector field.

Parameters:

Name Type Description Default
x0 VectorField

The x0 vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
def sample_step_deterministic_x0(
    self,
    x0: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of deterministic sampling using the x0 vector field.

    Args:
        x0 (VectorField): The x0 vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors (unused in deterministic sampling), of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_stochastic_eps(eps, x, zs, idx, ts)

Perform one step of stochastic sampling using the eps vector field.

Parameters:

Name Type Description Default
eps VectorField

The eps vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors, of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
def sample_step_stochastic_eps(
    self,
    eps: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of stochastic sampling using the eps vector field.

    Args:
        eps (VectorField): The eps vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_stochastic_score(score, x, zs, idx, ts)

Perform a stochastic sampling step using the score vector field.

This method implements one step of the stochastic reverse process using the score function.

Parameters:

Name Type Description Default
score VectorField

The score vector field model

required
x Tensor

The current state tensor, of shape (N, *D) where N is the batch size and D represents the data dimensions

required
zs Tensor

The noise tensors for stochastic sampling, of shape (L-1, N, *D) where L is the number of time steps

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,) where L is the number of time steps

required

Returns:

Type Description
Tensor

torch.Tensor: The next state tensor, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
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
def sample_step_stochastic_score(
    self,
    score: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform a stochastic sampling step using the score vector field.

    This method implements one step of the stochastic reverse process using the score function.

    Args:
        score (VectorField): The score vector field model
        x (torch.Tensor): The current state tensor, of shape (N, *D)
            where N is the batch size and D represents the data dimensions
        zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
            where L is the number of time steps
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)
            where L is the number of time steps

    Returns:
        torch.Tensor: The next state tensor, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_stochastic_v(v, x, zs, idx, ts)

Perform one step of stochastic sampling using the v vector field.

Parameters:

Name Type Description Default
v VectorField

The velocity vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors, of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def sample_step_stochastic_v(
    self,
    v: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of stochastic sampling using the v vector field.

    Args:
        v (VectorField): The velocity vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_step_stochastic_x0(x0, x, zs, idx, ts)

Perform one step of stochastic sampling using the x0 vector field.

Parameters:

Name Type Description Default
x0 VectorField

The x0 vector field model

required
x Tensor

The current state, of shape (N, *D)

required
zs Tensor

The noise tensors, of shape (L-1, N, *D)

required
idx int

The current step index

required
ts Tensor

The time steps tensor, of shape (L,)

required

Returns:

Type Description
Tensor

torch.Tensor: The next state after one sampling step, of shape (N, *D)

Source code in src/diffusionlab/samplers.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def sample_step_stochastic_x0(
    self,
    x0: VectorField,
    x: torch.Tensor,
    zs: torch.Tensor,
    idx: int,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Perform one step of stochastic sampling using the x0 vector field.

    Args:
        x0 (VectorField): The x0 vector field model
        x (torch.Tensor): The current state, of shape (N, *D)
        zs (torch.Tensor): The noise tensors, of shape (L-1, N, *D)
        idx (int): The current step index
        ts (torch.Tensor): The time steps tensor, of shape (L,)

    Returns:
        torch.Tensor: The next state after one sampling step, of shape (N, *D)
    """
    raise NotImplementedError

sample_trajectory(vector_field, x_init, zs, ts)

Sample a trajectory from the model using the reverse diffusion process.

This method is similar to sample() but returns the entire trajectory of intermediate samples rather than just the final sample.

Parameters:

Name Type Description Default
vector_field VectorField

The vector field model to use for sampling

required
x_init Tensor

The initial noisy tensor to start sampling from, of shape (N, *D) where N is the batch size and D represents the data dimensions

required
zs Tensor

The noise tensors for stochastic sampling, of shape (L-1, N, *D) where L is the number of time steps

required
ts Tensor

The time schedule for sampling, of shape (L,) where L is the number of time steps

required

Returns:

Type Description
Tensor

torch.Tensor: The generated trajectory, of shape (L, N, *D) where L is the number of time steps

Source code in src/diffusionlab/samplers.py
 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
def sample_trajectory(
    self,
    vector_field: VectorField,
    x_init: torch.Tensor,
    zs: torch.Tensor,
    ts: torch.Tensor,
) -> torch.Tensor:
    """
    Sample a trajectory from the model using the reverse diffusion process.

    This method is similar to sample() but returns the entire trajectory of
    intermediate samples rather than just the final sample.

    Args:
        vector_field (VectorField): The vector field model to use for sampling
        x_init (torch.Tensor): The initial noisy tensor to start sampling from, of shape (N, *D)
            where N is the batch size and D represents the data dimensions
        zs (torch.Tensor): The noise tensors for stochastic sampling, of shape (L-1, N, *D)
            where L is the number of time steps
        ts (torch.Tensor): The time schedule for sampling, of shape (L,)
            where L is the number of time steps

    Returns:
        torch.Tensor: The generated trajectory, of shape (L, N, *D)
            where L is the number of time steps
    """
    sample_step_function = self.get_sample_step_function(
        vector_field.vector_field_type
    )
    xs = [x_init]
    x = x_init
    for i in range(ts.shape[0] - 1):
        x = sample_step_function(vector_field, x, zs, i, ts)
        xs.append(x)
    return torch.stack(xs)