
    9i|6              	          % 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"\#" \RH                  S5      (       a  \RH                  RJ                  r&O	S\S\4S jr&\#" \RH                  S5      (       a  \RH                  RN                  r(O	S\S\4S jr(S r)S r*S\+4S jr,S8S jr- " S S\.5      r/S\+4S jr0S\+4S jr1S9S\\   SS4S jjr2S\4S jr3S\4S jr4S9S\\   S\4S jjr5S9S\\   S\4S jjr6 S:S\\   S\S \SS4S! jjr7S\8\\4   4S" jr9S#\\\\\/S4   SS4S$ jr:S;S%\+4S& jjr;S9S\\   S\\\4   4S' jjr<S8S( jr=S)\4S* jr>S\SS4S+ jr?S9S\\   S\8\\4   4S, jjr@ " S- S5      r	 " S. S/5      rAS0 rBS)\S1   S\A4S2 jrCS<S\\\\R                  4   S\4S3 jjrD S<S4\S\\\\R                  4   SS4S5 jjrESS6KF7  / S7QrGg)=zH
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_calls_mtia_exchangeDevicer   returnc                 &    U S:  a  g[        S5      eNr   z)PyTorch was compiled without MTIA supportRuntimeErrorr   s    S/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/mtia/__init__.py_exchange_devicer   %       A:FGG    _mtia_maybeExchangeDevicec                 &    U S:  a  g[        S5      er   r   r   s    r   _maybe_exchange_devicer   /   r   r   c                      [        5         g N)
_lazy_init r   r   initr#   5   s    Lr   c                  :    [         =(       a    [        5       (       + $ )z9Return whether PyTorch's MTIA state has been initialized.)_initialized_is_in_bad_forkr"   r   r   is_initializedr'   9   s    1 111r   c                  >    [         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>d   s     X0NuRWUU0Ns   
	z6MTIA call failed lazily at initialization with error: z(

MTIA call was originally invoked at:

 )r'   hasattr_tls_initialization_lockr&   r   _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!   B   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)r@   u   r"   N)__name__
__module____qualname____firstlineno____static_attributes__r"   r   r   r@   r@   u   s    r   r@   c                  >    [         R                  R                  5       $ )z*Return true if compiled with MTIA support.)r)   r*   _mtia_isBuiltr"   r   r   r7   r7   y   s    88!!##r   c                  <    [        5       (       d  g[        5       S:  $ )z'Return true if MTIA device is availableFr   )r7   device_countr"   r   r   is_availablerQ   ~   s    >>>A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_deviceSynchronizer   s    r   synchronizerU      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   rP   rP           88((**r   c                  >    [         R                  R                  5       $ )z0Return the index of a currently selected device.)r)   r*   %_accelerator_hooks_get_current_devicer"   r   r   current_devicer[      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   r   s    r   current_streamr`      !     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).
Tr]   )r)   r*   _mtia_getDefaultStreamr   r   s    r   default_streamrd      ra   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)re   rf   rg   s      r   record_memory_historyrj      s$     	HH&&wDr   c                  >    [         R                  R                  5       $ )z4Return a dictionary of MTIA memory allocator history)r)   r*   _mtia_memorySnapshotr"   r   r   snapshotrm      rX   r   observerc                 B    [         R                  R                  U 5        g)z9Attach an out-of-memory observer to MTIA memory allocatorN)r)   r*   _mtia_attachOutOfMemoryObserver)rn   s    r   attach_out_of_memory_observerrq      s     
HH,,X6r   including_emulationc                     g)NTr"   )rr   s    r   is_bf16_supportedrt      s    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).
Tr]   )r)   r*   _mtia_getDeviceCapabilityr   r   s    r   get_device_capabilityrw      "     88--.?QU.VWWr   c                  >    [         R                  R                  5       $ )zEmpty the MTIA device cache.)r)   r*   _mtia_emptyCacher"   r   r   empty_cacher{      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r|   s    r   
set_streamr      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_devicer   s    r   
set_devicer      s,     v&F{66v> r   c                 P    [         R                  R                  [        U SS95      $ )zReturn a dictionary of MTIA device properties

Args:
    device (torch.device or int, optional) selected device. Returns
        statistics for the current device, given by current_device(),
        if device is None (default).
Tr]   )r)   r*   _mtia_getDevicePropertiesr   r   s    r   get_device_propertiesr      rx   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   i  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 )NTr]   r   )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_devicer   r   )r   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   r   r   )r   r   r   r   s       r   __exit__device.__exit__  s    88DDT]]Sr   )r   r   N)
rH   rI   rJ   rK   __doc__r   r   r   r   rL   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)StreamContexti  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_streamr|   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 )NTr   )r   r|   r   r   r)   jitis_scriptingrS   rd   src_prev_streamdst_prev_stream)r   r|   s     r   r   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Nr   )	r|   r   r)   rS   r`   r   r   r   r   )r   r   s     r   r   StreamContext.__enter__9  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   )r|   r   r   r   r)   rS   r   r   )r   r   r   r   r   s        r   r   StreamContext.__exit__H  sj    [[
R &&**;*;;JJ!!$"6"67

d223r   )r   r   r   r   r|   N)rH   rI   rJ   rK   r   r   __annotations__r   r   r   r   rL   r"   r   r   r   r     sF    	 ,--
x(;< 
*4S 4 4 4r   r   c                 D    [         R                  R                  XU5        g)zset stream specified by the stream id, device index and
    device type

Args: stream_id (int): stream id in stream pool
      device_index (int): device index in topo
      device_type (int): enum device type
N)r)   r*   _mtia_setStream)	stream_iddevice_indexdevice_types      r   _set_stream_by_idr   V  s     
HHYkBr   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   r   s    r   r|   r|   a  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uint8r   s    r   get_rng_stater   l  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   {  s     MM8r   )*)r#   rQ   r'   rU   rP   r[   r`   rd   memory_statsmax_memory_allocatedmemory_allocatedreset_peak_memory_statsrw   r   rj   rm   rq   r{   r   r   r|   r   r   r   rt   )r   Nr    )allpythonr   )T)rS   )Hr   	threadingr   typingr   r   r   r   r)   r   _devicer   torch._utilsr	   r
   r   torch.typesr   _utilsr   r>   int	_device_tEventStreamr%   r   listtupler   localr5   Lockr6   r;   r4   r*   r   r   r   r   r#   r'   boolr&   r!   r=   r@   r7   rQ   rU   rP   r[   r`   rd   rj   dictrm   rq   rt   rw   r{   r   r   r   r   r   r|   r   r   memory__all__r"   r   r   <module>r      sB     1 1  + E E  % '3#$	 		  t	(2t8
d3i
'(   ~~' %'  588+,,xx44H H H 588011"XX??Hs Hs H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. +7S#s+T127	74 X(9"5 XsCx X'
,v ,	?y 	?T 	?X(9"5 Xc3h X (74 74tC!8/0 !] !>%S%,, 67 >V >  @F$S#u||%;<	" r   