
    rhPE                        U d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlZe
e	egef      ed<   ej                  j!                         rddlmZ ndZi Zeeef   ed<   i Zeeef   ed	<    G d
 d      Z G d d      Z G d de      Ze
e	egef      ed<   ej6                  j!                         rddlmZ ndZ G d de      Ze G d d             Z G d de      Z  G d de      Z!i Z"eee#e   f   ed<   da$deeejJ                  f   de#e   fdZ&deeejJ                  f   de#e   fd Z'dee(ee#e   f      fd!Z)d" Z*y)#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                   ~   e Zd ZdZ G d d      Z G d d      Z G d d      Z G d d	      Zed
        Z	ede
j                  j                  fd       Zededefd       Zededefd       Zed        Zedefd       Zede
j                  fd       Zed        Zede
j                  fd       Zedededefd       Zededefd       Zed&de
j                  j                  fd       Zed&de
j                  j                  fd       Zed&de
j                  j                  fd       Zed'defd        Ze	 d'd!e
j<                  dedefd"       Zed&de
j                  j                  defd#       Z ed&de
j                  j                  defd$       Z!ed&de
j                  j                  ddfd%       Z"y)(DeviceInterfacez
    This is a simple device runtime interface for Inductor. It enables custom
    backends to be integrated with Inductor in a device-agnostic semantic.
    c                   @    e Zd Zdej                  j
                  fdZy)DeviceInterface.devicedevicec                     t         NNotImplementedErrorclsr   s     q/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/device_interface.py__new__zDeviceInterface.device.__new__/   s    %%    N)__name__
__module____qualname__torchtypesDevicer    r   r   r   r   .   s    	&!3!3 	&r   r   c                       e Zd Zd Zy)DeviceInterface.Eventc                     t        d      )NzYEvent should be inherited from torch.Event, otherwise, it couldn't be captured by dynamo.r   r   argskwargss      r   r   zDeviceInterface.Event.__new__3   s    %k r   Nr   r   r   r   r!   r   r   Eventr#   2       	r   r)   c                       e Zd Zd Zy)DeviceInterface.Streamc                     t        d      )Nz[Stream should be inherited from torch.Stream, otherwise, it couldn't be captured by dynamo.r   r%   s      r   r   zDeviceInterface.Stream.__new__9   s    %m r   Nr(   r!   r   r   Streamr,   8   r*   r   r.   c                   |    e Zd ZdZedefd       Zedefd       Zeddej                  j                  fd       Zy)	DeviceInterface.Workera  
        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                     t         r   r   r   s    r   
set_devicez!DeviceInterface.Worker.set_deviceF       %%r   returnc                      t         r   r   r!   r   r   current_devicez%DeviceInterface.Worker.current_deviceJ   r4   r   Nc                     t         r   r   r2   s    r   get_device_propertiesz,DeviceInterface.Worker.get_device_propertiesN   r4   r   r   )r   r   r   __doc__staticmethodintr3   r7   r   r   r    r9   r!   r   r   Workerr0   >   sb    	 
	&s 	& 
	& 
	& 	& 
	& 
	&%++*<*< 	& 
	&r   r=   c                      t         r   r   r!   r   r   r7   zDeviceInterface.current_deviceR       !!r   c                     t         r   r   r2   s    r   r3   zDeviceInterface.set_deviceV   r?   r   r5   c                     t         r   r   r2   s    r   maybe_exchange_devicez%DeviceInterface.maybe_exchange_deviceZ   r?   r   c                     t         r   r   r2   s    r   exchange_devicezDeviceInterface.exchange_device^   r?   r   c                      t         r   r   r!   r   r   device_countzDeviceInterface.device_countb   r?   r   c                      t         r   r   r!   r   r   is_availablezDeviceInterface.is_availablef   r?   r   streamc                     t         r   r   rI   s    r   rI   zDeviceInterface.streamj   r?   r   c                      t         r   r   r!   r   r   current_streamzDeviceInterface.current_streamn   r?   r   c                     t         r   r   rK   s    r   
set_streamzDeviceInterface.set_streamr   r?   r   	stream_iddevice_indexdevice_typec                     t         r   r   )rP   rQ   rR   s      r   _set_stream_by_idz!DeviceInterface._set_stream_by_idv   r?   r   
device_idxc                     t         r   r   rU   s    r   get_raw_streamzDeviceInterface.get_raw_streamz   r?   r   Nc                     t         r   r   r2   s    r   synchronizezDeviceInterface.synchronize~   r?   r   c                 8    | j                   j                  |      S r   )r=   r9   r   s     r   r9   z%DeviceInterface.get_device_properties   s    zz//77r   c                     t         r   r   r2   s    r   get_compute_capabilityz&DeviceInterface.get_compute_capability   r?   r   including_emulationc                     t         r   r   r^   s    r   is_bf16_supportedz!DeviceInterface.is_bf16_supported   r?   r   dtypec                 N    |t         j                  k7  xs | j                  |      S r   )r   bfloat16ra   r   rb   r^   s      r   is_dtype_supportedz"DeviceInterface.is_dtype_supported   s$     &T#*?*?@S*TTr   c                     t         r   r   r2   s    r   memory_allocatedz DeviceInterface.memory_allocated   r?   r   c                      y)z
        Returns True if the device has Triton support, False otherwise, even if
        the appropriate Triton backend is not available.
        Fr!   r2   s    r   is_triton_capablez!DeviceInterface.is_triton_capable   s     r   c                 :    | j                         st        d      y)aH  
        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)rj   RuntimeErrorr   s     r   raise_if_triton_unavailablez+DeviceInterface.raise_if_triton_unavailable   s!     $$&PQQ 'r   r   F)#r   r   r   r:   r   r)   r.   r=   r;   r7   r   r   r    r3   r<   rB   rD   rF   boolrH   rI   rM   rO   rT   rX   rZ   classmethodr9   r]   ra   rb   rf   rh   rj   rm   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                   J    e Zd ZdZdee   dee   ddfdZd Z	de
d	e
d
e
fdZy)DeviceGuarda_  
    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indexr5   Nc                 .    || _         || _        d| _        y )N)rs   idxprev_idx)selfrs   rt   s      r   __init__zDeviceGuard.__init__   s     !1r   c                 r    | j                   +| j                  j                  | j                         | _        y y r   )rw   rs   rD   rx   )ry   s    r   	__enter__zDeviceGuard.__enter__   s-    88 11AA$((KDM  r   typevalue	tracebackc                 p    | j                   *| j                  j                  | j                        | _         yNF)rw   rs   rB   rx   )ry   r}   r~   r   s       r   __exit__zDeviceGuard.__exit__   s+    88,,BB4==QDHr   )r   r   r   r:   r}   r   r   r<   rz   r|   r   r   r!   r   r   rr   rr      sM     $_ 5>Fsm	LS   r   rr   c                   j   e Zd Zej                  j
                  Zej                  j                  Zej                  j                  Z G d d      Z e	ej                  j                        Z
 e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                         Z e	ej                  j"                        Z e	ej                  j$                        Z e	e      Z e	ej                  j*                        Z e	ej                  j.                        Z e	ej                  j2                        Z e	ej                  j4                        Ze	defd       Ze	d
dej:                  j<                  fd       Ze	d
dej:                  j<                  defd       Z e	d
dej:                  j<                  ddfd	       Z!y)CudaInterfacec                   x    e Zd Zedefd       Zedefd       Zeddej                  j                  fd       Z
y)CudaInterface.Workerr   c                     | t         d<   y Ncudar   r2   s    r   r3   zCudaInterface.Worker.set_device   s    5;*62r   r5   c                  `    dt         v r	t         d   S t        j                  j                         S r   )r   r   r   r7   r!   r   r   r7   z#CudaInterface.Worker.current_device   s)    775f==::,,..r   Nc                    | \t        | t              r&t        j                  |       } | j                  dk(  sJ t        | t        j                        r| j
                  } | t        j                  j                         } dt        vrZt        t        j                  j                               D cg c]!  }t        j                  j                  |      # }}|t        d<   t        d   |    S c c}w r   )
isinstancestrr   r   r}   rt   r   r=   r7   r   ranger   rF   r9   r   idevice_props      r   r9   z*CudaInterface.Worker.get_device_properties   s    !fc*"\\&1F!;;&000fell3#\\F~&--<<>== #5::#:#:#<= JJ44Q7  <G083F;FCC   0&C-r   r   r   r   r;   r<   r3   r7   r   r   r    r9   r!   r   r   r=   r      s^    		<s 	< 
	< 
	/ 	/ 
	/
 
	D%++*<*< 	D 
	Dr   r=   r5   c                  >    t         j                  j                         S r   )r   r   rH   r!   r   r   rH   zCudaInterface.is_available   s    zz&&((r   Nr   c                    t         j                  j                  *t         j                  j	                  |       \  }}|dz  |z   S t         j                  j                  |       j                  j                  dd      d   S )N
   :   r   )r   versionhipr   get_device_capabilityr9   gcnArchNamesplit)r   majormins      r   r]   z$CudaInterface.get_compute_capability  sh    ==$99&AJE32:##::33F;GGMMcSTUVWXXr   c                     t         j                  j                  d uxs, t         j                  j	                  |       j
                  dk\  S )N   )r   r   r   r   r9   r   r2   s    r   rj   zCudaInterface.is_triton_capable  s>     MMT) Czz//7==B	
r   c                 x   ddl m} t        j                  |       s:t        j
                  j                  |       } ||t        j                               dd l	}t        j                  j                  $d|j                  j                  vrt        d      y d|j                  j                  vrt        d      y )Nr   )GPUTooOldForTritonamdz'triton not built with the 'amd' backendnvidiaz*triton not built with the 'nvidia' backend)torch._inductor.excr   r   rj   r   r   r9   inspectcurrentframetriton.backendsr   r   backendsrl   )r   r   device_propstritons       r   rm   z)CudaInterface.raise_if_triton_unavailable  s    :..v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.   r=   r;   r7   r3   rF   rI   rM   rO   rT   rZ   r9   r	   rX   _exchange_devicerD   _maybe_exchange_devicerB   rh   ra   ro   rH   r   r    r]   rj   rm   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                   N   e Zd Zej                  j
                  Zej                  j                  Zej                  j                  Z G d d      Z e	ej                  j                        Z
 e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                        Z e	ej                  j                         Z e	ej                  j"                        Z e	ej                  j$                        Z e	e      Z e	ej                  j*                        Z e	ej                  j.                        Z e	ej                  j2                        Ze	defd       Ze	ddej8                  j:                  fd       Ze	ddedefd	       Ze	ddej8                  j:                  defd
       Z e	ddej8                  j:                  ddfd       Z!y)XpuInterfacec                   x    e Zd Zedefd       Zedefd       Zeddej                  j                  fd       Z
y)XpuInterface.Workerr   c                     | t         d<   y Nxpur   r2   s    r   r3   zXpuInterface.Worker.set_device1  s    4:*51r   r5   c                  `    dt         v r	t         d   S t        j                  j                         S r   )r   r   r   r7   r!   r   r   r7   z"XpuInterface.Worker.current_device5  s)    665e<<99++--r   Nc                    | \t        | t              r&t        j                  |       } | j                  dk(  sJ t        | t        j                        r| j
                  } | t        j                  j                         } dt        vrZt        t        j                  j                               D cg c]!  }t        j                  j                  |      # }}|t        d<   t        d   |    S c c}w r   )r   r   r   r   r}   rt   r   r=   r7   r   r   r   rF   r9   r   s      r   r9   z)XpuInterface.Worker.get_device_properties;  s    !fc*"\\&1F!;;%///fell3#\\F~%,,;;=<< #599#9#9#;< II33A6  ;F073E:6BBr   r   r   r!   r   r   r=   r   0  s^    		;s 	; 
	; 
	. 	. 
	.
 
	C%++*<*< 	C 
	Cr   r=   r5   c                  >    t         j                  j                         S r   )r   r   rH   r!   r   r   rH   zXpuInterface.is_available^  s    yy%%''r   Nr   c                 D    t         j                  j                  |       }|S r   )r   r   r   )r   ccs     r   r]   z#XpuInterface.get_compute_capabilityb  s    YY,,V4	r   r^   c                 >    t         j                  j                         S r   )r   r   ra   r`   s    r   ra   zXpuInterface.is_bf16_supportedg  s    yy**,,r   c                      yNTr!   r2   s    r   rj   zXpuInterface.is_triton_capablek      r   evicec                 R    dd l }d|j                  j                  vrt        d      y )Nr   intelz)triton not built with the 'intel' backendr   r   rl   )r   r   s     r   rm   z(XpuInterface.raise_if_triton_unavailableo  s(    &//222JKK 3r   r   rn   )"r   r   r   r   r   r   r)   r.   r=   r;   r7   r3   rF   rI   rM   rO   rT   rZ   r9   r   rX   r   rD   r   rB   rh   ro   rH   r   r    r]   ra   rj   rm   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                       e Zd ZU eed<   y)CpuDevicePropertiesmulti_processor_countN)r   r   r   r<   __annotations__r!   r   r   r   r   w  s    r   r   c                      e Zd Z G d dej                        Z G d d      Zedefd       Zeddefd       Z	edd
ej                  j                  defd       Zedefd       Zed        Zedd
ej                  j                  fd       Zedd
ej                  j                  defd       Zedd
ej                  j                  dd	fd       Zy	)CpuInterfacec                   (    e Zd ZddZdefdZddZy)CpuInterface.Eventc                     d| _         y )Ng        time)ry   enable_timings     r   rz   zCpuInterface.Event.__init__~  s	    DIr   r5   c                 :    |j                   | j                   z
  dz  S )Ni  r   )ry   	end_events     r   elapsed_timezCpuInterface.Event.elapsed_time  s    NNTYY.$66r   Nc                 6    t        j                         | _         y r   )r   perf_counter)ry   rI   s     r   recordzCpuInterface.Event.record  s    ))+DIr   )Tr   )r   r   r   rz   floatr   r   r!   r   r   r)   r   }  s    		7U 	7	,r   r)   c                   L    e Zd Zeddej
                  j                  fd       Zy)CpuInterface.WorkerNr   c                 @    dd l }|j                         }t        |      S Nr   )multiprocessing	cpu_countr   )r   r   r   s      r   r9   z)CpuInterface.Worker.get_device_properties  s    "'113I&y11r   r   )r   r   r   r;   r   r   r    r9   r!   r   r   r=   r     s%    		2%++*<*< 	2 
	2r   r=   r5   c                       yr   r!   r!   r   r   rH   zCpuInterface.is_available  r   r   r^   c                      yr   r!   r`   s    r   ra   zCpuInterface.is_bf16_supported  r   r   Nr   c                      yN r!   r2   s    r   r]   z#CpuInterface.get_compute_capability      r   c                      yr   r!   rW   s    r   rX   zCpuInterface.get_raw_stream      r   c                       yr   r!   r!   r   r   r7   zCpuInterface.current_device  r   r   c                      y r   r!   r2   s    r   rZ   zCpuInterface.synchronize  s    r   c                      yr   r!   r2   s    r   rj   zCpuInterface.is_triton_capable  r   r   c                 R    dd l }d|j                  j                  vrt        d      y )Nr   cpuz'triton not built with the 'cpu' backendr   )r   r   s     r   rm   z(CpuInterface.raise_if_triton_unavailable  s(    000HII 1r   rn   r   )r   r   r   r   r)   r=   r;   ro   rH   ra   r   r    r   r]   r<   rX   r7   rZ   rj   rm   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                      e Zd Zeddedefd       Ze	 ddej                  dedefd       Z	edefd       Z
ed        Zedd	ej                  j                  defd
       Zedd	ej                  j                  fd       Z G d d      Zy)MpsInterfacer^   r5   c                 V    t         j                  j                  j                  dd      S )N   r   )r   r   mpsis_macos_or_newerr`   s    r   ra   zMpsInterface.is_bf16_supported  s    ~~!!33B::r   rb   c                     |t         j                  t         j                  fv ry|t         j                  k7  xs | j	                  |      S r   )r   float64
complex128rd   ra   re   s      r   rf   zMpsInterface.is_dtype_supported  s>     U]]E$4$455&T#*?*?@S*TTr   c                  R    t         j                  j                  j                         S r   )r   r   r   rH   r!   r   r   rH   zMpsInterface.is_available  s    ~~!!..00r   c                       yr   r!   r!   r   r   r7   zMpsInterface.current_device  r   r   Nr   c                      yr   r!   r2   s    r   r]   z#MpsInterface.get_compute_capability  r   r   c                 @    t         j                  j                          y r   )r   r   rZ   r2   s    r   rZ   zMpsInterface.synchronize  s    		r   c                   \    e Zd Zeddej
                  j                  fd       Zed        Zy)MpsInterface.WorkerNr   c                     i S r   r!   r2   s    r   r9   z)MpsInterface.Worker.get_device_properties  s    Ir   c                       yr   r!   r!   r   r   r7   z"MpsInterface.Worker.current_device  s    r   r   )	r   r   r   r;   r   r   r    r9   r7   r!   r   r   r=   r     s9    		%++*<*< 	 
	 
	 
	r   r=   rn   r   )r   r   r   r;   ro   ra   rp   r   rb   rf   rH   r7   r   r    r   r]   rZ   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   rs   c                 b    t        | t        j                        r| j                  } |t        | <   y r   )r   r   r   r}   r   )r   rs   s     r   register_interface_for_devicer     s&     &%,,' 0fr   r5   c                     t        | t        j                        r| j                  } t        s
t                | t        v r	t        |    S t        d|        )NzNo interface for device )r   r   r   r}   _device_initializedinit_device_regr   r   r2   s    r   get_interface_for_devicer    sI    &%,,'"" ((
 8A
BBr   c                  J    t         s
t                t        j                         S r   )r  r  r   itemsr!   r   r    get_registered_device_interfacesr    s    ""$$r   c                     t        dt               t        t        j                  j                               D ]  } t        d|  t                t        dt               t        t        j                  j                               D ]  } t        d|  t                t        dt               t        dt               da
y )Nr   zcuda:r   zxpu:r   r   T)r   r   r   r   r   rF   r   r   r   r   r  )r   s    r   r  r    s    !&-85::**,- B%aSk=AB "%6599))+, @%QCj,?@ "%6!%6r   )+r:   r   r   collections.abcr   dataclassesr   typingr   r   r   r   r   r<   r   r   _is_compiledtorch._Cr
   r	   r   dictr   r   r   rr   r   r   r   r   r   r   r   r   r   r}   r  r   r   r  tupler  r  r!   r   r   <module>r     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   