
    
Ng]                         d dl mZ d dlmZmZmZmZmZmZ d dl	Z
d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZ d d	lmZmZmZmZm Z  d d
l!m"Z"  G d d          Z#dS )    )BytesIO)AnyDictListOptionalTupleUnionN)load)onnx_opset_version)ExternalDataInfouses_external_data)ModelContainer)FunctionProto
GraphProto
ModelProto	NodeProtoTensorProto	TypeProto)OpFunctionContextDependantOpRunOpRunExpandRuntimeContextErrorto_array_extended)optimized_operatorsc                      e Zd ZdZ	 	 	 	 	 d&dedeeeef                  dee	e
d ef                           ded	ee	e                  d
efdZdedej        fdZdedefdZdedede	e         ddfdZed             Zed             Zed             Zed             Zed             Zed             ZdefdZd'dededefdZd(dZ	 d)d ed!ee          defd"Z!d)d#eeef         d$eeeef                  fd%Z"dS )*ReferenceEvaluatora  Computes the outputs of an ONNX proto (`ModelProto`, `FunctionProto`, `GraphProto`, `NodeProto`).

    This is a pure python implementation of ONNX specifications.
    Mismatches may remain between the official specifications and the implementation here.
    In the case of such a mismatch, the official spec overrides this implementation.

    Args:
        proto: :class:`onnx.ModelProto`, :class:`onnx.GraphProto`,
            :class:`onnx.FunctionProto`, :class:`onnx.NodeProto`,
            filename or bytes
        verbose: display intermediate results on the standard output
            during the execution
        opsets: if *proto* is an instance of *GraphProto*, opsets must
            be defined by a dictionary of
        functions: known onnx functions
        new_ops: this runtime can be used to test the implementations of
            new operators, *new_ops* is a list of classes derived from
            :class:`OpRun <onnx.reference.op_run.OpRun>`, every class
            must define the static attribute `domain`, there may be
            multiple implementations for the same operator, the first
            one in the list is used.
        optimized: some operators have two implementations, a naive one
            corresponding to definition of the mathematical definition
            of the operator, another one more efficient. This is the
            case for operator Conv. The naive version is ten times
            slower than the optimized one using a decomposition into
            *Conv = im2col + Gemm*. If True, all optimized kernels are
            added in `new_ops` and are used instead of the inner
            implementation if list *new_ops* does not already contain
            one.

    The class maps every node to its associated implementation.
    When a subgraph of a function is met,
    it uses this class to execute the subgraph or the function.
    Next example shows how to run `ReferenceEvaluator` with an onnx model
    stored in file `model.onnx`.

    ::

        import numpy as np
        from onnx.reference import ReferenceEvaluator

        X = np.array(...)
        sess = ReferenceEvaluator("model.onnx")
        results = sess.run(None, {"X": X})
        print(results[0])  # display the first result

    Parameter *verbose* may be used to show intermediate results.

    ::

        import numpy as np
        from onnx.reference import ReferenceEvaluator

        X = np.array(...)
        sess = ReferenceEvaluator("model.onnx", verbose=1)
        results = sess.run(None, {"X": X})
        print(results[0])  # display the first result

    The class can use any implementation available in folder
    `ops <https://github.com/onnx/onnx/tree/main/onnx/reference/ops>`_.
    Adding an implementation requires two changes. The first one is
    the implementation itself. Any existing node can be used as a template.
    The second is one line in file `_op_list.py
    <https://github.com/onnx/onnx/tree/main/onnx/reference/ops/_op_list.py>`_
    to import the file and let the reference evaluator know it exists.

    This class can also be used to test an implementation of
    a custom operator. Let's assume this new operator
    is `InvAlpha` from domain `custom`. The implementation
    must take place in a class inheriting from
    :class:`OpRun <onnx.reference.op_run.OpRun>`.
    It must also define attribute `op_domain`.
    Here is an example which computes :math:`\\frac{1}{X + \\alpha}`.

    .. exec_code::

        from onnx.reference.op_run import OpRun

        class InvAlpha(OpRun):

            op_domain = "custom"

            def _run(self, x, alpha=None):  # type: ignore
                # None must be the default value, it is automatically
                # replaced by class OpRun with either the default value
                # specified in the NodeProto or an attribute value defined
                # in a `FunctionProto`.
                return (1 / (x + alpha),)

    `alpha` is an attribute. It can be defined by the onnx node or
    be defined by the function using this node. It is safe to assume
    that attributes are known at the same time as the input.
    Class `ReferenceEvaluator` must know about this new implementation
    and this can be done by specified argument *new_ops*.

    ::

        sess = ReferenceEvaluator(onnx_model, new_ops=[InvAlpha])
        got = sess.run(None, {"X": x})[0]

    A specific node can be simply evaluated.

    .. exec_code::

        import numpy as np
        from onnx.reference.ops._op_list import Celu

        x = np.array([[0, 1], [-1, 2]], dtype=np.float32)
        y = Celu.eval(x, alpha=0.5)
        print(y)

    This can also be expressed as:

    .. exec_code::

        import numpy as np
        from onnx.reference.ops import load_op

        Celu = load_op("", "Celu")  # domain is ""
        x = np.array([[0, 1], [-1, 2]], dtype=np.float32)
        y = Celu.eval(x, alpha=0.5)
        print(y)

    It is possible to overwrite an existing operator.
    The class name must be the same. The domain does not have
    to be specified for the default domain. However, by default,
    class `OpRun` will load the most recent for this operator.
    It can be explicitly specified by adding static attribute
    `op_schema` of type :class:`OpSchema
    <onnx.onnx_cpp2py_export.defs.OpSchema>`.

    ::

        from onnx.reference.op_run.op_conv import Conv as _Conv

        class Conv(_Conv):

            op_schema = instance_of_OpSchema()

            def _run(self, ...):
                ...

        An operator may be different in a later opset. In that case,
        a new implementation needs to be registered. `Pad_11`, `Pad_18`.
        `Pad_11` is the implementation chose for opset in [11, 17].
        `Pad_18` is selected for any greater opset. Both classes must be
        imported into file `_op_list.py` to register their existence to the
        runtime.

        An operator may have a reference implementation such as `CastLike`
        and still be defined as a function. By default, the reference implementation
        is used. This behaviour can be changed by adding a class to the list
        of overwritten operators. It must inherit from :class:`OpRunExpand`.

        ::

            from onnx.reference.op_run import OpRunExpand

            class CastLike(OpRunExpand):
                op_domain = ""

            ref = ReferenceEvaluator(model, new_ops=[CastLike])
            # ...

            This mechanism is used in unit test to check the function
            implementation a schema may define.
    Nr   Tprotoopsets	functionsverbosenew_ops	optimizedc           	      
   |rH|t          j                    }n2t          |          }t           D ]}||vr|                    |           d | _        d | _        t          |t                    r|| _        | j        j	        }nd | _        t          |t                    r8t          |d          5 }	t          |	          }d d d            n# 1 swxY w Y   n1t          |t                    rt          t          |                    }|| _        i | _        g | _        t          |t$                    rM|j        | _        d |j        D             | _        |t/          d          |t/          d          |j        }n	t          |t2                    r3|| _        t          |t4                    st/          d          || _        nt          |t6                    rHd | _        d |j        D             | _        |t/          d          t9          |j                  | _        ndt          |t<                    r/d | _        |j        |j        dk    rd	ntA                      i| _        n tC          d
tE          |           d          | j        rd | j        j#        D             | _$        d | j        j#        D             | _        d | j        j%        D             | _&        d | j        j%        D             | _        t9          | j        j'                  t9          | j        j(                  z   | _)        | j        j*        | _+        d | j        j#        D             }
tY          | j        d          r| j        j-        D ]}|j"        |
|j.        <   |
| _/        nct9          |j#                  | _$        t9          |j%                  | _&        g | _)        t          |t<                    r	|g| _+        n|j*        | _+        ||D ]}	t          |	t6                    rQ| 0                    |	|t9          | j        1                                                    | j        |	j        |	j.        f<   ht          |	td                    r|	j        }|	| j        |j        |j.        f<   tC          d
tE          |	          d          || _3        i | _4        |r|D ]o}tY          |d          stk          d| d          tm          |tn                    stC          d| d          |j8        |j9        f}|| j4        v re|| j4        |<   p| :                                 d S )Nrbc                 (    i | ]}|j         |j        S  domainversion.0ds     ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/reference/reference_evaluator.py
<dictcomp>z/ReferenceEvaluator.__init__.<locals>.<dictcomp>       LLLAAHaiLLL    z+opsets must be None if proto is ModelProto.z.functions must be None if proto is ModelProto.z3opsets must be a dictionary if proto is GraphProto.c                 (    i | ]}|j         |j        S r&   r'   r*   s     r-   r.   z/ReferenceEvaluator.__init__.<locals>.<dictcomp>   r/   r0   z.opsets must be None if proto is FunctionProto.    zUnexpected type z for proto.c                     g | ]	}|j         
S r&   namer+   is     r-   
<listcomp>z/ReferenceEvaluator.__init__.<locals>.<listcomp>       H H HA H H Hr0   c                     g | ]	}|j         
S r&   typer7   s     r-   r9   z/ReferenceEvaluator.__init__.<locals>.<listcomp>  r:   r0   c                     g | ]	}|j         
S r&   r5   )r+   os     r-   r9   z/ReferenceEvaluator.__init__.<locals>.<listcomp>	      !J!J!JQ!&!J!J!Jr0   c                     g | ]	}|j         
S r&   r<   r7   s     r-   r9   z/ReferenceEvaluator.__init__.<locals>.<listcomp>
  r@   r0   c                 (    i | ]}|j         |j        S r&   r6   r=   r7   s     r-   r.   z/ReferenceEvaluator.__init__.<locals>.<dictcomp>      HHHAHHHr0   
value_info)r    r   z for a function.	op_domainzClass z# must define attribute 'op_domain'.z& must inherit from OpRun (in new_ops).);r   copysetappendoutput_types_input_types_
isinstancer   
container_model_protostropenr
   bytesr   proto_
functions_attributes_r   graphonnx_graph_opset_importopsets_
ValueErrorr   r   dictr   list	attributer   r(   r   	TypeErrorr=   inputinput_names_outputoutput_names_initializersparse_initializerinits_nodenodes_hasattrrE   r6   
all_types_	__class__valuesr   r    new_ops_AttributeError
issubclassr   rF   __name___init)selfr   r   r   r    r!   r"   set_new_opsopf	all_types
shape_typeonxclkeys                  r-   __init__zReferenceEvaluator.__init__   s     	+-244!'ll- + +B,,r***! e^,, 	##DOO/EE"DOeS!! 	)eT""  aQ                             u%% 	)((EEG&(eZ(( 	I${DLL9KLLLDL! !NOOO$ !QRRRIIz** 	I$Dfd++ X !VWWW!DLL}-- 	I#DLL9KLLLDL! !QRRR#EO44Dy)) 	I#D5<2#5#5aa;M;O;ODLL GtE{{GGGHHH 	) H H1A1G H H HD H H1A1G H H HD!J!J$2B2I!J!J!JD!J!J$2B2I!J!J!JDt/;<<t 3@ @ DK */DKHH1A1GHHHIt{L11 A"&+"8 A AJ1;Ijo..'DOO $U[ 1 1D!%el!3!3DDK%++ )$g#j  	T 	Ta// T8<7d4?;Q;Q;S;S6T6T 9G 9 9DOAHaf$455  #566 T(C<=DOCJ$899#$RtAww$R$R$RSSS68 ( (r;// (HHHH   ""e,, Y#$WR$W$W$WXXXlBK/$-''%'c""

s   .C

CCrb   returnc                     t          |          }|j        }| j        r'| j                            |          r| j        |         S | j        t	          d          t	          d          )z#Returns a tensor saved as external.NzPReferenceEvaluator assumes a LargeContainer was loaded with its external tensor.zPAn instance of LargeContainer should be created before using ReferenceEvaluator.)r   locationrM   !is_in_memory_external_initializerRuntimeError)rp   rb   infor|   s       r-   retrieve_external_dataz)ReferenceEvaluator.retrieve_external_data8  s    ,,=? 	-tPP 
  
 	- ?8,,?&b   ^
 
 	
r0   ac           
         t          |t          t          t          f          r|S t          |t          j                  r| j        dk     r<|j         d|j         d|	                                 d|
                                 dS |                                                                }t          |          dk    rE|d d         }|j         d|j         dd                    t          t          |                     dS |j         d|j         d| S t!          |d	          r(d                    t          | j        |                    S |S )
N   :z in [, ]   ,z...rI   )rL   rO   intfloatnpndarrayr    dtypeshapeminmaxraveltolistlenjoinmaprg   _log_arg)rp   r   elementss      r-   r   zReferenceEvaluator._log_argJ  sA   a#sE*++ 	Ha$$ 	5|a'GGAGGG!%%''GGQUUWWGGGGwwyy''))H8}}q  #BQB<'OOAGOOchhs37I7I.J.JOOOOg4444(4441h 	499S22333r0   levelpatternargsc                 |     | j         k     r/ fd|D             }t          |t          |          z             d S d S )Nc                 :    g | ]}                     |          S r&   )r   )r+   r   rp   s     r-   r9   z+ReferenceEvaluator._log.<locals>.<listcomp>[  s%    777Qa((777r0   )r    printtuple)rp   r   r   r   new_argss   `    r-   _logzReferenceEvaluator._logY  sP    4<7777$777H'E(OO+,,,,,  r0   c                     | j         S )zReturns the input names.)r_   rp   s    r-   input_nameszReferenceEvaluator.input_names^         r0   c                     | j         S )z)Returns the input types if any specified.)rK   r   s    r-   input_typeszReferenceEvaluator.input_typesc  r   r0   c                     | j         S )zReturns the output names.)ra   r   s    r-   output_nameszReferenceEvaluator.output_namesh       !!r0   c                     | j         S )zReturns the output types.)rJ   r   s    r-   output_typeszReferenceEvaluator.output_typesm  r   r0   c                     | j         S )zReturns the opsets.)rX   r   s    r-   r   zReferenceEvaluator.opsetsr  s     |r0   c                 >    t          d | j        D                       S )z{Checks if the graph has a linked attribute (= an attribute whose value is defined
        by a function attribute.
        c              3   $   K   | ]}|j         V  d S N)has_linked_attribute)r+   re   s     r-   	<genexpr>z:ReferenceEvaluator.has_linked_attribute.<locals>.<genexpr>|  s%      HH4,HHHHHHr0   )any	rt_nodes_r   s    r-   r   z'ReferenceEvaluator.has_linked_attributew  s#    
 HHHHHHHHr0   c                     | j         j         dd                    | j                   dd                    | j                   S )N(r   z) -> )ri   rn   r   r   r   r   s    r-   __str__zReferenceEvaluator.__str__~  sB    .)llDIId6F,G,GlldiiX\XiNjNjlllr0   r6   excc                     | j         t          d|d          || j         vr,|r(t          d|dt          | j                    d          d S | j         |         S )NzUnable to return type for name z. Run shape_inference first.z, it was not found in .)rh   r~   sorted)rp   r6   r   s      r-   get_result_typesz#ReferenceEvaluator.get_result_types  s    ?"V$VVV   t&& "ndnnTZ[_[jTkTknnn   4t$$r0   c                     i  _         g  _         j        D ]B}t          |          r                     |          nt          |           j         |j        <   C fd j         j         j	         j
                                         j        d} j        rQd  j        j        D             }t!           j        d          r j        j        D ]}|j        ||j        <   | _        nd _         j        D ]}	                      |          }nt# t.          $ rg} j        r5 fd|j        D             }d|v r dd}n8                     ||          }n!t/          d	|j        d
|j        d          |Y d}~nd}~ww xY w	  |||          }	n,# t4          $ r}t5          d|d| d| d          |d}~ww xY w j                            |	           dS )z5Loads the implementation for every node in the graph.c                 "     j         d| g|R  S )N
   )r   )r   r   rp   s     r-   <lambda>z*ReferenceEvaluator._init.<locals>.<lambda>  s    )$)B*G$*G*G*G r0   )logr   r    r!   existing_functionsevaluator_clsc                 (    i | ]}|j         |j        S r&   rC   r7   s     r-   r.   z,ReferenceEvaluator._init.<locals>.<dictcomp>  rD   r0   rE   Nc                 >    g | ]}                     |d           S )F)r   )r   )r+   r8   rp   s     r-   r9   z,ReferenceEvaluator._init.<locals>.<listcomp>  s,    RRR!$//u/==RRRr0   )parentc                     t          |d| iS )Nr   )r   )r   r   s     r-   r   z*ReferenceEvaluator._init.<locals>.<lambda>  s    8R!9*09 9 r0   z*No implementation was found for node type  from domain z~. If this node has a context dependent implementation, you should run function infer_shapes before calling ReferenceEvaluator.zUnable to instantiate class z with run_params=z
 and node=r   )	rt_inits_r   rd   r   r   r   r6   r   r    rk   rS   rG   ri   rK   rV   r^   rg   rR   rE   r=   rh   rf   
_load_implr   op_typer(   r]   rI   )
rp   init
run_paramsrt   ru   re   rw   eitinsts
   `         r-   ro   zReferenceEvaluator._init  s   K 	 	D &d++-++D111&t,, N49%% HGGGk|}"&/"6"6"8"8!^
 

  	#HH1A1GHHHIt{L11 A"&+"8 A AJ1;Ijo..'DOO"DOK 	( 	(D__T**&    ? RRRRtzRRRBrzz26     "__T266->T\ > >bfbm > > >  	$r$
++   @2 @ @",@ @8<@ @ @  
 N!!$''''9	( 	(s1   :D
FAE<<FF
F;F66F;re   r   c           
         |j         | j        vr(t          d|j         d|j        d| j        d          | j        |j                  }|j         |j        f}d}|| j        v r&| j        |         }t          |t                    s|S d}|j         dk    rZdd	lm} 	  ||j         |j        ||| j	        
          S # t          $ r' |  ||j         |j        ||||| j	                  cY S w xY w|r t          d|j          d|j         d          |j         dk    r$dd	lm}  ||j         |j        || j	                  S |j         dk    r$dd	lm}	  |	|j         |j        || j	                  S |j         dk    r$dd	lm}
  |
|j         |j        || j	                  S || j        v r2dd	lm} | j        |         } ||j         |j        ||| j	                  S t          d|j        d|j         dt!          | j                   d          )z1Loads the implementation for a specified runtime.zDomain z (node type: z") is not specified. Known opsets: r   FTr2   r   )load_op)expandr   N)re   r   r   r   zkExpanding an operator with its function definition is only implemented for the main opset. Remove operator r   z# from the list of inlined operator.zai.onnx.preview.training)r   experimentalz
ai.onnx.ml)customr   z
Node type r   z is unknown, known functions: )r(   r   r~   r   rk   rm   r   onnx.reference.opsr   ri   r   NotImplementedError*onnx.reference.ops.aionnx_preview_trainingonnx.reference.ops.experimentalonnx.reference.ops.aionnxmlrS   r   )rp   re   r   r)   rx   r   rw   r   
load_op_ptload_op_exp
load_op_mlimpls               r-   r   zReferenceEvaluator._load_impl  sL    ;dk))D$+ D Ddl D D37;D D D   +dk*k4<'$- s#Bb+.. 	F;"222222wKL!"&.    '   &wKL +!"&.       	%S;S S!%S S S  
 ;444XXXXXX:T\7$.    ;.((NNNNNN;T\7$.    ;,&&IIIIII:T\7$.   
 $/!!222222?3'D7"n    "G G Gdk G G,24?,C,CG G G
 
 	
s   B9 9.C*)C*feed_inputs
attributesc                 P   || j         }t          | j        t                    r|t	                      ddi                    | j                                       |           | j                                        D ]\  }}|                     dd||           |                                D ]\  }}|                     dd||           | j	        D ]}|                     dd|j
        |j        |j                   |j        D ]N}|vrHt          d|d	t                     d
t          | j                   dt          |           d	          Ofd|j        D             }i }	|j        r|r||	d<   |                                r |j        |di|	}
n |j        |i |	}
t%          |j        |
          D ]"\  }}|                     dd||           ||<   #|D ]0}|vr*t          d|dt                     d| j                   1fd|D             S )aK  Executes the onnx model.

        Args:
            output_names: requested outputs by names, None for all
            feed_inputs: dictionary `{ input name: input value }`
            attributes: attributes value if the instance runs a
                FunctionProto

        Returns:
            list of requested outputs
        Nr2      z
 +C %s: %sz
 +I %s: %sr3   z%s(%s) -> %szUnable to find input z in known results z, self.rt_inits_ has z, feed_inputs has r   c                      g | ]
}|         S r&   r&   )r+   r8   resultss     r-   r9   z*ReferenceEvaluator.run.<locals>.<listcomp>E  s    555Qgaj555r0   linked_attributescontextz	 + %s: %szUnable to find output name z in z, proto is
c                      g | ]
}|         S r&   r&   )r+   r6   r   s     r-   r9   z*ReferenceEvaluator.run.<locals>.<listcomp>W  s    777$777r0   )r   rL   rR   r   r]   updater   itemsr   r   r   r^   r`   r~   r   r   need_contextrunzip)rp   r   r   r   kvre   r8   inputsr   outputsr6   valuer   s                @r-   r   zReferenceEvaluator.run!  s    ,Ldk=11 	j6H++ t*t~&&&{###N((** 	- 	-DAqIIaq!,,,,%%'' 	- 	-DAqIIaq!,,,, N 	& 	&DIIatz4;OOOZ  G##&B B Bvg B B.4T^.D.DB B+1++>+>B B B   $ 6555$*555F "( DZ D9C!"56  "" A"$(FQGQ?PQQ"$(F@.?@@"4;88 & &e		![$666 %&
 ! 	 	D7"""h$hhfWoohh[_[fhh   # 8777,7777r0   )NNr   NT)T)rz   Nr   )#rn   
__module____qualname____doc__r   r   r   rO   r   r   r	   r   r   boolry   r   r   arrayr   r   r   propertyr   r   r   r   r   r   r   r   ro   r   r   r   r   r&   r0   r-   r   r       s       g gX ,0PT)-l ll c3h(l D';]'J!KLM	l
 l $u+&l l l l l\
+ 
"( 
 
 
 
$# #    -# - -DI -$ - - - -
 ! ! X! ! ! X! " " X" " " X"   X I I XIm m m m m% %S %t %s % % % %7( 7( 7( 7(t CGX
 X
X
,4Y,?X
	X
 X
 X
 X
t68 68T#s(^ 68RVWZ\_W_R`Ia 68 68 68 68 68 68r0   r   )$ior   typingr   r   r   r   r   r	   numpyr   onnxr
   	onnx.defsr   onnx.external_data_helperr   r   onnx.model_containerr   onnx.onnx_pbr   r   r   r   r   r   onnx.reference.op_runr   r   r   r   r   onnx.reference.ops_optimizedr   r   r&   r0   r-   <module>r     s  
       : : : : : : : : : : : : : : : :           ( ( ( ( ( ( J J J J J J J J / / / / / /                             = < < < < <w8 w8 w8 w8 w8 w8 w8 w8 w8 w8r0   