DiT
Diffusion Transformer
A Transformer-based diffusion model. The foundational architecture of Sora.
Diffusion Transformer
A Transformer-based diffusion model. The foundational architecture of Sora.
DiT (Diffusion Transformer) is an innovative model that replaces the conventional U-Net backbone in diffusion models for image generation with a Transformer architecture. Published in 2022 by William Peebles and Saining Xie of Meta AI, it applied the scalability of Transformers to diffusion models and demonstrated performance improvements as model size increases.
DiT drew attention when it was adopted as the core architecture of OpenAI Sora. Based on DiT, Sora processes spatiotemporal patches to generate high-quality videos up to 1 minute long. This shows far superior temporal consistency and image quality compared to conventional U-Net-based video models.
The core principle is to split an image into patch units, treat each patch as a token, and pass it through Transformer blocks. The noise step and class label are injected conditionally through adaLN (Adaptive Layer Normalization), which enables conditional image generation. On the ImageNet 256x256 benchmark, the DiT-XL/2 model achieved an FID of 2.27, setting the SOTA at the time.
In practice, DiT is used as a foundation model for large-scale image/video generation services. Because of the nature of Transformers, parallelization is easy, so it can be trained efficiently on large GPU clusters, and applying fine-tuning techniques such as LoRA is straightforward. Stable Diffusion 3.0 also adopted a DiT-based architecture (MM-DiT), greatly improving text understanding and image quality.
# DiT 모델 구조 간소화 예제 (PyTorch)
import torch
import torch.nn as nn
class DiTBlock(nn.Module):
"""DiT의 기본 Transformer 블록 (adaLN 포함)"""
def __init__(self, hidden_size, num_heads):
super().__init__()
self.norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False)
self.attn = nn.MultiheadAttention(hidden_size, num_heads, batch_first=True)
self.norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False)
self.mlp = nn.Sequential(
nn.Linear(hidden_size, hidden_size * 4),
nn.GELU(),
nn.Linear(hidden_size * 4, hidden_size)
)
# adaLN 모듈레이션 파라미터 (6개: scale1, shift1, gate1, scale2, shift2, gate2)
self.adaLN_modulation = nn.Sequential(
nn.SiLU(),
nn.Linear(hidden_size, 6 * hidden_size)
)
def forward(self, x, c):
# c: 조건 임베딩 (timestep + class label)
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = \
self.adaLN_modulation(c).chunk(6, dim=-1)
# Modulated self-attention
h = self.norm1(x) * (1 + scale_msa.unsqueeze(1)) + shift_msa.unsqueeze(1)
h = self.attn(h, h, h)[0]
x = x + gate_msa.unsqueeze(1) * h
# Modulated MLP
h = self.norm2(x) * (1 + scale_mlp.unsqueeze(1)) + shift_mlp.unsqueeze(1)
x = x + gate_mlp.unsqueeze(1) * self.mlp(h)
return x
# 사용 예제
block = DiTBlock(hidden_size=768, num_heads=12)
x = torch.randn(2, 256, 768) # (batch, num_patches, hidden)
c = torch.randn(2, 768) # condition embedding
output = block(x, c)
print(f"Output shape: {output.shape}") # (2, 256, 768)
"When upgrading our image generation model, I looked into switching to the DiT architecture. Compared to U-Net, Transformers follow scaling laws better, so as we increase model size, performance improves predictably. However, I think we need to experiment with combinations of patch size and hidden dimension."
"DiT's key innovation is adaLN (Adaptive Layer Normalization). Instead of injecting conditions through cross-attention as before, it dynamically adjusts the scale/shift parameters of LayerNorm according to the condition. This reduces computation and also improves training stability."
"Just as Sora is DiT-based, if we migrate our video generation pipeline to DiT as well, consistency between frames should improve significantly. How about we proceed by using the DiT checkpoint released by Meta as the base and doing domain-specific fine-tuning?"
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Training the DiT-XL/2 model requires an A100 80GB-class GPU. Even for inference, at least 24GB of VRAM must be secured to generate high-resolution images. Increasing the patch size reduces memory but loses detail.
Because of the O(n^2) complexity of self-attention inherent to Transformers, inference speed drops at high resolutions. Applying optimization libraries such as Flash Attention and xFormers is essential.
Depending on the patch size (2, 4, 8, etc.), there is a trade-off between sequence length and detail expressiveness. A patch size of 2 gives the highest quality but increases computation 16-fold, so it should be chosen to suit the intended use.