explain the specifics of how they do sampling during inference. Illustrate using pytorch
- User
explain the specifics of how they do sampling during inference. Illustrate using pytorch
Assistant
Sana's inference sampling process employs several sophisticated techniques for generating high-quality images from text prompts. Here's a breakdown of the key components and a PyTorch illustration:
1. Core Sampling Methods:
Sana primarily utilizes three sampling algorithms during inference:
- DPM-Solver: This method excels at balancing speed and quality. It discretizes a diffusion ODE (Ordinary Differential Equation) and uses a predictor-corrector approach for efficient sampling. It supports classifier-free guidance for incorporating conditional information (like text prompts).
- SA-Solver: Designed for speed, the Stochastic Adams solver offers another approach to solving diffusion SDEs (Stochastic Differential Equations).
- Flow Matching Euler: Based on the Euler method, this is particularly useful for flow matching models.
2. Classifier-Free Guidance:
To incorporate the text prompt's influence, Sana employs classifier-free guidance. This technique involves running the model twice for each sampling step: once with the text embedding (conditional) and once without (unconditional). The final denoising direction is a weighted combination of these two directions, controlled by the
cfg_scale. Highercfg_scalevalues lead to stronger adherence to the prompt but can sometimes reduce image quality. Additionally, Prompt-Aware Guidance (PAG) can be employed to further enhance prompt alignment.3. Noise Schedules:
The sampling process relies on noise schedules that dictate how noise is added and removed throughout the diffusion process. Sana supports various noise schedules like "linear," "squaredcos_cap_v2," and schedules specific to flow matching models. These schedules impact both sample quality and convergence speed.
4. VAE Decoding:
Since Sana operates in the latent space of a Variational Autoencoder (VAE), the final generated samples need to be decoded back into pixel space using the VAE decoder.
PyTorch Illustration (Simplified DPM-Solver with Classifier-Free Guidance):
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.