{"record":{"id":"7e1536a0c9ee68e6","repo":"Lightning-AI/pytorch-lightning","slug":"when-using-the-type-self-name-you-are-requ","errorCode":null,"errorMessage":"When using the {type(self).__name__}, you are required to override the `configure_model()` hook in the LightningModule and apply parallelization there.","messagePattern":"When using the (.+?), you are required to override the `configure_model\\(\\)` hook in the LightningModule and apply parallelization there\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/strategies/model_parallel.py","lineNumber":170,"sourceCode":"        if self._tensor_parallel_size == \"auto\":\n            self._tensor_parallel_size = self.num_processes\n        self._device_mesh = _setup_device_mesh(\n            self._data_parallel_size, self._tensor_parallel_size, self.world_size, self.root_device\n        )\n        # Users can access device mesh in `LightningModule.configure_model()`\n        assert self.lightning_module is not None\n        self.lightning_module._device_mesh = self._device_mesh\n\n    @override\n    def setup(self, trainer: \"pl.Trainer\") -> None:\n        from torch.distributed.fsdp import FullyShardedDataParallel\n\n        assert self.model is not None\n        assert self.accelerator is not None\n        self.accelerator.setup(trainer)\n\n        if not is_overridden(\"configure_model\", self.lightning_module):\n            raise TypeError(\n                f\"When using the {type(self).__name__}, you are required to override the `configure_model()` hook in\"\n                f\" the LightningModule and apply parallelization there.\"\n            )\n        if any(isinstance(mod, FullyShardedDataParallel) for mod in self.model.modules()):\n            raise TypeError(\n                \"Found modules that are wrapped with `torch.distributed.fsdp.FullyShardedDataParallel`.\"\n                f\" The `{self.__class__.__name__}` only supports the new FSDP2 APIs in PyTorch >= 2.4.\"\n            )\n\n        _materialize_distributed_module(self.model, self.root_device)\n\n        self.model = self.precision_plugin.convert_module(self.model)\n        self.model_to_device()  # move all remaining layers if any left on CPU.\n\n        self.barrier()\n\n        if trainer.state.fn == TrainerFn.FITTING:\n            self.setup_optimizers(trainer)","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/strategies/model_parallel.py#L152-L188","documentation":"ModelParallelStrategy requires that model parallelization (e.g. FSDP2 fully_shard calls) happen inside LightningModule.configure_model(), which is invoked on each rank with the right device context. If that hook is not overridden, Lightning cannot apply parallelization and raises TypeError.","triggerScenarios":"Using ModelParallelStrategy (or subclasses like FSDP2Strategy/ModelParallelStrategy-based strategies) with a LightningModule that does not override configure_model; wrapping applied in __init__ or never applied at all.","commonSituations":"Migrating a single-device model to a model-parallel strategy; new users expecting automatic FSDP wrapping (as older FSDPStrategy auto-wrapped).","solutions":["Override configure_model() in your LightningModule and apply parallelization there (e.g. torch.distributed.fsdp.fully_shard on submodules)","Do the wrapping lazily there rather than in __init__","See the ModelParallel / FSDP2 examples in the Lightning repo/docs"],"exampleFix":"# before\nclass LitModel(L.LightningModule):\n    def __init__(self):\n        self.model = Transformer()  # no configure_model\n\n# after\nclass LitModel(L.LightningModule):\n    def configure_model(self):\n        self.model = Transformer()\n        for block in self.model.blocks:\n            fully_shard(block)\n        fully_shard(self.model)","handlingStrategy":"validation","validationCode":"from lightning.pytorch.utilities.model_helpers import is_overridden\nassert is_overridden(\"configure_model\", model), \"override configure_model() when using ModelParallelStrategy\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always override configure_model for model-parallel strategies and apply parallelization there","Never build/wrap the parallelized model in __init__"],"tags":["model-parallel","fsdp2","configure-model","hook-required"],"backgroundTag":"required-hook-not-overridden","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}