
    
Ng$                         d dl Z d dlZd dlZd dlZd dlZ G d dej                  Zedk    r ej                     dS dS )    Nc                       e Zd ZdedefdZdefdZd Zd Zd Z	d Z
d	 Zd
 Zdedej        e         fdZd Zd Zd Zd Zd ZdS )TestModelInference
model_textexpectedc                    t           j                            |          }t           j                            |          }|j        j        }t          ||          D ]\  }}|j        }| 	                    |
                    d                     |j        }	| 	                    |	
                    d                     |	j        }
|                     |
|           dS )zCheck that the model inference infers the expected types for outputs.
        Restricted to the simple case of tensor types, so expected types specify
        only the element type (ints corresponding to onnx.TensorProto.DataType).
        tensor_type	elem_typeN)onnxparserparse_modelshape_inferenceinfer_shapesgraphoutputziptype
assertTrueHasFieldr   r	   assertEqual)selfr   r   modelinferredoutputsr   expected_elem_typeinferred_typer   r	   s              Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/test/model_inference_test.py_checkzTestModelInference._check   s    
 ''
33'44U;;.'*-gx*@*@ 	< 	<&F&"KMOOM22=AABBB'3KOOK00==>>>#-IY(:;;;;	< 	<    c                     t           j                            |          }|                     t           j        j                  5  t           j                            |dd           ddd           dS # 1 swxY w Y   dS )z8Check that the model inference raises an InferenceError.TN)r
   r   r   assertRaisesr   InferenceErrorr   )r   r   r   s      r   _check_inference_errorz)TestModelInference._check_inference_error   s    ''
33t3BCC 	A 	A --eT4@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	As   "A33A7:A7c                 4    d}|                      |           dS )zTest that model inference handles unknown ops.
        This special treatment is to support custom ops.
        See comments in shape inference code for details.
        z
            <ir_version: 7, opset_import: [ "" : 17]>
            agraph (float[N] x) => (y)
            {
                y = SomeUnknownOp (x)
            }
        N)r   r   r   s     r   test_unknown_opz"TestModelInference.test_unknown_op#   s#    
 	Er   c                 T    d}|                      |t          j        j                   dS )z3Test that model inference infers model output type.z
            <
                ir_version: 7,
                opset_import: [ "" : 17]
            >
            agraph (float[N] x) => (y)
            {
                y = Cast<to=6> (x)
            }
        Nr   r
   TensorProtoINT32r$   s     r   test_mi_basicz TestModelInference.test_mi_basic3   s+    	 	E4+122222r   c                 T    d}|                      |t          j        j                   dS )Test use of functions.a  
            <
                ir_version: 7,
                opset_import: [ "" : 17, "local" : 1]
            >
            agraph (float[N] x) => (y)
            {
                y = local.cast(x)
            }
            <
                opset_import: [ "" : 17 ],
                domain: "local"
            >
            cast (x) => (y)
            {
                y = Cast<to=6> (x)
            }
        Nr'   r$   s     r   test_mi_functionz#TestModelInference.test_mi_functionA   +    $ 	E4+122222r   c                 T    d}|                      |t          j        j                   dS )z0Test use of functions with attribute parameters.a  
            <
                ir_version: 7,
                opset_import: [ "" : 17, "local" : 1]
            >
            agraph (float[N] x) => (y)
            {
                y = local.cast<target=6>(x)
            }
            <
                opset_import: [ "" : 17 ],
                domain: "local"
            >
            cast<target>(x) => (y)
            {
                y = Cast<to:int = @target> (x)
            }
        Nr'   r$   s     r   test_mi_function_attrz(TestModelInference.test_mi_function_attrW   r.   r   c                 T    d}|                      |t          j        j                   dS )z1Test use of function attributes within subgraphs.a  
            <
                ir_version: 7,
                opset_import: [ "" : 17, "local" : 1]
            >
            agraph (float[N] x, bool flag) => (y)
            {
                y = local.cast<target=6>(x, flag)
            }
            <
                opset_import: [ "" : 17 ],
                domain: "local"
            >
            cast<target>(x, flag) => (y)
            {
                y = If (flag) <
                    then_branch = g1 () => (z_then) { z_then = Cast<to:int = @target> (x) },
                    else_branch = g2 () => (z_else) { z_else = Cast<to:int = @target> (x) }
                    >
            }
        Nr'   r$   s     r   test_mi_function_subgraph_attrz1TestModelInference.test_mi_function_subgraph_attrm   s+    * 	E4+122222r   c                 t    d}|                      |t          j        j        t          j        j                   dS )z-Test use of multiple invocation of functions.a  
            <
                ir_version: 7,
                opset_import: [ "" : 17, "local" : 1]
            >
            agraph (float[N] x, bool flag) => (y, z)
            {
                y = local.cast<target=6>(x, flag)
                z = local.cast<target=7>(x, flag)
            }
            <
                opset_import: [ "" : 17 ],
                domain: "local"
            >
            cast<target>(x, flag) => (y)
            {
                y = If (flag) <
                    then_branch = g1 () => (z_then) { z_then = Cast<to:int = @target> (x) },
                    else_branch = g2 () => (z_else) { z_else = Cast<to:int = @target> (x) }
                    >
            }
        Nr   r
   r(   r)   INT64r$   s     r   test_mi_function_multiple_callsz2TestModelInference.test_mi_function_multiple_calls   s3    , 	E4+143C3IJJJJJr   c                    t           j                            |          }t           j                            |ddd          }|j        j        }t          ||          D ]\  }}|j        }| 	                    |
                    d                     |j        }	| 	                    |	
                    d                     |	j        }
|                     t          |
j                  t          |                     t          |
j        |          D ]H\  }}| 	                    |
                    d                     |                     |j        |           IdS )zCheck that the model inference infers the expected shapes for outputs.
        Restricted to the simple case of tensor type outputs with completely
        known shapes.
        Tr   shape	dim_valueN)r
   r   r   r   r   r   r   r   r   r   r   r   r8   r   lendimr9   )r   r   r   r   r   r   r   expected_shaper   r   inferred_shapeinferred_dimexpected_dims                r   _check_shapezTestModelInference._check_shape   sP   
 ''
33'44UD$MM.'&)'8&<&< 		G 		G"FN"KMOOM22=AABBB'3KOOK0099:::(.NS!344c.6I6IJJJ.1.2Dn.U.U G G*l 5 5k B BCCC  !7FFFFG		G 		Gr   c                 :    d}|                      |g d           d S )Na  
            <
                ir_version: 7,
                opset_import: [ "" : 17]
            >
            mymodel (float[4, 8, 16] x) => (y) {
                shape = Constant<value_ints=[8,4,16]>()
                y = Reshape(x, shape)
            }
                     r@   r$   s     r   test_mi_constantz#TestModelInference.test_mi_constant   s+    	 	%,,,,,r   c                 :    d}|                      |g d           d S )Nae  
            <
                ir_version: 7,
                opset_import: [ "" : 17]
            >
            mymodel (float[4, 8, 16] x) => (y) {
                shape = Constant<value_ints=[4,2,8]>()
                two = Constant<value_int=2>()
                shape2 = Mul(shape, two)
                y = Reshape(x, shape2)
            }
            rB   rF   r$   s     r   test_mi_constant_2z%TestModelInference.test_mi_constant_2   s+     	%,,,,,r   c                 @    d}|                      |ddgg d           d S )Na:  
            <
                ir_version: 7,
                opset_import: [ "" : 17, "local" : 1]
            >
            main (float x) => (y, z) {
                y, z = local.expand(x)
            }
            <
                opset_import: [ "" : 17 ],
                domain: "local"
            >
            expand (x) => (y, z) {
                shape1 = Constant<value = int64[2] {4,4}>()
                shape2 = Constant<value = int64[3] {8,8,8}>()
                z = Expand (x, shape2)
                y = Expand (x, shape1)
            }
            rD   )rC   rC   rC   rF   r$   s     r   test_mi_constant_in_functionz/TestModelInference.test_mi_constant_in_function   s1    & 	%!Q33333r   c                 t    d}|                      |t          j        j        t          j        j                   dS )z2Test use of default values of function attributes.a  
            <ir_version: 7, opset_import: [ "" : 17, "local" : 1]>
            agraph (float[N] x) => (y, z)
            {
                y = local.cast <target=6> (x) # casts to INT32 type (encoding value 6)
                z = local.cast (x)  # uses default-attribute value of 1 (FLOAT type)
            }

            <opset_import: [ "" : 17 ], domain: "local">
            cast <target: int = 1> (x) => (y)
            {
                y = Cast <to:int = @target> (x)
            }
        N)r   r
   r(   r)   FLOATr$   s     r   test_mi_function_default_attrz0TestModelInference.test_mi_function_default_attr   s3     	E4+143C3IJJJJJr   c                 t    d}|                      |t          j        j        t          j        j                   dS )r,   a?  
            <ir_version: 10, opset_import: [ "" : 17, "local" : 1]>
            agraph (float[N] x) => (y, z)
            {
                y = local.cast:to_int32 (x)
                z = local.cast:to_int64 (x)
            }
            <opset_import: [ "" : 17 ], domain: "local", overload: "to_int32">
            cast (x) => (y)
            {
                y = Cast<to=6> (x)
            }
            <opset_import: [ "" : 17 ], domain: "local", overload: "to_int64">
            cast (x) => (y)
            {
                y = Cast<to=7> (x)
            }
        Nr4   r$   s     r   test_mi_overloaded_functionz.TestModelInference.test_mi_overloaded_function   s3    $ 	E4+143C3IJJJJJr   N)__name__
__module____qualname__strintr   r"   r%   r*   r-   r0   r2   r6   typingSequencer@   rG   rI   rK   rN   rP    r   r   r   r      s5       < < < < < < A A A A A   3 3 33 3 3,3 3 3,3 3 32K K K4Gs Gvs7K G G G G&- - -- - -4 4 4,K K K$K K K K Kr   r   __main__)	rV   unittestr
   onnx.parseronnx.shape_inferenceTestCaser   rQ   mainrX   r   r   <module>r_      s              K K K K K* K K KD zHMOOOOO r   