U
    qh!                     @  sR   d dl mZ d dlZd dlZd dlmZmZ G dd dejZedkrNe	  dS )    )annotationsN)checkerutilsc                   @  s"   e Zd Zdd ZddddZdS )TestFunctionc                   sP   t | | t|jt| |D ]&| t fdd|jD d  q$d S )Nc                 3  s&   | ]}|j kr|j kr|V  qd S )N)namedomain).0ffunc_domainfunction ;/tmp/pip-unpacked-wheel-xnis5xre/onnx/test/function_test.py	<genexpr>   s   
 
z4TestFunction._verify_function_set.<locals>.<genexpr>)r   check_modelassertEquallen	functionsZassertIsNotNonenext)selfZextracted_modelZfunction_setr   r   r
   r   _verify_function_set   s    
z!TestFunction._verify_function_setNone)returnc           '      C  s  d}t jddg}t jddt j|dg}d}ddg}dg}t jd	ddgdgg}t j||||||}d
}	dg}
dg}t jddgdgg}t j||	|
|||}d}ddg}dg}t jjd
dgdg|dt jjd
dgdg|dt jjdddgdg|dg}t j||||||}t jj|ddgdg|d}t jd	ddgdg}t jd	ddgdg}t jj|	dgdg|d}t jddgdg}t jd	ddgdg}t jj|ddgdg|d}t jj|ddgdg|d}d}d}t jddt jddg}t jjddgd}t j||||||||g|t jjd|d t jjd|d t jjd|d gt jjd|d t jjd|d t jjd|d t jjd|d g}||d!|||gd"} t jj|f| }!t	
|! t|!dddgdg}"| |"i | t|!dddgdg}#| |#|h| t|!dddgdg}$| |$||	|h| t|!dddgdg}%| |%||	|h| t|!dddgddddg}&| |&||	|h| d#S )$aO  #   1. build a model with graph below. extract models with output combinations
        #   2. validate extracted models' local functions
        #
        # model graph:
        #      i0                    i1                 i2
        #      |   __________________|__________________/_________
        #      |  |                  |             |   /          |
        #      |  |                  |             |  /           |
        #   func_add        func_identity          add         identity
        #    |  ___\___________\____________________|_________    |
        #    | |    \           \                   |  _______|___|
        #    | |     \           \                  | |       |   |
        #    add     function_nested_identity_add   add     function_nested_identity_add
        #     |                 |                    |              |
        #     |                 |                    |              |
        #   o_func_add      o_all_func0           o_no_func     o_all_func1
        #
        # where function_nested_identity_add is a function that is defined with functions:
        #       a               b
        #       |               |
        #   func_identity   func_identity
        #             \       /
        #             func_add
        #                |
        #                c
        #
        local       func_addabcZAddfunc_identityZIdentityfunc_nested_identity_addZa1)r   Zb1Zi0i1t0i2t2Z
o_func_addt1t3Z	o_no_funcZo_all_func0Zo_all_func1Zgraph_with_imbedded_functions         )Z	elem_typeshape)r   Z
type_proto&test_extract_model_with_local_function)
ir_versionopset_importsZproducer_namer   N)onnxhelperZmake_opsetidZ	make_nodeZmake_functionZmake_tensor_type_protoZ
make_graphZmake_value_infoZ
make_modelr   r   r   Z	ExtractorZextract_modelr   )'r   r   Zfunc_opset_importsZfunc_nested_opset_importsZfunc_add_nameZfunc_add_inputsZfunc_add_outputsZfunc_add_nodesr   Zfunc_identity_nameZfunc_identity_inputsZfunc_identity_outputsZfunc_identity_nodesr!   Zfunc_nested_identity_add_nameZfunc_nested_identity_add_inputsZ func_nested_identity_add_outputsZfunc_nested_identity_add_nodesr"   Znode_func_addZ	node_add0Z	node_add1Znode_func_identityZnode_identityZ	node_add2Znode_func_nested0Znode_func_nested1Z
graph_namer.   r/   Ztensor_type_protographmetamodelZextracted_with_no_funcionZextracted_with_add_funcionZextracted_with_o_all_funcion0Zextracted_with_o_all_funcion1Zextracted_with_o_all_funcion2r   r   r   r-      s>   


      
    "

 
   
 
 

z3TestFunction.test_extract_model_with_local_functionN)__name__
__module____qualname__r   r-   r   r   r   r   r      s   r   __main__)

__future__r   Zunittestr0   r   r   ZTestCaser   r5   mainr   r   r   r   <module>   s    V