Stability-AI/generative-models · error · NotImplementedError

Unknown loss type {self.loss_type}

Error message

Unknown loss type {self.loss_type}

What it means

VDenoisingWarmupLoss/VAEDiffusionLoss's get_loss only implements loss_type values such as 'l1', 'l2', and 'lpips'; any other configured loss type raises NotImplementedError inside the training step.

Source

Thrown at sgm/modules/diffusionmodules/loss.py:105

            network, noised_input, sigmas, cond, **additional_model_inputs
        )
        w = append_dims(self.loss_weighting(sigmas), input.ndim)
        return self.get_loss(model_output, input, w)

    def get_loss(self, model_output, target, w):
        if self.loss_type == "l2":
            return torch.mean(
                (w * (model_output - target) ** 2).reshape(target.shape[0], -1), 1
            )
        elif self.loss_type == "l1":
            return torch.mean(
                (w * (model_output - target).abs()).reshape(target.shape[0], -1), 1
            )
        elif self.loss_type == "lpips":
            loss = self.lpips(model_output, target).reshape(-1)
            return loss
        else:
            raise NotImplementedError(f"Unknown loss type {self.loss_type}")

View on GitHub (pinned to e8cd657656)

Solutions

  1. Set loss_type to 'l1', 'l2', or 'lpips' in the training config
  2. Fix the typo in loss_type
  3. Add the new loss implementation to get_loss if a custom loss is required

Example fix

// before (yaml)
loss_config:
  loss_type: mse
// after (yaml)
loss_config:
  loss_type: l2
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED = {"l1", "l2", "lpips"}
if loss_config["loss_type"] not in SUPPORTED:
    raise ValueError(f"loss_type {loss_config['loss_type']!r} unsupported; choose from {SUPPORTED}")

Type guard

def is_supported_loss(t) -> bool:
    return t in ("l1", "l2", "lpips")

Try / catch

try:
    loss = loss_module(x, t, context)
except NotImplementedError as e:
    raise ConfigError(f"training config uses unsupported loss: {e}") from e

Prevention

When it happens

Trigger: Training a model whose config sets loss_type to an unimplemented string (e.g. 'huber', 'mse', 'ssim') so get_loss reaches the final else.

Common situations: Copied configs from other diffusion repos using 'mse'/'mae' naming, hand-edited loss_type entries, or newer configs run against older library code.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of Stability-AI/generative-models@e8cd657656 (2026-08-29). Data as JSON: /api/errors/2b1dd18fd77a8485. Report an issue: GitHub.