{"record":{"id":"3b0a11a50f2dd136","repo":"Lightning-AI/pytorch-lightning","slug":"model-is-required-to-be-a-compiled-lightningmodu","errorCode":null,"errorMessage":"`model` is required to be a compiled LightningModule. Found a non-compiled LightningModule instead.","messagePattern":"`model` is required to be a compiled LightningModule\\. Found a non-compiled LightningModule instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/compile.py","lineNumber":85,"sourceCode":"\n    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.\n\n    This takes either a ``torch._dynamo.OptimizedModule`` returned by ``torch.compile()`` or a ``LightningModule``\n    returned by ``from_compiled``.\n\n    Note: this method will in-place modify the ``LightningModule`` that is passed in.\n\n    \"\"\"\n    if isinstance(model, OptimizedModule):\n        original = model._orig_mod\n        if not isinstance(original, pl.LightningModule):\n            raise TypeError(\n                f\"Unexpected error, the wrapped model should be a LightningModule, found {type(model).__name__}\"\n            )\n\n    elif isinstance(model, pl.LightningModule):\n        if model._compiler_ctx is None:\n            raise ValueError(\n                \"`model` is required to be a compiled LightningModule. Found a non-compiled LightningModule instead.\"\n            )\n        original = model\n\n    else:\n        raise ValueError(\"`model` must either be an instance of OptimizedModule or LightningModule\")\n\n    ctx = original._compiler_ctx\n    if ctx is not None:\n        original.forward = ctx[\"original_forward\"]  # type: ignore[method-assign]\n        original.training_step = ctx[\"original_training_step\"]  # type: ignore[method-assign]\n        original.validation_step = ctx[\"original_validation_step\"]  # type: ignore[method-assign]\n        original.test_step = ctx[\"original_test_step\"]  # type: ignore[method-assign]\n        original.predict_step = ctx[\"original_predict_step\"]  # type: ignore[method-assign]\n        original._compiler_ctx = None\n\n    return original\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/compile.py#L67-L103","documentation":"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.","triggerScenarios":"Calling _module_to_compiled.to_uncompiled(plain_lightning_module) on a module that was never passed through torch.compile.","commonSituations":"Unconditionally un-compiling in a workflow where compile is optional; feature-flag for compile disabled but unwrap still called.","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"],"exampleFix":"# before\nuncompiled = _module_to_compiled.to_uncompiled(model)  # model never compiled\n# after\nif isinstance(model, OptimizedModule) or getattr(model, \"_compiler_ctx\", None):\n    model = _module_to_compiled.to_uncompiled(model)","handlingStrategy":"type-guard","validationCode":"if isinstance(model, LightningModule) and getattr(model, \"_compiler_ctx\", None) is None:\n    pass  # already uncompiled; nothing to do","typeGuard":"def needs_unwrap(m) -> bool:\n    from torch._dynamo import OptimizedModule\n    return isinstance(m, OptimizedModule) or getattr(m, \"_compiler_ctx\", None) is not None","tryCatchPattern":null,"preventionTips":["Gate uncompile calls behind a compile feature flag check"],"tags":["torch-compile","state-check"],"backgroundTag":"operation-on-uncompiled-module","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}