
    JTh/              	       R   % S r SSKrSSKrSSKJrJrJrJr  SSKrSSKJ	r
Jr  SSKJrJrJr  SSKJr  SSKJr  \\
\\4   r\R.                  r\R0                  rS	q/ q\\\/ S4   \\   4      \S
'   \R<                  " 5       r\R@                  " 5       r!\" 5       r"S r#S r$S\%4S jr&S.S jr' " S S\(5      r)S\%4S jr*S\%4S jr+S/S\\   SS4S jjr,S\4S jr-S\4S jr.S/S\\   S\4S jjr/S/S\\   S\4S jjr0 S0S\\   S\S\SS4S jjr1S\2\\4   4S jr3S/S\\   S\\\4   4S jjr4S.S  jr5S!\4S" jr6S\SS4S# jr7 " S$ S5      r	 " S% S&5      r8S!\S'   S\84S( jr9S1S\\\\R                  4   S\4S) jjr: S1S*\S\\\\R                  4   SS4S+ jjr;SS,K<7  / S-Qr=g)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                      [        5         g N)
_lazy_init     K/var/www/auris/envauris/lib/python3.13/site-packages/torch/mtia/__init__.pyinitr   !   s    Lr   c                  :    [         =(       a    [        5       (       + $ )z9Return whether PyTorch's MTIA state has been initialized.)_initialized_is_in_bad_forkr   r   r   is_initializedr   %   s    1 111r   returnc                  >    [         R                  R                  5       $ r   )torch_C_mtia_isInBadForkr   r   r   r   r   *   s    88%%''r   c            	         [        5       (       d  [        [        S5      (       a  g [           [        5       (       a
   S S S 5        g [	        5       (       a  [        S5      e[        5       (       d  [        S5      e[        R                  R                  5         S[        l        [        R                  S [        R                  5        5       5         [         H  u  p U " 5         M     [)        [        S5        SqS S S 5        g ! [          a1  nS[#        U5       SSR%                  U5       3n['        U5      UeS nAff = f! [)        [        S5        f = f! , (       d  f       g = f)	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.dynamic_library` in your python src file and include `//mtia/host_runtime/torch_mtia:torch_mtia` as your target dependency!Tc              3   6   #    U  H  o(       d  M  Uv   M     g 7fr   r   ).0callss     r   	<genexpr>_lazy_init.<locals>.<genexpr>P   s     X0NuRWUU0Ns   
	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   .   sH   74):;;	  
	 I  ~~ *  	  $X0B0L0L0NXX	-/<+<M 0= D+,Y 
	H ! <PQTUVQWPX YCCE77>CZB[]  04!;< D+,W 
	sN   EBEED%E)E
D?,D::D??EEE
E%c                       \ rS rSrSrg)r5   a   r   N)__name__
__module____qualname____firstlineno____static_attributes__r   r   r   r5   r5   a   s    r   r5   c                  >    [         R                  R                  5       $ )z*Return true if compiled with MTIA support.)r   r   _mtia_isBuiltr   r   r   r,   r,   e   s    88!!##r   c                  <    [        5       (       d  g[        5       S:  $ )z'Return true if MTIA device is availableFr   )r,   device_countr   r   r   is_availablerF   j   s    >>>Ar   r   c                     [         R                  R                  U 5         [         R                  R	                  5       sSSS5        $ ! , (       d  f       g= f)z?Waits for all jobs in all streams on a MTIA device to complete.N)r   mtiar   r   _mtia_deviceSynchronizer   s    r   synchronizerK   r   s0    			6	"xx//1 
#	"	"s   A
Ac                  >    [         R                  R                  5       $ )z,Return the number of MTIA devices available.)r   r   _mtia_getDeviceCountr   r   r   rE   rE   x        88((**r   c                  >    [         R                  R                  5       $ )z0Return the index of a currently selected device.)r   r   %_accelerator_hooks_get_current_devicer   r   r   current_devicerQ   ~   s    8899;;r   c                 P    [         R                  R                  [        U SS95      $ )a;  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   rJ   s    r   current_streamrV      !     88**+<Vd+STTr   c                 P    [         R                  R                  [        U SS95      $ )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).
TrS   )r   r   _mtia_getDefaultStreamr   rJ   s    r   default_streamrZ      rW   r   enabledstacksmax_entriesc                 d    [        5       (       d  g[        R                  R                  XU5        g)ay  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)r[   r\   r]   s      r   record_memory_historyr`      s$     	HH&&wDr   c                  >    [         R                  R                  5       $ )z4Return a dictionary of MTIA memory allocator history)r   r   _mtia_memorySnapshotr   r   r   snapshotrc      rN   r   c                 P    [         R                  R                  [        U SS95      $ )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).
TrS   )r   r   _mtia_getDeviceCapabilityr   rJ   s    r   get_device_capabilityrf      s"     88--.?QU.VWWr   c                  >    [         R                  R                  5       $ )zEmpty the MTIA device cache.)r   r   _mtia_emptyCacher   r   r   empty_cacheri      s    88$$&&r   streamc                 J    U c  g[         R                  R                  U 5        g)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rj   s    r   
set_streamrn      s     ~	HH##F+r   c                 f    [        U 5      n U S:  a   [        R                  R                  U 5        gg)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_devicerJ   s    r   
set_devicerq      s,     v&F{66v> r   c                   B    \ rS rSrSrS \4S jrS rS\S\S\4S jrS	r	g
)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                 0    [        USS9U l        SU l        g )NTrS   )r   idxprev_idx)selfr   s     r   __init__device.__init__   s    $Vd;r   c                 `    [         R                  R                  U R                  5      U l        g r   )r   r   (_accelerator_hooks_maybe_exchange_devicerv   rw   )rx   s    r   	__enter__device.__enter__   s    II$((Sr   typevalue	tracebackc                 `    [         R                  R                  U R                  5      U l        g)NF)r   r   r|   rw   rv   )rx   r   r   r   s       r   __exit__device.__exit__   s    88DDT]]Sr   )rv   rw   N)
r=   r>   r?   r@   __doc__r   ry   r}   r   rA   r   r   r   r   r      s3    s TS   r   c                   Z    \ rS rSr% Sr\S   \S'   S\S   4S jrS rS\	S	\	S
\	4S jr
Srg)StreamContext   a  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_streamrj   c                    S U l         Xl        [        S S5      U l        [        R
                  R                  5       (       d  U R                  c  SU l        [        R
                  R                  5       (       d  S O[        R                  R                  S 5      U l	        [        R
                  R                  5       (       d  S U l
        g [        R                  R                  S 5      U l
        g )NTru   )r   rj   r   rv   r   jitis_scriptingrH   rZ   src_prev_streamdst_prev_stream)rx   rj   s     r   ry   StreamContext.__init__   s    $T40yy%%''xx 		..00Dejj6O6OPT6U 	 		..00D 	6;jj6O6OPT6U 	r   c                    U R                   nUb  U R                  S:X  a  g [        R                  R	                  S 5      U l        U R
                  R                  UR                  :w  aL  [        UR                  5         [        R                  R	                  UR                  5      U l        S S S 5        [        R                  R                  U5        g ! , (       d  f       N.= fNru   )	rj   rv   r   rH   rV   r   r   r   rn   )rx   r   s     r   r}   StreamContext.__enter__  s    [[
R$zz88> &&**;*;;
))*',zz'@'@ARAR'S$ +

j) +*s   >/C
C#r   r   r   c                 0   U R                   nUb  U R                  S:X  a  g U R                  R                  UR                  :w  a)  [        R
                  R                  U R                  5        [        R
                  R                  U R                  5        g r   )rj   rv   r   r   r   rH   rn   r   )rx   r   r   r   r   s        r   r   StreamContext.__exit__  sj    [[
R &&**;*;;JJ!!$"6"67

d223r   )r   r   rv   r   rj   N)r=   r>   r?   r@   r   r   __annotations__ry   r}   r   r   rA   r   r   r   r   r      sF    	 ,--
x(;< 
*4S 4 4 4r   r   r   c                     [        U 5      $ )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   rm   s    r   rj   rj   ,  s       r   c                     [         R                  " S[        SS9  [        R                  " S/[        R
                  U S9$ )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uint8rJ   s    r   get_rng_stater   7  s4     MM8
 ;;s%++f==r   	new_statec                 8    [         R                  " S[        SS9  g)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     MM8r   )*)r   rF   r   rK   rE   rQ   rV   rZ   memory_statsmax_memory_allocatedreset_peak_memory_statsrf   r`   rc   ri   rq   rn   rj   r   r   r   )r   Nr   )allpythonr   )rH   )>r   	threadingr   typingr   r   r   r   r   r   _devicer   torch._utilsr	   r
   r   torch.typesr   _utilsr   r3   int	_device_tEventStreamr   r   listtupler   localr)   Lockr*   r0   r   r   boolr   r   r2   r5   r,   rF   rK   rE   rQ   rV   rZ   r`   dictrc   rf   ri   rn   rq   r   rj   r   r   memory__all__r   r   r   <module>r      sq     1 1  + E E  % '3#$	 		  t	(2t8
d3i
'(   ~~' %' 2
( (0f	I 	$d $
d 2+ 2t 2+c +< <
	U8I. 	U& 	U	U8I. 	U& 	U PQEc]E,/EILE	E&+$sCx. +X(9"5 XsCx X'
,v ,	?y 	?T 	? (74 74t!8/0 !] !>%S%,, 67 >V >  @F$S#u||%;<	" r   