Lightning-AI/pytorch-lightning · error · RuntimeError

Using a compiled model is incompatible with the current stra

Error message

Using a compiled model is incompatible with the current strategy: `{type(strategy).__name__}`. Only {supported_strategy_names} support compilation. Either switch to one of the supported strategies or avoid passing in compiled model.

What it means

PyTorch Lightning raises this RuntimeError in _verify_strategy_supports_compile when you pass an already-compiled model (torch.compile) to a Trainer whose strategy is not one of SingleDeviceStrategy, DDPStrategy, or FSDPStrategy. Compiled models rely on lazy tracing and initialization hooks that these strategies implement; other strategies (e.g. DeepSpeed, DeepSpeedStrategy subclasses, or custom/parallel strategies) do not support them. The check fires on fit/validate/test/predict entry before training starts.

Source

Thrown at src/lightning/pytorch/utilities/compile.py:121


def _maybe_unwrap_optimized(model: object) -> "pl.LightningModule":
    if isinstance(model, OptimizedModule):
        return from_compiled(model)
    if isinstance(model, pl.LightningModule):
        return model
    _check_mixed_imports(model)
    raise TypeError(
        f"`model` must be a `LightningModule` or `torch._dynamo.OptimizedModule`, got `{type(model).__qualname__}`"
    )


def _verify_strategy_supports_compile(model: "pl.LightningModule", strategy: Strategy) -> None:
    if model._compiler_ctx is not None:
        supported_strategies = (SingleDeviceStrategy, DDPStrategy, FSDPStrategy)
        if not isinstance(strategy, supported_strategies) or isinstance(strategy, DeepSpeedStrategy):
            supported_strategy_names = ", ".join(s.__name__ for s in supported_strategies)
            raise RuntimeError(
                f"Using a compiled model is incompatible with the current strategy: `{type(strategy).__name__}`."
                f" Only {supported_strategy_names} support compilation. Either switch to one of the supported"
                " strategies or avoid passing in compiled model."
            )

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Switch to a supported strategy: Trainer(strategy='ddp'|'ddp_spawn'|'fsdp'|'single_device') while keeping the compiled model
  2. Or stop compiling the model yourself and let Lightning handle it via Trainer(compile=True) with a supported strategy
  3. Or keep DeepSpeedStrategy/custom strategy and pass the uncompiled LightningModule

Example fix

// before
model = torch.compile(MyLightningModule())
trainer = Trainer(strategy='deepspeed')
trainer.fit(model)

// after
trainer = Trainer(strategy='fsdp')  // or pass the uncompiled model
trainer.fit(model)
Defensive patterns

Strategy: validation

Validate before calling

from lightning.pytorch.strategies import SingleDeviceStrategy, DDPStrategy, FSDPStrategy, DeepSpeedStrategy
SUPPORTED = (SingleDeviceStrategy, DDPStrategy, FSDPStrategy)
if model._compiler_ctx is not None and (not isinstance(trainer.strategy, SUPPORTED) or isinstance(trainer.strategy, DeepSpeedStrategy)):
    raise SystemExit('switch strategy or uncompile model')

Type guard

def strategy_supports_compile(strategy) -> bool:
    from lightning.pytorch.strategies import SingleDeviceStrategy, DDPStrategy, FSDPStrategy, DeepSpeedStrategy
    supported = (SingleDeviceStrategy, DDPStrategy, FSDPStrategy)
    return isinstance(strategy, supported) and not isinstance(strategy, DeepSpeedStrategy)

Prevention

When it happens

Trigger: Calling Trainer(strategy='deepspeed', ...).fit(torch.compile(model)) or any compiled model with a strategy outside (SingleDeviceStrategy, DDPStrategy, FSDPStrategy), including DeepSpeedStrategy subclasses which are explicitly rejected even though DDPStrategy is otherwise supported.

Common situations: Mixing torch.compile with DeepSpeed ZeRO; using custom parallel strategies with a compiled LightningModule; upgrading Lightning where compiled-model support was added but only for a subset of strategies.

Related errors


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