# Copyright The Lightning AI team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.fromtypingimportAny,Dict,List,Optional,UnionimporttorchfromtorchimportTensorfromtorch.nnimportDataParallel,Modulefromlightning_fabric.acceleratorsimportAcceleratorfromlightning_fabric.plugins.io.checkpoint_ioimportCheckpointIOfromlightning_fabric.plugins.precisionimportPrecisionfromlightning_fabric.strategies.parallelimportParallelStrategyfromlightning_fabric.strategies.strategyimportTBroadcast,TReducefromlightning_fabric.utilities.apply_funcimportapply_to_collectionfromlightning_fabric.utilities.distributedimportReduceOp
[docs]classDataParallelStrategy(ParallelStrategy):"""Implements data-parallel training in a single process, i.e., the model gets replicated to each device and each gets a split of the data."""def__init__(self,accelerator:Optional[Accelerator]=None,parallel_devices:Optional[List[torch.device]]=None,checkpoint_io:Optional[CheckpointIO]=None,precision:Optional[Precision]=None,):super().__init__(accelerator=accelerator,parallel_devices=parallel_devices,cluster_environment=None,checkpoint_io=checkpoint_io,precision=precision,)@propertydefroot_device(self)->torch.device:assertself.parallel_devicesisnotNonereturnself.parallel_devices[0]@propertydefdistributed_sampler_kwargs(self)->None:returnNone
[docs]defsetup_module(self,module:Module)->DataParallel:"""Wraps the given model into a :class:`~torch.nn.parallel.DataParallel` module."""returnDataParallel(module=module,device_ids=self.parallel_devices)
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. Read PyTorch Lightning's Privacy Policy.