{"record":{"id":"b90f5e1ed36e01ed","repo":"Lightning-AI/pytorch-lightning","slug":"using-a-compiled-model-is-incompatible-with-the-cu","errorCode":null,"errorMessage":"Using a compiled model is incompatible with the current strategy: `{type(strategy).__name__}`. Only {supported_strategy_names} support compilation. Either switch to one of the supported strategies or avoid passing in compiled model.","messagePattern":"Using a compiled model is incompatible with the current strategy: `(.+?)`\\. Only (.+?) support compilation\\. Either switch to one of the supported strategies or avoid passing in compiled model\\.","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/compile.py","lineNumber":121,"sourceCode":"\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\n    _check_mixed_imports(model)\n    raise TypeError(\n        f\"`model` must be a `LightningModule` or `torch._dynamo.OptimizedModule`, got `{type(model).__qualname__}`\"\n    )\n\n\ndef _verify_strategy_supports_compile(model: \"pl.LightningModule\", strategy: Strategy) -> None:\n    if model._compiler_ctx is not None:\n        supported_strategies = (SingleDeviceStrategy, DDPStrategy, FSDPStrategy)\n        if not isinstance(strategy, supported_strategies) or isinstance(strategy, DeepSpeedStrategy):\n            supported_strategy_names = \", \".join(s.__name__ for s in supported_strategies)\n            raise RuntimeError(\n                f\"Using a compiled model is incompatible with the current strategy: `{type(strategy).__name__}`.\"\n                f\" Only {supported_strategy_names} support compilation. Either switch to one of the supported\"\n                \" strategies or avoid passing in compiled model.\"\n            )\n","sourceCodeStart":103,"sourceCodeEnd":126,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/compile.py#L103-L126","documentation":"PyTorch Lightning raises this RuntimeError in _verify_strategy_supports_compile when you pass an already-compiled model (torch.compile) to a Trainer whose strategy is not one of SingleDeviceStrategy, DDPStrategy, or FSDPStrategy. Compiled models rely on lazy tracing and initialization hooks that these strategies implement; other strategies (e.g. DeepSpeed, DeepSpeedStrategy subclasses, or custom/parallel strategies) do not support them. The check fires on fit/validate/test/predict entry before training starts.","triggerScenarios":"Calling Trainer(strategy='deepspeed', ...).fit(torch.compile(model)) or any compiled model with a strategy outside (SingleDeviceStrategy, DDPStrategy, FSDPStrategy), including DeepSpeedStrategy subclasses which are explicitly rejected even though DDPStrategy is otherwise supported.","commonSituations":"Mixing torch.compile with DeepSpeed ZeRO; using custom parallel strategies with a compiled LightningModule; upgrading Lightning where compiled-model support was added but only for a subset of strategies.","solutions":["Switch to a supported strategy: Trainer(strategy='ddp'|'ddp_spawn'|'fsdp'|'single_device') while keeping the compiled model","Or stop compiling the model yourself and let Lightning handle it via Trainer(compile=True) with a supported strategy","Or keep DeepSpeedStrategy/custom strategy and pass the uncompiled LightningModule"],"exampleFix":"// before\nmodel = torch.compile(MyLightningModule())\ntrainer = Trainer(strategy='deepspeed')\ntrainer.fit(model)\n\n// after\ntrainer = Trainer(strategy='fsdp')  // or pass the uncompiled model\ntrainer.fit(model)","handlingStrategy":"validation","validationCode":"from lightning.pytorch.strategies import SingleDeviceStrategy, DDPStrategy, FSDPStrategy, DeepSpeedStrategy\nSUPPORTED = (SingleDeviceStrategy, DDPStrategy, FSDPStrategy)\nif model._compiler_ctx is not None and (not isinstance(trainer.strategy, SUPPORTED) or isinstance(trainer.strategy, DeepSpeedStrategy)):\n    raise SystemExit('switch strategy or uncompile model')","typeGuard":"def strategy_supports_compile(strategy) -> bool:\n    from lightning.pytorch.strategies import SingleDeviceStrategy, DDPStrategy, FSDPStrategy, DeepSpeedStrategy\n    supported = (SingleDeviceStrategy, DDPStrategy, FSDPStrategy)\n    return isinstance(strategy, supported) and not isinstance(strategy, DeepSpeedStrategy)","tryCatchPattern":null,"preventionTips":["Only compile models when using single-device, DDP, or FSDP strategies","Prefer Trainer(compile=True) over manual torch.compile so Lightning manages compatibility","Add a smoke test that runs one fit step with your exact strategy+compile combination in CI"],"tags":["pytorch-lightning","torch-compile","strategy","distributed-training"],"backgroundTag":"torch-compile-strategy-incompatible","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}