Lightning-AI/pytorch-lightning · error · ValueError
`model` is required to be a `OptimizedModule`. Found a `{typ
Error message
`model` is required to be a `OptimizedModule`. Found a `{type(model).__name__}` instead. What it means
_module_to_compiled.from_compiled expects a torch._dynamo.OptimizedModule (the return type of torch.compile(model)). Passing anything else raises ValueError with the found type name.
Source
Thrown at src/lightning/pytorch/utilities/compile.py:37
import lightning.pytorch as pl
from lightning.pytorch.strategies import DDPStrategy, DeepSpeedStrategy, FSDPStrategy, SingleDeviceStrategy, Strategy
from lightning.pytorch.utilities.model_helpers import _check_mixed_imports
def from_compiled(model: OptimizedModule) -> "pl.LightningModule":
"""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,
}View on GitHub (pinned to 9fed5c27d2)
Solutions
- Only call from_compiled on modules produced by torch.compile(model)
- For LightningModules, pass the module directly; unwrap only OptimizedModule instances
Example fix
# before compiled = torch.compile(model) if use_compile else model unwrapped = _module_to_compiled.from_compiled(model) # after compiled = torch.compile(model) if use_compile else model unwrapped = _module_to_compiled.from_compiled(compiled) if use_compile else model
Defensive patterns
Strategy: type-guard
Type guard
from torch._dynamo import OptimizedModule
def is_compiled_module(m) -> bool:
return isinstance(m, OptimizedModule) Prevention
- Only unwrap objects returned by torch.compile
When it happens
Trigger: Calling _module_to_compiled.from_compiled(model) on a plain LightningModule or arbitrary object; indirectly via trainer fit/validate when unwrapping a model that was never compiled.
Common situations: Conditionally compiling (if torch compile available) but always calling unwrap logic; passing the unwrapped module back in.
Related errors
- `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
- `model` must be a `LightningModule` or `torch._dynamo.Optimi
- `devices` selected with `CPUAccelerator` should be an int >
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/f39cb82f9b6532ab.
Report an issue: GitHub.