
    /j J              	          U d Z ddlZddlZddlmZ ddlmZ ddlZddlmZ ddl	m
Z
mZ ddlmZ ddlmZ d	d
lmZ ej        Zej        ZdZeej        j        df         ed<   dag aeeeg df         ee         f                  ed<    ej                    Z ej                    Z  e            a! e"ej        d          rej        j#        Z$nde%de%fdZ$ e"ej        d          rej        j&        Z'nde%de%fdZ'd Z(d Z)d Z*de+fdZ,dJdZ- G d de.          Z/de+fdZ0de+fdZ1dKdeddfdZ2de%fd Z3de%fd!Z4dKdedefd"Z5dKdedefd#Z6	 dLd&edz  d'ed(e%ddfd)Z7de8eef         fd*Z9d+ee%e%e%e%gdf         ddfd,Z:dMd.e+fd/Z;dKdedee%e%f         fd0Z<dJd1Z=d2efd3Z>deddfd4Z?dKdede8eef         fd5Z@ G d6 d          ZA G d7 d8          ZBd9 ZCd2edz  deBfd:ZDdNdedefd<ZEdee         fd=ZFdNd>ededdfd?ZGd@ee         ddfdAZHdBe%ddfdCZIdBe%ddfdDZJde%fdEZKde%fdFZLde%fdGZMd	dHlNT d	dHlOT g dIZPdS )OzH
This package enables an interface for accessing MTIA backend in python
    N)Callable)Any)Tensor)	is_fbcodeis_prod)_LazySeedTracker)Device   )_get_device_index .default_generatorsF_queued_calls_mtia_exchangeDevicedevicereturnc                 0    | dk     rdS t          d          Nr   z)PyTorch was compiled without MTIA supportRuntimeErrorr   s    X/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/mtia/__init__.py_exchange_devicer   )       A::2FGGG    _mtia_maybeExchangeDevicec                 0    | dk     rdS t          d          r   r   r   s    r   _maybe_exchange_devicer   3   r   r   c                  "    t                       d S N)
_lazy_initr   r   r   initr"   9   s    LLLLLr   c                  .    t           ot                       S )z9Return whether PyTorch's MTIA state has been initialized.)_initialized_is_in_bad_forkr   r   r   is_initializedr&   =   s    1 1 111r   c                    t           5  t                      r |             cd d d            S |                    dd          r-t                              | t          j                               np|                    dd          r-t                              | t          j                               n-t          	                    | t          j                    f           d d d            d S # 1 swxY w Y   d S )Nseed_allFseed)
_initialization_lockr&   get_lazy_seed_trackerqueue_seed_all	tracebackformat_stack
queue_seedr   append)callablekwargss     r   
_lazy_callr4   B   s^   	 
K 
K 		K8::
K 
K 
K 
K 
K 
K 
K 
K
 zz*e,, K"11(I<R<T<TUUUUFE** K"--h	8N8P8PQQQQ$$h	0F0H0H%IJJJ
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
K 
Ks   C-B3C--C14C1c                  >    t           j                                        S r    )torch_C_mtia_isInBadForkr   r   r   r%   r%   P   s    8%%'''r   c            	         t                      st          t          d          rd S t          5  t                      r	 d d d            d S t	                      rt          d          t                      st          d          t                      r:t                      r,	 ddl
m}  |                                  n# t          $ r Y nw xY wt          j                                         dt          _        t$                              d t(                                          D                        	 t$          D ]Z\  }}	  |             # t,          $ r=}dt/          |           d	d
                    |           }t3          |          |d }~ww xY w	 t5          t          d           n# t5          t          d           w xY wdad d d            d S # 1 swxY w 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.dynamic_library` in your python src file and include `//mtia/host_runtime/torch_mtia:torch_mtia` as your target dependency!r   )cxx_resource_managerTc              3      K   | ]}||V  	d S r    r   ).0callss     r   	<genexpr>z_lazy_init.<locals>.<genexpr>   s(      XXuRWXUXXXXXXr   z6MTIA call failed lazily at initialization with error: z(

MTIA call was originally invoked at:

 )r&   hasattr_tlsr*   r%   r   _is_compiledAssertionErrorr   r   libfb.py.cxx_resourcesr;   installModuleNotFoundErrorr6   r7   
_mtia_initr:   r   extendr,   	get_calls	ExceptionstrjoinDeferredMtiaCallErrordelattrr$   )r;   queued_callorig_tracebackemsgs        r   r!   r!   T   s    74):;; 	 7 7  	7 7 7 7 7 7 7 7  	I   ~~ 	 *   ;; 	799 	GGGGGG$,,....&    	  $XX0B0L0L0N0NXXXXXX	-/< < <+^<KMMMM  < < <]QTUVQWQW ] ]CE77>CZCZ] ]  044!;<< D+,,,,GD+,,,,o7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7sy   G
AG!B<;G<
C	GC		A(G2F+?
E
	F+

F8FFF+G+GGGGc                       e Zd ZdS )rN   N)__name__
__module____qualname__r   r   r   rN   rN      s        Dr   rN   c                  >    t           j                                        S )z*Return true if compiled with MTIA support.)r6   r7   _mtia_isBuiltr   r   r   rC   rC      s    8!!###r   c                  F    t                      sdS t                      dk    S )z'Return true if MTIA device is availableFr   )rC   device_countr   r   r   is_availabler\      s"    >> u>>Ar   c                     t           j                            |           5  t           j                                        cddd           S # 1 swxY w Y   dS )z?Waits for all jobs in all streams on a MTIA device to complete.N)r6   mtiar   r7   _mtia_deviceSynchronizer   s    r   synchronizer`      s    			6	"	" 2 2x//112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   AAAc                  >    t           j                                        S )z,Return the number of MTIA devices available.)r6   r7   _mtia_getDeviceCountr   r   r   r[   r[           8((***r   c                  >    t           j                                        S )z0Return the index of a currently selected device.)r6   r7   %_accelerator_hooks_get_current_devicer   r   r   current_devicerf      s    899;;;r   c                 ^    t           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)r6   r7   _mtia_getCurrentStreamr   r   s    r   current_streamrk      (     8**+<Vd+S+S+STTTr   c                 ^    t           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).
    Trh   )r6   r7   _mtia_getDefaultStreamr   r   s    r   default_streamro      rl   r   allpythonenabledstacksmax_entriesc                 h    t                      sdS t          j                            | ||           dS )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&   r6   r7   _mtia_recordMemoryHistory)rr   rs   rt   s      r   record_memory_historyrw      s7      	H&&wDDDDDr   c                  >    t           j                                        S )z4Return a dictionary of MTIA memory allocator history)r6   r7   _mtia_memorySnapshotr   r   r   snapshotrz      rc   r   observerc                 D    t           j                            |            dS )z9Attach an out-of-memory observer to MTIA memory allocatorN)r6   r7   _mtia_attachOutOfMemoryObserver)r{   s    r   attach_out_of_memory_observerr~      s      
H,,X66666r   Tincluding_emulationc                     dS )zLReturn a bool indicating if the current MTIA device supports dtype bfloat16.Tr   )r   s    r   is_bf16_supportedr      s    4r   c                 ^    t           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).
    Trh   )r6   r7   _mtia_getDeviceCapabilityr   r   s    r   get_device_capabilityr      )     8--.?QU.V.V.VWWWr   c                  >    t           j                                        S )zEmpty the MTIA device cache.)r6   r7   _mtia_emptyCacher   r   r   empty_cacher      s    8$$&&&r   streamc                 L    | 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)r6   r7   _mtia_setCurrentStreamr   s    r   
set_streamr     s*     ~	H##F+++++r   c                 r    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   r6   r7   %_accelerator_hooks_set_current_devicer   s    r   
set_devicer     s=     v&&F{{66v>>>>> {r   c                 ^    t           j                            t          | d                    S )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).
    Trh   )r6   r7   _mtia_getDevicePropertiesr   r   s    r   get_device_propertiesr     r   r   c                   8    e Zd ZdZd efdZd Zdededef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                 @    t          |d          | _        d| _        d S )NTrh   r   )r   idxprev_idx)selfr   s     r   __init__zdevice.__init__.  s!    $Vd;;;r   c                 X    t           j                            | j                  | _        d S r    )r6   r7   (_accelerator_hooks_maybe_exchange_devicer   r   )r   s    r   	__enter__zdevice.__enter__2  s    II$(SSr   typevaluer.   c                 X    t           j                            | j                  | _        dS )NF)r6   r7   r   r   r   )r   r   r   r.   s       r   __exit__zdevice.__exit__5  s     8DDT]SSur   N)rU   rV   rW   __doc__r   r   r   r   r   r   r   r   r   &  st         s    T T TS        r   c                   P    e Zd ZU dZedz  ed<   dedz  fdZd Zdeded	ef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.
    N
cur_streamr   c                    d | _         || _        t          d d          | _        t          j                                        s| j        d| _        t          j                                        sd nt          j                            d           | _	        t          j                                        sd nt          j                            d           | _
        d S )NTr   )r   r   r   r   r6   jitis_scriptingr^   ro   src_prev_streamdst_prev_stream)r   r   s     r   r   zStreamContext.__init__H  s    $T400y%%'' 	x 	..00UDDej6O6OPT6U6U 	 	..00UDDej6O6OPT6U6U 	r   c                    | j         }|| j        dk    rd S t          j                            d           | _        | j        j        |j        k    rUt          |j                  5  t          j                            |j                  | _        d d d            n# 1 swxY w Y   t          j                            |           d S Nr   )	r   r   r6   r^   rk   r   r   r   r   )r   r   s     r   r   zStreamContext.__enter__W  s    [
RF$z88>> &**;;;
)** T T',z'@'@AR'S'S$T T T T T T T T T T T T T T T
j)))))s   $*BB!Br   r   r.   c                     | j         }|| j        dk    rd S | j        j        |j        k    r$t          j                            | j                   t          j                            | j                   d S r   )r   r   r   r   r6   r^   r   r   )r   r   r   r.   r   s        r   r   zStreamContext.__exit__f  sl    [
RF &**;;;J!!$"6777
d233333r   )
rU   rV   rW   r   Stream__annotations__r   r   r   r   r   r   r   r   r   :  s         	 	 
v} 
 
 
 
* * *4S 4 4 4 4 4 4 4 4r   r   c                 H    t           j                            | ||           dS )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)r6   r7   _mtia_setStream)	stream_iddevice_indexdevice_types      r   _set_stream_by_idr   t  s$     
HYkBBBBBr   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   r   s    r   r   r     s        r   r^   c                     t                       t          | d          }|t                      }t          |         }|                                S )a[  Returns the random number generator state of the specified MTIA device 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).

    .. warning::
        This function eagerly initializes MTIA.
    Trh   )r!   r   rf   r   	get_state)r   r   default_generators      r   get_rng_stater     sL     LLL
FT
2
2
2C
{*3/&&(((r   c                  P    d t          t                                D             } | S )zRReturns a list of ByteTensor representing the random number states of all devices.c                 ,    g | ]}t          |          S r   )r   )r=   is     r   
<listcomp>z%get_rng_state_all.<locals>.<listcomp>  s     ???A}Q???r   )ranger[   )resultss    r   get_rng_state_allr     s&    ??|~~)>)>???GNr   	new_statec                 >    t                      sVt          j                                        5                       t          j                   ddd           n# 1 swxY w Y   t          |d          t                       fd}t          |           dS )a8  Sets the random number generator state of the specified MTIA device.

    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).
    )memory_formatNTrh   c                  L    t                    } |                                d S r    )r   	set_state)r   r   r   s    r   cbzset_rng_state.<locals>.cb  s(    .s3##I.....r   )	r&   r6   r7   _DisableFuncTorchclonecontiguous_formatr   rf   r4   )r   r   r   r   s   `  @r   set_rng_stater     s      OX'')) 	O 	O "e6MNNI	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O
 FT
2
2
2C
{/ / / / / / rNNNNNs   !AA #A 
new_statesc                 P    t          |           D ]\  }}t          ||           dS )zSets the random number generator state of all devices.

    Args:
        new_states (Iterable of torch.ByteTensor): The desired state for each device.
    N)	enumerater   )r   r   states      r   set_rng_state_allr     s<     j))    5eQ   r   r)   c                 R     t                       fd}t          |d           dS )a  Sets the seed for generating random numbers for the current MTIA device.
    It's safe to call this function if MTIA is not available; in that case, it is silently ignored.

    Args:
        seed (int): The desired seed.

    .. warning::
        If you are working with a multi-GPU model, this function is insufficient
        to get determinism.  To seed all GPUs, use :func:`manual_seed_all`.
    c                  h    t                      } t          |          }|                               d S r    )rf   r   manual_seed)r   r   r)   s     r   r   zmanual_seed.<locals>.cb  s3    .s3%%d+++++r   Tr)   Nintr4   r)   r   s   ` r   r   r     sD     t99D, , , , ,
 rr   c                 R     t                       fd}t          |d           dS )zSets the seed for generating random numbers on all MTIA devices.
    It's safe to call this function if MTIA is not available; in that case, it is silently ignored.

    Args:
        seed (int): The desired seed.
    c                      t          t                                D ]$} t          |          }|                               %d S r    )r   r[   r   r   )r   r   r)   s     r   r   zmanual_seed_all.<locals>.cb  sJ    |~~&& 	0 	0A 21 5))$////	0 	0r   Tr(   Nr   r   s   ` r   manual_seed_allr     sD     t99D0 0 0 0 0
 rD!!!!!!r   c                  *    d } t          | d          S )ax  Sets the seed for generating random numbers to a random number for the current MTIA device.
    It's safe to call this function if MTIA is not available; in that case, it is silently ignored.

    .. warning::
        If you are working with a multi-GPU model, this function will only initialize
        the seed on one GPU.  To initialize all GPUs, use :func:`seed_all`.
    c                  `    t                      } t          |          }|                                S r    )rf   r   r)   )r   r   s     r   r   zseed.<locals>.cb  s*    .s3 %%'''r   Tr   r4   r   s    r   r)   r)     s&    ( ( (
 bt$$$$r   c                  *    d } t          | d          S )zSets the seed for generating random numbers to a random number on all MTIA devices.

    It's safe to call this function if MTIA is not available; in that case, it is silently ignored.
    c                      d} d}t          t                                D ]=}t          |         }|s|                                } d}(|                    |            >| S )Nr   FT)r   r[   r   r)   r   )random_seedseededr   r   s       r   r   zseed_all.<locals>.cb  so    |~~&& 	; 	;A 21 5 ;/4466!--k::::r   Tr   r   r   s    r   r(   r(     s&    
 
 
 b4((((r   c                  x    t                       t                      } t          |                                          S )zReturns the current random seed of the current MTIA device.

    .. warning::
        This function eagerly initializes MTIA.
    )r!   rf   r   initial_seed)r   s    r   r   r     s/     LLL


Cc"//111r   )*)#r"   r\   r&   r`   r[   rf   rk   ro   memory_statsmax_memory_allocatedmemory_allocatedreset_peak_memory_statsr   r   rw   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r)   r(   r   r   	MTIAGraphgraphgraph_pool_handle)r   Nr    )rp   rq   r   )T)r^   )Qr   	threadingr.   collections.abcr   typingr   r6   r   torch._environmentr   r   torch._utilsr   torch.typesr	   _utilsr   Eventr   r   tupler7   	Generatorr   r$   r   listrL   localrB   Lockr*   r,   rA   r   r   r   r   r   r"   r&   r4   boolr%   r!   rK   rN   rC   r\   r`   r[   rf   rk   ro   rw   dictrz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r(   r   memory
mtia_graph__all__r   r   r   <module>r      s             $ $ $ $ $ $              1 1 1 1 1 1 1 1 ) ) ) ) ) )       % % % % % % 		 68 E%(,c12 7 7 7   t	(2t8
d3i
'(    y%y~'' %%''  758+,, Hx4H H H H H H 758011 H"X?Hs Hs H H H H  2 2 2
K K K( ( ( ( (; ; ; ;|	 	 	 	 	I 	 	 	$d $ $ $ $
d    2 2 2$ 2 2 2 2+c + + + +< < < < <
	U 	U6 	UV 	U 	U 	U 	U	U 	U6 	UV 	U 	U 	U 	U MNE E4ZE),EFIE	E E E E&+$sCx. + + + +7S#s+T127	7 7 7 7 4    
X X& XE#s(O X X X X' ' ' '
,v , , , ,	?v 	?$ 	? 	? 	? 	?X X& XDcN X X X X       (74 74 74 74 74 74 74 74tC C C!6D= !] ! ! ! !) )& )f ) ) ) )$4<     V V     2 $v,  4        c d    *"# "$ " " " ""%c % % % %")# ) ) ) )*2c 2 2 2 2        $ $ $r   