class_docstr: A dispatcher that finds the best ONNX Function for ATen/Custom operators.
  
  It uses the `torch.ops` name to find the function. If not found, it falls back to default.
  Otherwise, the best match is found among all function overloads. An exact match has
  higher precedence over the closest ones.
  
  Below is a breakdown on how the dispatch mechanism works:
  
  1. Use the torch.ops name to find the function:
      a. Check if the ATen overload exists in the registry.
      b. If not, check if the default overload exists in the registry.
  
  2. Find the nearest match among all overloaded functions:
      a. If the types match perfectly, select the function.
      b. Otherwise, find the nearest one with the highest matching score. Because of
          the potential wrongly annotated dtypes and attributes matching, we use
          nearest match to find the best function once the aten name is targeted.
  
  3. Tie-breaker: If there are multiple nearest matches, we will select the one with
      the highest matching score.
  
  NOTE: The nearest match `doesn't guarantee` a correct match, and a warning message is logged.
docstr: Find the perfect/nearest matched OnnxFunction for the given FX node, arguments, and keyword arguments.
  
  Args:
      default_and_custom_functions: The list includes overloaded functions, with
          custom ones appearing after the default ones.
      onnx_args: Arguments organized in PyTorch inputs way.
      onnx_kwargs: Keyword arguments organized in PyTorch inputs way.
      diagnostic_context: The diagnostic context to use for reporting errors.
  
      Returns:
          Either an `onnxscript.OnnxFunction` or `onnxscript.TracedOnnxFunction` instance based on the dispatch algorithm.
      Raises:
          RuntimeError: If there are no overloaded functions available for the given FX node.
docstr_tok: ["Find","the","perfect\/nearest","matched","OnnxFunction","for","the","given","FX","node",",","arguments",",","and","keyword","arguments",".","Args",":","default_and_custom_functions",":","The","list","includes","overloaded","functions",",","with","custom","ones","appearing","after","the","default","ones",".","onnx_args",":","Arguments","organized","in","PyTorch","inputs","way",".","onnx_kwargs",":","Keyword","arguments","organized","in","PyTorch","inputs","way",".","diagnostic_context",":","The","diagnostic","context","to","use","for","reporting","errors",".","Returns",":","Either","an","`","onnxscript.OnnxFunction","`","or","`","onnxscript.TracedOnnxFunction","`","instance","based","on","the","dispatch","algorithm",".","Raises",":","RuntimeError",":","If","there","are","no","overloaded","functions","available","for","the","given","FX","node","."]
code: def _find_the_perfect_or_nearest_match_onnxfunction(
      self,
      node: torch.fx.Node,  # this is used in diagnostic_message_formatter
      default_and_custom_functions: List[registration.ONNXFunction],
      onnx_args: Sequence[
          Optional[
              Union[
                  fx_type_utils.TensorLike,
                  str,
                  int,
                  float,
                  bool,
                  list,
                  complex,
              ]
          ]
      ],
      onnx_kwargs: Dict[str, fx_type_utils.Argument],
      diagnostic_context: diagnostics.DiagnosticContext,
  ):
      """Find the perfect/nearest matched OnnxFunction for the given FX node, arguments, and keyword arguments.
  
      Args:
          default_and_custom_functions: The list includes overloaded functions, with
              custom ones appearing after the default ones.
          onnx_args: Arguments organized in PyTorch inputs way.
          onnx_kwargs: Keyword arguments organized in PyTorch inputs way.
          diagnostic_context: The diagnostic context to use for reporting errors.
  
          Returns:
              Either an `onnxscript.OnnxFunction` or `onnxscript.TracedOnnxFunction` instance based on the dispatch algorithm.
          Raises:
              RuntimeError: If there are no overloaded functions available for the given FX node.
      """
      overload_match_ranking: Dict[
          registration.ONNXFunction, Optional[int]
      ] = {}
      diagnostic = diagnostic_context.inflight_diagnostic()
  
      # Iterate the overloaded functions in reverse order to prioritize the custom ones
      # over the default ones, and find the perfect match.
      for symbolic_function in reversed(default_and_custom_functions):
          function_opschema = _OnnxSchemaChecker(
              symbolic_function.onnx_function
          )
  
          # NOTE: 1. If the perfect match is found, return the function
          if function_opschema.perfect_match_inputs(
              diagnostic, onnx_args, onnx_kwargs
          ):
              return symbolic_function.onnx_function
          # Record the match score for the nearest match if it's not the perfect match
          overload_match_ranking[
              symbolic_function
          ] = function_opschema.match_score
  
      # NOTE: 2. If there is no perfect match, find the nearest match among the nearest matche candidates
      # If there is no nearest match, raise an error
      overload_match_ranking = {
          k: v
          for k, v in overload_match_ranking.items()
          if v is not None
      }
      if not overload_match_ranking:
          # If there are no overloaded functions available for the given FX node, raise an
          # unsupported error
          op_full_name = self._get_aten_name(
              node, diagnostic_context
          ).qualified_name()
          diagnostic = diagnostics.UnsupportedFxNodeDiagnostic(
              diagnostics.rules.no_symbolic_function_for_call_function,
              diagnostics.levels.ERROR,
              f"Cannot find any perfect/nearest match of symbolic function for {op_full_name},"
              f"which should be registered under {node.target}.",
              unsupported_fx_node=node,
          )
          diagnostic_context.log(diagnostic)
          raise diagnostics.RuntimeErrorWithDiagnostic(diagnostic)
  
      diagnostic.warning(
          "### Exact match is not found!\n"
          "Cannot find a perfect match of symbolic overload, "
          "a nearest match is found. Please check the ONNX output carefully. \n",
      )
      diagnostic.level = diagnostics.levels.WARNING
      # NOTE: 3. Tie breaker: if there are multiple nearest matches, we will choose the one
      # that is custom first. If there are multiple custom ones, we will choose the one
      # that is added lastly in the list.
      symbolic_function_list: List[registration.ONNXFunction] = sorted(
          overload_match_ranking,
          key=lambda k: (
              overload_match_ranking[k],
              k.is_custom,
              default_and_custom_functions.index(k),
          ),
          reverse=True,
      )
      return symbolic_function_list[0].onnx_function
  
imports: from __future__ import annotations
  import logging
  import operator
  import types
  from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, TYPE_CHECKING, Union
  import torch
  import torch._ops
  import torch.fx
  from torch.onnx._internal import _beartype
  from torch.onnx._internal.fx import diagnostics, registration, type_utils
  from onnxscript.function_libs.torch_lib import graph_building
label_desc: Use image node_id 4 for calling the OnnxFunctionDispatcher obj's underlying member method code with example usage: obj._find_the_perfect_or_nearest_match_onnxfunction(node, default_and_custom_functions, onnx_args, onnx_kwargs, diagnostic_context) and returns: symbolic_function_list, symbolic_function