
    
Ngk                        d dl Z d dlmZmZmZ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edefdZdededefd	Zd
edee         fdZdedefdZdZdZ G d de j                  Ze dk    r e j!                     dS dS )    N)CallableListOptionalSequenceTuple)FunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProtoValueInfoProtocheckercomposehelperparserversion_converterm_defreturnc                 V    t          j        |           }t          j        |           |S )zYParses a model from a string representation, including checking the model for correctness)r   parse_modelr   check_model)r   ms     R/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/test/compose_test.py_load_modelr      s(    5!!AH    prefixsc                 6    t          |          dk    r| |z   n|S )z Prefixes a string (if not empty)r   )len)r   r   s     r   	_prefixedr!   !   s    Q!6A::*r   
value_infoc                 |      fdt          t           j        j        j        j                            D             S )zPReturns a list of integers representing the shape of the provided ValueInfoProtoc                 R    g | ]#}j         j        j        j        |         j        $S  )typetensor_typeshapedim	dim_value).0dr"   s     r   
<listcomp>z_get_shape.<locals>.<listcomp>(   s<        	#)-a0:  r   )ranger    r&   r'   r(   r)   )r"   s   `r   
_get_shaper/   &   sI       s:?6<@AABB   r   namec                    ddg}g d}g d}t          j        | dz   t          j        t	          |          gt          j        |                              t
          j                  d          }t          j        | dz   t          j	        t	          |          gt          j        |                              t
          j
                  d          }t          j        |||          S )N   )   r2      )g333333?g?g?_valuesF)r0   	data_typedimsvalsraw_idx)r   make_tensorr   FLOATr    nparrayastypefloat32INT64int64make_sparse_tensor)r0   dense_shapelinear_indicessparse_valuesvalues_tensorindices_tensors         r   _make_sparse_tensorrI   .   s    a&KYYN#OOM&I#-  !Xm$$++BJ77  M 'F]#.!!"Xn%%,,RX66  N $]NKP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)
        M1 = Mul(C0, C1)
    }
    c                      e Zd Z	 	 	 	 dDdededeeeef                  deeeegdf         deee                  deee                  dee         d	ee         d
dfdZ	dEdZ
dEdZdEdZdEdZdEdZdEdZdEdZdEdZdEdZdEdZdEdZ	 	 	 	 	 	 	 dFdededededededed
dfdZdEd ZdEd!ZdEd"ZdEd#ZdEd$ZdEd%ZdEd&ZdEd'Z	 	 	 	 	 	 	 	 	 	 dGd2ee         d3ee         d4ee         d5ee         d6ee         d7ee         d8ee         d9ee         d:ee         d;ee         d
dfd<Z dEd=Z!dEd>Z"dEd?Z#dEd@Z$dEdAZ%dEdBZ&dEdCZ'dS )HTestComposeFunctionsNm1defm2defio_mapcheck_expectationsinputsoutputsprefix1prefix2r   c	           	      x   t          |          t          |          }
}	t          j        |	j        |
j        |||||          }t	          j        |            ||	j        |
j        |           t          j        |	|
|||||          }t	          j        |            ||	j        |
j        |j                   d S )N)rN   rP   rQ   rR   rS   )r   r   merge_graphsgraphr   check_graphmerge_modelsr   )selfrL   rM   rN   rO   rP   rQ   rR   rS   m1m2g3m3s                r   _test_merge_modelsz'TestComposeFunctions._test_merge_models`   s     U##[%7%7B!HH
 
 
 	B28RXr222!
 
 
 	B28RXrx88888r   c                      dt           dt           dt           ddf fd}g d}                     t          t          ||           dS )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
        g1g2r\   r   Nc                                          |j        | j                                        |j        |j                                        g dd |j        D                        d S )NAddSubMulrd   re   rf   c                     g | ]	}|j         
S r%   op_typer+   items     r   r-   zlTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>       222$222r   assertEqualinputoutputnoder`   ra   r\   rY   s      r   rO   zXTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectations   su    RXrx000RY	222:::22"'222    r   B00B01B10B11B20B21r	   r^   M1_DEFM2_DEFrY   rO   rN   s   `  r   'test_case_connect_all_no_name_collisionz<TestComposeFunctions.test_case_connect_all_no_name_collision   sm    
	: 	: 	: 	RV 	 	 	 	 	 	 BAA8JKKKKKr   c                      dt           dt           dt           ddf fd}g d}                     t          t          ||           dS )zTests a scenario where we merge two models by connecting a single output in the first model
        to all the inputs in the second
        r`   ra   r\   r   Nc                     ~                     |j        | j                                        g dd |j        D                                             g dd |j        D                        d S )N)rx   r{   D0c                     g | ]	}|j         
S r%   r0   r+   elems     r   r-   zhTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations.<locals>.<listcomp>   s    3T3T3T$DI3T3T3Tr   rc   c                     g | ]	}|j         
S r%   rh   rj   s     r   r-   zhTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations.<locals>.<listcomp>   rl   r   rm   rr   s      r   rO   zTTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations   s    RXrx0001113T3T")3T3T3TUUU:::22"'222    r   rt   )ru   ry   )ru   r|   r}   r   s   `  r   #test_case_connect_same_output_twicez8TestComposeFunctions.test_case_connect_same_output_twice   sm    
	: 	: 	: 	RV 	 	 	 	 	 	 BAA8JKKKKKr   c                      dt           dt           dt           ddf fd}g d}dg}                     t          t          |||	           dS )
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
        r`   ra   r\   r   Nc                     ~                     |j        | j                                        dgd |j        D                                             g dd |j        D                        d S )Nr   c                     g | ]	}|j         
S r%   r   r   s     r   r-   zoTestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations.<locals>.<listcomp>       %F%F%FDdi%F%F%Fr   )rd   rd   re   rf   c                     g | ]	}|j         
S r%   rh   rj   s     r   r-   zoTestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations.<locals>.<listcomp>   s    .P.P.Pt|.P.P.Pr   rm   rr   s      r   rO   z[TestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations   s    RXrx000dV%F%FBI%F%F%FGGG,,,.P.P.P.P.P    r   r   r   )rQ   r}   )rY   rO   rN   rQ   s   `   r   *test_case_connect_same_output_drop_outputsz?TestComposeFunctions.test_case_connect_same_output_drop_outputs   s    
	: 	: 	: 	RV 	 	 	 	 	 	 BAA&FF$6 	  	
 	
 	
 	
 	
r   c                      d}d}dg}dt           dt           dt           ddf fd	}                     ||||           dS )
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   r`   ra   r\   r   Nc                     ~ ~                     dgd |j        D                                             dgd |j        D                        d S )NAc                     g | ]	}|j         
S r%   r   r   s     r   r-   zmTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectations.<locals>.<listcomp>   s    $D$D$D4TY$D$D$Dr   Cc                     g | ]	}|j         
S r%   r   r   s     r   r-   zmTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectations.<locals>.<listcomp>   s    $E$E$E4TY$E$E$Er   rn   ro   rp   rr   s      r   rO   zYTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectations   s`    BcU$D$D28$D$D$DEEEcU$E$E29$E$E$EFFFFFr   r	   r^   )rY   m1_defm2_defrN   rO   s   `    r   (test_case_connect_same_input_output_namez=TestComposeFunctions.test_case_connect_same_input_output_name   s    		 	G: 	G: 	G: 	GRV 	G 	G 	G 	G 	G 	G 	8JKKKKKr   c                      d}d}dg}dt           dt           dt           ddf fd	}d
g}dg}                     ||||||           dS )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B2r`   ra   r\   r   Nc                     ~ ~                     dgd |j        D                                             dgd |j        D                                             ddgd |j        D                        d S )NA0c                     g | ]	}|j         
S r%   r   r   s     r   r-   zbTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations.<locals>.<listcomp>   s    %E%E%EDdi%E%E%Er   B3c                     g | ]	}|j         
S r%   r   r   s     r   r-   zbTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations.<locals>.<listcomp>   r   r   rd   re   c                     g | ]	}|j         
S r%   rh   r   s     r   r-   zbTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations.<locals>.<listcomp>       -O-O-Otdl-O-O-Or   rm   rr   s      r   rO   zNTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations   s    BdV%E%EBH%E%E%EFFFdV%F%FBI%F%F%FGGGeU^-O-Orw-O-O-OPPPPPr   r   r   rP   rQ   r   )rY   r   r   rN   rO   rP   rQ   s   `      r   test_case_drop_inputs_outputsz2TestComposeFunctions.test_case_drop_inputs_outputs   s    

 	Q: 	Q: 	Q: 	QRV 	Q 	Q 	Q 	Q 	Q 	Q &FF$6vw 	  	
 	
 	
 	
 	
r   c                      d}dg}dt           dt           dt           ddf fd}                     ||||d	d
           dS )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   r`   ra   r\   r   Nc                     ~ ~                     g dd |j        D                                             dgd |j        D                                             ddgd |j        D                        d S )N)zm1/Azm1/Bzm2/Bc                     g | ]	}|j         
S r%   r   r   s     r   r-   zdTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations.<locals>.<listcomp>  s    7W7W7Wd	7W7W7Wr   zm2/Cc                     g | ]	}|j         
S r%   r   r   s     r   r-   zdTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations.<locals>.<listcomp>  s    'H'H'Hd	'H'H'Hr   rd   c                     g | ]	}|j         
S r%   rh   r   s     r   r-   zdTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations.<locals>.<listcomp>  r   r   rm   rr   s      r   rO   zPTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations  s    B5557W7Wbh7W7W7WXXXfX'H'Hbi'H'H'HIIIeU^-O-Orw-O-O-OPPPPPr   m1/m2/)rR   rS   r   )rY   r   rN   rO   s   `   r   test_case_name_collision_prefixz4TestComposeFunctions.test_case_name_collision_prefix   s    	 	Q: 	Q: 	Q: 	QRV 	Q 	Q 	Q 	Q 	Q 	Q 	FF$6u 	  	
 	
 	
 	
 	
r   c                      dt           dt           dt           ddf fd}ddg}                     t          t          ||           dS )	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
        r`   ra   g4r   Nc                     ~ ~                     g dd |j        D                                             ddgd |j        D                        d S )N)r   r   _Ar|   c                     g | ]	}|j         
S r%   r   r   s     r   r-   zrTestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>'  s    +K+K+K$DI+K+K+Kr   r{   r   c                     g | ]	}|j         
S r%   r   r   s     r   r-   zrTestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>)  s    ,M,M,M4TY,M,M,Mr   r   )r`   ra   r   rY   s      r   rO   z^TestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectations!  sq    B )))+K+K"(+K+K+K   eT],M,M29,M,M,MNNNNNr   rt   rw   r}   r   s   `  r   -test_case_connect_partially_no_name_collisionzBTestComposeFunctions.test_case_connect_partially_no_name_collision  su    	O: 	O: 	O: 	ORV 	O 	O 	O 	O 	O 	O !.18JKKKKKr   c                 2   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
           | 	                    t          t
          j        |||           d S )Nv1v2)p1p2v3v4)p3p4rt   rN      )r   r   r2   v5)r   r~   r   set_model_propsr   r   rX   r    metadata_propsassertRaises
ValueErrorrY   rZ   r[   rN   r]   s        r   %test_merge_models_with_metadata_propsz:TestComposeFunctions.test_merge_models_with_metadata_props.  s"     r$d#;#;<<<  r$d#;#;<<< !!"b8882$%%**** 	r$d#;#;<<<!"b8882$%%**** 	r$d#;#;<<<*g&:B6RRRRRr   c                    t          t                    t          t                    }}|                     t          t
          j        ||g d           |                     t          t
          j        ||g d           dS )z_Tests that providing a non existing output/input name in the io_map argument produces an error.))wrong_outnamerv   rw   rz   r   ))ru   wrong_inputrw   rz   N)r   r~   r   r   r   r   rX   rY   rZ   r[   s      r   "test_error_wrong_input_output_namez7TestComposeFunctions.test_error_wrong_input_output_nameB  s    V$$k&&9&9B MMM 	 	
 	
 	
 	 KKK 	 	
 	
 	
 	
 	
r   c                     t          d          }t          d          }|                     t          t          j        ||dg           d S )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   rX   r   s      r   test_error_ir_version_mismatchz3TestComposeFunctions.test_error_ir_version_mismatchW  se    	
 
 	
 
 	,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}|                     t          t          j
        |||           t          j        |d          }t          j
        |||          }t          j        |           dS )	zSTests that providing models with different operator set imported produces an error.test 
   producer_nameopset_imports   rs   r   N)r   r~   r   r   
make_modelrV   make_opsetidr   r   r   rX   r   convert_versionr   r   r   s        r    test_error_opset_import_mismatchz5TestComposeFunctions.test_error_opset_import_mismatchv  s    V$$k&&9&9BHF6;NrSU;V;V:W
 
 
 HF6;NrSU;V;V:W
 
 
 BAA*g&:BFKKK .r266!"b888Br   Frename_nodesrename_edgesrename_inputsrename_outputsrename_initializersrename_value_infosinplacec                 	   t          t                    }d}	|rAt                      }
|
                    |           t	          j        |
|	||||||d	  	         nt	          j        ||	||||||          }
|j        }|
j        }|s	|s|s|s|r\i }|rD|j        D ]<}|j        D ]}t          |	|          ||<   |j
        D ]}t          |	|          ||<   =|r'|j        D ]}t          |	|j                  ||j        <    |r'|j
        D ]}t          |	|j                  ||j        <    |r|j        D ]}t          |	|j                  ||j        <    |j        D ]P}t          |	|j        j                  ||j        j        <   t          |	|j        j                  ||j        j        <   Q|r'|j
        D ]}t          |	|j                  ||j        <    t!          |j        |j                  D ]\  }}t!          |j        |j                  D ]/\  }}|                     |                    ||          |           0t!          |j
        |j
                  D ]/\  }}|                     |                    ||          |           0t!          |j        |j                  D ]>\  }}|                     |                    |j        |j                  |j                   ?t!          |j
        |j
                  D ]>\  }}|                     |                    |j        |j                  |j                   ?t!          |j        |j                  D ]>\  }}|                     |                    |j        |j                  |j                   ?t!          |j        |j                  D ]\  }}|                     |                    |j        j        |j        j                  |j        j                   |                     |                    |j        j        |j        j                  |j        j                   t!          |j        |j                  D ]>\  } }!|                     |                    |!j        |!j                  | j                   ?|rPt!          |j        |j                  D ]7\  }}|                     t          |	|j                  |j                   4d S d S d S )Nzpre/T)r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r~   r
   CopyFromr   
add_prefixrV   rq   ro   r!   rp   r0   initializersparse_initializervaluesindicesziprn   getr"   )"rY   r   r   r   r   r   r   r   rZ   r   r[   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OOO))+-$7#5
 
 
 
 
 #))+-$7#5	 	 	B x H	JH	J H	J #	H	J
 "H	J L  ? ? ?AW ? ?*3FA*>*>QX ? ?*3FA*>*>Q? K J K KD.7	.J.JL++ K K K KD.7	.J.JL++" 	 , K KD.7	.J.JL++#'#:  K<E 2 7= =L!3!89 >G 3 8> >L!4!9:: " W"&+ W WJ4=fjo4V4VL11ej$)44 C CB!"(BH55 C CFB$$\%5%5b"%=%=rBBBB!")RY77 C CFB$$\%5%5b"%=%=rBBBBCek4:66 N NB  !1!1"'27!C!CRWMMMMelDK88 N NB  !1!1"'27!C!CRWMMMM #E$5t7G H H W Wu  !1!1%*ej!I!I5:VVVV.1($*A/ /  *l    $$$+0,2E2J  !',	      $$$,1<3G3L  !(-	      0$/BB Q QS  !1!1#(CH!E!EsxPPPP J!%*di88 J JFB$$Yvrw%?%?IIIIQH	J H	JLJ JJ Jr   c                 2    |                      d           dS )zTests renaming nodes onlyT)r   Nr  rY   s    r   test_add_prefix_nodesz*TestComposeFunctions.test_add_prefix_nodes      400000r   c                 2    |                      d           dS )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                 2    |                      d           dS )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11111r   c                 2    |                      d           dS )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22222r   c                 p   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 ]\  }}|                     t          ||j                  |j                   t          |j        |j                  D ]\  }}|j        rt          |j        j        |j        j                  D ]\  }}t          |j        |j                  D ])\  }}|                     t          ||          |           *t          |j        |j                  D ])\  }}|                     t          ||          |           *dS )zQTests prefixing attribute's subgraph. Relevant subgraph should be renamed as wellr      XNYZOutrf   XYr   rd   re   Ifthen)nodesr0   rP   rQ   else)rP   rQ   then_branchelse_branchrV   zprefix.)r   make_tensor_value_infor   BOOLr<   	make_node
make_graphr   add_prefix_graphr   rW   r   rq   rn   r!   r0   	attributegro   rp   )rY   r   r  r  r  r  r  addsubcondrV   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E)#{/@4)LL)#{/@4)LL)#{/@4)LL+E;3DtQiPPeS#JGGGudC[5'JJJudC[5'JJJ5G)e&cU   )e&cU  

 

 

 !t*7Aq!Q<#
 
 
  1%@@N+++.-uz:: 	V 	VFBYvrw77AAA*-blBL*I*I V V&
J< V47"):<+<5 5 
V 
V0[ 36'-{/@3 3 T T.Hh !,,Yvx-H-H(SSSS47'.0B5 5 V V0Iy !,,Yvy-I-I9UUUUVV	V 	Vr   c                 :    |                      dddddd           dS )z&Tests prefixing all names in the graphTNr  r  s    r   test_add_prefix_allz(TestComposeFunctions.test_add_prefix_all:  s&    dD$dDAAAAAr   c                 2    |                      d           dS )zTests prefixing inplaceTr   Nr  r  s    r   test_add_prefix_inplacez,TestComposeFunctions.test_add_prefix_inplace>  s    d+++++r   c                 J    t          t                    }dt          dt          dt          ddf fd}dD ]$}t	          j        ||          } ||||           %t                      }|                    |           d}t	          j        ||d	
            ||||           dS )zTests expanding output dimensions. The resulting graph should have the same output names,
        but with one more dimension at the specified index.
        rZ   r[   dim_idxr   Nc                    t          |j        j        | j        j                  D ]\  }}                    |j        |j                                       |j        j        j        |j        j        j                   t          |          }|	                    |d                               t          |          |           d S )Nr  )
r   rV   rp   rn   r0   r&   r'   	elem_typer/   insert)rZ   r[   r8  out_g2out_g1expected_out_shaperY   s         r   _check_modelz>TestComposeFunctions.test_expand_out_dim.<locals>._check_modelH  s    "%bhorx"G"G I I  fk:::  K+5v{7N7X   &0%7%7""))'1555  F!3!35GHHHHI Ir   )r   r3   r   Tr5  )r   r~   r
   intr   expand_out_dimr   )rY   rZ   r?  r8  r[   s   `    r   test_expand_out_dimz(TestComposeFunctions.test_expand_out_dimB  s       	IZ 	IZ 	I# 	I$ 	I 	I 	I 	I 	I 	I & 	* 	*G'G44BLR)))) \\
Br7D9999RW%%%%%r   r   r   i2i3r   r   o2o3v0r   r   r   r   r   init2init3r  r  sparse_init2sparse_init3inputs0inputs1outputs0outputs1value_info0value_info1initializer0initializer1sparse_initializer0sparse_initializer1c                 H	  	
 fdt          t                              D             }fdt          t                              D             }fdt          t                              D             }fdt          t                              D             }fdt          t                              D             }	fdt          t          	                    D             }fdt          t                              D             }fdt          t                              D             }fd	t          t                              D             }fd
t          t                              D             }fdt          t                              D             }
fdt          t          
                    D             }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#|                     ||         d|f           |dz  }t          t                    t                    z            }|r#|                     ||         d|f           |dz  }t          t                    t                    z            } | r#|                     ||         d| f           |dz  }t          t          	          t          
          z            }!|!rZg }"|!D ]2}#|"                    |#dz              |"                    |#dz              3|                     ||         d|"f           |dz  }t          j        |d          }$t          j        |$j        |j                  }|                     dt          |                     d S )Nc                 Z    g | ]'}t          j        d |         g|         g          (S Identityr   r   r   )r+   irX  rZ  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>j  H     
 
 
 ZxPQ{mTTT
 
 
r   c                 \    g | ](}t          j        |         t          j        g           )S r%   r   r  r   r<   )r+   rg  rX  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>n  ?     
 
 
 )'!*k6GLL
 
 
r   c                 \    g | ](}t          j        |         t          j        g           )S r%   rj  )r+   rg  rZ  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>r  ?     
 
 
 )(1+{7H"MM
 
 
r   c                 \    g | ](}t          j        |         t          j        g           )S r%   rj  )r+   rg  r\  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>v  ?     
 
 
 )+a.+:KRPP
 
 
r   c                 b    g | ]+}t          j        |         t          j        d dg          ,S r%   r  r0   r6   r7   r8   r   r;   r   rA   )r+   rg  r^  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>z  Q     
 
 
  !!_0ARSQT  
 
 
r   c                 :    g | ]}t          |                   S r%   rI   )r+   rg  r`  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  7     
 
 
   3A 677
 
 
r   c                 Z    g | ]'}t          j        d |         g|         g          (S rd  rf  )r+   rg  rY  r[  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  rh  r   c                 \    g | ](}t          j        |         t          j        g           )S r%   rj  )r+   rg  rY  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  rk  r   c                 \    g | ](}t          j        |         t          j        g           )S r%   rj  )r+   rg  r[  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  rm  r   c                 \    g | ](}t          j        |         t          j        g           )S r%   rj  )r+   rg  r]  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  ro  r   c                 b    g | ]+}t          j        |         t          j        d dg          ,S rq  rs  )r+   rg  r_  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  rt  r   c                 :    g | ]}t          |                   S r%   rv  )r+   rg  ra  s     r   r-   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  rw  r   r   r   g0)r  r0   rP   rQ   r"   r   r   r   r   r`   r   edger  r"   r   r5   r:   r   zg0/)r   )r.   r    r   r   r   r!  r   check_overlapping_namesrV   listsetrn   appendr   )%rY   rX  rY  rZ  r[  r\  r]  r^  r_  r`  ra  r   r   r   r  r   r  r   r   r   r  r   r  opsm0rZ   overlaprg  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<<((
 
 

 
 
 
3w<<((
 
 

 
 
 
3x==))
 
 

 
 
 
3{++,,
 
 

 
 
 
 3|,,--	
 
 

 
 
 
323344
 
 


 
 
 
 
3w<<((
 
 

 
 
 
3w<<((
 
 

 
 
 
3x==))
 
 

 
 
 
3{++,,
 
 

 
 
 
 3|,,--	
 
 

 
 
 
323344
 
 

 "2r**+!#/   !
 
 
 !#/   !
 
 
 1"(BHEE!#g,,W"=>>"3x==3x==#@AA %7:M%M!N!NOO 	WQZ&2C)DEEEFAs;//#k2B2BBCC 	WQZ,)HIIIFAL 1 1C4E4E EFF 	WQZ-9I)JKKKFA"&#$$s+>'?'??#
 #
 # 	!$; C C  ''(89(DEEE ''(86(ABBBBWQZ*>@P)QRRRFA#Bu5551&,IICLL)))))r   c                 <    |                      ddgddg           dS )z9Tests error checking when the name of the inputs overlapsr   r   rG  )rX  rY  Nr  r  s    r   test_overlapping_input_namesz1TestComposeFunctions.test_overlapping_input_names  s)    $$dD\D$<$PPPPPr   c                 <    |                      ddgddg           dS )z9Tests error checking when the name of the output overlapsr   r   rK  )rZ  r[  Nr  r  s    r   test_overlapping_output_namesz2TestComposeFunctions.test_overlapping_output_names  s)    $$tTldD\$RRRRRr   c                 <    |                      ddgddg           dS )zATests error checking when the name of value_info entries overlapsr  r  vi2)r\  r]  Nr  r  s    r   !test_overlapping_value_info_namesz6TestComposeFunctions.test_overlapping_value_info_names  s6    $$UEN 	% 	
 	
 	
 	
 	
r   c                 <    |                      ddgddg           dS )zBTests error checking when the name of initializer entries overlapsr   r   rR  )r^  r_  Nr  r  s    r   "test_overlapping_initializer_namesz7TestComposeFunctions.test_overlapping_initializer_names  s8    $$!7+7G:L 	% 	
 	
 	
 	
 	
r   c                 <    |                      ddgddg           dS )zITests error checking when the name of sparse_initializer entries overlapsr  r  rV  )r`  ra  Nr  r  s    r   )test_overlapping_sparse_initializer_namesz>TestComposeFunctions.test_overlapping_sparse_initializer_names  s9    $$!/ @!/ @ 	% 	
 	
 	
 	
 	
r   c                    t          j        dd          t          j        dd          gdt          dt          dt          t                   dt          t                   dt          t                   d	t
          ffd
}t          j        dd          t          j        dd          gt                      }|j                            t          j	        dt          j        g           t          j	        dt          j        g           g           |j                            t          j	        dt          j        g           g           |j                            t          j        ddddgdg          g           t                      }|                    |           d|_        t          j        |d          }|j                             |ddddgdgt          j        dddgdg          g          g           t'          j        |           t                      }|                    |           d|_        t          j        |d          }|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        |           d |j        j        D             }|                     ddg|           d |j        D             }	|                     ddg|	           t                      }
|
                    |           d |
_        d!|
j        d"         _        t          j        |
d          }|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        |           d& |j        j        D             }|                     dd'g|           d( |j        D             }	|                     g d)|	           |                     dgd* |j        d"         j        D                        |                     g d+d, |j        d-         j        D                        |                     g d.d/ |j        d0         j        D                        d1S )2zETests error checking when the name of local function entries overlapsr   r   localdomainfnamerP   rQ   r  r   c                    t                      }| |_        ||_        |j                            |           |j                            |           |j                            |           |j                                       |S )N)r   r  r0   ro   extendrp   rq   opset_import)r  r  rP   rQ   r  fr  s         r   _make_functionzLTestComposeFunctions.test_overlapping_function_names.<locals>._make_function  ss     AAHAFGNN6"""HOOG$$$FMM%   N!!#&&&Hr   x0x1yf1)r  rP   rQ   r`   r   r   rd   r   ra   rf   )r  r  )r  r  r   r   )rN   rR   rS   c                     g | ]	}|j         
S r%   rh   r+   r   s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>H      111q111r   m1/f1zm2/f1c                     g | ]	}|j         
S r%   r   r+   r  s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>K      111QV111r   r\   f2r   y0y1zm3/c                     g | ]	}|j         
S r%   rh   r  s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>v  r  r   m3/f2c                     g | ]	}|j         
S r%   r   r  s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>y  r  r   )r  m3/f1r  c                     g | ]	}|j         
S r%   rh   r  s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>|  s    "J"J"J19"J"J"Jr   )rd   rf   rd   c                     g | ]	}|j         
S r%   rh   r  s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>~  s    #K#K#K!AI#K#K#Kr   r  )r  rf   rd   c                     g | ]	}|j         
S r%   rh   r  s     r   r-   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>  s    %M%M%MAai%M%M%Mr   r3   N)r   r   strr   r   r   r	   ro   r  r  r   r<   rp   rq   r   r   r0   r   	functionsr   r   r   rX   rV   rn   ri   )rY   r  r$  r`   rZ   ra   r[   r   r  r  r\   r]   r  s               @r   test_overlapping_function_namesz4TestComposeFunctions.test_overlapping_function_names  s   "2r**F,?,L,LM			 I	 #Y		
 	?	 	 	 	 	 	 	  "2r**F,?,L,LMLL	-dK4ErJJ-dK4ErJJ	
 	
 	
 	
-c;3DbII	
 	
 	

 	
d7D$<RUQVWWWX	
 	
 	
 \\
ArsKKK
4LE%eT4L3%PPPQ 
	
 
	
 
	
 	B\\
ArsKKK
4LE%eT4L3%PPPQ 
	
 
	
 
	
 	B K5ue
 
 
 	A11AGL111'7+U33311Q[111	'7+Y777\\
A!
rsKKK
4LE(d|dVTTT(d|dVTTT(d|cUSSS
 
 4LE( $PTv   (d|dVTTT(d|cUSSS 	
 	
 	
8 	B K5ue
 
 
 	A11AGL111'7+U33311Q[111	444i@@@%"J"Jak!n6I"J"J"JKKK!!!#K#Kq{1~7J#K#K#K	
 	
 	
 	###%M%MQ9L%M%M%M	
 	
 	
 	
 	
r   c                    t          j        dd          g}t                      }|j                            t          j        dt          j        g           g           |j                            t          j        dt          j        g           g           |j	                            t          j
        ddgdg          g           t                      }|                    |           d|_        t          j        |d|	          }t          j        |           t                      }|                    |           d
|_        |j                            t          j        dt          j        ddg          g           t          j        |d|	          }t          j        |           t                      }|                    |           d|_        |j                            t'          d          g           t          j        |d|	          }t          j        |           t                      }	|	                    |           d|	_        |	j                            t          j        dt          j        g           g           t          j        |	d|	          }
t          j        |
           t+          j        ||ddg          }|                     dt1          |j        j                             t+          j        ||ddg          }|                     dt1          |j        j                             t+          j        ||
ddg          }|                     dt1          |j        j                             dS )z;Tests automatic removal of initializers when merging graphsr   r   xr  re  r   r`   r   r   ra   r%   r   rr  r\   r   )r  r  )rR   rN   N)r   r   r	   ro   r  r  r   r<   rp   rq   r   r   r0   r   r   r   r   r;   r   rI   r"   r   rX   rn   r    rV   )rY   r  r$  r`   rZ   ra   r[   r\   r]   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**+LL	5c;;LbQQRSSS	6sK<MrRRSTTT	v'
C53%PPPQRRR\\
ArsKKKB\\
A
"(9!  	
 	
 	
 rsKKKB\\
A
$$&9#&>&>%?@@@rsKKKB\\
A
*30A2FFG	
 	
 	
 rsKKKB %b"eZLQQQC 899::: %b"eZLQQQC 899::: %b"eZLQQQC 78899999r   )NNNN)r   N)FFFFFFF)
rE  rF  rI  rJ  rM  rO  rP  rQ  rT  rU  )(__name__
__module____qualname__r  r   r   r   r	   r   r^   r   r   r   r   r   r   r   r   r   r   r   boolr  r	  r  r  r  r1  r3  r6  rD  r   r  r  r  r  r  r  r  r  r%   r   r   rK   rK   _   sw        '+'+!%!%!9 !9!9 !9 U38_%	!9
 %j*j%I4%OP!9 c#!9 $s)$!9 #!9 #!9 
!9 !9 !9 !9FL L L L L L L L"
 
 
 
& L  L  L  LD%
 %
 %
 %
N
 
 
 
6L L L L&S S S S(
 
 
 
*
 
 
 
>       * #"#$$)#(rJ rJrJ rJ 	rJ
 rJ "rJ !rJ rJ 
rJ rJ rJ rJh1 1 1 11 1 1 12 2 2 23 3 3 3+V +V +V +VZB B B B, , , ,& & & &: ".!-".".%1%1&8&8-M-MA* A*#A* #A* 3-	A*
 3-A* c]A* c]A* smA* smA* &c]A* &c]A* 
A* A* A* A*FQ Q Q QS S S S
 
 
 

 
 
 

 
 
 
G
 G
 G
 G
R6: 6: 6: 6: 6: 6:r   rK   __main__)"unittesttypingr   r   r   r   r   numpyr=   onnxr   r	   r
   r   r   r   r   r   r   r   r   r   r  r   r!   rB  r/   rI   r~   r   TestCaserK   r  mainr%   r   r   <module>r     s  
  < < < < < < < < < < < < < <                                s z    +c +c +c + + + +
> d3i    Qc Q&7 Q Q Q Q,

[: [: [: [: [:8, [: [: [:| zHMOOOOO r   