# 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.importplatformfromfunctoolsimportlru_cachefromtypingimportDict,List,Optional,Unionimporttorchfromlightning_fabric.accelerators.acceleratorimportAcceleratorfromlightning_fabric.utilities.importsimport_TORCH_GREATER_EQUAL_1_12
[docs]classMPSAccelerator(Accelerator):"""Accelerator for Metal Apple Silicon GPU devices."""
[docs]defsetup_device(self,device:torch.device)->None:""" Raises: ValueError: If the selected device is not MPS. """ifdevice.type!="mps":raiseValueError(f"Device should be MPS, got {device} instead.")
[docs]@staticmethoddefget_parallel_devices(devices:Union[int,str,List[int]])->List[torch.device]:"""Gets parallel devices for the Accelerator."""parsed_devices=MPSAccelerator.parse_devices(devices)assertparsed_devicesisnotNonereturn[torch.device("mps",i)foriinrange(len(parsed_devices))]
[docs]@staticmethoddefauto_device_count()->int:"""Get the devices when set to auto."""return1
[docs]@staticmethod@lru_cache(1)defis_available()->bool:"""MPS is only available for certain torch builds starting at torch>=1.12, and is only enabled on a machine with the ARM-based Apple Silicon processors."""return(_TORCH_GREATER_EQUAL_1_12andtorch.backends.mps.is_available()andplatform.processor()in("arm","arm64"))
def_get_all_available_mps_gpus()->List[int]:""" Returns: A list of all available MPS GPUs """return[0]ifMPSAccelerator.is_available()else[]
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.