U
    Mh"                     @   s  U d Z ddlZddlmZmZmZmZmZmZm	Z	 ddl
Z
ddlmZ ddlmZmZ ddlmZmZmZ dd	lmZ e	eeedf Ze
jZe
jZd
ag aeeeg df ee f  ed< e Ze  Z!e Z"dd Z#dd Z$e%dddZ&ddddZ'G dd de(Z)e%dddZ*e%dddZ+ddddZ,edddZ-eddd Z.d1ee ed!d"d#Z/d2ee ed!d$d%Z0ed&d'd(Z1G d)d* d*ZG d+d, d,Z2ed- e2d.d/d0Z3dddddd d#d%d(d0d*gZ4dS )3zH
This package enables an interface for accessing MTIA backend in python
    N)AnyCallableDictListOptionalTupleUnion)Device   )deviceTensor)_dummy_type_LazySeedTrackerclassproperty   )_get_device_indexF_queued_callsc                   C   s
   t   d S N)
_lazy_init r   r   E/var/www/html/venv/lib/python3.8/site-packages/torch/mtia/__init__.pyinit    s    r   c                   C   s   t o
t  S )z9Return whether PyTorch's MTIA state has been initialized.)_initialized_is_in_bad_forkr   r   r   r   is_initialized$   s    r   )returnc                   C   s
   t j S r   )torch_CZ_mtia_isInBadForkr   r   r   r   r   )   s    r   c                  C   s   t  sttdrd S t t  r.W 5 Q R  d S t r<tdt sJtdtj	
  dt_t D ]} | rbt|  qbzdtD ]Z\}}z
|  W q| tk
r } z(dt| dd| }t||W 5 d }~X Y q|X q|W 5 ttd X daW 5 Q R X d S )Nis_initializingzwCannot re-initialize MTIA in forked subprocess. To use MTIA with multiprocessing, you must use the 'spawn' start methodz$Torch not compiled with MTIA enabledTz6MTIA call failed lazily at initialization with error: z(

MTIA call was originally invoked at:

 )r   hasattr_tls_initialization_lockr   RuntimeError_is_compiledAssertionErrorr   r   Z
_mtia_initr   _lazy_seed_trackerZ	get_callsr   appenddelattr	ExceptionstrjoinDeferredMtiaCallErrorr   )ZcallsZqueued_callZorig_tracebackemsgr   r   r   r   -   s4    

"r   c                   @   s   e Zd ZdS )r,   N)__name__
__module____qualname__r   r   r   r   r,   ]   s   r,   c                   C   s
   t j S )z*Return true if compiled with MTIA support.)r   r   Z_mtia_isBuiltr   r   r   r   r$   a   s    r$   c                   C   s   t  s
dS t dkS )z'Return true if MTIA device is availableFr   )r$   device_countr   r   r   r   is_availablef   s    r3   c                   C   s
   t j S )z?Waits for all jobs in all streams on a MTIA device to complete.)r   r   Z_mtia_deviceSynchronizer   r   r   r   synchronizen   s    r4   c                   C   s
   t j S )z,Return the number of MTIA devices available.)r   r   Z_accelerator_hooks_device_countr   r   r   r   r2   s   s    r2   c                   C   s
   t j S )z0Return the index of a currently selected device.)r   r   Z%_accelerator_hooks_get_current_devicer   r   r   r   current_devicex   s    r5   )r   r   c                 C   s   t jt| ddS )aS  Return the currently selected :class:`Stream` for a given device.

    Args:
        device (torch.device or int, optional): selected device. Returns
            the currently selected :class:`Stream` for the current device, given
            by :func:`~torch.mtia.current_device`, if :attr:`device` is ``None``
            (default).
    Toptional)r   r   Z_mtia_getCurrentStreamr   r   r   r   r   current_stream}   s    	r9   c                 C   s   t jt| ddS )a=  Return the default :class:`Stream` for a given device.

    Args:
        device (torch.device or int, optional): selected device. Returns
            the default :class:`Stream` for the current device, given by
            :func:`~torch.mtia.current_device`, if :attr:`device` is ``None``
            (default).
    Tr6   )r   r   Z_mtia_getDefaultStreamr   r8   r   r   r   default_stream   s    	r:   streamc                 C   s   | dkrdS t j|  dS )a  Set the current stream.This is a wrapper API to set the stream.
        Usage of this function is discouraged in favor of the ``stream``
        context manager.

    Args:
        stream (Stream): selected stream. This function is a no-op
            if this argument is ``None``.
    N)r   r   Z_mtia_setCurrentStreamr;   r   r   r   
set_stream   s    	r=   c                   @   s8   e Zd ZdZedddZdd Zeeeddd	Zd
S )r   zContext-manager that changes the selected device.

    Args:
        device (torch.device or int): device index to select. It's a no-op if
            this argument is a negative integer or ``None``.
    r8   c                 C   s   t |dd| _d| _d S )NTr6   )r   idxprev_idx)selfr   r   r   r   __init__   s    zdevice.__init__c                 C   s   t j| j| _d S r   )r   r   (_accelerator_hooks_maybe_exchange_devicer?   r@   )rA   r   r   r   	__enter__   s    zdevice.__enter__typevalue	tracebackc                 C   s   t j| j| _dS )NF)r   r   rC   r@   r?   )rA   rF   rG   rH   r   r   r   __exit__   s    zdevice.__exit__N)r/   r0   r1   __doc__r   rB   rD   rI   r   r   r   r   r      s   r   c                   @   sJ   e Zd ZU dZed ed< ed dddZdd Zeeed	d
dZ	dS )StreamContexta  Context-manager that selects a given stream.

    All MTIA kernels queued within its context will be enqueued on a selected
    stream.

    Args:
        Stream (Stream): selected stream. This manager is a no-op if it's
            ``None``.
    .. note:: Streams are per-device.
    torch.mtia.Stream
cur_streamr;   c                 C   sh   || _ td d| _tj s,| jd kr,d| _tj s:d n
tjd | _tj sVd n
tjd | _	d S )NTr>   )
r<   r   r?   r   ZjitZis_scriptingmtiar:   src_prev_streamdst_prev_stream)rA   r<   r   r   r   rB      s    

zStreamContext.__init__c              	   C   sn   | j }|d ks| jdkrd S tjd | _| jj|jkr^t|j tj|j| _W 5 Q R X tj| d S Nr>   )	r<   r?   r   rN   r9   rO   r   rP   r=   )rA   rM   r   r   r   rD      s    zStreamContext.__enter__rE   c                 C   sJ   | j }|d ks| jdkrd S | jj|jkr8tj| j tj| j d S rQ   )r<   r?   rO   r   r   rN   r=   rP   )rA   rF   rG   rH   rM   r   r   r   rI      s    zStreamContext.__exit__N)
r/   r0   r1   rJ   r   __annotations__rB   rD   r   rI   r   r   r   r   rK      s
   
rK   rL   )r<   r   c                 C   s   t | S )a+  Wrap around the Context-manager StreamContext that selects a given stream.

    Arguments:
        stream (Stream): selected stream. This manager is a no-op if it's
            ``None``.
    ..Note:: In eager mode stream is of type Stream class while in JIT it doesn't support torch.mtia.stream
    )rK   r;   r   r   r   r<      s    r<   )N)N)5rJ   	threadingtypingr   r   r   r   r   r   r   r   Ztorch.typesr	   r   r   Z_devicer   Z_utilsr   r   r   r   r*   intZ	_device_tEventZStreamr   r   rR   localr!   Lockr"   r&   r   r   boolr   r   r)   r,   r$   r3   r4   r2   r5   r9   r:   r=   rK   r<   __all__r   r   r   r   <module>   s\   $09