
    Vpfd"                        d Z ddlm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	Z	ddl
mZ 	 ddlmZ n# e$ r dZY nw xY w	 ddlZn# e$ r dZY nw xY w ej        ddd          Z ej        d	dd
          Z ej        ddd          Z ej        ddd          Z ej        dddd          Z ej        ddd          Z ej        ddd          ZdddZd Z ej        ed          Z ej        ed          Zd Z d Z!i dej"        d ej#        d!ej$        d"ej%        d#ej&        d$ej'        d%ej(        d&ej)        d'ej*        d(ej+        d)ej,        d*ej-        d+ej.        d,ej/        d-ej0        d.ej1        d/ej2        Z3 e4ed0          r
ej5        e3d1<    e4ed2          r
ej6        e3d3<    ej7        d4d58                    e3           d6          Z9d7 Z:e;d8k    r e:              ej<        e            dS dS )9aL  Tool to convert a JAX function to serialized representations.

This script is meant to be used as part of a genrule that converts a JAX program
into an IR that can be consumed by another system (e.g. a compiler).

Convert to HLO
==============

For example, you can generate an HLO proto for the XLA compiler. The HLO proto
represents an XLA program, and can be run from e.g. a C++ program, without
involving any Python.

This lets you use JAX as a convenient frontend for writing "XLA programs".  From
another perspective, this script lets you make JAX into an ahead-of-time JAX ->
XLA compiler, although when you run the XLA program, it will still be compiled
just-in-time.

See tensorflow/compiler/xla/service/hlo_runner.h.

Usage:

  $ cat prog.py
  import jax.numpy as jnp

  def fn(x, y, z):
    return jnp.dot(x, y) / z

  $ python jax_to_ir.py \
    --fn prog.fn \
    --input_shapes '[("y", "f32[128,32]"), ("x", "f32[8,128]")]' \
    --constants '{"z": 3.14159}' \
    --ir_format HLO \
    --ir_human_dest /tmp/fn_hlo.txt \
    --ir_dest /tmp/fn_hlo.pb

Alternatively, you can use this script via a genrule.  This way bazel will
generate the hlo text/proto as part of compilation, and then e.g. a C++ program
can depend on this.  See jax_to_hlo macro in build_defs.bzl.

The order of elements in input_shapes determines the order of parameters in the
resulting HLO program.

Values of `constants` which are lists are converted to Numpy arrays using
jnp.asarray.  In addition, you can specify constants using the flag
--evaled_constants; values there that are strings are first evaluated using
ast.literal_eval.  --evaled_constants is primarily useful for genrules; Skylark
doesn't support floating-point types, so genrules need to deal in strings.

Note that XLA's backwards-compatibility guarantees for saved HLO are currently
(2019-06-13) best-effort.  It will mostly work, but it will occasionally break,
and the XLA team won't (and in fact will be unable to) help.  One way to be sure
it won't break is to use the same version of XLA to build the HLO as you use to
run it.  The genrule above makes this easy.
    )literal_evalN)app)flags)jax2tffnz<Fully-qualified name of function that we're going to convertinput_shapesz+Python dict indicating XLA shapes of params	constantsz{}z2Python dict giving constant values for some paramsevaled_constantszPython dict giving constant values for some params.  Values in this dict that are of type str are evaluated using ast.literal_eval.	ir_formatHLO)r   TFzOutput format.ir_destzFile to write IR toir_human_destz)File to write human readable debug output)r	   c                   |si }d D             t          |                                          z  }|r2t          dd                    t	          |                    z            d D             }t          j        | fi |fd}|dk    rX t          j        |          j	        | 
                    d          }|                                }|                                }	n|dk    sJ t          t          d
          t          j        |d          }
t!          |
          }
t          j        |
d          }
 |
j        | j                                        }|                                }t-          |          }	||	fS )aW  Converts a JAX function to a serialized ir and a debug txt dump.

  Args:
    fn: Function to convert.
    input_shapes: List of tuples (arg name, jax.core.ShapedArray),
      indicating the shapes of the arguments to fn.  The order of parameters in
      the resulting XLA program will match the order in this list.
    constants: Dict mapping function argument name to a Python value.  Specified
      arguments these values as compile-time constants.
    format: Which IR format to use. Supported values are 'HLO' and 'TF'.

  Returns:
    A tuple of (compiler_suitable_ir, human_readable_ir).
  c                     h | ]\  }}|S  r   .0arg_name_s      S/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/tools/jax_to_ir.py	<setcomp>zjax_to_ir.<locals>.<setcomp>   s    ???;8Qh???    z;Arguments appear in both `input_shapes` and `constants`: %sz, c                 R    g | ]$\  }}t          j        |j        |j                  %S r   )jnpzerosshapedtype)r   r   ss      r   
<listcomp>zjax_to_ir.<locals>.<listcomp>   s,    	?	?	?$!Q#)AGQW
%
%	?	?	?r   c            
      b    d D             } di t          t          ||                     S )Nc                     g | ]\  }}|S r   r   r   s      r   r    z6jax_to_ir.<locals>.ordered_wrapper.<locals>.<listcomp>   s    :::kh:::r   r   )dictzip)args	arg_names
fn_curriedr   s     r   ordered_wrapperz"jax_to_ir.<locals>.ordered_wrapper   s>    ::\:::I:33SD1122333r   r   hlor   Nz;Conversion to TF graph requires TensorFlow to be installed.F)native_serialization)	autograph)setkeys
ValueErrorjoinsorted	functoolspartialjaxjitlowercompiler_iras_serialized_hlo_module_protoas_hlo_texttfr   converttf_wrap_with_input_namesfunctionget_concrete_functiongraphas_graph_defSerializeToStringstr)r   r   r	   formatoverlapping_argsr%   r(   compserialized_proto	debug_txtfgr'   s    `          @r   	jax_to_irrI   t   s    
 I??,???#nnC C  -
E		&)**++	,- - -
 
@	?,	?	?	?$  11y11*4 4 4 4 4 4 u__)37?##)40<<UCCD::<<  ""IIT>>>>	z
GI I I 	UCCCA L11A
A'''A&,99;;A**,,AI	9	$$r   c                       fd}|S )Nc                  \    t          d t          |           D                       }  |  S )Nc              3   N   K   | ] \  }\  }}t          j        ||           V  !dS ))nameN)r9   identity)r   arM   r   s       r   	<genexpr>z<tf_wrap_with_input_names.<locals>.wrapper.<locals>.<genexpr>   sR       O O&2a$AD!!!O O O O O Or   )tupler$   )r%   rG   r   s    r   wrapperz)tf_wrap_with_input_names.<locals>.wrapper   sL     O O69$6M6MO O O O OD 1d8Or   r   )rG   r   rR   s   `` r   r;   r;      s)         
 
.r   )rB   r   c                    t          |           dk    rt          j        d          t          j        s t
          j        st          j        d          t          j                            dd          \  }}t          j
        |          }t          ||          }d t          t          j                  D             }i }t          t          j                                                  D ]3\  }}t!          |t"                    rt%          j        |          }|||<   4t          t(          j                                                  D ]m\  }}t!          |t*                    rt          |          }t!          |t"                    rt%          j        |          }||v rt-          d|z            |||<   nt/          |||t0          j                  \  }	}
t          j        rGt3          t          j        d          5 }|                    |	           d d d            n# 1 swxY w Y   t
          j        rJt3          t
          j        d	          5 }|                    |
           d d d            d S # 1 swxY w Y   d S d S )
N   z%No positional arguments are accepted.z:At least one of --ir_dest and --ir_human_dest is required..c                 6    g | ]\  }}|t          |          fS r   )parse_shape_str)r   rM   	shape_strs      r   r    zmain.<locals>.<listcomp>   s?     L L L%dI 334 L L Lr   z?Argument appears in both --constants and --evaled_constants: %s)r	   rB   wbw)lenr   
UsageError_IR_DESTvalue_IR_HUMAN_DESTError_FNrsplit	importlibimport_modulegetattrr   _INPUT_SHAPES
_CONSTANTSitems
isinstancelistr   asarray_EVALED_CONSTANTSrA   r.   rI   
_IR_FORMATopenwrite)argvmodule_namefn_namemoduler   r   r	   kvirdebug_irrG   s               r   mainrx      s   YY!^^
.@
A
AA	 4 4 4
) 3 4 4 4 ))#q11+w";//&vw"L L)5m6I)J)JL L L, ):+,,2244  da!T 
+a..aIaLL,23399;;  da!S 
q//a!T 
+a..aI~~
Ka
OQ Q QIaLL2|y","24 4 4,"h ^ 	hnd	#	# qggbkkk                	n"C	(	( Aggh                  s$   >H  H$'H$I44I8;I8c                    t                               |           }|s't          d|  dt          t                               t          |                    d                   }|                    d          r@t          d |                    d                              d          D                       }nd}t          j	        
                    ||          S )NzInvalid shape z=. Valid example: "f32[1,2,3]".Note that dtype must be one of rT      c              3   X   K   | ]%}t          |                                          V  &d S )N)intstrip)r   ds     r   rP   z"parse_shape_str.<locals>.<genexpr>   s0      DDQ#aggii..DDDDDDr   ,r   )	_SHAPE_REmatchr.   rj   _DTgrouprQ   splitr3   coreShapedArray)r   r   r   r   s       r   rW   rW      s    
//!

%	 D
 Ca C C7;CyyC C D D D
ekk!nn
%
[[^^ DD%++a..*>*>s*C*CDDDDDEEE			eU	+	++r   predu4u8u16u32u64s4s8s16s32s64bf16f16f32f64c64c128int2s2uint2u2z^(|z)\[\s*(\d*[\s*,\d+]*)\s*\]$c                  V    t          j        d           t          j        d           d S )Nr   r   )r   mark_flag_as_requiredr   r   r   set_up_flagsr     s*    d###n-----r   __main__)=__doc__astr   rc   r1   reabslr   r   r3   	jax.numpynumpyr   jax.experimentalr   ImportError
tensorflowr9   DEFINE_stringra   rf   rg   rl   DEFINE_enumrm   r]   r_   rI   r;   r2   
jax_to_hlo	jax_to_tfrx   rW   bool_uint4uint8uint16uint32uint64int4int8int16int32int64bfloat16float16float32float64	complex64
complex128r   hasattrr   r   compiler/   r   r   __name__runr   r   r   <module>r      s  5 5n               				             



      %%%%%%%   &&&   """ e$N  $#DG  !U K 
 (E'   U'7 
 5y$0EFF$$TF 
 .2 6% 6% 6% 6% 6%r   Yy777
Ii555	) ) )X
, 
, 
,
CI#)39&+SZ9>
LQSVS] 	#( #( %*39 7<SY INsy CL	
 
3;
 s{
 -23; 
3= !#. 73 h#d)
73 i#d)BJMCHHSMMMMMNN	. . .
 z,...	#'$----- s   3 ==A AA