
    Vpf,                        d 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	 ddl
mZ ddlmZ ddlZd Zed%d            Zed&d            Zed'd            Zed(d            Zd)dZd*dZd+dZd,dZd-d$ZdS ).zPallas utility functions.    )annotations)overloadN)lax)core)
split_listc                      fd}|S )Nc                    t          t                    rr |              d S d S t          j        | d            d S )Nc                     d S N r       U/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/pallas/utils.py<lambda>z(when.<locals>._wrapped.<locals>.<lambda>"   s    T r   )
isinstanceboolr   cond)f	conditions    r   _wrappedzwhen.<locals>._wrapped   sT    )T"" +	 	  
hy!\\*****r   r   )r   r   s   ` r   whenr      s#    + + + + + 
/r   aintbreturnc                    d S r   r   r   r   s     r   cdivr   %       #r   	jax.Arrayc                    d S r   r   r   s     r   r   r   )   r   r   c                    d S r   r   r   s     r   r   r   -   r   r   c                    d S r   r   r   s     r   r   r   1   r   r   int | jax.Arrayc                    t          | t                    r t          |t                    r| |z   dz
  |z  S t          j        | |z   dz
  |          S )N   )r   r   r   divr   s     r   r   r   5   sR    3 Jq#.. EAI!	QA		r   shapetuple[int, ...]c                    t          j        |           }g }| D ])}||z  }|                    t          |                     *t	          |          S r   )npprodappendr   tuple)r'   sizestridesss       r   strides_from_shaper1   ;   sS    	$'  a19DNN3t99	wr   xc                p    | dk     rt          d          | dk    rdnd| dz
                                  z  S )z;Returns the next power of two greater than or equal to `x`.r   z2`next_power_of_2` requires a non-negative integer.r%      )
ValueError
bit_length)r2   s    r   next_power_of_2r7   D   sA    UU
I
J
JJ1ff!A113333r   jaxprjax_core.Jaxpr
num_consts	num_carrytuple[jax_core.Jaxpr, bool]c                D   |dk    r| j         |         }| j        d         }|j        j        s$|j        j        t
          j        t
          j        fvrt          d|j         d|           |j        j        s$|j        j        t
          j        t
          j        fvrt          d|j         d|           t          | j
                  D ]{\  }}|j        t          j        k    ra|j         d         |k    rPt          |j         d         t          j                  r+|j         d         j        dk    r|j        d         |k    r|} n|t          d          |                     | j
        d |         | j
        |dz   d          z   | j        dd                    } d}nd	}| |fS )
Nr   znot a fori_loop index in: z jaxpr=znot a fori_loop index out: r%   z!Unable to match fori_loop pattern)eqnsoutvarsTF)invarsr?   avalr'   dtypejnpint32int64NotImplementedError	enumerater>   	primitiver   add_pr   jax_coreLiteralvalreplace)	r8   r:   r;   in_index_varout_index_varieqn	eqn_indexhas_loop_indexs	            r   pattern_match_scan_to_fori_looprT   K   s    ]] <
+LM!$M E	39'===
C|'8
C
C5
C
CE E E  G CI(>>>
E(:
E
EU
E
EG G G EJ'' 	E 	E3	#)	#	#:a=L((
1x'788 z!} A%%Q=00	 CDDD MMZ

#ejQ&@@abb!  # #E NN N		r   
cond_jaxprcond_nconsts
body_jaxprbody_nconsts(tuple[jax_core.Jaxpr | None, str | None]c                X   |rdS t          | j        j        |g          \  }}d |D             }t          |          dk     rdS |d d         \  }}|j        s|j        t          j        t          j        fvrdS |j        s|j        t          j        t          j        fvrdS |d d         \  }	}
| j        j	        d         }|j
        j        t          j        k    sJ t          | j        j                  dk    rd	S | j        j        d         }|j        t          j        k    rd	S |j	        |gk    rd	S |j        |	|
gk    rd	S t          |j        j        |g          \  }}|d d         \  }	}
|j        j	        d d         \  }}|
|urd
S t!          |j        j                  D ]w\  }}|j        t          j        u r_|j        d         |	u rPt%          |j        d         t&          j                  r+|j        d         j        dk    r|j	        d         |k    r|} nxdS |j        }g |j        d |         |j        |         |j        |dz   d          R }t-          |j	        dd                    }|                    |j        d |         |j        |dz   d          z   ||          }|d fS )N)Nz'Conditional jaxpr can't contain consts.c                    g | ]	}|j         
S r   )rA   ).0vs     r   
<listcomp>z4pattern_match_while_to_fori_loop.<locals>.<listcomp>   s    ///a16///r   r4   )Nz+Conditional jaxpr have only two carry args.)Nz6First conditional jaxpr carry arg is not a scalar int.)Nz7Second conditional jaxpr carry arg is not a scalar int.r   r%   )Nz-Non-trivial conditional jaxprs not supported.)Nz!Loop upper bound is not constant.)Nz#Loop index not incremented in body.)r>   r@   r?   )r   r8   r@   lenr'   rB   rC   rD   rE   r?   rA   bool_r>   rH   r   lt_prG   rI   r   rJ   rK   rL   r-   rM   )rU   rV   rW   rX   _cond_invarscond_in_avalsa1a2v1v2outvarrQ   body_invarsvo1vo2rP   rR   r8   
new_invarsnew_outvarss                        r    pattern_match_while_to_fori_loopro   u   s     =<<j.5~FF.![//;///-!@@!&"bX L#)SY!777KKX M#)SY!777LLrr?&"b#A&&		ci	'	'	'	'		1$$BBa #]chBB[VHBBZB8BBj.5~FF.![rr?&"b%bqb)(#ss]]66**/00 	9 	9fa
}	!!	A"		cjmX%566 	Z]!##{1~$$ie88

%|M\M"l<  |L1$&&' *
 emABB'((+
--:jyj!EJy1}$??   %
 
r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r#   r   r#   r   r#   )r'   r(   r   r(   )r2   r   r   r   )r8   r9   r:   r   r;   r   r   r<   )
rU   r9   rV   r   rW   r9   rX   r   r   rY   )__doc__
__future__r   typingr   jaxr   jax._srcr   rJ   jax._src.utilr   	jax.numpynumpyrC   r*   r   r   r1   r7   rT   ro   r   r   r   <module>rx      s      " " " " " "       



       % % % % % % $ $ $ $ $ $             
   
 
   
 
   
 
   
      4 4 4 4' ' ' 'T@ @ @ @ @ @r   