
    uki_2                        U 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	 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Z ej.                  e      Zg aee   ed	<   d
ee   ddfdZd,dZdee   fdZ defdZ! G d dejD                        Z#de#jH                  fdejJ                  dejL                  dejN                  dejP                  dede#defdZ)defdZ*dejJ                  de#fdZ+dejJ                  de#de,fdZ-dejJ                  de#de,fd Z.de#fd!Z/dejL                  ddfd"Z0d#ejL                  fd$Z1g d%Z2e2D  ch c]  } | jg                  d&       c} Z4	 d-d'Z5d( Z6d)ee   fd*Z7d+ Z8yc c} w ).    N)cast)config)version_str)_jax)
xla_client)ir)passmanager_extra_flag_prefixesflag_prefixesreturnc                     t         | z  a y)zFAdd flag prefixes to include in the cache key. Call prior to get().
  Nr
   )r   s    M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/cache_key.pyadd_flag_prefixesr   %   s     -'    c                      g a y)z4Clear flag prefixes added by add_flag_prefixes().
  Nr    r   r   clear_flag_prefixesr   ,   s
     r   c                      t         S )z5Return flag prefixes added by add_flag_prefixes().
  r   r   r   r   get_flag_prefixesr   3   s
     
r   c                       y)zCustom hook for any addition to the cache key.

  The custom hook will be called every time get() is called and can be
  defined to return a string that will be hashed into the cache key.
   r   r   r   r   custom_hookr   9   s     
r   c                   r    e Zd Z ej                         Z ej                         Z ej                         Zy)IgnoreCallbacksN)__name__
__module____qualname__enumautoNOALLCUSTOM_PARTITIONINGr   r   r   r   r   B   s*    tyy{"		#!		r   r   	zstandardmoduledevicescompile_optionsbackendcompression_algorithmignore_callbacksc           	          d fdfdd fdfdfdd fd	fd
fdfdfdfdfdd fg}t        j                         }|D ]  \  }}	 |	|       t        |||	         j                  j                  d   }
t        j                  |
      j                  }|dz   |j                         j                         z   S )a  Creates a hashed string to use as a key to the compilation cache.

  Creates a cache key that is a hex-encoded string of a unique hash based on
  the arguments. The hex-encoded string is 256 characters long.

  Args:
    module: the input program
    devices: an array of accelerator devices that the program will run on
    compile_options: options passed to the XLA compiler
    backend: description of the platform (e.g., TPU version)
    compression_algorithm: a string representing the compression algorithm used
      for the executable before persisting in the cache
    ignore_callbacks: whether to remove the all callback pointer from the
      computation.

  Typical return value example:
   'jit__psum-14ac577cdb2ef6d986078b4054cc9893a9a14a16dbb0d8f37b89167c1f1aacdf'
  computationc                     t        |       S N)_hash_computation)hash_objr*   r%   s    r   <lambda>zget.<locals>.<lambda>h   s    , 0 r   zjax_lib versionc                 \    | j                  t        t        j                  d                  S Nzutf-8)updatebytesjaxlib_version_strencoder0   s    r   r1   zget.<locals>.<lambda>n   s#    8??&--g67 r   zbackend versionc                     t        |       S r.   )_hash_platformr0   r(   s    r   r1   zget.<locals>.<lambda>t   s    >(G< r   z	XLA flagsc                 *    t        | t                     S r.   )_hash_xla_flagsr   r8   s    r   r1   zget.<locals>.<lambda>x   s    ?85F5HI r   r'   c                 :    t        | j                  dk(        S )Ngpu)strip_device_assignment) _hash_serialized_compile_optionsplatform)r0   r(   r'   s    r   r1   zget.<locals>.<lambda>|   s#    ; (/'7'75'@ r   accelerator_configc                     t        |       S r.   )_hash_accelerator_config)r0   r&   s    r   r1   zget.<locals>.<lambda>   s    3HgF r   compressionc                     t        |       S r.   )_hash_string)r0   r)   s    r   r1   zget.<locals>.<lambda>   s    <2GH r   r   c                 *    t        | t                     S r.   )rH   r   r8   s    r   r1   zget.<locals>.<lambda>   s    |Hkm'L r   sym_name-)
hashlibsha256_log_cache_key_hash	operation
attributesr   
StringAttrvaluedigesthex)r%   r&   r'   r(   r)   r*   entriesr0   namehashfnrJ   module_names   ``````      r   getrY   K   s    8   
<
 
I
 	 
F
 
H LMO('T ^^( 0ldF
8$/0 ((4(h'--+	s	X__.224	44r   last_serializedc                 P   t         j                  t        j                        rt	        j
                         } ||       t         j                  d||j                         j                                t         j                  d|| j                         j                                y y )Nz'get_cache_key hash of serialized %s: %sz+get_cache_key hash after serializing %s: %s)	loggerisEnabledForloggingDEBUGrL   rM   debugrS   rT   )r0   rZ   rW   fresh_hash_objs       r   rN   rN      s{    '^^%N
>
LL1##% LL5 (r   mc                     dt         j                  dt         j                  ffd}t        j                  k(  r| S | j
                  j                  |       | S )zvRemoves callback pointers from precompiled IR.

  Python function pointers are not deterministic across executions.
  opr   c                 \   | j                   dk(  rt        j                  k(  r(| j                  d   j                  j                  d      s| j                  d   j                  dk(  r,t        j                  j                  d      | j                  d<   t        j                  j                  S )Nzstablehlo.custom_callcall_target_namecallbackCustomSPMDPartitioningREMOVEDbackend_config)rV   r   r"   rP   rR   endswithr   rQ   rY   
WalkResultADVANCE)rd   r*   s    r   _update_bc_attributez/_remove_callbacks.<locals>._update_bc_attribute   s    	ww)) 3 330177@@L==+,226NN(*(9(9)(Dbmm$%==   r   )r   	Operationrl   r   r!   rO   walk)rb   r*   rn   s    ` r   _remove_callbacksrq      sK    
	!r|| 	! 	! +++H++'(	
(r   c                     t        j                         }|t        j                  k7  r<t	        t        t        j                  | j                  j                               |      } | j                  j                  |       |j                         S )N)file)ioBytesIOr   r!   rq   	type_castr   ModulerO   clonewrite_bytecodegetvalue)rb   r*   outputs      r   _serialize_irr|      sg    ::<&+++"))Q[[..013C	A ++&)		r   
m_originalc                 6   | j                   5  t        t        j                  | j                  j                               }t        j                  j                  d      }|j                  |j                         t        ||      cd d d        S # 1 sw Y   y xY w)Nzbuiltin.module(strip-debuginfo))contextrv   r   rw   rO   rx   pmPassManagerparserunr|   )r}   r*   rb   passess       r   _canonicalize_irr      su      ."))Z11779:A^^!!)F JJq{{,-. . .s   A8BBc                     t         j                  j                  rt        ||      }nt	        ||      }| j                  |       y r.   )r   )compilation_cache_include_metadata_in_keyrR   r|   r   r4   )r0   r%   r*   canonical_irs       r   r/   r/      s7    55;; )9:L#F,<=L
//,r   c                 R    |j                   D ]  }t        | |j                          y r.   )flatrH   device_kind)r0   r&   devices      r   _hash_devicesr      s%     /f6--./r   acceleratorsc                 J   g }|j                   D ]  }|j                  |        	 | j                  t        j                  |      j                                y # t        j                  $ r6}t        j                  d|t        |             t        | |       Y d }~y d }~ww xY w)Nzoget (_hash_accelerator_config): unable to hash accelerator config, falling back to hashing devices %s (type %s))r   appendr4   r   get_topology_for_devices	serializer   JaxRuntimeErrorr\   infotyper   )r0   r   accelerator_devicesacceleratorexs        r   rE   rE      s    !&& ,k{+,
*OO++,?@JJL 
		 * KK '(*DH6 (L))*s   2A B",,BB")z--xla_dump_compress_protosz--xla_dump_module_metadataz--xla_dump_max_hlo_modulesz--xla_dump_include_timestampz--xla_dump_hlo_pass_rez--xla_dump_hlo_module_rez--xla_dump_hlo_snapshotsz--xla_dump_fusion_visualizationz--xla_dump_hlo_as_urlz--xla_dump_hlo_as_protoz--xla_dump_hlo_as_textz--xla_dump_hlo_as_long_textz--xla_dump_hlo_as_htmlz--xla_dump_hlo_as_dotz--xla_dump_toz&--xla_force_host_platform_device_countz--xla_dump_disable_metadataz--xla_dump_hlo_pipeline_rez$--xla_tpu_sdc_checker_streamz_metricz0--xla_tpu_sdc_checker_enable_sdc_event_callbacksz3--xla_tpu_sdc_checker_enable_coresweep_ng_callbacksz9--xla_tpu_sdc_checker_no_logging_if_callbacks_are_presentz--xla_gpu_cuda_data_dirz*--xla_gpu_experimental_autotune_cache_moderK   c                    t        j                  |      }|j                  j                  }d|_        d|_        d|_        d|_        d|_        d|_	        d|_
        d|_        d|_        d|_        d|_        d|_        d|_        d|_        d|_        d|_        d|_        d|_        d|_        |j.                  D cg c]  }|d   t0        vr| c}|_        |r|j2                  r|j2                  j5                         }|j2                  j7                         }t8        j:                  j=                  t?        j@                  ||z        jC                  ||g            |_        | jE                  |jG                               S c c}w )Nr   r   F)$copydeepcopyexecutable_build_optionsdebug_options$xla_force_host_platform_device_countxla_dump_toxla_dump_hlo_module_rexla_dump_hlo_pass_rexla_dump_hlo_as_textxla_dump_hlo_as_protoxla_dump_hlo_as_dotxla_dump_hlo_as_urlxla_dump_hlo_as_htmlxla_dump_fusion_visualizationxla_dump_hlo_snapshotsxla_dump_max_hlo_modulesxla_dump_module_metadataxla_dump_compress_protosxla_dump_hlo_as_long_textxla_dump_disable_metadataxla_dump_hlo_pipeline_re(xla_gpu_experimental_autotune_cache_modexla_gpu_cuda_data_direnv_option_overrides,env_override_flags_to_exclude_from_cache_keydevice_assignmentreplica_countcomputation_countr   DeviceAssignmentcreatenparangereshaper4   SerializeAsString)r0   compile_options_objr@   compile_options_copyr   
flag_valuer   r   s           r   rA   rA     s    ':; '??MM-78-4 -)+-&')-$',-$(--%&+-#&+-#',-$05--).-&+0-(+0-(+0-(,1-),1-)+--(;<-8 )+-%
 -AA/
	AJ	J /+
 !5!G!G(::HHJM,>>PPR-7-H-H-O-O
		-"334<<+
,	..* 
-??A	BB/s   ?Fc                 \    t        | |j                         t        | |j                         y r.   )rH   rB   platform_versionr;   s     r   r:   r:   N  s"    x))*x112r   extra_flag_prefixesc                 @   g }t        j                  d      }|r|j                  |j                                t        j                  d      }|r|j                  |j                                t        j
                  D ]9  j                  d      st        fd|D              s)|j                         ; t        |      D ]U  }|j                  d      d   t        v rt        j                  d|       4t        j                  d|       t        | |       W y )	N	XLA_FLAGSLIBTPU_INIT_ARGSz--xlac              3   @   K   | ]  }j                  |        y wr.   )
startswith).0pargs     r   	<genexpr>z"_hash_xla_flags.<locals>.<genexpr>^  s      &q&s   =r   z'Not including XLA flag in cache key: %sz#Including XLA flag in cache key: %s)osgetenvextendsplitsysargvr   anyr   sorted#xla_flags_to_exclude_from_cache_keyr\   r`   rH   )r0   r   	xla_flagsxla_flags_env_varlibtpu_init_args_env_varflagr   s         @r   r=   r=   S  s    )ii,&,,./YY'9:-3356XX c
~~g# &#6& # s	 Y !dzz#q@@ll<dC
LL6=4 !r   c                 `    | j                  |j                  d      j                                y r3   )r4   r7   strip)r0   str_vars     r   rH   rH   m  s     
//'..)//12r   )r   N)F)9r   r   rL   rt   r^   r   r   typingr   rv   jax._srcr   jax._src.libr   r6   r   r   jax._src.lib.mlirr   r	   r   numpyr   	getLoggerr   r\   r
   liststr__annotations__r   r   r   r   IntEnumr   r!   rw   ndarrayCompileOptionsClientrY   rN   rq   r5   r|   r   r/   r   rE   r   r   r   rA   r:   r=   rH   )xs   0r   <module>r      s      	  	 
 $  :  #   /  
		8	$"$ d3i $(T#Y (4 (49 S $dll $ "-(7(:(:J5IIJ5ZZJ5  ..J5 	J5
 J5 &J5 	J5Z3 $ o .RYY / e 	.			.-<	.
	. /  /RZZ /D /
*RZZ *"' #8 >0AGGCL0 , >C6Cr3
!49 !43{0s   F