diffusion_models.utils.schemas

Module Contents

class PredictionType[source]

Initialize self. See help(type(self)) for accurate signature.

Epsilon = 'epsilon'[source]

The noise added to the image.

V = 'v'[source]

The velocity, \(v = \sqrt{\bar{\alpha}}\epsilon - \sqrt{1-\bar{\alpha}}x_0\).

class TrainingConfiguration[source]

A training configuration for simple experiment management.

training_name: str[source]

The name of the training.

batch_size: int[source]

The batch size used for training.

learning_rate: float[source]

The learning rate of the training.

number_of_epochs: int[source]

The number of epoch used for training.

checkpoint_rate: int = 100[source]

The rate at which checkpoints are saved..

mixed_precision_training: bool = False[source]

Whether or not to use automatic mixed precision training.

mixed_precision_dtype: str = 'bfloat16'[source]

bfloat16 (no gradient scaling needed) or float16 (uses a GradScaler).

Type:

The autocast dtype when mixed precision is on

channels_last: bool = False[source]

Whether to use the channels_last memory format for the model and its inputs. This speeds up mixed precision training on tensor cores but is very slow in fp32, so it should be combined with mixed_precision_training.

gradient_clip: float | None = None[source]

Whether or not to clip gradients.

ema_decay: float | None = None[source]

The decay of the exponential moving average of the weights. Disabled when None.

ema_warmup_steps: int = 0[source]

When positive, ramps the EMA decay up as (1 + n) / (warmup + n) (capped at ema_decay).

latest_checkpoint_rate: int | None = None[source]

The rate (in epochs) at which the rolling latest.pt is overwritten. Disabled when None.

num_workers: int = 4[source]

The number of dataloader worker processes. 0 loads data in the main process.

pin_memory: bool = True[source]

Whether the dataloader copies batches into pinned memory (only used with CUDA).

persistent_workers: bool = True[source]

Whether dataloader workers are kept alive between epochs (needs num_workers > 0).

prefetch_factor: int | None = None[source]

Batches prefetched per worker. Uses the torch default when None (needs num_workers > 0).

compile: bool = False[source]

Whether to torch.compile the model’s forward pass during training. Checkpoints and the EMA always hold the uncompiled model’s weights.

class EvaluationConfiguration[source]

Settings of the FID / KID evaluation that runs during training.

fid_rate: int | None = None[source]

The rate (in epochs) at which FID/KID is computed and logged. Also computed after the last epoch. Disabled when None.

number_of_samples: int = 5000[source]

The number of generated (and real) images compared. Only compare scores that use the same number.

batch_size: int = 100[source]

The batch size used for sampling and feature extraction.

compute_kid: bool = True[source]

Whether to also compute the Kernel Inception Distance.

kid_subset_size: int = 1000[source]

The KID subset size.

seed: int = 0[source]

The seed of the sampling noise, so scores are comparable across runs.

num_workers: int = 4[source]

Dataloader workers used to read the real images.

class LogConfiguration[source]

An object to manage logging configuration.

log_rate: int = 10[source]

The rate at which training metrics are logged.

image_rate: int = 50[source]

The rate at which images are generated for visualization. This can be used to validate model performance.

number_of_images: int = 5[source]

The number of images to generate.

weight_rate: int | None = None[source]

The rate (in steps) at which weight histograms are logged. Disabled when None.

class BetaSchedulerConfiguration[source]

A simplified beta scheduler configuration.

steps: int[source]

The number of steps in the beta scheduler.

betas: Tensor[source]

The beta values.

alpha_bars: Tensor[source]

The alpha bar values.

class Checkpoint[source]

A simplified checkpoint framework for easy saving and loading.

epoch: int[source]

The current epoch.

model_state_dict: Dict[str, Any][source]

The model state dict.

optimizer_state_dict: Dict[str, Any][source]

The optimizer state dict.

beta_scheduler_config: BetaSchedulerConfiguration[source]

The beta scheduler configuration.

scaler: Dict[str, Any] | None = None[source]

The GradScaler instance.

ema_model_state_dict: Dict[str, Any] | None = None[source]

The state dict of the EMA (exponential moving average) weights, if tracked.

ema_num_updates: int | None = None[source]

The number of EMA updates performed so far (needed to resume the EMA warmup).

completed_epochs: int | None = None[source]

The number of fully completed epochs. Training resumes from here.

tensorboard_run_name: str | None = None[source]

The name of the tensorboard run.

image_channels: int = 3[source]

The number of image channels used in the training.

prediction_type: str = 'epsilon'[source]

What the model predicts ("epsilon" or "v"). Legacy checkpoints are epsilon.

config_yaml: str | None = None[source]

The resolved configuration (YAML) of the run that produced this checkpoint.

loss: float | None = None[source]

The final loss value recorded.

Note

This is a legacy parameter and will be removed in a future release.

classmethod from_file(file_path, map_location=None)[source]

Load and instantiate a checkpoint from a file.

Parameters:
  • file_path (str) – The path to the checkpoint file.

  • map_location (Optional[str]) – A function, torch. device, string or a dict specifying how to remap storage location.

Returns:

A checkpoint instance.

Return type:

Checkpoint

get_model_state_dict(use_ema=True)[source]

Get the weights to load into a model.

Parameters:

use_ema (bool) – Prefer the EMA weights when the checkpoint has them. Falls back to the raw weights (with a warning) for checkpoints without EMA.

Returns:

A model state dict.

Return type:

Dict[str, Any]

to_file(file_path)[source]

Saves a checkpoint to a file.

The write is atomic: the checkpoint goes to a temporary file which then replaces the destination, so readers never see a partial file.

class OldCheckpoint[source]
epoch: int[source]
model_state_dict: Dict[str, Any][source]
optimizer_state_dict: Dict[str, Any][source]
scaler: Dict[str, Any] | None[source]
tensorboard_run_name: str | None = None[source]
loss: float | None = None[source]
classmethod from_file(file_path)[source]
to_file(file_path)[source]
to_new_checkpoint(beta_scheduler)[source]
class Timestep[source]
current: Tensor[source]
previous: Tensor | None = None[source]