code: def _dispatch_opset_version(
      target: OpsetVersion, registered_opsets: Collection[OpsetVersion]
  ) -> Optional[OpsetVersion]:
      """Finds the registered opset given a target opset version and the available opsets.
  
      Args:
          target: The target opset version.
          registered_opsets: The available opsets.
  
      Returns:
          The registered opset version.
      """
      if not registered_opsets:
          return None
  
      descending_registered_versions = sorted(
          registered_opsets, reverse=True
      )
      # Linear search for the opset version, which is fine since the number of opset
      # versions is small.
  
      if target >= _constants.ONNX_BASE_OPSET:
          # Always look down toward opset 1 when the target is >= ONNX_BASE_OPSET (opset 9).
          # When a custom op is register at opset 1, we want to be able to discover it as a
          # fallback for all opsets >= ONNX_BASE_OPSET.
          for version in descending_registered_versions:
              if version <= target:
                  return version
          return None
  
      # target < opset 9. This is the legacy behavior to support opset 7 and opset 8.
      # for caffe2 support. We search up toward opset 9.
      for version in reversed(descending_registered_versions):
          # Count back up until _constants.ONNX_BASE_OPSET
          if target <= version <= _constants.ONNX_BASE_OPSET:
              return version
  
      return None
  
imports: import warnings
  from typing import Callable, Collection, Dict, Generic, Optional, Sequence, Set, TypeVar, Union
  from torch.onnx import _constants, errors
  from torch.onnx._internal import _beartype
label_desc: Use image node_id 1 for calling a global function with example usage: _dispatch_opset_version(target, registered_opsets) and returns: None, None, None, version, version