
    x-j                    ,    d dl mZ  G d d          ZdS )    )annotationsc                  T    e Zd ZdZddZddZ	 dddZddZ	 dddZd dZ	dddZ
dS )!AOATracebackzf
    When error occurs, print the chain of "original aoa_statement -> ... -> current expression".
    returnNonec                h    g | _         g | _        d| _        d| _        d| _        i | _        i | _        d S )N )recordslast_error_chainlast_error_messagelast_error_stagelast_error_type
parent_mapchild_macro_map)selfs    p/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/distributed/flex_checkpoint/aoa/traceback.py__init__zAOATraceback.__init__   s>    #%+-')%'$&13/1    expressions	list[str]c                F    |D ]}| j                             |d           dS )zDRegister the original aoa_statements as the root nodes of the chain.N)r   
setdefault)r   r   exprs      r   register_rootszAOATraceback.register_roots    s6     	3 	3DO&&tT2222	3 	3r   Nparentstrchildren
macro_name
str | Nonec                N    |pd}|D ]}||k    r	|| j         |<   || j        |<   dS )z`Record the children expressions obtained by the parent expression, and mark the macro name used.ExpandedN)r   r   )r   r   r   r   macrochilds         r   record_childrenzAOATraceback.record_children%   sO     (j 	0 	0E%+DOE"*/D ''		0 	0r   r   c                    g }t                      }|}|N||vrJ|                    |           |                    |           | j                            |          }|||vJ|                                 |S )zRBuild the chain from the root to expr by tracing back from the current expression.)setappendaddr   getreverse)r   r   chainvisitedcurs        r   build_chainzAOATraceback.build_chain0   s    %%o#W"4"4LLKK/%%c**C o#W"4"4 	r   r	   error_messagestager+   
error_typec                    || _         || _        || _        |pd| _        | j                            d||| j        |d           dS )z'Record the error chain and information.r	   error)typer0   messager1   r+   N)r   r   r   r   r
   r'   )r   r/   r0   r+   r1   s        r   	add_errorzAOATraceback.add_error<   si     !&"/ %)/R("2 	
 	
 	
 	
 	
r   c                0   g }d}d| d}dt          |          z  }| j        r|                    |           d}|                    d           | j        d                             dd          }|                    d	|            t	          | j        d
d          d
          D ]v\  }}||z  }	|                    dd          }
| j                            || j        pd          }|                    |	 d|            |                    |	 d	|
            w| j        r| j	        pd}| j        rd| j         dnd}t          | j                  }||z  }	| j                            dd          }|                    |	 d           |                    |	 d| | d|            |                    |           n?|                    |           |                    d           |                    |           d
                    |          S )Nz AOA Traceback (related chain) z=====z    z| Origin AOA Statementr   
 z|->    )startr!   z| Errorz []r	   z| Errorz|-> (z) z(No trace records))lenr   r'   replace	enumerater   r)   r   r   r   join)r   linesheader_textheaderfooterindent_unitorigin_exprlevelr   indentsingle_line_exprr"   	err_title	stage_str	err_levelsingle_line_msgs                   r   format_tracebackzAOATraceback.format_tracebackR   st   7))))s6{{"  $	!LL    KLL1222/2::4EEKLL---...()>qrr)B!LLL @ @t$u,#'<<c#:#: ,00$/=:  11%11222>>,<>>????&  0;G	 ,1.1111 
   566	$y0"&"9"A"A$"L"L///000MMIMyMMOMM   LL    LL   LL-...LL   yyr   c                ~    |                                  }|r|                    |           d S t          |           d S N)rP   r3   print)r   loggertexts      r   rS   zAOATraceback.print   sB    $$&& 	LL$KKKKKr   )r   r   )r   r   r   r   rR   )r   r   r   r   r   r   r   r   )r   r   r   r   )r	   )
r/   r   r0   r   r+   r   r1   r   r   r   )r   r   )__name__
__module____qualname____doc__r   r   r$   r.   r6   rP   rS    r   r   r   r      s         2 2 2 23 3 3 3 JN	0 	0 	0 	0 	0
 
 
 
" 
 
 
 
 
,,  ,  ,  , \      r   r   N)
__future__r   r   rZ   r   r   <module>r\      sU    # " " " " "s s s s s s s s s sr   