Source code for lightning_fabric.strategies.single_tpu
# 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.fromtypingimportDict,Optionalfromlightning_fabric.acceleratorsimportAcceleratorfromlightning_fabric.plugins.io.checkpoint_ioimportCheckpointIOfromlightning_fabric.plugins.io.xlaimportXLACheckpointIOfromlightning_fabric.plugins.precisionimportPrecisionfromlightning_fabric.strategies.single_deviceimportSingleDeviceStrategy
[docs]classSingleTPUStrategy(SingleDeviceStrategy):"""Strategy for training on a single TPU device."""def__init__(self,device:int,accelerator:Optional[Accelerator]=None,checkpoint_io:Optional[CheckpointIO]=None,precision:Optional[Precision]=None,):importtorch_xla.core.xla_modelasxmsuper().__init__(accelerator=accelerator,device=xm.xla_device(device),checkpoint_io=checkpoint_io,precision=precision,)@propertydefcheckpoint_io(self)->CheckpointIO:ifself._checkpoint_ioisNone:self._checkpoint_io=XLACheckpointIO()returnself._checkpoint_io@checkpoint_io.setterdefcheckpoint_io(self,io:Optional[CheckpointIO])->None:self._checkpoint_io=io@classmethoddefregister_strategies(cls,strategy_registry:Dict)->None:strategy_registry.register("single_tpu",cls,description=f"{cls.__class__.__name__}")
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.