{"record":{"id":"c2261eb31b5ea627","repo":"Stability-AI/generative-models","slug":"sampler-and-loss-function-need-to-be-set-for-train","errorCode":null,"errorMessage":"Sampler and loss function need to be set for training.","messagePattern":"Sampler and loss function need to be set for training\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sgm/models/diffusion.py","lineNumber":191,"sourceCode":"            \"global_step\",\n            self.global_step,\n            prog_bar=True,\n            logger=True,\n            on_step=True,\n            on_epoch=False,\n        )\n\n        if self.scheduler_config is not None:\n            lr = self.optimizers().param_groups[0][\"lr\"]\n            self.log(\n                \"lr_abs\", lr, prog_bar=True, logger=True, on_step=True, on_epoch=False\n            )\n\n        return loss\n\n    def on_train_start(self, *args, **kwargs):\n        if self.sampler is None or self.loss_fn is None:\n            raise ValueError(\"Sampler and loss function need to be set for training.\")\n\n    def on_train_batch_end(self, *args, **kwargs):\n        if self.use_ema:\n            self.model_ema(self.model)\n\n    @contextmanager\n    def ema_scope(self, context=None):\n        if self.use_ema:\n            self.model_ema.store(self.model.parameters())\n            self.model_ema.copy_to(self.model)\n            if context is not None:\n                print(f\"{context}: Switched to EMA weights\")\n        try:\n            yield None\n        finally:\n            if self.use_ema:\n                self.model_ema.restore(self.model.parameters())\n                if context is not None:","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/Stability-AI/generative-models/blob/e8cd657656fa5d61688191730d0e03242bf4ed44/sgm/models/diffusion.py#L173-L209","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nmodel:\n  target: sgm.models.diffusion.DiffusionEngine\n  params:\n    network_config: ...\n    # no sampler / loss_fn\n// after\nmodel:\n  target: sgm.models.diffusion.DiffusionEngine\n  params:\n    network_config: ...\n    sampler:\n      target: sgm.samplers.EulerEDMSampler  # or any configured sampler\n    loss_fn:\n      target: sgm.modules.diffusionmodules.loss.StandardDiffusionLoss","handlingStrategy":"validation","validationCode":"params = config.model.params\nassert params.get('sampler') is not None, \"config.model.params.sampler missing\"\nassert params.get('loss_fn') is not None, \"config.model.params.loss_fn missing\"\n# before calling trainer.fit(config.model, ...)","typeGuard":null,"tryCatchPattern":"try:\n    trainer.fit(model, data=dm)\nexcept ValueError as e:\n    if \"Sampler and loss function need to be set\" in str(e):\n        raise RuntimeError(\"Add sampler and loss_fn sections to the model config before training\") from e","preventionTips":["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."],"tags":["configuration","training","missing-config"],"backgroundTag":"missing-required-config","analyzedSha":"e8cd657656fa5d61688191730d0e03242bf4ed44","analyzedAt":"2026-08-29T11:23:43.234Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}