U
    qhk                     @  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 ddddd	Zdddd
ddZdddddZdddddZdZdZG dd dejZedkre  dS )    )annotationsN)CallableSequence)FunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProtoValueInfoProtocheckercomposehelperparserversion_converterstrr   )m_defreturnc                 C  s   t | }t| |S )zYParses a model from a string representation, including checking the model for correctness)r   Zparse_modelr   check_model)r   m r   :/tmp/pip-unpacked-wheel-xnis5xre/onnx/test/compose_test.py_load_model   s    

r   )prefixsr   c                 C  s   t |dkr| | S |S )z Prefixes a string (if not empty)r   )len)r   r   r   r   r   	_prefixed"   s    r   r   z	list[int])
value_infor   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dimZ	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'   r	   )namer   c                 C  s   ddg}dddg}dddg}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         g333333?g?g?_valuesF)r(   	data_typedimsvalsraw_idx)r   make_tensorr
   FLOATr   nparrayZastypeZfloat32INT64Zint64Zmake_sparse_tensor)r(   Zdense_shapeZlinear_indicesZsparse_valuesZvalues_tensorZindices_tensorr   r   r   _make_sparse_tensor/   s$    

r7   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dSdddddddddd	d	d
ZddddZddddZddddZddddZddddZddddZ	ddddZ
ddddZddddZddddZddd d!ZdTd#d#d#d#d#d#d#dd$d%d&Zddd'd(Zddd)d*Zddd+d,Zddd-d.Zddd/d0Zddd1d2Zddd3d4Zddd5d6ZdUdAdAdAdAdAdAdAdAdAdAddBdCdDZdddEdFZdddGdHZdddIdJZdddKdLZdddMdNZdddOdPZdddQdRZdS )VTestComposeFunctionsNr   zlist[tuple[str, str]]z4Callable[[GraphProto, GraphProto, GraphProto], None]zlist[str] | Nonez
str | NoneNone)	m1defm2defio_mapcheck_expectationsinputsoutputsprefix1prefix2r   c	              	   C  s   t |t | }	}
tj|	j|
j|||||d}t| ||	j|
j| tj|	|
|||||d}t| ||	j|
j|j d S )N)r<   r>   r?   r@   rA   )r   r   Zmerge_graphsgraphr   check_graphmerge_modelsr   )selfr:   r;   r<   r=   r>   r?   r@   rA   m1m2g3m3r   r   r   _test_merge_modelsa   s.    	
	
z'TestComposeFunctions._test_merge_models)r   c                   s6   ddddd fdd}dddg}  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
        r   r9   g1g2rH   r   c                   sH     |j| j   |j|j   ddddddgdd |jD  d S )NAddSubMulc                 S  s   g | ]
}|j qS r   op_typer"   itemr   r   r   r%      s     zlTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>assertEqualinputoutputnoderL   rM   rH   rE   r   r   r=      s    zXTestComposeFunctions.test_case_connect_all_no_name_collision.<locals>.check_expectationsB00B01B10B11B20B21NrJ   M1_DEFM2_DEFrE   r=   r<   r   r[   r   'test_case_connect_all_no_name_collision   s    
z<TestComposeFunctions.test_case_connect_all_no_name_collisionc                   s6   ddddd fdd}d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
        r   r9   rK   c                   sX   ~  |j| j   dddgdd |jD    ddddddgd	d |jD  d S )
Nr`   rc   D0c                 S  s   g | ]
}|j qS r   r(   r"   elemr   r   r   r%      s     zhTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectations.<locals>.<listcomp>rN   rO   rP   c                 S  s   g | ]
}|j qS r   rQ   rS   r   r   r   r%      s     rU   rZ   r[   r   r   r=      s    zTTestComposeFunctions.test_case_connect_same_output_twice.<locals>.check_expectationsr\   r]   ra   r]   rd   Nre   rh   r   r[   r   #test_case_connect_same_output_twice   s    	
z8TestComposeFunctions.test_case_connect_same_output_twicec                   s@   ddddd fdd}dddg}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   r9   rK   c                   sP   ~  |j| j   dgdd |jD    ddddgdd |jD  d S )Nrj   c                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%      s     zoTestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectations.<locals>.<listcomp>rN   rO   rP   c                 S  s   g | ]
}|j qS r   rQ   rS   r   r   r   r%      s     rU   rZ   r[   r   r   r=      s    
 z[TestComposeFunctions.test_case_connect_same_output_drop_outputs.<locals>.check_expectationsr\   rn   ro   rj   )r?   Nre   )rE   r=   r<   r?   r   r[   r   *test_case_connect_same_output_drop_outputs   s    
    z?TestComposeFunctions.test_case_connect_same_output_drop_outputsc                   s:   d}d}dg}dddd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)
            }
            )Brr   r   r9   rK   c                   s<   ~ ~  dgdd |jD    dgdd |jD  d S )NAc                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%      s     zmTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectations.<locals>.<listcomp>Cc                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%      s     rV   rW   rX   rZ   r[   r   r   r=      s    zYTestComposeFunctions.test_case_connect_same_input_output_name.<locals>.check_expectationsNrJ   )rE   m1_defm2_defr<   r=   r   r[   r   (test_case_connect_same_input_output_name   s
    

z=TestComposeFunctions.test_case_connect_same_input_output_namec                   sL   d}d}dg}dddd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)
            }
            )A1ZB2r   r9   rK   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  s   g | ]
}|j qS r   rk   rl   r   r   r   r%      s     zbTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectations.<locals>.<listcomp>B3c                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%      s     rN   rO   c                 S  s   g | ]
}|j qS r   rQ   rl   r   r   r   r%      s     rU   rZ   r[   r   r   r=      s    zNTestComposeFunctions.test_case_drop_inputs_outputs.<locals>.check_expectationsr{   r|   r>   r?   Nrv   )rE   rw   rx   r<   r=   r>   r?   r   r[   r   test_case_drop_inputs_outputs   s         z2TestComposeFunctions.test_case_drop_inputs_outputsc                   s<   d}dg}dddd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)
            }
            )rt   rs   r   r9   rK   c                   s\   ~ ~  dddgdd |jD    dgdd |jD    ddgd	d |jD  d S )
Nzm1/Azm1/Bzm2/Bc                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%     s     zdTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectations.<locals>.<listcomp>zm2/Cc                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%     s     rN   c                 S  s   g | ]
}|j qS r   rQ   rl   r   r   r   r%     s     rU   rZ   r[   r   r   r=     s    zPTestComposeFunctions.test_case_name_collision_prefix.<locals>.check_expectationsm1/m2/)r@   rA   Nrv   )rE   rw   r<   r=   r   r[   r   test_case_name_collision_prefix  s    
     z4TestComposeFunctions.test_case_name_collision_prefixc                   s4   dddd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   r9   )rL   rM   g4r   c                   sD   ~ ~  ddddgdd |jD    ddgd	d |jD  d S )
Nr{   rz   Z_Ard   c                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%   (  s     zrTestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectations.<locals>.<listcomp>rc   rj   c                 S  s   g | ]
}|j qS r   rk   rl   r   r   r   r%   *  s     ru   )rL   rM   r   r[   r   r   r=   "  s    
 z^TestComposeFunctions.test_case_connect_partially_no_name_collision.<locals>.check_expectationsr\   r_   Nre   rh   r   r[   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\t	t|ddd
 tj|||d}t|jdkst	t|ddd
 | j
ttj|||d d S )Nv1v2)p1p2v3Zv4)Zp3p4r\   r<      )r   r   r)   Zv5)r   rf   r   Zset_model_propsrg   r   rD   r   Zmetadata_propsAssertionErrorassertRaises
ValueErrorrE   rF   rG   r<   rI   r   r   r   %test_merge_models_with_metadata_props/  s    z:TestComposeFunctions.test_merge_models_with_metadata_propsc                 C  sN   t tt t }}| jttj||dddgd | jttj||dddgd dS )z_Tests that providing a non existing output/input name in the io_map argument produces an error.)Zwrong_outnamer^   r_   rb   r   )r]   Zwrong_inputN)r   rf   rg   r   r   r   rD   rE   rF   rG   r   r   r   "test_error_wrong_input_output_nameC  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)
    }
    )ZY0ZX1r   )r   r   r   r   rD   r   r   r   r   test_error_ir_version_mismatchX  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}dddg}| tt	j
||| t|d}t	j
|||d	}t| d
S )zSTests that providing models with different operator set imported produces an error.test 
   Zproducer_nameZopset_imports   r\   r_   rb   r   N)r   rf   rg   r   
make_modelrB   make_opsetidr   r   r   rD   r   Zconvert_versionr   r   r   r   r   r    test_error_opset_import_mismatchw  s         
z5TestComposeFunctions.test_error_opset_import_mismatchFbool)rename_nodesrename_edgesrename_inputsrename_outputsrename_initializersrename_value_infosinplacer   c           "      C  sh  t t}d}	|r>t }
|
| tj|
|	||||||dd	 ntj||	||||||d}
|j}|
j}|sz|sz|sz|sz|rdi }|r|jD ]8}|jD ]}t	|	|||< q|j
D ]}t	|	|||< qq|r|jD ]}t	|	|j||j< q|r|j
D ]}t	|	|j||j< q|rh|jD ]}t	|	|j||j< q|jD ]2}t	|	|jj||jj< t	|	|jj||jj< q4|r|j
D ]}t	|	|j||j< qtt|j|jD ]f\}}t|j|jD ]\}}| |||| qt|j
|j
D ]\}}| |||| qqt|j|jD ]$\}}| ||j|j|j qt|j
|j
D ]$\}}| ||j|j|j qFt|j|jD ]$\}}| ||j|j|j qzt|j|jD ]J\}}| ||jj|jj|jj | ||jj|jj|jj qt|j|jD ]$\} }!| ||!j|!j| j q|rdt|j|jD ] \}}| t	|	|j|j qBd S )Nzpre/T)r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   rf   r   CopyFromr   
add_prefixrB   rY   rW   r   rX   r(   initializersparse_initializervaluesindicesziprV   getr   )"rE   r   r   r   r   r   r   r   rF   r   rG   Zg_inZg_outZname_mappingnerm   initZsparse_initr   n1n0e1Ze0i1i0o1o0init1init0sparse_init1sparse_init0vi1vi0r   r   r   _test_add_prefix  s    









  
   z%TestComposeFunctions._test_add_prefixc                 C  s   | j dd dS )zTests renaming nodes onlyT)r   Nr   r[   r   r   r   test_add_prefix_nodes  s    z*TestComposeFunctions.test_add_prefix_nodesc                 C  s   | j dd dS )z]Tests prefixing nodes edges. This will also rename inputs/outputs, since the names are sharedT)r   Nr   r[   r   r   r   test_add_prefix_edges  s    z*TestComposeFunctions.test_add_prefix_edgesc                 C  s   | j dd dS )zPTests prefixing graph inputs only. Relevant node edges should be renamed as wellT)r   Nr   r[   r   r   r   test_add_prefix_inputs  s    z+TestComposeFunctions.test_add_prefix_inputsc                 C  s   | j dd dS )zQTests prefixing graph outputs only. Relevant node edges should be renamed as wellT)r   Nr   r[   r   r   r   test_add_prefix_outputs
  s    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 ]\}}| t||j|j t|j|jD ]\}}|jrTt|jj|jjD ]b\}}t|j|jD ]\}}| t||| qt|j|jD ]\}}| t||| qqvqTq(dS )zQTests prefixing attribute's subgraph. Relevant subgraph should be renamed as wellrt      XNYZOutrP   XYr}   rN   rO   ZIfZthen)nodesr(   r>   r?   else)r>   r?   Zthen_branchZelse_branchrB   zprefix.)r   make_tensor_value_infor
   ZBOOLr3   	make_node
make_graphr   Zadd_prefix_graphr   rC   r   rY   rV   r   r(   	attributegrW   rX   )rE   rt   r   r   r   r   r   addsubZcondrB   r   Zprefixed_graphr   r   Z
attribute1Z
attribute0Zsubgraph_n1Zsubgraph_n0Zinput_n1Zinput_n0Z	output_n1Z	output_n0r   r   r   "test_add_prefix_attribute_subgraph  sh            
 
   z7TestComposeFunctions.test_add_prefix_attribute_subgraphc                 C  s   |  dddddd dS )z&Tests prefixing all names in the graphTNr   r[   r   r   r   test_add_prefix_all;  s    z(TestComposeFunctions.test_add_prefix_allc                 C  s   | j dd dS )zTests prefixing inplaceTr   Nr   r[   r   r   r   test_add_prefix_inplace?  s    z,TestComposeFunctions.test_add_prefix_inplacec                   sv   t t}dddd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.
        r   intr9   )rF   rG   dim_idxr   c                   sl   t |jj| jjD ]T\}} |j|j  |jjj|jjj t|}|	|d  t|| qd S )Nr   )
r   rB   rX   rV   r(   r   r   Z	elem_typer'   insert)rF   rG   r   Zout_g2Zout_g1Zexpected_out_shaper[   r   r   _check_modelI  s     z>TestComposeFunctions.test_expand_out_dim.<locals>._check_model)r   r*   r   Tr   N)r   rf   r   Zexpand_out_dimr   r   )rE   rF   r   r   rG   r   r[   r   test_expand_out_dimC  s    

z(TestComposeFunctions.test_expand_out_dimr   r   i2Zi3r   r   o2Zo3Zv0r   r   r   r   r   init2Zinit3r   r   sparse_init2Zsparse_init3zSequence[str])inputs0inputs1outputs0outputs1value_info0value_info1initializer0initializer1sparse_initializer0sparse_initializer1r   c           %   
     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
	@ }|r6| || d|f |d7 }t	t
 t
@ } | rl| || d| f |d7 }t	t
t
@ }!|!rg }"|!D ]"}#|"|#d  |"|#d  q| || d|"f |d7 }tj|dd}$t|$j|j}| dt| d S )Nc                   s(   g | ] }t jd  | g| gdqS Identityr}   r   r   r"   i)r   r   r   r   r%   k  s   z@TestComposeFunctions._test_overlapping_names.<locals>.<listcomp>c                   s    g | ]}t  | tjg qS r   r   r   r
   r3   r   )r   r   r   r%   o  s   c                   s    g | ]}t  | tjg qS r   r   r   )r   r   r   r%   s  s   c                   s    g | ]}t  | tjg qS r   r   r   )r   r   r   r%   w  s   c                   s&   g | ]}t j | tjd dgdqS r   r   r(   r-   r.   r/   r   r2   r
   r6   r   )r   r   r   r%   {  s      c                   s   g | ]}t  | qS r   r7   r   )r   r   r   r%     s   c                   s(   g | ] }t jd  | g| gdqS r   r   r   )r   r   r   r   r%     s   c                   s    g | ]}t  | tjg qS r   r   r   )r   r   r   r%     s   c                   s    g | ]}t  | tjg qS r   r   r   )r   r   r   r%     s   c                   s    g | ]}t  | tjg qS r   r   r   )r   r   r   r%     s   c                   s&   g | ]}t j | tjd dgdqS r   r   r   )r   r   r   r%     s      c                   s   g | ]}t  | qS r   r   r   )r   r   r   r%     s   r   r   Zg0)r   r(   r>   r?   r   r   r   r   r   rL   r   Zedger   r   r   r,   r1   r   zg0/)r   )r&   r   r   r   r   r   r   Zcheck_overlapping_namesrB   listsetrV   appendr   )%rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   opsZm0rF   overlapr   Zoverlapping_inputsZoverlapping_outputsZoverlapping_edgesZoverlapping_visZoverlapping_initZoverlapping_sparse_initZexpected_overlapZoverlapping_nameZm0_newr   )
r   r   r   r   r   r   r   r   r   r   r   _test_overlapping_names^  s    





















		z,TestComposeFunctions._test_overlapping_namesc                 C  s   | j ddgddgd dS )z9Tests error checking when the name of the inputs overlapsr   r   r   )r   r   Nr  r[   r   r   r   test_overlapping_input_names  s    z1TestComposeFunctions.test_overlapping_input_namesc                 C  s   | j ddgddgd dS )z9Tests error checking when the name of the output overlapsr   r   r   )r   r   Nr  r[   r   r   r   test_overlapping_output_names  s    z2TestComposeFunctions.test_overlapping_output_namesc                 C  s   | j ddgddgd dS )zATests error checking when the name of value_info entries overlapsr   r   Zvi2)r   r   Nr  r[   r   r   r   !test_overlapping_value_info_names  s     z6TestComposeFunctions.test_overlapping_value_info_namesc                 C  s   | j ddgddgd dS )zBTests error checking when the name of initializer entries overlapsr   r   r   )r   r   Nr  r[   r   r   r   "test_overlapping_initializer_names  s     z7TestComposeFunctions.test_overlapping_initializer_namesc                 C  s   | j ddgddgd dS )zITests error checking when the name of sparse_initializer entries overlapsr   r   r   )r   r   Nr  r[   r   r   r   )test_overlapping_sparse_initializer_names  s    z>TestComposeFunctions.test_overlapping_sparse_initializer_namesc                   s  t ddt ddg ddddddd 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 }	| dd*d(g|	 | dgd+d |jd# j	D  | dddgd,d |jd- j	D  | d*ddgd.d |jd/ j	D  d0S )1zETests error checking when the name of local function entries overlapsr   r   localr   z	list[str]zlist[NodeProto]r   )domainfnamer>   r?   r   r   c                   sF   t  }| |_||_|j| |j| |j| |j  |S )N)r   r  r(   rW   extendrX   rY   Zopset_import)r  r  r>   r?   r   fr  r   r   _make_function   s    zLTestComposeFunctions.test_overlapping_function_names.<locals>._make_functionx0x1yf1)r  r>   r?   rL   r   r   rN   r}   rM   rP   )r  r  )r  r  r   r   )r<   r@   rA   c                 S  s   g | ]
}|j qS r   rQ   r"   r   r   r   r   r%   I  s     zHTestComposeFunctions.test_overlapping_function_names.<locals>.<listcomp>zm1/f1zm2/f1c                 S  s   g | ]
}|j qS r   rk   r"   r  r   r   r   r%   L  s     rH   f2r   Zy0y1zm3/c                 S  s   g | ]
}|j qS r   rQ   r  r   r   r   r%   w  s     zm3/f2c                 S  s   g | ]
}|j qS r   rk   r  r   r   r   r%   z  s     zm3/f1c                 S  s   g | ]
}|j qS r   rQ   r  r   r   r   r%   }  s     c                 S  s   g | ]
}|j qS r   rQ   r  r   r   r   r%     s     r   c                 S  s   g | ]
}|j qS r   rQ   r  r   r   r   r%     s     r*   N)r   r   r   rW   r  r   r
   r3   rX   rY   r   r   r(   r   	functionsr   r   r   rD   rB   rV   rR   )rE   r  r   rL   rF   rM   rG   r   r   r  rH   rI   r   r  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   xr  r   r}   rL   r   r   rM   r   r   r   rH   r   )r  r  )r@   r<   N)r   r   r   rW   r  r   r
   r3   rX   rY   r   r   r(   r   r   r   r   r2   r   r7   r   r   rD   rV   r   rB   )rE   r  r   rL   rF   rM   rG   rH   rI   r   Zm4Zout_m1Zout_m2Zout_m3r   r   r   7test_merge_drop_unnecessary_initializers_and_value_info  sX    


   




zLTestComposeFunctions.test_merge_drop_unnecessary_initializers_and_value_info)NNNN)FFFFFFF)
r   r   r   r   r   r   r   r   r   r   ) __name__
__module____qualname__rJ   ri   rp   rq   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r	  r  r  r   r   r   r   r8   `   sh        #"'       t-          $  
r8   __main__)
__future__r   Zunittesttypingr   r   Znumpyr4   Zonnxr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r'   r7   rf   rg   ZTestCaser8   r  mainr   r   r   r   <module>   s&   8      d