
    BjZ                        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                                        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                                        rddlmZ  ndZ  G d de          Z!eegef         dz  ed<   e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j-        z  d%e+e         d&dfd'Z.d$eej-        z  d&e+e         fd(Z/d&ee0ee+e         f                  fd)Z1d+d*Z2dS ),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#dS ))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                   2    e Zd Zdej        j        defdZdS )DeviceInterface.devicedevicereturnc                     t           NNotImplementedErrorclsr   s     a/var/www/html/Carbon-Document/venv/lib/python3.11/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 	& 	& 	& 	& 	& 	& 	&r   r   c                   "    e Zd ZdededefdZdS )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dS )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dS )	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   >   s        	 	 
	&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                 6    | j                             |          S r   )r@   r<   r   s     r   r<   z%DeviceInterface.get_device_properties   s    z//777r   c                     t           r   r   r6   s    r   get_compute_capabilityz&DeviceInterface.get_compute_capability   rB   r   Fincluding_emulationc                     t           r   r   ra   s    r   is_bf16_supportedz!DeviceInterface.is_bf16_supported   rB   r   dtypec                 L    |t           j        k    p|                     |          S r   )r"   bfloat16rd   r   re   ra   s      r   is_dtype_supportedz"DeviceInterface.is_dtype_supported   s%     &T#*?*?@S*T*TTr   c                     t           r   r   r6   s    r   memory_allocatedz DeviceInterface.memory_allocated   rB   r   c                     dS )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	     ur   c                 L    |                                  st          d          dS )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   s4     $$&& 	RPQQQ	R 	R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| " " " " \" "EL " " " \" "5< "D " " " \" "S " "# "RV " " " \" "3 "3 " " " \" " "EK. "$ " " " \" 8 85;+= 8 8 8 8 [8 " "u{'9 "S " " " \" " "t " " " " \" =BU UKU6:U	U U U [U
 " "!3 "s " " " \"  %+"4     \ 
R 
R1C 
Rt 
R 
R 
R [
R 
R 
Rr   r   c            	       d    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dS )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                 0    || _         || _        d| _        d S )N)rv   idxprev_idx)selfrv   rw   s      r   __init__zDeviceGuard.__init__   s     !1r   c                 `    | j         &| j                            | j                   | _        d S d S r   )rz   rv   rG   r{   )r|   s    r   	__enter__zDeviceGuard.__enter__   s0    8 1AA$(KKDMMM  r   typevalue	tracebackFc                 \    | j         $| j                            | j                  | _         dS )NF)rz   rv   rE   r{   )r|   r   r   r   s       r   __exit__zDeviceGuard.__exit__   s)    8,BB4=QQDHur   r   N)r   r    r!   r=   r   r   r?   r}   r   r   r	   r   r%   r   r   ru   ru      s          $_ 5>ADj	   L L L LS         r   ru   c                   l   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        j                  Z e	ej        j                  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#dS )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dS )CudaInterface.Workerr   r   Nc                     | t           d<   d S Ncudar   r6   s    r   r7   zCudaInterface.Worker.set_device       5;*6222r   c                  j    dt           v rt           d         S t          j                                        S r   )r   r"   r   r:   r%   r   r   r:   z#CudaInterface.Worker.current_device   -    7775f==:,,...r   c                    | Wt          | t                    r!t          j        |           } | j        dk    sJ t          | t          j                  r| j        } | t          j                                        } dt          vr?d t          t          j                                                  D             }|t          d<   t          d         |          S )Nr   c                 L    g | ]!}t           j                            |          "S r%   )r"   r   r<   .0is     r   
<listcomp>z>CudaInterface.Worker.get_device_properties.<locals>.<listcomp>   8        J44Q77  r   )
isinstancestrr"   r   r   rw   r   r@   r:   r   ranger   rI   r   device_props     r   r<   z*CudaInterface.Worker.get_device_properties       !fc** 1"\&11F!;&0000fel33 *#\F~&-<<>>=== "5:#:#:#<#<==   <G083F;FCCr   r   r   r    r!   r>   r?   r7   r:   r"   r#   r$   r   r<   r%   r   r   r@   r              		<s 	<t 	< 	< 	< 
	< 
	/ 	/ 	/ 	/ 
	/
 
	D 	D%+*< 	D 	D 	D 	D 
	D 	D 	Dr   r@   r   c                  >    t           j                                        S r   )r"   r   rK   r%   r   r   rK   zCudaInterface.is_available  s    z&&(((r   Nr   c                     t           j        j        *t           j                            |           \  }}|dz  |z   S t           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  se    =$99&AAJE32:##:33F;;GMMcSTUUVWXXr   c                 x    t           j        j        d up't           j                            |           j        dk    S )N   )r"   r   r   r   r<   r   r6   s    r   rm   zCudaInterface.is_triton_capable  s7     MT) Cz//77=B	
r   c                 `   ddl m} t                              |           s<t          j                            |           } ||t          j                              dd l	}t          j
        j        d|j        j        vrt          d          d S d|j        j        vrt          d          d S )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6 	K :;;FCCL$$\73G3I3IJJJ=(FO444"#LMMM 54V_555KLLL 65r   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      sa       ZF JEZFD D D D D D D D> "\%*";<<Nej344J<
 788L\%*+,,F!\%*";<<Nej344J$UZ%ABB,uz566K(L)IJJ!\/22N"l5:#>??O(L)JKK#|EJ$?@@$UZ%ABB )$ ) ) ) \) Y Yu{'9 YS3Y Y Y Y \Y 
 
%+"4 
 
 
 
 \
 M MEK,> M$ M M M \M M Mr   r   get_mtia_stream)_mtia_getCurrentRawStreamc                   f   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        j                  Z e	ej        j                  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"dS )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dS )MtiaInterface.Workerr   r   Nc                     | t           d<   d S Nmtiar   r6   s    r   r7   zMtiaInterface.Worker.set_device3  r   r   c                  j    dt           v rt           d         S t          j                                        S r   )r   r"   r   r:   r%   r   r   r:   z#MtiaInterface.Worker.current_device7  r   r   c                    | Wt          | t                    r!t          j        |           } | j        dk    sJ t          | t          j                  r| j        } | t          j                                        } dt          vr?d t          t          j                                                  D             }|t          d<   t          d         |          S )Nr   c                 L    g | ]!}t           j                            |          "S r%   )r"   r   r<   r   s     r   r   z>MtiaInterface.Worker.get_device_properties.<locals>.<listcomp>I  r   r   )r   r   r"   r   r   rw   r   r@   r:   r   r   r   rI   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                                        } | S r   )r"   r   rK   )rets    r   rK   zMtiaInterface.is_availablea  s    j%%''
r   Nr   c                 D    t           j                            |           }|S r   )r"   r   r   r   ccs     r   r`   z$MtiaInterface.get_compute_capabilityf  s    Z--f55	r   c                     dS NTr%   r6   s    r   rm   zMtiaInterface.is_triton_capablek      tr   c                 H    dd l }d|j        j        vrt          d          d S )Nr   r   z(triton not built with the 'mtia' backendr   r   ro   r   r   s     r   rp   z)MtiaInterface.raise_if_triton_unavailableo  s5    111IJJJ 21r   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   ,  sT       ZFJEZFD D D D D D D D> "\%*";<<Nej344J<
 788L\%*+,,F!\%*";<<Nej344J$UZ%ABB,uz566K(L)IJJ!\/22N"l5:#>??O(L)JKK#|EJ$?@@$UZ%ABB $    \  u{'9 S    \  %+"4     \ K KEK,> K$ K K K \K K Kr   r   get_xpu_stream)_xpu_getCurrentRawStreamc                   ^   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        j                  Ze	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        j        defd            Z!e	ddej        j        ddfd            Z"dS )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dS )XpuInterface.Workerr   r   Nc                     | t           d<   d S Nxpur   r6   s    r   r7   zXpuInterface.Worker.set_device  s    4:*5111r   c                  j    dt           v rt           d         S t          j                                        S r   )r   r"   r   r:   r%   r   r   r:   z"XpuInterface.Worker.current_device  s-    6665e<<9++---r   c                    | Wt          | t                    r!t          j        |           } | j        dk    sJ t          | t          j                  r| j        } | t          j                                        } dt          vr?d t          t          j                                                  D             }|t          d<   t          d         |          S )Nr   c                 L    g | ]!}t           j                            |          "S r%   )r"   r   r<   r   s     r   r   z=XpuInterface.Worker.get_device_properties.<locals>.<listcomp>  s8        I33A66  r   )r   r   r"   r   r   rw   r   r@   r:   r   r   r   rI   r   s     r   r<   z)XpuInterface.Worker.get_device_properties  s    !fc** 0"\&11F!;%////fel33 *#\F~%,;;==<<< "59#9#9#;#;<<   ;F073E:6BBr   r   r   r%   r   r   r@   r     s        		;s 	;t 	; 	; 	; 
	; 
	. 	. 	. 	. 
	.
 
	C 	C%+*< 	C 	C 	C 	C 
	C 	C 	Cr   r@   r   c                  >    t           j                                        S r   )r"   r   rK   r%   r   r   rK   zXpuInterface.is_available  s    y%%'''r   Nr   c                 D    t           j                            |           }|S r   )r"   r   r   r   s     r   r`   z#XpuInterface.get_compute_capability  s    Y,,V44	r   Fra   c                 >    t           j                                        S r   )r"   r   rd   rc   s    r   rd   zXpuInterface.is_bf16_supported  s    y**,,,r   c                     dS r   r%   r6   s    r   rm   zXpuInterface.is_triton_capable  r   r   c                 H    dd l }d|j        j        vrt          d          d S )Nr   intelz)triton not built with the 'intel' backendr   r   s     r   rp   z(XpuInterface.raise_if_triton_unavailable  s5    &/222JKKK 32r   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   ~  sq       YFIOEYFC C C C C C C C> "\%)":;;Nei233J<	 677L\%)*++F!\%)":;;Nei233J$UY%@AA,uy455K(L)HII!\.11N"l59#=>>O(L)IJJ#|EI$>?? ($ ( ( ( \(  u{'9 S    \ - -t - - - - \-  %+"4     \ L LEK,> L$ L L L \L L Lr   r   c                       e Zd ZU eed<   dS )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d
S )CpuInterfacec                   B    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dS )CpuInterface.EventTenable_timingr   Nc                     d| _         d S )Ng        time)r|   r   s     r   r}   zCpuInterface.Event.__init__  s    DIIIr   otherc                 &    |j         | j         z
  dz  S )Ni  r   )r|   r   s     r   elapsed_timezCpuInterface.Event.elapsed_time  s    J*d22r   rL   c                 6    t          j                    | _         d S r   )r   perf_counter)r|   rL   s     r   recordzCpuInterface.Event.record  s    )++DIIIr   )Tr   )	r   r    r!   rr   r}   r   floatr   r   r%   r   r   r-   r     s        	 	$ 	$ 	 	 	 		3c 	3e 	3 	3 	3 	3	, 	, 	, 	, 	, 	, 	, 	, 	,r   r-   c                   F    e Zd Ze	 ddej        j        defd            ZdS )CpuInterface.WorkerNr   r   c                 P    dd l }|                                }t          |          S Nr   )multiprocessing	cpu_countr   )r   r   r   s      r   r<   z)CpuInterface.Worker.get_device_properties  s/     #"""'1133I&y111r   r   )	r   r    r!   r>   r"   r#   r$   r   r<   r%   r   r   r@   r     sO        	)-	2 	2K&	2 	2 	2 	2 
	2 	2 	2r   r@   r   c                      dS r   r%   r%   r   r   rK   zCpuInterface.is_available  r   r   Fra   c                     dS r   r%   rc   s    r   rd   zCpuInterface.is_bf16_supported  r   r   Nr   c                     dS N r%   r6   s    r   r`   z#CpuInterface.get_compute_capability      rr   rX   c                     dS r   r%   rZ   s    r   r[   zCpuInterface.get_raw_stream      qr   c                      dS r   r%   r%   r   r   r:   zCpuInterface.current_device  r  r   c                     d S r   r%   r6   s    r   r]   zCpuInterface.synchronize  s    r   c                     dS r   r%   r6   s    r   rm   zCpuInterface.is_triton_capable  r   r   c                 H    dd l }d|j        j        vrt          d          d S )Nr   cpuz'triton not built with the 'cpu' backendr   r   s     r   rp   z(CpuInterface.raise_if_triton_unavailable  s5    000HIII 10r   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     s        , , , , , , , ,2 2 2 2 2 2 2 2 $    \  t     \  u{'9 S    \ 3 3    \ C    \  EK. $    \  %+"4     \ J JEK,> J$ J J J \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d	S )MpsInterfaceFra   r   c                     dS r   r%   rc   s    r   rd   zMpsInterface.is_bf16_supported  r   r   re   c                 >    |t           j        t           j        fv rdS dS )NFT)r"   float64
complex128rh   s      r   ri   zMpsInterface.is_dtype_supported  s#     U]E$45555tr   c                  H    t           j        j                                        S r   )r"   r   mpsrK   r%   r   r   rK   zMpsInterface.is_available  s    ~!..000r   c                      dS r   r%   r%   r   r   r:   zMpsInterface.current_device  r  r   Nr   c                     dS r   r%   r6   s    r   r`   z#MpsInterface.get_compute_capability   r  r   c                 B    t           j                                         d S r   )r"   r  r]   r6   s    r   r]   zMpsInterface.synchronize$  s    	r   c                   `    e Zd Zeddej        j        defd            Zede	fd            Z
dS )MpsInterface.WorkerNr   r   c                 x     t          ddg          t          j        j                                                  S )NMPSPropertiesr   )r   r"   r   r  get_core_countr6   s    r   r<   z)MpsInterface.Worker.get_device_properties*  s8    I:o0G/HII"1133  r   c                      dS r   r%   r%   r   r   r:   z"MpsInterface.Worker.current_device0      1r   r   r   r    r!   r>   r"   r#   r$   r   r<   r?   r:   r%   r   r   r@   r  )  q        		 	%+*< 	 	 	 	 
	
 
	 	 	 	 
	 	 	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    sm        t     \ =B K6:	   [ 1$ 1 1 1 \1 C    \  u{'9 S    \    EK.  $       \ 	 	 	 	 	 	 	 	 	 	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d	S )TpuInterfaceFra   r   c                     dS r   r%   rc   s    r   rd   zTpuInterface.is_bf16_supported6  r   r   re   c                 x    |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:  s0     MOOJ
 
 	
r   c                      t                      S r   r
   r%   r   r   rK   zTpuInterface.is_availableF  s    r   c                      dS r   r%   r%   r   r   r:   zTpuInterface.current_deviceJ  r  r   Nr   c                     dS r   r%   r6   s    r   r`   z#TpuInterface.get_compute_capabilityN  r  r   c                   `    e Zd Zeddej        j        defd            Zede	fd            Z
dS )TpuInterface.WorkerNr   r   c                 6     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I  r   c                      dS 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  s5        t     \ =B	
 	
K	
6:	
		
 	
 	
 [	
 $    \ C    \  u{'9 S    \	 	 	 	 	 	 	 	 	 	r   r  device_interfacesFr   rv   r   c                 \    t          | t          j                  r| j        } |t          | <   d S r   )r   r"   r   r   r-  )r   rv   s     r   register_interface_for_devicer/  c  s0     &%,''  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_devicer3  k  s_    &%,''  """ ((
AAA
B
BBr   c                  ^    t           st                       t                                          S r   )r1  r2  r-  itemsr%   r   r    get_registered_device_interfacesr6  u  s(     ""$$$r   c                     t          dt                     t          t          j                                                  D ]} t          d|  t                     t          dt                     t          t          j                                                  D ]} t          d|  t                     t          dt                     t          t          j	                                                  D ]} t          d|  t                     t          dt                     t          dt                     t          d	t                     d
ad S )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  r1  )r   s    r   r2  r2  {  s.   !&-8885:**,,-- B B%kakk=AAAA!%66659))++,, @ @%jQjj,????!&-8885:**,,-- B B%kakk=AAAA!%666!%666!%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   r1  r   r/  r3  tupler6  r2  r%   r   r   <module>rB     se    "   " " " " " " . . . . . . . . ! ! ! ! ! !          - - - - - - 3%*%, , , ,: EEEEEEEO 46  $sCx. 5 5 513 S#X 3 3 3CR CR CR CR CR CR CR CRL       6YM YM YM YM YMO YM YM YMx 3%*%, , , ,: EEEEEEEOHK HK HK HK HKO HK HK HKV #$t+ + + +9 CCCCCCCNJL JL JL JL JL? JL JL JLZ        8J 8J 8J 8J 8J? 8J 8J 8Jv' ' ' ' '? ' ' 'T' ' ' ' '? ' ' 'T 79 4T/223 8 8 8 1%,1262G1	1 1 1 1CS5<%7 CD<Q C C C C%(5d?>S9S3T*U % % % %     r   