
    9jZ                     6   U d Z ddl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mZ ddlZddlmZ eegef   dz  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gef   dz  ed<   ej<                  j'                         rddlmZ  ndZ  G d de      Z!eegef   dz  ed<   ejD                  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( G d  d!e      Z)i Z*eee+e   f   ed"<   d#a,d$eejZ                  z  d%e+e   d&dfd'Z.d$eejZ                  z  d&e+e   fd(Z/d&ee0ee+e   f      fd)Z1d+d*Z2y),a  
Device abstraction layer for TorchDynamo and Inductor backends.

This module provides a unified interface for different hardware backends (CUDA, XPU,
CPU, MPS, MTIA) 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, MtiaInterface
- 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
namedtuple)CallableIterable)	dataclass)AnyLiteralhas_torch_tpu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
e	fd       Z
edej                  j                  d
dfd       Zede	d
e	fd       Zede	d
e	fd       Zed
e	fd       Zed
efd       Zedej                  d
efd       Zed
ej                  fd       Zedej                  d
dfd       Zede	de	de	d
dfd       Zede	d
e	fd       Zed&dej                  j                  d
d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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                   D    e Zd Zdej                  j
                  defdZy)DeviceInterface.devicedevicereturnc                     t         NNotImplementedErrorclsr   s     ^/media/conek/DATA/Code/OCR/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   r   .   s    	&!3!3 	& 	&r   r   c                        e Zd ZdededefdZy)DeviceInterface.Eventargskwargsr   c                     t        d      )NzYEvent should be inherited from torch.Event, otherwise, it couldn't be captured by dynamo.r   r   r(   r)   s      r   r   zDeviceInterface.Event.__new__3   s    %k r   Nr   r    r!   r   r   r%   r   r   Eventr'   2       	 	s 	s 	r   r-   c                        e Zd ZdededefdZy)DeviceInterface.Streamr(   r)   r   c                     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   Streamr0   8   r.   r   r2   c                       e Zd ZdZededdfd       Zedefd       Zeddej                  j                  de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   r   Nc                     t         r   r   r   s    r   
set_devicez!DeviceInterface.Worker.set_deviceF       %%r   c                      t         r   r   r%   r   r   current_devicez%DeviceInterface.Worker.current_deviceJ   r8   r   c                     t         r   r   r6   s    r   get_device_propertiesz,DeviceInterface.Worker.get_device_propertiesN   r8   r   r   )r   r    r!   __doc__staticmethodintr7   r:   r"   r#   r$   r   r<   r%   r   r   Workerr4   >   sp    	 
	&s 	&t 	& 
	& 
	& 	& 
	& 
	&%++*<*< 	& 	& 
	&r   r@   r   c                      t         r   r   r%   r   r   r:   zDeviceInterface.current_deviceR       !!r   Nc                     t         r   r   r6   s    r   r7   zDeviceInterface.set_deviceV   rB   r   c                     t         r   r   r6   s    r   maybe_exchange_devicez%DeviceInterface.maybe_exchange_deviceZ   rB   r   c                     t         r   r   r6   s    r   exchange_devicezDeviceInterface.exchange_device^   rB   r   c                      t         r   r   r%   r   r   device_countzDeviceInterface.device_countb   rB   r   c                      t         r   r   r%   r   r   is_availablezDeviceInterface.is_availablef   rB   r   streamc                     t         r   r   rL   s    r   rL   zDeviceInterface.streamj   rB   r   c                      t         r   r   r%   r   r   current_streamzDeviceInterface.current_streamn   rB   r   c                     t         r   r   rN   s    r   
set_streamzDeviceInterface.set_streamr   rB   r   	stream_iddevice_indexdevice_typec                     t         r   r   )rS   rT   rU   s      r   _set_stream_by_idz!DeviceInterface._set_stream_by_idv   rB   r   
device_idxc                     t         r   r   rX   s    r   get_raw_streamzDeviceInterface.get_raw_streamz   rB   r   c                     t         r   r   r6   s    r   synchronizezDeviceInterface.synchronize~   rB   r   c                 8    | j                   j                  |      S r   )r@   r<   r   s     r   r<   z%DeviceInterface.get_device_properties   s    zz//77r   c                     t         r   r   r6   s    r   get_compute_capabilityz&DeviceInterface.get_compute_capability   rB   r   including_emulationc                     t         r   r   ra   s    r   is_bf16_supportedz!DeviceInterface.is_bf16_supported   rB   r   dtypec                 N    |t         j                  k7  xs | j                  |      S r   )r"   bfloat16rd   r   re   ra   s      r   is_dtype_supportedz"DeviceInterface.is_dtype_supported   s$     &T#*?*?@S*TTr   c                     t         r   r   r6   s    r   memory_allocatedz DeviceInterface.memory_allocated   rB   r   c                      y)z
        Returns True if the device has Triton support, False otherwise, even if
        the appropriate Triton backend is not available.
        Fr%   r6   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)rm   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-   r2   r@   r>   r?   r:   r"   r#   r$   r7   rE   rG   rI   boolrK   r   rL   rP   rR   rW   r[   r]   classmethodr<   r`   rd   re   ri   rk   rm   rp   r%   r   r   r   r   (   s   
& &  & &( "C " " "5;;-- "$ " " "c "c " " " " " " "# " " "$ " " "u|| " " " "ELL " " "5<< "D " " "S " "# "RV " " "3 "3 " " "EKK.. "$ " " 85;;+=+= 8 8 8 "u{{'9'9 "S " " "t " " " =BUKKU6:U	U U
 "!3!3 "s " " %++"4"4    
R1C1C 
Rt 
R 
Rr   r   c            	       V    e Zd ZdZdee   dedz  ddfdZddZde	d	e	d
e	de
d   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indexNr   c                 .    || _         || _        d| _        y )N)rv   idxprev_idx)selfrv   rw   s      r   __init__zDeviceGuard.__init__   s     !1r   c                 r    | j                   +| j                  j                  | j                         | _        y y r   )rz   rv   rG   r{   )r|   s    r   	__enter__zDeviceGuard.__enter__   s-    88 11AA$((KDM  r   typevalue	tracebackFc                 p    | j                   *| j                  j                  | j                        | _         y)NF)rz   rv   rE   r{   )r|   r   r   r   s       r   __exit__zDeviceGuard.__exit__   s+    88,,BB4==QDHr   r   N)r   r    r!   r=   r   r   r?   r}   r   r   r	   r   r%   r   r   ru   ru      sX     $_ 5>ADj	LS    r   ru   c                   t   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<                  dee z  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                       e Zd Zededdfd       Zedefd       Zeddej                  j                  de
fd       Zy)CudaInterface.Workerr   r   Nc                     | t         d<   y Ncudar   r6   s    r   r7   zCudaInterface.Worker.set_device       5;*62r   c                  `    dt         v r	t         d   S t        j                  j                         S r   )r   r"   r   r:   r%   r   r   r:   z#CudaInterface.Worker.current_device   )    775f==::,,..r   c                    | \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   rw   r   r@   r:   r   ranger   rI   r<   r   idevice_props      r   r<   z*CudaInterface.Worker.get_device_properties       !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?   r7   r:   r"   r#   r$   r   r<   r%   r   r   r@   r      m    		<s 	<t 	< 
	< 
	/ 	/ 
	/
 
	D%++*<*< 	D 	D 
	Dr   r@   r   c                  >    t         j                  j                         S r   )r"   r   rK   r%   r   r   rK   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_capabilityr<   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   r<   r   r6   s    r   rm   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   rm   r"   r   r<   inspectcurrentframetriton.backendsr   r   backendsro   )r   r   device_propstritons       r   rp   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-   r2   r@   r>   r:   r7   rI   rL   rP   rR   rW   r]   r<   r   r[   _exchange_devicerG   _maybe_exchange_devicerE   rk   rd   rr   rK   r#   r$   r?   r   r`   rm   rp   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S3Y Y Y 
%++"4"4 
 
 
 MEKK,>,> M$ M Mr   r   get_mtia_stream)_mtia_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	ej                  j4                        Z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)MtiaInterfacec                       e Zd Zededdfd       Zedefd       Zeddej                  j                  de
fd       Zy)MtiaInterface.Workerr   r   Nc                     | t         d<   y Nmtiar   r6   s    r   r7   zMtiaInterface.Worker.set_device3  r   r   c                  `    dt         v r	t         d   S t        j                  j                         S r   )r   r"   r   r:   r%   r   r   r:   z#MtiaInterface.Worker.current_device7  r   r   c                    | \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   rw   r   r@   r:   r   r   r   rI   r<   r   s      r   r<   z*MtiaInterface.Worker.get_device_properties=  r   r   r   r   r%   r   r   r@   r   2  r   r   r@   r   c                  B    t         j                  j                         } | S r   )r"   r   rK   )rets    r   rK   zMtiaInterface.is_availablea  s    jj%%'
r   Nr   c                 D    t         j                  j                  |       }|S r   )r"   r   r   r   ccs     r   r`   z$MtiaInterface.get_compute_capabilityf  s    ZZ--f5	r   c                      yNTr%   r6   s    r   rm   zMtiaInterface.is_triton_capablek      r   c                 R    dd l }d|j                  j                  vrt        d      y )Nr   r   z(triton not built with the 'mtia' backendr   r   ro   r   r   s     r   rp   z)MtiaInterface.raise_if_triton_unavailableo  s(    111IJJ 2r   r   )#r   r    r!   r"   r   r   r-   r2   r@   r>   r:   r7   rI   rL   rP   rR   rW   r]   r<   r   r[   r   rG   r   rE   rk   rd   rr   rK   r#   r$   r   r`   rm   rp   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 $   u{{'9'9 S   %++"4"4    KEKK,>,> K$ K Kr   r   get_xpu_stream)_xpu_getCurrentRawStreamc                   R   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:                  de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                       e Zd Zededdfd       Zedefd       Zeddej                  j                  de
fd       Zy)XpuInterface.Workerr   r   Nc                     | t         d<   y Nxpur   r6   s    r   r7   zXpuInterface.Worker.set_device  s    4:*51r   c                  `    dt         v r	t         d   S t        j                  j                         S r   )r   r"   r   r:   r%   r   r   r:   z"XpuInterface.Worker.current_device  s)    665e<<99++--r   c                    | \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   rw   r   r@   r:   r   r   r   rI   r<   r   s      r   r<   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     sm    		;s 	;t 	; 
	; 
	. 	. 
	.
 
	C%++*<*< 	C 	C 
	Cr   r@   r   c                  >    t         j                  j                         S r   )r"   r   rK   r%   r   r   rK   zXpuInterface.is_available  s    yy%%''r   Nr   c                 D    t         j                  j                  |       }|S r   )r"   r   r   r   s     r   r`   z#XpuInterface.get_compute_capability  s    YY,,V4	r   ra   c                 >    t         j                  j                         S r   )r"   r   rd   rc   s    r   rd   zXpuInterface.is_bf16_supported  s    yy**,,r   c                      yr   r%   r6   s    r   rm   zXpuInterface.is_triton_capable  r   r   c                 R    dd l }d|j                  j                  vrt        d      y )Nr   intelz)triton not built with the 'intel' backendr   r   s     r   rp   z(XpuInterface.raise_if_triton_unavailable  s(    &//222JKK 3r   r   rq   )#r   r    r!   r"   r   r   r-   r2   r@   r>   r:   r7   rI   rL   rP   rR   rW   r]   r<   r   r[   r   rG   r   rE   rk   rr   rK   r#   r$   r   r`   rd   rm   rp   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 S   -t - - - %++"4"4    LEKK,>,> 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     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defd       Z	edd
ej                  j                  defd       Zededefd       Zedefd       Zedd
ej                  j                  dd	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eddfdZdedefdZd
deddfdZy)CpuInterface.Eventenable_timingr   Nc                     d| _         y )Ng        time)r|   r   s     r   r}   zCpuInterface.Event.__init__  s	    DIr   otherc                 :    |j                   | j                   z
  dz  S )Ni  r   )r|   r   s     r   elapsed_timezCpuInterface.Event.elapsed_time  s    JJ*d22r   rL   c                 6    t        j                         | _         y r   )r   perf_counter)r|   rL   s     r   recordzCpuInterface.Event.record  s    ))+DIr   )Tr   )	r   r    r!   rr   r}   r   floatr   r   r%   r   r   r-   r     s;    	$ 	$ 		3c 	3e 	3	, 	, 	,r   r-   c                   R    e Zd Ze	 ddej
                  j                  defd       Zy)CpuInterface.WorkerNr   r   c                 @    dd l }|j                         }t        |      S Nr   )multiprocessing	cpu_countr   )r   r   r   s      r   r<   z)CpuInterface.Worker.get_device_properties  s      #'113I&y11r   r   )	r   r    r!   r>   r"   r#   r$   r   r<   r%   r   r   r@   r     s1    	)-	2KK&&	2 	2 
	2r   r@   r   c                       yr   r%   r%   r   r   rK   zCpuInterface.is_available  r   r   ra   c                      yr   r%   rc   s    r   rd   zCpuInterface.is_bf16_supported  r   r   Nr   c                      yN r%   r6   s    r   r`   z#CpuInterface.get_compute_capability      r   rX   c                      yr   r%   rZ   s    r   r[   zCpuInterface.get_raw_stream      r   c                       yr   r%   r%   r   r   r:   zCpuInterface.current_device  r   r   c                      y r   r%   r6   s    r   r]   zCpuInterface.synchronize  s    r   c                      yr   r%   r6   s    r   rm   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   s     r   rp   z(CpuInterface.raise_if_triton_unavailable  s(    000HII 1r   rq   r   )r   r    r!   r"   r-   r@   r>   rr   rK   rd   r#   r$   r   r`   r   r?   r[   r:   r]   rm   rp   r%   r   r   r   r     sJ   , ,2 2 $   t    u{{'9'9 S   3 3   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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 G d d      Zy)MpsInterfacera   r   c                      yr   r%   rc   s    r   rd   zMpsInterface.is_bf16_supported  r   r   re   c                 J    |t         j                  t         j                  fv ryy)NFT)r"   float64
complex128rh   s      r   ri   zMpsInterface.is_dtype_supported  s!     U]]E$4$455r   c                  R    t         j                  j                  j                         S r   )r"   r   mpsrK   r%   r   r   rK   zMpsInterface.is_available  s    ~~!!..00r   c                       yr   r%   r%   r   r   r:   zMpsInterface.current_device  r   r   Nr   c                      yr   r%   r6   s    r   r`   z#MpsInterface.get_compute_capability   r   r   c                 @    t         j                  j                          y r   )r"   r  r]   r6   s    r   r]   zMpsInterface.synchronize$  s    		r   c                   f    e Zd Zeddej
                  j                  defd       Zede	fd       Z
y)MpsInterface.WorkerNr   r   c                 t     t        ddg      t        j                  j                  j	                               S )NMPSPropertiesr   )r   r"   r   r  get_core_countr6   s    r   r<   z)MpsInterface.Worker.get_device_properties*  s2    I:o0G/HI""113 r   c                       yr   r%   r%   r   r   r:   z"MpsInterface.Worker.current_device0      r   r   r   r    r!   r>   r"   r#   r$   r   r<   r?   r:   r%   r   r   r@   r  )  G    		%++*<*< 	 	 
	
 
	 	 
	r   r@   rq   r   )r   r    r!   r>   rr   rd   rs   r"   re   ri   rK   r?   r:   r#   r$   r   r`   r]   r@   r%   r   r   r  r    s    t    =BKK6:	  1$ 1 1 C   u{{'9'9 S    EKK..  $    	 	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efd       Zedd	ej                  j                  defd
       Z G d d      Zy)TpuInterfacera   r   c                      yr   r%   rc   s    r   rd   zTpuInterface.is_bf16_supported6  r   r   re   c                     |t         j                  t         j                  t         j                  t         j                  t         j
                  fvS r   )r"   r  	complex32	complex64r	  halfrh   s      r   ri   zTpuInterface.is_dtype_supported:  s;     MMOOOOJJ
 
 	
r   c                      t               S r   r
   r%   r   r   rK   zTpuInterface.is_availableF  s
    r   c                       yr   r%   r%   r   r   r:   zTpuInterface.current_deviceJ  r   r   Nr   c                      yr   r%   r6   s    r   r`   z#TpuInterface.get_compute_capabilityN  r   r   c                   f    e Zd Zeddej
                  j                  defd       Zede	fd       Z
y)TpuInterface.WorkerNr   r   c                 (     t        ddg      d      S )NTPUPropertiesr   r   r   r6   s    r   r<   z)TpuInterface.Worker.get_device_propertiesT  s    I:o0G/HI r   c                       yr   r%   r%   r   r   r:   z"TpuInterface.Worker.current_deviceZ  r  r   r   r  r%   r   r   r@   r#  S  r  r   r@   rq   r   )r   r    r!   r>   rr   rd   rs   r"   re   ri   rK   r?   r:   r#   r$   r   r`   r@   r%   r   r   r  r  5  s    t    =B	
KK	
6:	
		
 	
 $   C   u{{'9'9 S  	 	r   r  device_interfacesFr   rv   r   c                 b    t        | t        j                        r| j                  } |t        | <   y r   )r   r"   r   r   r'  )r   rv   s     r   register_interface_for_devicer)  c  s&     &%,,' 0fr   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   r6   s    r   get_interface_for_devicer-  k  sI    &%,,'"" ((
 8A
BBr   c                  J    t         s
t                t        j                         S r   )r+  r,  r'  itemsr%   r   r    get_registered_device_interfacesr0  u  s    ""$$r   c                  B   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        t        j                  j                               D ]  } t        d|  t                t        dt               t        dt               t        d	t               d
ay )Nr   zcuda:r   zxpu:r   zmtia:r  r  tpuT)r)  r   r   r"   r   rI   r   r   r   r   r   r  r  r+  )r   s    r   r,  r,  {  s    !&-85::**,- B%aSk=AB "%6599))+, @%QCj,?@ "&-85::**,- B%aSk=AB "%6!%6!%6r   r   )3r=   r   r   collectionsr   collections.abcr   r   dataclassesr   typingr   r	   r"   torch.utils._pallasr   r?   r   r   _is_compiledtorch._Cr   r   r   dictr   r   r   ru   r   r   r   r   r   r   r   r   r   r   r   r  r  r'  r   r+  r   r)  r-  tupler0  r,  r%   r   r   <module>r<     s   "   " . !   - 3%*%, ,::EO 46  $sCx. 513 S#X 3CR CRL 6YMO YMx 3%*%, ,::EOHKO HKV #$t+ +99CNJL? JLZ   8J? 8Jv'? 'T'? 'T 79 4T/223 8 1%,,1262G1	1CS5<<%7 CD<Q C%(5d?>S9S3T*U %r   