diffusion_process
diffusion_process
¶
This module provides reusable building blocks for implementing diffusion processes.
DiffusionSchedulerConfig
dataclass
¶
DiffusionSchedulerConfig(scheduler_type, num_train_timesteps, num_inference_steps, beta_start, beta_end, beta_schedule, prediction_type, clip_sample, variance_type=None, set_alpha_to_one=None, steps_offset=None)
Configuration for diffusion noise schedulers.
Attributes:
| Name | Type | Description |
|---|---|---|
scheduler_type |
str
|
Type of scheduler ("ddpm" or "ddim") |
num_train_timesteps |
int
|
Number of diffusion steps during training |
num_inference_steps |
int
|
Number of denoising steps during inference |
beta_start |
float
|
Starting value of noise schedule |
beta_end |
float
|
Ending value of noise schedule |
beta_schedule |
str
|
Noise schedule type (e.g., "squaredcos_cap_v2", "linear") |
prediction_type |
str
|
What model predicts ("epsilon" for noise, "sample" for clean data) |
clip_sample |
bool
|
Whether to clip samples during inference |
variance_type |
str | None
|
Variance type for DDPM scheduler (e.g., "fixed_small") |
set_alpha_to_one |
bool | None
|
Whether to set final alpha to 1 (DDIM only) |
steps_offset |
int | None
|
Offset for timestep calculation (DDIM only) |
SchedulerType
¶
Bases: StrEnum
Diffusion scheduler types (compatible with diffusers API).
create_noise_scheduler
¶
Factory function for creating diffusion noise schedulers.
Creates either a DDPM or DDIM scheduler based on configuration. Both schedulers support the same forward diffusion process but differ in their reverse process (DDIM allows faster inference with fewer steps).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DiffusionSchedulerConfig
|
Scheduler configuration |
required |
Returns:
| Type | Description |
|---|---|
DDPMScheduler | DDIMScheduler
|
Configured noise scheduler (DDPM or DDIM) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If scheduler_type is not recognized |
Example
Source code in src/versatil/models/layers/denoising/diffusion_process.py
add_noise_to_tensor
¶
Add noise to a clean tensor at specified timesteps.
Implements the forward diffusion process
x_t = sqrt(alpha_t) * x_0 + sqrt(1 - alpha_t) * epsilon
where x_0 is the clean data, epsilon is sampled noise, and alpha_t controls the signal-to-noise ratio at timestep t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clean
|
Tensor
|
Clean input tensor of shape (..., features) |
required |
noise_scheduler
|
DDPMScheduler | DDIMScheduler
|
Configured noise scheduler |
required |
timesteps
|
Tensor
|
Timesteps at which to add noise, shape (batch_size,) |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (noisy_tensor, noise) where: - noisy_tensor: Input with added noise, same shape as clean - noise: The sampled noise that was added, same shape as clean |
Source code in src/versatil/models/layers/denoising/diffusion_process.py
sample_random_timesteps
¶
Sample random timesteps for training.
Samples uniformly from [0, num_train_timesteps) for each batch element. This ensures the model learns to denoise at all noise levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of timesteps to sample |
required |
num_train_timesteps
|
int
|
Maximum timestep value (exclusive) |
required |
device
|
device
|
Device to place timesteps on |
required |
Returns:
| Type | Description |
|---|---|
IntTensor
|
Tensor of shape (batch_size,) with sampled timesteps |
Raises:
| Type | Description |
|---|---|
ValueError
|
If batch_size is negative or num_train_timesteps is not positive. |
Source code in src/versatil/models/layers/denoising/diffusion_process.py
setup_inference_timesteps
¶
Configure scheduler for inference denoising.
Sets up the timestep schedule for the reverse diffusion process. For DDPM, this uses all timesteps. For DDIM, this can use fewer steps by skipping timesteps uniformly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
noise_scheduler
|
DDPMScheduler | DDIMScheduler
|
Configured noise scheduler (modified in-place) |
required |
num_inference_steps
|
int
|
Number of denoising steps to use |
required |
Side Effects
Modifies noise_scheduler.timesteps in-place