
    Vpf'                    >   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 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j        ecZZ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j"        df         Z#d#dZ$d#dZ%d&dZ&d'd!Z'd(d"Z(dS ))zUtilities for the Jaxpr IR.    )annotations)Counterdefaultdict)CallableN)AnyUnion)core)util)source_info_util)
xla_clientjaxpr
core.Jaxprc              #     K   | j         D ]}| |fV  	t          j        |           D ]}t          |          E d {V  d S N)eqnsr	   	subjaxprsall_eqns)r   eqnsubjaxprs      S/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/jaxpr_util.pyr   r   $   sp      Z  c#,.'' " "h!!!!!!!!!!" "    keyr   c                    t          t                    }t          |           D ])\  }}| ||                                       |           *t	          |          S r   )r   listr   appenddict)r   r   d_r   s        r   collect_eqnsr   *   sV    $!  facc#hhKs	a.r   c                    | S r    )xs    r   <lambda>r#   1   s    A r   key_fmtc                d    t          | |          }fd|                                D             S )Nc                H    i | ]\  }} |          t          |          S r!   len).0kvr$   s      r   
<dictcomp>zhistogram.<locals>.<dictcomp>3   s-    	3	3	3A''!**c!ff	3	3	3r   )r   items)r   r   r$   r   s     ` r   	histogramr.   0   s4    5#!	3	3	3	3	3	3	33r   c                $    t          | d           S )Nc                    | j         j        S r   )	primitivenamer   s    r   r#   zprimitives.<locals>.<lambda>6   s    cm&8 r   r.   )r   s    r   
primitivesr5   5   s    	588	9	99r   c                4    d }t          | |dj                  S )Nc                P    t          j        | j                  }| j        j        |fS r   r   	summarizesource_infor1   r2   )r   srcs     r   r   z!primitives_by_source.<locals>.key9   s$    

$S_
5
5CM$$r    @ r.   joinr   r   s     r   primitives_by_sourcer@   8   s'    % % % 
5#uz	*	**r   c                @    d fd}t          | |dj                  S )Nc                l    t          | t          j                  rdn| j                                        S )N*)
isinstancer	   DropVaraval	str_short)vars    r   	shape_fmtz&primitives_by_shape.<locals>.shape_fmt?   s+    S$,//I33SX5G5G5I5IIr   c                l    | j         j        d                    t          | j                            fS )N )r1   r2   r>   mapoutvars)r   rI   s    r   r   z primitives_by_shape.<locals>.keyA   s+    MY)D)D E EFFr   z :: r=   )r   r   rI   s     @r   primitives_by_shaperN   >   sG    J J JG G G G G	5#v{	+	++r   c                (    d }t          | |          S )Nc                4    t          j        | j                  S r   )r   r9   r:   r3   s    r   r   zsource_locations.<locals>.keyF   s    %co666r   r4   r?   s     r   source_locationsrQ   E   s!    7 7 7	5#		r   c                  	 i i 	d
	fd}d	fd}| j         D ]} ||d            | j        D ]} ||d            | j        D ].}|j        D ]} |||           |j        D ]} |||           /| j        D ]} ||d            	fd	D             }t	          t
          t          j        |                     }|r| |fg|n| |fS )Na	core.Atomr   MaybeEqnc                    t          | t          j                  s9| v s
J |             | v s
J |             |                              |           d S d S r   )rD   r	   Literalr   )rS   r   defsrefss     r   readzvar_defs_and_refs.<locals>.readP   sc    a&& $YYYYYY$YYYYYY
1gnnS r   r+   core.Varc                    | vs
J |             | vs
J |             t          | t          j                  s|| <   g | <   d S d S r   )rD   r	   rE   )r+   r   rX   rY   s     r   writez var_defs_and_refs.<locals>.writeV   s]    D===!===D===!===a&& d1gd1ggg r   c                2    g | ]}||         |         fS r!   r!   )r)   r+   rX   rY   s     r   
<listcomp>z%var_defs_and_refs.<locals>.<listcomp>k   s(    ---1!T!Wd1g	---r   )rS   rT   r   rU   )r+   r[   r   rU   )	constvarsinvarsr   rM   rL   var_defs_and_refsr	   r   )
r   rZ   r]   r+   r   rS   ressubsrX   rY   s
           @@r   rb   rb   L   sn   #%$)+$             ?  a	E!TNNNN<  a	E!TNNNNZ  cZ  
d1cllll[  eAsmmmm =  aDDMMMM--------#	u 5 5	6	6$"&	85#,			UCL8r   c                L    d fdfdt          |           D             S )Nc                j    ||  dS t          j        |j                  }|  d|j        j         d| S )Nz	 <- invarz <- r<   r8   )rH   r   r;   s      r   fmt_keyzvars_by_fanout.<locals>.fmt_keyp   sI    
{&s77c55+55555r   c                     fd|D             S )Nc                L    i | ] \  }}} ||          t          |          !S r!   r'   )r)   rH   var_defvar_refsrg   s       r   r,   z0vars_by_fanout.<locals>.hist.<locals>.<dictcomp>x   sC     1 1 1&Wh GC!!3x== 1 1 1r   r!   )r   readsrg   s     r   histzvars_by_fanout.<locals>.histw   s.    1 1 1 1*/1 1 1 1r   c                2    g | ]\  }}| ||          fS r!   r!   )r)   jrl   rm   s      r   r_   z"vars_by_fanout.<locals>.<listcomp>{   s,    	G	G	G(!U1dd1enn
	G	G	Gr   )rb   )r   rg   rm   s    @@r   vars_by_fanoutrp   o   sR    6 6 61 1 1 1 1 
H	G	G	G.?.F.F	G	G	GGr   r.   dict[Any, int]c                6   t          d |                                 D                       }dt          |          z   dz   }d |                                 D             }t	          |d          D ](\  }}t          |                    |          |           )d S )Nc              3  N   K   | ] }t          t          |                    V  !d S r   )r(   str)r)   r+   s     r   	<genexpr>z"print_histogram.<locals>.<genexpr>~   s.      <<ACAKK<<<<<<r   z{:>zd}c                    g | ]	\  }}||f
S r!   r!   )r)   r*   r+   s      r   r_   z#print_histogram.<locals>.<listcomp>   s     
0
0
0daAq6
0
0
0r   T)reverse)maxvaluesrt   r-   sortedprintformat)r.   count_width	count_fmtpairscountr2   s         r   print_histogramr   }   s    <<)9)9););<<<<<+c+&&&-)
0
0ioo//
0
0
0%E4000 ) )keT	)

5
!
!4(((() )r   profile=dict[tuple[xla_client.Traceback | None, core.Primitive], int]returnbytesc                j   t          t          j        d          j                  t          t          j        d          j                  t          t          j        d          j                  dd<   d         }g }|                                 D ]_\  \  }}}|g }n)t          |                                 }fd|D             }|                    ||g||j                 dgd           `fd	                                D             }fd
                                D             }	d         d         dg}
t          j
        t                                                    ||	|
|d          }t          j        t          j                            |                    S )zConverts a profile into a compressed pprof protocol buffer.

  The input profile is a map from (traceback, primitive) pairs to counts.
     r    r1   Nc                \    g | ](\  }}t          j        |j                  ||f         )S r!   )r   is_user_filenameco_filename)r)   codelastilocs      r   r_   z"_pprof_profile.<locals>.<listcomp>   sO     H H H{tU#4T5EFFHT5M" H H Hr   )r   rt   )location_idvaluelabelc                v    g | ]5\  \  }}}||         t           j                            ||          d gd6S ))function_idline)idr   )r   	Tracebackcode_addr2line)r)   r   r   loc_idfuncs       r   r_   z"_pprof_profile.<locals>.<listcomp>   sn         -4  $T
#-<<T5IIK K LM M  r   c                r    g | ]3\  }}||j                  |j                  |j                 |j        d 4S ))r   r2   system_namefilename
start_line)co_namer   co_firstlineno)r)   r   func_idss      r   r_   z"_pprof_profile.<locals>.<listcomp>   s\        $ oT%&(	* *  r   	equationsr   )typeunit)string_tablelocationfunctionsample_typesample)r   	itertoolsr   __next__r-   zip
raw_framesr   r2   jsondumpsr   keysgzipcompressr   _xlajson_to_pprof_profile)r   primitive_keysamplestbr1   r   framesr   	locations	functionsr   json_profiler   r   r   s               @@@r   _pprof_profiler      s    )/!$$-..!	Y_Q''0	1	1$IOA&&/00#
!B%K.-' '  or9u	zff(jH H H Hj H H HfNN).!
 
          $'99;;	  )     ::<<  ) K.!G*==>+ NN   , 
z<<\JJ	K	KKr   c                l    t          d t          |           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  B   K   | ]\  }}|j         j        |j        fV  d S r   )r:   	tracebackr1   )r)   r   r   s      r   ru   z)pprof_equation_profile.<locals>.<genexpr>   sF        
!S  #-0     r   )r   r   r   )r   r   s     r   pprof_equation_profiler      sE       UOO    ! 
		r   )r   r   )r   r   r   r   )r   r   r   r   r$   r   )r.   rq   )r   r   r   r   )r   r   r   r   ))__doc__
__future__r   collectionsr   r   collections.abcr   r   r   r   typestypingr   r   jax._srcr	   r
   r   jax._src.libr   safe_maprL   
unsafe_mapsafe_zipr   
unsafe_zipr   r   r.   r5   r@   rN   rQ   JaxprEqnrU   rb   rp   r   r   r   r!   r   r   <module>r      s   " ! " " " " " " , , , , , , , , $ $ $ $ $ $                            % % % % % % # # # # # #-Z-Z" " " "    #.+4 4 4 4 4
: : : :+ + + +, , , ,   
 $%!9 !9 !9 !9FH H H H) ) ) ):L :L :L :Lz     r   