code: def iter_chunk_and_save(
      data: Generator,
      chunk_size: int,
      data_args: dict,
      output_dir: str,
      recorder_callback=None,
  ):
      os.makedirs(output_dir, exist_ok=True)
      chunk_i = 0
      length = 0
      current_chunk = []
      for datum in data:
          if recorder_callback is not None:
              recorder_callback(datum)
          length += 1
          current_chunk.append(datum)
          if len(current_chunk) == chunk_size:
              torch.save(
                  current_chunk,
                  os.path.join(output_dir, f"data_{chunk_i:05d}.chunk"),
              )
              chunk_i += 1
              current_chunk = []
      if current_chunk:
          torch.save(
              current_chunk,
              os.path.join(output_dir, f"data_{chunk_i:05d}.chunk"),
          )
          chunk_i += 1
      data_args = data_args.copy()
      data_args["num_chunks"] = chunk_i
      data_args["length"] = length
      torch.save(data_args, os.path.join(output_dir, "data_args.p"))
  
imports: import math
  import numpy
  import os
  from typing import Generator, Union, Sequence
  import torch
  import torch.utils.data.dataset
label_desc: Use image node_id 3 for calling a global function with example usage: iter_chunk_and_save(data, chunk_size, data_args, output_dir, recorder_callback) without return types