Ë
    Q/æj3+  ã                   óä  — 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df   Zej4                  Zej6                  Zd	ag aee	eg df   ee   f      ed
<    ej>                  «       Z  ejB                  «       Z" e«       Z#d„ Z$d„ Z%de&fd„Z'd'd„Z( G d„ de)«      Z*de&fd„Z+de&fd„Z,d(dee   ddfd„Z-defd„Z.defd„Z/d(dee   defd„Z0d(dee   defd„Z1d(dee   deeef   fd„Z2defd„Z3deddfd„Z4 G d„ d«      Z G d„ d «      Z5ded!   de5fd"„Z6d)de
eeej                  f   defd#„Z7	 d)d$ede
eeej                  f   ddfd%„Z8g d&¢Z9y)*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                  ó   — t        «        y ©N)Ú
_lazy_init© ó    úV/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/mtia/__init__.pyÚinitr   !   s   € Ü…Lr   c                  ó(   — t         xr t        «        S )z9Return whether PyTorch's MTIA state has been initialized.)Ú_initializedÚ_is_in_bad_forkr   r   r   Úis_initializedr   %   s   € äÒ1¤Ó 1Ð1Ð1r   Úreturnc                  ó>   — t         j                  j                  «       S r   )ÚtorchÚ_CÚ_mtia_isInBadForkr   r   r   r   r   *   s   € Ü�8‰8×%Ñ%Ó'Ð'r   c            	      ó˜  — t        «       st        t        d«      ry t        5  t        «       r
	 d d d «       y t	        «       rt        d«      ‚t        «       st        d«      ‚t        j                  j                  «        dt        _        t        j                  «       D ]  } | sŒt        j                  | «       Œ 	 t        D ]  \  }}	  |«        Œ 	 t)        t        d«       dad d d «       y # t         $ r1}dt#        |«      › ddj%                  |«      › �}t'        |«      |‚d }~ww xY w# t)        t        d«       w xY w# 1 sw Y   y xY w)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 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!Tz6MTIA 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$   Ú_lazy_seed_trackerÚ	get_callsr   ÚappendÚ	ExceptionÚstrÚjoinÚDeferredMtiaCallErrorÚdelattrr   )ÚcallsÚqueued_callÚorig_tracebackÚeÚmsgs        r   r   r   .   sW  € äÔœ7¤4Ð):Ô;ØÜ	ñ .ô ÔØ÷.ð .ô ÔÜðIóð ô Œ~Ü ð*óð ô 	�‰×ÑÔð  $ŒÔä'×1Ñ1Ó3ò 	,ˆEÚÜ×$Ñ$ UÕ+ð	,ð	-Ü/<ò <Ñ+�˜^ð<Ù•Mñ<ô ”DÐ+Ô,Øˆ÷].ð .øôL !ò <àPÔQTÐUVÓQWÐPXð YCØCEÇ7Á7È>ÓCZÐB[ð]ð ô 0°Ó4¸!Ð;ûð<ûô ”DÐ+Õ,ú÷[.ð .úsS   ¢E ·A,E Â$E Â<D+Ã	C.ÃD+ÃE Ã.	D(Ã7,D#Ä#D(Ä(D+Ä+D=Ä=E Å E	c                   ó   — e Zd Zy)r3   N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r3   r3   c   s   „ Ør   r3   c                  ó>   — t         j                  j                  «       S )z*Return true if compiled with MTIA support.)r    r!   Ú_mtia_isBuiltr   r   r   r*   r*   g   s   € ä�8‰8×!Ñ!Ó#Ð#r   c                  ó2   — t        «       syt        «       dkD  S )z'Return true if MTIA device is availableFr   )r*   Údevice_countr   r   r   Úis_availablerB   l   s   € äŒ>Øä‹>˜AÑÐr   r
   c                 ó¨   — t         j                  j                  | «      5  t         j                  j	                  «       cddd«       S # 1 sw Y   yxY w)z?Waits for all jobs in all streams on a MTIA device to complete.N)r    Úmtiar
   r!   Ú_mtia_deviceSynchronize©r
   s    r   ÚsynchronizerG   t   s9   € ä	�‰×	Ñ	˜6Ó	"ñ 2Ü�x‰x×/Ñ/Ó1÷2÷ 2ò 2ús    AÁAc                  ó>   — t         j                  j                  «       S )z,Return the number of MTIA devices available.)r    r!   Ú_accelerator_hooks_device_countr   r   r   rA   rA   z   s   € ä�8‰8×3Ñ3Ó5Ð5r   c                  ó>   — t         j                  j                  «       S )z0Return the index of a currently selected device.)r    r!   Ú%_accelerator_hooks_get_current_devicer   r   r   Úcurrent_devicerL      s   € ä�8‰8×9Ñ9Ó;Ð;r   c                 óV   — t         j                  j                  t        | 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   rF   s    r   Úcurrent_streamrQ   „   ó!   € ô �8‰8×*Ñ*Ô+<¸VÈdÔ+SÓTÐTr   c                 óV   — t         j                  j                  t        | 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).
    TrN   )r    r!   Ú_mtia_getDefaultStreamr   rF   s    r   Údefault_streamrU   �   rR   r   c                 ón   — t        «       si S t        j                  j                  t	        | d¬«      «      S )a  Return a dictionary of MTIA memory allocator statistics for a given device.

    Args:
        device (torch.device or int, optional) selected device. Returns
            statistics for the current device, given by current_device(),
            if device is None (default).
    TrN   )r   r    r!   Ú_mtia_memoryStatsr   rF   s    r   Úmemory_statsrX   œ   s,   € ô ÔØˆ	Ü�8‰8×%Ñ%Ô&7¸ÈÔ&NÓOÐOr   Ústreamc                 óH   — | €yt         j                  j                  | «       y)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©rY   s    r   Ú
set_streamr]   ©   s   € ð €~ØÜ	‡H�H×#Ñ# FÕ+r   c                 ód   — t        | «      } | dk\  r t        j                  j                  | «       yy)z¡Set 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_devicerF   s    r   Ú
set_devicer`   ·   s,   € ô ˜vÓ&€FØ�‚{Ü�‰×6Ñ6°vÕ>ð r   c                   ó6   — e Zd ZdZd efd„Zd„ Zdededefd„Zy)	r
   zÌContext-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                 ó6   — t        |d¬«      | _        d| _        y )NTrN   éÿÿÿÿ)r   ÚidxÚprev_idx)Úselfr
   s     r   Ú__init__zdevice.__init__Ë   s   € Ü$ V°dÔ;ˆŒØˆ�r   c                 ó`   — t         j                  j                  | j                  «      | _        y r   )r    r!   Ú(_accelerator_hooks_maybe_exchange_devicerd   re   )rf   s    r   Ú	__enter__zdevice.__enter__Ï   s   € ÜŸ™×IÑIÈ$Ï(É(ÓSˆ�r   ÚtypeÚvalueÚ	tracebackc                 ó`   — t         j                  j                  | j                  «      | _        y)NF)r    r!   ri   re   rd   )rf   rk   rl   rm   s       r   Ú__exit__zdevice.__exit__Ò   s   € Ü—8‘8×DÑDÀTÇ]Á]ÓSˆŒØr   N)r;   r<   r=   Ú__doc__r   rg   rj   ro   r   r   r   r
   r
   Ã   s3   „ ñð˜só òTð˜Sð ¨ð ¸ô r   c                   óN   — e Zd ZU dZed   ed<   ded   fd„Zd„ Zdeded	efd
„Z	y)Ú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_streamrY   c                 óÀ  — d | _         || _        t        d d«      | _        t        j
                  j                  «       s| j                  €d| _        t        j
                  j                  «       sd nt        j                  j                  d «      | _	        t        j
                  j                  «       sd | _
        y t        j                  j                  d «      | _
        y )NTrc   )rt   rY   r   rd   r    ÚjitÚis_scriptingrD   rU   Úsrc_prev_streamÚdst_prev_stream)rf   rY   s     r   rg   zStreamContext.__init__å   s¤   € ØˆŒØˆŒÜ$ T¨4Ó0ˆŒÜ�y‰y×%Ñ%Ô'Ø�x‰xÐØ�”ô Ÿ	™	×.Ñ.Ô0‰D´e·j±j×6OÑ6OÐPTÓ6Uð 	Ôô Ÿ	™	×.Ñ.Ô0ˆDð 	ÕÜ6;·j±j×6OÑ6OÐPTÓ6Uð 	Õr   c                 ó¼  — | j                   }|�| j                  dk(  ry t        j                  j	                  d «      | _        | j
                  j                  |j                  k7  rLt        |j                  «      5  t        j                  j	                  |j                  «      | _        d d d «       t        j                  j                  |«       y # 1 sw Y   Œ)xY w©Nrc   )	rY   rd   r    rD   rQ   rx   r
   ry   r]   )rf   rt   s     r   rj   zStreamContext.__enter__ô   s§   € à—[‘[ˆ
àÐ §¡¨R¢ØÜ$Ÿz™z×8Ñ8¸Ó>ˆÔð ×Ñ×&Ñ&¨*×*;Ñ*;Ò;Ü˜
×)Ñ)Ó*ñ TÜ',§z¡z×'@Ñ'@À×ARÑARÓ'S�Ô$÷Tä�
‰
×Ñ˜jÕ)÷Tð Tús   Á;/CÃCrk   rl   rm   c                 ó*  — | j                   }|�| j                  dk(  ry | j                  j                  |j                  k7  r)t        j
                  j                  | j                  «       t        j
                  j                  | j                  «       y r{   )rY   rd   rx   r
   r    rD   r]   ry   )rf   rk   rl   rm   rt   s        r   ro   zStreamContext.__exit__  sj   € à—[‘[ˆ
àÐ §¡¨R¢Øð ×Ñ×&Ñ&¨*×*;Ñ*;Ò;Ü�J‰J×!Ñ! $×"6Ñ"6Ô7Ü�
‰
×Ñ˜d×2Ñ2Õ3r   N)
r;   r<   r=   rp   r   Ú__annotations__rg   rj   r   ro   r   r   r   rr   rr   ×   sF   … ñ	ð Ð,Ñ-Ó-ð
˜xÐ(;Ñ<ó 
ò*ð4˜Sð 4¨ð 4¸ô 4r   rr   rs   c                 ó   — 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
    )rr   r\   s    r   rY   rY     s   € ô ˜Ó Ð r   c                 óˆ   — t        j                  dt        d¬«       t        j                  dgt        j
                  | ¬«      S )züReturns 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Úuint8rF   s    r   Úget_rng_stater‰     s4   € ô ‡M�MØ8ÜØõô
 �;‰;˜�s¤%§+¡+°fÔ=Ð=r   Ú	new_statec                 ó<   — t        j                  dt        d¬«       y)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.mtiar€   r�   N)r„   r…   r†   )rŠ   r
   s     r   Úset_rng_staterŒ   +  s   € ô ‡M�MØ8ÜØör   )r   rB   r   rG   rA   rL   rQ   rU   rX   r`   r]   rY   r
   rŒ   r‰   )r   Nr   )rD   ):rp   Ú	threadingr„   Útypingr   r   r   r   r   r   r	   r    r
   Ú_devicer   Útorch._utilsr   r   r   Útorch.typesr   Ú_utilsr   r1   ÚintÚ	_device_tÚEventÚStreamr   r   r}   Úlocalr'   ÚLockr(   r-   r   r   Úboolr   r   r0   r3   r*   rB   rG   rA   rL   rQ   rU   rX   r]   r`   rr   rY   r‰   rŒ   Ú__all__r   r   r   ú<module>r›      s'  ðòó Û ß D× DÑ Dã ß +ß EÑ EÝ å %ð �'˜3  TÐ)Ñ*€	ð 	�‰€Ø	�‰€à€ð ð ˆtØ	ˆ(�2�t�8Ñ
˜d 3™iÐ
'Ñ(ñó ð €y‡�Ó€Ø%�y—~‘~Ó'Ð Ù%Ó'Ð òò2ð
(˜ó (ó2ôj	˜Iô 	ð$�dó $ð
�dó ñ2˜ Ñ+ð 2°tó 2ð6�có 6ð
<˜ó <ñ
	U˜8 IÑ.ð 	U¸&ó 	Uñ	U˜8 IÑ.ð 	U¸&ó 	Uñ
P˜ )Ñ,ð 
P¸¸SÀ#¸X¹ó 
Pð,�vó ,ð	?�yð 	? Tó 	?÷ñ ÷(74ñ 74ðt!�8Ð/Ñ0ð !°]ó !ñ>˜%  S¨%¯,©,Ð 6Ñ7ð >ÀVó >ð  @FñØðØ$ S¨#¨u¯|©|Ð%;Ñ<ðà	óò"�r   