
     Ti                        S r SSKJr  S/rSSKrSSKr\R                  " \5      r	SS jr
 " S S\R                  R                  5      rg)z@Identity elimination pass for removing redundant Identity nodes.    )annotationsIdentityEliminationPassNc           
        S nU c  U$ Uc  U $ [        U 5      [        U5      :w  a$  [        S[        U 5       S[        U5       S35      e[        R                  " [	        X5       VVs/ s H  u  p4U" X45      PM     snn5      $ s  snnf )Nc                    X:X  a  U $ [        U [        R                  5      (       d  U $ [        U[        R                  5      (       d  U$ U R                  c  U$ U $ )N)
isinstanceirSymbolicDimvalue)dim1dim2s     i/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/passes/common/identity_elimination.py
merge_dims!_merge_shapes.<locals>.merge_dims   sL    <K$//K$//K::K    z$Shapes must have the same rank, got z and .)len
ValueErrorr   Shapezip)shape1shape2r   r   r   s        r   _merge_shapesr      s    	 ~~
6{c&k!23v;-uS[MQRS
 	
 88c&>QR>Q
Z+>QRSSRs   (B
c                  ,    \ rS rSrSrSS jrSS jrSrg)	r   )   a  Pass for eliminating redundant Identity nodes.

This pass removes Identity nodes according to the following rules:

1. For any node of the form `y = Identity(x)`, where `y` is not an output
   of any graph, replace all uses of `y` with a use of `x`, and remove the node.
2. If `y` is an output of a graph, and `x` is not an input of any graph,
   we can still do the elimination, but the value `x` should be renamed to be `y`.
3. If `y` is a graph-output and `x` is a graph-input, we cannot eliminate
   the node. It should be retained.
c                   Sn[         R                  R                  UR                  5       H  nU R	                  U5      (       d  M  SnM     UR
                  R                  5        HC  n[         R                  R                  U5       H  nU R	                  U5      (       d  M  SnM     ME     U(       a  [        R                  S5        [         R                  R                  XS9$ )z3Main entry point for the identity elimination pass.FTz,Identity elimination pass modified the model)modified)r   	traversalRecursiveGraphIteratorgraph_try_eliminate_identity_node	functionsvaluesloggerinfopasses
PassResult)selfmodelr   nodefunctions        r   callIdentityEliminationPass.call6   s     LL77DD0066 E
 ..0H;;HE44T::#H F 1
 KKFGyy##E#==r   c                   UR                   S:w  d  UR                  S:w  a  g[        UR                  5      S:w  d  [        UR                  5      S:w  a  gUR                  S   nUR                  S   nUc  gUR
                  nUc   S5       eUR                  5       nUR                  5       nU(       a  U(       a  g[        UR                  UR                  5      Ul	        UR                  c  UR                  Ul
        [        R                  R                  X2SS9  U(       a  UR                  Ul        UR                  USS	9  [         R#                  S
U5        g)zBTry to eliminate a single identity node. Returns True if modified.Identity F   r   zNode must be in a graphT)replace_graph_outputs)safezEliminated identity node: %s)op_typedomainr   inputsoutputsr   is_graph_outputis_graph_inputr   shapetyper   conveniencereplace_all_uses_withnameremover#   debug)r'   r)   input_valueoutput_value
graph_likeoutput_is_graph_outputinput_is_graph_inputs          r   r    4IdentityEliminationPass._try_eliminate_identity_nodeJ   s8   <<:%):t{{q C$5$:kk!n||A ZZ
%@'@@%!-!=!=!?*99; "&: *+*;*;\=O=OP#+00K 	,,T 	- 	

 "+00K 	$T*3T:r    N)r(   zir.Modelreturnzir.passes.PassResult)r)   zir.NoderG   bool)__name__
__module____qualname____firstlineno____doc__r+   r    __static_attributes__rF   r   r   r   r   )   s    
>(.r   )r   ir.Shape | Noner   rO   rG   rO   )rM   
__future__r   __all__loggingonnx_irr   	getLoggerrI   r#   r   r%   InPlacePassr   rF   r   r   <module>rV      sP    G "   			8	$T.Obii33 Or   