unslothai/unsloth · error · ValueError

snr_gamma must be > 0, or null to disable min-SNR weighting

Error message

snr_gamma must be > 0, or null to disable min-SNR weighting

What it means

snr_gamma controls min-SNR loss weighting. A zero or negative gamma would zero out or invert the min-SNR weight, silently training on a degenerate loss, so it must be strictly positive. None is the documented way to disable min-SNR weighting.

Source

Thrown at studio/backend/core/training/diffusion_train_common.py:1172

        if not isinstance(flow_shift, str):
            flow_shift = float(flow_shift)
            # isfinite as well as positive: JSON accepts 1e309, which floats to inf and would poison every sampled sigma while progress looks normal.
            if not math.isfinite(flow_shift) or flow_shift <= 0:
                raise ValueError(
                    "flow_shift must be a finite number > 0 (1.0 disables the shift), or 'auto'"
                )
        try:
            cfg_dropout = float(self.cfg_dropout or 0.0)
        except (TypeError, ValueError) as exc:
            raise ValueError(f"cfg_dropout must be a number, got {self.cfg_dropout!r}") from exc
        if not 0.0 <= cfg_dropout <= 1.0:
            raise ValueError("cfg_dropout must be between 0 and 1")
        weighting_scheme = str(self.weighting_scheme or "none").strip().lower()
        if weighting_scheme not in ("none", "bell"):
            raise ValueError("weighting_scheme must be one of none / bell")
        # A zero/negative gamma would zero out (or invert) the min-SNR weight and silently train on a degenerate loss; None is the documented disable.
        if self.snr_gamma is not None and float(self.snr_gamma) <= 0:
            raise ValueError("snr_gamma must be > 0, or null to disable min-SNR weighting")
        # learning_rate can arrive as a string ("1e-4") from the Studio config path, so coerce it before AdamW sees it.
        try:
            learning_rate = float(self.learning_rate)
        except (TypeError, ValueError) as exc:
            raise ValueError(f"learning_rate must be a number, got {self.learning_rate!r}") from exc
        if learning_rate <= 0:
            raise ValueError("learning_rate must be > 0")
        alpha = self.lora_alpha if self.lora_alpha is not None else self.lora_rank
        targets = tuple(self.lora_target_modules) or DEFAULT_LORA_TARGETS
        # A blank Hub token (the Studio default when none is configured) must load anonymously, not as an explicit empty credential.
        token = self.hf_token.strip() if isinstance(self.hf_token, str) else self.hf_token
        from core.inference.diffusion_families import (
            _is_local_path,
            mirror_repo,
            prefer_ungated_mirror,
            upstream_is_gated,
        )

View on GitHub (pinned to 203007d190)

Solutions

  1. Set snr_gamma=None to disable min-SNR weighting.
  2. Use a positive value such as 5.0 (the common default in the literature) to enable it.

Example fix

# before
cfg = DiffusionLoraConfig(snr_gamma=0)
# after
cfg = DiffusionLoraConfig(snr_gamma=None)  # disable, or 5.0 to enable
Defensive patterns

Strategy: type-guard

Validate before calling

if snr_gamma is not None:
    assert float(snr_gamma) > 0, 'snr_gamma must be > 0, or None to disable'

Type guard

def is_valid_snr_gamma(v) -> bool:
    return v is None or (isinstance(v, (int, float)) and not isinstance(v, bool) and v > 0)

Prevention

When it happens

Trigger: snr_gamma=0 (attempting to disable) or a negative value in the config.

Common situations: Users set 0 expecting 'off' behavior; the API uses None for off, not 0.

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/121ccd28f8db5ba2. Report an issue: GitHub.