code: def measure_peak_memory_cpu(
      function: Callable[[], None], interval=0.5, device_idx=None
  ) -> int:
      """
      measures peak cpu memory consumption of a given `function`
      running the function for at least interval seconds
      and at most 20 * interval seconds.
      This function is heavily inspired by: `memory_usage`
      of the package `memory_profiler`: https://github.com/pythonprofilers/memory_profiler/blob/895c4ac7a08020d66ae001e24067da6dcea42451/memory_profiler.py#L239
  
      Args:
          - `function`: (`callable`): function() -> ...
              function without any arguments to measure for which to measure the peak memory
  
          - `interval`: (`float`, `optional`, defaults to `0.5`)
              interval in second for which to measure the memory usage
  
          - `device_idx`: (`int`, `optional`, defaults to `None`)
              device id for which to measure gpu usage
  
      Returns:
          - `max_memory`: (`int`)
              cosumed memory peak in Bytes
      """
  
      if not is_psutil_available():
          logger.warning(
              "Psutil not installed, we won't log CPU memory usage. "
              "Install Psutil (pip install psutil) to use CPU memory tracing."
          )
          max_memory = "N/A"
      else:
          while True:
              # create child, parent connection
              child_connection, parent_connection = Pipe()
  
              # instantiate process
              mem_process = MemoryMeasureProcess(
                  os.getpid(), child_connection, interval
              )
              mem_process.start()
  
              # wait until we get memory
              parent_connection.recv()
  
              try:
                  # execute function
                  function()
  
                  # start parent connection
                  parent_connection.send(0)
  
                  # receive memory and num measurements
                  max_memory = parent_connection.recv()
                  num_measurements = parent_connection.recv()
              except Exception:
                  # kill process in a clean way
                  parent = psutil.Process(os.getpid())
                  for child in parent.children(recursive=True):
                      os.kill(child.pid, SIGKILL)
                  mem_process.join(0)
                  raise RuntimeError("Process killed. Error in Process")
  
              # run process at least 20 * interval or until it finishes
              mem_process.join(20 * interval)
  
              if (num_measurements > 4) or (interval < 1e-6):
                  break
  
              # reduce interval
              interval /= 10
  
          return max_memory
  
imports: import copy
  import csv
  import linecache
  import logging
  import os
  import platform
  import sys
  import timeit
  import numpy
  import gluonnlp
  from gluonnlp.models import get_backbone
  from gluonnlp.utils.misc import logging_config
  from gluonnlp.utils.lazy_imports import try_import_tvm
  from collections import defaultdict, namedtuple
  from datetime import datetime
  import multiprocessing
  from multiprocessing import Pipe, Process, Queue
  from multiprocessing.connection import Connection
  from typing import Callable, Iterable, List, NamedTuple, Optional, Union, Tuple
label_desc: Use image node_id 9 for calling a global function with example usage: measure_peak_memory_cpu(function, interval, device_idx) and returns: max_memory