{"record":{"id":"3f7aecf86d71afe3","repo":"Lightning-AI/pytorch-lightning","slug":"to-spawn-processes-with-the-type-self-strategy","errorCode":null,"errorMessage":"To spawn processes with the `{type(self.strategy).__name__}` strategy, `.launch()` needs to be called with a function that contains the code to launch in processes.","messagePattern":"To spawn processes with the `(.+?)` strategy, `\\.launch\\(\\)` needs to be called with a function that contains the code to launch in processes\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":1006,"sourceCode":"        \"\"\"\n        if _is_using_cli():\n            raise RuntimeError(\n                \"This script was launched through the CLI, and processes have already been created. Calling \"\n                \" `.launch()` again is not allowed.\"\n            )\n        if function is not _do_nothing:\n            if not callable(function):\n                raise TypeError(\n                    f\"`Fabric.launch(...)` needs to be a callable, but got {function}.\"\n                    \" HINT: do `.launch(your_fn)` instead of `.launch(your_fn())`\"\n                )\n            if not inspect.signature(function).parameters:\n                raise TypeError(\n                    f\"`Fabric.launch(function={function})` needs to take at least one argument. The launcher will\"\n                    \" pass in the `Fabric` object so you can use it inside the function.\"\n                )\n        elif isinstance(self.strategy.launcher, (_MultiProcessingLauncher, _XLALauncher)):\n            raise TypeError(\n                f\"To spawn processes with the `{type(self.strategy).__name__}` strategy, `.launch()` needs to be called\"\n                \" with a function that contains the code to launch in processes.\"\n            )\n        return self._wrap_and_launch(function, self, *args, **kwargs)\n\n    def _filter_kwargs_for_callback(self, method: Callable, kwargs: dict[str, Any]) -> dict[str, Any]:\n        \"\"\"Filter keyword arguments to only include those that match the callback method's signature.\n\n        Args:\n            method: The callback method to inspect\n            kwargs: The keyword arguments to filter\n\n        Returns:\n            A filtered dictionary of keyword arguments that match the method's signature\n\n        \"\"\"\n        try:\n            sig = inspect.signature(method)","sourceCodeStart":988,"sourceCodeEnd":1024,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L988-L1024","documentation":"Strategies that spawn processes (via _MultiProcessingLauncher or _XLALauncher, e.g. ddp_spawn, XLA/TPU) need the code to run in child processes supplied as a function to .launch(). If function is _do_nothing (nothing passed) and the strategy uses a spawning launcher, Fabric cannot pickle/spawn anything, so it raises TypeError.","triggerScenarios":"Creating Fabric(strategy='ddp_spawn', devices=2) and never calling fabric.launch(fn) — e.g. going straight to fabric.setup(model), whose validation path requires the launch for spawn strategies.","commonSituations":"Switching a single-device script to ddp_spawn/XLA without adding .launch(); calling setup inside a helper that skips the launch branch.","solutions":["Wrap your training code in a function and call fabric.launch(train) first","Or switch to a non-spawning strategy: strategy='ddp' so setup works without a spawned function"],"exampleFix":"# before\nfabric = Fabric(strategy='ddp_spawn', devices=2)\nmodel = fabric.setup(model)\n# after\nfabric = Fabric(strategy='ddp_spawn', devices=2)\ndef train(fabric):\n    model = fabric.setup(model)\nfabric.launch(train)","handlingStrategy":"fallback","validationCode":"from lightning.fabric.plugins.collective import _MultiProcessingLauncher\nif fabric.strategy.launcher is not None and 'spawn' in type(fabric.strategy.launcher).__name__.lower():\n    raise SystemExit('spawn strategies require .launch(fn) with your training code')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["For spawn-based strategies (ddp_spawn, xla), always structure code as a launched function","Prefer non-spawn strategies (ddp) for simpler setups"],"tags":["lightning","fabric","ddp-spawn","launch","xla"],"backgroundTag":"spawn-without-entry-function","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}