{"record":{"id":"e2cadeef51a1e580","repo":"Lightning-AI/pytorch-lightning","slug":"model-must-either-be-an-instance-of-optimizedmod","errorCode":null,"errorMessage":"`model` must either be an instance of OptimizedModule or LightningModule","messagePattern":"`model` must either be an instance of OptimizedModule or LightningModule","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/compile.py","lineNumber":91,"sourceCode":"    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\n\ndef _maybe_unwrap_optimized(model: object) -> \"pl.LightningModule\":\n    if isinstance(model, OptimizedModule):\n        return from_compiled(model)\n    if isinstance(model, pl.LightningModule):\n        return model","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/compile.py#L73-L109","documentation":"to_uncompiled only accepts torch._dynamo.OptimizedModule or LightningModule instances; any other type (plain nn.Module, numpy object, etc.) raises ValueError.","triggerScenarios":"Passing an nn.Sequential, a raw tensor-wrapping object, or arbitrary class to _module_to_compiled.to_uncompiled.","commonSituations":"Sending helper modules or strategies' wrapped models that are neither type.","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"],"exampleFix":"# before\nunwrapped = _module_to_compiled.to_uncompiled(my_sequential)\n# after\nunwrapped = _module_to_compiled.to_uncompiled(model)  # a LightningModule or OptimizedModule","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_uncompilable(m) -> bool:\n    from torch._dynamo import OptimizedModule\n    import lightning.pytorch as pl\n    return isinstance(m, (OptimizedModule, pl.LightningModule))","tryCatchPattern":null,"preventionTips":["Always pass the exact model object you constructed for the Trainer"],"tags":["torch-compile","type-mismatch"],"backgroundTag":"wrong-type-passed","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}