lllyasviel/ControlNet · error · ValueError

Invalid learning rate: {}

Error message

Invalid learning rate: {}

What it means

Raised by the EMA-tracking AdamW optimizer in ldm/util.py when the learning rate passed to __init__ is negative (lr < 0.0). This mirrors torch.optim.AdamW's built-in argument validation; lr == 0 is allowed but any negative value aborts construction.

Source

Thrown at ldm/util.py:97

    return get_obj_from_str(config["target"])(**config.get("params", dict()))


def get_obj_from_str(string, reload=False):
    module, cls = string.rsplit(".", 1)
    if reload:
        module_imp = importlib.import_module(module)
        importlib.reload(module_imp)
    return getattr(importlib.import_module(module, package=None), cls)


class AdamWwithEMAandWings(optim.Optimizer):
    # credit to https://gist.github.com/crowsonkb/65f7265353f403714fce3b2595e0b298
    def __init__(self, params, lr=1.e-3, betas=(0.9, 0.999), eps=1.e-8,  # TODO: check hyperparameters before using
                 weight_decay=1.e-2, amsgrad=False, ema_decay=0.9999,   # ema decay to match previous code
                 ema_power=1., param_names=()):
        """AdamW that saves EMA versions of the parameters."""
        if not 0.0 <= lr:
            raise ValueError("Invalid learning rate: {}".format(lr))
        if not 0.0 <= eps:
            raise ValueError("Invalid epsilon value: {}".format(eps))
        if not 0.0 <= betas[0] < 1.0:
            raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0]))
        if not 0.0 <= betas[1] < 1.0:
            raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1]))
        if not 0.0 <= weight_decay:
            raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
        if not 0.0 <= ema_decay <= 1.0:
            raise ValueError("Invalid ema_decay value: {}".format(ema_decay))
        defaults = dict(lr=lr, betas=betas, eps=eps,
                        weight_decay=weight_decay, amsgrad=amsgrad, ema_decay=ema_decay,
                        ema_power=ema_power, param_names=param_names)
        super().__init__(params, defaults)

    def __setstate__(self, state):
        super().__setstate__(state)
        for group in self.param_groups:

View on GitHub (pinned to ed85cd1e25)

Solutions

  1. Check the lr value at the call site and fix the sign (e.g. 1e-4 not -1e-4)
  2. If lr comes from a config, validate/clamp it: max(lr, 0.0) only if a floor is intended; otherwise fix the source of the value
  3. If lr is computed by a scheduler lambda, guard the lambda to return max(new_lr, 0.0)

Example fix

# before
opt = AdamW(params, lr=-1e-4)
# after
opt = AdamW(params, lr=1e-4)
Defensive patterns

Strategy: validation

Validate before calling

assert 0.0 <= lr, f'bad lr: {lr}'

Try / catch

try:
    opt = AdamW(params, lr=lr)
except ValueError as e:
    if 'learning rate' in str(e):
        raise ValueError(f'Fix learning rate config: lr={lr}')
    raise

Prevention

When it happens

Trigger: Constructing this AdamW with a negative lr, e.g. AdamW(params, lr=-1e-3), or reading lr from a config/YAML where the value is negative (mistyped sign, subtraction bug, or a schedule that computed a negative value).

Common situations: Diffusion training scripts that compute lr from a schedule or multiply by a factor that underflows past zero, typo'd YAML values, or CLI arg parsing that passes a negated number.

Related errors


AI-assisted analysis of lllyasviel/ControlNet@ed85cd1e25 (2026-08-27). Data as JSON: /api/errors/1c61ce31c4739510. Report an issue: GitHub.