code: def memory_summary(
      device: Union[Device, int] = None, abbreviated: bool = False
  ) -> str:
      r"""Return a human-readable printout of the current memory allocator statistics for a given device.
  
      This can be useful to display periodically during training, or when
      handling out-of-memory exceptions.
  
      Args:
          device (torch.device or int, optional): selected device. Returns
              printout for the current device, given by :func:`~torch.cuda.current_device`,
              if :attr:`device` is ``None`` (default).
          abbreviated (bool, optional): whether to return an abbreviated summary
              (default: False).
  
      .. note::
          See :ref:`cuda-memory-management` for more details about GPU memory
          management.
      """
      device = _get_device_index(device, optional=True)
      stats = memory_stats(device=device)
  
      def _format_size(sz, pref_sz):
          prefixes = ["B  ", "KiB", "MiB", "GiB", "TiB", "PiB"]
          prefix = prefixes[0]
          for new_prefix in prefixes[1:]:
              if pref_sz < 768 * 1024:
                  break
              prefix = new_prefix
              sz //= 1024
              pref_sz /= 1024
          return f"{sz:6d} {prefix}"
  
      def _format_count(cnt, pref_cnt):
          prefixes = [" ", "K", "M"]
          prefix = prefixes[0]
          for new_prefix in prefixes[1:]:
              if pref_cnt < 750 * 1000:
                  break
              prefix = new_prefix
              cnt //= 1000
              pref_cnt /= 1000
          return f"{cnt:7d} {prefix} "
  
      metrics_to_display = [
          ("allocated_bytes", "Allocated memory", _format_size),
          ("active_bytes", "Active memory", _format_size),
          ("requested_bytes", "Requested memory", _format_size),
          ("reserved_bytes", "GPU reserved memory", _format_size),
          (
              "inactive_split_bytes",
              "Non-releasable memory",
              _format_size,
          ),
          ("allocation", "Allocations", _format_count),
          ("active", "Active allocs", _format_count),
          ("segment", "GPU reserved segments", _format_count),
          ("inactive_split", "Non-releasable allocs", _format_count),
      ]
  
      lines = []
      lines.append("=" * 75)
      lines.append(
          " {_:16} PyTorch CUDA memory summary, device ID {device:<17d} "
      )
      lines.append("-" * 75)
      lines.append(
          "  {_:9} CUDA OOMs: {num_ooms:<12d} | {_:6} cudaMalloc retries: {num_alloc_retries:<8d}  "
      )
      lines.append("=" * 75)
      lines.append(
          "        Metric         | Cur Usage  | Peak Usage | Tot Alloc  | Tot Freed  "
      )
  
      for metric_key, metric_name, formatter in metrics_to_display:
          lines.append("-" * 75)
          submetrics = [("all", metric_name)]
          if not abbreviated:
              submetrics.append(("large_pool", "      from large pool"))
              submetrics.append(("small_pool", "      from small pool"))
  
          (
              current_prefval,
              peak_prefval,
              allocated_prefval,
              freed_prefval,
          ) = (
              None,
              None,
              None,
              None,
          )
  
          for submetric_key, submetric_name in submetrics:
              prefix = metric_key + "." + submetric_key + "."
  
              current = stats[prefix + "current"]
              peak = stats[prefix + "peak"]
              allocated = stats[prefix + "allocated"]
              freed = stats[prefix + "freed"]
  
              if current_prefval is None:
                  current_prefval = current
                  peak_prefval = peak
                  allocated_prefval = allocated
                  freed_prefval = freed
  
              lines.append(
                  " {:<21} | {} | {} | {} | {} ".format(
                      submetric_name,
                      formatter(current, current_prefval),
                      formatter(peak, peak_prefval),
                      formatter(allocated, allocated_prefval),
                      formatter(freed, freed_prefval),
                  ),
              )
  
      metrics_to_display = [
          (
              "oversize_allocations",
              "Oversize allocations",
              _format_count,
          ),
          ("oversize_segments", "Oversize GPU segments", _format_count),
      ]
  
      for metric_key, metric_name, formatter in metrics_to_display:
          lines.append("-" * 75)
  
          prefix = metric_key + "."
  
          current = stats[prefix + "current"]
          peak = stats[prefix + "peak"]
          allocated = stats[prefix + "allocated"]
          freed = stats[prefix + "freed"]
  
          lines.append(
              " {:<21} | {} | {} | {} | {} ".format(
                  metric_name,
                  formatter(current, current),
                  formatter(peak, peak),
                  formatter(allocated, allocated),
                  formatter(freed, freed),
              ),
          )
  
      lines.append("=" * 75)
  
      fmt_dict = {"_": "", "device": device}
      for k, v in stats.items():
          fmt_dict[k.replace(".", "-")] = v
      return "|" + "|\n|".join(lines).format(**fmt_dict) + "|\n"
  
imports: import collections
  import contextlib
  import ctypes
  import pickle
  import sys
  import warnings
  from inspect import signature
  from typing import Any, Dict, Optional, Tuple, Union
  import torch
  from torch import _C
  from torch.types import Device
  from .None import _get_device_index, _get_nvml_device_index, _lazy_init, is_initialized
  from ._memory_viz import memory, segments
  from ._utils import _dummy_type
label_desc: Use image node_id 20 for calling a global function with example usage: memory_summary(device, abbreviated) and returns: unknown, sz, str, prefix, cnt, str, prefix, str