
    Χg                        U d Z ddlmZ ddlZddlmZ  eej        dd           Zdaej        j	        e
d<   dej        j	        fd	Zdefd
ZddZddeeeej        f         defdZ	 ddedeeeej        f         ddfdZdeddfdZddZddZddZdefdZdefdZdefdZdefdZddlmZ ddlmZ g dZ dS ) ag  
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 increased
performance can be achieved, by running work on the metal GPU(s).
See https://developer.apple.com/documentation/metalperformanceshaders for more details.
    )UnionN)Tensor_mps_is_in_bad_forkc                      dS )NF r       N/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/mps/__init__.py<lambda>r
      s    5 r   _default_mps_generatorreturnc                  Z    t           t          j                                        a t           S )N)r   torch_C_mps_get_default_generatorr   r   r	   _get_default_mps_generatorr      s"    %!&!D!D!F!F!!r   c                  z    t          t          j        j        ot          j                                                  S )z,Returns the number of available MPS devices.)intr   r   _has_mps_mps_is_availabler   r   r	   device_countr      s(    ux AUX%?%?%A%ABBBr   c                  >    t           j                                        S )zAWaits for all kernels in all streams on a MPS device to complete.)r   r   _mps_deviceSynchronizer   r   r	   synchronizer      s    8**,,,r   mpsdevicec                 B    t                                                      S )zReturns 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).
    )r   	get_state)r   s    r	   get_rng_stater   $   s     &''11333r   	new_statec                     |                      t          j                  }t                                          |           dS )a  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).
    )memory_formatN)cloner   contiguous_formatr   	set_state)r   r   new_state_copys      r	   set_rng_stater&   .   s:     __53J_KKN  **>:::::r   seedc                     t           j        j        sdS t          |           } t	                                          |            dS )zbSets the seed for generating random numbers.

    Args:
        seed (int): The desired seed.
    N)r   r   r   r   r   manual_seed)r'   s    r	   r)   r)   <   sA     8 t99D  ,,T22222r   c                  F    t                                                       dS )z?Sets the seed for generating random numbers to a random number.N)r   r'   r   r   r	   r'   r'   L   s       %%'''''r   c                  B    t           j                                         dS )zReleases all unoccupied cached memory currently held by the caching
    allocator so that those can be used in other GPU applications.
    N)r   r   _mps_emptyCacher   r   r	   empty_cacher-   Q   s     
Hr   c                     t          | t                    st          d          | dk     s| dk    rt          d|  d          t          j                            |            dS )a  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.
    z3Invalid type for fraction argument, must be `float`r      zInvalid fraction value: z. Allowed range: 0~2N)
isinstancefloat	TypeError
ValueErrorr   r   _mps_setMemoryFraction)fractions    r	   set_per_process_memory_fractionr6   X   sm    " h&& OMNNN!||x!||RHRRRSSS	H##H-----r   c                  >    t           j                                        S )zReturns the current GPU memory occupied by tensors in bytes.

    .. note::
       The returned size does not include cached allocations in
       memory pools of MPSAllocator.
    )r   r   _mps_currentAllocatedMemoryr   r   r	   current_allocated_memoryr9   q   s     8//111r   c                  >    t           j                                        S )zReturns 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.
    )r   r   _mps_driverAllocatedMemoryr   r   r	   driver_allocated_memoryr<   {   s     8..000r   c                  >    t           j                                        S )zReturns recommended max Working set size for GPU memory in bytes.

    .. note::
       Recommended max working set size for Metal.
       returned from device.recommendedMaxWorkingSetSize.
    )r   r   _mps_recommendedMaxMemoryr   r   r	   recommended_max_memoryr?      s     8--///r   c                  &    t                      dk    S )Nr   )r   r   r   r	   is_availablerA      s    >>Ar      )profiler)Event)r   r   r)   r'   r&   r   r-   r6   r9   r<   rD   rC   r?   rA   )r   N)r   )!__doc__typingr   r   r   getattrr   _is_in_bad_forkr   	Generator__annotations__r   r   r   r   strr   r   r&   r)   r'   r-   r6   r9   r<   r?   boolrA    rC   eventrD   __all__r   r   r	   <module>rP      sG                  '%($9==II-1 * 1 1 1"EH$6 " " " "Cc C C C C
- - - -
4 4%S%, 67 4F 4 4 4 4 @E; ;;$S#u|%;<;	; ; ; ;3c 3d 3 3 3 3 ( ( ( (
   . . . .22# 2 2 2 21 1 1 1 10 0 0 0 0d                  r   