U
    9%e                     @   s  U d Z ddlZddlmZ eejddd Zdaejje	d< ejjd	d
dZ
dd	ddZed	ddZeddddZeddddZdd	ddZdd	ddZdd	ddZed	ddZed	ddZd d!lmZ d d"lmZ dddddddddd#d$g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.
    N   )TensorZ_mps_is_in_bad_forkc                   C   s   dS )NF r   r   r   Q/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/mps/__init__.py<lambda>
       r   _default_mps_generator)returnc                   C   s   t d krtj a t S )N)r   torch_CZ_mps_get_default_generatorr   r   r   r   _get_default_mps_generator   s    
r   c                   C   s
   t j S )zAWaits for all kernels in all streams on a MPS device to complete.)r
   r   Z_mps_deviceSynchronizer   r   r   r   synchronize   s    r   c                   C   s
   t   S )z:Returns the random number generator state as a ByteTensor.)r   Z	get_stater   r   r   r   get_rng_state   s    r   )	new_stater	   c                 C   s   | j tjd}t | dS )zoSets the random number generator state.

    Args:
        new_state (torch.ByteTensor): The desired state
    )Zmemory_formatN)cloner
   Zcontiguous_formatr   Z	set_state)r   Znew_state_copyr   r   r   set_rng_state    s    r   )seedr	   c                 C   s$   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   Z_has_mpsintr   manual_seed)r   r   r   r   r   *   s    
r   c                   C   s   t    dS )z?Sets the seed for generating random numbers to a random number.N)r   r   r   r   r   r   r   :   s    r   c                   C   s   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   Z_mps_emptyCacher   r   r   r   empty_cache?   s    r   c                 C   sB   t | tstd| dk s"| dkr2t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   r   zInvalid fraction value: z. Allowed range: 0~2N)
isinstancefloat	TypeError
ValueErrorr
   r   Z_mps_setMemoryFraction)fractionr   r   r   set_per_process_memory_fractionF   s
    
r   c                   C   s
   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   Z_mps_currentAllocatedMemoryr   r   r   r   current_allocated_memory_   s    r   c                   C   s
   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   Z_mps_driverAllocatedMemoryr   r   r   r   driver_allocated_memoryi   s    r      )profiler)Eventr    r   )__doc__r
    r   getattrr   Z_is_in_bad_forkr   	Generator__annotations__r   r   r   r   r   r   r   r   r   r   r   r   eventr    __all__r   r   r   r   <module>   s8   


