Lightning-AI/pytorch-lightning · error · ValueError

`precision={precision!r})` is not supported in XLA. `precisi

Error message

`precision={precision!r})` is not supported in XLA. `precision` must be one of: {supported_precision}.

What it means

XLAPrecision.__init__ validates the precision string against the _PRECISION_INPUT literal tuple before doing anything else. Any value that is not an exact supported precision mode string raises this ValueError, listing the accepted values.

Source

Thrown at src/lightning/fabric/plugins/precision/xla.py:44

class XLAPrecision(Precision):
    """Plugin for training with XLA.

    Args:
        precision: Full precision (32-true) or half precision (16-true, bf16-true).

    Raises:
        ValueError:
            If unsupported ``precision`` is provided.

    """

    def __init__(self, precision: _PRECISION_INPUT) -> None:
        if not _XLA_AVAILABLE:
            raise ModuleNotFoundError(str(_XLA_AVAILABLE))
        supported_precision = get_args(_PRECISION_INPUT)
        if precision not in supported_precision:
            raise ValueError(
                f"`precision={precision!r})` is not supported in XLA."
                f" `precision` must be one of: {supported_precision}."
            )
        self.precision = precision

        if precision == "16-true":
            os.environ["XLA_USE_F16"] = "1"
            self._desired_dtype = torch.float16
        elif precision == "bf16-true":
            os.environ["XLA_USE_BF16"] = "1"
            self._desired_dtype = torch.bfloat16
        else:
            self._desired_dtype = torch.float32

    @override
    def optimizer_step(
        self,
        optimizer: Optimizable,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use an exact literal such as 'bf16-mixed' or '16-mixed'
  2. Inspect get_args from lightning.fabric.plugins.precision.xla to see the accepted values for your version
  3. Validate precision strings once at config-load time

Example fix

# before
precision = XLAPrecision("bf16")
# after
precision = XLAPrecision("bf16-mixed")
Defensive patterns

Strategy: validation

Validate before calling

from typing import get_args
from lightning.fabric.plugins.precision.xla import _PRECISION_INPUT
assert precision in get_args(_PRECISION_INPUT), f"bad precision: {precision!r}"

Type guard

from typing import get_args
from lightning.fabric.plugins.precision.xla import _PRECISION_INPUT

def is_valid_precision(p: object) -> bool:
    return isinstance(p, str) and p in get_args(_PRECISION_INPUT)

Try / catch

try:
    plugin = XLAPrecision(precision)
except ValueError:
    plugin = XLAPrecision("bf16-mixed")

Prevention

When it happens

Trigger: XLAPrecision('fp16'), XLAPrecision('bf16'), XLAPrecision(16), or any other value not in get_args(_PRECISION_INPUT).

Common situations: Reusing precision strings from older Lightning ('fp16'/'bf16') in a Fabric/XLA setup; passing a torch dtype instead of the string literal.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/dc53b9ab4a30f50c. Report an issue: GitHub.