
    uki=#                       U d dl 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  e
ded	e	f   
      Zg Zded<   d'dZ ee        eej*                         d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d/dZd0dZ  G d de!      Z"dZ# G d de!      Z$de$_%        d1dZ&d1d Z'd2d!Z(dd"d#	 	 	 	 	 	 	 d3d$Z)	 d d%lm*Z* e*jV                  Z,y# e-$ r dZ*d& Z,Y yw xY w)4    )annotations)CallableN)AnyTypeVarcast)config)util)_jaxC.)boundz	list[str]_exclude_pathsc                z    t         j                  |        t        t        d      rt        j                  |        y y )Nadd_exclude_path)r   appendhasattrr
   r   )paths    R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/traceback_util.pyregister_exclusionr   !   s.    T%&$ '    zThe stack trace below excludes JAX-internal frames.
The preceding is the original exception that occurred, unmodified.

--------------------c                N   t         j                  j                  |       } t         j                  j                  |      }	 t         j                  j                  | |g      }	 ||k(  xs  t         j                  j                  ||      S # t        $ r Y yw xY w# t        $ r Y yw xY wNF)osr   abspath
commonpath
ValueErrorsamefileOSError)r   path_prefixcommons      r   _path_starts_withr    /   s    		$,+WW{ 34F[ IBGG$4$4V[$II 
  	 
 s$    !B	 "&B 		BB	B$#B$c                @    t        | j                  j                        S N)include_filenamef_codeco_filenamefs    r   include_framer(   ?   s    	!((..	//r   c                6     t         fdt        D               S )Nc              3  6   K   | ]  }t        |        y wr"   )r    ).0r   filenames     r   	<genexpr>z#include_filename.<locals>.<genexpr>C   s     Nt"8T2Ns   )anyr   )r,   s   `r   r#   r#   B   s    N~NN	NNr   c                2    d| j                   j                  v S )Nzimportlib._bootstrap)r$   r%   r&   s    r   _ignore_known_hidden_framer0   H   s    	188#7#7	77r   c                    t        j                  |       D ],  \  }}t        |      rt        |      rd|j                  d<   . y )NT__tracebackhide__)	tracebackwalk_tbr(   _is_reraiser_framef_locals)tbr'   _linenos      r   #_add_tracebackhide_to_hidden_framesr9   K   s?    %%b) -ja$6q$9(,ajj$%-r   c                    d }t        t        j                  |             }t        |      D ]3  \  }}t	        |      st        j                  |||j                  |      }5 |S r"   )listr3   r4   reversedr(   typesTracebackTypef_lasti)r7   outframesr'   linenos        r   filter_tracebackrC   P   s]    #	!!"%&&F# ;iaQQ		6:c; 
*r   c                B   | }d}t        j                  | j                        D ]x  \  }}t        |      r|r|j                  j
                  dk7  r |S t        |      r"t        j                  |||j                  |      }|j                  j
                  dk(  swd}z |S )NFz<module>T)
r3   
walk_stacktb_framer0   r$   co_namer(   r=   r>   r?   )r7   r@   reached_module_levelr'   rB   s        r   _add_call_stack_framesrI   Y   s     	#''4 "ia!!$ 0 0J >
 
*	 QQ		6:cxx:%!" 
*r   c                    t        | t        j                        r| j                  | j                  }}n,| j
                  j                  | j
                  j                  }}|t        k(  xr |dk(  S )Nreraise_with_filtered_traceback)	
isinstancer3   FrameSummaryr,   namer$   r%   rG   __file__)r'   r,   rN   s      r   r5   r5   q   sV    9))*ZZdHXX))188+;+;dH	X		K$*K"KKr   c                    | j                   yt        j                  | j                   j                        }t	        d |d d D              S )NFc              3  2   K   | ]  }t        |        y wr"   )r5   )r+   r'   s     r   r-   z%_is_under_reraiser.<locals>.<genexpr>|   s     4q"4s   )__traceback__r3   extract_stackrF   r.   )er7   s     r   _is_under_reraiserrV   x   s@    __q778"	4BsG4	44r   c                z    dj                  t        j                  t        |       |             j	                         S )N )joinr3   format_exception_onlytypestrip)rU   s    r   rZ   rZ   ~   s+    	00a!<	=	C	C	EEr   c                      e Zd Zy)UnfilteredStackTraceN)__name__
__module____qualname__ r   r   r^   r^      s    r   r^   zFor simplicity, JAX has removed its internal frames from the traceback of the following exception. Set JAX_TRACEBACK_FILTERING=off to include these.c                      e Zd Zd Zy)SimplifiedTracebackc                    t         S r"   )_simplified_tb_msg)selfs    r   __str__zSimplifiedTraceback.__str__   s    r   N)r_   r`   ra   rh   rb   r   r   rd   rd      s    r   rd   z
jax.errorsc                 8    	 t                y# t        $ r Y yw xY w)z6Returns true if we appear to be in an IPython session.TF)get_ipython	NameErrorrb   r   r   _running_under_ipythonrl      s!    M	 s   
 	c                 .    ddl } | j                  dd dk\  S )z?Returns true if the IPython version supports __tracebackhide__.r   N   )      )IPythonversion_info)rq   s    r   _ipython_supports_tracebackhiders      s    			bq	!W	,,r   c                 |    t         j                  j                  } | | dk(  rt               rt	               rd} | S d} | S )Nautotracebackhidequiet_remove_frames)r   traceback_filteringvaluerl   rs   )modes    r   _filtering_moder{      sB    		#	#	)	)$	\TV^ %D%Fd 
+ #d	+r   F)repro_api_namerepro_user_funcc                    t        j                          fd       }|s|rt        rt        j                  |||      }t	        t
        |      S )aO  Wraps ``fun`` to form a boundary for filtering exception tracebacks.

  When an exception occurs below ``fun``, this appends to it a custom
  ``__cause__`` that carries a filtered traceback. The traceback imitates the
  stack trace of the original exception, but with JAX-internal frames removed.

  This boundary annotation works in composition with itself. The topmost frame
  corresponding to an :func:`~api_boundary` is the one below which stack traces
  are filtered. In other words, if ``api_boundary(f)`` calls
  ``api_boundary(g)``, directly or indirectly, the filtered stack trace provided
  is the same as if ``api_boundary(f)`` were to simply call ``g`` instead.

  This annotation is primarily useful in wrapping functions output by JAX's
  transformations. For example, consider ``g = jax.jit(f)``. When ``g`` is
  called, JAX's JIT compilation machinery is invoked, which in turn calls ``f``
  in order to trace and translate it. If the function ``f`` raises an exception,
  the stack unwinds through JAX's JIT internals up to the original call site of
  ``g``. Because the function returned by :func:`~jax.jit` is annotated as an
  :func:`~api_boundary`, such an exception is accompanied by an additional
  traceback that excludes the frames specific to JAX's implementation.

  For the "repro" kwargs, see the comments for `repro.boundary`.
  c                 h   d}	  | i |S # t         $ r}t               }t        |      s|dk(  r |dk(  rt        |j                          |j                  }	 |j                  t        |             |dk(  r|j                  dt        z           |dk(  r<t        |      }| dt         }t        |      }|j                  t        |             nt        d| d	      |j                  |_        |j                  |_        |j                   |_        ||_        d |_        ~ # ~~w xY wd }~ww xY w)
NToffrv   rw   z--------------------
remove_framesz

zJAX_TRACEBACK_FILTERING=z is not a valid value.)	Exceptionr{   rV   r9   rS   with_tracebackrC   add_noterf   rZ   _jax_message_appendr^   rI   r   	__cause____context____suppress_context__)	argskwargsr2   rU   rz   r7   msg	jax_errorfuns	           r   rK   z5api_boundary.<locals>.reraise_with_filtered_traceback   s;   $!&!! d	A	$%-		 +AOO<??b	)"-.((
**-0BB
C 	 _$'*CE123C,S1I$$%;B%?@7v=STUU !)
"#--)
+,+A+A)
(!!+!-";s(    
D1AD,CD%%D))D,,D1)api_nameis_user)	functoolswrapsreproboundaryr   r   )r   r|   r}   rK   s   `   r   api_boundaryr      sQ    8 ??3! !D U&+nn'.'!# 
a0	11r   )r   c                      yr   rb   rb   r   r   repro_is_enabledr      s    r   )r   str)r   r   r   r   returnbool)r'   ztypes.FrameTyper   r   )r,   r   r   r   )r7   types.TracebackType)r7   r   r   ztypes.TracebackType | None)r7   r   r   r   )r'   z(traceback.FrameSummary | types.FrameTyper   r   )rU   BaseExceptionr   r   )rU   r   r   r   )r   r   )r   r   )r   r   r|   z
str | Noner}   r   r   r   ).
__future__r   collections.abcr   r   r   r3   r=   typingr   r   r   jax._srcr   r	   jax._src.libr
   r   r   __annotations__r   rO   r   r    r(   r#   r0   r9   rC   rI   r5   rV   rZ   r   r^   rf   rd   r`   rl   rs   r{   r   r   
is_enabledr   ImportErrorrb   r   r   <module>r      s>   # $  	   % %    CxS)*	   8  4== ! 
 0O8-
0L5F ,9 +F )  ".  -
 "&!C2	
C2C2 C2 '(C2J'%% '
%&'s   C& &
C32C3