code: def _create_tensor_proto_with_external_data(
      tensor: torch.Tensor, name: str, location: str, basepath: str
  ) -> onnx.TensorProto:  # type: ignore[name-defined]
      """Create a TensorProto with external data from a PyTorch tensor.
      The external data is saved to os.path.join(basepath, location).
  
      Args:
          tensor: Tensor to be saved.
          name: Name of the tensor (i.e., initializer name in ONNX graph).
          location: Relative location of the external data file
              (e.g., "/tmp/initializers/weight_0" when model is "/tmp/model_name.onnx").
          basepath: Base path of the external data file (e.g., "/tmp/external_data" while model must be in "/tmp").
  
  
      Reference for ONNX's external data format:
          How to load?
          https://github.com/onnx/onnx/blob/5dac81ac0707bdf88f56c35c0a5e8855d3534673/onnx/external_data_helper.py#L187
          How to save?
          https://github.com/onnx/onnx/blob/5dac81ac0707bdf88f56c35c0a5e8855d3534673/onnx/external_data_helper.py#L43
          How to set ONNX fields?
          https://github.com/onnx/onnx/blob/5dac81ac0707bdf88f56c35c0a5e8855d3534673/onnx/external_data_helper.py#L88
      """
      # FIXME: Avoid importing onnx into torch.onnx.
      import onnx
  
      tensor_proto = onnx.TensorProto()  # type: ignore[attr-defined]
      tensor_proto.name = name
      tensor_proto.data_type = jit_type_utils.JitScalarType.from_dtype(
          tensor.dtype
      ).onnx_type()
      tensor_proto.dims.extend(tensor.shape)
      tensor_proto.data_location = onnx.TensorProto.EXTERNAL  # type: ignore[attr-defined]
  
      # Settings for saving one tensor per file.
      # Offset is zero because there is no other tensor in the same file.
      key_value_pairs = {
          "location": location,
          "offset": 0,
          "length": tensor.untyped_storage().nbytes(),
      }
      for k, v in key_value_pairs.items():
          entry = tensor_proto.external_data.add()
          entry.key = k
          entry.value = str(v)
  
      # Actual path to write content of tensor.
      external_data_file_path = os.path.join(basepath, location)
      if os.path.exists(external_data_file_path):
          os.remove(external_data_file_path)
  
      # Create external data's folder if not exists.
      external_data_dir_path = os.path.dirname(external_data_file_path)
      if not os.path.exists(external_data_dir_path):
          # if the demo_folder directory is not present
          # then create it.
          os.makedirs(external_data_dir_path)
  
      # Create a fresh file.
      with open(external_data_file_path, "xb") as data_file:
          # No need to call "seek" because offset is 0.
          # data_file.seek(0)
          # Write tensor content to the file.
          data_file.write(tensor.numpy(force=True).tobytes())
  
      return tensor_proto
  
file_path: pytorch/torch/onnx/_internal/fx/serialization.py
imports: from __future__ import annotations
  import io
  import os
  from typing import Tuple, TYPE_CHECKING, Union
  import torch
  from torch.onnx import _type_utils
  from torch.onnx._internal import _beartype
label_desc: Use image node_id 1 for calling a global function with example usage: _create_tensor_proto_with_external_data(tensor, name, location, basepath) and returns: tensor_proto