
    3jZ                     \   % S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	  SSK
JrJr  SSKrSSKJr  \\/\4   S-  \S'   \R$                  R'                  5       (       a  SS	KJr  OSr0 r\\\4   \S
'   0 r\\\4   \S'    " S S5      r " S S5      r " S S\5      r\\/\4   S-  \S'   \R<                  R'                  5       (       a  SSKJr   OSr  " S S\5      r!\\/\4   S-  \S'   \RD                  R'                  5       (       a  SSKJ#r$  OSr$ " S S\5      r%\	 " S S5      5       r& " S S\5      r' " S S\5      r( " S  S!\5      r)0 r*\\\+\   4   \S"'   S#q,S$\\RZ                  -  S%\+\   S&S4S' jr.S$\\RZ                  -  S&\+\   4S( jr/S&\\0\\+\   4      4S) jr1S+S* jr2g),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            	          \ rS rSrSr " S S5      r " S S5      r " S S5      r " S	 S
5      r\	S\
4S j5       r\	S\R                  R                  SS4S j5       r\	S\
S\
4S j5       r\	S\
S\
4S j5       r\	S\
4S j5       r\	S\4S j5       r\	S\R                  S\4S j5       r\	S\R                  4S j5       r\	S\R                  SS4S j5       r\	S\
S\
S\
SS4S j5       r\	S\
S\
4S j5       r\	S(S\R                  R                  SS4S jj5       r\S(S\R                  R                  S\4S jj5       r\	S(S\R                  R                  S\4S jj5       r\	S)S \S\4S! jj5       r\ S)S"\R@                  S \S\4S# jj5       r!\	S(S\R                  R                  S\
4S$ jj5       r"\	S(S\R                  R                  S\4S% jj5       r#\S(S\R                  R                  SS4S& jj5       r$S'r%g)*DeviceInterface(   z
This is a simple device runtime interface for Inductor. It enables custom
backends to be integrated with Inductor in a device-agnostic semantic.
c                   N    \ rS rSrS\R
                  R                  S\4S jrSr	g)DeviceInterface.device.   devicereturnc                     [         eNNotImplementedErrorclsr   s     X/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_dynamo/device_interface.py__new__DeviceInterface.device.__new__/   s    %%     N)
__name__
__module____qualname____firstlineno__torchtypesDevicer   r   __static_attributes__r"   r!   r   r   r   .   s    	&!3!3 	& 	&r!   r   c                   *    \ rS rSrS\S\S\4S jrSrg)DeviceInterface.Event2   argskwargsr   c                     [        S5      e)NzYEvent should be inherited from torch.Event, otherwise, it couldn't be captured by dynamo.r   r   r.   r/   s      r   r   DeviceInterface.Event.__new__3   s    %k r!   r"   Nr#   r$   r%   r&   r   r   r*   r"   r!   r   Eventr,   2       	 	s 	s 	r!   r4   c                   *    \ rS rSrS\S\S\4S jrSrg)DeviceInterface.Stream8   r.   r/   r   c                     [        S5      e)Nz[Stream should be inherited from torch.Stream, otherwise, it couldn't be captured by dynamo.r   r1   s      r   r   DeviceInterface.Stream.__new__9   s    %m r!   r"   Nr3   r"   r!   r   Streamr7   8   r5   r!   r;   c                       \ rS rSrSr\S\SS4S j5       r\S\4S j5       r\S
S\	R                  R                  S\4S jj5       rS	rg)DeviceInterface.Worker>   z
Worker API to query device properties that will work in multi processing
workers that cannot use the GPU APIs (due to processing fork() and
initialization time issues). Properties are recorded in the main process
before we fork the workers.
r   r   Nc                     [         er   r   r   s    r   
set_device!DeviceInterface.Worker.set_deviceF       %%r!   c                      [         er   r   r"   r!   r   current_device%DeviceInterface.Worker.current_deviceJ   rC   r!   c                     [         er   r   r@   s    r   get_device_properties,DeviceInterface.Worker.get_device_propertiesN   rC   r!   r"   r   )r#   r$   r%   r&   __doc__staticmethodintrA   rE   r'   r(   r)   r   rH   r*   r"   r!   r   Workerr=   >   sp    	 
	&s 	&t 	& 
	& 
	& 	& 
	& 
	&%++*<*< 	& 	& 
	&r!   rM   r   c                      [         er   r   r"   r!   r   rE   DeviceInterface.current_deviceR       !!r!   Nc                     [         er   r   r@   s    r   rA   DeviceInterface.set_deviceV   rP   r!   c                     [         er   r   r@   s    r   maybe_exchange_device%DeviceInterface.maybe_exchange_deviceZ   rP   r!   c                     [         er   r   r@   s    r   exchange_deviceDeviceInterface.exchange_device^   rP   r!   c                      [         er   r   r"   r!   r   device_countDeviceInterface.device_countb   rP   r!   c                      [         er   r   r"   r!   r   is_availableDeviceInterface.is_availablef   rP   r!   streamc                     [         er   r   r_   s    r   r_   DeviceInterface.streamj   rP   r!   c                      [         er   r   r"   r!   r   current_streamDeviceInterface.current_streamn   rP   r!   c                     [         er   r   ra   s    r   
set_streamDeviceInterface.set_streamr   rP   r!   	stream_iddevice_indexdevice_typec                     [         er   r   )ri   rj   rk   s      r   _set_stream_by_id!DeviceInterface._set_stream_by_idv   rP   r!   
device_idxc                     [         er   r   ro   s    r   get_raw_streamDeviceInterface.get_raw_streamz   rP   r!   c                     [         er   r   r@   s    r   synchronizeDeviceInterface.synchronize~   rP   r!   c                 8    U R                   R                  U5      $ r   )rM   rH   r   s     r   rH   %DeviceInterface.get_device_properties   s    zz//77r!   c                     [         er   r   r@   s    r   get_compute_capability&DeviceInterface.get_compute_capability   rP   r!   including_emulationc                     [         er   r   r|   s    r   is_bf16_supported!DeviceInterface.is_bf16_supported   rP   r!   dtypec                 X    U[         R                  :g  =(       d    U R                  U5      $ r   )r'   bfloat16r   r   r   r|   s      r   is_dtype_supported"DeviceInterface.is_dtype_supported   s$     &T#*?*?@S*TTr!   c                     [         er   r   r@   s    r   memory_allocated DeviceInterface.memory_allocated   rP   r!   c                     g)zz
Returns True if the device has Triton support, False otherwise, even if
the appropriate Triton backend is not available.
Fr"   r@   s    r   is_triton_capable!DeviceInterface.is_triton_capable   s     r!   c                 D    U R                  5       (       d  [        S5      eg)a  
Raises a `RuntimeError` with the appropriate human-readable instructions
to resolve the issue if Triton is not available for the given device, or
the default device if `device` is `None`.

The caller should ensure the presence of the 'triton' package before
calling this method.
z/This device is not capable of supporting TritonN)r   RuntimeErrorr   s     r   raise_if_triton_unavailable+DeviceInterface.raise_if_triton_unavailable   s$     $$&&PQQ 'r!   r"   r   F)&r#   r$   r%   r&   rJ   r   r4   r;   rM   rK   rL   rE   r'   r(   r)   rA   rT   rW   rZ   boolr]   r   r_   rd   rg   rm   rr   ru   classmethodrH   rz   r   r   r   r   r   r   r*   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            	       d    \ rS rSrSrS\\   S\S-  SS4S jrSS jr	S	\
S
\
S\
S\S   4S jrSrg)DeviceGuard   aG  
This class provides a context manager for device switching. This is a stripped
down version of torch.{device_name}.device.

The context manager changes the current device to the given device index
on entering the context and restores the original device on exiting.
The device is switched using the provided device interface.
device_interfaceindexNr   c                 *    Xl         X l        SU l        g )Nr   idxprev_idx)selfr   r   s      r   __init__DeviceGuard.__init__   s     !1r!   c                 t    U R                   b+  U R                  R                  U R                   5      U l        g g r   )r   r   rW   r   )r   s    r   	__enter__DeviceGuard.__enter__   s-    88 11AA$((KDM  r!   typevalue	tracebackFc                 r    U R                   b*  U R                  R                  U R                  5      U l         g)NF)r   r   rT   r   )r   r   r   r   s       r   __exit__DeviceGuard.__exit__   s+    88,,BB4==QDHr!   r   r   N)r#   r$   r%   r&   rJ   r   r   rL   r   r   r   r	   r   r*   r"   r!   r   r   r      sX     $_ 5>ADj	LS    r!   r   c                      \ rS rSr\R
                  R                  r\R
                  R                  r\R
                  R                  r " S S5      r	\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                   5      r\
" \R
                  R"                  5      r\
" \R
                  R$                  5      r\
" \R
                  R&                  5      r\
" \5      r\
" \R
                  R,                  5      r\
" \R
                  R0                  5      r\
" \R
                  R4                  5      r\
" \R
                  R6                  5      r\
S\4S j5       r\
SS\R<                  R>                  S\ \!-  4S jj5       r"\
SS\R<                  R>                  S\4S	 jj5       r#\
SS\R<                  R>                  SS4S
 jj5       r$Sr%g)CudaInterface   c                       \ rS rSr\S\SS4S j5       r\S\4S j5       r\S	S\R                  R                  S\4S jj5       rSrg)
CudaInterface.Worker   r   r   Nc                     U [         S'   g Ncudar   r@   s    r   rA   CudaInterface.Worker.set_device       5;*62r!   c                  d    S[         ;   a	  [         S   $ [        R                  R                  5       $ r   )r   r'   r   rE   r"   r!   r   rE   #CudaInterface.Worker.current_device   )    775f==::,,..r!   c                 
   U bh  [        U [        5      (       a(  [        R                  " U 5      n U R                  S:X  d   e[        U [        R                  5      (       a  U R
                  n U c  [        R                  R                  5       n S[        ;  a]  [        [        R                  R                  5       5       Vs/ s H"  n[        R                  R                  U5      PM$     nnU[        S'   [        S   U    $ s  snf r   )
isinstancestrr'   r   r   r   r   rM   rE   r   ranger   rZ   rH   r   idevice_props      r   rH   *CudaInterface.Worker.get_device_properties       !fc**"\\&1F!;;&000fell33#\\F~&--<<>== #5::#:#:#<== JJ44Q7=   <G083F;FCC    )D r"   r   r#   r$   r%   r&   rK   rL   rA   rE   r'   r(   r)   r   rH   r*   r"   r!   r   rM   r      m    		<s 	<t 	< 
	< 
	/ 	/ 
	/
 
	D%++*<*< 	D 	D 
	Dr!   rM   r   c                  >    [         R                  R                  5       $ r   )r'   r   r]   r"   r!   r   r]   CudaInterface.is_available  s    zz&&((r!   Nr   c                    [         R                  R                  c)  [         R                  R	                  U 5      u  pUS-  U-   $ [         R                  R                  U 5      R                  R                  SS5      S   $ )N
   :   r   )r'   versionhipr   get_device_capabilityrH   gcnArchNamesplit)r   majormins      r   rz   $CudaInterface.get_compute_capability  sf    ==$99&AJE2:##::33F;GGMMcSTUVWXXr!   c                     [         R                  R                  S L=(       d,    [         R                  R	                  U 5      R
                  S:  $ )N   )r'   r   r   r   rH   r   r@   s    r   r   CudaInterface.is_triton_capable  s>     MMT) Czz//7==B	
r!   c                    SSK Jn  [        R                  U 5      (       d;  [        R
                  R                  U 5      nU" U[        R                  " 5       5      eSS K	n[        R                  R                  b&  SUR                  R                  ;  a  [        S5      eg SUR                  R                  ;  a  [        S5      eg )Nr   )GPUTooOldForTritonamdz'triton not built with the 'amd' backendnvidiaz*triton not built with the 'nvidia' backend)torch._inductor.excr   r   r   r'   r   rH   inspectcurrentframetriton.backendsr   r   backendsr   )r   r   device_propstritons       r   r   )CudaInterface.raise_if_triton_unavailable  s    :..v66 ::;;FCL$\73G3G3IJJ==(FOO444"#LMM 5V__555KLL 6r!   r"   r   )&r#   r$   r%   r&   r'   r   r   r4   r;   rM   rK   rE   rA   rZ   r_   rd   rg   rm   ru   rH   r   rr   _exchange_devicerW   _maybe_exchange_devicerT   r   r   r   r]   r(   r)   rL   r   rz   r   r   r*   r"   r!   r   r   r      s   ZZF JJEZZFD D> "%**";";<Nejj334J

 7 78L%**++,F!%**";";<Nejj334J$UZZ%A%ABuzz556K()I)IJ!/2N"5::#>#>?O()J)JK#EJJ$?$?@$UZZ%A%AB )$ ) ) Yu{{'9'9 YS3Y Y Y 
%++"4"4 
 
 
 MEKK,>,> M$ M Mr!   r   get_mtia_stream)_mtia_getCurrentRawStreamc                      \ rS rSr\R
                  R                  r\R
                  R                  r\R
                  R                  r " S S5      r	\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                   5      r\
" \R
                  R"                  5      r\
" \R
                  R$                  5      r\
" \R
                  R&                  5      r\
" \5      r\
" \R
                  R,                  5      r\
" \R
                  R0                  5      r\
" \R
                  R4                  5      r\
" \R
                  R6                  5      r\
S\4S j5       r\
SS\R<                  R>                  S\ 4S jj5       r!\
SS\R<                  R>                  S\4S	 jj5       r"\
SS\R<                  R>                  SS4S
 jj5       r#Sr$g)MtiaInterfacei,  c                       \ rS rSr\S\SS4S j5       r\S\4S j5       r\S	S\R                  R                  S\4S jj5       rSrg)
MtiaInterface.Workeri2  r   r   Nc                     U [         S'   g Nmtiar   r@   s    r   rA   MtiaInterface.Worker.set_device3  r   r!   c                  d    S[         ;   a	  [         S   $ [        R                  R                  5       $ r   )r   r'   r   rE   r"   r!   r   rE   #MtiaInterface.Worker.current_device7  r   r!   c                 
   U bh  [        U [        5      (       a(  [        R                  " U 5      n U R                  S:X  d   e[        U [        R                  5      (       a  U R
                  n U c  [        R                  R                  5       n S[        ;  a]  [        [        R                  R                  5       5       Vs/ s H"  n[        R                  R                  U5      PM$     nnU[        S'   [        S   U    $ s  snf r   )r   r   r'   r   r   r   r   rM   rE   r   r   r   rZ   rH   r   s      r   rH   *MtiaInterface.Worker.get_device_properties=  r   r   r"   r   r   r"   r!   r   rM   r   2  r   r!   rM   r   c                  B    [         R                  R                  5       n U $ r   )r'   r   r]   )rets    r   r]   MtiaInterface.is_availablea  s    jj%%'
r!   Nr   c                 D    [         R                  R                  U 5      nU$ r   )r'   r   r   r   ccs     r   rz   $MtiaInterface.get_compute_capabilityf  s    ZZ--f5	r!   c                     gNTr"   r@   s    r   r   MtiaInterface.is_triton_capablek      r!   c                 V    SS K nSUR                  R                  ;  a  [        S5      eg )Nr   r   z(triton not built with the 'mtia' backendr   r   r   r   r   s     r   r   )MtiaInterface.raise_if_triton_unavailableo  s(    111IJJ 2r!   r"   r   )%r#   r$   r%   r&   r'   r   r   r4   r;   rM   rK   rE   rA   rZ   r_   rd   rg   rm   ru   rH   r   rr   r   rW   r   rT   r   r   r   r]   r(   r)   r   rz   r   r   r*   r"   r!   r   r   r   ,  s   ZZFJJEZZFD D> "%**";";<Nejj334J

 7 78L%**++,F!%**";";<Nejj334J$UZZ%A%ABuzz556K()I)IJ!/2N"5::#>#>?O()J)JK#EJJ$?$?@$UZZ%A%AB $   u{{'9'9 S   %++"4"4    KEKK,>,> K$ K Kr!   r   get_xpu_stream)_xpu_getCurrentRawStreamc                   l   \ rS rSr\R
                  R                  r\R
                  R                  r\R
                  R                  r " S S5      r	\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                  5      r\
" \R
                  R                   5      r\
" \R
                  R"                  5      r\
" \R
                  R$                  5      r\
" \R
                  R&                  5      r\
" \5      r\
" \R
                  R,                  5      r\
" \R
                  R0                  5      r\
" \R
                  R4                  5      r\
S\4S j5       r\
SS\R:                  R<                  S\4S jj5       r \
SS	\S\4S
 jj5       r!\
SS\R:                  R<                  S\4S jj5       r"\
SS\R:                  R<                  SS4S jj5       r#Sr$g)XpuInterfacei~  c                       \ rS rSr\S\SS4S j5       r\S\4S j5       r\S	S\R                  R                  S\4S jj5       rSrg)
XpuInterface.Workeri  r   r   Nc                     U [         S'   g Nxpur   r@   s    r   rA   XpuInterface.Worker.set_device  s    4:*51r!   c                  d    S[         ;   a	  [         S   $ [        R                  R                  5       $ r	  )r   r'   r
  rE   r"   r!   r   rE   "XpuInterface.Worker.current_device  s)    665e<<99++--r!   c                 
   U bh  [        U [        5      (       a(  [        R                  " U 5      n U R                  S:X  d   e[        U [        R                  5      (       a  U R
                  n U c  [        R                  R                  5       n S[        ;  a]  [        [        R                  R                  5       5       Vs/ s H"  n[        R                  R                  U5      PM$     nnU[        S'   [        S   U    $ s  snf r	  )r   r   r'   r   r   r   r  rM   rE   r   r   r
  rZ   rH   r   s      r   rH   )XpuInterface.Worker.get_device_properties  s    !fc**"\\&1F!;;%///fell33#\\F~%,,;;=<< #599#9#9#;<< II33A6<   ;F073E:6BBr   r"   r   r   r"   r!   r   rM   r    sm    		;s 	;t 	; 
	; 
	. 	. 
	.
 
	C%++*<*< 	C 	C 
	Cr!   rM   r   c                  >    [         R                  R                  5       $ r   )r'   r
  r]   r"   r!   r   r]   XpuInterface.is_available  s    yy%%''r!   Nr   c                 D    [         R                  R                  U 5      nU$ r   )r'   r
  r   r   s     r   rz   #XpuInterface.get_compute_capability  s    YY,,V4	r!   r|   c                 >    [         R                  R                  5       $ r   )r'   r
  r   r~   s    r   r   XpuInterface.is_bf16_supported  s    yy**,,r!   c                     gr   r"   r@   s    r   r   XpuInterface.is_triton_capable  r   r!   c                 V    SS K nSUR                  R                  ;  a  [        S5      eg )Nr   intelz)triton not built with the 'intel' backendr   r   s     r   r   (XpuInterface.raise_if_triton_unavailable  s(    &//222JKK 3r!   r"   r   r   )%r#   r$   r%   r&   r'   r
  r   r4   r;   rM   rK   rE   rA   rZ   r_   rd   rg   rm   ru   rH   r  rr   r   rW   r   rT   r   r   r]   r(   r)   r   rz   r   r   r   r*   r"   r!   r   r  r  ~  s   YYFIIOOEYYFC C> "%))":":;Neii223J		 6 67L%))**+F!%))":":;Neii223J$UYY%@%@Auyy445K()H)HI!.1N"599#=#=>O()I)IJ#EII$>$>? ($ ( ( u{{'9'9 S   -t - - - %++"4"4    LEKK,>,> L$ L Lr!   r  c                        \ rS rSr% \\S'   Srg)CpuDevicePropertiesi  multi_processor_countr"   N)r#   r$   r%   r&   rL   __annotations__r*   r"   r!   r   r  r    s    r!   r  c                      \ rS rSr " S S\R
                  5      r " S S5      r\S\4S j5       r	\SS\S\4S	 jj5       r
\SS\R                  R                  S\4S jj5       r\S\S\4S j5       r\S\4S j5       r\SS\R                  R                  SS
4S jj5       r\SS\R                  R                  S\4S jj5       r\SS\R                  R                  SS
4S jj5       rSrg
)CpuInterfacei  c                   R    \ rS rSrSS\SS4S jjrS\S\4S jrSS\SS4S	 jjr	S
r
g)CpuInterface.Eventi  enable_timingr   Nc                     SU l         g )Ng        time)r   r#  s     r   r   CpuInterface.Event.__init__  s	    DIr!   otherc                 :    UR                   U R                   -
  S-  $ )Ni  r%  )r   r(  s     r   elapsed_timeCpuInterface.Event.elapsed_time  s    JJ*d22r!   r_   c                 8    [         R                  " 5       U l         g r   )r&  perf_counter)r   r_   s     r   recordCpuInterface.Event.record  s    ))+DIr!   r%  )Tr   )r#   r$   r%   r&   r   r   r   floatr*  r.  r*   r"   r!   r   r4   r"    s@    	$ 	$ 		3c 	3e 	3	, 	, 	, 	,r!   r4   c                   ^    \ rS rSr\ SS\R                  R                  S\4S jj5       r	Sr
g)CpuInterface.Workeri  Nr   r   c                 @    SS K nUR                  5       n[        U5      $ Nr   )multiprocessing	cpu_countr  )r   r5  r6  s      r   rH   )CpuInterface.Worker.get_device_properties  s      #'113I&y11r!   r"   r   )r#   r$   r%   r&   rK   r'   r(   r)   r  rH   r*   r"   r!   r   rM   r2    s1    	)-	2KK&&	2 	2 
	2r!   rM   r   c                      gr   r"   r"   r!   r   r]   CpuInterface.is_available  r   r!   r|   c                     gr   r"   r~   s    r   r   CpuInterface.is_bf16_supported  r   r!   Nr   c                     gN r"   r@   s    r   rz   #CpuInterface.get_compute_capability      r!   ro   c                     gr4  r"   rq   s    r   rr   CpuInterface.get_raw_stream      r!   c                      gr4  r"   r"   r!   r   rE   CpuInterface.current_device  rC  r!   c                     g r   r"   r@   s    r   ru   CpuInterface.synchronize  s    r!   c                     gr   r"   r@   s    r   r   CpuInterface.is_triton_capable  r   r!   c                 V    SS K nSUR                  R                  ;  a  [        S5      eg )Nr   cpuz'triton not built with the 'cpu' backendr   r   s     r   r   (CpuInterface.raise_if_triton_unavailable  s(    000HII 1r!   r"   r   r   )r#   r$   r%   r&   r'   r4   rM   rK   r   r]   r   r(   r)   r   rz   r   rL   rr   rE   ru   r   r   r*   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                   B   \ rS rSr\SS\S\4S jj5       r\ SS\R                  S\S\4S jj5       r
\S\4S j5       r\S\4S j5       r\SS
\R                  R                  S\4S jj5       r\SS
\R                  R                  SS	4S jj5       r " S S5      rSrg	)MpsInterfacei  r|   r   c                     gr   r"   r~   s    r   r   MpsInterface.is_bf16_supported  r   r!   r   c                 N    U[         R                  [         R                  4;   a  gg)NFT)r'   float64
complex128r   s      r   r   MpsInterface.is_dtype_supported  s!     U]]E$4$455r!   c                  R    [         R                  R                  R                  5       $ r   )r'   r   mpsr]   r"   r!   r   r]   MpsInterface.is_available  s    ~~!!..00r!   c                      gr4  r"   r"   r!   r   rE   MpsInterface.current_device  rC  r!   Nr   c                     gr=  r"   r@   s    r   rz   #MpsInterface.get_compute_capability   r@  r!   c                 @    [         R                  R                  5         g r   )r'   rV  ru   r@   s    r   ru   MpsInterface.synchronize$  s    		r!   c                   t    \ rS rSr\SS\R                  R                  S\4S jj5       r	\S\
4S j5       rSrg)	MpsInterface.Workeri)  Nr   r   c                 t    [        SS/5      " [        R                  R                  R	                  5       5      $ )NMPSPropertiesr  )r   r'   r   rV  get_core_countr@   s    r   rH   )MpsInterface.Worker.get_device_properties*  s0    o0G/HI""113 r!   c                      gr4  r"   r"   r!   r   rE   "MpsInterface.Worker.current_device0      r!   r"   r   r#   r$   r%   r&   rK   r'   r(   r)   r   rH   rL   rE   r*   r"   r!   r   rM   r_  )  G    		%++*<*< 	 	 
	
 
	 	 
	r!   rM   r"   r   r   )r#   r$   r%   r&   rK   r   r   r   r'   r   r   r]   rL   rE   r(   r)   r   rz   ru   rM   r*   r"   r!   r   rN  rN    s    t    =BKK6:	  1$ 1 1 C   u{{'9'9 S    EKK..  $    	 	r!   rN  c                       \ rS rSr\SS\S\4S jj5       r\ SS\R                  S\S\4S jj5       r
\S\4S j5       r\S\4S j5       r\SS
\R                  R                  S\4S jj5       r " S S5      rSrg	)TpuInterfacei5  r|   r   c                     gr   r"   r~   s    r   r   TpuInterface.is_bf16_supported6  r   r!   r   c                     U[         R                  [         R                  [         R                  [         R                  [         R
                  4;  $ r   )r'   rR  	complex32	complex64rS  halfr   s      r   r   TpuInterface.is_dtype_supported:  s;     MMOOOOJJ
 
 	
r!   c                      [        5       $ r   r
   r"   r!   r   r]   TpuInterface.is_availableF  s
    r!   c                      gr4  r"   r"   r!   r   rE   TpuInterface.current_deviceJ  rC  r!   Nr   c                     gr=  r"   r@   s    r   rz   #TpuInterface.get_compute_capabilityN  r@  r!   c                   t    \ rS rSr\SS\R                  R                  S\4S jj5       r	\S\
4S j5       rSrg)	TpuInterface.WorkeriS  Nr   r   c                 (    [        SS/5      " S5      $ )NTPUPropertiesr  r   r   r@   s    r   rH   )TpuInterface.Worker.get_device_propertiesT  s    o0G/HI r!   c                      gr4  r"   r"   r!   r   rE   "TpuInterface.Worker.current_deviceZ  rf  r!   r"   r   rg  r"   r!   r   rM   ry  S  rh  r!   rM   r"   r   r   )r#   r$   r%   r&   rK   r   r   r   r'   r   r   r]   rL   rE   r(   r)   r   rz   rM   r*   r"   r!   r   rj  rj  5  s    t    =B	
KK	
6:	
		
 	
 $   C   u{{'9'9 S  	 	r!   rj  device_interfacesFr   r   r   c                 l    [        U [        R                  5      (       a  U R                  n U[        U '   g r   )r   r'   r   r   r  )r   r   s     r   register_interface_for_devicer  c  s)     &%,,'' 0fr!   c                     [        U [        R                  5      (       a  U R                  n [        (       d
  [        5         U [        ;   a	  [        U    $ [        SU  35      e)NzNo interface for device )r   r'   r   r   _device_initializedinit_device_regr  r   r@   s    r   get_interface_for_devicer  k  sO    &%,,''"" ((
 8A
BBr!   c                  T    [         (       d
  [        5         [        R                  5       $ r   )r  r  r  itemsr"   r!   r    get_registered_device_interfacesr  u  s    ""$$r!   c                  N   [        S[        5        [        [        R                  R                  5       5       H  n [        SU  3[        5        M     [        S[        5        [        [        R                  R                  5       5       H  n [        SU  3[        5        M     [        S[        5        [        [        R                  R                  5       5       H  n [        SU  3[        5        M     [        S[        5        [        S[        5        [        S	[        5        S
qg )Nr   zcuda:r
  zxpu:r   zmtia:rK  rV  tpuT)r  r   r   r'   r   rZ   r  r
  r   r   r   rN  rj  r  )r   s    r   r  r  {  s    !&-85::**,-%aSk=A . "%6599))+,%QCj,? - "&-85::**,-%aSk=A . "%6!%6!%6r!   r   )3rJ   r   r&  collectionsr   collections.abcr   r   dataclassesr   typingr   r	   r'   torch.utils._pallasr   rL   r  r   _is_compiledtorch._Cr   r   r   dictr   r   r   r   r   r   r   r   r   r
  r  r  r  r  r   rN  rj  r  r   r  r   r  r  tupler  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!   