huggingface/pytorch-image-models · error · ValueError

Invalid {name}: {value}

Error message

Invalid {name}: {value}

What it means

The range check in _validate_scalar: values must satisfy value >= min_value (default 0) and, when max_value is given, value < max_value. Violations (negative lr/eps/weight decay, or betas >= 1) raise 'Invalid {name}'.

Source

Thrown at timm/optim/_helpers.py:76

        state_steps: Sequence[Tensor],
        supports_xla: bool = True,
) -> None:
    capturable_supported_devices = _get_capturable_supported_devices(supports_xla=supports_xla)
    assert all(
        p.device.type == step.device.type and p.device.type in capturable_supported_devices
        for p, step in zip(params, state_steps)
    ), f"If capturable=True, params and state_steps must be on supported devices: {capturable_supported_devices}."


def _validate_scalar(name: str, value, min_value: float = 0.0, max_value: Optional[float] = None) -> None:
    if torch.is_tensor(value):
        if value.numel() != 1:
            raise ValueError(f"{name} must be a scalar or scalar tensor.")
        value_float = float(value.detach().cpu())
    else:
        value_float = float(value)
    if value_float < min_value or (max_value is not None and value_float >= max_value):
        raise ValueError(f"Invalid {name}: {value}")


def _add_scaled_(param: Tensor, update: Tensor, scale) -> None:
    if torch.is_tensor(scale):
        param.add_(update * scale)
    else:
        param.add_(update, alpha=scale)


def _addcdiv_scaled_(param: Tensor, tensor1: Tensor, tensor2: Tensor, scale) -> None:
    if torch.is_tensor(scale):
        param.add_(tensor1 / tensor2 * scale)
    else:
        param.addcdiv_(tensor1, tensor2, value=scale)

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Clamp/validate hyper-parameters to their valid ranges before constructing the optimizer (lr>0, 0<=beta<1, eps>0)
  2. Fix the sign or magnitude in the config source (YAML/CLI) that produced the bad value

Example fix

# before
opt = timm.optim.AdamW(params, lr=-1e-3, betas=(1.2, 0.99))
# after
opt = timm.optim.AdamW(params, lr=1e-3, betas=(0.9, 0.99))
Defensive patterns

Strategy: validation

Validate before calling

assert lr > 0 and 0.0 <= beta1 < 1.0 and 0.0 <= beta2 < 1.0 and eps > 0 and weight_decay >= 0

Type guard

def valid_hp(name: str, v, lo=0.0, hi=None) -> bool:
    v = v.item() if torch.is_tensor(v) else v
    return v >= lo and (hi is None or v < hi)

Try / catch

try:
    opt = timm.optim.create_optimizer_v2(model, opt='adamw', lr=lr, **extra)
except ValueError as e:
    if 'Invalid' in str(e):
        lr = max(lr, 1e-8)
        opt = timm.optim.create_optimizer_v2(model, opt='adamw', lr=lr, **extra)
    else:
        raise

Prevention

When it happens

Trigger: AdamW(lr=-0.1), eps=0, or betas=(1.2, 0.999) with timm optimizers; weight_decay=-1e-4 via create_optimizer_v2 kwargs.

Common situations: Sweep configs where a search produces out-of-range values; reading betas/eps from env vars or YAML without validation; sign errors on weight decay.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/e51bb81d39038f41. Report an issue: GitHub.