Lightning-AI/pytorch-lightning · error · ValueError
`model` must either be an instance of OptimizedModule or Lig
Error message
`model` must either be an instance of OptimizedModule or LightningModule
What it means
to_uncompiled only accepts torch._dynamo.OptimizedModule or LightningModule instances; any other type (plain nn.Module, numpy object, etc.) raises ValueError.
Source
Thrown at src/lightning/pytorch/utilities/compile.py:91
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
def _maybe_unwrap_optimized(model: object) -> "pl.LightningModule":
if isinstance(model, OptimizedModule):
return from_compiled(model)
if isinstance(model, pl.LightningModule):
return modelView on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass the LightningModule you gave the Trainer
- If you have an OptimizedModule from torch.compile, pass that instead
- Add an isinstance check before calling
Example fix
# before unwrapped = _module_to_compiled.to_uncompiled(my_sequential) # after unwrapped = _module_to_compiled.to_uncompiled(model) # a LightningModule or OptimizedModule
Defensive patterns
Strategy: type-guard
Type guard
def is_uncompilable(m) -> bool:
from torch._dynamo import OptimizedModule
import lightning.pytorch as pl
return isinstance(m, (OptimizedModule, pl.LightningModule)) Prevention
- Always pass the exact model object you constructed for the Trainer
When it happens
Trigger: Passing an nn.Sequential, a raw tensor-wrapping object, or arbitrary class to _module_to_compiled.to_uncompiled.
Common situations: Sending helper modules or strategies' wrapped models that are neither type.
Related errors
- `model` is required to be a `OptimizedModule`. Found a `{typ
- `model` is expected to be a compiled LightningModule. Found
- `model` must be a `LightningModule` or `torch._dynamo.Optimi
- `devices` selected with `CPUAccelerator` should be an int >
- Blocking backward sync is only possible if the module passed
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/e2cadeef51a1e580.
Report an issue: GitHub.