
    [Tha:                        % S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	J
r
  SSKr\	\\/\4      \S'   \R                  R                  5       (       a  SSKJr  OSr\
\R&                  \\S4   r0 r\\\4   \S'   0 r\\\4   \S	'    " S
 S5      r " S S5      r " S S\5      r\	\\/\4      \S'   \R8                  R                  5       (       a  SSKJr  OSr " S S\5      r\ " S S5      5       r  " S S\5      r! " S S\5      r"0 r#\\\$\   4   \S'   Sq%S\
\\R&                  4   S\$\   4S jr&S\
\\R&                  4   S\$\   4S  jr'S\\(\\$\   4      4S! jr)S" r*g)#a  
Device abstraction layer for TorchDynamo and Inductor backends.

This module provides a unified interface for different hardware backends (CUDA, XPU,
CPU, MPS) through a common device interface. Key components include:

- DeviceInterface: Base class defining the common API for all device types
- Device-specific implementations: CudaInterface, XpuInterface, CpuInterface, MpsInterface
- Device registration system for managing available backends
- Worker APIs for multi-processing scenarios
- Stream and event management across different devices
- Device property caching for worker processes

The abstraction layer enables device-agnostic code in TorchDynamo while allowing
specialized implementations for each hardware backend's unique features.
    N)Iterable)	dataclass)AnyCallableOptionalUnionget_cuda_stream)_cuda_getCurrentRawStream caching_worker_device_propertiescaching_worker_current_devicesc                   ^   \ rS rSrSr " S S5      r " S S5      r " S S5      r " S	 S
5      r\	S 5       r
\	S\4S j5       r\	S\S\4S j5       r\	S\S\4S j5       r\	S 5       r\	S\4S j5       r\	S\R                  4S j5       r\	S 5       r\	S\R                  4S j5       r\	S\S\S\4S j5       r\	S\S\4S j5       r\	S&S\4S jj5       r\S&S\4S jj5       r\	S&S\4S jj5       r\	S'S \4S! jj5       r\ S'S"\R<                  S \S\4S# jj5       r\	S&S\S\4S$ jj5       r S%r!g)(DeviceInterface)   z
This is a simple device runtime interface for Inductor. It enables custom
backends to be integrated with Inductor in a device-agnostic semantic.
c                   "    \ rS rSrS\4S jrSrg)DeviceInterface.device/   devicec                     [         eNNotImplementedErrorclsr   s     V/var/www/auris/envauris/lib/python3.13/site-packages/torch/_dynamo/device_interface.py__new__DeviceInterface.device.__new__0   s    %%     N)__name__
__module____qualname____firstlineno__	_device_tr   __static_attributes__r   r   r   r   r   /   s    	& 	&r   r   c                       \ rS rSrS rSrg)DeviceInterface.Event3   c                     [        S5      e)NzYEvent should be inherited from torch.Event, otherwise, it couldn't be captured by dynamo.r   r   argskwargss      r   r   DeviceInterface.Event.__new__4   s    %k r   r   Nr   r    r!   r"   r   r$   r   r   r   Eventr&   3       	r   r.   c                       \ rS rSrS rSrg)DeviceInterface.Stream9   c                     [        S5      e)Nz[Stream should be inherited from torch.Stream, otherwise, it couldn't be captured by dynamo.r   r)   s      r   r   DeviceInterface.Stream.__new__:   s    %m r   r   Nr-   r   r   r   Streamr1   9   r/   r   r5   c                   d    \ rS rSrSr\S\4S j5       r\S\4S j5       r\S
S\	4S jj5       r
S	rg)DeviceInterface.Worker?   z
Worker API to query device properties that will work in multi processing
workers that cannot use the GPU APIs (due to processing fork() and
initialization time issues). Properties are recorded in the main process
before we fork the workers.
r   c                     [         er   r   r   s    r   
set_device!DeviceInterface.Worker.set_deviceG       %%r   returnc                      [         er   r   r   r   r   current_device%DeviceInterface.Worker.current_deviceK   r=   r   Nc                     [         er   r   r:   s    r   get_device_properties,DeviceInterface.Worker.get_device_propertiesO   r=   r   r   r   )r   r    r!   r"   __doc__staticmethodintr;   r@   r#   rC   r$   r   r   r   Workerr7   ?   sX    	 
	&s 	& 
	& 
	& 	& 
	& 
	&) 	& 
	&r   rH   c                      [         er   r   r   r   r   r@   DeviceInterface.current_deviceS       !!r   c                     [         er   r   r:   s    r   r;   DeviceInterface.set_deviceW   rK   r   r>   c                     [         er   r   r:   s    r   maybe_exchange_device%DeviceInterface.maybe_exchange_device[   rK   r   c                     [         er   r   r:   s    r   exchange_deviceDeviceInterface.exchange_device_   rK   r   c                      [         er   r   r   r   r   device_countDeviceInterface.device_countc   rK   r   c                      [         er   r   r   r   r   is_availableDeviceInterface.is_availableg   rK   r   streamc                     [         er   r   rZ   s    r   rZ   DeviceInterface.streamk   rK   r   c                      [         er   r   r   r   r   current_streamDeviceInterface.current_streamo   rK   r   c                     [         er   r   r\   s    r   
set_streamDeviceInterface.set_streams   rK   r   	stream_iddevice_indexdevice_typec                     [         er   r   )rd   re   rf   s      r   _set_stream_by_id!DeviceInterface._set_stream_by_idw   rK   r   
device_idxc                     [         er   r   rj   s    r   get_raw_streamDeviceInterface.get_raw_stream{   rK   r   Nc                     [         er   r   r:   s    r   synchronizeDeviceInterface.synchronize   rK   r   c                 8    U R                   R                  U5      $ r   )rH   rC   r   s     r   rC   %DeviceInterface.get_device_properties   s    zz//77r   c                     [         er   r   r:   s    r   get_compute_capability&DeviceInterface.get_compute_capability   rK   r   including_emulationc                     [         er   r   rw   s    r   is_bf16_supported!DeviceInterface.is_bf16_supported   rK   r   dtypec                 X    U[         R                  :g  =(       d    U R                  U5      $ r   )torchbfloat16rz   r   r|   rw   s      r   is_dtype_supported"DeviceInterface.is_dtype_supported   s$     &T#*?*?@S*TTr   c                     [         er   r   r:   s    r   memory_allocated DeviceInterface.memory_allocated   rK   r   r   r   F)"r   r    r!   r"   rE   r   r.   r5   rH   rF   r@   r#   r;   rG   rO   rR   rU   boolrX   r~   rZ   r_   rb   rh   rm   rp   classmethodrC   ru   rz   r|   r   r   r$   r   r   r   r   r   )   s4   
& &  & &( " " "9 " " "c "c " " " " " " " " "$ " " "u|| " " " " "5<< " " "S " "# " " "3 "3 " " "I " " 89 8 8 "y " " "t " " =BUKKU6:U	U U
 " "c " "r   r   c                   V    \ 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)DeviceGuard   aG  
This class provides a context manager for device switching. This is a stripped
down version of torch.{device_name}.device.

The context manager changes the current device to the given device index
on entering the context and restores the original device on exiting.
The device is switched using the provided device interface.
device_interfaceindexr>   Nc                 *    Xl         X l        SU l        g )Nr   idxprev_idx)selfr   r   s      r   __init__DeviceGuard.__init__   s     !1r   c                 t    U R                   b+  U R                  R                  U R                   5      U l        g g r   )r   r   rR   r   )r   s    r   	__enter__DeviceGuard.__enter__   s-    88 11AA$((KDM  r   typevalue	tracebackc                 r    U R                   b*  U R                  R                  U R                  5      U l         gNF)r   r   rO   r   )r   r   r   r   s       r   __exit__DeviceGuard.__exit__   s+    88,,BB4==QDHr   r   )r   r    r!   r"   rE   r   r   r   rG   r   r   r   r   r$   r   r   r   r   r      sM     $_ 5>Fsm	LS   r   r   c                      \ rS rSr\R
                  R                  r\R
                  R                  r\R
                  R                  r " S S5      r	\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                   5      r\
" \R
                  R"                  5      r\
" \R
                  R$                  5      r\
" \R
                  R&                  5      r\
" \5      r\
" \R
                  R,                  5      r\
" \R
                  R0                  5      r\
" \R
                  R4                  5      r\
" \R
                  R6                  5      r\
S\4S j5       r\
S
S\4S jj5       rS	r g)CudaInterface   c                   `    \ rS rSr\S\4S j5       r\S\4S j5       r\S	S\4S jj5       r	Sr
g)
CudaInterface.Worker   r   c                     U [         S'   g Ncudar   r:   s    r   r;   CudaInterface.Worker.set_device   s    5;*62r   r>   c                  d    S[         ;   a	  [         S   $ [        R                  R                  5       $ r   )r   r~   r   r@   r   r   r   r@   #CudaInterface.Worker.current_device   s)    775f==::,,..r   Nc                 
   U bh  [        U [        5      (       a(  [        R                  " U 5      n U R                  S:X  d   e[        U [        R                  5      (       a  U R
                  n U c  [        R                  R                  5       n S[        ;  a]  [        [        R                  R                  5       5       Vs/ s H"  n[        R                  R                  U5      PM$     nnU[        S'   [        S   U    $ s  snf r   )
isinstancestrr~   r   r   r   r   rH   r@   r   ranger   rU   rC   r   idevice_props      r   rC   *CudaInterface.Worker.get_device_properties   s    !fc**"\\&1F!;;&000fell33#\\F~&--<<>== #5::#:#:#<== JJ44Q7=   <G083F;FCC    )D r   r   r   r    r!   r"   rF   rG   r;   r@   r#   rC   r$   r   r   r   rH   r      sT    		<s 	< 
	< 
	/ 	/ 
	/
 
	D) 	D 
	Dr   rH   r>   c                  >    [         R                  R                  5       $ r   )r~   r   rX   r   r   r   rX   CudaInterface.is_available   s    zz&&((r   Nr   c                    [         R                  R                  c)  [         R                  R	                  U 5      u  pUS-  U-   $ [         R                  R                  U 5      R                  R                  SS5      S   $ )N
   :   r   )r~   versionhipr   get_device_capabilityrC   gcnArchNamesplit)r   majormins      r   ru   $CudaInterface.get_compute_capability   sf    ==$99&AJE2:##::33F;GGMMcSTUVWXXr   r   r   )!r   r    r!   r"   r~   r   r   r.   r5   rH   rF   r@   r;   rU   rZ   r_   rb   rh   rp   rC   r	   rm   _exchange_devicerR   _maybe_exchange_devicerO   r   rz   r   rX   r#   ru   r$   r   r   r   r   r      s   ZZF JJEZZFD D> "%**";";<Nejj334J

 7 78L%**++,F!%**";";<Nejj334J$UZZ%A%ABuzz556K()I)IJ!/2N"5::#>#>?O()J)JK#EJJ$?$?@$UZZ%A%AB )$ ) ) Yy Y Yr   r   get_xpu_stream)_xpu_getCurrentRawStreamc                      \ rS rSr\R
                  R                  r\R
                  R                  r\R
                  R                  r " S S5      r	\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                   5      r\
" \R
                  R"                  5      r\
" \R
                  R$                  5      r\
" \R
                  R&                  5      r\
" \5      r\
" \R
                  R,                  5      r\
" \R
                  R0                  5      r\
" \R
                  R4                  5      r\
S\4S j5       r\
SS\4S jj5       r\
SS	\S\4S
 jj5       rSr g)XpuInterface   c                   `    \ rS rSr\S\4S j5       r\S\4S j5       r\S	S\4S jj5       r	Sr
g)
XpuInterface.Workeri  r   c                     U [         S'   g Nxpur   r:   s    r   r;   XpuInterface.Worker.set_device  s    4:*51r   r>   c                  d    S[         ;   a	  [         S   $ [        R                  R                  5       $ r   )r   r~   r   r@   r   r   r   r@   "XpuInterface.Worker.current_device
  s)    665e<<99++--r   Nc                 
   U bh  [        U [        5      (       a(  [        R                  " U 5      n U R                  S:X  d   e[        U [        R                  5      (       a  U R
                  n U c  [        R                  R                  5       n S[        ;  a]  [        [        R                  R                  5       5       Vs/ s H"  n[        R                  R                  U5      PM$     nnU[        S'   [        S   U    $ s  snf r   )r   r   r~   r   r   r   r   rH   r@   r   r   r   rU   rC   r   s      r   rC   )XpuInterface.Worker.get_device_properties  s    !fc**"\\&1F!;;%///fell33#\\F~%,,;;=<< #599#9#9#;<< II33A6<   ;F073E:6BBr   r   r   r   r   r   r   rH   r     sT    		;s 	; 
	; 
	. 	. 
	.
 
	C) 	C 
	Cr   rH   r>   c                  >    [         R                  R                  5       $ r   )r~   r   rX   r   r   r   rX   XpuInterface.is_available3  s    yy%%''r   Nr   c                 D    [         R                  R                  U 5      nU$ r   )r~   r   r   )r   ccs     r   ru   #XpuInterface.get_compute_capability7  s    YY,,V4	r   rw   c                 >    [         R                  R                  5       $ r   )r~   r   rz   ry   s    r   rz   XpuInterface.is_bf16_supported<  s    yy**,,r   r   r   r   )!r   r    r!   r"   r~   r   r   r.   r5   rH   rF   r@   r;   rU   rZ   r_   rb   rh   rp   rC   r   rm   r   rR   r   rO   r   r   rX   r#   ru   rz   r$   r   r   r   r   r      s   YYFIIOOEYYFC C> "%))":":;Neii223J		 6 67L%))**+F!%))":":;Neii223J$UYY%@%@Auyy445K()H)HI!.1N"599#=#=>O()I)IJ#EII$>$>? ($ ( ( y   -t - - -r   r   c                        \ rS rSr% \\S'   Srg)CpuDevicePropertiesiA  multi_processor_countr   N)r   r    r!   r"   rG   __annotations__r$   r   r   r   r   r   A  s    r   r   c                       \ rS rSr " S S\R
                  5      r\S\4S j5       r\SS\4S jj5       r	\SS	\
S\4S
 jj5       r\S\4S j5       r\S 5       r\SS	\
4S jj5       r " S S5      rSrg)CpuInterfaceiF  c                   6    \ rS rSrSS jrS\4S jrS	S jrSrg)
CpuInterface.EventiG  c                     SU l         g )Ng        time)r   enable_timings     r   r   CpuInterface.Event.__init__H  s	    DIr   r>   c                 :    UR                   U R                   -
  S-  $ )Ni  r   )r   	end_events     r   elapsed_timeCpuInterface.Event.elapsed_timeK  s    NNTYY.$66r   Nc                 8    [         R                  " 5       U l         g r   )r   perf_counter)r   rZ   s     r   recordCpuInterface.Event.recordN  s    ))+DIr   r   )Tr   )	r   r    r!   r"   r   floatr   r   r$   r   r   r   r.   r   G  s    		7U 	7	,r   r.   r>   c                      gNTr   r   r   r   rX   CpuInterface.is_availableQ      r   rw   c                     gr   r   ry   s    r   rz   CpuInterface.is_bf16_supportedU  r   r   Nr   c                     gN r   r:   s    r   ru   #CpuInterface.get_compute_capabilityY      r   c                     gNr   r   rl   s    r   rm   CpuInterface.get_raw_stream]      r   c                      gr   r   r   r   r   r@   CpuInterface.current_devicea  r  r   c                     g r   r   r:   s    r   rp   CpuInterface.synchronizee  s    r   c                   0    \ rS rSr\SS\4S jj5       rSrg)CpuInterface.Workerii  Nr   c                 @    SS K nUR                  5       n[        U5      $ r   )multiprocessing	cpu_countr   )r   r	  r
  s      r   rC   )CpuInterface.Worker.get_device_propertiesj  s    "'113I&y11r   r   r   )r   r    r!   r"   rF   r#   rC   r$   r   r   r   rH   r  i  s    		2) 	2 
	2r   rH   r   r   r   )r   r    r!   r"   r~   r.   rF   r   rX   rz   r#   r   ru   rG   rm   r@   rp   rH   r$   r   r   r   r   r   F  s    , , $   t   y C   c     I  2 2r   r   c                       \ rS rSr\SS\S\4S jj5       r\ SS\R                  S\S\4S jj5       r
\S\4S j5       r\S 5       r\SS
\S\4S jj5       r\SS
\4S jj5       r " S S5      rSrg	)MpsInterfaceir  rw   r>   c                 V    [         R                  R                  R                  SS5      $ )N   r   )r~   backendsmpsis_macos_or_newerry   s    r   rz   MpsInterface.is_bf16_supporteds  s    ~~!!33B::r   r|   c                     U[         R                  :X  a  gU[         R                  :g  =(       d    U R                  U5      $ r   )r~   float64r   rz   r   s      r   r   MpsInterface.is_dtype_supportedw  s3     EMM!&T#*?*?@S*TTr   c                  R    [         R                  R                  R                  5       $ r   )r~   r  r  rX   r   r   r   rX   MpsInterface.is_available  s    ~~!!..00r   c                      gr   r   r   r   r   r@   MpsInterface.current_device  r  r   Nr   c                     gr   r   r:   s    r   ru   #MpsInterface.get_compute_capability  r   r   c                 @    [         R                  R                  5         g r   )r~   r  rp   r:   s    r   rp   MpsInterface.synchronize  s    		r   c                   @    \ rS rSr\SS\4S jj5       r\S 5       rSrg)MpsInterface.Workeri  Nr   c                     0 $ r   r   r:   s    r   rC   )MpsInterface.Worker.get_device_properties  s    Ir   c                      gr   r   r   r   r   r@   "MpsInterface.Worker.current_device  s    r   r   r   )	r   r    r!   r"   rF   r#   rC   r@   r$   r   r   r   rH   r     s/    		) 	 
	 
	 
	r   rH   r   r   r   )r   r    r!   r"   rF   r   rz   r   r~   r|   r   rX   r@   r#   r   ru   rp   rH   r$   r   r   r   r  r  r  s    ;t ; ; ; =BUKKU6:U	U U 1$ 1 1   y C    I     r   r  device_interfacesFr   r   c                 l    [        U [        R                  5      (       a  U R                  n U[        U '   g r   )r   r~   r   r   r%  )r   r   s     r   register_interface_for_devicer'    s)     &%,,'' 0fr   r>   c                     [        U [        R                  5      (       a  U R                  n [        (       d
  [        5         U [        ;   a	  [        U    $ [        SU  35      e)NzNo interface for device )r   r~   r   r   _device_initializedinit_device_regr%  r   r:   s    r   get_interface_for_devicer+    sO    &%,,''"" ((
 8A
BBr   c                  T    [         (       d
  [        5         [        R                  5       $ r   )r)  r*  r%  itemsr   r   r    get_registered_device_interfacesr.    s    ""$$r   c                     [        S[        5        [        [        R                  R                  5       5       H  n [        SU  3[        5        M     [        S[        5        [        [        R                  R                  5       5       H  n [        SU  3[        5        M     [        S[        5        [        S[        5        Sq
g )Nr   zcuda:r   zxpu:cpur  T)r'  r   r   r~   r   rU   r   r   r   r  r)  )r   s    r   r*  r*    s    !&-85::**,-%aSk=A . "%6599))+,%QCj,? - "%6!%6r   )+rE   r   collections.abcr   dataclassesr   typingr   r   r   r   r~   rG   r   r   _is_compiledtorch._Cr
   r	   r   r   r#   r   dictr   r   r   r   r   r   r   r   r   r   r  r%  r   r)  r'  r+  tupler.  r*  r   r   r   <module>r8     s  "  $ ! 1 1  (C5#:./ /::EO%,,S$./	 46  $sCx. 513 S#X 3n" n"b 6AYO AYH 3%*-. .99CN>-? >-B   )2? )2X$? $N 79 4T/223 8 1#u||#$18<_8M1CU3+<%= C$BW C%(5d?>S9S3T*U %r   