code: def _get_submodels_and_onnx_configs(
      model: Union["PreTrainedModel", "TFPreTrainedModel"],
      task: str,
      monolith: bool,
      custom_onnx_configs: Dict,
      custom_architecture: bool,
      _variant: str,
      int_dtype: str = "int64",
      float_dtype: str = "fp32",
      fn_get_submodels: Optional[Callable] = None,
      preprocessors: Optional[List[Any]] = None,
      legacy: bool = False,
      library_name: str = "transformers",
      model_kwargs: Optional[Dict] = None,
  ):
      is_stable_diffusion = "stable-diffusion" in task
      if not custom_architecture:
          if is_stable_diffusion:
              onnx_config = None
              models_and_onnx_configs = (
                  get_stable_diffusion_models_for_export(
                      model,
                      int_dtype=int_dtype,
                      float_dtype=float_dtype,
                  )
              )
          else:
              onnx_config_constructor = (
                  TasksManager.get_exporter_config_constructor(
                      model=model,
                      exporter="onnx",
                      task=task,
                      library_name=library_name,
                  )
              )
              onnx_config = onnx_config_constructor(
                  model.config,
                  int_dtype=int_dtype,
                  float_dtype=float_dtype,
                  preprocessors=preprocessors,
                  legacy=legacy,
              )
  
              onnx_config.variant = _variant
              all_variants = "\n".join(
                  [
                      f"    - {name}: {description}"
                      for name, description in onnx_config.VARIANTS.items()
                  ]
              )
              logger.info(
                  f"Using the export variant {onnx_config.variant}. Available variants are:\n{all_variants}"
              )
  
              # TODO: this succession of if/else strongly suggests a refactor is needed.
              if (
                  model.config.is_encoder_decoder
                  and task.startswith(
                      TasksManager._ENCODER_DECODER_TASKS
                  )
                  and not monolith
              ):
                  models_and_onnx_configs = (
                      get_encoder_decoder_models_for_export(
                          model, onnx_config
                      )
                  )
              elif task.startswith("text-generation") and not monolith:
                  models_and_onnx_configs = (
                      get_decoder_models_for_export(
                          model, onnx_config, legacy=legacy
                      )
                  )
              elif model.config.model_type == "sam":
                  models_and_onnx_configs = get_sam_models_for_export(
                      model, onnx_config
                  )
              elif model.config.model_type == "speecht5":
                  models_and_onnx_configs = (
                      get_speecht5_models_for_export(
                          model, onnx_config, model_kwargs
                      )
                  )
              else:
                  models_and_onnx_configs = {
                      "model": (model, onnx_config)
                  }
  
          # When specifying custom ONNX configs for supported transformers architectures, we do
          # not force to specify a custom ONNX config for each submodel.
          for key, custom_onnx_config in custom_onnx_configs.items():
              models_and_onnx_configs[key] = (
                  models_and_onnx_configs[key][0],
                  custom_onnx_config,
              )
      else:
          onnx_config = None
          submodels_for_export = None
          models_and_onnx_configs = {}
  
          if fn_get_submodels is not None:
              submodels_for_export = fn_get_submodels(model)
          else:
              if is_stable_diffusion:
                  submodels_for_export = (
                      _get_submodels_for_export_stable_diffusion(model)
                  )
              elif (
                  model.config.is_encoder_decoder
                  and task.startswith(
                      TasksManager._ENCODER_DECODER_TASKS
                  )
                  and not monolith
              ):
                  submodels_for_export = (
                      _get_submodels_for_export_encoder_decoder(
                          model, use_past=task.endswith("-with-past")
                      )
                  )
              elif task.startswith("text-generation") and not monolith:
                  submodels_for_export = (
                      _get_submodels_for_export_decoder(
                          model, use_past=task.endswith("-with-past")
                      )
                  )
              else:
                  submodels_for_export = {"model": model}
  
          if submodels_for_export.keys() != custom_onnx_configs.keys():
              logger.error(
                  f"ONNX custom configs for: {', '.join(custom_onnx_configs.keys())}"
              )
              logger.error(
                  f"Submodels to export: {', '.join(submodels_for_export.keys())}"
              )
              raise ValueError(
                  "Trying to export a custom model, but could not find as many custom ONNX configs as the number of submodels to export. Please specifiy the fn_get_submodels argument, that should return a dictionary of submodules with as many items as the provided custom_onnx_configs dictionary."
              )
  
          for key, custom_onnx_config in custom_onnx_configs.items():
              models_and_onnx_configs[key] = (
                  submodels_for_export[key],
                  custom_onnx_config,
              )
  
      # Default to the first ONNX config for stable-diffusion and custom architecture case.
      if onnx_config is None:
          onnx_config = next(iter(models_and_onnx_configs.values()))[1]
  
      return onnx_config, models_and_onnx_configs
  
imports: import argparse
  import os
  from pathlib import Path
  from packaging import version
  from requests.exceptions import ConnectionError
  from transformers import AutoConfig, AutoTokenizer
  from transformers.utils import is_torch_available
  from ...commands.export.onnx import parse_args_onnx
  from ...configuration_utils import _transformers_version
  from ...utils import DEFAULT_DUMMY_SHAPES, ONNX_WEIGHTS_NAME, logging
  from ...utils.modeling_utils import MODEL_TO_PATCH_FOR_PAST
  from ...utils.save_utils import maybe_load_preprocessors, maybe_save_preprocessors
  from ..error_utils import AtolError, OutputMatchError, ShapeError
  from ..tasks import TasksManager
  from .base import OnnxConfigWithPast
  from .constants import SDPA_ARCHS_ONNX_EXPORT_NOT_SUPPORTED, UNPICKABLE_ARCHS
  from .convert import export_models, validate_models_outputs
  from .utils import MODEL_TYPES_REQUIRING_POSITION_IDS, _get_submodels_for_export_decoder, _get_submodels_for_export_encoder_decoder, _get_submodels_for_export_stable_diffusion, get_decoder_models_for_export, get_encoder_decoder_models_for_export, get_sam_models_for_export, get_speecht5_models_for_export, get_stable_diffusion_models_for_export
  from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
label_desc: Use image node_id 1 for calling a global function with example usage: _get_submodels_and_onnx_configs(model, task, monolith, custom_onnx_configs, custom_architecture, _variant, int_dtype, float_dtype, fn_get_submodels, preprocessors, legacy, library_name, model_kwargs) and returns: onnx_config,  models_and_onnx_configs