
    hDV                         S SK Jr  S SKJrJrJrJrJrJr  S SK	r
S SKJr  S SKJr  S SKJrJrJrJrJr  S SKJrJrJrJrJr  S SKJr   " S	 S
5      rg)    )BytesIO)AnyDictListOptionalTupleUnionN)load)onnx_opset_version)FunctionProto
GraphProto
ModelProto	NodeProto	TypeProto)OpFunctionContextDependantOpRunOpRunExpandRuntimeContextErrorto_array_extended)optimized_operatorsc                      \ rS rSrSr     S$S\S\\\\	4      S\\
\S \4         S\	S\\
\      S	\4S
 jjrS\S\4S jrS\	S\S\
\   SS4S jr\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS\4S jrS%S\S\S\4S jjrS&S jr S'S\S\\   S\4S jjrS'S \\\4   S!\\\\4      4S" jjrS#r g)(ReferenceEvaluator   a  
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.

:param proto: :class:`onnx.ModelProto`, :class:`onnx.GraphProto`,
    :class:`onnx.FunctionProto`, :class:`onnx.NodeProto`,
    filename or bytes
:param verbose: display intermediate results
    on the standard output during the execution
:param opsets: if *proto* is an instance of *GraphProto*,
    opsets must be defined by a dictionary of
:param functions: known onnx functions
:param 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.
:param 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.
Nprotoopsets	functionsverbosenew_ops	optimizedc                    U(       aI  Uc  [         R                  " 5       nO0[        U5      n[          H  nX;  d  M
  UR                  U5        M     S U l        S U l        [        U[        5      (       a!  [        US5       n	[        U	5      nS S S 5        O)[        U[        5      (       a  [        [        U5      5      nXl        0 U l        / U l        [        U[        5      (       aq  UR                   U l        UR$                   V
s0 s H  oR&                  U
R(                  _M     sn
U l        Ub  [-        S5      eUb  [-        S5      eUR.                  nGO[        U[0        5      (       a-  Xl        [        U[2        5      (       d  [-        S5      eX l        O[        U[4        5      (       af  S U l        UR$                   V
s0 s H  oR&                  U
R(                  _M     sn
U l        Ub  [-        S5      e[7        UR8                  5      U l        Ob[        U[:        5      (       a5  S U l        UR&                  UR&                  S:w  a  SO	[=        5       0U l        O[?        S[A        U5       S	35      eU R"                  (       Ga  U R"                  RB                   Vs/ s H  oRD                  PM     snU l#        U R"                  RB                   Vs/ s H  oR@                  PM     snU l        U R"                  RH                   Vs/ s H  oRD                  PM     snU l%        U R"                  RH                   Vs/ s H  oR@                  PM     snU l        [7        U R"                  RL                  5      [7        U R"                  RN                  5      -   U l(        U R"                  RR                  U l*        U R"                  RB                   Vs0 s H  oRD                  UR@                  _M     nn[W        U R                  S
5      (       a5  U R                  RX                   H  nUR@                  XRD                  '   M     Xl-        Oj[7        URB                  5      U l#        [7        URH                  5      U l%        / U l(        [        U[:        5      (       a	  U/U l*        OURR                  U l*        Ub  U H  n	[        U	[4        5      (       aR  [7        U R                  R]                  5       5      n[_        XUS9U R                  U	R&                  U	RD                  4'   Mj  [        U	[^        5      (       a2  U	R                  nXR                  UR&                  URD                  4'   M  [?        S[A        U	5      < S35      e   X@l0        0 U l1        Ub  U H  n[W        US5      (       d  [e        SU S35      e[g        U[h        5      (       d  [?        SU S35      eURj                  URl                  4nUU Rb                  ;   a  Mq  UU Rb                  U'   M     U Ro                  5         g ! , (       d  f       GN= fs  sn
f s  sn
f s  snf s  snf s  snf s  snf s  snf )Nrbz+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.z.opsets must be None if proto is FunctionProto.    zUnexpected type z for proto.
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).)8r   copysetappendoutput_types_input_types_
isinstancestropenr
   bytesr   proto_
functions_attributes_r   graphonnx_graph_opset_importdomainversionopsets_
ValueErrorr   r   dictr   list	attributer   r   	TypeErrortypeinputnameinput_names_outputoutput_names_initializersparse_initializerinits_nodenodes_hasattrr$   
all_types_valuesr   r   new_ops_AttributeError
issubclassr   r%   __name___init)selfr   r   r   r   r   r   set_new_opsopfdio	all_types
shape_typeexisting_functionsonxclkeys                      \/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/reference/reference_evaluator.py__init__ReferenceEvaluator.__init__   s    -224!'l-B,r* . " eS!!eT"aQ #"u%%(EEG&(eZ((${{D9>9K9KL9KAHHaii/9KLDL! !NOO$ !QRRIz**$fd++ !VWW!L}--#D9>9K9KL9KAHHaii/9KLDL! !QRR#EOO4Dy))#D5<<2#5a;M;ODL .tE{m;GHH151A1A1G1G H1GA1G HD151A1A1G1G H1GA1G HD262B2B2I2I!J2IQ&&2I!JD262B2B2I2I!J2IQ&&2I!JDt//;;<t  33@ DK **//DK151A1A1G1GH1GA1GIHt{{L11"&++"8"8J1;Ioo. #9'O $U[[ 1D!%ell!3DDK%++$g#jj a//)-doo.D.D.F)G&8J6H9DOOAHHaff$45  #566((C<=OOCJJ$89#&6tAwkAQ$RSS  68r;//( $GH  ""e,,#fRD0V$WXXllBKK/$--'%'c"  	

e #" M M !I H!J!J
 Is6    X/ Y Y!YYY Y Y/
X>areturnc           
         [        U[        [        [        45      (       a  U$ [        U[        R
                  5      (       a  U R                  S:  a>  UR                   SUR                   SUR                  5        SUR                  5        S3$ UR                  5       R                  5       n[        U5      S:  aA  US S nUR                   SUR                   SSR                  [        [        U5      5       S3$ UR                   SUR                   SU 3$ [!        US	5      (       a%  SR                  [        U R"                  U5      5      $ U$ )
N   :z in [, ]   ,z...r(   )r+   r,   intfloatnpndarrayr   dtypeshapeminmaxraveltolistlenjoinmaprH   _log_arg)rP   r`   elementss      r]   rv   ReferenceEvaluator._log_arg)  s   a#sE*++Ha$$||a''!AGG9E!%%'"QUUWIQGGwwy'')H8}q #BQ<''!AGG9Achhs37I.J-K3OOggYay(441h99S233    levelpatternargsc                     XR                   :  a8  U Vs/ s H  o@R                  U5      PM     nn[        U[        U5      -  5        g g s  snf N)r   rv   printtuple)rP   rz   r{   r|   r`   new_argss         r]   _logReferenceEvaluator._log8  s@    <<267$Qa($H7'E(O+,  7s   A	c                     U R                   $ )zReturns the input names.)r@   rP   s    r]   input_namesReferenceEvaluator.input_names=          ry   c                     U R                   $ )z)Returns the input types if any specified.)r*   r   s    r]   input_typesReferenceEvaluator.input_typesB  r   ry   c                     U R                   $ )zReturns the output names.)rB   r   s    r]   output_namesReferenceEvaluator.output_namesG       !!!ry   c                     U R                   $ )zReturns the output types.)r)   r   s    r]   output_typesReferenceEvaluator.output_typesL  r   ry   c                     U R                   $ )zReturns the opsets.)r7   r   s    r]   r   ReferenceEvaluator.opsetsQ  s     ||ry   c                 :    [        S U R                   5       5      $ )zl
Checks if the graph has a linked attribute (= an attribute whose value is defined
by a function attribute.
c              3   8   #    U  H  oR                   v   M     g 7fr~   )has_linked_attribute).0rF   s     r]   	<genexpr>:ReferenceEvaluator.has_linked_attribute.<locals>.<genexpr>\  s     H,,s   )any	rt_nodes_r   s    r]   r   'ReferenceEvaluator.has_linked_attributeV  s     HHHHry   c                     U R                   R                   SSR                  U R                  5       SSR                  U R                  5       3$ )N(re   z) -> )	__class__rN   rt   r   r   r   s    r]   __str__ReferenceEvaluator.__str__^  sE    ..))*!DIId6F6F,G+HdiiX\XiXiNjMkllry   r?   excc                     U R                   c  [        SU< S35      eXR                   ;  a.  U(       a&  [        SU< S[        U R                   5       S35      eg U R                   U   $ )NzUnable to return type for name z. Run shape_inference first.z, it was not found in .)rI   RuntimeErrorsorted)rP   r?   r   s      r]   get_result_types#ReferenceEvaluator.get_result_typesa  sx    ??"1$9UV  &"5dX=STZ[_[j[jTkSllmn  t$$ry   c                 h  ^  0 T l         / T l        T R                   H%  n[        U5      T R                   UR                  '   M'     U 4S jT R
                  T R                  T R                  S.nT R                  (       a  T R                  R                   Vs0 s H  o3R                  UR                  _M     nn[        T R                  S5      (       a5  T R                  R                   H  nUR                  XER                  '   M     UT l        OST l        T R                    H9  n T R#                  U5      n U" Xb5      n
T R                  R/                  U
5        M;     gs  snf ! [$         a  nT R                  (       aX  UR                   Vs/ s H  nT R'                  USS9PM     Os  snf n	nSU	;   a  T S.S jn SnANT R#                  Xi5      n SnAN[%        S	UR(                  < S
UR*                  < S35      UeSnAff = f! [,         a  n[-        SU< SU SU S35      UeSnAff = f)z7
Loads the implementation for every node in the graph.
c                 ,   > TR                   " SU /UQ76 $ )N
   )r   )r{   r|   rP   s     r]   <lambda>*ReferenceEvaluator._init.<locals>.<lambda>w  s    $))B*G$*Gry   )logr   r   r   r$   NF)r   )parentc                     [        USU 06$ )Nr   )r   )r   r|   s     r]   r   r     s    8R!9*09ry   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   rE   r   r?   r   r   rK   r*   r3   r>   r=   rH   r/   r$   rI   rG   
_load_implr   r   op_typer5   r<   r(   )rP   init
run_paramsrU   rW   rX   rF   r[   eitinsts   `          r]   rO   ReferenceEvaluator._initn  s    KKD(9$(?DNN499%   Hkk||}}	

 151A1A1G1GH1GA1GIHt{{L11"&++"8"8J1;Ioo. #9'DO"DOKKD__T*&$+ NN!!$'9   I '  ??GKzzRz!$//u/=zRBRrz26  "__T6-DT\\DTTabfbmbmap q= > 	(  22& 9"",ZvQ@ sN    E"(E':H'
H1HF+*HH)HH
H1H,,H1rF   r   c           
         UR                   U R                  ;  a6  [        SUR                   < SUR                  < SU R                  < S35      eU R                  UR                      nUR                   UR                  4nSnX@R                  ;   a(  U R                  U   n[        U[        5      (       d  U$ SnUR                   S:X  a#  SS	KJn   U" UR                   UR                  X5S
9$ U(       a&  [        SUR                    SUR                   S35      eUR                   S:X  a$  SS	KJn  U" UR                   UR                  U5      $ UR                   S:X  a$  SS	KJn	  U	" UR                   UR                  U5      $ UR                   S:X  a$  SS	KJn
  U
" UR                   UR                  U5      $ X@R                  ;   a1  SS	KJn  U R                  U   nU" UR                   UR                  X;S9$ [        SUR                  < SUR                   < S[        U R                  5       S35      e! [         a&    Uc  e U" UR                   UR                  UUUUS9s $ f = f)z3
Loads the implementation for a specified runtime.
zDomain z (node type: z") is not specified. Known opsets: r   FTr"   r   )load_op)expand)rF   r   r   zkExpanding an operator with its function definition is only implemented for the main opset. Remove operator rh   z# from the list of inlined operator.zai.onnx.preview.trainingexperimentalz
ai.onnx.ml)customz
Node type r   z is unknown, known functions: )r5   r   r   r   rK   rM   r   onnx.reference.opsr   r   NotImplementedError*onnx.reference.ops.aionnx_preview_trainingonnx.reference.ops.experimentalonnx.reference.ops.aionnxmlr0   r   )rP   rF   r   r6   r\   r   r[   r   
load_op_ptload_op_exp
load_op_mlimpls               r]   r   ReferenceEvaluator._load_impl  s4    ;;dkk)$++dll5E F337;;/D  ++dkk*kk4<<'-- s#Bb+..	F;;"2t{{DLL'QQ %K;;-q.QS 
 ;;44Xdkk4<<AA;;.(Nt{{DLL'BB;;,&Idkk4<<AA //!2??3'D4;;gKK!(dkk_ E,,24??,C+DAG
 	
O ' 
&KKLL +! 
s   H5 5-I%$I%feed_inputs
attributesc           	         Uc  U R                   n[        U R                  [        5      (       a  Uc
  [	        5       eSS0nUR                  U R                  5        UR                  U5        U R                  R                  5        H  u  pVU R                  SSXV5        M     UR                  5        H  u  pVU R                  SSXV5        M     U R                   GH  nU R                  SSUR                  UR                  UR                  5        UR                   Vs/ s H  oU   PM	     n	n0 n
UR                  (       a  U(       a  X:S'   UR                  5       (       a  UR                  " U	S	U0U
D6nOUR                  " U	0 U
D6n[!        UR                  U5       HM  u  p[        U["        5      (       a  [	        S
[%        U5       SU< S35      eU R                  SSX5        XU'   MO     GM     / nU HC  nX;  a(  ['        SU< S[)        U5       SU R                   35      eUR+                  XL   5        ME     U$ s  snf )a  
Executes the onnx model.

:param output_names: requested outputs by names,
    None for all
:param feed_inputs: dictionary `{ input name: input value }`
:param attributes: attributes value if the instance runs a FunctionProto
:return: list of requested outputs
Nr"      z
 +C %s: %sz
 +I %s: %sr#   z%s(%s) -> %slinked_attributescontextzUnexected type z for output r   z	 + %s: %szUnable to find output name z in z, proto is
)r   r+   r/   r   r<   updater   itemsr   r   r   r>   rA   r   need_contextrunzipr   r=   r   r   r(   )rP   r   r   r   resultskvrF   rU   inputsr   outputsr?   valuelist_resultss                  r]   r   ReferenceEvaluator.run  s    ,,Ldkk=11j6H+ t*t~~&{#NN((*DAIIaq, +%%'DAIIaq, ( NNDIIatzz4;;O*.**5*Qaj*F5 "((Z9C"56  ""((FQGQ?PQ((F@.?@"4;;8eU++#)$u+l4(!L  		![$6 %  9 #& #% D""1$fWo=Nl[_[f[fZgh  . ! 1 6s   I)rI   r1   r0   rE   r@   r*   rK   rG   r3   r7   rB   r)   r/   r   r   r   )NNr   NT)T)ra   Nr~   )!rN   
__module____qualname____firstlineno____doc__r   r   r   r,   ri   r   r	   r   r   boolr^   rv   r   propertyr   r   r   r   r   r   r   r   rO   r   r   r   r   __static_attributes__ ry   r]   r   r      s   gX ,0PT)-ff c3h(f D';]'J!KLM	f
 f $u+&f fP# # -# - -DI -$ -
 ! ! ! ! " " " "   I Im m%S %t %s %3(l CGG
G
,4Y,?G
	G
R3T#s(^ 3RVWZ\_W_R`Ia 3 3ry   r   )ior   typingr   r   r   r   r   r	   numpyrk   onnxr
   	onnx.defsr   onnx.onnx_pbr   r   r   r   r   onnx.reference.op_runr   r   r   r   r   onnx.reference.ops_optimizedr   r   r   ry   r]   <module>r      s:   
  : :   ( T T  =H Hry   