
    wi                        d dl mZ d dlZd dl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 erd dlmZmZ dd
ZddZddZddZdZdZ G d dej                  Zedk    r ej                      dS dS )    )annotationsN)TYPE_CHECKING)FunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProtoValueInfoProtocheckercomposehelperparserversion_converter)CallableSequencem_defstrreturnr   c                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     f/root/.openclaw/workspace/chatterbox_venv_py311/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_infor   	list[int]c                |      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_shaper0   *   sI       s:?6<@AABB   r   namer	   c                   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   )   r3      )g333333?g?g?_valuesF)r1   	data_typedimsvalsraw_idx)r   make_tensorr
   FLOATr    nparrayastypefloat32INT64int64make_sparse_tensor)r1   dense_shapelinear_indicessparse_valuesvalues_tensorindices_tensors         r   _make_sparse_tensorrJ   2   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)
        D0 = Mul(C0, C1)
    }
    c                  >   e Zd Z	 	 	 	 dOdPdZdQdZdQdZdQdZdQdZdQdZdQdZ	dQdZ
dQdZdQdZdQdZdQdZdQdZdQdZdQdZ	 	 	 	 	 	 	 dRdSd)ZdQd*ZdQd+ZdQd,ZdQd-ZdQd.ZdQd/ZdQd0ZdQd1Z	 	 	 	 	 	 	 	 	 	 dTdUdGZdQdHZdQdIZdQdJZdQdKZdQdLZ dQdMZ!dQdNZ"dS )VTestComposeFunctionsNm1defr   m2defio_maplist[tuple[str, str]]check_expectations4Callable[[GraphProto, GraphProto, GraphProto], None]inputslist[str] | Noneoutputsprefix1
str | Noneprefix2r   Nonec	           	     x   t          |          t          |          }
}	t          j        |	j        |
j        |||||          }t	          j        |            ||	j        |
j        |           t          j        |	|
|||||          }t	          j        |            ||	j        |
j        |j                   d S )N)rO   rS   rU   rV   rX   )r   r   merge_graphsgraphr   check_graphmerge_modelsr   )selfrM   rN   rO   rQ   rS   rU   rV   rX   m1m2g3m3s                r   _test_merge_modelsz'TestComposeFunctions._test_merge_modelsd   s     U##[%7%7B!HH
 
 
 	B28RXr222!
 
 
 	B28RXrx88888r   c                `     d
 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
        g1r   g2rb   r   rY   c                                         |j        | j                                        |j        |j                                        g dd |j        D                        d S )NAddSubMulrj   rk   rl   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rf   rg   rb   r_   s      r   rQ   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B21Nrf   r   rg   r   rb   r   r   rY   rd   M1_DEFM2_DEFr_   rQ   rO   s   `  r   'test_case_connect_all_no_name_collisionz<TestComposeFunctions.test_case_connect_all_no_name_collision   sP    
	 	 	 	 	 	 BAA8JKKKKKr   c                `     d
 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
        rf   r   rg   rb   r   rY   c                    ~                     |j        | j                                        g dd |j        D                                             g dd |j        D                        d S )N)r~   r   D0c                    g | ]	}|j         
S r&   r1   r,   elems     r   r.   zhTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations.<locals>.<listcomp>   s    3T3T3T$DI3T3T3Tr   ri   c                    g | ]	}|j         
S r&   rn   rp   s     r   r.   zhTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations.<locals>.<listcomp>   rr   r   rs   rx   s      r   rQ   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   rz   )r{   r   )r{   r   Nr   r   r   s   `  r   #test_case_connect_same_output_twicez8TestComposeFunctions.test_case_connect_same_output_twice   sP    
	 	 	 	 	 	 BAA8JKKKKKr   c                j     d 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
        rf   r   rg   rb   r   rY   c                    ~                     |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   )rj   rj   rk   rl   c                    g | ]	}|j         
S r&   rn   rp   s     r   r.   zoTestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations.<locals>.<listcomp>   s    .P.P.Pt|.P.P.Pr   rs   rx   s      r   rQ   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   )rU   Nr   r   )r_   rQ   rO   rU   s   `   r   *test_case_connect_same_output_drop_outputsz?TestComposeFunctions.test_case_connect_same_output_drop_outputs   si    
	 	 	 	 	 	 BAA&FF$6 	  	
 	
 	
 	
 	
r   c                R     d}d}dg}d 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   rf   r   rg   rb   r   rY   c                    ~ ~                     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   rt   ru   rv   rx   s      r   rQ   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   Nr   rd   )r_   m1_defm2_defrO   rQ   s   `    r   (test_case_connect_same_input_output_namez=TestComposeFunctions.test_case_connect_same_input_output_name   sa    		 	G 	G 	G 	G 	G 	G 	8JKKKKKr   c                d     d}d}dg}d 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B2rf   r   rg   rb   r   rY   c                    ~ ~                     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   rj   rk   c                    g | ]	}|j         
S r&   rn   r   s     r   r.   zbTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations.<locals>.<listcomp>       -O-O-Otdl-O-O-Or   rs   rx   s      r   rQ   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   rS   rU   Nr   r   )r_   r   r   rO   rQ   rS   rU   s   `      r   test_case_drop_inputs_outputsz2TestComposeFunctions.test_case_drop_inputs_outputs   s    

 	Q 	Q 	Q 	Q 	Q 	Q &FF$6vw 	  	
 	
 	
 	
 	
r   c                T     d}dg}d 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   rf   r   rg   rb   r   rY   c                    ~ ~                     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   rj   c                    g | ]	}|j         
S r&   rn   r   s     r   r.   zdTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations.<locals>.<listcomp>  r   r   rs   rx   s      r   rQ   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/)rV   rX   Nr   r   )r_   r   rO   rQ   s   `   r   test_case_name_collision_prefixz4TestComposeFunctions.test_case_name_collision_prefix  sn    	 	Q 	Q 	Q 	Q 	Q 	Q 	FF$6u 	  	
 	
 	
 	
 	
r   c                `     d 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
        rf   r   rg   g4r   rY   c                    ~ ~                     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   )rf   rg   r   r_   s      r   rQ   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   rz   r}   N)rf   r   rg   r   r   r   r   rY   r   r   s   `  r   -test_case_connect_partially_no_name_collisionzBTestComposeFunctions.test_case_connect_partially_no_name_collision  sT    	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p4rz   rO      )r   r   r3   v5)r   r   r   set_model_propsr   r   r^   r    metadata_propsassertRaises
ValueErrorr_   r`   ra   rO   rc   s        r   %test_merge_models_with_metadata_propsz:TestComposeFunctions.test_merge_models_with_metadata_props2  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_outnamer|   r}   r   r   ))r{   wrong_inputr}   r   N)r   r   r   r   r   r   r^   r_   r`   ra   s      r   "test_error_wrong_input_output_namez7TestComposeFunctions.test_error_wrong_input_output_nameF  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   r^   r   s      r   test_error_ir_version_mismatchz3TestComposeFunctions.test_error_ir_version_mismatch[  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   ry   r   N)r   r   r   r   
make_modelr\   make_opsetidr   r   r   r^   r   convert_versionr   r   r   s        r    test_error_opset_import_mismatchz5TestComposeFunctions.test_error_opset_import_mismatchz  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   c                   d}t          |          }d}t          j        ||ddd          }t          j        |j                   |j        j        D ]/}|                     |j        	                    |                     0|j        j
        D ]/}|                     |j        	                    |                     0dS )z)Tests prefixing inputs and outputs nodes.aB  
            <
                ir_version: 6,
                opset_import: [ "": 13]
            >
            agraph (float[N, 128] X, float[128, 10] W, float[10] B) => (float[N, 10] C)
            {
                T = MatMul(X, W)
                S = Add(T, B)
                C = Softmax(S)
            }
            pre_Tmodelr   rename_inputsrename_outputsrename_edgesNr   r   
add_prefixr   r]   r\   ru   
assertTruer1   
startswithrv   )r_   input_graphinput_modelr   prefixed_modelios          r   !test_add_prefix_to_inputs_outputsz6TestComposeFunctions.test_add_prefix_to_inputs_outputs  s     "+.. +
 
 
 	N0111%+ 	7 	7AOOAF--f556666%, 	7 	7AOOAF--f556666	7 	7r   c                8   d}t          |          }d}t          j        ||ddd          }t          j        |j                   |                     |j        j        |j        j                   |                     |j        j        |j        j                   dS )zWTests prefixing input and output nodes, when renaming of inputs/outputs is deactivated.z
            <
                ir_version: 6,
                opset_import: [ "": 13]
            >
            agraph (float[2,2] A) => (float[2,2] B)
            {
            B = Identity(A)
            }
            r   FTr   N)	r   r   r   r   r]   r\   rt   ru   rv   )r_   r   r   r   r   s        r   !test_add_prefix_wo_inputs_outputsz6TestComposeFunctions.test_add_prefix_wo_inputs_outputs  s    	 "+.. + 
 
 
 	N0111*0.2F2LMMM*1>3G3NOOOOOr   c                   d}t          |          }d}t          j        ||ddd          }t          j        |j                   |j        j        D ]/}|                     |j        	                    |                     0|j        j
        D ]/}|                     |j        	                    |                     0dS )z,Tests prefixing of graphs with loose inputs.a  
            <
                ir_version: 7,
                opset_import: [ "" : 13 ]
            >
            agraph (bool b, float[128] X, float[128] Y, bool unused_input) => (float[128] Z)
            {
            Z = If (b) <
                then_branch = g1 () => (float[128] z_then) { z_then = Add(X, X) },
                else_branch = g2 () => (float[128] z_else) { z_else = Sub(Y, Y) }
                >
            }
            r   Tr   Nr   )r_   r   r   r   r   r   graph_outputs          r   )test_add_prefix_with_loose_inputs_outputsz>TestComposeFunctions.test_add_prefix_with_loose_inputs_outputs  s     "+.. +
 
 
 	N0111  %+ 	7 	7AOOAF--f556666*07 	B 	BLOOL-88@@AAAA	B 	Br   Frename_nodesboolr   r   r   rename_initializersrename_value_infosinplacec                
   t          t                    }d}	|rAt                      }
|
                    |           t	          j        |
|	||||||d	  	         nt	          j        ||	||||||          }
|j        }|
j        }|s	|s|s|s|rni }|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          D ]\  }}t!          |j        |j        d          D ]/\  }}|                     |                    ||          |           0t!          |j
        |j
        d          D ]/\  }}|                     |                    ||          |           0t!          |j        |j        d          D ]>\  }}|                     |                    |j        |j                  |j                   ?t!          |j
        |j
        d          D ]>\  }}|                     |                    |j        |j                  |j                   ?t!          |j        |j        d          D ]>\  }}|                     |                    |j        |j                  |j                   ?t!          |j        |j        d          D ]\  }}|                     |                    |j        j        |j        j                  |j        j                   |                     |                    |j        j        |j        j                  |j        j                   t!          |j        |j        d          D ]>\  } }!|                     |                    |!j        |!j                  | j                   ?|rRt!          |j        |j        d          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   strict)r   r   r   CopyFromr   r   r\   rw   ru   r!   rv   r1   initializersparse_initializervaluesindicesziprt   getr"   )"r_   r   r   r   r   r   r   r  r`   r   ra   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$)DAAA C CB!"(BHTBBB C CFB$$\%5%5b"%=%=rBBBB!")RYtDDD C CFB$$\%5%5b"%=%=rBBBBCek4:dCCC N NB  !1!1"'27!C!CRWMMMMelDKEEE N NB  !1!1"'27!C!CRWMMMM #E$5t7GPT U U U W Wu  !1!1%*ej!I!I5:VVVV.1($*A$/ / /  *l    $$$+0,2E2J  !',	      $$$,1<3G3L  !(-	      0$/$OOO Q QS  !1!1#(CH!E!EsxPPPP J!%*diEEE 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!  r_   s    r   test_add_prefix_nodesz*TestComposeFunctions.test_add_prefix_nodesZ  s    400000r   c                6    |                      ddd           dS )z]Tests prefixing nodes edges. This will also rename inputs/outputs, since the names are sharedT)r   r   r   Nr#  r$  s    r   test_add_prefix_edgesz*TestComposeFunctions.test_add_prefix_edges^  s0    T$ 	 	
 	
 	
 	
 	
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_inputsd  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_outputsh  s    T2222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          D ]\  }}|                     t          ||j                  |j                   t          |j        |j        d          D ]\  }}|j        rt          |j        j        |j        j        d          D ]\  }}t          |j        |j        d          D ])\  }}|                     t          ||          |           *t          |j        |j        d          D ])\  }}|                     t          ||          |           *ŐdS )zQTests prefixing attribute's subgraph. Relevant subgraph should be renamed as wellr      XNYZOutrl   XYr   rj   rk   Ifthen)nodesr1   rS   rU   else)rS   rU   then_branchelse_branchr\   zprefix.Tr  )r   make_tensor_value_infor
   BOOLr=   	make_node
make_graphr   add_prefix_graphr   r]   r
  rw   rt   r!   r1   	attributegru   rv   )r_   r   r.  r/  r0  r1  r2  addsubcondr\   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_subgraphl  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$GGG 	V 	VFBYvrw77AAA*-blBLQU*V*V*V V V&
J< V47"):<+<T5 5 5 
V 
V0[ 36'-{/@3 3 3 T T.Hh !,,Yvx-H-H(SSSS47'.0B45 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                    t          t                    }d 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.
        r`   r   ra   dim_idxintr   rY   c                   t          |j        j        | j        j        d          D ]\  }}                    |j        |j                                       |j        j        j        |j        j        j                   t          |          }|	                    |d                               t          |          |           d S )NTr  r-  )
r
  r\   rv   rt   r1   r'   r(   	elem_typer0   insert)r`   ra   rS  out_g2out_g1expected_out_shaper_   s         r   _check_modelz>TestComposeFunctions.test_expand_out_dim.<locals>._check_model  s    "%bhorxt"T"T"T I I  fk:::  K+5v{7N7X   &0%7%7""))'1555  F!3!35GHHHHI Ir   )r   r4   r   TrP  N)r`   r   ra   r   rS  rT  r   rY   )r   r   r   expand_out_dimr   r  )r_   r`   r[  rS  ra   s   `    r   test_expand_out_dimz(TestComposeFunctions.test_expand_out_dim  s       	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Sequence[str]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,   r   rs  rv  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  H     
 
 
 ZxPQ{mTTT
 
 
r   c                \    g | ](}t          j        |         t          j        g           )S r&   r   r9  r
   r=   )r,   r   rs  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  ?     
 
 
 )'!*k6GLL
 
 
r   c                \    g | ](}t          j        |         t          j        g           )S r&   r  )r,   r   rv  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  ?     
 
 
 )(1+{7H"MM
 
 
r   c                \    g | ](}t          j        |         t          j        g           )S r&   r  )r,   r   rx  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  ?     
 
 
 )+a.+:KRPP
 
 
r   c                b    g | ]+}t          j        |         t          j        d dg          ,S r&   r-  r1   r7   r8   r9   r   r<   r
   rB   )r,   r   rz  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  Q     
 
 
  !!_0ARSQT  
 
 
r   c                :    g | ]}t          |                   S r&   rJ   )r,   r   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 r  r  )r,   r   ru  rw  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  r  r   c                \    g | ](}t          j        |         t          j        g           )S r&   r  )r,   r   ru  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  r  r   c                \    g | ](}t          j        |         t          j        g           )S r&   r  )r,   r   rw  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  r  r   c                \    g | ](}t          j        |         t          j        g           )S r&   r  )r,   r   ry  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  r  r   c                b    g | ]+}t          j        |         t          j        d dg          ,S r  r  )r,   r   r{  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  r  r   c                :    g | ]}t          |                   S r&   r  )r,   r   r}  s     r   r.   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>  r  r   r   r   g0)r5  r1   rS   rU   r"   r  r  r   r   rf   r   edger-  r"   r  r6   r;   r  zg0/)r   )r/   r    r   r   r   r<  r   check_overlapping_namesr\   listsetrt   appendr   )%r_   rs  ru  rv  rw  rx  ry  rz  r{  r|  r}  r  r  r  r   r  r  r  r  r  r  r  r  opsm0r`   overlapr   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  rb  )rs  ru  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  rf  )rv  rw  Nr  r$  s    r   test_overlapping_output_namesz2TestComposeFunctions.test_overlapping_output_namesC  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)rx  ry  Nr  r$  s    r   !test_overlapping_value_info_namesz6TestComposeFunctions.test_overlapping_value_info_namesG  s6    $$UEN 	% 	
 	
 	
 	
 	
r   c                <    |                      ddgddg           dS )zBTests error checking when the name of initializer entries overlapsr  r  rm  )rz  r{  Nr  r$  s    r   "test_overlapping_initializer_namesz7TestComposeFunctions.test_overlapping_initializer_namesM  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  rq  )r|  r}  Nr  r$  s    r   )test_overlapping_sparse_initializer_namesz>TestComposeFunctions.test_overlapping_sparse_initializer_namesS  s9    $$!/ @!/ @ 	% 	
 	
 	
 	
 	
r   c                |
   t          j        dd          t          j        dd          gd6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/d0 |j        d1         j	        D                        |                     g d2d3 |j        d4         j	        D                        d5S )7zETests error checking when the name of local function entries overlapsr   r   localdomainr   fnamerS   	list[str]rU   r5  list[NodeProto]r   r   c                   t                      }| |_        ||_        |j                            |           |j                            |           |j                            |           |j                                       |S )N)r   r  r1   ru   extendrv   rw   opset_import)r  r  rS   rU   r5  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  rS   rU   rf   r   r   rj   r   rg   rl   )r  r  )r  r  r   r   )rO   rV   rX   c                    g | ]	}|j         
S r&   rn   r,   r  s     r   r.   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>      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>      111QV111r   rb   f2r   y0y1zm3/c                    g | ]	}|j         
S r&   rn   r  s     r   r.   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>  r  r   m3/f2c                    g | ]	}|j         
S r&   r   r  s     r   r.   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>  r  r   )r  m3/f1r  c                    g | ]	}|j         
S r&   rn   r  s     r   r.   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>  s    "J"J"J19"J"J"Jr   )rj   rl   rj   c                    g | ]	}|j         
S r&   rn   r  s     r   r.   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>  s    #K#K#K!AI#K#K#Kr   r-  )r  rl   rj   c                    g | ]	}|j         
S r&   rn   r  s     r   r.   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>  s    %M%M%MAai%M%M%Mr   r4   N)r  r   r  r   rS   r  rU   r  r5  r  r   r   )r   r   r   ru   r  r9  r
   r=   rv   rw   r;  r  r1   r   	functionsr   r   r   r^   r\   rt   ro   )r_   r  r?  rf   r`   rg   ra   r   r5  r  rb   rc   r  s               @r   test_overlapping_function_namesz4TestComposeFunctions.test_overlapping_function_namesZ  s   "2r**F,?,L,LM	 	 	 	 	 	  "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  r  r   rf   r   r   rg   r&   r   r  rb   r   )r  r  )rV   rO   N)r   r   r   ru   r  r9  r
   r=   rv   rw   r;  r  r1   r   r   r   r  r<   r  rJ   r"   r   r^   rt   r    r\   )r_   r  r?  rf   r`   rg   ra   rb   rc   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)rM   r   rN   r   rO   rP   rQ   rR   rS   rT   rU   rT   rV   rW   rX   rW   r   rY   )r   rY   )FFFFFFF)r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   rY   )
r`  ra  rd  re  rh  rj  rk  rl  ro  rp  )rs  rt  ru  rt  rv  rt  rw  rt  rx  rt  ry  rt  rz  rt  r{  rt  r|  rt  r}  rt  r   rY   )#__name__
__module____qualname__rd   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!  r%  r'  r)  r+  rL  rN  rQ  r_  r  r  r  r  r  r  r  r  r&   r   r   rL   rL   c   s/        $($(""!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(
 
 
 
*
 
 
 
>       $7 7 7 7>P P P P4B B B BF #"#$$)#(rJ rJ rJ rJ rJh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* A*FQ Q Q QS S S S
 
 
 

 
 
 

 
 
 
G
 G
 G
 G
R6: 6: 6: 6: 6: 6:r   rL   __main__)r   r   r   r   )r   r   r   r   r   r   )r"   r   r   r#   )r1   r   r   r	   )!
__future__r   unittesttypingr   numpyr>   onnxr   r   r   r   r	   r
   r   r   r   r   r   r   collections.abcr   r   r   r!   r0   rJ   r   r   TestCaserL   r  mainr&   r   r   <module>r     s   # " " " " "                                              322222222   + + + +
   Q Q Q Q,

v: v: v: v: v:8, v: v: v:r zHMOOOOO r   