
    7hPE                        % S r SSK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0 r\\\4   \S'   0 r\\\4   \S	'    " S
 S5      r " S S5      r " S S\5      r\
\	\/\4      \S'   \R6                  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\\\RJ                  4   S\#\   4S jr&S\\\RJ                  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\R                  R                  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\R                  R                  4S jj5       r\S(S\R                  R                  4S jj5       r\	S(S\R                  R                  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\R                  R                  S\4S$ jj5       r!\	S(S\R                  R                  S\4S% jj5       r"\S(S\R                  R                  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                   J    \ rS rSrS\R
                  R                  4S jrSrg)DeviceInterface.device.   devicec                     [         eNNotImplementedErrorclsr   s     X/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_dynamo/device_interface.py__new__DeviceInterface.device.__new__/   s    %%     N)	__name__
__module____qualname____firstlineno__torchtypesDevicer   __static_attributes__r   r   r   r   r   .   s    	&!3!3 	&r   r   c                       \ rS rSrS rSrg)DeviceInterface.Event2   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__3   s    %k r   r   Nr   r    r!   r"   r   r&   r   r   r   Eventr(   2       	r   r0   c                       \ rS rSrS rSrg)DeviceInterface.Stream8   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__9   s    %m r   r   Nr/   r   r   r   Streamr3   8   r1   r   r7   c                       \ rS rSrSr\S\4S j5       r\S\4S j5       r\S
S\	R                  R                  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_deviceF       %%r   returnc                      [         er   r   r   r   r   current_device%DeviceInterface.Worker.current_deviceJ   r?   r   Nc                     [         er   r   r<   s    r   get_device_properties,DeviceInterface.Worker.get_device_propertiesN   r?   r   r   r   )r   r    r!   r"   __doc__staticmethodintr=   rB   r#   r$   r%   rE   r&   r   r   r   Workerr9   >   sb    	 
	&s 	& 
	& 
	& 	& 
	& 
	&%++*<*< 	& 
	&r   rJ   c                      [         er   r   r   r   r   rB   DeviceInterface.current_deviceR       !!r   c                     [         er   r   r<   s    r   r=   DeviceInterface.set_deviceV   rM   r   r@   c                     [         er   r   r<   s    r   maybe_exchange_device%DeviceInterface.maybe_exchange_deviceZ   rM   r   c                     [         er   r   r<   s    r   exchange_deviceDeviceInterface.exchange_device^   rM   r   c                      [         er   r   r   r   r   device_countDeviceInterface.device_countb   rM   r   c                      [         er   r   r   r   r   is_availableDeviceInterface.is_availablef   rM   r   streamc                     [         er   r   r\   s    r   r\   DeviceInterface.streamj   rM   r   c                      [         er   r   r   r   r   current_streamDeviceInterface.current_streamn   rM   r   c                     [         er   r   r^   s    r   
set_streamDeviceInterface.set_streamr   rM   r   	stream_iddevice_indexdevice_typec                     [         er   r   )rf   rg   rh   s      r   _set_stream_by_id!DeviceInterface._set_stream_by_idv   rM   r   
device_idxc                     [         er   r   rl   s    r   get_raw_streamDeviceInterface.get_raw_streamz   rM   r   Nc                     [         er   r   r<   s    r   synchronizeDeviceInterface.synchronize~   rM   r   c                 8    U R                   R                  U5      $ r   )rJ   rE   r   s     r   rE   %DeviceInterface.get_device_properties   s    zz//77r   c                     [         er   r   r<   s    r   get_compute_capability&DeviceInterface.get_compute_capability   rM   r   including_emulationc                     [         er   r   ry   s    r   is_bf16_supported!DeviceInterface.is_bf16_supported   rM   r   dtypec                 X    U[         R                  :g  =(       d    U R                  U5      $ r   )r#   bfloat16r|   r   r~   ry   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   rM   r   c                     g)zz
Returns True if the device has Triton support, False otherwise, even if
the appropriate Triton backend is not available.
Fr   r<   s    r   is_triton_capable!DeviceInterface.is_triton_capable   s     r   c                 D    U R                  5       (       d  [        S5      eg)a  
Raises a `RuntimeError` with the appropriate human-readable instructions
to resolve the issue if Triton is not available for the given device, or
the default device if `device` is `None`.

The caller should ensure the presence of the 'triton' package before
calling this method.
z/This device is not capable of supporting TritonN)r   RuntimeErrorr   s     r   raise_if_triton_unavailable+DeviceInterface.raise_if_triton_unavailable   s$     $$&&PQQ 'r   r   r   F)%r   r    r!   r"   rG   r   r0   r7   rJ   rH   rB   r#   r$   r%   r=   rI   rQ   rT   rW   boolrZ   r\   ra   rd   rj   ro   rr   classmethodrE   rw   r|   r~   r   r   r   r   r&   r   r   r   r   r   (   s   
& &  & &( " " "5;;-- " " "c "c " " " " " " " " "$ " " "u|| " " " " "5<< " " "S " "# " " "3 "3 " " "EKK.. " " 85;;+=+= 8 8 "u{{'9'9 " " "t " " =BUKKU6:U	U U
 "!3!3 "s " " %++"4"4    
R1C1C 
Rt 
R 
R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   rT   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   rQ   r   )r   r   r   r   s       r   __exit__DeviceGuard.__exit__   s+    88,,BB4==QDHr   r   )r   r    r!   r"   rG   r   r   r   rI   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\R<                  R>                  4S jj5       r \
SS\R<                  R>                  S\4S	 jj5       r!\
SS\R<                  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\R                  R                  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   rB   r   r   r   rB   #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   rJ   rB   r   ranger   rW   rE   r   idevice_props      r   rE   *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"   rH   rI   r=   rB   r#   r$   r%   rE   r&   r   r   r   rJ   r      s^    		<s 	< 
	< 
	/ 	/ 
	/
 
	D%++*<*< 	D 
	Dr   rJ   r@   c                  >    [         R                  R                  5       $ r   )r#   r   rZ   r   r   r   rZ   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_capabilityrE   gcnArchNamesplit)r   majormins      r   rw   $CudaInterface.get_compute_capability  sf    ==$99&AJE2:##::33F;GGMMcSTUVWXXr   c                     [         R                  R                  S L=(       d,    [         R                  R	                  U 5      R
                  S:  $ )N   )r#   r   r   r   rE   r   r<   s    r   r   CudaInterface.is_triton_capable  s>     MMT) Czz//7==B	
r   c                    SSK Jn  [        R                  U 5      (       d;  [        R
                  R                  U 5      nU" U[        R                  " 5       5      eSS K	n[        R                  R                  b&  SUR                  R                  ;  a  [        S5      eg SUR                  R                  ;  a  [        S5      eg )Nr   )GPUTooOldForTritonamdz'triton not built with the 'amd' backendnvidiaz*triton not built with the 'nvidia' backend)torch._inductor.excr   r   r   r#   r   rE   inspectcurrentframetriton.backendsr   r   backendsr   )r   r   device_propstritons       r   r   )CudaInterface.raise_if_triton_unavailable  s    :..v66 ::;;FCL$\73G3G3IJJ==(FOO444"#LMM 5V__555KLL 6r   r   r   )$r   r    r!   r"   r#   r   r   r0   r7   rJ   rH   rB   r=   rW   r\   ra   rd   rj   rr   rE   r	   ro   _exchange_devicerT   _maybe_exchange_devicerQ   r   r|   r   rZ   r$   r%   rw   r   r   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u{{'9'9 Y Y 
%++"4"4 
 
 
 MEKK,>,> M$ M Mr   r   get_xpu_stream)_xpu_getCurrentRawStreamc                   h   \ 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\R:                  R<                  4S jj5       r\
SS	\S\4S
 jj5       r \
SS\R:                  R<                  S\4S jj5       r!\
SS\R:                  R<                  SS4S jj5       r"Sr#g)XpuInterfacei+  c                       \ rS rSr\S\4S j5       r\S\4S j5       r\S	S\R                  R                  4S jj5       rSrg)
XpuInterface.Workeri0  r   c                     U [         S'   g Nxpur   r<   s    r   r=   XpuInterface.Worker.set_device1  s    4:*51r   r@   c                  d    S[         ;   a	  [         S   $ [        R                  R                  5       $ r   )r   r#   r   rB   r   r   r   rB   "XpuInterface.Worker.current_device5  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   rJ   rB   r   r   r   rW   rE   r   s      r   rE   )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   rJ   r   0  s^    		;s 	; 
	; 
	. 	. 
	.
 
	C%++*<*< 	C 
	Cr   rJ   r@   c                  >    [         R                  R                  5       $ r   )r#   r   rZ   r   r   r   rZ   XpuInterface.is_available^  s    yy%%''r   Nr   c                 D    [         R                  R                  U 5      nU$ r   )r#   r   r   )r   ccs     r   rw   #XpuInterface.get_compute_capabilityb  s    YY,,V4	r   ry   c                 >    [         R                  R                  5       $ r   )r#   r   r|   r{   s    r   r|   XpuInterface.is_bf16_supportedg  s    yy**,,r   c                     gNTr   r<   s    r   r   XpuInterface.is_triton_capablek      r   evicec                 V    SS K nSUR                  R                  ;  a  [        S5      eg )Nr   intelz)triton not built with the 'intel' backendr   r   r   )r   r   s     r   r   (XpuInterface.raise_if_triton_unavailableo  s(    &//222JKK 3r   r   r   r   )$r   r    r!   r"   r#   r   r   r0   r7   rJ   rH   rB   r=   rW   r\   ra   rd   rj   rr   rE   r   ro   r   rT   r   rQ   r   r   rZ   r$   r%   rw   r|   r   r   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$>$>? ($ ( ( u{{'9'9   -t - - - %++"4"4    L5;;+=+= L L Lr   r   c                        \ rS rSr% \\S'   Srg)CpuDevicePropertiesiw  multi_processor_countr   N)r   r    r!   r"   rI   __annotations__r&   r   r   r   r   r   w  s    r   r   c                      \ rS rSr " S S\R
                  5      r " S S5      r\S\4S j5       r	\SS\4S	 jj5       r
\SS\R                  R                  S\4S jj5       r\S\4S j5       r\S 5       r\SS\R                  R                  4S jj5       r\SS\R                  R                  S\4S jj5       r\SS\R                  R                  SS
4S jj5       rSrg
)CpuInterfacei|  c                   6    \ rS rSrSS jrS\4S jrS	S jrSrg)
CpuInterface.Eventi}  c                     SU l         g )Ng        time)r   enable_timings     r   r   CpuInterface.Event.__init__~  s	    DIr   r@   c                 :    UR                   U R                   -
  S-  $ )Ni  r  )r   	end_events     r   elapsed_timeCpuInterface.Event.elapsed_time  s    NNTYY.$66r   Nc                 8    [         R                  " 5       U l         g r   )r  perf_counter)r   r\   s     r   recordCpuInterface.Event.record  s    ))+DIr   r  )Tr   )	r   r    r!   r"   r   floatr  r  r&   r   r   r   r0   r  }  s    		7U 	7	,r   r0   c                   X    \ rS rSr\SS\R                  R                  4S jj5       rSr	g)CpuInterface.Workeri  Nr   c                 @    SS K nUR                  5       n[        U5      $ Nr   )multiprocessing	cpu_countr   )r   r  r  s      r   rE   )CpuInterface.Worker.get_device_properties  s    "'113I&y11r   r   r   )
r   r    r!   r"   rH   r#   r$   r%   rE   r&   r   r   r   rJ   r    s%    		2%++*<*< 	2 
	2r   rJ   r@   c                      gr   r   r   r   r   rZ   CpuInterface.is_available  r   r   ry   c                     gr   r   r{   s    r   r|   CpuInterface.is_bf16_supported  r   r   Nr   c                     gN r   r<   s    r   rw   #CpuInterface.get_compute_capability      r   c                     gr  r   rn   s    r   ro   CpuInterface.get_raw_stream      r   c                      gr  r   r   r   r   rB   CpuInterface.current_device  r$  r   c                     g r   r   r<   s    r   rr   CpuInterface.synchronize  s    r   c                     gr   r   r<   s    r   r   CpuInterface.is_triton_capable  r   r   c                 V    SS K nSUR                  R                  ;  a  [        S5      eg )Nr   cpuz'triton not built with the 'cpu' backendr   )r   r   s     r   r   (CpuInterface.raise_if_triton_unavailable  s(    000HII 1r   r   r   r   )r   r    r!   r"   r#   r0   rJ   rH   r   rZ   r|   r$   r%   r   rw   rI   ro   rB   rr   r   r   r&   r   r   r   r  r  |  s.   , ,2 2 $   t   u{{'9'9 S   c     EKK..   %++"4"4    JEKK,>,> J$ J Jr   r  c                   6   \ 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
\R                  R                  S\4S jj5       r\SS
\R                  R                  4S jj5       r " S S5      rSrg	)MpsInterfacei  ry   r@   c                 V    [         R                  R                  R                  SS5      $ )N   r   )r#   r   mpsis_macos_or_newerr{   s    r   r|   MpsInterface.is_bf16_supported  s    ~~!!33B::r   r~   c                     U[         R                  [         R                  4;   a  gU[         R                  :g  =(       d    U R	                  U5      $ r   )r#   float64
complex128r   r|   r   s      r   r   MpsInterface.is_dtype_supported  s>     U]]E$4$455&T#*?*?@S*TTr   c                  R    [         R                  R                  R                  5       $ r   )r#   r   r2  rZ   r   r   r   rZ   MpsInterface.is_available  s    ~~!!..00r   c                      gr  r   r   r   r   rB   MpsInterface.current_device  r$  r   Nr   c                     gr  r   r<   s    r   rw   #MpsInterface.get_compute_capability  r!  r   c                 @    [         R                  R                  5         g r   )r#   r2  rr   r<   s    r   rr   MpsInterface.synchronize  s    		r   c                   h    \ rS rSr\SS\R                  R                  4S jj5       r\S 5       r	Sr
g)MpsInterface.Workeri  Nr   c                     0 $ r   r   r<   s    r   rE   )MpsInterface.Worker.get_device_properties  s    Ir   c                      gr  r   r   r   r   rB   "MpsInterface.Worker.current_device  s    r   r   r   )r   r    r!   r"   rH   r#   r$   r%   rE   rB   r&   r   r   r   rJ   rB    s9    		%++*<*< 	 
	 
	 
	r   rJ   r   r   r   )r   r    r!   r"   rH   r   r|   r   r#   r~   r   rZ   rB   r$   r%   r   rw   rr   rJ   r&   r   r   r   r/  r/    s    ;t ; ; ; =BUKKU6:U	U U 1$ 1 1   u{{'9'9 S    EKK..     r   r/  device_interfacesFr   r   c                 l    [        U [        R                  5      (       a  U R                  n U[        U '   g r   )r   r#   r   r   rG  )r   r   s     r   register_interface_for_devicerI    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_regrG  r   r<   s    r   get_interface_for_devicerM    sO    &%,,''"" ((
 8A
BBr   c                  T    [         (       d
  [        5         [        R                  5       $ r   )rK  rL  rG  itemsr   r   r    get_registered_device_interfacesrP    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:r,  r2  T)rI  r   r   r#   r   rW   r   r   r  r/  rK  )r   s    r   rL  rL    s    !&-85::**,-%aSk=A . "%6599))+,%QCj,? - "%6!%6r   )+rG   r   r  collections.abcr   dataclassesr   typingr   r   r   r   r#   rI   r   r   _is_compiledtorch._Cr
   r	   r   dictr   r   r   r   r   r   r   r   r   r   r  r/  rG  r   rK  r   rI  rM  tuplerP  rL  r   r   r   <module>rY     s  "   $ ! 1 1  (C5#:./ /::EO 46  $sCx. 513 S#X 3CR CRL 6XMO XMv 3%*-. .99CNIL? ILX   4J? 4Jn$? $N 79 4T/223 8 1#u||#$18<_8M1CU3+<%= C$BW C%(5d?>S9S3T*U %r   