o
    wi                     @  s   d dl mZ d dlZd dlmZmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ er:d dl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Callable)FunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProtoValueInfoProtocheckercomposehelperparserversion_converter)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   S/home/ubuntu/sommelier/.venv/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				didjddZdkddZdkddZdkddZdkddZdkddZdkddZ	dkdd Z
dkd!d"Zdkd#d$Zdkd%d&Zdkd'd(Z	)	)	)	)	)	)	)dldmd2d3Zdkd4d5Zdkd6d7Zdkd8d9Zdkd:d;Zdkd<d=Zdkd>d?Zdkd@dAZdkdBdCZ	D	E	F	G	H	I	J	K	L	MdndodYdZZdkd[d\Zdkd]d^Zdkd_d`ZdkdadbZdkdcddZdkdedfZdkdgdhZdS )p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_mismatchFrename_nodesboolrename_edgesrename_inputsrename_outputs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 ]0\}}t|j|jD ]\}}| |||| qt|j
|j
D ]\}}| |||| qqt|j|jD ]\}}| ||j|j|j q t|j
|j
D ]\}}| ||j|j|j qt|j|jD ]\}}| ||j|j|j q4t|j|jD ]%\}}| ||jj|jj|jj | ||jj|jj|jj qNt|j|jD ]\} }!| ||!j|!j| j q{|rt|j|j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   )r   r~   r   CopyFromr   
add_prefixrU   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_nodes     z*TestComposeFunctions.test_add_prefix_nodesc                 C  r   )z]Tests prefixing nodes edges. This will also rename inputs/outputs, since the names are sharedT)r   Nr   rq   r   r   r   test_add_prefix_edges  r   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_inputs	  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_outputs  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 ]V\}}| t||j|j t|j|jD ]?\}}|jrt|jj|jjD ].\}}t|j|jD ]\}}| t||| qt|j|j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.)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_subgraph  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_inplaceB  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                   sl   t |jj| jjD ]*\}} |j|j  |jjj|jjj t|}|	|d  t|| q	d S )Nr   )
r   rU   rn   rl   r-   r"   r#   	elem_typer,   insert)rY   rZ   r  out_g2out_g1expected_out_shaperq   r   r   _check_modelL  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_dimF  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'   i)r/  r2  r   r   r*   n      z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>c                       g | ]}t  | tjg qS r   r   r   r
   r8   r>  )r/  r   r   r*   r      c                   rA  r   rB  r>  )r2  r   r   r*   v  rC  c                   rA  r   rB  r>  )r4  r   r   r*   z  rC  c                   &   g | ]}t j | tjd dgdqS r   r   r-   r2   r3   r4   r   r7   r
   r=   r>  )r6  r   r   r*   ~      c                      g | ]}t  | qS r   rE   r>  )r8  r   r   r*         
c                   r:  r;  r=  r>  )r1  r3  r   r   r*     r@  c                   rA  r   rB  r>  )r1  r   r   r*     rC  c                   rA  r   rB  r>  )r3  r   r   r*     rC  c                   rA  r   rB  r>  )r5  r   r   r*     rC  c                   rD  rE  rG  r>  )r7  r   r   r*     rH  c                   rI  r   rJ  r>  )r9  r   r   r*     rK  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   r/  r1  r2  r3  r4  r5  r6  r7  r8  r9  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   )
r6  r7  r/  r1  r2  r3  r8  r9  r4  r5  r   _test_overlapping_namesa  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   r  )r/  r1  Nr^  rq   r   r   r   test_overlapping_input_names     z1TestComposeFunctions.test_overlapping_input_namesc                 C  r_  )z9Tests error checking when the name of the output overlapsr   r   r"  )r2  r3  Nr`  rq   r   r   r   test_overlapping_output_names  rb  z2TestComposeFunctions.test_overlapping_output_namesc                 C  r_  )zATests error checking when the name of value_info entries overlapsr   r   vi2)r4  r5  Nr`  rq   r   r   r   !test_overlapping_value_info_names     
z6TestComposeFunctions.test_overlapping_value_info_namesc                 C  r_  )zBTests error checking when the name of initializer entries overlapsr   r   r)  )r6  r7  Nr`  rq   r   r   r   "test_overlapping_initializer_names  rf  z7TestComposeFunctions.test_overlapping_initializer_namesc                 C  r_  )zITests error checking when the name of sparse_initializer entries overlapsr   r   r-  )r8  r9  Nr`  rq   r   r   r   )test_overlapping_sparse_initializer_names  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   rj  r-   rm   extendrn   ro   opset_import)rj  rk  rM   rO   r   frR  r   r   _make_function  s   zLTestComposeFunctions.test_overlapping_function_names.<locals>._make_functionx0x1yf1)rj  rM   rO   r_   r   r   rb   r   r`   rd   )ru  rs  )ru  rt  r   r   )rI   rP   rR   c                 S  re   r   rf   r'   r   r   r   r   r*   L  rj   zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>m1/f1zm2/f1c                 S  re   r   r   r'   rp  r   r   r   r*   O  rj   r[   f2r   y0y1zm3/c                 S  re   r   rf   rw  r   r   r   r*   z  rj   m3/f2c                 S  re   r   r   ry  r   r   r   r*   }  rj   )rx  m3/f1r}  c                 S  re   r   rf   rw  r   r   r   r*     rj   )rb   rd   rb   c                 S  re   r   rf   rw  r   r   r   r*     rj   r   )r~  rd   rb   c                 S  re   r   rf   rw  r   r   r   r*     rj   r/   N)rj  r   rk  r   rM   rl  rO   rl  r   rm  r   r   )r   r   r   rm   rn  r   r
   r8   rn   ro   r   r   r-   r   	functionsr   r   r   rW   rU   rl   rg   )rX   rr  r   r_   rY   r`   rZ   r   r   r  r[   r\   r   rq  r   test_overlapping_function_names  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   xru  r<  r   r_   r   r   r`   r   r   rF  r[   r   )ru  r  )rP   rI   N)r   r   r   rm   rn  r   r
   r8   rn   ro   r   r   r-   r   r   r   r   r7   r   rE   r    r   rW   rl   r   rU   )rX   rR  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  r   r!  r$  r&  r'  r(  r+  r,  )r/  r0  r1  r0  r2  r0  r3  r0  r4  r0  r5  r0  r6  r0  r7  r0  r8  r0  r9  r0  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^  ra  rc  re  rg  rh  r  r  r   r   r   r   rF   c   sj    
#



"
'





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   r   numpyr9   onnxr   r   r   r   r	   r
   r   r   r   r   r   r   collections.abcr   r   r   r,   rE   r~   r   TestCaserF   r  mainr   r   r   r   <module>   s.   8



      d