Ë
    [^(h‡/  ã            	       ó   — U d Z ddlZddlZddl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j0                  Zd	ag aeeeg df   ee   f      ed
<    ej<                  «       Z ej@                  «       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„Z/d/dee   defd„Z0	 d0dee   dededdfd„Z1de2eef   fd„Z3d/dee   deeef   fd„Z4d.d „Z5d!efd"„Z6deddfd#„Z7 G d$„ d«      Z	 G d%„ d&«      Z8d!ed'   de8fd(„Z9d1deeeej                  f   defd)„Z:	 d1d*edeeeej                  f   ddfd+„Z;dd,l<­ g d-¢Z=y)2zH
This package enables an interface for accessing MTIA backend in python
é    N)ÚAnyÚCallableÚOptionalÚUnion)ÚdeviceÚTensor)Ú_dummy_typeÚ_LazySeedTrackerÚclassproperty)ÚDeviceé   )Ú_get_device_indexFÚ_queued_callsc                  ó   — t        «        y ©N)Ú
_lazy_init© ó    úQ/var/www/skyplay_api_hub/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„ t        j                  «       D «       «       	 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.dynamic_library` in your python src file and include `//mtia/host_runtime/torch_mtia:torch_mtia` as your target dependency!Tc              3   ó&   K  — | ]	  }|sŒ|–— Œ y ­wr   r   )Ú.0Úcallss     r   ú	<genexpr>z_lazy_init.<locals>.<genexpr>P   s   è ø€ ÒX uÒRWœUÑXùs   ‚Š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Úmsgs       r   r   r   .   sL  € äÔœ7¤4Ð):Ô;ØÜ	ñ ,ô ÔØ÷,ð ,ô ÔÜðIóð ô Œ~Ü ð*óð ô 	�‰×ÑÔð  $ŒÔä×ÑÑXÔ0B×0LÑ0LÓ0NÔXÔXð	-Ü/<ò <Ñ+�˜^ð<Ù•Mñ<ô ”DÐ+Ô,Øˆ÷Y,ð ,øôH !ò <àPÔQTÐUVÓQWÐPXð YCØCEÇ7Á7È>ÓCZÐB[ð]ð ô 0°Ó4¸!Ð;ûð<ûô ”DÐ+Õ,ú÷W,ð ,úsM   ¢D=·BD=Â9D(ÃC+ÃD(ÃD=Ã+	D%Ã4,D Ä D%Ä%D(Ä(D:Ä:D=Ä=Ec                   ó   — e Zd Zy)r4   N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r4   r4   a   s   „ Ør   r4   c                  ó>   — t         j                  j                  «       S )z*Return true if compiled with MTIA support.)r   r   Ú_mtia_isBuiltr   r   r   r+   r+   e   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   j   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   r   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   Ú_mtia_getDeviceCountr   r   r   rA   rA   x   ó   € ô �8‰8×(Ñ(Ó*Ð*r   c                  ó>   — t         j                  j                  «       S )z0Return the index of a currently selected device.)r   r   Ú%_accelerator_hooks_get_current_devicer   r   r   Úcurrent_devicerM   ~   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_streamrR   ƒ   ó!   € ô �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).
    TrO   )r   r   Ú_mtia_getDefaultStreamr   rF   s    r   Údefault_streamrV   �   rS   r   ÚenabledÚstacksÚmax_entriesc                 ó\   — t        «       syt        j                  j                  | ||«       y)a•  Enable/Disable the memory profiler on MTIA allocator

    Args:
        enabled (all or state, optional) selected device. Returns
            statistics for the current device, given by current_device(),
            if device is None (default).

        stacks ("python" or "cpp", optional). Select the stack trace to record.

        max_entries (int, optional). Maximum number of entries to record.
    N)r   r   r   Ú_mtia_recordMemoryHistory)rW   rX   rY   s      r   Úrecord_memory_historyr\   ›   s#   € ô ÔØÜ	‡H�H×&Ñ& w°¸ÕDr   c                  ó>   — t         j                  j                  «       S )z4Return a dictionary of MTIA memory allocator history)r   r   Ú_mtia_memorySnapshotr   r   r   Úsnapshotr_   ®   rJ   r   c                 óV   — t         j                  j                  t        | d¬«      «      S )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).
    TrO   )r   r   Ú_mtia_getDeviceCapabilityr   rF   s    r   Úget_device_capabilityrb   ´   s"   € ô �8‰8×-Ñ-Ô.?ÀÐQUÔ.VÓWÐWr   c                  ó>   — t         j                  j                  «       S )zEmpty the MTIA device cache.)r   r   Ú_mtia_emptyCacher   r   r   Úempty_cachere   ¿   s   € ä�8‰8×$Ñ$Ó&Ð&r   Ú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©rf   s    r   Ú
set_streamrj   Ä   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_devicerm   Ò   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 )NTrO   éÿÿÿÿ)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_devicerq   rr   )rs   s    r   Ú	__enter__zdevice.__enter__ê   s   € ÜŸ™×IÑIÈ$Ï(É(ÓSˆ�r   ÚtypeÚvalueÚ	tracebackc                 ó`   — t         j                  j                  | j                  «      | _        y)NF)r   r   rv   rr   rq   )rs   rx   ry   rz   s       r   Ú__exit__zdevice.__exit__í   s   € Ü—8‘8×DÑDÀTÇ]Á]ÓSˆŒØr   N)r;   r<   r=   Ú__doc__r   rt   rw   r|   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_streamrf   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 )NTrp   )r�   rf   r   rq   r   ÚjitÚis_scriptingrD   rV   Úsrc_prev_streamÚdst_prev_stream)rs   rf   s     r   rt   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©Nrp   )	rf   rq   r   rD   rR   r…   r   r†   rj   )rs   r�   s     r   rw   zStreamContext.__enter__  s§   € à—[‘[ˆ
àÐ §¡¨R¢ØÜ$Ÿz™z×8Ñ8¸Ó>ˆÔð ×Ñ×&Ñ&¨*×*;Ñ*;Ò;Ü˜
×)Ñ)Ó*ñ TÜ',§z¡z×'@Ñ'@À×ARÑARÓ'S�Ô$÷Tä�
‰
×Ñ˜jÕ)÷Tð Tús   Á;/CÃCrx   ry   rz   c                 ó*  — | j                   }|�| j                  dk(  ry | j                  j                  |j                  k7  r)t        j
                  j                  | j                  «       t        j
                  j                  | j                  «       y rˆ   )rf   rq   r…   r   r   rD   rj   r†   )rs   rx   ry   rz   r�   s        r   r|   zStreamContext.__exit__  sj   € à—[‘[ˆ
àÐ §¡¨R¢Øð ×Ñ×&Ñ&¨*×*;Ñ*;Ò;Ü�J‰J×!Ñ! $×"6Ñ"6Ô7Ü�
‰
×Ñ˜d×2Ñ2Õ3r   N)
r;   r<   r=   r}   r   Ú__annotations__rt   rw   r   r|   r   r   r   r   r   ò   sF   … ñ	ð Ð,Ñ-Ó-ð
˜xÐ(;Ñ<ó 
ò*ð4˜Sð 4¨ð 4¸ô 4r   r   r€   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
    )r   ri   s    r   rf   rf   ,  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–   7  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™   F  s   € ô ‡M�MØ8ÜØör   )Ú*)r   rB   r   rG   rA   rM   rR   rV   Úmemory_statsÚmax_memory_allocatedÚreset_peak_memory_statsrb   r\   r_   re   rm   rj   rf   r   r™   r–   )r   Nr   )ÚallÚpythonr   )rD   )>r}   Ú	threadingr‘   Útypingr   r   r   r   r   r   Ú_devicer   Útorch._utilsr	   r
   r   Útorch.typesr   Ú_utilsr   r2   ÚintÚ	_device_tÚEventÚStreamr   r   ÚlistÚtuplerŠ   Úlocalr(   ÚLockr)   r/   r   r   Úboolr   r   r1   r4   r+   rB   rG   rA   rM   rR   rV   r\   Údictr_   rb   re   rj   rm   r   rf   r–   r™   ÚmemoryÚ__all__r   r   r   ú<module>r²      su  ðòó Û ß 1Ó 1ã ß +ß EÑ EÝ å %ð �'˜3 Ð#Ñ$€	ð 	�‰€Ø	�‰€à€ð ð ˆtØ	ˆ(�2�t�8Ñ
˜d 3™iÐ
'Ñ(ñó ð €y‡�Ó€Ø%�y—~‘~Ó'Ð Ù%Ó'Ð òò2ð
(˜ó (ó0ôf	˜Iô 	ð$�dó $ð
�dó ñ2˜ Ñ+ð 2°tó 2ð+�có +ð<˜ó <ñ
	U˜8 IÑ.ð 	U¸&ó 	Uñ	U˜8 IÑ.ð 	U¸&ó 	Uð PQñEØ�c‰]ðEØ,/ðEØILðEà	óEð&+�$�s˜C�x‘.ó +ñX (¨9Ñ"5ð XÀÀsÈCÀxÁó Xó'ð
,�vó ,ð	?�yð 	? Tó 	?÷ñ ÷(74ñ 74ðt!�8Ð/Ñ0ð !°]ó !ñ>˜%  S¨%¯,©,Ð 6Ñ7ð >ÀVó >ð  @FñØðØ$ S¨#¨u¯|©|Ð%;Ñ<ðà	óô" ò�r   