Lightning-AI/pytorch-lightning · error · ValueError
`model` is required to be a compiled LightningModule. Found
Error message
`model` is required to be a compiled LightningModule. Found a non-compiled LightningModule instead.
What it means
to_uncompiled also accepts a bare LightningModule with compilation metadata (_compiler_ctx set). If _compiler_ctx is None the module was never compiled and there is nothing to restore, so ValueError is raised.
Source
Thrown at src/lightning/pytorch/utilities/compile.py:85
.. warning:: This is an :ref:`experimental <versioning:Experimental API>` feature.
This takes either a ``torch._dynamo.OptimizedModule`` returned by ``torch.compile()`` or a ``LightningModule``
returned by ``from_compiled``.
Note: this method will in-place modify the ``LightningModule`` that is passed in.
"""
if isinstance(model, OptimizedModule):
original = model._orig_mod
if not isinstance(original, pl.LightningModule):
raise TypeError(
f"Unexpected error, the wrapped model should be a LightningModule, found {type(model).__name__}"
)
elif isinstance(model, pl.LightningModule):
if model._compiler_ctx is None:
raise ValueError(
"`model` is required to be a compiled LightningModule. Found a non-compiled LightningModule instead."
)
original = model
else:
raise ValueError("`model` must either be an instance of OptimizedModule or LightningModule")
ctx = original._compiler_ctx
if ctx is not None:
original.forward = ctx["original_forward"] # type: ignore[method-assign]
original.training_step = ctx["original_training_step"] # type: ignore[method-assign]
original.validation_step = ctx["original_validation_step"] # type: ignore[method-assign]
original.test_step = ctx["original_test_step"] # type: ignore[method-assign]
original.predict_step = ctx["original_predict_step"] # type: ignore[method-assign]
original._compiler_ctx = None
return original
View on GitHub (pinned to 9fed5c27d2)
Solutions
- Guard the call: only uncompile when model._compiler_ctx is not None or the model is an OptimizedModule
- Use from_compiled for OptimizedModule inputs
Example fix
# before
uncompiled = _module_to_compiled.to_uncompiled(model) # model never compiled
# after
if isinstance(model, OptimizedModule) or getattr(model, "_compiler_ctx", None):
model = _module_to_compiled.to_uncompiled(model) Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(model, LightningModule) and getattr(model, "_compiler_ctx", None) is None:
pass # already uncompiled; nothing to do Type guard
def needs_unwrap(m) -> bool:
from torch._dynamo import OptimizedModule
return isinstance(m, OptimizedModule) or getattr(m, "_compiler_ctx", None) is not None Prevention
- Gate uncompile calls behind a compile feature flag check
When it happens
Trigger: Calling _module_to_compiled.to_uncompiled(plain_lightning_module) on a module that was never passed through torch.compile.
Common situations: Unconditionally un-compiling in a workflow where compile is optional; feature-flag for compile disabled but unwrap still called.
Related errors
- Failed to determine the arguments that were used to compile
- `model` is required to be a `OptimizedModule`. Found a `{typ
- `model` is expected to be a compiled LightningModule. Found
- Unexpected error, the wrapped model should be a LightningMod
- `model` must either be an instance of OptimizedModule or Lig
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/3b0a11a50f2dd136.
Report an issue: GitHub.