
    bIgk                        d dl mZ d dlZd dlmZmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddZddZddZddZd	Zd
Z G d dej6                        Zedk(  r ej<                          yy)    )annotationsN)CallableSequence)FunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProtoValueInfoProtocheckercomposehelperparserversion_converterc                Z    t        j                  |       }t        j                  |       |S )zYParses a model from a string representation, including checking the model for correctness)r   parse_modelr   check_model)m_defms     S/var/www/html/answerous/venv/lib/python3.12/site-packages/onnx/test/compose_test.py_load_modelr      s%    5!AH    c                ,    t        |      dkD  r| |z   S |S )z Prefixes a string (if not empty)r   )len)prefixss     r   	_prefixedr   "   s    Q!6A:**r   c                
   t        t        | j                  j                  j                  j
                              D cg c]9  }| j                  j                  j                  j
                  |   j                  ; c}S c c}w )zPReturns a list of integers representing the shape of the provided ValueInfoProto)ranger   typetensor_typeshapedim	dim_value)
value_infods     r   
_get_shaper(   '   se     s:??66<<@@AB 	##))--a0::  s   >B c                   ddg}g d}g d}t        j                  | dz   t        j                  t	        |      gt        j                  |      j                  t
        j                        d      }t        j                  | dz   t        j                  t	        |      gt        j                  |      j                  t
        j                        d      }t        j                  |||      S )N   )   r*      )g333333?g?g?_valuesF)name	data_typedimsvalsraw_idx)r   make_tensorr   FLOATr   nparrayastypefloat32INT64int64make_sparse_tensor)r.   dense_shapelinear_indicessparse_valuesvalues_tensorindices_tensors         r   _make_sparse_tensorrB   /   s    a&KN#M&&I##- !XXm$++BJJ7M ''F]##.!"XXn%,,RXX6N $$]NKPPr   a)  
    <
        ir_version: 7,
        opset_import: [ "": 10, "com.microsoft": 1]
    >
    agraph (float[N, M] A0, float[N, M] A1, float[N, M] _A) => (float[N, M] B00, float[N, M] B10, float[N, M] B20)
    {
        B00 = Add(A0, A1)
        B10 = Sub(A0, A1)
        B20 = Mul(A0, A1)
    }
    a
  
    <
        ir_version: 7,
        opset_import: [ "": 10, "com.microsoft": 1]
    >
    agraph (float[N, M] B01, float[N, M] B11, float[N, M] B21) => (float[N, M] D0)
    {
        C0 = Add(B01, B11)
        C1 = Sub(B11, B21)
        D0 = Mul(C0, C1)
    }
    c                     e Zd Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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d!dZd!dZd!dZ	 	 	 	 	 	 	 d"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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	 	 	 	 	 	 	 	 	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZd!dZd!dZd!dZd!dZd!dZd!dZd!dZy)&TestComposeFunctionsNc	           	        t        |      t        |      }
}	t        j                  |	j                  |
j                  |||||      }t	        j
                  |        ||	j                  |
j                  |       t        j                  |	|
|||||      }t	        j                  |        ||	j                  |
j                  |j                         y )N)io_mapinputsoutputsprefix1prefix2)r   r   merge_graphsgraphr   check_graphmerge_modelsr   )selfm1defm2defrF   check_expectationsrG   rH   rI   rJ   m1m2g3m3s                r   _test_merge_modelsz'TestComposeFunctions._test_merge_modelsa   s     U#[%7B!!HHHH
 	B288RXXr2!!
 	B288RXXrxx8r   c                R     d fd}g d} j                  t        t        ||       y)zTests a simple scenario where two models without overlapping names are merged by
        connecting all the outputs in the first models to all the inputs in the second model
        c                   j                  |j                  | j                         j                  |j                  |j                         j                  g d|j                  D cg c]  }|j                   c}       y c c}w )NAddSubMulr[   r\   r]   )assertEqualinputoutputnodeop_type)g1g2rU   itemrO   s       r   rR   zXTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectations   s]    RXXrxx0RYY		2:*,''2$22s   *B
B00B01B10B11B20B21Nrc   r   rd   r   rU   r   returnNonerW   M1_DEFM2_DEFrO   rR   rF   s   `  r   'test_case_connect_all_no_name_collisionz<TestComposeFunctions.test_case_connect_all_no_name_collision   s%    
	 B8JKr   c                R     d fd}g d} j                  t        t        ||       y)zTests a scenario where we merge two models by connecting a single output in the first model
        to all the inputs in the second
        c                @   ~j                  |j                  | j                         j                  g d|j                  D cg c]  }|j                   c}       j                  g d|j                  D cg c]  }|j
                   c}       y c c}w c c}w )N)rk   rn   D0rZ   r^   r_   r`   r.   ra   rb   rc   rd   rU   elemre   rO   s        r   rR   zTTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations   st    RXXrxx01"))3T$DII3TU:*,''2$2 4U 3s   B
;B
rg   )rh   rl   )rh   ro   Nrp   rs   rv   s   `  r   #test_case_connect_same_output_twicez8TestComposeFunctions.test_case_connect_same_output_twice   s%    
	 B8JKr   c                \     d fd}g d}dg} j                  t        t        |||       y)zTests a scenario where we merge two models by connecting a single output in the first model
        to all the inputs in the second, while dropping the rest of the outputs in the first model
        c                >   ~j                  |j                  | j                         j                  dg|j                  D cg c]  }|j                   c}       j                  g d|j                  D cg c]  }|j
                   c}       y c c}w c c}w )Nrz   )r[   r[   r\   r]   r{   r|   s        r   rR   z[TestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations   sr    RXXrxx0dVBII%FDdii%FG,.Pt||.P &G.Ps   B
:B
r~   rz   )rH   Nrp   rs   )rO   rR   rF   rH   s   `   r   *test_case_connect_same_output_drop_outputsz?TestComposeFunctions.test_case_connect_same_output_drop_outputs   s6    
	 B&FF$6 	  	
r   c                H     d}d}dg}d fd} j                  ||||       y)zzTests a scenario where we merge two models, where the inputs/outputs connected
        are named exactly the same
        z
            <
                ir_version: 7,
                opset_import: [ "": 10]
            >
            agraph (float[N, M] A) => (float[N, M] B)
            {
                B = Add(A, A)
            }
            z
            <
                ir_version: 7,
                opset_import: [ "": 10]
            >
            agraph (float[N, M] B) => (float[N, M] C)
            {
                C = Add(B, B)
            }
            )Br   c                    ~ ~j                  dg|j                  D cg c]  }|j                   c}       j                  dg|j                  D cg c]  }|j                   c}       y c c}w c c}w )NACr^   r_   r.   r`   rc   rd   rU   r}   rO   s       r   rR   zYTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectations   s[    BcU288$D4TYY$DEcU299$E4TYY$EF %E$Es   A/
A4
Nrp   rW   )rO   m1_defm2_defrF   rR   s   `    r   (test_case_connect_same_input_output_namez=TestComposeFunctions.test_case_connect_same_input_output_name   s7    		 	G 	8JKr   c                Z     d}d}dg}d	 fd}dg}dg} j                  ||||||       y)
zTTests a scenario where we merge two models, not including some of the inputs/outputsa  
            <
                ir_version: 7,
                opset_import: [ "": 10]
            >
            agraph (float[N] A0, float[N] B0) => (float[N] A1, float[N] B1)
            {
                A1 = Add(A0, A0)
                B1 = Sub(B0, B0)
            }
            a  
            <
                ir_version: 7,
                opset_import: [ "": 10]
            >
            agraph (float[N] A2, float[N] B2) => (float[N] A3, float[N] B3)
            {
                A3 = Add(A2, A2)
                B3 = Sub(B2, B2)
            }
            )A1B2c                h   ~ ~j                  dg|j                  D cg c]  }|j                   c}       j                  dg|j                  D cg c]  }|j                   c}       j                  ddg|j                  D cg c]  }|j
                   c}       y c c}w c c}w c c}w )NA0B3r[   r\   r^   r_   r.   r`   ra   rb   r   s       r   rR   zNTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations   s    BdVBHH%EDdii%EFdVBII%FDdii%FGeU^rww-Otdll-OP &F%F-Os   B%
B*

B/
r   r   rG   rH   Nrp   r   )rO   r   r   rF   rR   rG   rH   s   `      r   test_case_drop_inputs_outputsz2TestComposeFunctions.test_case_drop_inputs_outputs   sQ    

 	Q &FF$6vw 	  	
r   c                J     d}dg}d fd} j                  ||||dd       y)	zTests a scenario where we merge two models that have name collisions, but they
        are avoided by prefixing the models model.
        z
            <
                ir_version: 7,
                opset_import: [ "": 10]
            >
            agraph (float[N] A, float[N] B) => (float[N] C)
            {
                C = Add(A, B)
            }
            )r   r   c                j   ~ ~j                  g d|j                  D cg c]  }|j                   c}       j                  dg|j                  D cg c]  }|j                   c}       j                  ddg|j                  D cg c]  }|j
                   c}       y c c}w c c}w c c}w )N)zm1/Azm1/Bzm2/Bzm2/Cr[   r   r   s       r   rR   zPTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations  s    B5bhh7Wd		7WXfXbii'Hd		'HIeU^rww-Otdll-OP 8X'H-Os   B&
B+
B0
m1/m2/)rI   rJ   Nrp   r   )rO   r   rF   rR   s   `   r   test_case_name_collision_prefixz4TestComposeFunctions.test_case_name_collision_prefix  s<    	 	Q 	FF$6u 	  	
r   c                R     d fd}ddg} j                  t        t        ||       y)zTests a scenario where two models without overlapping names are merged by
        connecting some outputs from the first model to some inputs in the second.
        The remaining inputs/outputs should be present in the combined model
        c                    ~ ~j                  g d|j                  D cg c]  }|j                   c}       j                  ddg|j                  D cg c]  }|j                   c}       y c c}w c c}w )N)r   r   _Aro   rn   rz   r   )rc   rd   g4r}   rO   s       r   rR   z^TestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectations"  sb    B )"((+K$DII+K eT]299,M4TYY,MN ,L,Ms   A1
A6
rg   rj   N)rc   r   rd   r   r   r   rq   rr   rs   rv   s   `  r   -test_case_connect_partially_no_name_collisionzBTestComposeFunctions.test_case_connect_partially_no_name_collision  s*    	O !.18JKr   c                &   t        t              }t        j                  |ddd       t        t              }t        j                  |ddd       dg}t        j                  |||      }t        |j                        d	k(  sJ t        j                  |ddd
       t        j                  |||      }t        |j                        dk(  sJ t        j                  |ddd
       | j                  t        t
        j                  |||       y )Nv1v2)p1p2v3v4)p3p4rg   rF      )r   r   r*   v5)r   rt   r   set_model_propsru   r   rN   r   metadata_propsassertRaises
ValueErrorrO   rS   rT   rF   rV   s        r   %test_merge_models_with_metadata_propsz:TestComposeFunctions.test_merge_models_with_metadata_props/  s     r$d#;< r$d#;< !!!"b82$$%*** 	r$d#;<!!"b82$$%*** 	r$d#;<*g&:&:B6Rr   c                    t        t              t        t              }}| j                  t        t
        j                  ||g d       | j                  t        t
        j                  ||g d       y)z_Tests that providing a non existing output/input name in the io_map argument produces an error.))wrong_outnameri   rj   rm   r   ))rh   wrong_inputrj   rm   N)r   rt   ru   r   r   r   rN   rO   rS   rT   s      r   "test_error_wrong_input_output_namez7TestComposeFunctions.test_error_wrong_input_output_nameC  sg    V$k&&9B  M 	 	
 	  K 	 	
r   c                    t        d      }t        d      }| j                  t        t        j                  ||dg       y )Nz
    <
        ir_version: 7,
        opset_import: [ "": 13]
    >
    agraph (float[N, M] X0) => (float[N, M] Y0)
    {
        Y0 = Add(X0, X0)
    }
    z
    <
        ir_version: 6,
        opset_import: [ "": 13]
    >
    agraph (float[N, M] X1) => (float[N, M] Y1)
    {
        Y1 = Add(X1, X1)
    }
    )Y0X1r   )r   r   r   r   rN   r   s      r   test_error_ir_version_mismatchz3TestComposeFunctions.test_error_ir_version_mismatchX  sJ    	
 	
 	,,b"l^ 	 	
r   c                   t        t              t        t              }}t        j                  |j
                  dt        j                  dd      g      }t        j                  |j
                  dt        j                  dd      g      }g d}| j                  t        t        j                  |||       t        j                  |d      }t        j                  |||      }t        j                  |       y)	zSTests that providing models with different operator set imported produces an error.test 
   producer_nameopset_imports   rf   r   N)r   rt   ru   r   
make_modelrL   make_opsetidr   r   r   rN   r   convert_versionr   r   r   s        r    test_error_opset_import_mismatchz5TestComposeFunctions.test_error_opset_import_mismatchw  s    V$k&&9BHHF6;N;NrSU;V:W
 HHF6;N;NrSU;V:W
 B*g&:&:BFK ..r26!!"b8Br   c                   t        t              }d}	|r:t               }
|
j                  |       t	        j
                  |
|	||||||d	       nt	        j
                  ||	||||||      }
|j                  }|
j                  }|s	|s|s|s|rHi }|rQ|j                  D ]B  }|j                  D ]  }t        |	|      ||<    |j                  D ]  }t        |	|      ||<    D |r4|j                  D ]%  }t        |	|j                        ||j                  <   ' |r4|j                  D ]%  }t        |	|j                        ||j                  <   ' |r|j                  D ]%  }t        |	|j                        ||j                  <   ' |j                  D ]p  }t        |	|j                  j                        ||j                  j                  <   t        |	|j                  j                        ||j                  j                  <   r |r4|j                  D ]%  }t        |	|j                        ||j                  <   ' t!        |j                  |j                        D ]  \  }}t!        |j                  |j                        D ]'  \  }}| j#                  |j%                  ||      |       ) t!        |j                  |j                        D ]'  \  }}| j#                  |j%                  ||      |       )  t!        |j                  |j                        D ]E  \  }}| j#                  |j%                  |j                  |j                        |j                         G t!        |j                  |j                        D ]E  \  }}| j#                  |j%                  |j                  |j                        |j                         G t!        |j                  |j                        D ]E  \  }}| j#                  |j%                  |j                  |j                        |j                         G t!        |j                  |j                        D ]  \  }}| j#                  |j%                  |j                  j                  |j                  j                        |j                  j                         | j#                  |j%                  |j                  j                  |j                  j                        |j                  j                          t!        |j&                  |j&                        D ]E  \  } }!| j#                  |j%                  |!j                  |!j                        | j                         G |rYt!        |j                  |j                        D ]5  \  }}| j#                  t        |	|j                        |j                         7 y y y )Nzpre/T)rename_nodesrename_edgesrename_inputsrename_outputsrename_initializersrename_value_infosinplace)r   r   r   r   r   r   )r   rt   r   CopyFromr   
add_prefixrL   ra   r_   r   r`   r.   initializersparse_initializervaluesindiceszipr^   getr&   )"rO   r   r   r   r   r   r   r   rS   r   rT   g_ing_outname_mappingner}   initsparse_initr&   n1n0e1e0i1i0o1o0init1init0sparse_init1sparse_init0vi1vi0s"                                     r   _test_add_prefixz%TestComposeFunctions._test_add_prefix  s>     BKKO))+-$7#5
 ##))+-$7#5	B xx "!L  ?AWW ?*3FA*>Q?XX ?*3FA*>Q??
  JJ KD.7		.JL+K KK KD.7		.JL+K # ,, KD.7		.JL+K#'#:#: K<E 2 2 7 7=L!3!3!8!89 >G 3 3 8 8>L!4!4!9!9:	 ""&++ WJ4=fjoo4VL1W ejj$))4 CB!"((BHH5 CFB$$\%5%5b"%=rBC!"))RYY7 CFB$$\%5%5b"%=rBCC
 ekk4::6 NB  !1!1"''277!CRWWMNellDKK8 NB  !1!1"''277!CRWWMN !$E$5$5t7G7G H Wu  !1!1%**ejj!I5::VW /2(($*A*A/ *l    $$$++00,2E2E2J2J !'',,	    $$$,,11<3G3G3L3L !((--	    0 0$//B QS  !1!1#((CHH!EsxxPQ !%**dii8 JFB$$Yvrww%?IJ C "r   c                (    | j                  d       y)zTests renaming nodes onlyT)r   Nr   rO   s    r   test_add_prefix_nodesz*TestComposeFunctions.test_add_prefix_nodes      40r   c                (    | j                  d       y)z]Tests prefixing nodes edges. This will also rename inputs/outputs, since the names are sharedT)r   Nr   r   s    r   test_add_prefix_edgesz*TestComposeFunctions.test_add_prefix_edges  r   r   c                (    | j                  d       y)zPTests prefixing graph inputs only. Relevant node edges should be renamed as wellT)r   Nr   r   s    r   test_add_prefix_inputsz+TestComposeFunctions.test_add_prefix_inputs  s    D1r   c                (    | j                  d       y)zQTests prefixing graph outputs only. Relevant node edges should be renamed as wellT)r   Nr   r   s    r   test_add_prefix_outputsz,TestComposeFunctions.test_add_prefix_outputs
  s    T2r   c                   t        j                  dt        j                  dg      }t        j                  dt        j                  ddg      }t        j                  dt        j                  ddg      }t        j                  dt        j                  ddg      }t        j                  dt        j                  ddg      }t        j
                  dddgd	g
      }t        j
                  dd	dgdg
      }t        j
                  dd	dgdg
      }t        j
                  ddgdgt        j                  |gdg |g      t        j                  |gdg |g            }	t        j                  ||	gd||||g|g      }
d}t        j                  |
|      }t        j                  |       t        |j                  |
j                        D ]/  \  }}| j                  t        ||j                        |j                         t        |j                   |j                         D ]  \  }}|j"                  st        |j"                  j                  |j"                  j                        D ]  \  }}t        |j$                  |j$                        D ]!  \  }}| j                  t        ||      |       # t        |j&                  |j&                        D ]!  \  }}| j                  t        ||      |       #   2 y)zQTests prefixing attribute's subgraph. Relevant subgraph should be renamed as wellr      XNYZOutr]   XYr   r[   r\   Ifthen)nodesr.   rG   rH   else)rG   rH   then_branchelse_branchrL   zprefix.)r   make_tensor_value_infor   BOOLr5   	make_node
make_graphr   add_prefix_graphr   rM   r   ra   r^   r   r.   	attributegr_   r`   )rO   r   r   r   r   r   r   addsubcondrL   r   prefixed_graphr   r   
attribute1
attribute0subgraph_n1subgraph_n0input_n1input_n0	output_n1	output_n0s                          r   "test_add_prefix_attribute_subgraphz7TestComposeFunctions.test_add_prefix_attribute_subgraph  s   ))#{/?/?!E))#{/@/@4)L))#{/@/@4)L))#{/@/@4)L++E;3D3DtQiPeS#JGudC[5'JudC[5'J5G))e&cU ))e&cU

 !!t*7Aq!Q<#
  11%@N+.--uzz: 	VFBYvrww7A*-bllBLL*I V&
J<<47")):<<+<+<5 
V0[ 36'--{/@/@3 T.Hh !,,Yvx-H(ST 58'..0B0B5 V0Iy !,,Yvy-I9UV
VV	Vr   c                0    | j                  dddddd       y)z&Tests prefixing all names in the graphTNr   r   s    r   test_add_prefix_allz(TestComposeFunctions.test_add_prefix_all;  s    dD$dDAr   c                (    | j                  d       y)zTests prefixing inplaceTr   Nr   r   s    r   test_add_prefix_inplacez,TestComposeFunctions.test_add_prefix_inplace?  s    d+r   c                     t        t              }d fd}dD ]"  }t        j                  ||      } ||||       $ t	               }|j                  |       d}t        j                  ||d        ||||       y)zTests expanding output dimensions. The resulting graph should have the same output names,
        but with one more dimension at the specified index.
        c                   t        |j                  j                  | j                  j                        D ]  \  }}j                  |j                  |j                         j                  |j
                  j                  j                  |j
                  j                  j                         t        |      }|j                  |d       j                  t        |      |        y )Nr   )
r   rL   r`   r^   r.   r!   r"   	elem_typer(   insert)rS   rT   dim_idxout_g2out_g1expected_out_shaperO   s         r   _check_modelz>TestComposeFunctions.test_expand_out_dim.<locals>._check_modelI  s    "%bhhoorxx"G I  fkk:  KK++55v{{7N7N7X7X &0%7""))'15  F!35GHIr   )r   r+   r   Tr  N)rS   r   rT   r   r#  intrq   rr   )r   rt   r   expand_out_dimr   r   )rO   rS   r'  r#  rT   s   `    r   test_expand_out_dimz(TestComposeFunctions.test_expand_out_dimC  s{      	I & 	*G''G4BR)	*
 \
Br7D9RW%r   c                n   t        t        |            D cg c]"  }t        j                  d||   g||   g      $ }}t        t        |            D cg c]*  }t        j                  ||   t
        j                  g       , }}t        t        |            D cg c]*  }t        j                  ||   t
        j                  g       , }}t        t        |            D cg c]*  }t        j                  ||   t
        j                  g       , }}t        t        |            D cg c]-  }t        j                  ||   t
        j                  ddg      / }}t        t        |	            D cg c]  }t        |	|          }}t        t        |            D cg c]"  }t        j                  d||   g||   g      $ }}t        t        |            D cg c]*  }t        j                  ||   t
        j                  g       , }}t        t        |            D cg c]*  }t        j                  ||   t
        j                  g       , }}t        t        |            D cg c]*  }t        j                  ||   t
        j                  g       , }}t        t        |            D cg c]-  }t        j                  ||   t
        j                  ddg      / }}t        t        |
            D cg c]  }t        |
|          }}t        j                  dd      g}t        j                  t        j                  |d|||||	      d
|      }t        j                  t        j                  |d|||||	      d
|      }t        j                  |j                  |j                        }d}t!        t#        |      t#        |      z        }t!        t#        |      t#        |      z        }t!        t#        ||z               }|r| j%                  ||   d|f       |dz  }t!        t#        |      t#        |      z        }|r| j%                  ||   d|f       |dz  }t!        t#        |      t#        |      z        } | r| j%                  ||   d| f       |dz  }t!        t#        |	      t#        |
      z        }!|!rMg }"|!D ]*  }#|"j'                  |#dz          |"j'                  |#dz          , | j%                  ||   d|"f       |dz  }t        j(                  |d      }$t        j                  |$j                  |j                        }| j%                  dt        |             y c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )NIdentityr    r   r.   r/   r0   r1   r   r   g0)r  r.   rG   rH   r&   r   r   r   r   rc   r   edger&   r   r-   r3   r   zg0/)r   )r    r   r   r  r  r   r5   r4   r:   rB   r   r   r	  r   check_overlapping_namesrL   listsetr^   appendr   )%rO   inputs0inputs1outputs0outputs1value_info0value_info1initializer0initializer1sparse_initializer0sparse_initializer1ir   r   r   r   r   r   r   r   r   r   r   r   opsm0rS   overlapoverlapping_inputsoverlapping_outputsoverlapping_edgesoverlapping_visoverlapping_initoverlapping_sparse_initexpected_overlapoverlapping_namem0_news%                                        r   _test_overlapping_namesz,TestComposeFunctions._test_overlapping_names^  s    3w<(
 ZxPQ{mT
 
 3w<(
 ))'!*k6G6GL
 
 3x=)
 ))(1+{7H7H"M
 
 3{+,
 ))+a.+:K:KRP
 
 3|,-	
  !!_0A0ARSQT
 
 3234
   3A 67
 
 3w<(
 ZxPQ{mT
 
 3w<(
 ))'!*k6G6GL
 
 3x=)
 ))(1+{7H7H"M
 
 3{+,
 ))+a.+:K:KRP
 
 3|,-	
  !!_0A0ARSQT
 
 3234
   3A 67
 

 ""2r*+!#/ !
 !#/ !
 11"((BHHE!#g,W"=>"3x=3x=#@A %7:M%M!NOWQZ&2C)DEFAs;/#k2BBCWQZ,)HIFAL 1C4E EFWQZ-9I)JKFA"&#$s+>'??#
 #!$; C  ''(89(DE ''(86(ABC WQZ*>@P)QRFA##Bu511&,,ICL)i












sG   'U;/V /V$/V
+2V5V"'V!/V(/V#//V(62V- V2c                2    | j                  ddgddg       y)z9Tests error checking when the name of the inputs overlapsr   r   i2)r7  r8  NrN  r   s    r   test_overlapping_input_namesz1TestComposeFunctions.test_overlapping_input_names  s    $$dD\D$<$Pr   c                2    | j                  ddgddg       y)z9Tests error checking when the name of the output overlapsr   r   o2)r9  r:  NrQ  r   s    r   test_overlapping_output_namesz2TestComposeFunctions.test_overlapping_output_names  s    $$tTldD\$Rr   c                2    | j                  ddgddg       y)zATests error checking when the name of value_info entries overlapsr   r   vi2)r;  r<  NrQ  r   s    r   !test_overlapping_value_info_namesz6TestComposeFunctions.test_overlapping_value_info_names  s"    $$UEN 	% 	
r   c                2    | j                  ddgddg       y)zBTests error checking when the name of initializer entries overlapsr   r   init2)r=  r>  NrQ  r   s    r   "test_overlapping_initializer_namesz7TestComposeFunctions.test_overlapping_initializer_names  s$    $$!7+7G:L 	% 	
r   c                2    | j                  ddgddg       y)zITests error checking when the name of sparse_initializer entries overlapsr   r   sparse_init2)r?  r@  NrQ  r   s    r   )test_overlapping_sparse_initializer_namesz>TestComposeFunctions.test_overlapping_sparse_initializer_names  s%    $$!/ @!/ @ 	% 	
r   c                   t        j                  dd      t        j                  dd      g	 	 	 	 	 	 	 	 	 	 	 	 d%fd}t        j                  dd      t        j                  dd      gt               }|j                  j	                  t        j
                  dt        j                  g       t        j
                  dt        j                  g       g       |j                  j	                  t        j
                  dt        j                  g       g       |j                  j	                  t        j                  ddddgdg	      g       t               }|j                  |       d
|_        t        j                  |d      }|j                  j	                   |ddddgdgt        j                  dddgdg      g      g       t        j                   |       t               }|j                  |       d|_        t        j                  |d      }|j                  j	                   |ddddgdgt        j                  dddgdg      g      g       t        j                   |       t#        j$                  ||ddgdd      }t        j                   |       |j&                  j                  D cg c]  }|j(                   }	}| j+                  ddg|	       |j                  D 
cg c]  }
|
j                   }}
| j+                  ddg|       t               }|j                  |       d|_        d|j                  d   _        t        j                  |d      }|j                  j	                   |ddddgdgt        j                  dddgdg      t        j                  dddgdg      t        j                  dddgdg      g       |ddddgdgt        j                  ddddgdg	      t        j                  dddgdg      t        j                  dddgdg      g      g       t        j                   |       t#        j$                  ||ddgdd      }t        j                   |       |j&                  j                  D cg c]  }|j(                   }	}| j+                  ddg|	       |j                  D 
cg c]  }
|
j                   }}
| j+                  g d|       | j+                  dg|j                  d   j                  D cg c]  }|j(                   c}       | j+                  g d |j                  d!   j                  D cg c]  }|j(                   c}       | j+                  g d"|j                  d#   j                  D cg c]  }|j(                   c}       y$c c}w c c}
w c c}w c c}
w c c}w c c}w c c}w )&zETests error checking when the name of local function entries overlapsr   r   localc                   t               }| |_        ||_        |j                  j	                  |       |j
                  j	                  |       |j                  j	                  |       |j                  j	                         |S )N)r   domainr.   r_   extendr`   ra   opset_import)rb  fnamerG   rH   r  frB  s         r   _make_functionzLTestComposeFunctions.test_overlapping_function_names.<locals>._make_function   sa     AAHAFGGNN6"HHOOG$FFMM% NN!!#&Hr   x0x1yf1)rb  rG   rH   rc   r   r   r[   r   rd   r]   )rj  rh  )rj  ri  r   r   )rF   rI   rJ   m1/f1zm2/f1rU   f2r   y0y1zm3/m3/f2)rl  m3/f1rp  )r[   r]   r[   r   )rq  r]   r[   r+   N)rb  strre  rr  rG   	list[str]rH   rs  r  zlist[NodeProto]rq   r   )r   r   r   r_   rc  r  r   r5   r`   ra   r  r   r.   r   	functionsr   r   r   rN   rL   rb   r^   )rO   rg  r  rc   rS   rd   rT   r   r   r  rf  rt  rU   rV   rB  s                 @r   test_overlapping_function_namesz4TestComposeFunctions.test_overlapping_function_names  s`   ""2r*F,?,?,LM			 	 		
 #	 	  ""2r*F,?,?,LML	--dK4E4ErJ--dK4E4ErJ	
 	
--c;3D3DbI	

 	
d7D$<RUQVWX	
 \
ArsK
4LE%%eT4L3%PQ
	
 	B\
ArsK
4LE%%eT4L3%PQ
	
 	B  K5ue
 	A$%GGLL1q11'7+U3%&[[1QVV1	1'7+Y7\
A!
rsK
4LE((d|dVT((d|dVT((d|cUS
 4LE(( $PTv ((d|dVT((d|cUS	
8 	B  K5ue
 	A$%GGLL1q11'7+U3%&[[1QVV1	14i@%akk!n6I6I"J199"JK!q{{1~7J7J#K!AII#K	
 	#Q9L9L%MAaii%M	
q 2 2V 2 2 #K#K &Ns*   ;W2W	WW#W
&W
)W"
c                $   t        j                  dd      g}t               }|j                  j	                  t        j
                  dt        j                  g       g       |j                  j	                  t        j
                  dt        j                  g       g       |j                  j	                  t        j                  ddgdg      g       t               }|j                  |       d|_        t        j                  |d|	      }t        j                  |       t               }|j                  |       d
|_        |j                   j	                  t        j"                  dt        j                  ddg      g       t        j                  |d|	      }t        j                  |       t               }|j                  |       d|_        |j$                  j	                  t'        d      g       t        j                  |d|	      }t        j                  |       t               }	|	j                  |       d|	_        |	j(                  j	                  t        j
                  dt        j                  g       g       t        j                  |	d|	      }
t        j                  |
       t+        j,                  ||ddg      }| j/                  dt1        |j2                  j                                t+        j,                  ||ddg      }| j/                  dt1        |j2                  j                                t+        j,                  ||
ddg      }| j/                  dt1        |j2                  j(                               y)z;Tests automatic removal of initializers when merging graphsr   r   xrj  r.  r   rc   r   r   rd   r/  r   r0  rU   r   )rj  rw  )rI   rF   N)r   r   r   r_   rc  r  r   r5   r`   ra   r  r   r.   r   r   r   r   r4   r   rB   r&   r   rN   r^   r   rL   )rO   rB  r  rc   rS   rd   rT   rU   rV   r   m4out_m1out_m2out_m3s                 r   7test_merge_drop_unnecessary_initializers_and_value_infozLTestComposeFunctions.test_merge_drop_unnecessary_initializers_and_value_info  s   ""2r*+L	55c;;L;LbQRS	66sK<M<MrRST	v''
C53%PQR\
ArsKB\
A
""(9(9!	
 rsKB\
A
$$&9#&>%?@rsKB\
A
**30A0A2FG	
 rsKB %%b"eZLQC 8 89: %%b"eZLQC 8 89: %%b"eZLQC 7 789r   )NNNN)rP   rr  rQ   rr  rF   zlist[tuple[str, str]]rR   z4Callable[[GraphProto, GraphProto, GraphProto], None]rG   list[str] | NonerH   r}  rI   
str | NonerJ   r~  rq   rr   )rq   rr   )FFFFFFF)r   boolr   r  r   r  r   r  r   r  r   r  r   r  rq   rr   )
)r   r   )rP  i3)r   r   )rT  o3)v0r   )r   r   )r   r   )rZ  init3)r   r   )r]  sparse_init3)r7  Sequence[str]r8  r  r9  r  r:  r  r;  r  r<  r  r=  r  r>  r  r?  r  r@  r  rq   rr   ) __name__
__module____qualname__rW   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r,  rN  rR  rU  rX  r[  r^  ru  r|  r/  r   r   rD   rD   `   s    $($(""!9!9 !9 &	!9
 Q!9 !!9 "!9 !9 !9 
!9FL L"
& LD%
N
6L&S(
*
> * #"#$$)#(rJrJ rJ 	rJ
 rJ "rJ !rJ rJ 
rJh1123+VZB,&: ".!-".".%1%1&8&8-M-MA*A* A*  	A*
  A* #A* #A* $A* $A* +A* +A* 
A*FQS


G
R6:r   rD   __main__)r   rr  rq   r   )r   rr  r   rr  rq   rr  )r&   r   rq   z	list[int])r.   rr  rq   r
   )
__future__r   unittesttypingr   r   numpyr6   onnxr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r(   rB   rt   ru   TestCaserD   r  mainr/  r   r   <module>r     sy    #  %     +
Q,

[:8,, [:| zHMMO r   