
    vhT!                       d Z ddlmZ ddlmZm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mZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ej,                  ecZZej2                  ecZZ	 	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZddZd f	 d dZd!dZ d!dZ!d!dZ"d!dZ#eejH                  df   Z%d!dZ&d!dZ'd"dZ(	 	 	 	 d#dZ)d$dZ*d%dZ+d&dZ,d'dZ-y)(zUtilities for the Jaxpr IR.    )annotations)Counterdefaultdict)CallableN)AnyIteratorUnion)core)util)source_info_util)
xla_clientc              #     K   | j                   D ]  }| |f 
 t        j                  |       D ]C  }|t        ||      E d {    ||vs|j	                  |       t        ||      E d {    E y 7 27 	wN)eqnsr
   	subjaxprs	_all_eqnsadd)jaxprvisitedeqnsubjaxprs       N/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/jaxpr_util.pyr   r   $   s      ZZ c#,..' .h8W---		 kk(8W---.- .s*   AA:A6	A: A:.A8/A:8A:c              #  P   K   t        | |rd n	t                     E d {    y 7 wr   )r   set)r   revisit_inner_jaxprss     r   all_eqnsr   0   s      u&:dFFFs   &$&c                    t        t              }t        |       D ]  \  }}| ||         j                  |       ! t	        |      S r   )r   listr   appenddict)r   keyd_r   s        r   collect_eqnsr$   6   sB    $! fac#hKs	a.    c                    | S r    )xs    r   <lambda>r)   =   s    A r%   c                    t        | |      }|j                         D ci c]  \  }} ||      t        |       c}}S c c}}w r   )r$   itemslen)r   r!   key_fmtr"   kvs         r   	histogramr0   <   s9    5#!)*	3A'!*c!f
	33	3s   A c                    t        | d       S )Nc                .    | j                   j                  S r   )	primitivenamer   s    r   r)   zprimitives.<locals>.<lambda>B   s    cmm&8&8 r%   r0   )r   s    r   
primitivesr7   A   s    	58	99r%   c                6    d }t        | |dj                        S )Nc                p    t        j                  | j                        }| j                  j                  |fS r   r   	summarizesource_infor3   r4   )r   srcs     r   r!   z!primitives_by_source.<locals>.keyE   s+    

$
$S__
5CMM$$r%    @ r0   joinr   r!   s     r   primitives_by_sourcerB   D   s    % 
5#uzz	**r%   c                B    d fd}t        | |dj                        S )Nc                n    t        | t        j                        rdS | j                  j	                         S )N*)
isinstancer
   DropVaraval	str_short)vars    r   	shape_fmtz&primitives_by_shape.<locals>.shape_fmtK   s'    S$,,/3ISXX5G5G5IIr%   c                z    | j                   j                  dj                  t        | j                              fS )N )r3   r4   r@   mapoutvars)r   rK   s    r   r!   z primitives_by_shape.<locals>.keyM   s,    MMY)D EFFr%   z :: r?   )r   r!   rK   s     @r   primitives_by_shaperP   J   s"    JG	5#v{{	++r%   c                     d }t        | |      S )Nc                @    t        j                  | j                        S r   )r   r;   r<   r5   s    r   r!   zsource_locations.<locals>.keyR   s    %%coo66r%   r6   rA   s     r   source_locationsrS   Q   s    7	5#	r%   c                  	 i i 	d	fd}d	fd}| j                   D ]  } ||d         | j                  D ]  } ||d         | j                  D ]6  }|j                  D ]  } |||        |j                  D ]  } |||        8 | j                  D ]  } ||d         D cg c]  }||   	|   f }}t	        t
        t        j                  |             }|r| |fg|S | |fS c c}w )Nc                    t        | t        j                        s+| v sJ |        | v sJ |        |    j                  |       y y r   )rF   r
   Literalr   )ar   defsrefss     r   readzvar_defs_and_refs.<locals>.read\   sH    a&$YY$YY
1gnnS 'r%   c                |    | vsJ |        | vsJ |        t        | t        j                        s|| <   g | <   y y r   )rF   r
   rG   )r/   r   rX   rY   s     r   writez var_defs_and_refs.<locals>.writeb   sJ    D=!=D=!=a&d1gd1g 'r%   )rW   z	core.Atomr   MaybeEqn)r/   core.Varr   r]   )	constvarsinvarsr   rO   rN   var_defs_and_refsr
   r   )
r   rZ   r\   r/   r   rW   ressubsrX   rY   s
           @@r   ra   ra   X   s   #%$)+$ ?? a	!TN<< a	!TN ZZ cZZ 
1cl[[ Asm == aDM )--1!T!Wd1g	-#-	u 5	6$"&5#,		8UCL8 	.s   ,C0c           	     n    d fd}t        |       D cg c]  \  }}| |||      f c}}S c c}}w )Nc                    ||  dS t        j                  |j                        }|  d|j                  j                   d| S )Nz	 <- invarz <- r>   r:   )rJ   r   r=   s      r   fmt_keyzvars_by_fanout.<locals>.fmt_key|   sJ    
{uI&&s7cuD++,Cu55r%   c           	     d    |D ci c]  \  }}} ||      t        |       c}}}S c c}}}w r   )r,   )r   readsrJ   var_defvar_refsrf   s        r   histzvars_by_fanout.<locals>.hist   s>    */1 1&Wh C!3x=0 1 1 1s   +)ra   )r   rk   jrh   rf   s       @r   vars_by_fanoutrm   {   s8    61 /@.F	G(!U1d1en
	GG	Gs   1c                   t        d | j                         D              }dt        |      z   dz   }| j                         D cg c]	  \  }}||f }}}t	        |d      D ]   \  }}t        |j                  |      |       " y c c}}w )Nc              3  D   K   | ]  }t        t        |              y wr   )r,   str).0r/   s     r   	<genexpr>z"print_histogram.<locals>.<genexpr>   s     <ACAK<s    z{:>zd}T)reverse)maxvaluesrp   r+   sortedprintformat)r0   count_width	count_fmtr.   r/   pairscountr4   s           r   print_histogramr}      s    <)9)9);<<+c+&&-)'oo/
0daAq6
0%
0E40 )keT	)

5
!4() 1s   Bc                   t        t        j                  d      j                        }t        t        j                  d      j                        }t        t        j                  d      j                        }d|d<   |d   }g }| j	                         D ]  \  \  }}}|g }	nNt        |j                          }
|
D cg c]+  \  }}t        j                  |j                        r|||f   - }	}}|j                  |	|g|||j                     dgd        |j	                         D cg c]2  \  \  }}}|||   t        j                  j                  ||      dgd4 }}}}|j	                         D cg c]>  \  }}|||j                     ||j                     ||j                     |j                   d	@ }}}|d
   |d   dg}t#        j$                  t'        |j)                               ||||d      }t+        j,                  t        j.                  j1                  |            S c c}}w c c}}}w c c}}w )zConverts a profile into a compressed pprof protocol buffer.

  The input profile is a map from (traceback, primitive) pairs to counts.
     r    r3   )r!   rp   )location_idvaluelabel)function_idline)idr   )r   r4   system_namefilename
start_line	equationsr|   )typeunit)string_tablelocationfunctionsample_typesample)r   	itertoolsr|   __next__r+   zip
raw_framesr   is_user_filenameco_filenamer   r4   r   	Tracebackcode_addr2lineco_nameco_firstlinenojsondumpsr   keysgzipcompress_xlajson_to_pprof_profile)profilesfunclocprimitive_keysamplestbr3   r|   framesr   codelastiloc_id	locationsfunc_id	functionsr   json_profiles                      r   _pprof_profiler      sF    )//!$--.!	Y__Q'00	1$IOOA&//0#
!B%K.-' ' or9u	zf(j6@ H{tU#44T5E5EF T5M" Hf HNN)..!
  ( $'99;	   -4  $T
#--<<T5IK LM)   ::< $ oT%%&((	*)  K.!G*=>+ N , 
z<<\J	KKIHs   >0H2.7H8<AH?c                R    t        d t        | d      D              }t        |      S )as  Generates a pprof profile that maps jaxpr equations to Python stack traces.

  By visualizing the profile using pprof, one can identify Python code that is
  responsible for yielding large numbers of jaxpr equations.

  Args:
    jaxpr: a Jaxpr.

  Returns:
    A gzip-compressed pprof Profile protocol buffer, suitable for passing to
    pprof tool for visualization.
  c              3  f   K   | ])  \  }}|j                   j                  |j                  f + y wr   )r<   	tracebackr3   )rq   r#   r   s      r   rr   z)pprof_equation_profile.<locals>.<genexpr>   s/      
!S   #--0s   /1F)r   )r   r   r   )r   r"   s     r   pprof_equation_profiler      s/      U? ! 
	r%   c              #     K   | j                   D ],  }t        |j                        D ]  \  }}||k(  s||f  , . yw)zMFind all the equations which use invar and the positional index of its binderN)r   	enumerater`   )r   invarr   invar_indexeqn_vars        r   eqns_using_var_with_invar_indexr      sL     ZZ c )#** 5 W	E	;s   0??c              #    K   | j                         D ]  }t        |t              r|n|f}|D ]  }t        |t        j                        r;|t        |j                        k\  rt        d| d      ||j                  |   f Xt        |t        j                        ss|t        |j                  j                        k\  rt        d| d      |j                  |j                  j                  |   f   y w)NzFailed to find index z& in jaxpr.invars while building report)
ru   rF   tupler
   Jaxprr,   r`   RuntimeErrorClosedJaxprr   )paramsindexvalvalsr/   s        r   jaxpr_and_binder_in_paramsr      s     ]]_ 
-cS%(3sfD -	Atzz	"CM!4UG;abc
c%  a))*C''4UG;abc
cggqww~~e,,,-
-s   BC= AC=c              #     K   t        | |      D ]I  \  }}t        t        |j                  |            x}r|D ]  \  } }t	        | |      E d{     F| K y7 w)z(Find the leaf equations using a variableN)r   r   r   r   eqns_using_var)r   r   r   r   child_jaxprs_and_varss        r   r   r      so      :%G c;!&'A#**k'Z![[[1 0.5%!%///0 i 	0s   AAAA)r   
core.Jaxprr   zset[core.Jaxpr] | Nonereturn*Iterator[tuple[core.Jaxpr, core.JaxprEqn]])T)r   r   r   boolr   r   )r   r   r!   r   )r   r   r!   r   r-   r   )r   r   )r0   zdict[Any, int])r   z=dict[tuple[xla_client.Traceback | None, core.Primitive], int]r   bytes)r   r   r   r   )r   r   r   r^   r   z#Iterator[tuple[core.JaxprEqn, int]])r   intr   z%Iterator[tuple[core.Jaxpr, core.Var]])r   r   r   r^   r   zIterator[core.JaxprEqn]).__doc__
__future__r   collectionsr   r   collections.abcr   r   r   r   typestypingr   r   r	   jax._srcr
   r   r   jax._src.libr   safe_maprN   
unsafe_mapsafe_zipr   
unsafe_zipr   r   r$   r0   r7   rB   rP   rS   JaxprEqnr]   ra   rm   r}   r   r   r   r   r   r'   r%   r   <module>r      s   " " , $     ' '   % #--Z--Z
.
. 6
./
. 59GG-1G/G #.44
:+,
 $%!9FH):LJ:L
:Lz&-	r%   