{"record":{"id":"e064b91cb0ad5aee","repo":"Lightning-AI/pytorch-lightning","slug":"model-is-expected-to-be-a-compiled-lightningmodu","errorCode":null,"errorMessage":"`model` is expected to be a compiled LightningModule. Found a `{type(orig_module).__name__}` instead","messagePattern":"`model` is expected to be a compiled LightningModule\\. Found a `(.+?)` instead","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/compile.py","lineNumber":43,"sourceCode":"    \"\"\"Returns an instance LightningModule from the output of ``torch.compile``.\n\n    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.\n\n    The ``torch.compile`` function returns a ``torch._dynamo.OptimizedModule``, which wraps the LightningModule\n    passed in as an argument, but doesn't inherit from it. This means that the output of ``torch.compile`` behaves\n    like a LightningModule, but it doesn't inherit from it (i.e. `isinstance` will fail).\n\n    Use this method to obtain a LightningModule that still runs with all the optimizations from ``torch.compile``.\n\n    \"\"\"\n    if not isinstance(model, OptimizedModule):\n        raise ValueError(f\"`model` is required to be a `OptimizedModule`. Found a `{type(model).__name__}` instead.\")\n\n    orig_module = model._orig_mod\n\n    if not isinstance(orig_module, pl.LightningModule):\n        _check_mixed_imports(model)\n        raise ValueError(\n            f\"`model` is expected to be a compiled LightningModule. Found a `{type(orig_module).__name__}` instead\"\n        )\n\n    orig_module._compiler_ctx = {\n        \"compiler\": \"dynamo\",\n        \"dynamo_ctx\": model.dynamo_ctx,\n        \"original_forward\": orig_module.forward,\n        \"original_training_step\": orig_module.training_step,\n        \"original_validation_step\": orig_module.validation_step,\n        \"original_test_step\": orig_module.test_step,\n        \"original_predict_step\": orig_module.predict_step,\n    }\n\n    orig_module.forward = model.dynamo_ctx(orig_module.forward)  # type: ignore[method-assign]\n    orig_module.training_step = model.dynamo_ctx(orig_module.training_step)  # type: ignore[method-assign]\n    orig_module.validation_step = model.dynamo_ctx(orig_module.validation_step)  # type: ignore[method-assign]\n    orig_module.test_step = model.dynamo_ctx(orig_module.test_step)  # type: ignore[method-assign]\n    orig_module.predict_step = model.dynamo_ctx(orig_module.predict_step)  # type: ignore[method-assign]","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/compile.py#L25-L61","documentation":"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).","triggerScenarios":"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).","commonSituations":"Mixing `lightning.pytorch` and `pytorch_lightning` imports in one project; compiling helper nn.Modules and passing them to Lightning.","solutions":["Ensure the object passed to torch.compile is a LightningModule from the same namespace (lightning.pytorch) you run the Trainer with","Audit imports: grep for `pytorch_lightning` and unify to `lightning.pytorch`","Compile only the LightningModule that the Trainer consumes"],"exampleFix":"# before\nfrom pytorch_lightning import LightningModule\nclass M(LightningModule): ...\nunwrapped = _module_to_compiled.from_compiled(torch.compile(M()))\n# after\nfrom lightning.pytorch import LightningModule\nclass M(LightningModule): ...\nunwrapped = _module_to_compiled.from_compiled(torch.compile(M()))","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_compiled_lm(m) -> bool:\n    from torch._dynamo import OptimizedModule\n    import lightning.pytorch as pl\n    return isinstance(m, OptimizedModule) and isinstance(m._orig_mod, pl.LightningModule)","tryCatchPattern":null,"preventionTips":["Never mix lightning.pytorch and pytorch_lightning imports in one project","Compile only LightningModules intended for the Trainer"],"tags":["torch-compile","mixed-imports","type-mismatch"],"backgroundTag":"mixed-lightning-imports","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}