
     Ti!                        S SK Jr  S SKrS SKJrJr  S SKrS SKJ	r	J
r
  SS jrSSS jjrSS jrSS jrSSS	 jjrSSS
.         SS jjrSS jrSS jrSS jrSS jrSS jrg)    )annotationsN)CallableSequence)ir	optimizerc                    U (       aQ  U S   R                   nU(       a!  U H  nX ;   d  M
  UR                  5         M     gU  H  nUR                  5         M     gg)z>Display a list of nodes in the order they appear in the graph.r   N)graphdisplay)nodesr	   nodes      \/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxscript/rewriter/_ir_utils.pydisplay_nodesr      sI    a =LLN        c                  ^^^^ / mSUUUU4S jjm[        U [        R                  5      (       a
  T" U S5        OG[        U [        R                  5      (       a(  U R	                  5       b  T" U R	                  5       S5        [        T5        g)z[Display the (backward or forward) subgraph from a given value or node upto a certain depth.c                L  > U T;   a  g TR                  U 5        UT:  a  T(       aF  U R                   H5  nUc  M  UR                  5       c  M  T	" UR                  5       US-   5        M7     g U R                   H'  nUR	                  5        H  u  pET	" XAS-   5        M     M)     g g )N   )appendinputsproduceroutputsuses)
r   depthinpoutconsumer_backwarddepth_limitslicevisits
         r   r    display_slice.<locals>.visit    s    5=T;;;C3<<>+Ecllneai8 '  <<C'*xxzh	2 (2 ( r   r   N)r   zir.Node)
isinstancer   NodeValuer   r   )xr   r   r   r    s    ``@@r   display_slicer&      sb    E3 3 !RWWa	Arxx	 	 QZZ\%=ajjlA%r   c                n    U R                  5       nUb  [        R                  " U/5        U R                  $ N)r   r   basic_constant_propagationconst_value)valuer   s     r   get_const_valuer,   5   s0    >>D,,dV4r   c                j    U c  g[        U 5      nUb   UR                  5       $ g! [         a     gf = f)a  Convenience wrapper to get (optional) numpy value from an optional IR Value.

This is intended for use in optimizations/rewriting. Note that this does not
yet handle the distinction between inputs with default values (values that are
both graph inputs and graph initializers), which should not be treated as a
constant, and true constant values. The caller should make the distinction, as
a value does not contain enough information to determine this. (TODO)
N)r,   numpyFileNotFoundError)valr*   s     r   get_numpy_valuer1   <   sI     {!#&K	$$&&  ! 		s   % 
22c                   [        U 5      nUbq  UR                  S:X  aa  UR                  5       nUb%  [        U[        5      (       a  UR
                  U:X  a  U$ [        U[        5      (       a  UR
                  U;   a  U$ g)at  Returns element of a single element tensor constant value, and None otherwise.

If an int rank is specified, it checks that the value has the given rank.
If the rank is a sequence of ints, it checks that the value has one of the given ranks.

Thus, `rank=0` checks for a scalar, `rank=1` checks for a 1D tensor, and
`rank=(0,1)` checks for either a scalar or a 1D tensor.
Nr   )r1   sizeitemr"   intndimr   )r0   ranknp_valr+   s       r   get_singleton_valuer9   Q   sk     S!FfkkQ.Ljs339LLdH%%6;;$+>Lr   )rtolr7   c                   [        XS9nUc  g[        U5      (       a  U" U5      $ [        U[        5      (       a  X:H  $ Uc   e[        R
                  " XAUS9$ )z[Returns True if the value is a single element tensor with given value, and False otherwise.)r7   F)rel_tol)r9   callabler"   r5   mathisclose)r0   expectedr:   r7   scalars        r   is_singleton_valuerB   d   s_     !0F~(C  !!<<$77r   c                <   U c  g[        U R                  [        R                  5      (       d  g[	        U 5      nUc  gUR
                  [        U5      :w  d2  U R                  R                  [        R                  R                  :w  a  gUR                  5       nX1:H  $ )zUReturns True if the value is a 1d int64 tensor with given value, and False otherwise.F)r"   typer   TypeProtocolr1   r3   lendtypeDataTypeINT64tolist)r0   r@   r8   valuess       r   is_1d_valuerL   x   st    
{chh00S!F~s8}$#((..BKK<M<M*M]]_Fr   c                ^    U c  gU R                   nUSL=(       a    UR                  5       U:H  $ )zZReturns True if the value is statically known to have the given rank, and False otherwise.NFshaper7   )r+   r7   rO   s      r   has_rankrP      s.    }KKE9EJJLD$89r   c                    U c  gU R                   nUc  gUS:  a  XR                  5       -  nUS:  d  XR                  5       :  a  gX!   $ )zPReturns the value of the given dimension, or None if it is not statically known.Nr   rN   )r+   dimrO   s      r   get_dimrS      sM    }KKE}
Qwzz|
Qw#%:r   c                n    U b  Uc  gU R                  5       (       d  UR                  5       (       a  gX:H  $ )z.Check if two shapes are semantically the same.F)has_unknown_dim)shape1shape2s     r   
same_shaperX      s9    ~ 6#9#9#;#;r   c                v   [        U 5      [        U5      La  g[        U [        5      (       a  [        U[        5      (       a  X:H  $ [        U [        R                  5      (       a  [        U[        R                  5      (       d   eU R
                  b  UR
                  c  gU R
                  UR
                  :H  $ )z2Check if two dimensions are semantically the same.F)rD   r"   r5   r   SymbolicDimr+   )dim1dim2s     r   same_dimr]      s    Dzd#$D#!6!6|dBNN++
40P0PPPzzTZZ/::##r   )r   zSequence[ir.Node]returnNone)T   )r%   zir.Value | ir.Noder   boolr   r5   r^   r_   )r+   zir.Valuer^   zir.TensorProtocol | None)r0   ir.Value | Noner^   znp.ndarray | Noner(   )r0   rb   r7   int | Sequence[int] | None)
r0   rb   r@   zfloat | int | Callabler:   zfloat | Noner7   rc   r^   ra   )r0   rb   r@   z	list[int]r^   ra   )r+   rb   r7   r5   r^   ra   )r+   rb   rR   r5   r^   zir.SymbolicDim | int | None)rV   ir.Shape | NonerW   rd   r^   ra   )r[   ir.SymbolicDim | intr\   re   r^   ra   )
__future__r   r>   typingr   r   r.   np
onnxscriptr   r   r   r&   r,   r1   r9   rB   rL   rP   rS   rX   r]    r   r   <module>rk      s    #  %  $2*. '+8	8$8 	8
 %8 
8(:		$r   