File size: 1,041 Bytes
cfeea40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from typing import Protocol

import torch

from mattergen.diffusion.corruption.sde_lib import SDE


class TimestepSampler(Protocol):
    min_t: float
    max_t: float

    def __call__(self, batch_size: int, device: torch.device) -> torch.FloatTensor:
        raise NotImplementedError


class UniformTimestepSampler:
    """Samples diffusion timesteps uniformly over the training time."""

    def __init__(
        self,
        *,
        min_t: float,
        max_t: float,
    ):
        """Initializes the sampler.

        Args:
            min_t (float): Smallest timestep that will be seen during training.
            max_t (float): Largest timestep that will be seen during training.
        """
        super().__init__()
        self.min_t = min_t
        self.max_t = max_t

    def __call__(self, batch_size: int, device: torch.device) -> torch.FloatTensor:
        return torch.rand(batch_size, device=device) * (self.max_t - self.min_t) + self.min_t