o
    闦i+                     @   sd  U d Z ddl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mZ ddlmZmZmZ ddlmZ ddlmZ e
eee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%de&fddZ'd;ddZ(G dd de)Z*de&fddZ+de&fddZ,d<dee ddfddZ-defddZ.defdd Z/d<dee defd!d"Z0d<dee defd#d$Z1d<dee de	eef fd%d&Z2d;d'd(Z3d)efd*d+Z4deddfd,d-Z5G d.d dZG d/d0 d0Z6d)ed1 de6fd2d)Z7d=de
eeejf defd4d5Z8	3d=d6ede
eeejf ddfd7d8Z9dd9l:T g d:Z;dS )>zH
This package enables an interface for accessing MTIA backend in python
    N)AnyCallableDictListOptionalTupleUnion)deviceTensor)_dummy_type_LazySeedTrackerclassproperty)Device   )_get_device_indexF_queued_callsc                   C   s
   t   d S N)
_lazy_init r   r   Q/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/mtia/__init__.pyinit!      
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   
   t j S r   )torch_C_mtia_isInBadForkr   r   r   r   r   *   r   r   c                  C   s  t  sttdr
d S tq t  r	 W d    d S t r tdt s'tdtj	
  dt_tdd t D  z1tD ]'\} }z|   W q> tye } zdt| dd	| }t||d }~ww W ttd nttd w daW d    d S 1 sw   Y  d S )
Nis_initializingzwCannot re-initialize MTIA in forked subprocess. To use MTIA with multiprocessing, you must use the 'spawn' start methodzTorch not compiled with MTIA enabled. Ensure you have `import mtia.host_runtime.torch_mtia` in your python src file and include `//mtia/host_runtime/torch_mtia:torch_mtia` as your target dependency!Tc                 s   s    | ]}|r|V  qd S r   r   ).0callsr   r   r   	<genexpr>P   s    z_lazy_init.<locals>.<genexpr>z6MTIA call failed lazily at initialization with error: z(

MTIA call was originally invoked at:

 )r   hasattr_tls_initialization_lockr   RuntimeError_is_compiledAssertionErrorr   r   
_mtia_initr    r   extend_lazy_seed_tracker	get_calls	ExceptionstrjoinDeferredMtiaCallErrordelattrr   )queued_callorig_tracebackemsgr   r   r   r   .   sB   



"r   c                   @   s   e Zd ZdS )r2   N)__name__
__module____qualname__r   r   r   r   r2   a   s    r2   c                   C   r   )z*Return true if compiled with MTIA support.)r   r   _mtia_isBuiltr   r   r   r   r)   e      
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_availablej   s   
r>   r	   c                 C   s:   t j|  t j W  d   S 1 sw   Y  dS )z?Waits for all jobs in all streams on a MTIA device to complete.N)r   mtiar	   r   _mtia_deviceSynchronizer	   r   r   r   synchronizer   s   $rB   c                   C   r   )z,Return the number of MTIA devices available.)r   r   _accelerator_hooks_device_countr   r   r   r   r=   x   r<   r=   c                   C   r   )z0Return the index of a currently selected device.)r   r   %_accelerator_hooks_get_current_devicer   r   r   r   current_device}   r<   rE   c                 C      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   _mtia_getCurrentStreamr   rA   r   r   r   current_stream      	rJ   c                 C   rF   )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).
    TrG   )r   r   _mtia_getDefaultStreamr   rA   r   r   r   default_stream   rK   rM   c                 C   rF   )a  Return capability of a given device as a tuple of (major version, minor version).

    Args:
        device (torch.device or int, optional) selected device. Returns
            statistics for the current device, given by current_device(),
            if device is None (default).
    TrG   )r   r   _mtia_getDeviceCapabilityr   rA   r   r   r   get_device_capability   s   rO   c                   C   r   )zEmpty the MTIA device cache.)r   r   _mtia_emptyCacher   r   r   r   empty_cache   r<   rQ   streamc                 C   s   | du 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   _mtia_setCurrentStreamrR   r   r   r   
set_stream   s   	rU   c                 C   s$   t | } | dkrtj|  dS dS )zSet the current device.

    Args:
        device (torch.device or int): selected device. This function is a no-op
            if this argument is negative.
    r   N)r   r   r   %_accelerator_hooks_set_current_devicerA   r   r   r   
set_device   s   rW   c                   @   s<   e Zd ZdZd efddZdd Zdededef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``.
    c                 C   s   t |dd| _d| _d S )NTrG   )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_devicerY   rZ   )r[   r   r   r   	__enter__   s   zdevice.__enter__typevalue	tracebackc                 C   s   t j| j| _dS )NF)r   r   r]   rZ   rY   )r[   r_   r`   ra   r   r   r   __exit__   s   zdevice.__exit__N)r8   r9   r:   __doc__r   r\   r^   rb   r   r   r   r   r	      s
    c                   @   sN   e Zd ZU dZed ed< ded fddZdd Zd	ed
edef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_streamrR   c                 C   st   d | _ || _td d| _tj s| jd u rd| _tj s d ntjd | _	tj s1d | _
d S tjd | _
d S )NTrX   )rf   rR   r   rY   r   jitis_scriptingr?   rM   src_prev_streamdst_prev_stream)r[   rR   r   r   r   r\      s   


zStreamContext.__init__c                 C   s   | j }|d u s| jdkrd S tjd | _| jj|jkr9t|j tj|j| _W d    n1 s4w   Y  tj| d S NrX   )	rR   rY   r   r?   rJ   ri   r	   rj   rU   )r[   rf   r   r   r   r^      s   zStreamContext.__enter__r_   r`   ra   c                 C   sJ   | j }|d u s| jdkrd S | jj|jkrtj| j tj| j d S rk   )rR   rY   ri   r	   r   r?   rU   rj   )r[   r_   r`   ra   rf   r   r   r   rb     s   zStreamContext.__exit__N)
r8   r9   r:   rc   r   __annotations__r\   r^   r   rb   r   r   r   r   rd      s   
 rd   re   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
    )rd   rT   r   r   r   rR     s   r?   c                 C   s$   t jdtdd tjdgtj| d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: ``'mtia'`` (i.e., ``torch.device('mtia')``, the current mtia device).
    z.get_rng_state is not implemented in torch.mtia   
stacklevelr   )dtyper	   )warningswarnUserWarningr   zerosuint8rA   r   r   r   get_rng_state  s   rv   	new_statec                 C   s   t jdtdd 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: ``'mtia'`` (i.e., ``torch.device('mtia')``, the current mtia device).
    z.set_rng_state is not implemented in torch.mtiarm   rn   N)rq   rr   rs   )rw   r	   r   r   r   set_rng_state,  s
   

rx   )*)r   r>   r   rB   r=   rE   rJ   rM   memory_statsmax_memory_allocatedrO   rQ   rW   rU   rR   r	   rx   rv   )r   Nr   )r?   )<rc   	threadingrq   typingr   r   r   r   r   r   r   r   r	   _devicer
   torch._utilsr   r   r   torch.typesr   _utilsr   r0   int	_device_tEventStreamr   r   rl   localr&   Lockr'   r-   r   r   boolr   r   r/   r2   r)   r>   rB   r=   rE   rJ   rM   rO   rQ   rU   rW   rd   rR   rv   rx   memory__all__r   r   r   r   <module>   sd   $
3 
: 
