{"record":{"id":"1e7b338491730188","repo":"microsoft/qlib","slug":"unknown-unit","errorCode":null,"errorMessage":"Unknown unit: {:}","messagePattern":"Unknown unit: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_utils.py","lineNumber":36,"sourceCode":"    The number of parameters of the given model(s) or parameters.\n    \"\"\"\n    if isinstance(models_or_parameters, nn.Module):\n        counts = sum(v.numel() for v in models_or_parameters.parameters())\n    elif isinstance(models_or_parameters, nn.Parameter):\n        counts = models_or_parameters.numel()\n    elif isinstance(models_or_parameters, (list, tuple)):\n        return sum(count_parameters(x, unit) for x in models_or_parameters)\n    else:\n        counts = sum(v.numel() for v in models_or_parameters)\n    unit = unit.lower()\n    if unit in (\"kb\", \"k\"):\n        counts /= 2**10\n    elif unit in (\"mb\", \"m\"):\n        counts /= 2**20\n    elif unit in (\"gb\", \"g\"):\n        counts /= 2**30\n    elif unit is not None:\n        raise ValueError(\"Unknown unit: {:}\".format(unit))\n    return counts\n","sourceCodeStart":18,"sourceCodeEnd":38,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_utils.py#L18-L38","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use one of the supported units: 'kb'/'k', 'mb'/'m', 'gb'/'g' (any case), or pass unit=None for a raw parameter count.","If you intended a raw count of parameters, call count_parameters(model) with no unit argument.","Check for typos/whitespace in the unit string (it is only lowercased, not stripped)."],"exampleFix":"# before\ncount_parameters(model, unit=\"bytes\")  # ValueError: Unknown unit: bytes\n\n# after\ncount_parameters(model, unit=None)     # raw parameter count\ncount_parameters(model, unit=\"mb\")     # supported: kb/k, mb/m, gb/g","handlingStrategy":"type-guard","validationCode":"unit = unit.lower() if isinstance(unit, str) else unit\nif unit not in (None, \"kb\", \"k\", \"mb\", \"m\", \"gb\", \"g\"):\n    raise ValueError(f\"unsupported unit {unit!r}; use kb/k, mb/m, gb/g or None\")\ncount = count_parameters(model, unit=unit)","typeGuard":"VALID_UNITS = {None, \"kb\", \"k\", \"mb\", \"m\", \"gb\", \"g\"}\n\ndef valid_unit(u) -> bool:\n    return u is None or (isinstance(u, str) and u.lower() in VALID_UNITS)","tryCatchPattern":null,"preventionTips":["Prefer unit=None for raw parameter counts.","Centralize the allowed-unit set in one constant shared by callers.","Remember the string is lowercased but not stripped: avoid whitespace."],"tags":["qlib","pytorch","validation","arguments"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}