
    GjT                       U d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlZddlZddlmZmZ e
rddlmZ ddlmZ d	d
lmZ d	dlmZmZmZmZmZ d	dlmZmZ da ej                     Z! ej"                    Z#g a$de%d<    e&ej'        dd           Z( e            a)dZ*de%d<   dTdZ+ e+            r%ej'        j,        Z,ej'        j-        Z.ej'        j/        Z0n ed          Z,dUdZ.dUdZ0 ed	          dVd            Z1dTdZ2dWdXd"Z3dTd#Z4d$ Z5dYd&Z6dYd'Z7dYd(Z8 G d) d*          Z9 G d+ d          Z: G d, d-e:          Z;dZd/Z<d[d\d1Z= ed          d[d]d3            Z>	 d[d^d4Z?dVd5Z@d_d8ZAd`d:ZB G d; d<          ZCdad?ZDdYd@ZEdbdBZFd[dcdCZGd[dddEZHd[dZdFZIdedHZJdfdIZKdgdKZL	 dhdidNZMdhdjdOZNd	dPlOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z` d	dQlambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZj  e	dRekelelf                   Zmg dSZndS )kz
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.
    )annotationsN)	lru_cache)AnyNewTypeTYPE_CHECKING)_dummy_type_LazySeedTracker)Callable)Device   )_get_device_index)graphgraph_pool_handleis_current_stream_capturingmake_graphed_callablesXPUGraph)EventStreamFz*list[tuple[Callable[[], None], list[str]]]_queued_calls_xpu_isInBadForkc                     dS NF r       U/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/torch/xpu/__init__.py<lambda>r   ,   s     r   r   ztuple[torch._C.Generator]default_generatorsreturnboolc                 $    t           j        j        S )z(Return true if compile with XPU support.)torch_C_has_xpur   r   r   _is_compiledr$   1   s    8r   _XpuDevicePropertiesdeviceintc                     t          d          Nz(PyTorch was compiled without XPU supportNotImplementedErrorr&   s    r   _exchange_devicer-   >       !"LMMMr   c                     t          d          r)   r*   r,   s    r   _maybe_exchange_devicer0   A   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_countr4   E   s)     >> q8'')))r   c                 &    t                      dk    S )z7Return a bool indicating if XPU is currently available.r   )r4   r   r   r   is_availabler6   M   s     >>Ar   Tincluding_emulationc                l    t                      sdS | p"t          j                                        j        S )zKReturn a bool indicating if the current XPU device supports dtype bfloat16.F)r6   r!   xpuget_device_propertieshas_bfloat16_conversions)r7   s    r   is_bf16_supportedr<   S   s5    >> u 	F9**,,Er   c                 h    t                      sdS t          j                                        j        S )zGReturn a bool indicating if the current XPU device supports dtype tf32.F)r6   r!   r9   r:   'has_subgroup_matrix_multiply_accumulater   r   r   is_tf32_supportedr?   ]   s,    >> u
 9**,,TTr   c                 .    t           ot                       S )z8Return whether PyTorch's XPU state has been initialized.)_initialized_is_in_bad_forkr   r   r   is_initializedrC   h   s    1 1 111r   Nonec                   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)	rC   get_lazy_seed_trackerqueue_seed_all	tracebackformat_stack
queue_seedr   append)callablekwargss     r   
_lazy_callrQ   m   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   initrT   {   s     LLLLLr   c            	     2   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 t                                          D                        	 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 enabledTc              3     K   | ]}||V  	d S Nr   ).0callss     r   	<genexpr>z_lazy_init.<locals>.<genexpr>   s(      XXuRWXUXXXXXXr   z5XPU call failed lazily at initialization with error: z'

XPU call was originally invoked at:

 )rC   hasattr_tls_initialization_lockrB   RuntimeErrorr$   AssertionErrorr!   r"   	_xpu_initrV   r   extendrI   	get_calls	ExceptionstrjoindelattrrA   )queued_callorig_tracebackemsgs       r   rS   rS      s0    74):;; 	 ! !  		! ! ! ! ! ! ! !  	I   ~~ 	H !FGGG  $XX0B0L0L0N0NXXXXXX	-/< 0 0+^0KMMMM  0 0 0\PSTUPVPV \ \BD''.BYBY\ \  $C..a/00 D+,,,,GD+,,,,C! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !sT   F
BF*E#7
DE#
E	8EE		E#F#E::FFFc                  $    e Zd ZddZd ZddZdS )_DeviceGuardindexr'   r   rD   c                "    || _         d| _        d S N)idxprev_idx)selfro   s     r   __init__z_DeviceGuard.__init__   s    r   c                X    t           j                            | j                  | _        d S rX   r!   r9   r-   rs   rt   ru   s    r   	__enter__z_DeviceGuard.__enter__       	2248<<r   typer   valuerK   c                X    t           j                            | j                  | _        dS r   r!   r9   r0   rt   rs   ru   r|   r}   rK   s       r   __exit__z_DeviceGuard.__exit__        933DMBBur   N)ro   r'   r   rD   r|   r   r}   r   rK   r   )__name__
__module____qualname__rv   rz   r   r   r   r   rn   rn      sK           = = =     r   rn   c                  (    e Zd ZdZddZd Zd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``.
    r   r   rD   c                @    t          |d          | _        d| _        d S )NToptionalrr   )r   rs   rt   )ru   r&   s     r   rv   zdevice.__init__   s!    $Vd;;;r   c                X    t           j                            | j                  | _        d S rX   rx   ry   s    r   rz   zdevice.__enter__   r{   r   r|   r}   rK   c                X    t           j                            | j                  | _        dS r   r   r   s       r   r   zdevice.__exit__   r   r   N)r&   r   r   rD   r   )r   r   r   __doc__rv   rz   r   r   r   r   r&   r&      sU            = = =     r   c                  $     e Zd ZdZd 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.
    r   rD   c                    |j         r|                                nd}t                                          |           d S rq   )is_xpu
get_devicesuperrv   )ru   objrs   	__class__s      r   rv   zdevice_of.__init__   s<    "%*4cnn"r   r   rD   )r   r   r   r   rv   __classcell__)r   s   @r   r   r      sG                  r   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)rS   r   r!   r"   _xpu_setDevicer,   s    r   
set_devicer      sF     LLLv&&F{{''''' {r   rf   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
    )r:   namer,   s    r   get_device_namer      s     !((--r   dict[str, Any]c                    t          |           t          t          t          t          t          d          t          t          t          f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
    Nc                    i | ]:}|                     d           t          t          |          x          7|;S )__)
startswith
isinstancegetattr)rY   keypropsserializable_typesr}   s     r   
<dictcomp>z)get_device_capability.<locals>.<dictcomp>	  sa       ~~d## !4!447IJJ	U  r   )
r:   r'   floatr   rf   r|   listtupledictdir)r&   r   r   r}   s    @@@r   get_device_capabilityr      sl     "&))EudCdT5$O     u::   r   c                ^    t                       t          | d          } t          |           S )a  Get the properties of a device. Returns _XpuDeviceProperties containing the following device properties:

    - ``name`` (str): device name.
    - ``platform_name`` (str): SYCL platform name.
    - ``vendor`` (str): device vendor.
    - ``device_id`` (int): device identifier (product ID).
    - ``driver_version`` (str): driver version.
    - ``version`` (str): runtime version.
    - ``max_compute_units`` (int): number of parallel compute units.
    - ``gpu_eu_count`` (int): number of EUs (Execution Unit).
    - ``max_work_group_size``: (int): maximum number of work-items permitted in a work-group.
    - ``max_num_sub_groups`` (int): maximum number of sub-groups supported in a work-group.
    - ``memory_clock_rate`` (int) maximum clock rate of device's global memory in MHz.
    - ``memory_bus_width`` (int) maximum bus width between device and memory in bits.
    - ``sub_group_sizes``: (list[int]): a list of supported sub-group sizes.
    - ``local_mem_size`` (int): device local memory capacity that can be allocated per work-group in bytes.
    - ``has_fp16`` (bool): whether float16 dtype is supported.
    - ``has_fp64`` (bool): whether float64 dtype is supported.
    - ``has_atomic64`` (bool): whether 64-bit atomic operations are supported.
    - ``has_bfloat16_conversions`` (bool): whether bfloat16 conversions are supported.
    - ``has_subgroup_matrix_multiply_accumulate`` (bool): whether DPAS (Dot Product Accumulate Systolic) is supported.
    - ``has_subgroup_matrix_multiply_accumulate_tensor_float32`` (bool): whether DPAS with tf32 inputs is supported.
    - ``has_subgroup_2d_block_io`` (bool): whether 2D block I/O for efficient matrix multiplication is supported.
    - ``total_memory`` (int): device global memory in bytes.
    - ``gpu_subslice_count`` (int): number of subslice.
    - ``architecture`` (int): device architecture identifier (experimental).
    - ``type`` (str): device type, e.g. 'cpu', 'gpu', accelerator', 'host', 'unknown'.
    - ``uuid`` (Any): device UUID (Universal Unique ID), 16 bytes.

    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
    Tr   )rS   r   _get_device_propertiesr,   s    r   r:   r:     s.    N LLLv555F!&)))r   c                 Z    t                       t          j                                        S )z0Return the index of a currently selected device.)rS   r!   r"   _xpu_getDevicer   r   r   current_devicer   =  s    LLL8""$$$r   int | str | torch.devicetorch.devicec                    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.
    r9   )r   rf   r!   r&   r'   r,   s    r   _get_devicer   C  sO     &# -f%%	FC	 	  -eV,,Mr   peerc                    t                       t          | d          } t          |d          }t          j                            | |          S )a/  Query whether a device can access a peer device's memory.

    Args:
        device (torch.device or int or str): selected device.
        peer (torch.device or int or str): peer device to query access to.

    Returns:
        bool: ``True`` if ``device`` can access ``peer``, ``False`` otherwise.
    Tr   )rS   r   r!   r"   _xpu_canDeviceAccessPeer)r&   r   s     r   can_device_access_peerr   P  sI     LLLv555FTD111D8,,VT:::r   c                  4    e Zd ZU dZded<   ddZd Zd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 | None
cur_streamstreamr   rD   c                ^    || _         t          d d          | _        | j        	d| _        d S d S )NTrr   )r   r   rs   )ru   r   s     r   rv   zStreamContext.__init__n  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 rq   )	r   rs   r!   r9   current_streamsrc_prev_streamr&   dst_prev_stream
set_stream)ru   r   s     r   rz   zStreamContext.__enter__t  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r|   r   r}   rK   c                    | j         }|| j        dk    rd S | j        j        |j        k    r$t          j                            | j                   t          j                            | j                   d S rq   )r   rs   r   r&   r!   r9   r   r   )ru   r|   r}   rK   r   s        r   r   zStreamContext.__exit__  sl    [
RF &**;;;I  !5666	T122222r   N)r   r   r   rD   r   )r   r   r   r   __annotations__rv   rz   r   r   r   r   r   r   `  se         	 	 ('''   
) 
) 
)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     s6     
H!      r   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   )rS   r   r   r   r   r   s    r   r   r     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).
    Tr   r   r      r   )rS   r!   r"   _xpu_getCurrentStreamr   r   )r&   
streamdatas     r   r   r     sa     LLL//&4000 J Q-jmTU   r   data_ptrc                    t                       t          j                            | t	          |d                    }t          |d         |d         |d                   S )a;  Return a :class:`Stream` from an external SYCL queue.

    This function is used to wrap SYCL queue created in other libraries in order
    to facilitate data exchange and multi-library interactions.

    .. note:: This function doesn't manage the queue life-cycle, it is the user
       responsibility to keep the referenced queue alive while this returned stream is
       being used. The different SYCL queue pointers will result in distinct
       :class:`Stream` objects, even if the SYCL queues they dereference are equivalent.

    Args:
        data_ptr(int): Integer representation of the `sycl::queue*` value passed externally.
        device(torch.device or int, optional): the device where the queue was originally created.
            It is the user responsibility to ensure the device is specified correctly.
    Tr   r   r   r   r   )rS   r!   r"   _xpu_getStreamFromExternalr   r   )r   r&   r   s      r   get_stream_from_externalr     sd      LLL44#FT::: 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).
    Tr   )rS   r   r!   r"   _xpu_synchronizer,   s    r   synchronizer     s5     LLLv555F8$$V,,,r   	list[str]c                     t                      sg S t          j                                        } | g S |                                 S )z<Return list XPU architectures this library was compiled for.)r$   r!   r"   _xpu_getArchFlagssplit)
arch_flagss    r   get_arch_listr     sC    >> 	++--J	r   c                     t                      } t          |           dk    rdS dd                    d | D                        S )zIReturn XPU AOT(ahead-of-time) build flags this library was compiled with.r   r\   z-device ,c              3     K   | ]}|V  d S rX   r   )rY   archs     r   r[   z$get_gencode_flags.<locals>.<genexpr>  s"      ::t::::::r   )r   lenrg   )	arch_lists    r   get_gencode_flagsr     sJ    I
9~~r<chh::	:::::<<<r   torch._C.Generatorc                ^    | j         }|t                      }t          j        j        |         S )zuReturn the XPU Generator object for the given device.

    Args:
        device (torch.device): selected device.
    )ro   r   r!   r9   r   )r&   rs   s     r   _get_generatorr     s+     ,C
{9',,r   r9   offsetc                T     t          |          d 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).
    r   rD   c                 P    t                    } |                                d S rX   )r   
set_offset)default_generatorfinal_devicer   s    r   cbz!_set_rng_state_offset.<locals>.cb  s+    *<88$$V,,,,,r   Nr   )r   rQ   )r   r&   r   r   s   `  @r   _set_rng_state_offsetr     sG     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.
    )rS   r   r   
get_offset)r&   r   r   s      r   _get_rng_state_offsetr     s:     LLLv&&L&|44'')))r   )change_current_allocatorempty_cacheget_per_process_memory_fractionmax_memory_allocatedmax_memory_reservedmem_get_infomemory_allocatedmemory_reservedmemory_snapshotmemory_statsmemory_stats_as_nested_dictMemPoolreset_accumulated_memory_statsreset_peak_memory_statsset_per_process_memory_fractionuse_mem_poolXPUPluggableAllocator)	get_rng_stateget_rng_state_allinitial_seedmanual_seedmanual_seed_allrG   rF   set_rng_stateset_rng_state_all_POOL_HANDLE)9r   r   r   r	  r   r   r   r   r   r   r&   r   r4   r   r   r   r   r:   r   r   r
  r  r   r   r   rT   r  r6   r<   r   rC   r?   r   r  r  r   r   r   r   r   r  r  r  r  r  r  r  rG   rF   r   r  r  r  r   r   streamsr   )r   r   )r&   r'   r   r'   )r   r'   )T)r7   r   r   r   r   )r&   r   r   rD   rX   )r&   r   r   rf   )r&   r   r   r   )r&   r   r   r%   )r&   r   r   r   )r&   r   r   r   r   r   )r   r   r   r   )r   r   r   rD   )r&   r   r   r   )r   r'   r&   r   r   r   )r   r   )r   rf   )r&   r   r   r   )r9   )r   r'   r&   r   r   rD   )r&   r   r   r'   )or   
__future__r   	threadingrK   	functoolsr   typingr   r   r   r!   torch._Ctorch._utilsr   r	   collections.abcr
   torch.typesr   _utilsr   graphsr   r   r   r   r   r  r   r   rA   localr^   Lockr_   r   r   r   r"   rB   rI   r   r$   r%   _xpu_exchangeDevicer-   _xpu_maybeExchangeDevicer0   r4   r6   r<   r?   rC   rQ   rT   rS   rn   r&   r   r   r   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  r  r  r  r  r  r  r  r	  randomr
  r  r  r  r  rG   rF   r  r  r   r'   r  __all__r   r   r   <module>r$     s     # " " " " "               . . . . . . . . . .   6 6 6 6 6 6 6 6  #(((((("""""" % % % % % %              # " " " " " " " y%y~''        '%($6FF%%'' 02  2 2 2 2   
 <>> N 88x3"X> ';'=>>N N N NN N N N 1* * * *       U U U U2 2 2
G G G G   % % % %P
 
 
 
 
 
 
 
       (       
( 
( 
( 
(. . . . . 4    2 )* )* )* )* )*X% % % %
 
 
 
; ; ; ; (3 (3 (3 (3 (3 (3 (3 (3V! ! ! !      &    $    2
- 
- 
- 
- 
-   = = = =	- 	- 	- 	- 5:    &* * * * *"                                     &
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 w~uS#X77: : :r   