{"record":{"id":"f39cb82f9b6532ab","repo":"Lightning-AI/pytorch-lightning","slug":"model-is-required-to-be-a-optimizedmodule-fou","errorCode":null,"errorMessage":"`model` is required to be a `OptimizedModule`. Found a `{type(model).__name__}` instead.","messagePattern":"`model` is required to be a `OptimizedModule`\\. Found a `(.+?)` instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/compile.py","lineNumber":37,"sourceCode":"import lightning.pytorch as pl\nfrom lightning.pytorch.strategies import DDPStrategy, DeepSpeedStrategy, FSDPStrategy, SingleDeviceStrategy, Strategy\nfrom lightning.pytorch.utilities.model_helpers import _check_mixed_imports\n\n\ndef from_compiled(model: OptimizedModule) -> \"pl.LightningModule\":\n    \"\"\"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    }","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/compile.py#L19-L55","documentation":"_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.","triggerScenarios":"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.","commonSituations":"Conditionally compiling (if torch compile available) but always calling unwrap logic; passing the unwrapped module back in.","solutions":["Only call from_compiled on modules produced by torch.compile(model)","For LightningModules, pass the module directly; unwrap only OptimizedModule instances"],"exampleFix":"# before\ncompiled = torch.compile(model) if use_compile else model\nunwrapped = _module_to_compiled.from_compiled(model)\n# after\ncompiled = torch.compile(model) if use_compile else model\nunwrapped = _module_to_compiled.from_compiled(compiled) if use_compile else model","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from torch._dynamo import OptimizedModule\ndef is_compiled_module(m) -> bool:\n    return isinstance(m, OptimizedModule)","tryCatchPattern":null,"preventionTips":["Only unwrap objects returned by torch.compile"],"tags":["torch-compile","type-mismatch","unwrap"],"backgroundTag":"wrong-type-passed","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}