Implementation:Huggingface Diffusers Scheduler From Config
| Knowledge Sources | |
|---|---|
| Domains | Diffusion_Models, Sampling_Algorithms, ODE_Solvers |
| Last Updated | 2026-02-13 21:00 GMT |
Overview
Concrete tool for instantiating a noise scheduler from a configuration dictionary provided by the Diffusers library.
Description
ConfigMixin.from_config is a class method inherited by all scheduler classes in Diffusers. It takes a configuration dictionary (typically obtained from another scheduler's .config attribute) and returns a new scheduler instance initialized with those parameters. This is the primary mechanism for swapping schedulers at inference time: you extract the config from the pipeline's current scheduler and pass it to a different scheduler class's from_config method.
The method works by extracting the initialization parameters from the config dictionary, matching them to the target class's __init__ signature, and constructing a new instance. Any parameters in the config that are not recognized by the target class are silently ignored (or optionally returned as unused kwargs). Additional keyword arguments can be passed to override specific config values during instantiation.
Internally, the method calls cls.extract_init_dict(config, **kwargs) to separate known parameters from unknown ones, then calls cls(**init_dict) to create the new scheduler. It also preserves hidden metadata (like _class_name) for compatibility tracking.
Usage
Use from_config whenever you need to swap the scheduler on an existing pipeline. The typical pattern is to access pipeline.scheduler.config and pass it to the desired scheduler class. This is safe because all Diffusers schedulers share a common set of core configuration parameters (num_train_timesteps, beta_start, beta_end, beta_schedule, prediction_type).
Code Reference
Source Location
- Repository: diffusers
- File:
src/diffusers/configuration_utils.py - Lines: 207-329
Signature
@classmethod
def from_config(
cls,
config: FrozenDict | dict[str, Any] = None,
return_unused_kwargs=False,
**kwargs,
) -> Self | tuple[Self, dict[str, Any]]:
Import
from diffusers import EulerDiscreteScheduler # or any scheduler class
# from_config is a class method available on all scheduler classes:
# DDPMScheduler, DDIMScheduler, PNDMScheduler, DPMSolverMultistepScheduler,
# EulerAncestralDiscreteScheduler, UniPCMultistepScheduler, etc.
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | FrozenDict or dict[str, Any] |
Yes | A configuration dictionary from which the scheduler is instantiated. Typically obtained via scheduler.config from an existing scheduler instance. Contains parameters such as num_train_timesteps, beta_start, beta_end, beta_schedule, and prediction_type.
|
| return_unused_kwargs | bool |
No | Whether to return keyword arguments not consumed by the scheduler class. Defaults to False.
|
| **kwargs | dict |
No | Additional keyword arguments that override values in the config dictionary. Useful for changing specific parameters (e.g., use_karras_sigmas=True) during scheduler swapping.
|
Outputs
| Name | Type | Description |
|---|---|---|
| scheduler | SchedulerMixin (or subclass) |
A new scheduler instance initialized from the provided configuration dictionary. |
| unused_kwargs (optional) | dict[str, Any] |
Only returned if return_unused_kwargs=True. Contains any keyword arguments that were not consumed by the scheduler class constructor.
|
Usage Examples
Basic Usage
from diffusers import DiffusionPipeline, EulerDiscreteScheduler
import torch
# Load a pipeline with its default scheduler
pipe = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
)
# Swap to Euler scheduler using the existing scheduler's config
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
image = pipe("A vibrant coral reef underwater", num_inference_steps=25).images[0]
image.save("coral_reef.png")
Swapping With Parameter Overrides
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch
pipe = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
)
# Swap to DPM-Solver++ with Karras sigmas for faster, high-quality sampling
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
pipe.scheduler.config,
use_karras_sigmas=True,
algorithm_type="dpmsolver++",
)
pipe = pipe.to("cuda")
image = pipe("A mountain lake at dawn", num_inference_steps=20).images[0]
Multiple Scheduler Comparison
from diffusers import (
DiffusionPipeline,
DDIMScheduler,
EulerDiscreteScheduler,
EulerAncestralDiscreteScheduler,
)
import torch
pipe = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
).to("cuda")
schedulers = {
"ddim": DDIMScheduler,
"euler": EulerDiscreteScheduler,
"euler_a": EulerAncestralDiscreteScheduler,
}
config = pipe.scheduler.config
for name, scheduler_cls in schedulers.items():
pipe.scheduler = scheduler_cls.from_config(config)
image = pipe("A castle on a hill", generator=torch.manual_seed(42)).images[0]
image.save(f"castle_{name}.png")