{"record":{"id":"87dc5ad37e85ba4e","repo":"Lightning-AI/pytorch-lightning","slug":"failed-to-determine-the-arguments-that-were-used-t","errorCode":null,"errorMessage":"Failed to determine the arguments that were used to compile the module. Make sure to import lightning before `torch.compile` is used.","messagePattern":"Failed to determine the arguments that were used to compile the module\\. Make sure to import lightning before `torch\\.compile` is used\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/wrappers.py","lineNumber":356,"sourceCode":"        if isinstance(obj, _FabricDataLoader):\n            return obj._dataloader\n        return obj\n\n    types = [_FabricModule, _FabricOptimizer, _FabricDataLoader]\n    types.append(OptimizedModule)\n\n    return apply_to_collection(collection, dtype=tuple(types), function=_unwrap)\n\n\ndef _unwrap_compiled(obj: Union[Any, OptimizedModule]) -> tuple[Union[Any, nn.Module], Optional[dict[str, Any]]]:\n    \"\"\"Removes the :class:`torch._dynamo.OptimizedModule` around the object if it is wrapped.\n\n    Use this function before instance checks against e.g. :class:`_FabricModule`.\n\n    \"\"\"\n    if isinstance(obj, OptimizedModule):\n        if (compile_kwargs := getattr(obj, \"_compile_kwargs\", None)) is None:\n            raise RuntimeError(\n                \"Failed to determine the arguments that were used to compile the module. Make sure to import\"\n                \" lightning before `torch.compile` is used.\"\n            )\n        return obj._orig_mod, compile_kwargs\n    return obj, None\n\n\ndef _to_compiled(module: nn.Module, compile_kwargs: dict[str, Any]) -> OptimizedModule:\n    return torch.compile(module, **compile_kwargs)  # type: ignore[return-value]\n\n\ndef _backward_hook(requires_backward: bool, *_: Any) -> None:\n    if requires_backward and not _in_fabric_backward:\n        raise RuntimeError(\n            \"The current strategy and precision selection requires you to call `fabric.backward(loss)`\"\n            \" instead of `loss.backward()`.\"\n        )\n","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/wrappers.py#L338-L374","documentation":"_unwrap_compiled() detects a torch.compile'd module (OptimizedModule) and reads its _compile_kwargs to preserve the compile settings when re-wrapping. Lightning patches torch.compile to record these kwargs; if _compile_kwargs is missing it means torch.compile ran before lightning was imported, so the settings cannot be recovered and a RuntimeError is raised.","triggerScenarios":"Calling torch.compile(model) at module import time in a script that imports lightning later (or not at all before compile), then fabric.setup()/load/backward() which internally unwraps the OptimizedModule.","commonSituations":"A third-party library (e.g. transformers with torch_compile=True, or a utils module) compiles models at import time before `import lightning` executes; reordered imports after refactoring; worker processes that compile models without importing lightning first.","solutions":["Ensure `import lightning` (or `import lightning.fabric`) appears before any torch.compile call, including in transitively imported modules that compile at import time","For HF transformers, avoid torch_compile=True in from_pretrained unless lightning is imported first, and compile manually after setup instead","Compile the model after fabric.setup() rather than at import time"],"exampleFix":"# before\n# utils.py (imported first)\nmodel = torch.compile(model)  # lightning not imported yet\nimport lightning as L\n\n# after\nimport lightning as L  # first\nmodel = L.Fabric().setup(model)\nmodel = torch.compile(model)","handlingStrategy":"validation","validationCode":"import lightning  # must run before any torch.compile\nimport torch\nmodel = torch.compile(model)\nassert not isinstance(model, torch._dynamo.eval_frame.OptimizedModule) or getattr(model, '_compile_kwargs', None) is not None, 'compile ran before lightning import'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Put `import lightning` at the very top of the entrypoint, before third-party imports that may compile models","Compile after fabric.setup() instead of at import time","Disable torch_compile=True in from_pretrained and compile manually"],"tags":["pytorch-lightning","torch-compile","import-order"],"backgroundTag":"torch-compile-kwargs-missing","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}