
     Ti                     <   S SK Jr  / SQrS SKrS SKrS SKr\R                  " \5      r	        SS jr
SS jrSS jr " S S\R                  R                  5      r " S	 S
\R                  R                  5      r " S S\R                  R                  5      rg)    )annotations)RemoveUnusedNodesPassRemoveUnusedFunctionsPassRemoveUnusedOpsetsPassNc                H  ^ ^  T R                   S;  a  g [        R                  R                  T R                  UT R                   S9nT R                  S:X  a  SUU 4S jjnU" S5      (       d  U" S	5      (       a  g [        T R                  5      S:  a  S
T R                  S   l
        [        T R                  5      S	:  a  S
T R                  S	   l
        T R                  R                  SS 5        g / nUR                   H  nUR                  [        R                  R                  R                  R                   :X  a    g UR#                  UR                  [        R                  R                  R                  R$                  :H  5        M     [        U Vs/ s H  ofS:H  PM	     sn5      S:X  a  g ['        T R                  5       H4  u  pxUT;  d  M  UR)                  5       (       a  M$  XW   SL d  M-  S
Ul
        M6     [        T R                  5      n	[+        [-        [        T R                  5      5      5       H(  nT R                  U   R                  (       d  U	S-  n	M(    O   T R/                  U	5        g ! [
         a    [        R                  ST SS9   g f = fs  snf )N>    onnx.ai)domainz=Failed to get schema for %s, skipping optional output removalT)
stack_infoBatchNormalizationc                   > U [        TR                  5      :  a4  TR                  U    nUT;   =(       d    [        UR                  5       5      $ g)NF)lenoutputsbooluses)ivalgraph_outputsnodes     c/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/passes/common/unused_removal.pyis_used_output7_remove_unused_optional_outputs.<locals>.is_used_output&   s?    3t||$$ll1om+?tCHHJ/??          r   training_moder   )r   intreturnr   )r
   onnxdefs
get_schemaop_type	Exceptionloggerinfor   r   name
attributespopoptionOpSchemaFormalParameterOptionVariadicappendOptional	enumerater   reversedrangeresize_outputs)
r   r   onnx_opset_version	op_schemar   optional_infoor   outnew_output_counts
   ``        r   _remove_unused_optional_outputsr9      s!   
;;o-II((7IRVR]R](^	 ||++	 	 !q 1 1t||q #%DLLO t||q #%DLLO OT2M88tyy))??HHHQXX););)Q)Q)Z)ZZ[	  M*MqFM*+q0DLL)m#SXXZZ]=MQU=UCH *
 4<<(eC-./||A##!	 0
 	()_  K 	 	

 	B +s   I: 3I: J:JJc                    [        U R                  5      n[        [        [        U R                  5      5      5       H  nU R                  U   c  US-  nM    O   U R	                  U5        g )Nr   )r   inputsr0   r1   resize_inputs)r   new_input_countr   s      r   _remove_trailing_empty_inputsr>   M   sX    $++&OeC,-.;;q>!q O	 /
 	'r   c                   [        U R                  5      nU R                  R                  SS 5      nSn[	        U 5       GH%  nSnUR                   H!  nXa;   d  UR                  5       (       d  M  Sn  O   U(       a  U R                  USS9  US-  nMU  [        U5        Ub  [        XAU5        UR                  R                  5        H  nUR                  [        R                  R                  :X  a  U[        UR!                  5       5      -  nMI  UR                  [        R                  R"                  :X  d  Ms  UR%                  5        H  nU[        U5      -  nM     M     GM(     U$ )Nr   r   TF)safer   )	frozensetr   opset_importsgetr0   r   remover>   r9   r'   valuestypeirAttributeTypeGRAPH"_remove_unused_nodes_in_graph_likeas_graphGRAPHS	as_graphs)	function_or_graphr   r3   countr   	removableoutputattrgraphs	            r   rJ   rJ   X   s+   /778M*88<<RFE*+	llF&&++--!	 # $$T$5QJE)$/!-/EWX..099 0 0 6 66?PPEYY""2"2"9"99!%!1!CE!JJ "2	 1 ,& Lr   c                  "    \ rS rSrSrSS jrSrg)r   r   zPass for removing unused nodes and initializers (dead code elimination).

This pass does not modify the model signature (inputs and outputs). It ensures
that unused nodes and initializers are removed while preserving the original
contract of the model.
c                   [        UR                  5      n[        UR                  R                  5      n[        UR                  R                  5      nUR                  R
                  n[        UR                  5       5       HH  nUR                  5       (       a  M  Xc;   a  M!  Xd;   a  M(  UR                  c   eXVR                  	 US-  nMJ     UR                  R                  5        H  nU[        U5      -  nM     U(       a  [        R                  SU5        [        R                  R                  U[!        U5      S9$ )Nr   zRemoved %s unused nodesmodified)rJ   rS   rA   r   r;   initializerslistrE   r   r&   	functionsr$   r%   rG   passes
PassResultr   )selfmodelrO   r   graph_inputsrY   initfunctions           r   callRemoveUnusedNodesPass.callz   s    25;;?!%++"5"56 !3!34{{//,,./DIIKK4#8D<Pyy,,, +
	 0
 ..0H7AAE 1KK159yy##EDK#@@r    Nr_   ir.Modelr   zir.passes.PassResult)__name__
__module____qualname____firstlineno____doc__rc   __static_attributes__re   r   r   r   r   r   s    Ar   r   c                  F   ^  \ rS rSrU 4S jrSS jrSS jrS	S jrSrU =r	$ )
r      c                0   > [         TU ]  5         S U l        g N)super__init___used)r^   	__class__s    r   rs   "RemoveUnusedFunctionsPass.__init__   s    8<
r   c                   [        5       U l        [        R                  R	                  UR
                  5       H  nU R                  X5        M     [        UR                  5      U R                  -
  nU(       d3  [        R                  S5        [        R                  R                  USS9$ U H  nUR                  U	 M     [        R                  S[        U5      5        [        R                  S[        UR                  5      5        [        R                  SU5        S U l        [        R                  R                  U[        U5      S9$ )NzNo unused functions to removeFrW   zRemoved %s unused functionszFunctions left: %szFunctions removed: %s)setrt   rG   	traversalRecursiveGraphIteratorrS   
_call_noder[   r$   r%   r\   r]   r   debugrZ   r   )r^   r_   r   unusedop_identifiers        r   rc   RemoveUnusedFunctionsPass.call   s    U
LL77DDOOE( E U__%

2KK7899'''>>#M. $ 	13v;?)4+@A,f5
yy##EDL#AAr   c                    U R                   c   eUR                  5       U R                   ;   a  g U R                   R                  UR                  5       5        [        R                  R                  U5       H  nU R                  X5        M     g rq   )rt   
identifieraddrG   ry   rz   r{   )r^   r_   rb   r   s       r   _call_function(RemoveUnusedFunctionsPass._call_function   si    zz%%% DJJ.

x**,-LL77ADOOE( Br   c                    UR                  5       nX1R                  ;  a  g U R                  XR                  U   5        g rq   )r~   r[   r   )r^   r_   r   r~   s       r   r{   $RemoveUnusedFunctionsPass._call_node   s4    **,/E??=#ABr   )rt   rf   )r_   rg   rb   zir.Functionr   None)r_   rg   r   ir.Noder   r   )
rh   ri   rj   rk   rs   rc   r   r{   rm   __classcell__ru   s   @r   r   r      s    =B*)C Cr   r   c                  T   ^  \ rS rSrSrSSU 4S jjjr      S	S jrS
S jrSrU =r	$ )r      a  Remove unused opset imports from the model and functions.

Attributes:
    process_functions: Whether to process functions in the model. If True, the pass will
        remove unused opset imports from functions as well. If False, only the main graph
        will be processed.
c                .   > [         TU ]  5         Xl        g rq   )rr   rs   process_functions)r^   r   ru   s     r   rs   RemoveUnusedOpsetsPass.__init__   s    !2r   c                    [         R                  R                  U5       H  nUR                  UR                  5        M      [        UR                  5      U-
  nU H  nUR                  U	 M     [        U5      $ rq   )rG   ry   rz   r   r
   rx   rB   r   )r^   
graph_likeused_domainsr   r}   r
   s         r   _process_graph_like*RemoveUnusedOpsetsPass._process_graph_like   sg     LL77
CDT[[) DZ--.=F((0 F|r   c                x   S1nUR                   R                  5        H  nUR                  UR                  5        M      U R	                  UR
                  US9nU R                  (       a4  UR                   R                  5        H  nX@R	                  US1S9-  nM     [        R                  R                  XS9$ )Nr   )r   rW   )
r[   rE   r   r
   r   rS   r   rG   r\   r]   )r^   r_   r   rb   rX   s        r   rc   RemoveUnusedOpsetsPass.call   s    t..0HX__- 1++EKKl+S!!!OO22444XRD4QQ 5 yy##E#==r   )r   )T)r   r   )r   zir.Graph | ir.Functionr   zset[str]r   r   rf   )
rh   ri   rj   rk   rl   rs   r   rc   rm   r   r   s   @r   r   r      s9    3 30@H	> >r   r   )r   r   r   zfrozenset[ir.Value]r3   r   r   r   )r   r   r   r   )rN   zir.Function | ir.Graphr   r   )
__future__r   __all__loggingr   onnx_irrG   	getLoggerrh   r$   r9   r>   rJ   r\   InPlacePassr   r   r   re   r   r   <module>r      s    #   			8	$6*
6*"56*KN6*	6*r(4ABII11 A2'C		 5 5 'CT">RYY22 ">r   