
    vh{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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jB                        Z"de"jF                  fdejH                  dejJ                  dejL                  dejN                  dede"d
efdZ(defdZ)dejH                  de"fdZ*dejH                  de"d
e+fdZ,dejH                  de"d
e+fdZ-de"fd Z.dejJ                  d
dfd!Z/d"ejJ                  fd#Z0g d$Z1e1D  ch c]  } | je                  d%       c} Z3	 d,d&Z4d' Z5d(ee   fd)Z6d* Z7yc c} w )-    N)cast)config)version_str)
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/opt/face_recognition/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   2   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   8   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   A   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>g   s    , 0 r   zjax_lib versionc                 \    | j                  t        t        j                  d                  S Nzutf-8)updatebytesjaxlib_version_strencoder/   s    r   r0   zget.<locals>.<lambda>m   s#    8??&--g67 r   zbackend versionc                     t        |       S r-   )_hash_platformr/   r'   s    r   r0   zget.<locals>.<lambda>s   s    >(G< r   z	XLA flagsc                 *    t        | t                     S r-   )_hash_xla_flagsr   r7   s    r   r0   zget.<locals>.<lambda>w   s    ?85F5HI r   r&   c                 :    t        | j                  dk(        S )Ngpu)strip_device_assignment) _hash_serialized_compile_optionsplatform)r/   r'   r&   s    r   r0   zget.<locals>.<lambda>{   s#    ; (/'7'75'@ r   accelerator_configc                     t        |       S r-   )_hash_accelerator_config)r/   r%   s    r   r0   zget.<locals>.<lambda>   s    3HgF r   compressionc                     t        |       S r-   )_hash_string)r/   r(   s    r   r0   zget.<locals>.<lambda>   s    <2GH r   r   c                 *    t        | t                     S r-   )rG   r   r7   s    r   r0   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)   entriesr/   namehashfnrI   module_names   ``````      r   getrX   J   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DEBUGrK   rL   debugrR   rS   )r/   rY   rV   fresh_hash_objs       r   rM   rM      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)rU   r   r!   rO   rQ   endswithr   rP   rX   
WalkResultADVANCE)rc   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   	Operationrk   r   r    rN   walk)ra   r)   rm   s    ` r   _remove_callbacksrp      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    rp   	type_castr   ModulerN   clonewrite_bytecodegetvalue)ra   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))contextru   r   rv   rN   rw   pmPassManagerparserunr{   )r|   r)   ra   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_keyrQ   r{   r   r3   )r/   r$   r)   canonical_irs       r   r.   r.      s7    55;; )9:L#F,<=L
//,r   c                 R    |j                   D ]  }t        | |j                          y r-   )flatrG   device_kind)r/   r%   devices      r   _hash_devicesr      s%     /f6--./r   acceleratorsc                 ^   g }|j                   D ]  }|j                  |        	 | j                  t        j                  |      j                                y # t        j                  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   appendr3   r   get_topology_for_devices	serialize_xlaXlaRuntimeErrorr[   infotyper   )r/   r   accelerator_devicesacceleratorexs        r   rD   rD      s    !&& ,k{+,
*OO++,?@JJL 
	(	( * KK '(*DH6 (L))*s   2A B,6,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_moderJ   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reshaper3   SerializeAsString)r/   compile_options_objr?   compile_options_copyr   
flag_valuer   r   s           r   r@   r@     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                         t        | |j                         y r-   )rG   rA   platform_versionruntime_typer:   s     r   r9   r9   M  s2    x))*x112x--.r   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_   rG   )r/   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 r2   )r3   r6   strip)r/   str_vars     r   rG   rG   m  s     
//'..)//12r   )r   N)F)8r   r   rK   rs   r]   r   r   typingr   ru   jax._srcr   jax._src.libr   r5   r   jax._src.lib.mlirr   r   r   numpyr   	getLoggerr   r[   r	   liststr__annotations__r   r   r   r   IntEnumr   r    rv   ndarrayCompileOptionsClientrX   rM   rp   r4   r{   r   r.   r   rD   r   r   r   r@   r9   r<   rG   )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/!49 !43}0s   E<