hiyouga/LlamaFactory · error · RuntimeError

Unexpected precision: {precision}

Error message

Unexpected precision: {precision}

What it means

RuntimeError from DtypeInterface.is_available (dtype.py:48) when the precision argument is not in HALF_LIST (fp16/float16/half/torch.float16), FLOAT_LIST (fp32/float32/float/torch.float32), or BFLOAT_LIST (bf16/bfloat16/torch.bfloat16). The registry is a fixed allowlist, so any other string or torch dtype (e.g. 'fp8', 'tf32', torch.float64) is rejected with no fallback.

Source

Thrown at src/llamafactory/v1/utils/dtype.py:48


class DtypeInterface:
    """Type of precision used."""

    _is_fp16_available = is_torch_fp16_available_on_device(DistributedInterface().current_device)
    _is_bf16_available = is_torch_bf16_available_on_device(DistributedInterface().current_device)
    _is_fp32_available = True

    @staticmethod
    def is_available(precision: str | torch.dtype) -> bool:
        if precision in DtypeRegistry.HALF_LIST:
            return DtypeInterface._is_fp16_available
        elif precision in DtypeRegistry.FLOAT_LIST:
            return DtypeInterface._is_fp32_available
        elif precision in DtypeRegistry.BFLOAT_LIST:
            return DtypeInterface._is_bf16_available
        else:
            raise RuntimeError(f"Unexpected precision: {precision}")

    @staticmethod
    def is_fp16(precision: str | torch.dtype) -> bool:
        return precision in DtypeRegistry.HALF_LIST

    @staticmethod
    def is_fp32(precision: str | torch.dtype) -> bool:
        return precision in DtypeRegistry.FLOAT_LIST

    @staticmethod
    def is_bf16(precision: str | torch.dtype) -> bool:
        return precision in DtypeRegistry.BFLOAT_LIST

    @staticmethod
    def to_dtype(precision: str | torch.dtype) -> torch.dtype:
        if precision in DtypeRegistry.HALF_LIST:
            return torch.float16
        elif precision in DtypeRegistry.FLOAT_LIST:

View on GitHub (pinned to f28afaf635)

Solutions

  1. Normalize the precision to one of the supported strings: fp16/float16/half, fp32/float32/float, or bf16/bfloat16 (or the matching torch dtype).
  2. Resolve 'auto'-style values yourself before calling: pick bf16 if available, else fp16.
  3. If you control the caller, validate precision against DtypeRegistry lists before invoking is_available().

Example fix

# before
precision = "auto"
DtypeInterface.is_available(precision)

# after
precision = "bf16" if DtypeInterface.is_bf16_available_variant else "fp16"
DtypeInterface.is_available(precision)
Defensive patterns

Strategy: validation

Validate before calling

from llamafactory.v1.utils.dtype import DtypeRegistry
VALID = set(DtypeRegistry.HALF_LIST + DtypeRegistry.FLOAT_LIST + DtypeRegistry.BFLOAT_LIST)
assert precision in VALID, f"unsupported precision {precision!r}; valid: fp16/fp32/bf16"

Type guard

def is_supported_precision(p) -> bool:
    from llamafactory.v1.utils.dtype import DtypeRegistry
    return p in DtypeRegistry.HALF_LIST + DtypeRegistry.FLOAT_LIST + DtypeRegistry.BFLOAT_LIST

Prevention

When it happens

Trigger: Calling DtypeInterface.is_available('fp8'), is_available('tf32'), or passing a config precision value like 'auto' or a torch dtype such as torch.float64. Also 'float' vs 'foat' typos from YAML.

Common situations: Newer quantization/dtype names (fp8, tf32) set in training args reach this helper; users pass 'auto' expecting transformers-style resolution; mixed torch.dtype objects and strings from different code paths.

Related errors


AI-assisted analysis of hiyouga/LlamaFactory@f28afaf635 (2026-08-14). Data as JSON: /api/errors/4add9c1ce4eeeac9. Report an issue: GitHub.