
    Vpf+                       d dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dl	mZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ dZdZ eej        ej        ej        ej        ej        ej        ej        ej        ej         ej!        ej"        ej#        ej$        ej%        ej&        h          Z'	 	 	 d"d#dZ(	 	 	 	 	 d$d%dZ)d Z*	 	 d&d'dZ+	 	 d&d'dZ,	 	 d&d(d!Z-dS ))    )annotations)Any)
device_put)numpy)array)
xla_bridge)_array_copy)
xla_client)ArrayDLDeviceType)Sharding)r      )r      Nxr   streamint | Any | None
src_devicexla_client.Device | Nonedevicecopybool | Nonec                D   ||                                  \  }|r@|||k    r8|%|s#t          d|dt          |           d          t          | |          }n|rt	          |           n| }t
          j                            |                    d          |          S )NSpecified device=2 which requires a copy since the source device is T, however copy=False. Set copy=True or copy=None to perform the requested operation.r   r   )	devices
ValueErrorreprr   r	   r
   _xlabuffer_to_dlpack_managed_tensoraddressable_data)r   r   r   r   r   arrs         O/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/dlpack.py
_to_dlpackr%   ,   s    
 ))++KJ 
(#v';';	8V 	8 	8:	8 	8 	8   q&!!cc 
'+a...aC		8	8F 
9 
 
     	dl_devicetuple[DLDeviceType, int] | Nonemax_versiontuple[int, int] | Nonec                4   t          | t          j                  st          dt	          |                      d}|r|nd\  }}|rx	 t
          j        dt
          j        dt
          j        di|         }	t          j
        |	          }
|
                    |          }n!# t          $ r t          d| d          w xY w||t          k    rt          | ||||	          S |t          k    rt          | ||||	          S t          d
t           d| d          )at  Returns a DLPack tensor that encapsulates a :class:`~jax.Array` ``x``.

  Args:
    x: a :class:`~jax.Array`, on either CPU or GPU.
    stream: optional platform-dependent stream to wait on until the buffer is
      ready. This corresponds to the `stream` argument to ``__dlpack__``
      documented in https://dmlc.github.io/dlpack/latest/python_spec.html.
    src_device: either a CPU or GPU :class:`~jax.Device`.
    dl_device: a tuple of ``(dl_device_type, local_hardware_id)`` in DLPack
      format e.g. as produced by ``__dlpack_device__``.
    max_version: the maximum DLPack version that the consumer (i.e. caller of
      ``__dlpack__``) supports in the form of a 2-tuple of ``(major, minor)``.
      This function is not guaranteed to return a capsule of version
      ``max_version``.
    copy: a boolean indicating whether or not to copy the input. If
      ``copy=True`` then the function must always copy. When
      ``copy=False`` then the function must never copy, and must raise an error
      when a copy is deemed necessary. If ``copy=None`` then the function must
      avoid a copy if possible but may copy if needed.

  Returns:
    A DLPack PyCapsule object.

  Note:
    While JAX arrays are always immutable, ``DLPackManagedTensor`` buffers
    cannot be marked as immutable, and it is possible for processes external
    to JAX to mutate them in-place. If a DLPack buffer derived from a JAX array
    is mutated, it may lead to undefined behavior when using the associated JAX
    array. When JAX eventually supports ``DLManagedTensorVersioned``
    (DLPack 1.0), it will be possible to specify that a buffer is read-only.
  z/Argument to to_dlpack must be a jax.Array, got NNNcpucudarocmz`The device specification passed to to_dlpack contains an unsupported device type (DLDeviceType: ))r   r   r   r   z'JAX does not support any version below z but version (z) was requested.)
isinstancer   	ArrayImpl	TypeErrortyper   kDLCPUkDLCUDAkDLROCMr   get_backenddevice_from_local_hardware_idBufferErrorDLPACK_VERSIONr%   MIN_DLPACK_VERSION)r   r   r   r'   r)   r   r   dl_device_typelocal_hardware_iddl_device_platformbackends              r$   	to_dlpackrA   B   s   H 
Au	'	' &
 %77% % & & & &3<&Nii,#.# ;;

u



 		
 &'9::g445FGGff ; ; ; :(6: : :; ; ;; K>99	    (((	    00B 0 00 0 0  s   AB B<c                    |r=||k    r7|%|s#t          d|dt          |           d          t          | |          S |rt          j        | d          S | S )Nr   r   r   T)r   )r   r   r   jnpr   )_arrr   dlpack_devicer   s       r$   _place_arrayrF      s     &''	8V 	8 	8=!!	8 	8 	8   f%%%	 &9T%%%%	+r&   c           	        t          |dd           }|r|dk    rd|j        j        v rdnd}t          j        d          }d }|dv rj	 t          j        |          }nT# t
          $ rG t          dt                              |           dt                              |           d	          w xY w|N	 t          j        d          }n# t
          $ r Y nw xY w|&	 t          j        d          }n# t
          $ r Y nw xY wt          j
        t          j                            | ||                    }|                                \  }t          ||||          S )
Nplatformgpur.   r/   r-   >   r.   r/   zA z" device was specified, however no z backend was found.)getattrclientplatform_versionr   r8   RuntimeErrorr3   strupperrC   asarrayr
   r    dlpack_managed_tensor_to_bufferr   rF   )dlpackr   r   preferred_platformcpu_backendgpu_backendrD   rE   s           r$   _legacy_from_dlpackrV      s   vz488 X"e++#)V]-K#K#KQW&u--+++++*+=>>kk   	>SYY)** 	> 	>99'((	> 	> 	>   *622kk   
d  ,V44    
Z_DDk;( ( 
) 
)$<<>>.-	dFM4	8	88s1   A AB-3C 
CCC0 0
C=<C=c                h   |                                  \  }}	 t          j        dt          j        dt          j        di|         }n## t
          $ r t          d| d|            w xY wt          j        |          }|                    |          }	 |	                                }n1# t          j        $ r}	dt          |	          v rd }n Y d }	~	nd }	~	ww xY w|                     |          }
t          j        t          j                            |
||                    }t%          ||||          S )Nr-   r.   r/   zIArray passed to from_dlpack is on unsupported device type (DLDeviceType: z	, array: UNIMPLEMENTEDr   )__dlpack_device__r   r5   r6   r7   r3   r   r8   r9   $get_stream_for_external_ready_eventsr
   XlaRuntimeErrorrN   
__dlpack__rC   rP   r    rQ   rF   )external_arrayr   r   r=   	device_idr?   r@   rE   r   errrR   rD   s               r$   _from_dlpackr`      s   ,>>@@.)EUff 	
 
 E E E 	D(	D 	D3A	D 	DE E EE "#566'77	BB-??AAFF		#   #c((""ff ffff
 $$F$33&	Z_DDmV% % 
& 
&$	dFM4	8	88s#   ,A  A&B( (C7CC#xla_client.Device | Sharding | Nonec                   t          |t                    r>|j        }t          |          dk    r t	          dt          |           d          |\  }t          | d          rt          | ||          S t          | ||          S )a  Returns a :class:`~jax.Array` representation of a DLPack tensor.

  The returned :class:`~jax.Array` shares memory with ``external_array`` if no
  device transfer or copy was requested.

  Args:
    external_array: An array object that has __dlpack__ and __dlpack_device__
      methods, or a DLPack tensor on either CPU or GPU (legacy API).

    device: The (optional) :py:class:`Device`, representing the device on which
      the returned array should be placed. If given, then the result is committed
      to the device. If unspecified, the resulting array will be unpacked onto the
      same device it originated from. Setting ``device`` to a device different from
      the source of ``external_array`` will require a copy, meaning ``copy`` must be
      set to either ``True`` or ``None``.

    copy: An (optional) boolean, controlling whether or not a copy is performed.
      If ``copy=True`` then a copy is always performed, even if unpacked onto the
      same device. If ``copy=False`` then the copy is never performed and will raise
      an error if necessary. When ``copy=None`` then a copy may be performed if
      needed for a device transfer.

  Returns:
    A jax.Array

  Note:
    While JAX arrays are always immutable, dlpack buffers cannot be marked as
    immutable, and it is possible for processes external to JAX to mutate them
    in-place. If a jax Array is constructed from a dlpack buffer and the buffer
    is later modified in-place, it may lead to undefined behavior when using
    the associated JAX array.
     zXfrom_dlpack can only unpack a dlpack tensor onto a singular device, but a Sharding with z devices was provided.r\   )r1   r   
device_setlenr   hasattrr`   rV   )r]   r   r   rd   s       r$   from_dlpackrg      s    F !! "J
:	Cz??	C 	C 	C   GF^\** 6555 
^VT	:	::r&   )NNN)
r   r   r   r   r   r   r   r   r   r   )NNNNN)r   r   r   r   r   r   r'   r(   r)   r*   r   r   r,   )r   r   r   r   )r   ra   r   r   ).
__future__r   typingr   jax._src.apir   jaxr   rC   jax._srcr   r   jax._src.lax.laxr	   jax._src.libr
   jax._src.typingr   r   jax._src.shardingr   r;   r<   	frozensetint8int16int32int64uint8uint16uint32uint64float16bfloat16float32float64	complex64
complex128bool_SUPPORTED_DTYPESr%   rA   rF   rV   r`   rg    r&   r$   <module>r      s   # " " " " "       # # # # # #                   ( ( ( ( ( ( # # # # # # / / / / / / / / & & & & & &  9HciCIsy#*J
CKs{K	; < <  7;26#'    , 4859;?48#'	S S S S Sj   DH,0!9 !9 !9 !9 !9F EI%)9 9 9 9 9@ ?C$(/; /; /; /; /; /; /;r&   