Files
diffusers/docs/source/en/using-diffusers/automodel.md
Steven Liu 40de88af8c
Some checks failed
Build documentation / build (push) Has been cancelled
Run dependency tests / check_dependencies (push) Has been cancelled
Run Torch dependency tests / check_torch_dependencies (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Diffusers metadata / update_metadata (push) Has been cancelled
Nightly and release tests on main/release branch / Setup Torch Pipelines CUDA Slow Tests Matrix (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch Pipelines CUDA Tests (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch CUDA Tests (examples) (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch CUDA Tests (lora) (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch CUDA Tests (models) (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch CUDA Tests (others) (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch CUDA Tests (schedulers) (push) Has been cancelled
Nightly and release tests on main/release branch / Nightly Torch CUDA Tests (single_file) (push) Has been cancelled
Nightly and release tests on main/release branch / PyTorch Compile CUDA tests (push) Has been cancelled
Nightly and release tests on main/release branch / Torch tests on big GPU (push) Has been cancelled
Nightly and release tests on main/release branch / Torch Minimum Version CUDA Tests (push) Has been cancelled
Nightly and release tests on main/release branch / Torch quantization nightly tests (map[additional_deps:[] backend:nvidia_modelopt test_location:modelopt]) (push) Has been cancelled
Nightly and release tests on main/release branch / Torch quantization nightly tests (map[additional_deps:[] backend:optimum_quanto test_location:quanto]) (push) Has been cancelled
Nightly and release tests on main/release branch / Torch quantization nightly tests (map[additional_deps:[] backend:torchao test_location:torchao]) (push) Has been cancelled
Nightly and release tests on main/release branch / Torch quantization nightly tests (map[additional_deps:[peft kernels] backend:gguf test_location:gguf]) (push) Has been cancelled
Nightly and release tests on main/release branch / Torch quantization nightly tests (map[additional_deps:[peft] backend:bitsandbytes test_location:bnb]) (push) Has been cancelled
Nightly and release tests on main/release branch / Torch quantization nightly tests (push) Has been cancelled
Nightly and release tests on main/release branch / Generate Consolidated Test Report (push) Has been cancelled
Test, build, and push Docker images / test-build-docker-images (push) Has been cancelled
Test, build, and push Docker images / build-and-push-docker-images (diffusers-doc-builder) (push) Has been cancelled
Test, build, and push Docker images / build-and-push-docker-images (diffusers-pytorch-cpu) (push) Has been cancelled
Test, build, and push Docker images / build-and-push-docker-images (diffusers-pytorch-cuda) (push) Has been cancelled
Test, build, and push Docker images / build-and-push-docker-images (diffusers-pytorch-minimum-cuda) (push) Has been cancelled
Test, build, and push Docker images / build-and-push-docker-images (diffusers-pytorch-xformers-cuda) (push) Has been cancelled
[docs] AutoModel (#12644)
* automodel

* fix
2025-11-13 08:43:24 -08:00

2.2 KiB

AutoModel

The [AutoModel] class automatically detects and loads the correct model class (UNet, transformer, VAE) from a config.json file. You don't need to know the specific model class name ahead of time. It supports data types and device placement, and works across model types and libraries.

The example below loads a transformer from Diffusers and a text encoder from Transformers. Use the subfolder parameter to specify where to load the config.json file from.

import torch
from diffusers import AutoModel, DiffusionPipeline

transformer = AutoModel.from_pretrained(
    "Qwen/Qwen-Image", subfolder="transformer", torch_dtype=torch.bfloat16, device_map="cuda"
)

text_encoder = AutoModel.from_pretrained(
    "Qwen/Qwen-Image", subfolder="text_encoder", torch_dtype=torch.bfloat16, device_map="cuda"
)

[AutoModel] also loads models from the Hub that aren't included in Diffusers. Set trust_remote_code=True in [AutoModel.from_pretrained] to load custom models.

import torch
from diffusers import AutoModel

transformer = AutoModel.from_pretrained(
    "custom/custom-transformer-model", trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="cuda"
)

If the custom model inherits from the [ModelMixin] class, it gets access to the same features as Diffusers model classes, like regional compilation and group offloading.

Note

Learn more about implementing custom models in the Community components guide.