
    ΅i(K                        % S r SSKrSSKrSSKJr  SSKJr  SSKJrJ	r	  SSK
r
SSKr
SSKJrJr  SSKJr  SS	KJr  SS
KJrJr  Sq\R.                  " 5       r\R2                  " 5       r/ q\\\/ S4   \\   4      \S'   \ " \
RB                  SS 5      r"\" 5       q#Sr$\\
RB                  RJ                     \S'   S\&4S jr'\'" 5       (       aC  \
RB                  RP                  r(\
RB                  RR                  r*\
RB                  RV                  r,O\" S5      r(S\-S\-4S jr*S\-S\-4S jr,\" SS9S\-4S j5       r.S\&4S jr/SBS\&S\&4S jjr0S\&4S jr1S r2SCS jr3SCS jr4SCS  jr5 " S! S"5      r6 " S# S5      r7 " S$ S%\75      r8S\SS4S& jr9SDS\S\4S' jjr:\" S5      SDS\S\;\\4   4S( jj5       r< SDS\S\(4S) jjr=S\-4S* jr>S\-\-  \
Rn                  -  S\
Rn                  4S+ jr?S\S,\S\&4S- jr@ " S. S/5      rAS0\	S1   S\A4S2 jrBSCS3 jrCS0\SS4S4 jrDSDS\S\4S5 jjrESDS6\-S\S\4S7 jjrFSDS\SS4S8 jjrGS\\   4S9 jrHS\4S: jrIS\
Rn                  S\
RB                  RJ                  4S; jrJ SES<\-S\-\-  \
Rn                  -  SS4S= jjrKSES\-\-  \
Rn                  -  S\-4S> jjrLSS?KMJNrNJOrOJPrPJQrQJRrRJSrSJTrTJUrUJVrVJWrWJXrXJYrYJZrZJ[r[  SS@K\J]r]J^r^J_r_J`r`JaraJbrbJcrcJdrdJere  / SAQrfg)Fz
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Optional)_dummy_type_LazySeedTracker)Device   )_get_device_index)EventStreamF_queued_calls_xpu_isInBadForkc                      gNF r       L/home/james-whalen/.local/lib/python3.13/site-packages/torch/xpu/__init__.py<lambda>r      s    r   r   default_generatorsreturnc                  6    [         R                  R                  $ )z(Return true if compile with XPU support.)torch_C_has_xpur   r   r   _is_compiledr   $   s    88r   _XpuDevicePropertiesdevicec                     [        S5      eNz(PyTorch was compiled without XPU supportNotImplementedErrorr   s    r   _exchange_devicer$   1       !"LMMr   c                     [        S5      er    r!   r#   s    r   _maybe_exchange_devicer'   4   r%   r   )maxsizec                  ^    [        5       (       d  g[        R                  R                  5       $ )z*Return the number of XPU device available.r   )r   r   r   _xpu_getDeviceCountr   r   r   device_countr+   8   s!     >>88''))r   c                      [        5       S:  $ )z7Return a bool indicating if XPU is currently available.r   )r+   r   r   r   is_availabler-   @   s     >Ar   including_emulationc                     [        5       (       d  gU =(       d(    [        R                  R                  5       R                  $ )zKReturn a bool indicating if the current XPU device supports dtype bfloat16.F)r-   r   xpuget_device_propertieshas_bfloat16_conversions)r.   s    r   is_bf16_supportedr3   F   s0    >> 	F99**,EEr   c                  r    [        5       (       d  g[        R                  R                  5       R                  $ )zGReturn a bool indicating if the current XPU device supports dtype tf32.F)r-   r   r0   r1   'has_subgroup_matrix_multiply_accumulater   r   r   is_tf32_supportedr6   P   s'    >>
 99**,TTTr   c                  :    [         =(       a    [        5       (       + $ )z8Return whether PyTorch's XPU state has been initialized.)_initialized_is_in_bad_forkr   r   r   is_initializedr:   [   s    1 111r   c                    [        5       (       a  U " 5         g UR                  SS5      (       a*  [        R                  U [        R
                  " 5       5        g UR                  SS5      (       a*  [        R                  U [        R
                  " 5       5        g [        R                  U [        R
                  " 5       45        g )Nseed_allFseed)	r:   get_lazy_seed_trackerqueue_seed_all	tracebackformat_stack
queue_seedr   append)callablekwargss     r   
_lazy_callrG   `   s    
 ::j%((--h	8N8N8PQZZ&&))(I4J4J4LM   (I,B,B,D!EFr   c                      [        5         g)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   initrJ   n   s	     Lr   c            	         [        5       (       d  [        [        S5      (       a  g [           [        5       (       a
   S S S 5        g [	        5       (       a  [        S5      e[        5       (       d  [        S5      e[        R                  R                  5         S[        l        [        R                  S [        R                  5        5       5         [         H  u  p U " 5         M     ['        [        S5        SqS S S 5        g ! [          a1  nS[#        U5       SSR%                  U5       3n[!        U5      UeS nAff = f! ['        [        S5        f = f! , (       d  f       g = f)	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   6   #    U  H  o(       d  M  Uv   M     g 7fNr   ).0callss     r   	<genexpr>_lazy_init.<locals>.<genexpr>   s     X0NuRWUU0Ns   
	z5XPU call failed lazily at initialization with error: z'

XPU call was originally invoked at:

 )r:   hasattr_tls_initialization_lockr9   RuntimeErrorr   AssertionErrorr   r   	_xpu_initrL   r   extendr?   	get_calls	Exceptionstrjoindelattrr8   )queued_callorig_tracebackemsgs       r   rI   rI   w   s=   74):;;	 	 
	 I  ~~ !FGG  $X0B0L0L0NXX	-/<+0M 0= D+,C 
	2 ! 0OPSTUPVx XBBD''.BYAZ\  $C.a/0 D+,A 
	sN   EBEED%E)E
D?,D::D??EEE
E%c                   B    \ rS rSrS\SS4S jrS rS\S\S	\4S
 jrSr	g)_DeviceGuard   indexr   Nc                     Xl         SU l        g Nidxprev_idx)selfrg   s     r   __init___DeviceGuard.__init__   s    r   c                 `    [         R                  R                  U R                  5      U l        g rN   r   r0   r$   rl   rm   rn   s    r   	__enter___DeviceGuard.__enter__       		22488<r   typevaluerA   c                 `    [         R                  R                  U R                  5      U l        gr   r   r0   r'   rm   rl   rn   rw   rx   rA   s       r   __exit___DeviceGuard.__exit__       9933DMMBr   rk   )
__name__
__module____qualname____firstlineno__intro   rt   r   r|   __static_attributes__r   r   r   re   re      s4    c d =S   r   re   c                   F    \ rS rSrSrS \SS4S jrS rS\S\S	\4S
 jrSr	g)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                 0    [        USS9U l        SU l        g )NToptionalrj   )r   rl   rm   )rn   r   s     r   ro   device.__init__   s    $Vd;r   c                 `    [         R                  R                  U R                  5      U l        g rN   rr   rs   s    r   rt   device.__enter__   rv   r   rw   rx   rA   c                 `    [         R                  R                  U R                  5      U l        gr   rz   r{   s       r   r|   device.__exit__   r~   r   rk   )
r   r   r   r   __doc__r   ro   rt   r|   r   r   r   r   r   r      s9    s t =S   r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )	device_of   a	  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   > UR                   (       a  UR                  5       OSn[        TU ]  U5        g ri   )is_xpu
get_devicesuperro   )rn   objrl   	__class__s      r   ro   device_of.__init__   s$    "%**cnn"r   r   r   N)r   r   r   r   r   ro   r   __classcell__)r   s   @r   r   r      s     r   r   c                 z    [        5         [        U 5      n U S:  a   [        R                  R	                  U 5        gg)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)rI   r   r   r   _xpu_setDevicer#   s    r   
set_devicer      s1     Lv&F{' r   c                 ,    [        U 5      R                  $ )am  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
)r1   namer#   s    r   get_device_namer      s     !(---r   c           	      "   [        U 5      n[        [        [        [        [        S5      [        [        [        4n[        U5       Vs0 s H:  nUR                  S5      (       a  M  [        [        X5      =oB5      (       d  M8  X4_M<     sn$ s  snf )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__)r1   r   floatboolr]   rw   listtupledictdir
startswith
isinstancegetattr)r   propsserializable_typeskeyrx   s        r   get_device_capabilityr      s}     "&)EudCdT5$O u:C~~d# 	 !44J 	
  s   B&BBc                 @    [        5         [        U SS9n [        U 5      $ )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
Tr   )rI   r   _get_device_propertiesr#   s    r   r1   r1     s      Lv5F!&))r   c                  R    [        5         [        R                  R                  5       $ )z0Return the index of a currently selected device.)rI   r   r   _xpu_getDevicer   r   r   current_devicer     s    L88""$$r   c                     [        U [        5      (       a  [        R                  " U 5      n U $ [        U [        5      (       a  [        R                  " SU 5      n U $ )zReturn the torch.device type object from the passed in device.

Args:
    device (torch.device or int or str): selected device.
r0   )r   r]   r   r   r   r#   s    r   _get_devicer     sJ     &#f% M 
FC	 	 eV,Mr   peerc                 |    [        5         [        U SS9n [        USS9n[        R                  R	                  X5      $ )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   )rI   r   r   r   _xpu_canDeviceAccessPeer)r   r   s     r   can_device_access_peerr   (  s6     Lv5FTD1D88,,V::r   c                   ^    \ rS rSr% Sr\S   \S'   S\S   SS4S jrS	 rS
\	S\	S\	4S jr
Srg)StreamContexti8  a   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                 \    Xl         [        S S5      U l        U R                  c  SU l        g g )NTrj   )r   r   rl   )rn   r   s     r   ro   StreamContext.__init__F  s+    $T4088DH r   c                    U R                   nUb  U R                  S:X  a  g [        R                  R	                  S 5      U l        U R
                  R                  UR                  :w  aL  [        UR                  5         [        R                  R	                  UR                  5      U l        S S S 5        [        R                  R                  U5        g ! , (       d  f       N.= fri   )	r   rl   r   r0   current_streamsrc_prev_streamr   dst_prev_stream
set_stream)rn   r   s     r   rt   StreamContext.__enter__L  s    [[
R$yy77= &&**;*;;
))*',yy'?'?
@Q@Q'R$ +		Z( +*s   >/C
C#rw   rx   rA   c                 0   U R                   nUb  U R                  S:X  a  g U R                  R                  UR                  :w  a)  [        R
                  R                  U R                  5        [        R
                  R                  U R                  5        g ri   )r   rl   r   r   r   r0   r   r   )rn   rw   rx   rA   r   s        r   r|   StreamContext.__exit__X  sj    [[
R &&**;*;;II  !5!56		T112r   )r   rl   r   r   )r   r   r   r   r   r   __annotations__ro   rt   r   r|   r   r   r   r   r   r   8  sM    	 +,,x(:;  
)3S 3 3 3r   r   r   r   c                     [        U 5      $ )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   c  s       r   c                 B    [         R                  R                  U UUS9  g)zset 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   l  s$     
HH!  r   c                 r    U c  g[        5         [        U R                  U R                  U R                  S9  g)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   )rI   r   r   r   r   r   s    r   r   r   z  s4     ~L""((&&r   c                     [        5         [        R                  R                  [	        U SS95      n[        US   US   US   S9$ )a:  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   )rI   r   r   _xpu_getCurrentStreamr   r   )r   
streamdatas     r   r   r     sJ     L//&40J Q-jmTU r   data_ptrc                     [        5         [        R                  R                  U [	        USS95      n[        US   US   US   S9$ )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   )rI   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                 h    [        5         [        U SS9n [        R                  R	                  U 5      $ )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   )rI   r   r   r   _xpu_synchronizer#   s    r   synchronizer     s*     Lv5F88$$V,,r   c                      [        5       (       d  / $ [        R                  R                  5       n U c  / $ U R	                  5       $ )z<Return list XPU architectures this library was compiled for.)r   r   r   _xpu_getArchFlagssplit)
arch_flagss    r   get_arch_listr     s:    >>	++-J	r   c                  l    [        5       n [        U 5      S:X  a  gSSR                  S U  5       5       3$ )zIReturn XPU AOT(ahead-of-time) build flags this library was compiled with.r   rS   z-device ,c              3   $   #    U  H  ov   M     g 7frN   r   )rO   archs     r   rQ   $get_gencode_flags.<locals>.<genexpr>  s     :	t	s   )r   lenr^   )	arch_lists    r   get_gencode_flagsr     s5    I
9~chh:	::;<<r   c                 n    U R                   nUc
  [        5       n[        R                  R                  U   $ )ziReturn the XPU Generator object for the given device.

Args:
    device (torch.device): selected device.
)rg   r   r   r0   r   )r   rl   s     r   _get_generatorr     s/     ,,C
{99'',,r   offsetc                 F   ^ ^ [        U5      mSUU 4S jjn[        U5        g)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5      n U R                  T5        g rN   )r   
set_offset)default_generatorfinal_devicer   s    r   cb!_set_rng_state_offset.<locals>.cb  s    *<8$$V,r   r   )r   rG   )r   r   r   r   s   `  @r   _set_rng_state_offsetr     s"     v&L- - rNr   c                 b    [        5         [        U 5      n[        U5      nUR                  5       $ )a4  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.
)rI   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_statsmemory_stats_as_nested_dictreset_accumulated_memory_statsreset_peak_memory_statsset_per_process_memory_fractionXPUPluggableAllocator)	get_rng_stateget_rng_state_allinitial_seedmanual_seedmanual_seed_allr=   r<   set_rng_stateset_rng_state_all)1r   r   r   r	  r   r   r   r   r   r   r   r+   r   r   r   r   r1   r   r   r
  r  r   rJ   r  r-   r3   r:   r6   r  r  r   r   r  r  r  r  r  r  r  r=   r<   r   r  r  r  r   r   streamsr   )Tr   rN   )r0   )gr   	threadingrA   collections.abcr   	functoolsr   typingr   r   r   torch._Ctorch._utilsr   r   torch.typesr	   _utilsr   r  r   r   r8   localrU   LockrV   r   r   r   r]   r   r   r   r9   r?   r   	Generatorr   r   r   _xpu_exchangeDevicer$   _xpu_maybeExchangeDevicer'   r   r+   r-   r3   r6   r:   rG   rJ   rI   re   r   r   r   r   r   r   r1   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	  randomr
  r  r  r  r  r=   r<   r  r  __all__r   r   r   <module>r"     s     $      6  % "  ~~'   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 ***"% %
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 *"    
 
 
2r   