{"record":{"id":"4a890173596a14ca","repo":"hiyouga/LlamaFactory","slug":"compute-dtype-self-compute-dtype-r-is-not-a-torc","errorCode":null,"errorMessage":"compute_dtype={self.compute_dtype!r} is not a torch dtype name.","messagePattern":"compute_dtype=(.+?) is not a torch dtype name\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/model_plugins/quantization.py","lineNumber":52,"sourceCode":"    ) -> dict[str, Any]:\n        return super().__call__(init_kwargs, quant_config=quant_config, is_trainable=is_trainable)\n\n\n@dataclass\nclass BnbParams:\n    name: Literal[\"bnb\", \"auto\"] = \"bnb\"\n    quantization_bit: int | None = None\n    compute_dtype: str | Any = \"float16\"\n    double_quantization: bool = True\n    quantization_type: str = \"nf4\"\n\n    def __post_init__(self) -> None:\n        import torch\n\n        if isinstance(self.compute_dtype, str):\n            dtype = getattr(torch, self.compute_dtype, None)\n            if not isinstance(dtype, torch.dtype):\n                raise ValueError(f\"compute_dtype={self.compute_dtype!r} is not a torch dtype name.\")\n            self.compute_dtype = dtype\n        elif not isinstance(self.compute_dtype, torch.dtype):\n            raise TypeError(f\"compute_dtype must be str or torch.dtype, got {type(self.compute_dtype).__name__}.\")\n\n\n@QuantizationPlugin(\"auto\").register()\ndef quantization_auto(\n    init_kwargs: dict[str, Any],\n    quant_config: dict | BnbParams,\n    is_trainable: bool = False,\n) -> dict[str, Any]:\n    quant_config = QuantizationPlugin.parse_params(quant_config, BnbParams)\n    if quant_config.quantization_bit is None:\n        logger.warning_rank0(\"No quantization method applied.\")\n        return init_kwargs\n    if quant_config.quantization_bit not in (4, 8):\n        raise ValueError(f\"Unsupported quantization bit: {quant_config.quantization_bit} for auto quantization.\")\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/model_plugins/quantization.py#L34-L70","documentation":"BnbParams.__post_init__ converts the compute_dtype string to a torch.dtype via getattr(torch, name). If the string is not the name of a torch dtype (misspelled, wrong casing, or an unrelated torch attribute that is not a dtype), conversion fails and this ValueError is raised. Valid values are strings like 'float16', 'bfloat16', 'float32'.","triggerScenarios":"Setting compute_dtype: fp16 / bf16 / float / float64-typo in the quantization config; anything where getattr(torch, s) is not a torch.dtype instance.","commonSituations":"Users abbreviate dtypes (fp16, bf16) out of habit from other frameworks; or copy a config from a tool that uses different dtype names.","solutions":["Use the exact torch dtype name: float16, bfloat16, or float32","Check casing: 'Float16' is invalid","Alternatively pass an actual torch.dtype object if constructing BnbParams programmatically"],"exampleFix":"# before\nquantization:\n  compute_dtype: fp16\n\n# after\nquantization:\n  compute_dtype: float16","handlingStrategy":"type-guard","validationCode":"import torch\nif isinstance(compute_dtype, str):\n    assert isinstance(getattr(torch, compute_dtype, None), torch.dtype), f\"bad compute_dtype {compute_dtype!r}\"","typeGuard":"def is_torch_dtype_name(s: str) -> bool:\n    import torch\n    return isinstance(getattr(torch, s, None), torch.dtype)","tryCatchPattern":null,"preventionTips":["Restrict compute_dtype values to float16/bfloat16/float32 in config validation","Avoid abbreviations like fp16/bf16 from other frameworks"],"tags":["quantization","bitsandbytes","dtype","configuration"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}