
    iT                        U d Z ddlZddlZddlm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 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"e#e$eg df   e#e%   f      e&d<    e'ejP                  dd       Z) e       a*dZ+e$ejP                  jX                     e&d<   de-fdZ. e.       rCejP                  j^                  Z/ejP                  j`                  Z1ejP                  jd                  Z3n ed      Z/de4de4fdZ1de4de4fdZ3 ed      de4fd       Z5de-fdZ6dDde-de-fdZ7de-fdZ8d Z9dEdZ:dEd Z;dEd!Z< G d" d#      Z= G d$ d      Z> G d% d&e>      Z?deddfd'Z@dFdede%fd(ZA ed      dFdedeBe%ef   fd)       ZC	 dFdede/fd*ZDde4fd+ZEde4e%z  ej|                  z  dej|                  fd,ZFded-ede-fd.ZG G d/ d0      ZHd1e
d2   deHfd3ZIdEd4ZJd1eddfd5ZKdFdedefd6ZLdFd7e4dedefd8ZMdFdeddfd9ZNde#e%   fd:ZOde%fd;ZPdej|                  dejP                  jX                  fd<ZQ	 dGd=e4de4e%z  ej|                  z  ddfd>ZRdGde4e%z  ej|                  z  de4fd?ZSdd@lTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZe ddAlfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZo  e	dBe$e4e4f         Zpg dCZqy)Hz
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)Callable)	lru_cache)AnyNewTypeOptional)_dummy_type_LazySeedTracker)Device   )_get_device_index)graphgraph_pool_handleis_current_stream_capturingmake_graphed_callablesXPUGraph)EventStreamF_queued_calls_xpu_isInBadForkc                       yNF r       c/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/torch/xpu/__init__.py<lambda>r   &   s    r   r   default_generatorsreturnc                  6    t         j                  j                  S )z(Return true if compile with XPU support.)torch_C_has_xpur   r   r   _is_compiledr"   +   s    88r   _XpuDevicePropertiesdevicec                     t        d      Nz(PyTorch was compiled without XPU supportNotImplementedErrorr$   s    r   _exchange_devicer*   8       !"LMMr   c                     t        d      r&   r'   r)   s    r   _maybe_exchange_devicer-   ;   r+   r   )maxsizec                  T    t               syt        j                  j                         S )z*Return the number of XPU device available.r   )r"   r   r    _xpu_getDeviceCountr   r   r   device_countr1   ?   s     >88''))r   c                      t               dkD  S )z7Return a bool indicating if XPU is currently available.r   )r1   r   r   r   is_availabler3   G   s     >Ar   including_emulationc                 p    t               sy| xs( t        j                  j                         j                  S )zKReturn a bool indicating if the current XPU device supports dtype bfloat16.F)r3   r   xpuget_device_propertieshas_bfloat16_conversions)r4   s    r   is_bf16_supportedr9   M   s.    > 	F99**,EEr   c                  h    t               syt        j                  j                         j                  S )zGReturn a bool indicating if the current XPU device supports dtype tf32.F)r3   r   r6   r7   'has_subgroup_matrix_multiply_accumulater   r   r   is_tf32_supportedr<   W   s%    >
 99**,TTTr   c                  (    t         xr t                S )z8Return whether PyTorch's XPU state has been initialized.)_initialized_is_in_bad_forkr   r   r   is_initializedr@   b   s    1 111r   c                 f   t               r |         y |j                  dd      r)t        j                  | t	        j
                                y |j                  dd      r)t        j                  | t	        j
                                y t        j                  | t	        j
                         f       y )Nseed_allFseed)	r@   get_lazy_seed_trackerqueue_seed_all	tracebackformat_stack
queue_seedr   append)callablekwargss     r   
_lazy_callrM   g   s{    
 ::j%(--h	8N8N8PQZZ&))(I4J4J4LM   (I,B,B,D!EFr   c                      t                y)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   initrP   u   s	     Lr   c            	         t               st        t        d      ry t        5  t               r
	 d d d        y t	               rt        d      t               st        d      t        j                  j                          dt        _        t        j                  d t        j                         D               	 t        D ]  \  } }	  |          	 t'        t        d       dad d d        y # t         $ r1}dt#        |       ddj%                  |       }t!        |      |d }~ww xY w# t'        t        d       w xY w# 1 sw Y   y xY w)	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   | ]	  }|s|  y wNr   ).0callss     r   	<genexpr>z_lazy_init.<locals>.<genexpr>   s     XuRWUXs   z5XPU call failed lazily at initialization with error: z'

XPU call was originally invoked at:

 )r@   hasattr_tls_initialization_lockr?   RuntimeErrorr"   AssertionErrorr   r    	_xpu_initrR   r   extendrE   	get_calls	Exceptionstrjoindelattrr>   )queued_callorig_tracebackemsgs       r   rO   rO   ~   sA   74):;	 ! 	! ! I  ~ !FGG  $X0B0L0L0NXX	-/< 0+^0M0 D+,C! !2 ! 0OPSTUPVx XBBD''.BYAZ\  $C.a/0 D+,A! !sM   D=BD=9D(C+D(D=+	D%4,D  D%%D((D::D==Ec                   6    e Zd ZdeddfdZd Zdededefd	Zy)
_DeviceGuardindexr   Nc                      || _         d| _        y N)idxprev_idx)selfrk   s     r   __init__z_DeviceGuard.__init__   s    r   c                 `    t         j                  j                  | j                        | _        y rT   r   r6   r*   ro   rp   rq   s    r   	__enter__z_DeviceGuard.__enter__       		22488<r   typevaluerG   c                 `    t         j                  j                  | j                        | _        yr   r   r6   r-   rp   ro   rq   rx   ry   rG   s       r   __exit__z_DeviceGuard.__exit__       9933DMMBr   )__name__
__module____qualname__intrr   rv   r   r}   r   r   r   rj   rj      s4    c d =S   r   rj   c                   :    e Zd ZdZd eddfdZd Zdededefd	Zy)
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   Nc                 6    t        |d      | _        d| _        y )NToptionalrn   )r   ro   rp   )rq   r$   s     r   rr   zdevice.__init__   s    $Vd;r   c                 `    t         j                  j                  | j                        | _        y rT   rt   ru   s    r   rv   zdevice.__enter__   rw   r   rx   ry   rG   c                 `    t         j                  j                  | j                        | _        yr   r{   r|   s       r   r}   zdevice.__exit__   r~   r   )r   r   r   __doc__r   rr   rv   r}   r   r   r   r$   r$      s9    s t =S   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.
    c                 `    |j                   r|j                         nd}t        |   |       y rm   )is_xpu
get_devicesuperrr   )rq   objro   	__class__s      r   rr   zdevice_of.__init__   s$    "%**cnn"r   r   N)r   r   r   r   rr   __classcell__)r   s   @r   r   r      s     r   r   c                 x    t                t        |       } | dk\  r t        j                  j	                  |        yy)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)rO   r   r   r    _xpu_setDevicer)   s    r   
set_devicer      s1     L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
    )r7   namer)   s    r   get_device_namer      s     !(---r   c           	         t        |       }t        t        t        t        t        d      t        t        t        f}t        |      D ci c].  }|j                  d      st        t        ||      x}|      r||0 c}S c c}w )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
    N__)r7   r   floatboolrb   rx   listtupledictdir
startswith
isinstancegetattr)r$   propsserializable_typeskeyry   s        r   get_device_capabilityr      sv     "&)EudCdT5$O u:~~d#!447IJ 	U
  s   3A>c                 F    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.
    - ``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   )rO   r   _get_device_propertiesr)   s    r   r7   r7     s!    J Lv5F!&))r   c                  R    t                t        j                  j                         S )z0Return the index of a currently selected device.)rO   r   r    _xpu_getDevicer   r   r   current_devicer   5  s    L88""$$r   c                     t        | t              rt        j                  |       } | S 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.
    r6   )r   rb   r   r$   r   r)   s    r   _get_devicer   ;  sD     &#f% M 
FC	 eV,Mr   peerc                     t                t        | d      } t        |d      }t        j                  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   )rO   r   r   r    _xpu_canDeviceAccessPeer)r$   r   s     r   can_device_access_peerr   H  s8     Lv5FTD1D88,,VT::r   c                   R    e Zd ZU dZed   ed<   ded   ddfdZd Zd	ed
edefdZ	y)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streamr   Nc                 \    || _         t        d d      | _        | j                  d| _        y y )NTrn   )r   r   ro   )rq   r   s     r   rr   zStreamContext.__init__f  s-    $T4088DH r   c                    | j                   }|| j                  dk(  ry t        j                  j	                  d       | _        | j
                  j                  |j                  k7  rLt        |j                        5  t        j                  j	                  |j                        | _        d d d        t        j                  j                  |       y # 1 sw Y   )xY wrm   )	r   ro   r   r6   current_streamsrc_prev_streamr$   dst_prev_stream
set_stream)rq   r   s     r   rv   zStreamContext.__enter__l  s    [[
R$yy77= &&**;*;;
))* S',yy'?'?
@Q@Q'R$S		Z(S Ss   ;/CCrx   ry   rG   c                 *   | j                   }|| j                  dk(  ry | j                  j                  |j                  k7  r)t        j
                  j                  | j                         t        j
                  j                  | j                         y rm   )r   ro   r   r$   r   r6   r   r   )rq   rx   ry   rG   r   s        r   r}   zStreamContext.__exit__x  sj    [[
R &&**;*;;II  !5!56		T112r   )
r   r   r   r   r   __annotations__rr   rv   r   r}   r   r   r   r   r   X  sM    	 +,,x(:;  
)3S 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                 H    t         j                  j                  | ||       y)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     s$     
HH!  r   c                 v    | yt                t        | j                  | j                  | j                         y)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   )rO   r   r   r   r   r   s    r   r   r     s4     ~L""((&&r   c                     t                t        j                  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   )rO   r   r    _xpu_getCurrentStreamr   r   )r$   
streamdatas     r   r   r     sJ     L//&40J Q-jmTU r   data_ptrc                     t                t        j                  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   )rO   r   r    _xpu_getStreamFromExternalr   r   )r   r$   r   s      r   get_stream_from_externalr     sM      L44#FT:J Q-jmTU r   c                 n    t                t        | d      } t        j                  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   )rO   r   r   r    _xpu_synchronizer)   s    r   synchronizer     s*     Lv5F88$$V,,r   c                  ~    t               sg S t        j                  j                         } | g S | j	                         S )z<Return list XPU architectures this library was compiled for.)r"   r   r    _xpu_getArchFlagssplit)
arch_flagss    r   get_arch_listr     s8    >	++-J	r   c                  j    t               } t        |       dk(  ryddj                  d | D               S )zIReturn XPU AOT(ahead-of-time) build flags this library was compiled with.r   rX   z-device ,c              3       K   | ]  }|  y wrT   r   )rU   archs     r   rW   z$get_gencode_flags.<locals>.<genexpr>  s     :t:s   )r   lenrc   )	arch_lists    r   get_gencode_flagsr     s5    I
9~chh:	::;<<r   c                 l    | j                   }|
t               }t        j                  j                  |   S )zuReturn the XPU Generator object for the given device.

    Args:
        device (torch.device): selected device.
    )rk   r   r   r6   r   )r$   ro   s     r   _get_generatorr     s/     ,,C
{99'',,r   offsetc                 B     t        |      d fd}t        |       y)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).
    Nc                  >    t              } | j                         y rT   )r   
set_offset)default_generatorfinal_devicer   s    r   cbz!_set_rng_state_offset.<locals>.cb  s    *<8$$V,r   r   )r   rM   )r   r$   r   r   s   `  @r   _set_rng_state_offsetr     s     v&L- rNr   c                 b    t                t        |       }t        |      }|j                         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.
    )rO   r   r   
get_offset)r$   r   r   s      r   _get_rng_state_offsetr     s-     Lv&L&|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_allrC   rB   set_rng_stateset_rng_state_all_POOL_HANDLE)9r   r   r   r   r   r   r   r   r   r   r$   r   r1   r   r   r   r   r7   r   r   r   r  r   r   r   rP   r  r3   r9   r   r@   r<   r   r  r  r   r   r   r   r   r   r   r   r   r   r   r   rC   rB   r   r   r  r  r   r   streamsr   )Tr   rT   )r6   )rr   	threadingrG   collections.abcr   	functoolsr   typingr   r   r   r   torch._Ctorch._utilsr   r	   torch.typesr
   _utilsr   graphsr   r   r   r   r   r  r   r   r>   localrZ   Lockr[   r   r   r   rb   r   r   r    r?   rE   r   	Generatorr   r"   r#   _xpu_exchangeDevicer*   _xpu_maybeExchangeDevicer-   r   r1   r3   r9   r<   r@   rM   rP   rO   rj   r$   r   r   r   r   r   r7   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  rC   rB   r  r  r  __all__r   r   r   <module>r     s     $  ) )   6  %  # y%y~~'   t	(2t8
d3i
'(  %(($6F%' 02 E%((,,- 2d 
 > 8888xx33"XX>> ''=>N N NNs Ns N 1*c * *d 4 4 U4 U2
G%P
 
 ( 
(v 
($ 
(.F .c . 4& DcN  2 '*'*'*T% %
c	ELL0 
U\\ 
;6 ; ;D ; (3 (3V!8./ !M !v $ &6 V $s F f 2
- 
-$ 
-tCy =3 =	-5<< 	-EHH,>,> 	- 5:sU\\1	&*#)ell": *s *"    &
 
 
 ~uS#X7:r   