mr2.nn.nets.DiT

class mr2.nn.nets.DiT[source]

Bases: CondMixin, Module

DiT model.

DiT is a vision transformer popularized by [DiT]. Often used for latent diffusion models, but also suitable for image restoration etc.

References

[DiT]

Peebles, W., & Xie, S. Scalable Diffusion Models with Transformers. ICCV 2023, https://arxiv.org/abs/2212.09748

__init__(n_dim: int, n_channels_in: int, cond_dim: int, input_size: int | Sequence[int] = 32, patch_size: int | Sequence[int] = 2, n_channels_out: int | None = None, hidden_dim: int = 1152, depth: int = 28, n_heads: int = 16, mlp_ratio: float = 4.0) → None[source]

Initialize DiT.

Parameters:
  • n_dim (int) – Number of spatial dimensions.

  • n_channels_in (int) – Number of channels in the input tensor.

  • cond_dim (int) – Dimension of the conditioning tensor.

  • input_size (int | Sequence[int], default: 32) – Input spatial size. If scalar, the same size is used for all spatial dimensions.

  • patch_size (int | Sequence[int], default: 2) – Patch size. If scalar, the same patch size is used for all spatial dimensions.

  • n_channels_out (int | None, default: None) – Number of output channels. If None, use n_channels_in.

  • hidden_dim (int, default: 1152) – Transformer hidden dimension.

  • depth (int, default: 28) – Number of transformer blocks.

  • n_heads (int, default: 16) – Number of attention heads.

  • mlp_ratio (float, default: 4.0) – Ratio of hidden MLP channels to input channels.

__call__(x: Tensor, *, cond: Tensor | None = None) → Tensor[source]

Apply the module to the input.

forward(x: Tensor, *, cond: Tensor | None = None) → Tensor[source]

Apply DiT.

Parameters:
  • x (Tensor) – Input tensor with shape batch, channels, *spatial_dims.

  • cond (Tensor | None, default: None) – Conditioning tensor.

Returns:

Output tensor with shape batch, out_channels, *spatial_dims.

initialize_weights() → None[source]

Initialize network weights.