Source code for lightning.pytorch.strategies.single_xla
# 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.importosfromtypingimportOptional,Unionimporttorchfromtyping_extensionsimportoverrideimportlightning.pytorchasplfromlightning.fabric.accelerators.xlaimport_XLA_AVAILABLEfromlightning.fabric.pluginsimportXLACheckpointIOfromlightning.fabric.strategiesimport_StrategyRegistryfromlightning.fabric.utilities.optimizerimport_optimizers_to_devicefromlightning.fabric.utilities.typesimport_DEVICEfromlightning.pytorch.plugins.io.wrapperimport_WrappingCheckpointIOfromlightning.pytorch.plugins.precision.xlaimportXLAPrecisionfromlightning.pytorch.strategies.single_deviceimportSingleDeviceStrategyfromlightning.pytorch.trainer.statesimportTrainerFnfromlightning.pytorch.utilitiesimportfind_shared_parameters,set_shared_parameters
[docs]classSingleDeviceXLAStrategy(SingleDeviceStrategy):"""Strategy for training on a single XLA device."""def__init__(self,device:_DEVICE,accelerator:Optional["pl.accelerators.Accelerator"]=None,checkpoint_io:Optional[Union[XLACheckpointIO,_WrappingCheckpointIO]]=None,precision_plugin:Optional[XLAPrecision]=None,debug:bool=False,):ifnot_XLA_AVAILABLE:raiseModuleNotFoundError(str(_XLA_AVAILABLE))ifisinstance(device,torch.device):# unwrap the `torch.device` in favor of `xla_device`device=device.indeximporttorch_xla.core.xla_modelasxmsuper().__init__(accelerator=accelerator,device=xm.xla_device(device),checkpoint_io=checkpoint_io,precision_plugin=precision_plugin,)self.debug=debug@property@overridedefcheckpoint_io(self)->Union[XLACheckpointIO,_WrappingCheckpointIO]:plugin=self._checkpoint_ioifpluginisnotNone:assertisinstance(plugin,(XLACheckpointIO,_WrappingCheckpointIO))returnpluginreturnXLACheckpointIO()@checkpoint_io.setter@overridedefcheckpoint_io(self,io:Optional[Union[XLACheckpointIO,_WrappingCheckpointIO]])->None:ifioisnotNoneandnotisinstance(io,(XLACheckpointIO,_WrappingCheckpointIO)):raiseTypeError(f"The XLA strategy can only work with the `XLACheckpointIO` plugin, found {io}")self._checkpoint_io=io@property@overridedefprecision_plugin(self)->XLAPrecision:plugin=self._precision_pluginifpluginisnotNone:assertisinstance(plugin,XLAPrecision)returnpluginreturnXLAPrecision()@precision_plugin.setter@overridedefprecision_plugin(self,precision_plugin:Optional[XLAPrecision])->None:ifprecision_pluginisnotNoneandnotisinstance(precision_plugin,XLAPrecision):raiseTypeError(f"The XLA strategy can only work with the `XLAPrecision` plugin, found {precision_plugin}")self._precision_plugin=precision_plugin
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.
You are viewing an outdated version of PyTorch Lightning Docs