o
    toi                     @  s   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
dZdddZd ddZd!ddZdZdZG dd dejZedkree   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                 C  s   t | }t| |S )zYParses a model from a string representation, including checking the model for correctness)r   parse_modelr   check_model)r   m r   J/home/ubuntu/.local/lib/python3.10/site-packages/onnx/test/compose_test.py_load_model   s   

r   prefixsc                 C  s   t |dkr
| | S |S )z Prefixes a string (if not empty)r   )len)r   r   r   r   r   	_prefixed%      r   
value_infor
   	list[int]c                   s"    fddt t jjjjD S )zPReturns a list of integers representing the shape of the provided ValueInfoProtoc                   s   g | ]} j jjj| jqS r   )typetensor_typeshapedim	dim_value).0dr    r   r   
<listcomp>,   s    z_get_shape.<locals>.<listcomp>)ranger   r"   r#   r$   r%   r)   r   r)   r   
_get_shape*   s   
r,   namer   c                 C  s   ddg}g d}g d}t j| d tjt|gt|tjdd}t j| d tj	t|gt|tj
dd}t |||S )N   )   r.      )g333333?g?g?_valuesF)r-   	data_typedimsvalsraw_idx)r   make_tensorr	   FLOATr   nparrayastypefloat32INT64int64make_sparse_tensor)r-   dense_shapelinear_indicessparse_valuesvalues_tensorindices_tensorr   r   r   _make_sparse_tensor2   s$   rE   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                   @  s|  e Zd Z				dodpddZdqddZdqddZdqddZdqddZdqddZdqddZ	dqdd Z
dqd!d"Zdqd#d$Zdqd%d&Zdqd'd(Zdqd)d*Zdqd+d,Zdqd-d.Z	/	/	/	/	/	/	/drdsd8d9Zdqd:d;Zdqd<d=Zdqd>d?Zdqd@dAZdqdBdCZdqdDdEZdqdFdGZdqdHdIZ	J	K	L	M	N	O	P	Q	R	Sdtdud_d`ZdqdadbZdqdcddZdqdedfZdqdgdhZdqdidjZ dqdkdlZ!dqdmdn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	              	   C  s   t |t |}	}
tj|	j|
j|||||d}t| ||	j|
j| tj|	|
|||||d}t| ||	j|
j|j d S )N)rI   rM   rO   rP   rR   )r   r   merge_graphsgraphr   check_graphmerge_modelsr   )selfrG   rH   rI   rK   rM   rO   rP   rR   m1m2g3m3r   r   r   _test_merge_modelsd   s.   
	
	z'TestComposeFunctions._test_merge_modelsc                   *   d fd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   g2r[   r   rS   c                   s@     |j| j   |j|j   g ddd |jD  d S )NAddSubMulrb   rc   rd   c                 S     g | ]}|j qS r   op_typer'   itemr   r   r   r*          zlTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>assertEqualinputoutputnoder_   r`   r[   rX   r   r   rK      s   zXTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectationsB00B01B10B11B20B21Nr_   r   r`   r   r[   r   r   rS   r]   M1_DEFM2_DEFrX   rK   rI   r   rq   r   'test_case_connect_all_no_name_collision   s   z<TestComposeFunctions.test_case_connect_all_no_name_collisionc                   r^   )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_   r   r`   r[   r   rS   c                   sN   ~  |j| j   g ddd |jD    g ddd |jD  d S )N)rw   rz   D0c                 S  re   r   r-   r'   elemr   r   r   r*      rj   zhTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations.<locals>.<listcomp>ra   c                 S  re   r   rf   rh   r   r   r   r*      rj   rk   rp   rq   r   r   rK      s   zTTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectationsrs   )rt   rx   )rt   r{   Nr|   r}   r   r   rq   r   #test_case_connect_same_output_twice   s   	z8TestComposeFunctions.test_case_connect_same_output_twicec                   s4   d fdd}g d	}d
g} j tt|||d 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_   r   r`   r[   r   rS   c                   sL   ~  |j| j   dgdd |jD    g ddd |jD  d S )Nr   c                 S  re   r   r   r   r   r   r   r*      rj   zoTestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations.<locals>.<listcomp>)rb   rb   rc   rd   c                 S  re   r   rf   rh   r   r   r   r*      rj   rk   rp   rq   r   r   rK      s   z[TestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectationsr   r   )rO   Nr|   r}   )rX   rK   rI   rO   r   rq   r   *test_case_connect_same_output_drop_outputs   s   

z?TestComposeFunctions.test_case_connect_same_output_drop_outputsc                   s0   d}d}dg}d fd
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_   r   r`   r[   r   rS   c                   s<   ~ ~  dgdd |jD    dgdd |jD  d S )NAc                 S  re   r   r   r   r   r   r   r*      rj   zmTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectations.<locals>.<listcomp>Cc                 S  re   r   r   r   r   r   r   r*      rj   rl   rm   rn   rp   rq   r   r   rK      s   zYTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectationsNr|   r]   )rX   m1_defm2_defrI   rK   r   rq   r   (test_case_connect_same_input_output_name   s
   

z=TestComposeFunctions.test_case_connect_same_input_output_namec                   sB   d}d}dg}d fd
d}dg}dg} j ||||||d 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_   r   r`   r[   r   rS   c                   sX   ~ ~  dgdd |jD    dgdd |jD    ddgdd |jD  d S )	NA0c                 S  re   r   r   r   r   r   r   r*      rj   zbTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations.<locals>.<listcomp>B3c                 S  re   r   r   r   r   r   r   r*      rj   rb   rc   c                 S  re   r   rf   r   r   r   r   r*      rj   rk   rp   rq   r   r   rK      s    zNTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectationsr   r   rM   rO   Nr|   r   )rX   r   r   rI   rK   rM   rO   r   rq   r   test_case_drop_inputs_outputs   s   
z2TestComposeFunctions.test_case_drop_inputs_outputsc                   s2   d}dg}d fd	d
} j ||||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_   r   r`   r[   r   rS   c                   sZ   ~ ~  g ddd |jD    dgdd |jD    ddgdd |jD  d S )N)zm1/Azm1/Bzm2/Bc                 S  re   r   r   r   r   r   r   r*     rj   zdTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations.<locals>.<listcomp>zm2/Cc                 S  re   r   r   r   r   r   r   r*     rj   rb   c                 S  re   r   rf   r   r   r   r   r*     rj   rk   rp   rq   r   r   rK     s    zPTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectationsm1/m2/)rP   rR   Nr|   r   )rX   r   rI   rK   r   rq   r   test_case_name_collision_prefix  s   

z4TestComposeFunctions.test_case_name_collision_prefixc                   s*   d fd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_   r   r`   g4r   rS   c                   s@   ~ ~  g ddd |jD    ddgdd |jD  d S )N)r   r   _Ar{   c                 S  re   r   r   r   r   r   r   r*   +  rj   zrTestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>rz   r   c                 S  re   r   r   r   r   r   r   r*   -  rj   r   )r_   r`   r   rq   r   r   rK   %  s
    z^TestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectationsrs   rv   N)r_   r   r`   r   r   r   r   rS   r}   r   r   rq   r   -test_case_connect_partially_no_name_collision  s   
zBTestComposeFunctions.test_case_connect_partially_no_name_collisionc                 C  s   t t}t|ddd t t}t|ddd dg}tj|||d}t|jd	ks.J t|ddd
 tj|||d}t|jdksHJ t|ddd
 | j	t
tj|||d d S )Nv1v2)p1p2v3v4)p3p4rs   rI      )r   r   r.   v5)r   r~   r   set_model_propsr   r   rW   r   metadata_propsassertRaises
ValueErrorrX   rY   rZ   rI   r\   r   r   r   %test_merge_models_with_metadata_props2  s   z:TestComposeFunctions.test_merge_models_with_metadata_propsc                 C  sJ   t tt t}}| jttj||g dd | jttj||g dd dS )z_Tests that providing a non existing output/input name in the io_map argument produces an error.))wrong_outnameru   rv   ry   r   ))rt   wrong_inputrv   ry   N)r   r~   r   r   r   r   rW   rX   rY   rZ   r   r   r   "test_error_wrong_input_output_nameF  s   	
z7TestComposeFunctions.test_error_wrong_input_output_namec                 C  s,   t d}t d}| jttj||dgd 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   rW   r   r   r   r   test_error_ir_version_mismatch[  s   
z3TestComposeFunctions.test_error_ir_version_mismatchc                 C  s   t tt t}}tj|jdtddgd}tj|jdtddgd}g d}| tt	j
||| t|d}t	j
|||d}t| dS )	zSTests that providing models with different operator set imported produces an error.test 
   producer_nameopset_imports   rr   r   N)r   r~   r   r   
make_modelrU   make_opsetidr   r   r   rW   r   convert_versionr   r   r   r   r   r    test_error_opset_import_mismatchz  s   z5TestComposeFunctions.test_error_opset_import_mismatchc                 C  t   d}t |}d}tj||dddd}t|j |jjD ]}| |j	| q|jj
D ]}| |j	| q,d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   rV   rU   rm   
assertTruer-   
startswithrn   )rX   input_graphinput_modelr   prefixed_modelior   r   r   !test_add_prefix_to_inputs_outputs  s    z6TestComposeFunctions.test_add_prefix_to_inputs_outputsc                 C  s\   d}t |}d}tj||dddd}t|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   rV   rU   rl   rm   rn   )rX   r   r   r   r   r   r   r   !test_add_prefix_wo_inputs_outputs  s   
z6TestComposeFunctions.test_add_prefix_wo_inputs_outputsc                 C  r   )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   )rX   r   r   r   r   r   graph_outputr   r   r   )test_add_prefix_with_loose_inputs_outputs  s    z>TestComposeFunctions.test_add_prefix_with_loose_inputs_outputsFrename_nodesboolr   r   r   rename_initializersrename_value_infosinplacec           "      C  s  t t}d}	|rt }
|
| tj|
|	||||||dd	 ntj||	||||||d}
|j}|
j}|s=|s=|s=|s=|ri }|ra|jD ]}|jD ]	}t	|	|||< qI|j
D ]	}t	|	|||< qVqD|rr|jD ]}t	|	|j||j< qf|r|j
D ]}t	|	|j||j< qw|r|jD ]}t	|	|j||j< q|jD ]}t	|	|jj||jj< t	|	|jj||jj< q|r|j
D ]}t	|	|j||j< qt|j|jddD ]4\}}t|j|jddD ]\}}| |||| qt|j
|j
ddD ]\}}| |||| qqt|j|jddD ]\}}| ||j|j|j qt|j
|j
ddD ]\}}| ||j|j|j q$t|j|jddD ]\}}| ||j|j|j q@t|j|jddD ]%\}}| ||jj|jj|jj | ||jj|jj|jj q\t|j|jddD ]\} }!| ||!j|!j| j q|rt|j|jddD ]\}}| t	|	|j|j qd 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   rU   ro   rm   r   rn   r-   initializersparse_initializervaluesindicesziprl   getr    )"rX   r   r   r   r   r   r   r   rY   r   rZ   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vi0r   r   r   _test_add_prefix  s   











Fz%TestComposeFunctions._test_add_prefixc                 C     | j dd dS )zTests renaming nodes onlyT)r   Nr   rq   r   r   r   test_add_prefix_nodesZ     z*TestComposeFunctions.test_add_prefix_nodesc                 C  s   | j d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   rq   r   r   r   test_add_prefix_edges^  s   
z*TestComposeFunctions.test_add_prefix_edgesc                 C  r   )zPTests prefixing graph inputs only. Relevant node edges should be renamed as wellT)r   Nr   rq   r   r   r   test_add_prefix_inputsd  r   z+TestComposeFunctions.test_add_prefix_inputsc                 C  r   )zQTests prefixing graph outputs only. Relevant node edges should be renamed as wellT)r   Nr   rq   r   r   r   test_add_prefix_outputsh  r   z,TestComposeFunctions.test_add_prefix_outputsc                 C  s  t dtjdg}t dtjddg}t dtjddg}t dtjddg}t dtjddg}t jdddgd	gd
}t jdd	dgdgd
}t jdd	dgdgd
}t jddgdgt j|gdg |gdt j|gdg |gdd}	t j||	gd||||g|gd}
d}t|
|}t	
| t|j|
jddD ]^\}}| t||j|j t|j|jddD ]E\}}|jrt|jj|jjddD ]2\}}t|j|jddD ]\}}| t||| qt|j|jddD ]\}}| t||| qqqqdS )zQTests prefixing attribute's subgraph. Relevant subgraph should be renamed as wellr      XNYZOutrd   XYr   rb   rc   Ifthen)nodesr-   rM   rO   else)rM   rO   then_branchelse_branchrU   zprefix.Tr   )r   make_tensor_value_infor	   BOOLr8   	make_node
make_graphr   add_prefix_graphr   rV   r   ro   rl   r   r-   	attributegrm   rn   )rX   r   r   r  r  r  r  addsubcondrU   r   prefixed_graphr   r   
attribute1
attribute0subgraph_n1subgraph_n0input_n1input_n0	output_n1	output_n0r   r   r   "test_add_prefix_attribute_subgraphl  sX   


z7TestComposeFunctions.test_add_prefix_attribute_subgraphc                 C  s   |  dddddd dS )z&Tests prefixing all names in the graphTNr   rq   r   r   r   test_add_prefix_all  r   z(TestComposeFunctions.test_add_prefix_allc                 C  r   )zTests prefixing inplaceTr   Nr   rq   r   r   r   test_add_prefix_inplace  r   z,TestComposeFunctions.test_add_prefix_inplacec                   sl   t t}d fdd	}d
D ]}t||}|||| qt }|| d}tj||d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.
        rY   r   rZ   dim_idxintr   rS   c                   sp   t |jj| jjddD ]*\}} |j|j  |jjj|jjj t|}|	|d  t|| qd S )NTr   r   )
r   rU   rn   rl   r-   r"   r#   	elem_typer,   insert)rY   rZ   r"  out_g2out_g1expected_out_shaperq   r   r   _check_model  s   z>TestComposeFunctions.test_expand_out_dim.<locals>._check_model)r   r/   r   Tr   N)rY   r   rZ   r   r"  r#  r   rS   )r   r~   r   expand_out_dimr   r   )rX   rY   r)  r"  rZ   r   rq   r   test_expand_out_dim  s   

z(TestComposeFunctions.test_expand_out_dim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           %   
     s  fddt tD }fddt tD }fddt tD }fddt tD } fddt t D }fddt tD }fddt tD }fd	dt tD }fd
dt tD }	fddt t	D }fddt tD }fddt tD }tddg}tjtj|d|||||dd|d}tjtj|d|||||dd|d}t|j|j}d}t	t
t
@ }t	t
t
@ }t	t
|| }|r| || d|f |d7 }t	t
t
	@ }|r| || d|f |d7 }t	t
 t
@ } | r5| || d| f |d7 }t	t
t
@ }!|!rfg }"|!D ]}#|"|#d  |"|#d  qF| || d|"f |d7 }tj|dd}$t|$j|j}| dt| d S )Nc                   (   g | ]}t jd  | g| gdqS Identityr   r   r  r'   r   )rA  rD  r   r   r*         z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>c                       g | ]}t  | tjg qS r   r   r  r	   r8   rP  )rA  r   r   r*         c                   rR  r   rS  rP  )rD  r   r   r*     rT  c                   rR  r   rS  rP  )rF  r   r   r*     rT  c                   &   g | ]}t j | tjd dgdqS r   r   r-   r2   r3   r4   r   r7   r	   r=   rP  )rH  r   r   r*         c                      g | ]}t  | qS r   rE   rP  )rJ  r   r   r*         
c                   rL  rM  rO  rP  )rC  rE  r   r   r*     rQ  c                   rR  r   rS  rP  )rC  r   r   r*     rT  c                   rR  r   rS  rP  )rE  r   r   r*     rT  c                   rR  r   rS  rP  )rG  r   r   r*     rT  c                   rU  rV  rX  rP  )rI  r   r   r*     rY  c                   rZ  r   r[  rP  )rK  r   r   r*     r\  r   r   g0)r  r-   rM   rO   r    r   r   r   r   r_   r   edger   r    r   r1   r6   r   zg0/)r   )r+   r   r   r   r   r  r   check_overlapping_namesrU   listsetrl   appendr   )%rX   rA  rC  rD  rE  rF  rG  rH  rI  rJ  rK  r   r   r   r   r   r   r   r   r   r   r   r   opsm0rY   overlapr   overlapping_inputsoverlapping_outputsoverlapping_edgesoverlapping_visoverlapping_initoverlapping_sparse_initexpected_overlapoverlapping_namem0_newr   )
rH  rI  rA  rC  rD  rE  rJ  rK  rF  rG  r   _test_overlapping_names  s   





















		z,TestComposeFunctions._test_overlapping_namesc                 C     | j ddgddgd dS )z9Tests error checking when the name of the inputs overlapsr   r   r0  )rA  rC  Nro  rq   r   r   r   test_overlapping_input_names?     z1TestComposeFunctions.test_overlapping_input_namesc                 C  rp  )z9Tests error checking when the name of the output overlapsr   r   r4  )rD  rE  Nrq  rq   r   r   r   test_overlapping_output_namesC  rs  z2TestComposeFunctions.test_overlapping_output_namesc                 C  rp  )zATests error checking when the name of value_info entries overlapsr   r   vi2)rF  rG  Nrq  rq   r   r   r   !test_overlapping_value_info_namesG     
z6TestComposeFunctions.test_overlapping_value_info_namesc                 C  rp  )zBTests error checking when the name of initializer entries overlapsr   r   r;  )rH  rI  Nrq  rq   r   r   r   "test_overlapping_initializer_namesM  rw  z7TestComposeFunctions.test_overlapping_initializer_namesc                 C  rp  )zITests error checking when the name of sparse_initializer entries overlapsr   r   r?  )rJ  rK  Nrq  rq   r   r   r   )test_overlapping_sparse_initializer_namesS  s   
z>TestComposeFunctions.test_overlapping_sparse_initializer_namesc                   s  t ddt ddg d8 fdd}t ddt ddg t }|jt dtjg t dtjg g |jt dtjg g |j	t j
ddddgdgdg t }|| d|_t j|d d}|j|ddddgdgt j
dddgdgdgg t| t }|| d|_t j|d d}|j|ddddgdgt j
dddgdgdgg t| tj||ddgddd }t| d!d" |jj	D }| d#d$g| d%d" |jD }	| d#d$g|	 t }
|
| d&|
_d'|
j	d( _t j|
d d}|j|ddddgdgt j
dddgd)gdt j
dddgd*gdt j
dd)d*gdgdg|dd'ddgdgt j
ddddgd)gdt j
dddgd*gdt j
dd)d*gdgdgg t| tj||ddgdd+d }t| d,d" |jj	D }| d#d-g| d.d" |jD }	| g d/|	 | dgd0d" |jd( j	D  | g d1d2d" |jd3 j	D  | g d4d5d" |jd6 j	D  d7S )9zETests error checking when the name of local function entries overlapsr   r   localdomainr   fnamerM   	list[str]rO   r  list[NodeProto]r   r   c                   sF   t  }| |_||_|j| |j| |j| |j  |S )N)r   r{  r-   rm   extendrn   ro   opset_import)r{  r|  rM   rO   r  frc  r   r   _make_function^  s   zLTestComposeFunctions.test_overlapping_function_names.<locals>._make_functionx0x1yf1)r{  rM   rO   r_   r   r   rb   r   r`   rd   )r  r  )r  r  r   r   )rI   rP   rR   c                 S  re   r   rf   r'   r   r   r   r   r*     rj   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>m1/f1zm2/f1c                 S  re   r   r   r'   r  r   r   r   r*     rj   r[   f2r   y0y1zm3/c                 S  re   r   rf   r  r   r   r   r*     rj   m3/f2c                 S  re   r   r   r  r   r   r   r*     rj   )r  m3/f1r  c                 S  re   r   rf   r  r   r   r   r*     rj   )rb   rd   rb   c                 S  re   r   rf   r  r   r   r   r*     rj   r   )r  rd   rb   c                 S  re   r   rf   r  r   r   r   r*     rj   r/   N)r{  r   r|  r   rM   r}  rO   r}  r  r~  r   r   )r   r   r   rm   r  r  r	   r8   rn   ro   r  r   r-   r   	functionsr   r   r   rW   rU   rl   rg   )rX   r  r  r_   rY   r`   rZ   r   r  r  r[   r\   r   r  r   test_overlapping_function_namesZ  s   







 z4TestComposeFunctions.test_overlapping_function_namesc                 C  s  t ddg}t }|jt dtjg g |jt dtjg g |j	t j
ddgdgdg t }|| d|_t j|d|d	}t| t }|| d
|_|jt jdtjddgdg t j|d|d	}t| t }|| d|_|jtdg t j|d|d	}t| t }	|	| d|	_|	jt dtjg g t j|	d|d	}
t|
 tj||ddgd}| dt|jj tj||ddgd}| dt|jj tj||
ddgd}| dt|jj dS )z;Tests automatic removal of initializers when merging graphsr   r   xr  rN  r   r_   r   r   r`   r   r   rW  r[   r   )r  r  )rP   rI   N)r   r   r   rm   r  r  r	   r8   rn   ro   r  r   r-   r   r   r   r   r7   r   rE   r    r   rW   rl   r   rU   )rX   rc  r  r_   rY   r`   rZ   r[   r\   r   m4out_m1out_m2out_m3r   r   r   7test_merge_drop_unnecessary_initializers_and_value_info  sR   







zLTestComposeFunctions.test_merge_drop_unnecessary_initializers_and_value_info)NNNN)rG   r   rH   r   rI   rJ   rK   rL   rM   rN   rO   rN   rP   rQ   rR   rQ   r   rS   )r   rS   )FFFFFFF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rS   )
r.  r/  r2  r3  r6  r8  r9  r:  r=  r>  )rA  rB  rC  rB  rD  rB  rE  rB  rF  rB  rG  rB  rH  rB  rI  rB  rJ  rB  rK  rB  r   rS   )#__name__
__module____qualname__r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r!  r-  ro  rr  rt  rv  rx  ry  r  r  r   r   r   r   rF   c   sp    
#



"
'







#
t




-

 





 
rF   __main__)r   r   r   r   )r   r   r   r   r   r   )r    r
   r   r!   )r-   r   r   r   )!
__future__r   unittesttypingr   numpyr9   onnxr   r   r   r   r   r	   r
   r   r   r   r   r   collections.abcr   r   r   r   r,   rE   r~   r   TestCaserF   r  mainr   r   r   r   <module>   s0   8



       @