code: def check_and_save_model(
      model: onnx.ModelProto, save_path: Optional[Union[str, Path]]
  ):
      # for large models, a path must be provided instead of a ModelProto:
      # https://github.com/onnx/onnx/blob/main/docs/PythonAPIOverview.md#checking-a-large-onnx-model-2gb
      if model.ByteSize() < onnx.checker.MAXIMUM_PROTOBUF:
          # For the try catch, refer to https://github.com/microsoft/onnxruntime/issues/14768
          try:
              onnx.checker.check_model(model)
          except Exception as e:
              if "No Op registered for" in str(e):
                  pass
              else:
                  raise e
          if save_path:
              # Overwrite.
              save_path = Path(save_path).as_posix()
              external_file_name = os.path.basename(save_path) + "_data"
              # path/to/model.onnx_data
              external_path = os.path.join(
                  os.path.dirname(save_path), external_file_name
              )
  
              if save_path.endswith(".onnx") and os.path.isfile(
                  save_path
              ):
                  os.remove(save_path)
              if os.path.isfile(external_path):
                  # The new model may be below the maximum protobuf size, overwritting a model that was larger. Hence this os.remove.
                  os.remove(external_path)
  
              onnx.save(model, save_path)
      elif save_path is not None:
          # path/to/model.onnx
          save_path = Path(save_path).as_posix()
  
          external_file_name = os.path.basename(save_path) + "_data"
          # path/to/model.onnx_data
          external_path = os.path.join(
              os.path.dirname(save_path), external_file_name
          )
  
          if save_path.endswith(".onnx") and os.path.isfile(save_path):
              os.remove(save_path)
          if os.path.isfile(external_path):
              os.remove(external_path)
  
          onnx.save(
              model,
              save_path,
              save_as_external_data=True,
              all_tensors_to_one_file=True,
              location=external_file_name,
          )
          try:
              onnx.checker.check_model(save_path)
          except Exception as e:
              if "No Op registered for" in str(e):
                  pass
              else:
                  raise e
      else:
          logger.info(
              "Merged ONNX model exceeds 2GB, the model will not be checked without `save_path` given."
          )
  
file_path: optimum/optimum/onnx/graph_transformations.py
imports: import copy
  import os
  from pathlib import Path
  from typing import TYPE_CHECKING, List, Optional, Union
  import onnx
  from onnx import ModelProto
  from ..utils import logging
  from .transformations_utils import _create_name_sharing_dict, _deduplicate_gather_matmul, _deduplicated_cross_model_initializers, _find_duplicate_initializers, _find_matching_initializers, _get_all_inputs, _get_onnx_opset, _get_weights_to_tie, _remove_redundant_initializers, _replace_input_names, _unify_onnx_outputs, cast_int64_tensorproto_to_int32
label_desc: Use image node_id 4 for calling a global function with example usage: check_and_save_model(model, save_path) without return types