code: def offload_weight(weight, weight_name, offload_folder, index=None):
      dtype = None
      # Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
      if str(weight.dtype) == "torch.bfloat16":
          # Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
          weight = weight.view(torch.int16)
          dtype = "bfloat16"
      array = weight.cpu().numpy()
      tensor_file = os.path.join(offload_folder, f"{weight_name}.dat")
      if index is not None:
          if dtype is None:
              dtype = str(array.dtype)
          index[weight_name] = {
              "dtype": dtype,
              "shape": list(array.shape),
          }
      if array.ndim == 0:
          array = array[None]
      file_array = np.memmap(
          tensor_file, dtype=array.dtype, mode="w+", shape=array.shape
      )
      file_array[:] = array[:]
      file_array.flush()
      return index
  
imports: import json
  import os
  from collections.abc import Mapping
  from typing import Dict, List, Optional, Union
  import numpy
  import torch
  from safetensors import safe_open
label_desc: Use image node_id 1 for calling a global function with example usage: offload_weight(weight, weight_name, offload_folder, index) and returns: index