Implementation:AUTOMATIC1111 Stable diffusion webui Load model
| Knowledge Sources | |
|---|---|
| Domains | Diffusion Models, Model Management, Deep Learning Infrastructure |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for loading a Stable Diffusion checkpoint file into memory, instantiating the model architecture, and preparing it for inference, provided by the AUTOMATIC1111 stable-diffusion-webui repository.
Description
The load_model function orchestrates the complete model loading pipeline in a carefully sequenced process:
- Unload existing model -- If a model is already loaded, it is sent to a trash list and memory is freed via
torch_gc() - Load state dictionary -- The checkpoint file is read from disk (or an already-loaded state dict is reused)
- Find configuration -- The model variant (SD1, SD2, SDXL, SDXL Refiner) is detected by examining state dict keys via
sd_models_config.find_checkpoint_config() - Create model -- The model is instantiated from the OmegaConf YAML config using
instantiate_from_config(), with optimized initialization that avoids redundant weight allocation - Load weights -- State dict weights are loaded onto the model, with dtype conversion (float16 for most weights, preserving float32 for VAE and alphas_cumprod)
- Send to device -- The model is moved to the target device (GPU)
- Hijack --
sd_hijack.model_hijack.hijack()patches the model to support custom embeddings and extended token lengths - Eval mode -- The model is set to evaluation mode
- Load embeddings -- Textual inversion embeddings are reloaded
- Calculate empty prompt -- A cached empty-prompt conditioning is computed for CFG
The VAE can also be loaded separately via sd_vae.load_vae(), which:
- Checks the VAE checkpoint cache (LRU, configurable size)
- Loads the VAE state dict from a
.safetensorsor.ckptfile - Replaces the
first_stage_modelweights in the loaded SD model - Converts to the appropriate VAE dtype
Usage
This function is called at application startup, when switching models via the UI dropdown, or when the API specifies a different checkpoint. It is also called internally by reload_model_weights() which handles model reuse optimization.
Code Reference
Source Location
- Repository: stable-diffusion-webui
- File:
modules/sd_models.py - Lines: 786-875
- VAE loading:
modules/sd_vae.py, Lines: 194-234
Signature
def load_model(checkpoint_info=None, already_loaded_state_dict=None):
"""
Loads a Stable Diffusion model from a checkpoint file.
Args:
checkpoint_info: CheckpointInfo object describing the checkpoint to load.
If None, selects the currently configured checkpoint.
already_loaded_state_dict: Pre-loaded state dictionary to reuse,
avoiding redundant disk reads.
Returns:
sd_model: The loaded and initialized WebuiSdModel ready for inference.
"""
VAE loading signature:
def load_vae(model, vae_file=None, vae_source="from unknown source"):
"""
Loads a VAE checkpoint into the model's first_stage_model.
Args:
model: The Stable Diffusion model whose VAE will be replaced.
vae_file: Path to the VAE checkpoint file. If None, restores the base VAE.
vae_source: Human-readable description of where the VAE is from.
"""
Import
import modules.sd_models as sd_models
sd_model = sd_models.load_model()
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| checkpoint_info | CheckpointInfo or None | No | Describes the checkpoint file to load (path, hash, config). If None, the currently selected checkpoint from settings is used. |
| already_loaded_state_dict | dict or None | No | A pre-loaded state dictionary to avoid redundant file reads. Used when the state dict was already loaded for inspection. |
Outputs
| Name | Type | Description |
|---|---|---|
| sd_model | WebuiSdModel | The fully initialized Stable Diffusion model with UNet, CLIP, and VAE components loaded, hijacked for custom embeddings, and set to eval mode on the target device. |
Usage Examples
Basic Usage
import modules.sd_models as sd_models
# Load the default/selected checkpoint
sd_model = sd_models.load_model()
# Load a specific checkpoint
checkpoint_info = sd_models.get_closet_checkpoint_match("v1-5-pruned-emaonly.safetensors")
sd_model = sd_models.load_model(checkpoint_info=checkpoint_info)
# Reload model weights (with reuse optimization)
sd_models.reload_model_weights()
Loading a Custom VAE
import modules.sd_vae as sd_vae
# Load a custom VAE into the current model
sd_vae.load_vae(
model=shared.sd_model,
vae_file="/path/to/vae-ft-mse-840000-ema-pruned.safetensors",
vae_source="from user selection"
)
Related Pages
Implements Principle
Requires Environment
- Environment:AUTOMATIC1111_Stable_diffusion_webui_Python_And_PyTorch_Runtime
- Environment:AUTOMATIC1111_Stable_diffusion_webui_GPU_Compute_Backend