o
    to™iZ  ã                   @  sà   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlZd dl	Zd dl
Zd dlmZmZ er6d dlmZ ej dd¡Zej dd¡Zej d	d¡Zej d
d¡Zej dd¡Zeƒ ZG dd„ deƒZedkrne ¡  dS dS )é    )ÚannotationsN)ÚTYPE_CHECKING)ÚTestShapeInferenceHelper)ÚAttributeProtoÚ	TypeProto)ÚSequenceé   é   é   é   é
   c                   @  s8   e Zd Zddd	„Zdd
d„Zdd„ Zdd„ Zdd„ ZdS )ÚTestFunctionInferenceÚfunction_textÚstrÚinput_typesúSequence[TypeProto]Ú
attributesúSequence[AttributeProto]Úexpected_output_typesc           	      C  sX   t j |¡}t j |||¡}|  t|ƒt|ƒ¡ t||ddD ]
\}}|  ||¡ qd S )NT)Ústrict)	ÚonnxÚparserÚparse_functionÚshape_inferenceÚinfer_function_output_typesÚassertEqualÚlenÚzipÚ_compare_value_infos)	Úselfr   r   r   r   ÚfunctionÚresultÚexpectedÚactual© r$   úU/home/ubuntu/.local/lib/python3.10/site-packages/onnx/test/function_inference_test.pyÚ_check   s   ÿÿzTestFunctionInference._checkc                   s0   t j |¡‰‡ ‡‡fdd„}|  t jj|¡ d S )Nc                     s   t j ˆˆˆ ¡ d S )N)r   r   r   r$   ©r   r    r   r$   r%   Úinvoke_inference4   s   ÿz<TestFunctionInference._check_fails.<locals>.invoke_inference)r   r   r   ÚassertRaisesr   ÚInferenceError)r   r   r   r   r(   r$   r'   r%   Ú_check_fails,   s   z"TestFunctionInference._check_failsc                 C  sF   d}|   |ttgg tg¡ |   |ttgg tg¡ |  |ttgg ¡ d S )Nzª
            <opset_import: [ "" : 18 ], domain: "local">
            f (y, z) => (w) {
                x = Add(y, z)
                w = Mul(x, y)
            }
        )r&   Úfloat_type_Úint32_type_r+   ©r   Úcoder$   r$   r%   Útest_fi_basic;   s   z#TestFunctionInference.test_fi_basicc                 C  sP   d}t j dd¡}|  |tg|gtg¡ t j dd¡}|  |tg|gtg¡ d S )Nz©
            <opset_import: [ "" : 18 ], domain: "local">
            CastTo <dtype> (x) => (y) {
                y = Cast <to : int = @dtype> (x)
            }
        Údtyper   r   )r   ÚhelperÚmake_attributer&   r,   r-   Úfloat16_type_)r   r/   Údtype_6Údtype_10r$   r$   r%   Útest_fi_attributeG   s
   z'TestFunctionInference.test_fi_attributec                 C  s®   d}|   |tgg tg¡ |   |ttgg tg¡ d}|   |tttgg tg¡ |   |tttgg tg¡ |   |tttgg tg¡ d}|   |tttgg tg¡ |  |tttgg ¡ d S )Nz 
            <opset_import: [ "" : 18 ], domain: "local">
            DoReduce (x, axes) => (y) {
                y = ReduceMax (x, axes)
            }
        z¿
            <opset_import: [ "" : 18 ], domain: "local">
            Quantize (x, scale, zero_point) => (y) {
                y = QuantizeLinear (x, scale, zero_point)
            }
        z¡
            <opset_import: [ "" : 18 ], domain: "local">
            DoClip (x, min, max) => (y) {
                y = Clip (x, min, max)
            }
        )r&   r,   Úno_type_Ú
int8_type_Úuint8_type_r+   r.   r$   r$   r%   Útest_fi_optional_inputT   s   z,TestFunctionInference.test_fi_optional_inputN)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__r&   r+   r0   r7   r;   r$   r$   r$   r%   r      s    

r   Ú__main__)Ú
__future__r   ÚunittestÚtypingr   Úshape_inference_testr   r   Úonnx.helperÚonnx.parserÚonnx.shape_inferencer   r   Úcollections.abcr   r2   Úmake_tensor_type_protor,   r:   r9   r-   r4   r8   r   r<   Úmainr$   r$   r$   r%   Ú<module>   s*   ]ÿ