microsoft/qlib · error · ValueError

Unknown unit: {:}

Error message

Unknown unit: {:}

What it means

Raised by count_parameters in qlib/contrib/model/pytorch_utils.py when the unit argument is not None and not one of the recognized byte units ('kb'/'k', 'mb'/'m', 'gb'/'g', case-insensitive). The function counts tensor elements and optionally converts to larger units; anything else is rejected. Note that in this codebase the conversion divides raw parameter counts by powers of 2**10/20/30, so the 'unit' is treated as a scale factor over the parameter count.

Source

Thrown at qlib/contrib/model/pytorch_utils.py:36

    The number of parameters of the given model(s) or parameters.
    """
    if isinstance(models_or_parameters, nn.Module):
        counts = sum(v.numel() for v in models_or_parameters.parameters())
    elif isinstance(models_or_parameters, nn.Parameter):
        counts = models_or_parameters.numel()
    elif isinstance(models_or_parameters, (list, tuple)):
        return sum(count_parameters(x, unit) for x in models_or_parameters)
    else:
        counts = sum(v.numel() for v in models_or_parameters)
    unit = unit.lower()
    if unit in ("kb", "k"):
        counts /= 2**10
    elif unit in ("mb", "m"):
        counts /= 2**20
    elif unit in ("gb", "g"):
        counts /= 2**30
    elif unit is not None:
        raise ValueError("Unknown unit: {:}".format(unit))
    return counts

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use one of the supported units: 'kb'/'k', 'mb'/'m', 'gb'/'g' (any case), or pass unit=None for a raw parameter count.
  2. If you intended a raw count of parameters, call count_parameters(model) with no unit argument.
  3. Check for typos/whitespace in the unit string (it is only lowercased, not stripped).

Example fix

# before
count_parameters(model, unit="bytes")  # ValueError: Unknown unit: bytes

# after
count_parameters(model, unit=None)     # raw parameter count
count_parameters(model, unit="mb")     # supported: kb/k, mb/m, gb/g
Defensive patterns

Strategy: type-guard

Validate before calling

unit = unit.lower() if isinstance(unit, str) else unit
if unit not in (None, "kb", "k", "mb", "m", "gb", "g"):
    raise ValueError(f"unsupported unit {unit!r}; use kb/k, mb/m, gb/g or None")
count = count_parameters(model, unit=unit)

Type guard

VALID_UNITS = {None, "kb", "k", "mb", "m", "gb", "g"}

def valid_unit(u) -> bool:
    return u is None or (isinstance(u, str) and u.lower() in VALID_UNITS)

Prevention

When it happens

Trigger: Calling count_parameters(model, unit='bytes'), unit='params', unit='KB ' (with whitespace/typo), or any string outside {kb,k,mb,m,gb,g} after .lower(). The comparison happens after unit.lower(), so mixed case like 'MB' is fine, but any other token raises.

Common situations: Writing a model-summary helper and guessing an unsupported unit name; passing a variable that is None-checked incorrectly (the code path 'elif unit is not None' fires for any non-None unknown string); copy-pasting a call from another library with different unit vocabulary.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/1e7b338491730188. Report an issue: GitHub.