Overview
BaseDiffusion is an abstract base class that defines the core interface for diffusion models in Alpamayo R1. All diffusion implementations inherit from this class and implement the sample() method.
Class Definition
Constructor
The dimension of the input tensor. Can be a single integer or a sequence of integers defining the shape.
Example
Methods
sample
The number of samples to generate in parallel.
The denoising step function that takes a noisy tensor
x and a timestep t and returns either a denoised tensor, a vector field, or noise depending on the prediction type of the diffusion model.The step function should have the signature:The PyTorch device to use for sampling (e.g., “cpu”, “cuda”).
Whether to return the outputs from all intermediate sampling steps.
Returns
If
return_all_steps=False: Returns the final sampled tensor with shape [B, *x_dims].If return_all_steps=True: Returns a tuple of:- All sampled tensors with shape
[B, T, *x_dims]where T is the number of steps - The time steps with shape
[T]
StepFn Protocol
TheStepFn protocol defines the interface for denoising step functions:
The input tensor to denoise.
The timestep tensor indicating the current noise level.
Diffusion Sampling Process
The diffusion sampling process works as follows:- Initialization: Start with random noise sampled from a standard normal distribution
- Iterative Denoising: Apply the step function repeatedly at different timesteps to gradually denoise the sample
- Output: Return the final denoised sample (and optionally all intermediate steps)
Usage Example
See Also
- FlowMatching - Flow matching diffusion implementation