class_docstr: 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.
class_docstr_tok: ["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","."]
docstr: Returns a tensor saved as external.
docstr_tok: ["Returns","a","tensor","saved","as","external","."]
code: def retrieve_external_data(
      self, initializer: TensorProto
  ) -> np.array:
      """Returns a tensor saved as external."""
      info = ExternalDataInfo(initializer)
      location = info.location
      if (
          self.container_
          and self.container_.is_in_memory_external_initializer(
              location
          )
      ):
          # It comes from a large container.
          return self.container_[location]
      # Otherwise, the data is on disk.
      if self.container_ is not None:
          raise RuntimeError(
              "ReferenceEvaluator assumes a LargeContainer was loaded with its external tensor."
          )
      raise RuntimeError(
          "An instance of LargeContainer should be created before using ReferenceEvaluator."
      )
  
imports: from io import BytesIO
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  from onnx import load
  from onnx.defs import onnx_opset_version
  from onnx.external_data_helper import ExternalDataInfo, uses_external_data
  from onnx.model_container import ModelContainer
  from onnx.onnx_pb import FunctionProto, GraphProto, ModelProto, NodeProto, TensorProto, TypeProto
  from onnx.reference.op_run import OpFunctionContextDependant, OpRun, OpRunExpand, RuntimeContextError, to_array_extended
  from onnx.reference.ops_optimized import optimized_operators
label_desc: Use image node_id 2 for calling the ReferenceEvaluator obj's underlying member method code with example usage: obj.retrieve_external_data(initializer) and returns: self