# mypy: allow-untyped-defsr"""This package enables an interface for accessing MPS (Metal Performance Shaders) backend in Python.Metal is Apple's API for programming metal GPU (graphics processor unit). Using MPS means that increasedperformance can be achieved, by running work on the metal GPU(s).See https://developer.apple.com/documentation/metalperformanceshaders for more details."""fromtypingimportUnionimporttorchfromtorchimportTensor_is_in_bad_fork=getattr(torch._C,"_mps_is_in_bad_fork",lambda:False)_default_mps_generator:torch._C.Generator=None# type: ignore[assignment]# local helper function (not public or exported)def_get_default_mps_generator()->torch._C.Generator:global_default_mps_generatorif_default_mps_generatorisNone:_default_mps_generator=torch._C._mps_get_default_generator()return_default_mps_generator
[docs]defdevice_count()->int:r"""Returns the number of available MPS devices."""returnint(torch._C._has_mpsandtorch._C._mps_is_available())
[docs]defsynchronize()->None:r"""Waits for all kernels in all streams on a MPS device to complete."""returntorch._C._mps_deviceSynchronize()
[docs]defget_rng_state(device:Union[int,str,torch.device]="mps")->Tensor:r"""Returns the random number generator state as a ByteTensor. Args: device (torch.device or int, optional): The device to return the RNG state of. Default: ``'mps'`` (i.e., ``torch.device('mps')``, the current MPS device). """return_get_default_mps_generator().get_state()
[docs]defset_rng_state(new_state:Tensor,device:Union[int,str,torch.device]="mps")->None:r"""Sets the random number generator state. Args: new_state (torch.ByteTensor): The desired state device (torch.device or int, optional): The device to set the RNG state. Default: ``'mps'`` (i.e., ``torch.device('mps')``, the current MPS device). """new_state_copy=new_state.clone(memory_format=torch.contiguous_format)_get_default_mps_generator().set_state(new_state_copy)
[docs]defmanual_seed(seed:int)->None:r"""Sets the seed for generating random numbers. Args: seed (int): The desired seed. """# the torch.mps.manual_seed() can be called from the global# torch.manual_seed() in torch/random.py. So we need to make# sure mps is available (otherwise we just return without# erroring out)ifnottorch._C._has_mps:returnseed=int(seed)_get_default_mps_generator().manual_seed(seed)
[docs]defseed()->None:r"""Sets the seed for generating random numbers to a random number."""_get_default_mps_generator().seed()
[docs]defempty_cache()->None:r"""Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU applications. """torch._C._mps_emptyCache()
[docs]defset_per_process_memory_fraction(fraction)->None:r"""Set memory fraction for limiting process's memory allocation on MPS device. The allowed value equals the fraction multiplied by recommended maximum device memory (obtained from Metal API device.recommendedMaxWorkingSetSize). If trying to allocate more than the allowed value in a process, it will raise an out of memory error in allocator. Args: fraction(float): Range: 0~2. Allowed memory equals total_memory * fraction. .. note:: Passing 0 to fraction means unlimited allocations (may cause system failure if out of memory). Passing fraction greater than 1.0 allows limits beyond the value returned from device.recommendedMaxWorkingSetSize. """ifnotisinstance(fraction,float):raiseTypeError("Invalid type for fraction argument, must be `float`")iffraction<0orfraction>2:raiseValueError(f"Invalid fraction value: {fraction}. Allowed range: 0~2")torch._C._mps_setMemoryFraction(fraction)
[docs]defcurrent_allocated_memory()->int:r"""Returns the current GPU memory occupied by tensors in bytes. .. note:: The returned size does not include cached allocations in memory pools of MPSAllocator. """returntorch._C._mps_currentAllocatedMemory()
[docs]defdriver_allocated_memory()->int:r"""Returns total GPU memory allocated by Metal driver for the process in bytes. .. note:: The returned size includes cached allocations in MPSAllocator pools as well as allocations from MPS/MPSGraph frameworks. """returntorch._C._mps_driverAllocatedMemory()
[docs]defrecommended_max_memory()->int:r"""Returns recommended max Working set size for GPU memory in bytes. .. note:: Recommended max working set size for Metal. returned from device.recommendedMaxWorkingSetSize. """returntorch._C._mps_recommendedMaxMemory()
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. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.