a
    ¤þd\  ã                   @   s†   d dl Z d dlZd dlmZmZmZmZ G dd„ dƒZdd„ Zdd„ Z	d	d
„ Z
dd„ Ze jdd„ ƒZe jdd„ ƒZG dd„ deƒZdS )é    N)Ú_len_torch_dispatch_stackÚ_get_dispatch_stack_atÚ_pop_torch_dispatch_stackÚ_push_on_torch_dispatch_stackc                   @   s6   e Zd ZdZddd„Zdd„ Zdd	„ Zed
d„ ƒZdS )ÚTorchDispatchModeaþ  
    A ``TorchDispatchMode`` allows you to override the meaning of all
    ``__torch_dispatch__`` overrideable functions within a dynamic scope,
    without having to actually create a tensor subclass or manually
    monkey-patch functions in the PyTorch API.  Some common situations
    where you should use a mode:

        * You want to override the meaning of factory functions, or other
          functions that do not otherwise take a tensor as an argument
          (these cannot be overridden with tensor subclasses).

        * You want to override the behavior of all functions without needing
          to wrap your inputs in tensor subclasses; e.g., if you are just
          interested in logging intermediate computations.

        * You want to control the order of execution of various tensor
          subclasses explicitly, rather than implicitly via the return of
          ``NotImplemented``.

    Independent subclasses of :class:`TorchDispatchMode` are compositional:
    modes can be pushed onto a stack using ``with MyMode():``.
    When you call functions in the PyTorch API inside your
    ``__torch_dispatch__`` implementation, by default, they will forward on to
    the next mode on the mode stack.  If you want recursively call back into
    your current ``__torch_dispatch__`` implementation, either explicitly
    invoke ``self.__torch_dispatch__(...)``, or use the context manager
    ``__torch_dispatch__(self)`` to make PyTorch
    API self-referential (beware of infinite loops, in this case!)
    © Nc                 C   s
   t ƒ ‚d S ©N)ÚNotImplementedError©ÚselfÚfuncÚtypesÚargsÚkwargsr   r   úe/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/utils/_python_dispatch.pyÚ__torch_dispatch__,   s    z$TorchDispatchMode.__torch_dispatch__c                 C   s   t | ƒ | S r   )Ú
_push_mode)r   r   r   r   Ú	__enter__/   s    zTorchDispatchMode.__enter__c                 C   s
   t ƒ  d S r   ©Ú	_pop_mode)r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__3   s    zTorchDispatchMode.__exit__c                 O   s   t  d¡ | |i |¤Ž}|S )NzP`Mode.push()` is no longer necessary and can be replaced with just `with Mode()`)ÚwarningsÚwarn)Úclsr   r   Úinstancer   r   r   Úpush6   s    
zTorchDispatchMode.push)r   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úclassmethodr   r   r   r   r   r      s   
r   c                  C   s   t ƒ } | dkrt| d ƒS d S )Nr   é   )r   r   ©Z	stack_lenr   r   r   Ú_get_current_dispatch_mode<   s    r&   c                  C   s   t ƒ } dd„ t| ƒD ƒS )Nc                 S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0Úir   r   r   Ú
<listcomp>C   ó    z4_get_current_dispatch_mode_stack.<locals>.<listcomp>)r   Úranger%   r   r   r   Ú _get_current_dispatch_mode_stackA   s    r,   c                 C   s   t | ƒ d S r   )r   )Úmoder   r   r   r   E   s    r   c                   C   s   t ƒ S r   )r   r   r   r   r   r   I   s    r   c                  c   s(   t ƒ } z| V  W t| ƒ n
t| ƒ 0 d S r   )r   r   )Úoldr   r   r   Ú_pop_mode_temporarilyM   s    r/   c               	   c   sV   t ƒ } dd„ t| ƒD ƒ}z |V  W t|ƒD ]}t|ƒ q*nt|ƒD ]}t|ƒ qB0 d S )Nc                 S   s   g | ]
}t ƒ ‘qS r   r   )r'   Ú_r   r   r   r)   Y   r*   z*_disable_current_modes.<locals>.<listcomp>)r   r+   Úreversedr   )Zmode_lenZ	old_modesr-   r   r   r   Ú_disable_current_modesV   s    ÿr2   c                   @   s   e Zd Zddd„ZdS )ÚBaseTorchDispatchModer   Nc                 C   s   |d u ri }||i |¤ŽS r   r   r
   r   r   r   r   b   s    z(BaseTorchDispatchMode.__torch_dispatch__)r   N)r   r    r!   r   r   r   r   r   r3   a   s   r3   )Ú
contextlibr   Ztorch._Cr   r   r   r   r   r&   r,   r   r   Úcontextmanagerr/   r2   r3   r   r   r   r   Ú<module>   s   
.


