huggingface/pytorch-image-models · error · TypeError
Coefficients must be a preset name (str), a 3-sequence (a,b,
Error message
Coefficients must be a preset name (str), a 3-sequence (a,b,c), or a sequence of 3-sequences.
What it means
Thrown by resolve_ns_coefficients in timm's Muon optimizer when the ns_coefficients argument is neither a string preset name nor a sequence. The API accepts only a preset name, a single 3-sequence (a,b,c), or a sequence of 3-sequences.
Source
Thrown at timm/optim/muon.py:1039
is_real = lambda x: isinstance(x, numbers.Real) and not isinstance(x, bool)
def as_coeff(x: Sequence[float]) -> Tuple[float, float, float]:
if not is_seq(x) or len(x) != 3 or not all(is_real(v) for v in x):
raise ValueError(f"Coefficient must be length-3 of real numbers, got: {x!r}")
a, b, c = x # type: ignore[misc]
return float(a), float(b), float(c)
if isinstance(value, str):
if value not in presets:
valid = ", ".join(sorted(presets.keys()))
raise ValueError(f"Unknown coefficients preset '{value}'. Valid options: {valid}")
seq = presets[value]
if not is_seq(seq) or len(seq) == 0:
raise ValueError(f"Preset '{value}' is empty or invalid")
return [as_coeff(item) for item in seq] # validate & cast
if not is_seq(value):
raise TypeError(
"Coefficients must be a preset name (str), a 3-sequence (a,b,c), "
"or a sequence of 3-sequences."
)
# Decide single triple vs list-of-triples by structure
if len(value) == 3 and all(is_real(v) for v in value): # type: ignore[index]
return [as_coeff(value)] # single triple -> wrap
# Otherwise treat as list/tuple of triples
out = []
for i, item in enumerate(value): # type: ignore[assignment]
if not is_seq(item):
raise TypeError(f"Item {i} is not a sequence: {item!r}")
out.append(as_coeff(item))
if not out:
raise ValueError("Coefficient list cannot be empty")
return out
View on GitHub (pinned to 9a5261e31b)
Solutions
- Pass a preset name string, e.g. ns_coefficients='nesterov'
- Pass a single triple: ns_coefficients=(0.9, 0.999, 1e-8) style (a,b,c)
- Pass a list of triples for per-tensor coefficients
Example fix
# before Muon(params, ns_coefficients=0.9) # after Muon(params, ns_coefficients=(0.9, 0.95, 0.5))
Defensive patterns
Strategy: type-guard
Validate before calling
from collections.abc import Sequence assert isinstance(ns_coefficients, (str, Sequence)), 'ns_coefficients must be str preset or sequence'
Type guard
def is_valid_coeffs(v) -> bool:
return isinstance(v, str) or (isinstance(v, (list, tuple)) and not isinstance(v, str)) Prevention
- Pass explicit tuples like (a, b, c) or lists of triples
- Avoid scalars/None for ns_coefficients
When it happens
Trigger: Passing a scalar (ns_coefficients=0.5), None, or any non-iterable to timm.optim.Muon/AdamMuon's ns_coefficients parameter.
Common situations: Assuming ns_coefficients takes a single float, passing an uninitialized None default, or passing a dict of coefficients.
Related errors
- Item {i} is not a sequence: {item!r}
- Preset '{value}' is empty or invalid
- Coefficient list cannot be empty
- Please provide `hook_fns` for each `hook_fn_locs`, their len
- Input image must have positive dimensions, got H={height}, W
AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27).
Data as JSON: /api/errors/7585f52949a43730.
Report an issue: GitHub.