o
    r::jV                     @  s  d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dlmZ er*d dl	m
Z
 G dd dejZG dd	 d	ejZG d
d dejZG dd dejZG dd dejZedddg dddddg ddgG dd dejZedkre  dS dS )    )annotationsN)TYPE_CHECKING)defs)Sequencec                   @  s\   e Zd ZdddZdddZdddZdd	d
ZdddZdddZdddZ	dddZ
dS )
TestSchemareturnNonec                 C  s"   t d}| |jt jjj d S )NRelu)r   
get_schemaassertEqualnode_determinismOpSchemaNodeDeterminismDeterministic)selfrelu_schema r   \/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnx/test/schema_test.pytest_get_schema   s   
zTestSchema.test_get_schemac                 C  s   t d d S )NConv)r   r
   r   r   r   r   test_typecheck   s   zTestSchema.test_typecheckc                 C  s:   t djd j}| t|tj | |jtjj d S )NBatchNormalizationepsilon)	r   r
   
attributesdefault_valuer   typeonnxAttributeProtoFLOAT)r   vr   r   r   test_attr_default_value   s   z"TestSchema.test_attr_default_valuec                 C  s6   t d}| t|jtj | |jt jj	j
 d S )NSelu)r   r
   r   r   function_bodyr   FunctionProtor   r   r   r   )r   selu_schemar   r   r   test_function_body"   s
   
zTestSchema.test_function_bodyc                 C  s   t d}| |jt jjj | |j t d}| |jt jjj	 | 
|j t d}| |jt jjj	 | 
|j t d}| |jt jjj | |j d S )NRandomNormalLiker   CastLikeIf)r   r
   r   r   r   r   NonDeterministic
assertTruenon_deterministicr   assertFalse)r   rand_schema	bn_schemacast_like_schema	if_schemar   r   r   test_node_determinism)   s,   



z TestSchema.test_node_determinismc           	   	   C  s   t d}t }|jD ]}|jdkr||j qh d}|D ]"}| j|d | ||d|  W d   n1 s<w   Y  qh d}|D ]}|	dre|
d	re|d
d }| ||d| d qHdS )z8Test Range operator supports all expected numeric types.RangeT>   tensor(int16)tensor(int32)tensor(double)tensor(float16)tensor(bfloat16)tensor(float)tensor(int64))r   zRange should support N>   floatint16int32int64doublefloat16bfloat16tensor()   zUnexpected type z supported by Range)r   r
   settype_constraintstype_param_strupdateallowed_type_strssubTestassertIn
startswithendswith)	r   range_schemasupported_types
constraintexpected_typesexpected_typeallowed_type_familiessupported_type	base_typer   r   r   test_range_supported_typesA   s6   





z%TestSchema.test_range_supported_typesc                 C  s   t d}g d}| t|jt| t|D ]\}}|j| }| |j| | |jdd| d q| t|jd |jd }| |jd | |jdd	 d
d |j	D }| t|dd dS )z4Test Range operator type constraints are consistent.r3   )startlimitdeltar4   zInput 'z ' should use type constraint 'T'   r   outputz%Output should use type constraint 'T'c                 S  s   g | ]	}|j d kr|qS )r4   )rI   ).0cr   r   r   
<listcomp>   s    z:TestSchema.test_range_type_consistency.<locals>.<listcomp>z1Range should have exactly one type constraint 'T'N)
r   r
   r   leninputs	enumeratenametype_stroutputsrH   )r   rP   expected_input_namesiexpected_nameinput_paramoutput_paramrH   r   r   r   test_range_type_consistencyp   s2   



z&TestSchema.test_range_type_consistencyc                 C  s   t d}t }|jD ]}|jdkr||j qh d}|D ]}| ||d|  q|D ]!}| |	dd|  |dd }| 
|g d	d
| d q.dS )z<Test Range operator only supports appropriate numeric types.r3   r4   >   tensor(bool)tensor(int8)tensor(uint8)tensor(string)tensor(uint16)tensor(uint32)tensor(uint64)zRange should not support rC   z'All Range types should be tensors, got rE   rF   )rA   rB   r<   r@   r=   r>   r?   zRange type z# should be a supported numeric typeN)r   r
   rG   rH   rI   rJ   rK   assertNotInr+   rN   rM   )r   rP   rQ   rR   unsupported_typesunsupported_typerV   rW   r   r   r   test_range_numeric_types_only   s2   




z(TestSchema.test_range_numeric_types_onlyNr   r   )__name__
__module____qualname__r   r   r!   r&   r2   rX   rl   rw   r   r   r   r   r      s    






/!r   c                   @  sb   e Zd Zdd ZdddZdddZdd	d
Zdd Zdd Zdd Z	dd Z
dd Zdd ZdS )TestOpSchemac                 C  s4   t ddd}| |t j | |jt jjj d S Ntest_optest_domainr\   )r   r   assertIsInstancer   r   r   r   r   schemar   r   r   	test_init   s
   zTestOpSchema.test_initr   r   c                 C  s   t jdddt jddgddgdfgd}| |jd | |jd | |jd | t|jd | |jd	 jd | |jd	 j	d | t|j
d | |j
d	 jd | |j
d	 jdg d S )
Nr~   r   r\   input1r4   r;    rb   rH   r   )r   r   FormalParameterr   rd   domainsince_versionra   rb   re   rH   rI   rK   r   	op_schemar   r   r   test_init_with_inputs   s$   z"TestOpSchema.test_init_with_inputsc                 C  s  t jdddt jddt jddgt jddt jddgdd	gd
fgt jdt jjjdgd}| t|jd | |jd j	d | |jd j
d | |jd j	d | |jd j
d | t|jd | |jd j	d | |jd j
d | |jd j	d | |jd j
d | t|jd | |jd jd | |jd jd	g | t|jd | |jd j	d | |jd jt jjj | |jd jd d S )Nr~   r   r\   r   r4   input2output1output2r;   r   attr1attr1 descriptionrb   rf   rH   r      r   )r   r   r   	AttributeAttrTypeINTSr   ra   rb   rd   re   rf   rH   rI   rK   r   r   descriptionr   r   r   r   +test_init_creates_multi_input_output_schema   sJ   z8TestOpSchema.test_init_creates_multi_input_output_schemac                 C  sr   t ddd}| |jd | |jd | |jd | t|jd | t|jd | t|j	d d S )Nr~   r   r\   r   )
r   r   r   rd   r   r   ra   rb   rf   rH   r   r   r   r   $test_init_without_optional_arguments  s   z1TestOpSchema.test_init_without_optional_argumentsc                 C  s   |  t tjddd W d    n1 sw   Y  |  t tddd W d    n1 s3w   Y  tddd}| |jd d S )Nr   r\   )r   r   {   r~   )assertRaises	TypeErrorr   r   r   rd   r   r   r   r   	test_name
     zTestOpSchema.test_namec                 C  s   |  t tjddd W d    n1 sw   Y  |  t tddd W d    n1 s3w   Y  tddd}| |jd d S )Nr~   r\   )rd   r   r   r   )r   r   r   r   r   r   r   r   r   r   r     r   zTestOpSchema.test_domainc                 C  sV   |  t tdd W d    n1 sw   Y  tddd}| |jd d S r}   )r   r   r   r   r   r   r   r   r   r   test_since_version  s
   zTestOpSchema.test_since_versionc                 C  s$   t jddddd}| |jd d S )Nr~   r   r\   test_doc)doc)r   r   r   r   r   r   r   r   r   &  s   zTestOpSchema.test_docc                 C     t jjddddg}t jddd|ddgd	fgd
}| t|jd | |jd jd | |jd jd | |jd jd d S )Nr   r4   The first input.rd   re   r   r~   r   r\   r;   r   r   r   )	r   r   r   r   ra   rb   rd   re   r   )r   rb   r   r   r   r   test_inputs*  s   zTestOpSchema.test_inputsc                 C  r   )Nr   r4   zThe first output.r   r~   r   r\   r;   r   )rf   rH   r   )	r   r   r   r   ra   rf   rd   re   r   )r   rf   r   r   r   r   test_outputs>  s   zTestOpSchema.test_outputsNrx   )ry   rz   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r|      s    


*	

r|   c                   @  s   e Zd Zdd ZdS )TestFormalParameterc           	   	   C  s   d}d}d}t jjj}d}d}t jjj}t jj|||||||d}| |j| | |j	| | 
|jt | |j| | |j| | |j| | |j| | |j| d S )Nr   r:   r   Tr\   )param_optionis_homogeneous	min_aritydifferentiation_category)r   r   FormalParameterOptionSingleDifferentiationCategoryUnknownr   r   rd   re   r   typesrG   r   optionr   r   r   )	r   rd   re   r   r   r   r   r   formal_parameterr   r   r   r   T  s4   


zTestFormalParameter.test_initN)ry   rz   r{   r   r   r   r   r   r   S  s    r   c                   @  s>   e Zd ZejdddgdfddddgdfdgdddZdS )TestTypeConstraintParamsingle_typer4   r:   Test descriptiondouble_typesr;   )tupler4   )r:   r;   r   _strrI   allowed_typesSequence[str]r   r   r   c                 C  sB   t j|||}| |j| | |jt| | |j| d S N)r   r   TypeConstraintParamr   r   rK   listrI   )r   r   rI   r   r   type_constraintr   r   r   r   s  s   z!TestTypeConstraintParam.test_initN)
r   r   rI   r   r   r   r   r   r   r   )ry   rz   r{   parameterizedexpandr   r   r   r   r   r   r  s    r   c                   @  s   e Zd Zdd Zdd ZdS )TestAttributec                 C  sP   d}t jjj}d}t j|||}| |j| | |j| | |j| d S )N	test_attrzTest attribute)	r   r   r   STRINGSr   r   rd   r   r   )r   rd   type_r   	attributer   r   r   r     s   
zTestAttribute.test_initc                 C  s^   t djd j}| |tj t jd|d}| 	||j | 	d|j
 | 	d|j d S )Nr   r   r   r   )r   r
   r   r   r   r   r   r   r   r   rd   r   )r   r   r   r   r   r   test_init_with_default_value  s   z*TestAttribute.test_init_with_default_valueN)ry   rz   r{   r   r   r   r   r   r   r     s    
r   CustomOp   r   )r\   r      rE   )op_type
op_version	op_domaintrap_op_versiontestc                   @  sr   e Zd ZU ded< ded< ded< ded< dd
dZdddZdd Zdd Zdd Zdd Z	dd Z
dd ZdS )TestOpSchemaRegisterr   r   intr   r   z	list[int]r   r   r   c                 C  s   |  tj| j| j d S r   )r-   r   r   hasr   r   r   r   r   r   setUp  s   zTestOpSchemaRegister.setUpc              	   C  s\   g | j | jD ]#}ttjj tj| j|| j	 W d    n1 s&w   Y  qd S r   )
r   r   
contextlibsuppressr   r   SchemaErrorderegister_schemar   r   )r   versionr   r   r   tearDown  s   zTestOpSchemaRegister.tearDownc                 C  s   g | j | jD ]}t| j| j|}tj| | tj	| j|| j qg | j | jD ]}tj
|j||j}t| j| j|}| t|t| q/d S r   )r   r   r   r   r   r   r   register_schemar+   r   r
   rd   r   r   r   )r   r   r   registered_opr   r   r   test_register_multi_schema  s&   
z/TestOpSchemaRegister.test_register_multi_schemac                 C  s&  d| j  d| j d| j  d| j d	}tj|}tj| j| j | jtjddtjdd	gtjd
dgddgdfgtj	dtjj
jdgd}| tjj tjj|dd W d    n1 scw   Y  tj| | jD ]}tjtj| j| j |tjd
d	gd qqtjj|dd d S )Nzc
            <
                ir_version: 7,
                opset_import: [
                    "z" : z
                ]
            >
            agraph (float[N, 128] X, int32 Y) => (float[N] Z)
            {
                Z = .z-<attr1=[1,2]>(X, Y)
            }
           r   r4   r   r>   r   r:   r   r   r   r   T)check_custom_domain)rf   )r   r   r   r   parserparse_modelr   r   r   r   r   r   r   checkerValidationErrorcheck_modelr   r   )r   inputmodelr   r   r   r   r   .test_using_the_specified_version_in_onnx_check  sP   		

zCTestOpSchemaRegister.test_using_the_specified_version_in_onnx_checkc                 C  s`   t | j| j| j}tj | | tj j tj | W d    d S 1 s)w   Y  d S r   )	r   r   r   r   r   r   r   r   r   r   r   r   r   Atest_register_schema_raises_error_when_registering_a_schema_twice  s   "zVTestOpSchemaRegister.test_register_schema_raises_error_when_registering_a_schema_twicec                 C  s   g | j | jD ]}t| j| j|}tj| | tj	|j
||j qtj|j
| j|j | j D ]}| tj	|j
||j q5tj	|j
| j|jretj|j
| j|j}| |j| j d S d S r   )r   r   r   r   r   r   r   r   r+   r   rd   r   r   r
   
assertLessr   )r   r   r   r   r   r   r   $test_deregister_the_specified_schema  s"   
z9TestOpSchemaRegister.test_deregister_the_specified_schemac                 C  sJ   |  tjj tj| j| j| j W d    d S 1 sw   Y  d S r   )r   r   r   r   r   r   r   r   r   r   r   r   @test_deregister_schema_raises_error_when_opschema_does_not_exist*  s   "zUTestOpSchemaRegister.test_deregister_schema_raises_error_when_opschema_does_not_existc                   s   t | j| j| j tj   tj  j j	 j
}tj  j j
} fdd}|tj  }|tj  }| t|d | t|d | t|t  | t|t  | t|d t  | t|d t  d S )Nc                   s    fdd| D S )Nc                   s   g | ]
}|j  j kr|qS r   )rd   )r^   opr   r   r   r`   ;  s    znTestOpSchemaRegister.test_legacy_schema_accessible_after_deregister.<locals>.filter_schema.<locals>.<listcomp>r   )schemasr   r   r   filter_schema:  s   zZTestOpSchemaRegister.test_legacy_schema_accessible_after_deregister.<locals>.filter_schemar\   r   )r   r   r   r   r   r   r   r
   rd   r   r   get_all_schemasget_all_schemas_with_historyr   ra   r   )r   schema_aschema_br   schema_cschema_dr   r   r   .test_legacy_schema_accessible_after_deregister.  s&   zCTestOpSchemaRegister.test_legacy_schema_accessible_after_deregisterNrx   )ry   rz   r{   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r     s   
 

1
r   __main__)
__future__r   r   unittesttypingr   r   r   r   collections.abcr   TestCaser   r|   r   r   r   parameterized_classr   ry   mainr   r   r   r   <module>   sD    (  