
    ΧgQ=                     *   U d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZm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
a ej                    Z ej                    Zg ae	eeg df         e	e         f                  e d<    e!ej"        dd           Z#eeee$df         Z% e            a&dZ'eej"        j(                 e d<   de)fdZ* e*            r%ej"        j+        Z+ej"        j,        Z-ej"        j.        Z/n ed          Z+de$de$fdZ-de$de$fdZ/ ed          de$fd            Z0de)fdZ1d Z2d Z3d Z4d Z5d Z6 G d d          Z7 G d  d          Z G d! d"e          Z8de%ddfd#Z9d:de
e%         defd$Z: ed          d:de
e%         deeef         fd%            Z;d:de
e%         de+fd&Z<de$fd'Z=dee$eej        f         dej        fd(Z> G d) d*          Z?d+e
d,         de?fd-Z@d. ZAd+efd/ZBd:de
e%         defd0ZCd:de%ddfd1ZDdej        dej"        j(        fd2ZE	 d;d4e$dee$eej        f         ddfd5ZFd;dee$eej        f         de$fd6ZGdd7lHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQ dd8lRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[ g d9Z\dS )<z
This package introduces support for the XPU backend, specifically tailored for
Intel GPU optimization.

This package is lazily initialized, so you can always import it, and use
:func:`is_available()` to determine if your system supports XPU.
    N)	lru_cache)AnyCallableDictListOptionalTupleUniondevice)_dummy_type_LazySeedTracker   )_get_device_index)EventStreamF_queued_calls_xpu_isInBadForkc                      dS NF r       N/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/xpu/__init__.py<lambda>r      s     r   r   default_generatorsreturnc                  $    t           j        j        S )z(Return true if compile with XPU support.)torch_C_has_xpur   r   r   _is_compiledr!   #   s    8r   _XpuDevicePropertiesr   c                      t          d          Nz(PyTorch was compiled without XPU supportNotImplementedErrorr   s    r   _exchange_devicer'   0       !"LMMMr   c                      t          d          r$   r%   r   s    r   _maybe_exchange_devicer*   3   r(   r   )maxsizec                  ^    t                      sdS t          j                                        S )z*Return the number of XPU device available.r   )r!   r   r   _xpu_getDeviceCountr   r   r   device_countr.   7   s)     >> q8'')))r   c                  &    t                      dk    S )z7Return a bool indicating if XPU is currently available.r   )r.   r   r   r   is_availabler0   ?   s     >>Ar   c                      dS )zKReturn a bool indicating if the current XPU device supports dtype bfloat16.Tr   r   r   r   is_bf16_supportedr2   E   s    4r   c                  .    t           ot                       S )z8Return whether PyTorch's XPU state has been initialized.)_initialized_is_in_bad_forkr   r   r   is_initializedr6   J   s    1 1 111r   c                    t                      r |              d S |                    dd          r.t                              | t	          j                               d S |                    dd          r.t                              | t	          j                               d S t                              | t	          j                    f           d S )Nseed_allFseed)	r6   get_lazy_seed_trackerqueue_seed_all	tracebackformat_stack
queue_seedr   append)callablekwargss     r   
_lazy_callrC   O   s     
G




 ::j%(( 	G--h	8N8P8PQQQQQZZ&& 	G))(I4J4L4LMMMMM   (I,B,D,D!EFFFFFr   c                  "    t                       dS )zInitialize PyTorch's XPU state.
    This is a Python API about lazy initialization that avoids initializing
    XPU until the first time it is accessed. Does nothing if the XPU state is
    already initialized.
    N)
_lazy_initr   r   r   initrF   ]   s     LLLLLr   c            	      ,   t                      st          t          d          rd S t          5  t                      r	 d d d            d S t	                      rt          d          t                      st          d          t          j	        
                                 dt          _        t                                          D ]} | rt                              |            	 t          D ]Z\  }}	  |             # t           $ r=}dt#          |           dd                    |           }t!          |          |d }~ww xY w	 t'          t          d           n# t'          t          d           w xY wdad d d            d S # 1 swxY w Y   d S )Nis_initializingzuCannot re-initialize XPU in forked subprocess. To use XPU with multiprocessing, you must use the 'spawn' start methodz#Torch not compiled with XPU enabledTz5XPU call failed lazily at initialization with error: z'

XPU call was originally invoked at:

 )r6   hasattr_tls_initialization_lockr5   RuntimeErrorr!   AssertionErrorr   r   	_xpu_initrH   r;   	get_callsr   r@   	Exceptionstrjoindelattrr4   )callsqueued_callorig_tracebackemsgs        r   rE   rE   f   s8    74):;; 	 # #  		# # # # # # # #  	I   ~~ 	H !FGGG  $'1133 	, 	,E ,$$U+++	-/< 0 0+^0KMMMM  0 0 0\PSTUPVPV \ \BD''.BYBY\ \  $C..a/00 D+,,,,GD+,,,,G# # # # # # # # # # # # # # # # # #sT   F	
BF	'E 4
C?>E ?
E	8EEE 
F	 E77F		FFc                   4    e Zd ZdefdZd ZdededefdZdS )	_DeviceGuardindexc                 "    || _         d| _        d S N)idxprev_idx)selfr\   s     r   __init__z_DeviceGuard.__init__   s    r   c                 X    t           j                            | j                  | _        d S Nr   xpur'   r`   ra   rb   s    r   	__enter__z_DeviceGuard.__enter__       	2248<<r   typevaluer=   c                 X    t           j                            | j                  | _        dS r   r   rg   r*   ra   r`   rb   rk   rl   r=   s       r   __exit__z_DeviceGuard.__exit__        933DMBBur   N)__name__
__module____qualname__intrc   ri   r   rp   r   r   r   r[   r[      sg        c    = = =S        r   r[   c                   8    e Zd ZdZd efdZd ZdededefdZdS )	r   zContext-manager that changes the selected device.

    Args:
        device (torch.device or int or str): device index to select. It's a no-op if
            this argument is a negative integer or ``None``.
    c                 @    t          |d          | _        d| _        d S )NToptionalr_   )r   r`   ra   )rb   r   s     r   rc   zdevice.__init__   s!    $Vd;;;r   c                 X    t           j                            | j                  | _        d S re   rf   rh   s    r   ri   zdevice.__enter__   rj   r   rk   rl   r=   c                 X    t           j                            | j                  | _        dS r   rn   ro   s       r   rp   zdevice.__exit__   rq   r   N)rr   rs   rt   __doc__r   rc   ri   rp   r   r   r   r   r      sq         s    = = =S        r   c                   "     e Zd ZdZ fdZ xZS )	device_ofa  Context-manager that changes the current device to that of given object.

    You can use both tensors and storages as arguments. If a given object is
    not allocated on a XPU, this is a no-op.

    Args:
        obj (Tensor or Storage): object allocated on the selected device.
    c                     |j         r|                                nd}t                                          |           d S r^   )is_xpu
get_devicesuperrc   )rb   objr`   	__class__s      r   rc   zdevice_of.__init__   s<    "%*4cnn"r   )rr   rs   rt   r|   rc   __classcell__)r   s   @r   r~   r~      sB                 r   r~   c                     t                       t          |           } | dk    r!t          j                            |            dS dS )zSet the current device.

    Args:
        device (torch.device or int or str): selected device. This function is a
            no-op if this argument is negative.
    r   N)rE   r   r   r   _xpu_setDevicer   s    r   
set_devicer      sF     LLLv&&F{{''''' {r   c                 *    t          |           j        S )a  Get the name of a device.

    Args:
        device (torch.device or int or str, optional): device for which to
            return the name. This function is a no-op if this argument is a
            negative integer. It uses the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).

    Returns:
        str: the name of the device
    )get_device_propertiesnamer   s    r   get_device_namer      s     !((--r   c                 X    t          |           fdt                    D             S )a  Get the xpu capability of a device.

    Args:
        device (torch.device or int or str, optional): device for which to
            return the device capability. This function is a no-op if this
            argument is a negative integer. It uses the current device, given by
            :func:`~torch.xpu.current_device`, if :attr:`device` is ``None``
            (default).

    Returns:
        Dict[str, Any]: the xpu capability dictionary of the device
    c                 \    i | ](}|                     d           |t          |          )S )__)
startswithgetattr).0proppropss     r   
<dictcomp>z)get_device_capability.<locals>.<dictcomp>   sG       '+QUAVAVgeT""  r   )r   dir)r   r   s    @r   get_device_capabilityr      sC     "&))E   /25zz   r   c                     t                       t          | d          } | dk     s| t                      k    rt          d          t	          |           S )zGet the properties of a device.

    Args:
        device (torch.device or int or str): device for which to return the
            properties of the device.

    Returns:
        _XpuDeviceProperties: the properties of the device
    Trx   r   zInvalid device index)rE   r   r.   rN   _get_device_propertiesr   s    r   r   r      sS     LLLv555FzzV|~~--3444!&)))r   c                  Z    t                       t          j                                        S )z0Return the index of a currently selected device.)rE   r   r   _xpu_getDevicer   r   r   current_devicer     s    LLL8""$$$r   c                     t          | t                    rt          j        |           } n*t          | t                    rt          j        d|           } | S )zReturn the torch.device type object from the passed in device.

    Args:
        device (torch.device or int or str): selected device.
    rg   )
isinstancerR   r   r   ru   r   s    r   _get_devicer     sO     &# -f%%	FC	 	  -eV,,Mr   c                   \    e Zd ZU dZed         ed<   ded         fdZd Zdeded	efd
Z	dS )StreamContexta  Context-manager that selects a given stream.

    All XPU kernels queued within its context will be enqueued on a selected
    stream.

    Args:
        Stream (Stream): selected stream. This manager is a no-op if it's
            ``None``.
    .. note:: Streams are per-device.
    torch.xpu.Stream
cur_streamstreamc                 ^    || _         t          d d          | _        | j        	d| _        d S d S )NTr_   )r   r   r`   )rb   r   s     r   rc   zStreamContext.__init__!  s5    $T4008DHHH r   c                    | j         }|| j        dk    rd S t          j                            d           | _        | j        j        |j        k    rUt          |j                  5  t          j                            |j                  | _        d d d            n# 1 swxY w Y   t          j                            |           d S r^   )	r   r`   r   rg   current_streamsrc_prev_streamr   dst_prev_stream
set_stream)rb   r   s     r   ri   zStreamContext.__enter__'  s    [
RF$y77== &**;;;
)** S S',y'?'?
@Q'R'R$S S S S S S S S S S S S S S S	Z(((((s   $*BB!Brk   rl   r=   c                     | j         }|| j        dk    rd S | j        j        |j        k    r$t          j                            | j                   t          j                            | j                   d S r^   )r   r`   r   r   r   rg   r   r   )rb   rk   rl   r=   r   s        r   rp   zStreamContext.__exit__3  sl    [
RF &**;;;I  !5666	T122222r   N)
rr   rs   rt   r|   r   __annotations__rc   ri   r   rp   r   r   r   r   r     s         	 	 +,,,,x(:;    
) 
) 
)3S 3 3 3 3 3 3 3 3r   r   r   r   c                      t          |           S )zWrap around the Context-manager StreamContext that selects a given stream.

    Arguments:
        stream (Stream): selected stream. This manager is a no-op if it's ``None``.
    )r   r   s    r   r   r   >  s        r   c                 J    t           j                            | ||           dS )a  set stream specified by the stream id, device index and device type

    Args: stream_id (int): not visible to the user, used to assigned to the specific stream.
          device_index (int): selected device index.
          device_type (int): selected device type.
    	stream_iddevice_indexdevice_typeN)r   r   _xpu_setStreamr   s      r   _set_stream_by_idr   G  s6     
H!      r   c                 l    | dS t                       t          | j        | j        | j                   dS )a  Set the current stream.This is a wrapper API to set the stream.
        Usage of this function is discouraged in favor of the ``stream``
        context manager.

    Args:
        stream (Stream): selected stream. This function is a no-op
            if this argument is ``None``.
    Nr   )rE   r   r   r   r   r   s    r   r   r   U  sJ     ~LLL"(&     r   c                     t                       t          j                            t	          | d                    }t          |d         |d         |d                   S )aR  Return the currently selected :class:`Stream` for a given device.

    Args:
        device (torch.device or int, optional): selected device. Returns
            the currently selected :class:`Stream` for the current device, given
            by :func:`~torch.xpu.current_device`, if :attr:`device` is ``None``
            (default).
    Trx   r   r      r   )rE   r   r   _xpu_getCurrentStreamr   r   )r   
streamdatas     r   r   r   h  sa     LLL//&4000 J Q-jmTU   r   c                 ~    t                       t          | d          } t          j                            |           S )a*  Wait for all kernels in all streams on a XPU device to complete.

    Args:
        device (torch.device or int, optional): device for which to synchronize.
            It uses the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    Trx   )rE   r   r   r   _xpu_synchronizer   s    r   synchronizer   z  s5     LLLv555F8$$V,,,r   c                 ^    | j         }|t                      }t          j        j        |         S )zuReturn the XPU Generator object for the given device.

    Args:
        device (torch.device): selected device.
    )r\   r   r   rg   r   )r   r`   s     r   _get_generatorr     s+     ,C
{9',,r   rg   offsetc                 R     t          |           fd}t          |           dS )a$  Set the random number generator state offset of the specified GPU.

    Args:
        offset (int): The desired offset
        device (torch.device or int, optional): The device to set the RNG state.
            Default: ``'xpu'`` (i.e., ``torch.device('xpu')``, the current XPU device).
    c                  P    t                    } |                                d S re   )r   
set_offset)default_generatorfinal_devicer   s    r   cbz!_set_rng_state_offset.<locals>.cb  s+    *<88$$V,,,,,r   N)r   rC   )r   r   r   r   s   `  @r   _set_rng_state_offsetr     sB     v&&L- - - - - - rNNNNNr   c                     t                       t          |           }t          |          }|                                S )aL  Return the random number generator state offset of the specified GPU.

    Args:
        device (torch.device or int, optional): The device to return the RNG state offset of.
            Default: ``'xpu'`` (i.e., ``torch.device('xpu')``, the current XPU device).

    .. warning::
        This function eagerly initializes XPU.
    )rE   r   r   
get_offset)r   r   r   s      r   _get_rng_state_offsetr     s:     LLLv&&L&|44'')))r   )	empty_cachemax_memory_allocatedmax_memory_reservedmemory_allocatedmemory_reservedmemory_statsmemory_stats_as_nested_dictreset_accumulated_memory_statsreset_peak_memory_stats)	get_rng_stateget_rng_state_allinitial_seedmanual_seedmanual_seed_allr9   r8   set_rng_stateset_rng_state_all)(r   r   r   r   r   r   r   r~   r.   r   r   r   r   r   r   
get_streamrF   r   r0   r2   r6   r   r   r   r   r   r   r   r   r   r   r9   r8   r   r   r   r   r   streamsr   re   )rg   )]r|   	threadingr=   	functoolsr   typingr   r   r   r   r   r	   r
   r   torch._Cr   _devicetorch._utilsr   r   _utilsr   r   r   r   r4   localrK   LockrL   r   rR   r   r   r   r5   ru   	_device_tr;   r   	Generatorboolr!   r"   _xpu_exchangeDevicer'   _xpu_maybeExchangeDevicer*   r.   r0   r2   r6   rC   rF   rE   r[   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   memoryr   r   r   r   r   r   r   r   r   randomr   r   r   r   r   r9   r8   r   r   __all__r   r   r   <module>r      s                   D D D D D D D D D D D D D D D D D D   # # # # # # 6 6 6 6 6 6 6 6 % % % % % % " " " " " " " " y%y~''   t	(2t8
d3i
'(    '%($6FF'3T)*	%%'' 02 E%(,- 2 2 2d    
 <>> N 88x3"X> ';'=>>N N N N N NNs Ns N N N N 1*c * * * *d      
2 2 2
G G G  ' ' 'T
 
 
 
 
 
 
 
       (       
(y 
(T 
( 
( 
( 
(. .HY/ .3 . . . . 4 (9"5 c3h    &* *(9"5 *AU * * * *"% % % % %
c345 
%, 
 
 
 
'3 '3 '3 '3 '3 '3 '3 '3T!8./ !M ! ! ! !  v    & 8I. &    $
- 
-	 
-T 
- 
- 
- 
-	-5< 	-EH,> 	- 	- 	- 	- :? sC56	   &* *%S%,(>"? *C * * * *"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
) ) )r   