a
    d                     @   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g d ZdS )!zG
This package enables an interface for accessing MPS backend in python
    N   )TensorZ_mps_is_in_bad_forkc                   C   s   dS )NF r   r   r   [/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/mps/__init__.py<lambda>       r   _default_mps_generator)returnc                   C   s   t d u 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_synchronizer   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s
dS t| } t |  dS )zbSets the seed for generating random numbers.

    Args:
        seed (int): The desired seed.
    N)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   1   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_cache5   s    r   c                 C   s@   t | tstd| dk s"| dkr0t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: {}. Allowed range: 0~2N)
isinstancefloat	TypeError
ValueErrorformatr
   r   Z_mps_setMemoryFraction)fractionr   r   r   set_per_process_memory_fraction;   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_memoryS   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_memory\   s    r   )	r   r   r   r   r   r   r   r   r   )__doc__r
    r   getattrr   Z_is_in_bad_forkr   	Generator__annotations__r   r   r   r   r   r   r   r   r   r   r   __all__r   r   r   r   <module>   s   			