Lightning-AI/pytorch-lightning · error · ValueError

`model` is expected to be a compiled LightningModule. Found

Error message

`model` is expected to be a compiled LightningModule. Found a `{type(orig_module).__name__}` instead

What it means

from_compiled unwraps OptimizedModule._orig_mod and requires it to be a LightningModule. If you compiled a plain nn.Module (or non-Lightning model), the inner module type check fails and ValueError is raised (after a mixed-imports check).

Source

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

    """Returns an instance LightningModule from the output of ``torch.compile``.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    The ``torch.compile`` function returns a ``torch._dynamo.OptimizedModule``, which wraps the LightningModule
    passed in as an argument, but doesn't inherit from it. This means that the output of ``torch.compile`` behaves
    like a LightningModule, but it doesn't inherit from it (i.e. `isinstance` will fail).

    Use this method to obtain a LightningModule that still runs with all the optimizations from ``torch.compile``.

    """
    if not isinstance(model, OptimizedModule):
        raise ValueError(f"`model` is required to be a `OptimizedModule`. Found a `{type(model).__name__}` instead.")

    orig_module = model._orig_mod

    if not isinstance(orig_module, pl.LightningModule):
        _check_mixed_imports(model)
        raise ValueError(
            f"`model` is expected to be a compiled LightningModule. Found a `{type(orig_module).__name__}` instead"
        )

    orig_module._compiler_ctx = {
        "compiler": "dynamo",
        "dynamo_ctx": model.dynamo_ctx,
        "original_forward": orig_module.forward,
        "original_training_step": orig_module.training_step,
        "original_validation_step": orig_module.validation_step,
        "original_test_step": orig_module.test_step,
        "original_predict_step": orig_module.predict_step,
    }

    orig_module.forward = model.dynamo_ctx(orig_module.forward)  # type: ignore[method-assign]
    orig_module.training_step = model.dynamo_ctx(orig_module.training_step)  # type: ignore[method-assign]
    orig_module.validation_step = model.dynamo_ctx(orig_module.validation_step)  # type: ignore[method-assign]
    orig_module.test_step = model.dynamo_ctx(orig_module.test_step)  # type: ignore[method-assign]
    orig_module.predict_step = model.dynamo_ctx(orig_module.predict_step)  # type: ignore[method-assign]

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Ensure the object passed to torch.compile is a LightningModule from the same namespace (lightning.pytorch) you run the Trainer with
  2. Audit imports: grep for `pytorch_lightning` and unify to `lightning.pytorch`
  3. Compile only the LightningModule that the Trainer consumes

Example fix

# before
from pytorch_lightning import LightningModule
class M(LightningModule): ...
unwrapped = _module_to_compiled.from_compiled(torch.compile(M()))
# after
from lightning.pytorch import LightningModule
class M(LightningModule): ...
unwrapped = _module_to_compiled.from_compiled(torch.compile(M()))
Defensive patterns

Strategy: type-guard

Type guard

def is_compiled_lm(m) -> bool:
    from torch._dynamo import OptimizedModule
    import lightning.pytorch as pl
    return isinstance(m, OptimizedModule) and isinstance(m._orig_mod, pl.LightningModule)

Prevention

When it happens

Trigger: torch.compile(some_nn_module) then from_compiled(compiled); or a LightningModule imported from lightning.pytorch vs the old pytorch_lightning package, causing isinstance to fail (mixed imports).

Common situations: Mixing `lightning.pytorch` and `pytorch_lightning` imports in one project; compiling helper nn.Modules and passing them to Lightning.

Related errors


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