Ë
    ÏGìjE  ã                  ó~  — U d Z ddlmZ ddlZddlmZ ddlZddlZddlZddl	Z	ddl
Z
ddlmZmZmZ ddlmZ ej$                  ZdZej(                  Zdd	d
œZ ej.                  e«      Zeeeeeee   eef   f   Z	 	 	 	 	 	 	 	 	 d.	 d/d„Z ejB                  Z!ejD                  Z"	 d0	 	 	 d1d„Z#	 d0	 	 	 	 	 d2d„Z$d3d„Z%d4d„Z&d5d„Z'd3d„Z(d6d„Z)	 	 	 d7	 	 	 	 	 	 	 d8d„Z*d9d„Z+ejX                  Z,ejZ                  Z-de-_         ej\                  Z.de._         ej^                  Z/de/_         ej`                  Z0ejb                  Z1ejd                  Z2ejf                  Z3ejh                  Z4ejj                  Z5ejl                  Z6ejn                  Z7ejp                  Z8ejr                  Z9ejt                  Z:ejv                  Z;ejx                  Z<ejz                  Z=ej|                  Z>ej~                  Z?ej€                  Z@ej‚                  ZAej„                  ZBej†                  ZCd:d„ZDd:d„ZEeDe8_F        eEe8_G         G d„ dej�                  «      ZI G d„ de«      ZJi ZKdeLd<   i ZMd eLd!<    e
jœ                  «       ZOd"deIj                   f	 	 	 	 	 	 	 	 	 	 	 d;d#„ZQ	 	 	 	 	 	 d<d$„ZR G d%„ d&e«      ZSi ZTd'eLd(<   i ZUd)eLd*<    e
jœ                  «       ZV	 d=	 	 	 	 	 	 	 d>d+„ZW	 	 	 	 	 	 d?d,„ZXej²                  ZYej´                  ZZej¶                  Z[ej¸                  Z\ejº                  Z]ej¼                  Z^ej¾                  d@d-„«       Z`ejÂ                  Za ejÄ                  ejÆ                  «       ejÈ                  ZdejÊ                  ZeejÌ                  ZfejÎ                  ZgejÐ                  ZhejÒ                  ZiejÔ                  ZjejÖ                  ZkejØ                  Zly)AzAn XLA client in Python.é    )ÚannotationsN)ÚMapping)ÚAnyÚProtocolÚUnion)Ú_jaxip  ÚHostÚCUDA)ÚcpuÚgpuc	                ó¦   — t        dt        j                  «       t        dt        j                  «       t        j
                  | ||||||||¬«	      S )Nr   ©	ÚasynchronousÚdistributed_clientÚnode_idÚ	num_nodesÚcollectivesÚnum_devicesÚ"get_local_topology_timeout_minutesÚ#get_global_topology_timeout_minutesÚtransfer_server_factory)Úregister_custom_call_handlerÚ_xlaÚregister_custom_call_targetÚregister_custom_type_id_handlerÚregister_custom_type_idÚget_tfrt_cpu_clientr   s	            úUC:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\jaxlib/xla_client.pyÚmake_cpu_clientr   @   sR   € ô ˜u¤d×&FÑ&FÔGÜ! %¬×)EÑ)EÔFÜ	×	!Ñ	!ØØ+ØØØØØ)KØ*MØ5ô

ð 
ó    c           	     óB   — t        j                  d| t        di |¤Ž«      S )ú)Creates a PJRT C API TopologyDescription.Útpu© )r   Úget_default_c_api_topologyÚdict)Útopology_nameÚkwargss     r   Ú#make_tfrt_tpu_c_api_device_topologyr)   ^   s   € ô 
×	(Ñ	(¨°¼t¹~Àf¹~Ó	NÐNr    c           	     óB   — t        j                  | |t        di |¤Ž«      S )r"   r$   )r   Úget_c_api_topologyr&   )Úc_apir'   r(   s      r   Úmake_c_api_device_topologyr-   e   s   € ô 
×	 Ñ	  ¨´t±~¸f±~Ó	FÐFr    c                ó,   — t        j                  | «      S ©N)r   Úpjrt_plugin_loaded©Úplugin_names    r   r0   r0   l   s   € Ü	×	 Ñ	  Ó	-Ð-r    c                ó2   — t        j                  | |d ¬«      S )N)r,   ©r   Úload_pjrt_plugin)r2   Úlibrary_paths     r   Úload_pjrt_plugin_dynamicallyr7   p   s   € Ü	×	Ñ	˜{¨LÀÔ	EÐEr    c                ó0   — t        j                  | d |«      S r/   r4   )r2   r,   s     r   Úload_pjrt_plugin_with_c_apir9   t   s   € Ü	×	Ñ	˜{¨D°%Ó	8Ð8r    c                ó,   — t        j                  | «      S r/   )r   Úpjrt_plugin_initializedr1   s    r   r;   r;   x   s   € Ü	×	%Ñ	% kÓ	2Ð2r    c                ó.   — t        j                  | «       y)zÓInitializes a PJRT plugin.

  The plugin needs to be loaded first (through load_pjrt_plugin_dynamically or
  static linking) before this method is called.
  Args:
    plugin_name: the name of the PJRT plugin.
  N)r   Úinitialize_pjrt_pluginr1   s    r   r=   r=   |   s   € ô ×Ñ˜kÕ*r    c                ó:   — |€i }t        j                  | |||«      S )ax  Creates a PJRT C API client for a PJRT plugin.

  It is required that load_pjrt_plugin_dynamically is called once with the same
  plugin_name before this method is called.

  Args:
     plugin_name: the name of the PJRT plugin.
     options: extra platform-specific options.
     distributed_client: distributed client.

  Returns:
     A PJRT C API client for plugin_name.
  )r   Úget_c_api_client)r2   Úoptionsr   r   s       r   Úmake_c_api_clientrA   ‡   s-   € ð& €_Ø€GÜ	×	Ñ	ØØØØó	
ð r    c                 óh  — i } d| d<   t        j                  dd«      j                  «       }t        j                  dd«      }t        j                  dd«      }|r|rt        d«      ‚|}t        j                  d	d«      }t        j                  d
d«      }|dvrt        d|z  «      ‚|| d<   |rt	        |«      | d<   |r|dv| d<   |rt        |«      dz  | d<   t        j                  dd«      }t        t        |«      «      | d<   t        j                  dd«      }t        t        |«      «      | d<   | S )zYGenerates the PjRt GPU plugin options.

  Returns:
    A dictionary of plugin options.
  ÚcudaÚplatform_nameÚXLA_PYTHON_CLIENT_ALLOCATORÚdefaultÚXLA_CLIENT_MEM_FRACTIONÚ ÚXLA_PYTHON_CLIENT_MEM_FRACTIONz{XLA_CLIENT_MEM_FRACTION is specified together with XLA_PYTHON_CLIENT_MEM_FRACTION. Remove the latter one, it is deprecated.ÚXLA_PYTHON_CLIENT_PREALLOCATEÚ(XLA_PYTHON_CLIENT_COLLECTIVE_MEM_SIZE_MB)rF   ÚplatformÚbfcÚ
cuda_asynczcXLA_PYTHON_CLIENT_ALLOCATOR env var must be "default", "platform", "bfc", or "cuda_async", got "%s"Ú	allocatorÚmemory_fraction)ÚfalseÚFalseÚ0Úpreallocatei   Úcollective_memory_sizeÚ.XLA_PYTHON_CLIENT_ABORT_COLLECTIVES_ON_FAILURErS   Úabort_collectives_on_failureÚ%XLA_PYTHON_CLIENT_USE_TFRT_GPU_CLIENTÚuse_tfrt_gpu_client)ÚosÚgetenvÚlowerÚ
ValueErrorÚfloatÚintÚbool)r@   rO   rP   Údeprecated_memory_fractionrT   rU   ÚabortÚuse_trft_gpu_clients           r   Ú generate_pjrt_gpu_plugin_optionsrd   ¤   sV  € ð €'Ø#€'ˆ/ÑÜ�i‰iÐ5°yÓA×GÑGÓI€)Ü—I‘IÐ7¸Ó<€/Ü!Ÿy™yÐ)IÈ2ÓNÐÙÙÜð5óð ð 3€oÜ—	‘	Ð9¸2Ó>€+ÜŸ9™9Ø0°"óÐð ÐBÑBÜ
ð	+Ø-6ñ	7óð ð #€'ˆ+ÑÙÜ!& Ó!7€GÐÑÙØ(Ð0GÐG€GˆMÑÙÜ(+Ð,BÓ(CÀwÑ(O€GÐ$Ñ%Ü
�)‰)ÐDÀcÓ
J€%Ü,0´°U³Ó,<€'Ð
(Ñ)ÜŸ	™	Ð"IÈ3ÓOÐÜ#'¬Ð,?Ó(@Ó#A€'Ð
Ñ Ø	€.r    aá  
A Shape is an object defined in C++ that duck types like the following class:

class Shape:
  '''Represents an XLA shape.

  A shape is either an array shape, having rank-many integer
  dimensions and an element type (represented by a Numpy dtype), or it
  is a tuple shape, having a shape for every tuple component:

    type shape =
        TupleShape of shape list
      | ArrayShape of { dimensions: int list; element_type: dtype }
  '''

  @staticmethod
  def tuple_shape(tuple_shapes) -> Shape:
    "Construct a tuple shape."

  @staticmethod
  def array_shape(element_type, dimensions, minor_to_major=None) -> Shape:

  @staticmethod
  def from_pyval(pyval) -> Shape:
    "Returns a Shape that describes a tuple-tree of Numpy arrays."

  def __init__(self, str) -> Shape:
    "Parses a shape string."
  def __eq__(self, other: Shape) -> bool:
  def __ne__(self, other: Shape) -> bool:
  def __hash__(self):
  def __repr__(self):
  def is_tuple(self) -> bool:
  def is_array(self) -> bool:
  def tuple_shapes(self) -> [Shape]:
  def numpy_dtype(self) -> np.dtype:
    "Like element_type(), but returns dtype('O') for a tuple shape."
  def xla_element_type(self) -> PrimitiveType:
  def element_type(self) -> np.dtype:
  def dimensions(self) -> (int, int, ...):
  def rank(self) -> int:
  def with_major_to_minor_layout_if_absent(self) -> Shape:
    "Returns a copy with missing layouts set to major-to-minor."

  def to_serialized_proto(self) -> bytes:
    "Returns 'shape' as a serialized proto."
z÷
A ProgramShape is a C++ object that duck types like the following class.

class ProgramShape:
  def __init__(self, parameter_shapes, result_shape):
  def parameter_shapes(self) -> [Shape]:
  def result_shape(self) -> Shape:
  def __repr__(self):
a´  
A DeviceAssignment is a C++ object with the following signature.

def create(assignment):
  '''Builds a device assignment.

   Args:
     assignment: a 2D numpy array of device ordinal integers, indexed by
       [replica][computation_in_replica].
   Returns:
     A device assignment.
  '''

def replica_count():
  '''Returns the number of replicas.'''
def computation_count():
  '''Returns the number of computations per replica.'''
c                ór   — ~| j                  |«      }|j                  «       D �cg c]  }|d   ‘Œ	 c}S c c}w )Nr   )Úexecute_shardedÚ%disassemble_into_single_device_arrays©ÚselfÚ	argumentsÚdeviceÚresultsÚxs        r   ÚLoadedExecutable_executern   T  s<   € ØØ× Ñ  Ó+€'Ø×EÑEÔGÓ	HÑG�1ˆ!ˆA‹$ÐGÑ	HÐHùÒ	Hs   ¥4c                ó´   — ~| j                  |d¬«      }|j                  «       D �cg c]  }|d   ‘Œ	 c}|j                  «       j                  d«      fS c c}w )NT)Úwith_tokensr   )rf   rg   Úconsume_tokenÚ	get_tokenrh   s        r   Ú#LoadedExecutable_execute_with_tokenrs   Z  s_   € ØØ× Ñ  ¸Ð Ó=€'à×BÑBÔDÓEÑD�€qˆƒtÐDÑEØ×ÑÓ×'Ñ'¨Ó*ð
ð ùÚEs   §Ac                  ó   — e Zd ZdZdZy)ÚCustomCallTargetTraitsr   é   N)Ú__name__Ú
__module__Ú__qualname__ÚDEFAULTÚCOMMAND_BUFFER_COMPATIBLEr$   r    r   ru   ru   g  s   „ Ø€'ð  Ñr    ru   c                  ó0   — e Zd Z	 	 d	 	 	 	 	 	 	 	 	 	 	 dd„Zy)ÚCustomCallHandlerc                ó   — y r/   r$   )ri   ÚnameÚfnrL   Úapi_versionÚtraitss         r   Ú__call__zCustomCallHandler.__call__s  s   € ð r    N)..)r�   r_   r‚   ru   r   Ústrr€   r   rL   r„   ÚreturnÚNone©rw   rx   ry   rƒ   r$   r    r   r}   r}   q  sI   „ ð Ø'*ð	ð ð	ð %ð	àð	ð ð	ð ð		ð ô	r    r}   zdict[str, CustomCallHandler]Ú_custom_callback_handlerz=dict[str, list[tuple[str, Any, int, CustomCallTargetTraits]]]Ú_custom_callbackr   c                óî   — t         j                  ||«      }t        5  |t        v rt        |   | ||||«       n)t        j                  |g «      j                  | |||f«       ddd«       y# 1 sw Y   yxY w)a�  Registers a custom call target.

  Args:
    name: bytes containing the name of the function.
    fn: a PyCapsule object containing the function pointer.
    platform: the target platform.
    api_version: the XLA FFI version to use. Supported versions are: 0 for the
      untyped FFI and 1 for the typed FFI.
    traits: custom call traits corresponding to XLA FFI handler traits.
  N)Úxla_platform_namesÚgetÚ_custom_callback_lockrˆ   r‰   Ú
setdefaultÚappend)r   r€   rL   r�   r‚   Úxla_platform_names         r   r   r   ‡  ss   € ô& )×,Ñ,¨X°xÓ@ÐÞØÔ4Ñ4ÜÐ0Ñ1Ø
�Ð% {°Fõô ×!Ñ!Ð"3°RÓ8×?Ñ?Ø��[ &Ð
)ô÷ ×Ñús   �AA+Á+A4c           	     ó$  — t         j                  | | «      }t        5  |t        v r t        j                  d|«       	 ddd«       y|t        |<   |t        v r&t        |   D ]  \  }}}} ||||||«       Œ t        |= ddd«       y# 1 sw Y   yxY w)a1  Registers a custom handler and use it to register existing custom calls.

  If a custom call handler for the platform already exist, calling this method
  is a no-op and it will not register a new handler.

  Args:
    platform: the target platform.
    handler: the function to register a custom call.
  zKCustom call handler for %s is already register. Will not register a new oneN)r‹   rŒ   r�   rˆ   ÚloggerÚdebugr‰   )rL   Úhandlerr�   r   r€   r�   r‚   s          r   r   r   ¦  s›   € ô )×,Ñ,¨X°xÓ@ÐÞØÔ4Ñ4Ü‡l�lðà
ôð
 ÷ Ðð 3:ÔÐ.Ñ/ØÔ,Ñ,Ü+;Ð<MÔ+NÑ
'ˆ$��K Ù��bÐ+¨[¸&ÕAð ,Oä
Ð,Ð
-÷ ×Ñús   � BÁ7BÂBc                  ó   — e Zd Zdd„Zy)ÚCustomTypeIdHandlerc                 ó   — y r/   r$   )ri   r   Úcapsules      r   rƒ   zCustomTypeIdHandler.__call__Ä  s   € Ør    N)r   r„   r˜   r   r…   r†   r‡   r$   r    r   r–   r–   Â  s   „ ôr    r–   zdict[str, CustomTypeIdHandler]Ú_custom_type_id_handlerzdict[str, Any]Ú_custom_type_idc                óä   — t         j                  ||«      }t        5  |t        v rt        |   | |«       n't        j                  |g «      j                  | |f«       ddd«       y# 1 sw Y   yxY w)zÖRegister a custom type id for use with the FFI.

  Args:
    type_name: a unique name for the type.
    type_id: a PyCapsule object containing a pointer to the ``ffi::TypeId``.
    platform: the target platform.
  N)r‹   rŒ   Ú_custom_type_id_lockr™   rš   rŽ   r�   )Ú	type_nameÚtype_idrL   r�   s       r   r   r   Í  sc   € ô )×,Ñ,¨X°xÓ@ÐÞØÔ3Ñ3ÜÐ/Ñ0°¸GÕDä× Ñ Ð!2°BÓ7×>Ñ>Ø�gÐ
ô÷	 ×Ñús   �A A&Á&A/c                ó  — t         j                  | | «      }t        5  |t        v r t        j                  d|«       	 ddd«       y|t        |<   |t        v r!t        |   D ]  \  }} |||«       Œ t        |= ddd«       y# 1 sw Y   yxY w)a:  Register a custom type id handler and use it to register existing type ids.

  If a custom type id handler for the platform already exist, calling this
  method is a no-op and it will not register a new handler.

  Args:
    platform: the target platform.
    handler: the function to register a custom type id.
  zNCustom type id handler for %s is already register. Will not register a new oneN)r‹   rŒ   r�   r™   r’   r“   rš   )rL   r”   r�   r   r˜   s        r   r   r   ã  s�   € ô )×,Ñ,¨X°xÓ@ÐÞØÔ3Ñ3Ü‡l�lðà
ôð
 ÷ Ðð 29ÔÐ-Ñ.ØœOÑ+Ü*Ð+<Ô=‰-ˆ$�Ù��gÕð >ä
Ð+Ð
,÷ ×Ñús   � BÁ2BÂB
c              #  óÆ   K  — t        j                  «       }t        j                  | «       	 d–— t        j                  |«       y# t        j                  |«       w xY w­w)zVContext manager that overwrites and restores the current thread's execution_stream_id.N)r   Úget_execution_stream_idÚset_execution_stream_id)Únew_idÚsaveds     r   Úexecution_stream_idr¥     sH   è ø€ ô ×
&Ñ
&Ó
(€%Ü×Ñ˜vÔ&ð(Û	ä× Ñ  Õ'ø„D× Ñ  Õ'üs   ‚*A!­A ±A!ÁAÁA!)	TNr   rv   NNNNN)r…   ÚClient)rH   )r'   r„   r…   ÚDeviceTopology)r,   r   r'   r„   r…   r§   )r2   r„   r…   r`   )r2   r„   r6   r„   r…   r   )r2   r„   r,   r   r…   r†   )r2   r„   r…   r†   )NNN)r2   r„   r@   z_NameValueMapping | Noner   z$_xla.DistributedRuntimeClient | Noner   z*_xla.TransferServerInterfaceFactory | None)r…   Ú_NameValueMappingr/   )r   r„   r€   r   rL   r„   r�   r_   r‚   ru   r…   r†   )rL   r„   r”   r}   r…   r†   )r   )r�   r„   rž   r   rL   r„   r…   r†   )rL   r„   r”   r–   r…   r†   )r£   r_   )mÚ__doc__Ú
__future__r   ÚatexitÚcollections.abcr   Ú
contextlibÚenumÚloggingrZ   Ú	threadingÚtypingr   r   r   Újaxlibr   r   Úifrt_programsÚ_versionÚifrt_version_numberÚ_ifrt_versionr‹   Ú	getLoggerrw   r’   r„   r_   Úlistr^   r`   r¨   r   r§   Úget_topology_for_devicesr)   r-   r0   r7   r9   r;   r=   rA   rd   ÚPrimitiveTypeÚShapeÚProgramShapeÚDeviceAssignmentÚDeviceÚCompileOptionsÚHostBufferSemanticsÚXlaComputationr¦   ÚMemoryÚArrayÚ	ArrayImplÚLoadedExecutableÚ
ExecutableÚ
DeviceListÚ
OpShardingÚHloShardingÚShardingÚNamedShardingÚSingleDeviceShardingÚPmapShardingÚGSPMDShardingÚ
PjRtLayoutÚAutotuneCacheModern   rs   ÚexecuteÚexecute_with_tokenÚIntFlagru   r}   rˆ   Ú__annotations__r‰   ÚLockr�   rz   r   r   r–   r™   rš   rœ   r   r   Ú register_custom_call_partitionerÚ encode_inspect_sharding_callbackÚhlo_sharding_utilÚ+register_custom_call_as_batch_partitionableÚ	TracebackÚFrameÚcontextmanagerr¥   ÚXlaRuntimeErrorÚregisterÚcollect_garbageÚarray_result_handlerÚ+batched_copy_array_to_devices_with_shardingÚbatched_device_putÚreorder_shardsÚbatched_block_until_readyÚ"check_and_canonicalize_memory_kindÚLayoutÚcustom_call_targetsÚArrayCopySemanticsr$   r    r   Ú<module>ré      sÙ  ðò å "ã Ý #Û Û Û Û 	Û ß 'Ñ 'å ð ×"Ñ"€ð €ð ×(Ñ(€ð ØñÐ ð
 
ˆ×	Ñ	˜8Ó	$€à˜C  s¨C°°c±¸EÀ4Ð'GÑ!HÐHÑIÐ ð ØØØØØØ'+Ø(,Ø ðð óð4 ×$Ñ$€Ø×8Ñ8Ð ð ðOØðOàóOð &(ðGØðGØ"ðGàóGó.óFó9ó3ó+ð )-Ø?CØJNð	Øðà%ðð =ðð Hó	ó:)ðX ×"Ñ"€à�
‰
€ð.€„ð` × Ñ €ð€Ô ð ×(Ñ(Ð ðÐ Ô ð& 
�‰€Ø×$Ñ$€à×.Ñ.Ð ð8 ×$Ñ$€Ø	�‰€Ø	�‰€Ø�
‰
€Ø�N‰N€	Ø×(Ñ(Ð Ø�_‰_€
Ø�_‰_€
Ø�_‰_€
Ø×Ñ€Ø�=‰=€Ø×"Ñ"€Ø×0Ñ0Ð Ø× Ñ €Ø×"Ñ"€Ø�_‰_€
Ø×*Ñ*Ð óIóð 4Ð Ô Ø&IÐ Ô #ô ˜TŸ\™\ô  ô˜ô ð :<Ð Ð6Ó ;ð ð ð ó ð '˜	Ÿ™Ó(Ð ð ØØ%;×%CÑ%CðØ
ðàðð ðð ð	ð
 #ðð 
óð>.Øð.Ø-ð.à	ó.ô8˜(ô ð ;=Ð Ð7Ó <Ø"$€�Ó $Ø%�y—~‘~Ó'Ð ð ðØðàðð ðð 
ó	ð,-Øð-Ø/ð-à	ó-ð8 $(×#HÑ#HÐ  Ø#'×#HÑ#HÐ  Ø×*Ñ*Ð à×4Ñ4ð ,ð
 �N‰N€	Ø�
‰
€ð ×Ñò(ó ð(ð ×&Ñ&€ð €‡��×$Ñ$Ô %à×0Ñ0Ð à×4Ñ4ð ,ð ×,Ñ,Ð Ø×$Ñ$€Ø ×:Ñ:Ð Ø%)×%LÑ%LÐ "Ø	�‰€Ø×.Ñ.Ð Ø×,Ñ,Ñ r    