
    Spf{                    n   U d Z ddlmZ ddlZddlmZ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ZddlZddlmZmZmZ ddlZddlZddlmZ ej        Zej        Zej        ZdZd	Zd
ddZ ej        e          Z ee!ee!e"e#e"         e$e%f         f         Z&	 	 	 	 	 dddZ'	 	 	 	 	 	 ddZ(dddZ)ej*        Z*ej+        Z+	 dddZ,	 dddZ-dd Z.dd"Z/dd$Z0dd%Z1dd&Z2	 	 ddd)Z3	 ddd+Z4dd-Z5 G d. d/          Z6dd0Z7ej8        Z8ej9        Z9ej:        Z:ej;        Z;ej<        Z<ej=        Z=ej>        Z>i e8j?         ej@        d          e8jA         ej@        d1          e8jB         ej@        d2          e8jC         ej@        d3          e8jD         ej@        d4          e8jE         ej@        d5          e8jF         ej@        d6          e8jG         ej@        d7          e8jH         ej@        d8          e8jI         ej@        d9          e8jJ         ej@        d:          e8jK         ej@        e:          e8jL         ej@        e;          e8jM         ej@        e=          e8jN         ej@        e<          e8jO         ej@        e>          e8jP         ej@        e9          e8jQ         ej@        d;          e8jR         ej@        d<          e8jS         ej@        d=          e8jT         ej@        d>          e8jU         ej@        d?          e8jV         ej@        ejW                  e8jX         ej@        ejW                  iZYd@ eYZ                                D             Z[dA Z\ej]        Z]dBe]_         ej^        Z^dCe^_         ej_        Z_dDe__         dddGZ`eja        ZadHea_         ejb        Zbejc        Zcejd        ZddI ZedJ Zf G dK dLejg                  ZhdM Ziejj        Zjejk        Zkejl        Zlejm        Zmejn        Znejo        Zoejp        Zpejq        Zqejr        Zrejs        Zsejt        Zteju        Zuejv        Zvejw        Zwejx        Zxejy        Zyejz        ZzddNZ{ddOZ|e{eq_}        e|eq_~         G dP dQej                  Z G dR dSe          Zi ZdTedU<   i ZdVedW<    ej                    ZdXdej        fdd_ZddaZej        Zej        Zej        Z G db dc          Z G dd de          ZddhZ G di dj          ZddmZ G dn do          ZddrZ G ds dt          Z G du dv          Z G dw dx          Z G dy dz          Zd{ Zd| Zej        Zej        Zej        dd}            ZddZej        Z ej        ej                   ej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        ZdS )zAn XLA client in Python.    )annotationsN)MappingSequence)AnyProtocolUnion   )xla_extensioni  9   HostCUDA)cpugpuTreturn...c                h    t          dt          j                   t          j        | ||||          S )Nr   asynchronousdistributed_clientnode_id	num_nodescollectives)register_custom_call_handler_xlaregister_custom_call_targetget_tfrt_cpu_clientr   s        Q/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jaxlib/xla_client.pymake_cpu_clientr   D   s@     ud&FGGG		!+
 
 
     Fc           
     b   t                      }|d         }t          j                    }|dk    rt          j        j        j        |_        |dk    rt          j        j        j        |_        |dk    rt          j        j        j        |_        |dk    rt          j        j        j        |_        d|v r|d         |_	        d|v r|d         |_
        d|v r|d         |_        t          d	t          j                   t          d
t          j                   t          j        d|| |||||          S )z7Returns a GPU client. BFC allocator is used by default.	allocatordefaultplatformbfc
cuda_asyncmemory_fractionpreallocatecollective_memory_sizer   ROCMT)r   allocator_configr   r   r   platform_nameallowed_devicesmock) generate_pjrt_gpu_plugin_optionsr   GpuAllocatorConfigKindDEFAULTkindPLATFORMBFC
CUDA_ASYNCr&   r'   r(   r   r   get_gpu_client)	r   r   r   r+   r,   r-   optionsr!   configs	            r   make_gpu_clientr9   U   s5    -..'k")"$$&)).6FK*).7FK%).2FK,).9FK'!!$%67Fg /F(($+,D$EF!vt'GHHHvt'GHHH		+!%	
 	
 	
 	r   r7   _NameValueMapping | Nonec                    t          d          sJ t          d          st          d           | i } t          j        d|           S )Ntpu)pjrt_plugin_loadedpjrt_plugin_initializedinitialize_pjrt_pluginr   get_c_api_client)r7   s    r   make_tfrt_tpu_c_api_clientrA   ~   sQ    	E	"	""""	 	'	' "5!!!_G		ug	.	..r    topology_namestrDeviceTopologyc           	     B    t          j        d| t          di |          S ))Creates a PJRT C API TopologyDescription.r<    )r   get_default_c_api_topologydict)rC   kwargss     r   #make_tfrt_tpu_c_api_device_topologyrL      s#     
	(t~~f~~	N	NNr   c_apir   c           	     B    t          j        | |t          di |          S )rG   rH   )r   get_c_api_topologyrJ   )rM   rC   rK   s      r   make_c_api_device_topologyrP      s#     
	 t~~f~~	F	FFr   plugin_nameboolc                *    t          j        |           S N)r   r=   rQ   s    r   r=   r=      s    		 	-	--r   library_pathc                0    t          j        | |d           S )N)rM   r   load_pjrt_plugin)rQ   rV   s     r   load_pjrt_plugin_dynamicallyrZ      s    		{L	E	E	EEr   Nonec                .    t          j        | d |          S rT   rX   )rQ   rM   s     r   load_pjrt_plugin_with_c_apir]      s    		{D%	8	88r   c                *    t          j        |           S rT   )r   r>   rU   s    r   r>   r>      s    		%k	2	22r   c                .    t          j        |            dS )zInitializes 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   r?   rU   s    r   r?   r?      s     k*****r   r   $_xla.DistributedRuntimeClient | Nonec                6    |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   r@   )rQ   r7   r   s      r   make_c_api_clientrb      s$    $ _G		{G5G	H	HHr   
str | Nonec                    t          d          s,t          d| pd          }t                              |           t	          |          S )zEReturns a TPU client. Defaults to allowing 32 in-flight computations.r<   z	libtpu.so)r=   rZ   profilerregister_plugin_profilerrA   )rV   r7   rM   s      r   make_tpu_clientrg      sJ     
E	"	" -(0KLLE%%e,,,	#G	,	,,r   _NameValueMappingc                 ~   i } d| d<   t          j        dd                                          }t          j        d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<   | S )zYGenerates the PjRt GPU plugin options.

  Returns:
    A dictionary of plugin options.
  cudar+   XLA_PYTHON_CLIENT_ALLOCATORr"   XLA_PYTHON_CLIENT_MEM_FRACTIONrB   XLA_PYTHON_CLIENT_PREALLOCATE(XLA_PYTHON_CLIENT_COLLECTIVE_MEM_SIZE_MB)r"   r#   r$   r%   zcXLA_PYTHON_CLIENT_ALLOCATOR env var must be "default", "platform", "bfc", or "cuda_async", got "%s"r!   r&   )falseFalse0r'   i   r(   )osgetenvlower
ValueErrorfloatint)r7   r!   r&   r'   r(   s        r   r.   r.      s     '#'/i5yAAGGII)I>CC/	92>>+90"  BBB
	+-6	7   #'+ 8!&!7!7G H(0GGGM P(+,B(C(Cw(OG$%	.r   c                      e Zd ZdZdZddZdS )
OpMetadataz3Python representation of a xla.OpMetadata protobuf.op_typeop_namesource_filesource_linerB   r   c                >    || _         || _        || _        || _        d S rT   rz   )selfr{   r|   r}   r~   s        r   __init__zOpMetadata.__init__   s&    DLDL"D"Dr   N)rB   rB   rB   r   __name__
__module____qualname____doc__	__slots__r   rH   r   r   ry   ry      s3        ;;B)# # # # # #r   ry   c                    t          j                    |         dd         \  }}t          j                            |          }t          | |||          S )zCHelper for use in source mapping that returns an OpMetadata object.r	      rz   )inspectstackrr   pathbasenamery   )r{   r|   skip_framesfull_filenamelinenofilenames         r   CurrentSourceInfoMetadatar      sU    !-//+6qs;-Wm,,(	wH&
 
 
 r   int4int8int16int32int64uint4uint8uint16uint32uint64float16float32float64	complex64
complex128c                4    i | ]\  }}t          |          |S rH   )rD   ).0etdts      r   
<dictcomp>r   /  s1       BCGGR  r   c                Z    t           t          t          j        |                              S )z;Convenience function for reading DTYPE_TO_XLA_ELEMENT_TYPE.)DTYPE_TO_XLA_ELEMENT_TYPErD   npdtype)r   s    r   dtype_to_etyper   4  s    	"3rx#7#7	88r   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 Shape is an object defined in C++ that duck types like the following class:

class ShapeIndex:
  '''Represents an XLA ShapeIndex.

  An index for specifying a particular nested subshape within a shape. Used in
  ShapeUtil::GetSubshape and other interfaces. ShapeIndex defines a path through
  the Shape tree where each element of ShapeIndex indexes into a tuple (or
  nested tuple) within the shape. For a non-nested tuple, an index has a single
  element.
  '''

  def __init__(self, List[int]) -> ShapeIndex:
  def __eq__(self, other: Shape) -> bool:
  def __ne__(self, other: Shape) -> bool:
  def __hash__(self):
  def __repr__(self):
layoutSequence[int] | Nonec                (    fd |           S )z<Returns a Shape that describes a tuple-tree of Numpy arrays.c                   t          | t                    rDt          d          t                              t          fd| D                                 S t                              | j        t          j        |                     S )Nz:shape_from_pyval does not support layouts for tuple shapesc              3  .   K   | ]} |          V  d S rT   rH   )r   eltconverts     r   	<genexpr>z4shape_from_pyval.<locals>.convert.<locals>.<genexpr>  s+      $C$CcWWS\\$C$C$C$C$C$Cr   )	
isinstancetupleNotImplementedErrorShapetuple_shapearray_shaper   r   shape)pyvalr   r   s    r   r   z!shape_from_pyval.<locals>.convert  s    % E		!H
 
 	
 u$C$C$C$CU$C$C$CCCDDDu{BHUOOVDDDr   rH   )r   r   r   s    `@r   shape_from_pyvalr     s8    E E E E E E 
r   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      fdfd|D             }                      |          }d |D             S )zBExecute on one replica with Python values as arguments and output.c                b                         |                                 d                   S )Nr   )device)buffer_from_pyvallocal_devices)argbackend
executables    r   putz'execute_with_python_values.<locals>.put  s-    $$S1I1I1K1KA1N$OOOr   c                &    g | ]} |          S rH   rH   )r   r   r   s     r   
<listcomp>z.execute_with_python_values.<locals>.<listcomp>  s!    ---Css3xx---r   c                6    g | ]}t          j        |          S rH   r   asarrayr   xs     r   r   z.execute_with_python_values.<locals>.<listcomp>  s     	)	)	)A"*Q--	)	)	)r   )execute)r   	argumentsr   outputsr   s   ` ` @r   execute_with_python_valuesr     sk    P P P P P P .---9---)y))'	)	)	)	)	))r   c                    |                                  fdfdt          | D             }|                     |          }d t          | D             S )aB  Execute on many replicas with Python values as arguments and output.

  Args:
    executable: the program to run.
    arguments: a list of lists of Python values indexed by `[replica][arg_num]`
      to pass as inputs.
    backend: the backend we are targeting.

  Returns:
    A list of python values, one per replica.
  c                <    fdt          |           D             S )Nc                B    g | ]\  }}                     ||          S rH   )r   )r   vdr   s      r   r   zRexecute_with_python_values_replicated.<locals>.copy_to_devices.<locals>.<listcomp>  s-    MMM1G%%a++MMMr   )zip)pyvalsr   devicess    r   copy_to_devicesz>execute_with_python_values_replicated.<locals>.copy_to_devices  s'    MMMMFG8L8LMMMMr   c                &    g | ]} |          S rH   rH   )r   r   r   s     r   r   z9execute_with_python_values_replicated.<locals>.<listcomp>  s#    BBBOOF##BBBr   c                &    g | ]}d  |D             S )c                6    g | ]}t          j        |          S rH   r   r   s     r   r   zDexecute_with_python_values_replicated.<locals>.<listcomp>.<listcomp>  s     
%
%
%Q2:a==
%
%
%r   rH   )r   xss     r   r   z9execute_with_python_values_replicated.<locals>.<listcomp>  s'    	>	>	>"
%
%"
%
%
%	>	>	>r   )r   r    execute_sharded_on_local_devices)r   r   r   inputsr   r   r   s     `  @@r   %execute_with_python_values_replicatedr     s     $$&&'N N N N N N CBBB#y/BBB&77??'	>	>W	>	>	>>r   c                      e Zd ZdZdZdS )PaddingTyper	      N)r   r   r   VALIDSAMErH   r   r   r   r     s        
%	
$$$r   r   c                   t          | t          t          f          s1d}t          |                    t          |                               t          | t                    rn|                                 dk    rt          j        } nI|                                 dk    rt          j        } n$d}t          |                    |                     | t          j        k    rdgt          |          z  S | t          j        k    rgt          j        t          j        ||                                        t                    }d t!          ||||          D             }d |D             S d}t          |                    |                     )	zAMaps PaddingType or string to pad values (list of pairs of ints).z0padding_type must be str or PaddingType, got {}.r   r   z@Unknown padding type string: expected "VALID" or "SAME", got {}.)r   r   c                P    g | ]#\  }}}}t          |d z
  |z  |z   |z
  d          $S )r	   r   )max)r   out_sizestridefilter_sizein_sizes        r   r   z5window_padding_type_to_pad_values.<locals>.<listcomp>
  sM       2Hfk7 	X\V#k1G;Q??  r   c                (    g | ]}|d z  ||d z  z
  fS )r   rH   )r   pad_sizes     r   r   z5window_padding_type_to_pad_values.<locals>.<listcomp>  s*    PPP(X]Hx1}45PPPr   z Unexpected PaddingType value: {})r   rD   r   	TypeErrorformattypeupperr   r   ru   lenr   ceiltrue_divideastyperw   r   )padding_typelhs_dimsrhs_dimswindow_stridesmsg	out_shape	pad_sizess          r   !window_padding_type_to_pad_valuesr     sv    
L3"4	5	5 4
<C
CJJtL1122
3
33c"" 1w&& &ll						'	' %llNcszz,//000[&&&8c.)))){'''x@@AAHHMMI 69~x7
 7
  I QPiPPPP
,C
SZZ--
.
..r   c                j    ~|                      |          }d |                                D             S )Nc                    g | ]
}|d          S r   rH   r   s     r   r   z,LoadedExecutable_execute.<locals>.<listcomp>,  s    	H	H	H1!A$	H	H	Hr   )execute_sharded%disassemble_into_single_device_arraysr   r   r   resultss       r   LoadedExecutable_executer  )  s8      ++'	H	HEEGG	H	H	HHr   c                    ~|                      |d          }d |                                D             |                                                    d          fS )NT)with_tokensc                    g | ]
}|d          S r  rH   r   s     r   r   z7LoadedExecutable_execute_with_token.<locals>.<listcomp>3  s    EEEqtEEEr   r   )r  r  consume_token	get_tokenr  s       r   #LoadedExecutable_execute_with_tokenr  /  sa       =='EEWBBDDEEE''**
 r   c                      e Zd ZdZdZdS )CustomCallTargetTraitsr   r	   N)r   r   r   r1   COMMAND_BUFFER_COMPATIBLErH   r   r   r  r  <  s        '  r   r  c                      e Zd Z	 	 dddZdS )CustomCallHandler.api_versionrw   traitsr  namerD   fnr   r#   r   r[   c                   d S rT   rH   )r   r  r  r#   r  r  s         r   __call__zCustomCallHandler.__call__H  s	     Cr   N)..)r  rw   r  r  r  rD   r  r   r#   rD   r   r[   )r   r   r   r  rH   r   r   r  r  F  s5         '*	 	 	 	 	 	 	r   r  zdict[str, CustomCallHandler]_custom_callback_handlerz=dict[str, list[tuple[str, Any, int, CustomCallTargetTraits]]]_custom_callbackr   r  r  r#   r  rw   r  c                &   t                               ||          }t          5  |t          v rt          |         | ||||           n2t                              |g                               | |||f           ddd           dS # 1 swxY w Y   dS )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  r#   r  r  xla_platform_names         r   r   r   \  s    & ),,Xx@@  44401
%{F    !!"3R88??[&
)                   s   ABB
B
handlerc           	     ^   t                               | |           }t          5  |t          v r)t                              d|           	 ddd           dS |t          |<   |t          v r,t          |         D ]\  }}}} ||||||           t          |= ddd           dS # 1 swxY w Y   dS )a0  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  )r#   r!  r   r  r  r  r  s          r   r   r   {  s:    ),,Xx@@ . .444ll
  
 . . . . . . . . 3:./,,,+;<M+N B B
'$Kb+[&AAAA
,
-. . . . . . . . . . . . . . . . . .s   &B"?B""B&)B&c                  <    e Zd ZU dZdZded<   ded<   ded<   d ZdS )	PaddingConfigDimensionz?Python representation of a xla.PaddingConfigDimension protobuf.edge_padding_lowedge_padding_highinterior_paddingrw   r(  r)  r*  c                0    d| _         d| _        d| _        d S Nr   r'  r   s    r   r   zPaddingConfigDimension.__init__  s     DDDr   N)r   r   r   r   r   __annotations__r   rH   r   r   r&  r&    sT         GGK)    r   r&  c                      e Zd ZdZdZd ZdS )PaddingConfigz6Python representation of a xla.PaddingConfig protobuf.
dimensionsc                    g | _         d S rT   r1  r-  s    r   r   zPaddingConfig.__init__  s    DOOOr   Nr   rH   r   r   r0  r0    s.        >>)    r   r0  padding_config4Union[PaddingConfig, Sequence[tuple[int, int, int]]]c                    t          | t                    sV| }t                      } |D ]C\  }}}t                      }||_        ||_        ||_        | j                            |           D| S )a7  Create PaddingConfig proto from list of triples of integers.

  Args:
    padding_config: either a PaddingConfig or a list of integer triples
      (edge_padding_low, edge_padding_high, interior_padding) representing the
      configuration of the padding operation.

  Returns:
    A `PaddingConfig` object.
  )r   r0  r&  r(  r)  r*  r2  r  )r4  tripleslohiinterior	dimensions         r   make_padding_configr<    s}     
NM	2	2 2G"__N# 2 2B(**i#%i $&i!#+i &&y1111	r   c                      e Zd ZdZdZd ZdS )DotDimensionNumbersz<Python representation of a xla.DotDimensionNumbers protobuf.lhs_contracting_dimensionsrhs_contracting_dimensionslhs_batch_dimensionsrhs_batch_dimensionsc                >    g | _         g | _        g | _        g | _        d S rT   r?  r-  s    r   r   zDotDimensionNumbers.__init__  s(    &(D#&(D# "D "Dr   Nr   rH   r   r   r>  r>    s0        DD)# # # # #r   r>  dimension_numbers[Union[DotDimensionNumbers, tuple[tuple[list[int], list[int]], tuple[list[int], list[int]]]]c                D   t          | t          t          f          r| \  \  }}\  }}t                      }|j                            |           |j                            |           |j                            |           |j                            |           |S | S )a  Builds a DotDimensionNumbers object from a specification.

  Args:
    dimension_numbers: either a `DotDimensionNumbers` or a nested tuple
      `((lhs_contract, rhs_contract), (lhs_batch, rhs_batch))` of lists of
      integers representing the dimensions to treat as contracting dimensions
      and batch dimensions on each input operand.

  Returns:
    A `DotDimensionNumbers` object.
  )	r   listr   r>  r@  extendrA  rB  rC  )rE  lhs_contractrhs_contract	lhs_batch	rhs_batchdot_dims_protos         r   make_dot_dimension_numbersrO    s    " !D%=11 	;L8 \<"89i(**N-44\BBB-44\BBB'..y999'..y999r   c                      e Zd ZdZdZd ZdS )ConvolutionDimensionNumberszDPython representation of a xla.ConvolutionDimensionNumbers protobuf.	input_batch_dimensioninput_feature_dimensioninput_spatial_dimensionskernel_input_feature_dimensionkernel_output_feature_dimensionkernel_spatial_dimensionsoutput_batch_dimensionoutput_feature_dimensionoutput_spatial_dimensionsc                    d| _         d| _        g | _        d| _        d| _        g | _        d| _        d| _        g | _        d S r,  rR  r-  s    r   r   z$ConvolutionDimensionNumbers.__init__	  sP    !"D#$D $&D!*+D'+,D(%'D""#D$%D!%'D"""r   Nr   rH   r   r   rQ  rQ    s0        LL
)	( 	( 	( 	( 	(r   rQ  >Union[None, ConvolutionDimensionNumbers, tuple[str, str, str]]num_spatial_dimensionsc                   | |}t                      } d| _        d| _        d| _        d| _        d| _        d| _        | j                            t          dd|z                        | j
                            t          dd|z                        | j                            t          dd|z                        nt          | t                    ri| \  t                      }                     d          | _                            d          | _                            d          | _                            d          | _                            d          | _                            d          | _        | j
                            d	 t                    D                        | j                            t!          d
 t                    D             fd                     | j                            t!          d t                    D             fd                     | S )a  Builds a ConvolutionDimensionNumbers object from a specification.

  Args:
    dimension_numbers: optional, either a ConvolutionDimensionNumbers object or
      a tuple (lhs_spec, rhs_spec, out_spec). Each element is a string of length
      N+2 identifying by position: (1) batch dimensions in lhs, rhs, and the
      output with the character 'N', (2) feature dimensions in lhs and the
      output with the character 'C', (3) input and output feature dimensions in
      rhs with the characters 'I' and 'O' respectively, and (4) spatial
      dimension correspondences between lhs, rhs, and the output using any
      distinct characters. For example, to indicate dimension numbers consistent
      with the Conv operation with two spatial dimensions, one could use
      ('NCHW', 'OIHW', 'NCHW'). As another example, to indicate dimension
      numbers consistent with the TensorFlow Conv2D operation, one could use
      ('NHWC', 'HWIO', 'NHWC'). When using the latter form of convolution
      dimension specification, window strides are associated with spatial
      dimension character labels according to the order in which the labels
      appear in the rhs_spec string, so that window_strides[0] is matched with
      the dimension corresponding to the first character appearing in rhs_spec
      that is not 'I' or 'O'. By default, use the same dimension numbering as
      Conv and ConvWithGeneralPadding.
    num_spatial_dimensions: the number of spatial dimensions.

  Returns:
    A `ConvolutionDimensionNumbers` object.
  Nr   r	   r   NCOIc              3  (   K   | ]\  }}|d v	|V  dS )>   rc  rb  NrH   r   ics      r   r   z5make_convolution_dimension_numbers.<locals>.<genexpr>L  s9       7 7aQj-@-@-@-@-@-@7 7r   c              3  (   K   | ]\  }}|d v	|V  dS >   ra  r`  NrH   re  s      r   r   z5make_convolution_dimension_numbers.<locals>.<genexpr>Q  0      FF41a!:2E2EQ2E2E2E2EFFr   c                :                         |                    S rT   index)rf  lhs_specrhs_specs    r   <lambda>z4make_convolution_dimension_numbers.<locals>.<lambda>R      (..!55 r   )keyc              3  (   K   | ]\  }}|d v	|V  dS ri  rH   re  s      r   r   z5make_convolution_dimension_numbers.<locals>.<genexpr>W  rj  r   c                :                         |                    S rT   rl  )rf  out_specro  s    r   rp  z4make_convolution_dimension_numbers.<locals>.<lambda>X  rq  r   )rQ  rS  rT  rY  rZ  rW  rV  rU  rI  rangerX  r[  r   r   rm  	enumeratesorted)rE  r^  ndrn  ru  ro  s      @@@r   "make_convolution_dimension_numbersrz    se   @ 	B355./+01-/0,12.895784.55eAq2v6F6FGGG/66uQB7G7GHHH/66uQB7G7GHHHH#U++ #4 Hh355.6nnS.A.A+08s0C0C-/7~~c/B/B,191D1D.8@s8K8K57?~~c7J7J4/66 7 7))7 7 7    .55FF9X..FFF55555	
 	
 	
   /66FF9X..FFF55555	
 	
 	
   
r   c                  *    e Zd ZdZdZej        Zd ZdS )PrecisionConfigz8Python representation of a xla.PrecisionConfig protobuf.operand_precisionc                    g | _         d S rT   r}  r-  s    r   r   zPrecisionConfig.__init__e  s    Dr   N)	r   r   r   r   r   r   PrecisionConfig_Precision	Precisionr   rH   r   r   r|  r|  ^  s6        @@$),)         r   r|  c                      e Zd ZdZdZd ZdS )GatherDimensionNumbersz?Python representation of a xla.GatherDimensionNumbers protobuf.offset_dimscollapsed_slice_dimsstart_index_mapindex_vector_dimc                >    g | _         g | _        g | _        d| _        d S r,  r  r-  s    r   r   zGatherDimensionNumbers.__init__s  s(    D "DDDr   Nr   rH   r   r   r  r  i  s0        GG)    r   r  c                      e Zd ZdZdZd ZdS )ScatterDimensionNumbersz@Python representation of a xla.ScatterDimensionNumbers protobuf.update_window_dimsinserted_window_dimsscatter_dims_to_operand_dimsr  c                >    g | _         g | _        g | _        d| _        d S r,  r  r-  s    r   r   z ScatterDimensionNumbers.__init__  s(     D "D(*D%Dr   Nr   rH   r   r   r  r  z  s0        HH)    r   r  c                      e Zd ZdZdZd ZdS )ReplicaGroupz5Python representation of a xla.ReplicaGroup protobuf.replica_idsc                    g | _         d S rT   r  r-  s    r   r   zReplicaGroup.__init__  s    Dr   Nr   rH   r   r   r  r    s.        ==)    r   r  c                V    t                      }|j                            |            |S rT   )r  r  rI  )replica_groupreplica_group_protos     r   _make_replica_group_protor    s)    $!((777	r   c                F    | g }nt          |           } d | D             }|S )Nc                ,    g | ]}t          |          S rH   )r  )r   groups     r   r   z'make_replica_groups.<locals>.<listcomp>  s.       -2!%((  r   )rH  )replica_groupsreplica_groups_protoss     r   make_replica_groupsr    sE    .))N 6D   
r   c              #  ~   K   t           j        }| t           _        	 dV  |t           _        dS # |t           _        w xY w)z>Context manager that enables or disables traceback collection.N)	Tracebackenabled)r  saveds     r   
tracebacksr    sH       
%)	EEEIIs   . <clientClientbytesc                N    t          j        |                                           S )zBReturns a gzipped pprof protocol buffer containing a heap profile.)gzipcompressheap_profile)r  s    r   r  r    s    	v**,,	-	--r   )TNr   r	   N)r   r   )Nr   r	   NNFrT   )r7   r:   )rB   )rC   rD   r   rE   )rM   r   rC   rD   r   rE   )rQ   rD   r   rR   )rQ   rD   rV   rD   r   r   )rQ   rD   rM   r   r   r[   )rQ   rD   r   r[   )NN)rQ   rD   r7   r:   r   r`   )rV   rc   r7   r:   )r   rh   )NNr	   )r   r   )r  rD   r  r   r#   rD   r  rw   r  r  r   r[   )r#   rD   r!  r  r   r[   )r4  r5  r   r0  )rE  rF  r   r>  )rE  r]  r^  rw   r   rQ  )T)r  r  r   r  )r   
__future__r   atexitcollections.abcr   r   
contextlibenumr  r   loggingrr   	threadingtypingr   r   r   	ml_dtypesnumpyr   rB   r
   r   ifrt_programsopsre   _versionmlir_api_versionr  	getLoggerr   r#  rD   rw   rH  rv   rR   rh   r   r9   rA   rE   get_topology_for_devicesrL   rP   r=   rZ   r]   r>   r?   rb   rg   r.   ry   r   PrimitiveTypebfloat16float8_e4m3fnfloat8_e4m3b11fnuzfloat8_e4m3fnuzfloat8_e5m2float8_e5m2fnuzPREDr   S4S8S16S32S64U4U8U16U32U64F8E4M3FNF8E4M3B11FNUZF8E5M2
F8E4M3FNUZ
F8E5M2FNUZBF16F16F32F64C64C128TUPLEobject_TOKENXLA_ELEMENT_TYPE_TO_DTYPEitemsr   r   r   ProgramShape
ShapeIndexr   DeviceAssignmentDeviceCompileOptionsHostBufferSemanticsr   r   Enumr   r   
XlaBuilderXlaComputationXlaOpFftTyper  Memory	ArrayImplLoadedExecutable
DeviceList
OpShardingHloShardingShardingNamedShardingSingleDeviceShardingPmapShardingGSPMDSharding
PjRtLayoutr  r  r   execute_with_tokenIntFlagr  r  r  r.  r  Lockr  r1   r   r    register_custom_call_partitioner encode_inspect_sharding_callbackhlo_sharding_utilr&  r0  r<  r>  rO  rQ  rz  r|  r  r  r  r  r  r  Framecontextmanagerr  r  XlaRuntimeErrorregistercollect_garbageweakref_lru_cachearray_result_handler+batched_copy_array_to_devices_with_shardingbatched_device_putbatched_block_until_ready"check_and_canonicalize_memory_kindLayoutcustom_call_targetsrH   r   r   <module>r     s
      " " " " " "  - - - - - - - -         				     ' ' ' ' ' ' ' ' ' '         # # # # # # "
h=      
 
	8	$	$CsCcE4'G!HHI      $ 	& & & &R/ / / / / $8  O O O O O &(G G G G G. . . .F F F F9 9 9 93 3 3 3+ + + + )-?CI I I I I0 JN- - - - -   <	# 	# 	# 	# 	# 	# 	# 	#    "'1 +#+((hbhv&& hbhv&& xrx((	
 xrx(( xrx(( hbhw'' hbhw'' xrx)) xrx)) xrx)) HBH]33 *<!=!= ("(;// hbh77  hbh77!" **#$ xrx	**xrx	**xrx	**xrx,,.."*--"*--1  < 7==??   
9 9 9
 	
.`    _

 *      (   & 
$. 8* * *? ? ?.    $)   
/ / /B _
$

,		N	( _
_
="0  "_
I I I I    4  &I  #         T\              :<  ; ; ; ;       '	((  %;%C    >. . . .6 $(#H  #'#H  *                  2# # # # # # # #"   :( ( ( ( ( ( ( (6F F F FR                      "       "            N	
    . . . .
 & $ % % %* 0 4 , ,  : %)%L "	.   r   