Implementation:AUTOMATIC1111 Stable diffusion webui Model Loader
| Knowledge Sources | |
|---|---|
| Domains | Model_Management, Upscaling |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Provides utilities for discovering, downloading, and loading model files from local directories and remote URLs, as well as dynamically registering upscaler model classes and loading Spandrel model architectures.
Description
The Model Loader module is the central hub for finding and loading model files in the WebUI. The load_file_from_url function downloads a file from a URL into a specified directory if not already present, using PyTorch's hub download mechanism. The load_models function searches multiple directories (command-line path, default model path, pretrained_models subdirectory) for model files matching given extension filters, with optional blacklisting and automatic downloading as a fallback. The load_upscalers function dynamically imports all *_model.py modules, discovers Upscaler subclasses, instantiates them, and registers their scalers in a sorted global list. The load_spandrel_model function loads neural network models using the Spandrel library with support for half-precision, custom dtype, and optional architecture verification. The friendly_name utility extracts a human-readable model name from a file path or URL.
Usage
Use this module when you need to locate model files on disk, download missing models from URLs, register upscaler backends, or load Spandrel-compatible super-resolution and restoration models.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/modelloader.py
- Lines: 1-197
Signature
def load_file_from_url(url: str, *, model_dir: str, progress: bool = True, file_name: str | None = None, hash_prefix: str | None = None) -> str
def load_models(model_path: str, model_url: str = None, command_path: str = None, ext_filter=None, download_name=None, ext_blacklist=None, hash_prefix=None) -> list
def friendly_name(file: str) -> str
def load_upscalers() -> None
def load_spandrel_model(path: str | os.PathLike, *, device: str | torch.device | None, prefer_half: bool = False, dtype: str | torch.dtype | None = None, expected_architecture: str | None = None) -> spandrel.ModelDescriptor
Import
from modules import modelloader
from modules.modelloader import load_file_from_url, load_models
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | str | Yes | URL to download the model from |
| model_dir | str | Yes | Local directory to store or find the model file |
| model_path | str | Yes | The primary directory to search for models |
| command_path | str | No | An additional command-line-specified directory to search first |
| ext_filter | list[str] | No | List of file extensions to include (e.g., [".pt", ".pth"]) |
| ext_blacklist | list[str] | No | List of file extensions to exclude |
| download_name | str | No | If specified and no models found, download from model_url with this filename |
| path | str | Yes | Path to a Spandrel-compatible model file |
| device | str or torch.device | Yes | Target device for loading the Spandrel model |
| prefer_half | bool | No | Whether to convert the model to half precision if supported |
| expected_architecture | str | No | Expected Spandrel architecture name for validation |
Outputs
| Name | Type | Description |
|---|---|---|
| cached_file | str | Absolute path to the downloaded or existing model file |
| output | list[str] | List of discovered model file paths |
| model_descriptor | spandrel.ModelDescriptor | A loaded Spandrel model descriptor with architecture information |
Usage Examples
from modules import modelloader
# Download a model file if not present
path = modelloader.load_file_from_url(
"https://example.com/model.pth",
model_dir="/models/ESRGAN",
file_name="4x_ultrasharp.pth"
)
# Find all .pth models in a directory
models = modelloader.load_models(
model_path="/models/ESRGAN",
ext_filter=[".pth"],
ext_blacklist=[".txt"]
)
# Load upscalers at startup
modelloader.load_upscalers()
# Load a Spandrel model
descriptor = modelloader.load_spandrel_model(
"/models/ESRGAN/4x_ultrasharp.pth",
device="cuda",
prefer_half=True
)