Stability-AI/generative-models · error · ValueError
Sampler and loss function need to be set for training.
Error message
Sampler and loss function need to be set for training.
What it means
DiffusionEngine.on_train_start asserts, at the moment training actually begins, that both self.sampler and self.loss_fn were configured. If either is None it raises this ValueError — meaning you instantiated a DiffusionEngine without a sampler or loss and tried to call .fit() on it.
Source
Thrown at sgm/models/diffusion.py:191
"global_step",
self.global_step,
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
)
if self.scheduler_config is not None:
lr = self.optimizers().param_groups[0]["lr"]
self.log(
"lr_abs", lr, prog_bar=True, logger=True, on_step=True, on_epoch=False
)
return loss
def on_train_start(self, *args, **kwargs):
if self.sampler is None or self.loss_fn is None:
raise ValueError("Sampler and loss function need to be set for training.")
def on_train_batch_end(self, *args, **kwargs):
if self.use_ema:
self.model_ema(self.model)
@contextmanager
def ema_scope(self, context=None):
if self.use_ema:
self.model_ema.store(self.model.parameters())
self.model_ema.copy_to(self.model)
if context is not None:
print(f"{context}: Switched to EMA weights")
try:
yield None
finally:
if self.use_ema:
self.model_ema.restore(self.model.parameters())
if context is not None:View on GitHub (pinned to e8cd657656)
Solutions
- Add a `sampler` section to the model config (e.g. a DDIMSampler target with its discretization/guider configs).
- Add a `loss_fn` section to the model config (e.g. StandardDiffusionLoss target with its noise schedule).
- If you only intended inference, don't call trainer.fit on this model; run text_to_image-style sampling instead.
Example fix
// before
model:
target: sgm.models.diffusion.DiffusionEngine
params:
network_config: ...
# no sampler / loss_fn
// after
model:
target: sgm.models.diffusion.DiffusionEngine
params:
network_config: ...
sampler:
target: sgm.samplers.EulerEDMSampler # or any configured sampler
loss_fn:
target: sgm.modules.diffusionmodules.loss.StandardDiffusionLoss Defensive patterns
Strategy: validation
Validate before calling
params = config.model.params
assert params.get('sampler') is not None, "config.model.params.sampler missing"
assert params.get('loss_fn') is not None, "config.model.params.loss_fn missing"
# before calling trainer.fit(config.model, ...) Try / catch
try:
trainer.fit(model, data=dm)
except ValueError as e:
if "Sampler and loss function need to be set" in str(e):
raise RuntimeError("Add sampler and loss_fn sections to the model config before training") from e Prevention
- Use training configs (not inference configs) as the base for .fit() runs.
- Never delete or comment out sampler/loss_fn keys when pruning YAML configs.
When it happens
Trigger: Instantiating DiffusionEngine from a config where the `sampler:` or `loss_fn:` key is missing/null, then calling trainer.fit(model). Inference-only usage without these keys works; training does not.
Common situations: Reusing an inference config for training; a YAML config edit deleting or commenting out loss_fn/sampler; target class instantiated with only network params (ckpt, conditioner) for finetuning without defining the loss.
Understand the failure class
Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.
Related errors
- Model {model_id} not supported
- unknown discretization {params.discretization}
- unknown sampler {params.sampler}!
- Initializing ActNorm in reverse direction is disabled by def
- unsupported dimensions: {dims}
AI-assisted analysis of Stability-AI/generative-models@e8cd657656 (2026-08-29).
Data as JSON: /api/errors/c2261eb31b5ea627.
Report an issue: GitHub.