ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Advanced Diffusion Model Architectures and Training
Chapter 1: Foundation Review and Advanced Noise Schedules
Recap: Denoising Diffusion Probabilistic Models (DDPM)
Recap: Denoising Diffusion Implicit Models (DDIM)
Mathematical Underpinnings: Score Matching and ODEs
Limitations of Standard Noise Schedules
Designing Custom Noise Schedules
Learned Variance Schedules
Hands-on Practical: Implementing Noise Schedule Variants
Chapter 2: Advanced U-Net Architectures
The Standard U-Net in Diffusion Models
Attention Mechanisms in U-Nets (Self-Attention, Cross-Attention)
Integrating Time Embeddings in U-Nets
Advanced Conditioning Input Integration
Architectural Variants for Efficiency (Depth, Width, Pooling)
Normalization Techniques (GroupNorm, AdaLN)
Hands-on Practical: Modifying a U-Net with Attention
Chapter 3: Transformer-Based Diffusion Models
Motivation for Transformers in Generative Modeling
Adapting Transformers for Image Data (ViT, Patch Embeddings)
Diffusion Transformers (DiT): Architecture Overview
Conditioning in Diffusion Transformers
Comparison: U-Nets vs. Transformers for Diffusion
Implementation Considerations for DiTs
Hands-on Practical: Building a Simple DiT Block
Chapter 4: Advanced Training Techniques
Classifier Guidance: Principles and Implementation
Classifier-Free Guidance (CFG): Theory and Benefits
Implementing and Tuning CFG Scale
Advanced Loss Function Formulations (v-prediction, L_simple)
Model Parameterization (epsilon-prediction vs. x0-prediction)
Techniques for Training Stability (Gradient Clipping, EMA)
Mixed-Precision Training for Diffusion Models
Hands-on Practical: Implementing Classifier-Free Guidance
Chapter 5: Consistency Models
Motivation: The Need for Faster Sampling
Core Idea: Consistency Property
Consistency Model Training: Distillation Approach
Consistency Model Training: Standalone Approach
Sampling from Consistency Models (Single-step and Multi-step)
Architecture Considerations for Consistency Models
Trade-offs: Speed vs. Quality
Hands-on Practical: Basic Consistency Distillation
Chapter 6: Advanced Sampling and Optimization
Higher-Order Solvers (DPM-Solver, UniPC)
Stochastic Sampling Variants
Guided Sampling Refinements
Troubleshooting Sampling Issues (Artifacts, Blurriness)
Model Distillation for Diffusion
Quantization of Diffusion Models
Hardware Acceleration Considerations (GPU Kernels, Compilation)
Hands-on Practical: Comparing Advanced Samplers

Adapting Transformers for Image Data (ViT, Patch Embeddings)

Transformers fundamentally operate on sequences of tokens, effectively modeling relationships between elements in a 1D structure. This contrasts sharply with the grid-like, 2D (or higher dimensional) nature of images, where convolutional neural networks (CNNs) naturally excel due to their built-in inductive biases for locality and spatial hierarchies. To apply the capabilities of transformer self-attention to image data within a diffusion model, we first need a method to represent an image as a sequence that a transformer can process. The dominant approach borrows directly from the architecture that popularized transformers for vision tasks: the Vision Transformer (ViT).

From Pixels to Patches: Sequencing Image Data

The core strategy involves dividing the input image into a grid of smaller, non-overlapping patches. Imagine taking an image and cutting it up into a series of square tiles. Each tile, or patch, is then treated as a single "token" in the sequence that will be fed into the transformer.

Take an input image x∈RH×W×Cx \in \mathbb{R}^{H \times W \times C}x∈RH×W×C, where HHH is the height, WWW is the width, and CCC is the number of channels (e.g., 3 for RGB). We divide this image into NNN patches, each of size P×PP \times PP×P. The total number of patches is N=(H/P)×(W/P)N = (H/P) \times (W/P)N=(H/P)×(W/P), assuming HHH and WWW are divisible by PPP.

Each patch is then flattened into a vector. A single patch originally has dimensions P×P×CP \times P \times CP×P×C, so its flattened representation becomes a vector of length P2⋅CP^2 \cdot CP2⋅C.

Linear Projection: Creating Patch Embeddings

These flattened patch vectors, while sequential, often have a very high dimensionality (P2⋅CP^2 \cdot CP2⋅C). More importantly, they might not be in a representation space that's optimal for the transformer's self-attention mechanism to learn relationships. Therefore, each flattened patch vector is linearly projected into a lower-dimensional embedding space, typically denoted by dimension DDD. This is achieved using a learned linear projection matrix (an embedding layer):

Embedding=Flattened Patch×We+be\text{Embedding} = \text{Flattened Patch} \times W_e + b_eEmbedding=Flattened Patch×We​+be​

Where We∈R(P2⋅C)×DW_e \in \mathbb{R}^{(P^2 \cdot C) \times D}We​∈R(P2⋅C)×D is the learnable weight matrix and be∈RDb_e \in \mathbb{R}^{D}be​∈RD is the bias term. After this projection, our image is represented as a sequence of NNN vectors, each of dimension DDD. This sequence, [z1,z2,...,zN][z_1, z_2, ..., z_N][z1​,z2​,...,zN​] where each zi∈RDz_i \in \mathbb{R}^Dzi​∈RD, is now in a format suitable for input into a standard transformer architecture.

Process of converting an image into a sequence of patch embeddings suitable for a transformer.

Incorporating Spatial Information: Positional Embeddings

A standard transformer architecture is permutation-invariant; if you shuffle the input sequence, the output (before accounting for the original positions) changes predictably based only on the shuffled content, not the original order. However, the spatial arrangement of patches in an image is fundamental information. The patch from the top-left corner contains different contextual information than the patch from the center.

To provide the transformer with this important spatial awareness, positional embeddings are added to the patch embeddings. These positional embeddings are vectors of the same dimension DDD as the patch embeddings. Typically, one unique positional embedding vector is learned for each possible patch position in the grid. Before feeding the sequence into the first transformer block, the corresponding positional embedding is added element-wise to each patch embedding:

zi′=zi+piz'_i = z_i + p_izi′​=zi​+pi​

Where ziz_izi​ is the embedding of the iii-th patch and pip_ipi​ is the positional embedding corresponding to the spatial location of the iii-th patch. These positional embeddings pip_ipi​ are usually initialized randomly and learned jointly with the rest of the model during training. Alternative strategies exist, such as using fixed sinusoidal positional embeddings, similar to those used in the original transformer paper, sometimes adapted for 2D.

Relation to Vision Transformer (ViT)

This entire process, patching, flattening, linear projection, and adding positional embeddings, is the standard input processing pipeline defined by the Vision Transformer (ViT) model. ViT demonstrated that a pure transformer architecture, when fed image data processed in this way, could achieve state-of-the-art results on image classification tasks, challenging the dominance of CNNs.

Diffusion Transformers (DiTs), which we will explore next, directly adopt this ViT-style input processing. Instead of feeding the patch sequence into a transformer for classification, DiTs use the transformer as the backbone network within the diffusion framework to predict noise (or the original image). The input to this process in a diffusion context is typically the noisy image xtx_txt​ at a given timestep ttt. The resulting sequence of processed patch embeddings, now infused with spatial information, serves as the input for the subsequent transformer blocks responsible for the core denoising task. Time and conditioning information are also incorporated, as we will discuss later.

This adaptation effectively translates the image generation problem into a sequence modeling problem solvable by transformers, leveraging their strength in capturing global dependencies between different parts (patches) of the image.

  • Attention Is All You Need, Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin, 2017 Advances in Neural Information Processing Systems, Vol. 30 DOI: 10.48550/arXiv.1706.03762 - Introduces the Transformer architecture and self-attention mechanism, establishing the foundation for sequence processing models.
  • An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, and Neil Houlsby, 2020 International Conference on Learning Representations (ICLR) DOI: 10.48550/arXiv.2010.11929 - Presents the Vision Transformer (ViT) model, detailing the method of converting images into sequences of patches and using a transformer for image classification.
  • Scalable Diffusion Models with Transformers, William Peebles and Saining Xie, 2023 Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV) DOI: 10.48550/arXiv.2212.09748 - Introduces Diffusion Transformers (DiTs), demonstrating how ViT-style input processing and transformer architectures can be scaled for state-of-the-art diffusion models.

© 2026 ApX Machine Learning