code: def memory_stats(device: Union[Device, int] = None) -> Dict[str, Any]:
      r"""Return a dictionary of CUDA memory allocator statistics for a given device.
  
      The return value of this function is a dictionary of statistics, each of
      which is a non-negative integer.
  
      Core statistics:
  
      - ``"allocated.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        number of allocation requests received by the memory allocator.
      - ``"allocated_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        amount of allocated memory.
      - ``"segment.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        number of reserved segments from ``cudaMalloc()``.
      - ``"reserved_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        amount of reserved memory.
      - ``"active.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        number of active memory blocks.
      - ``"active_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        amount of active memory.
      - ``"inactive_split.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        number of inactive, non-releasable memory blocks.
      - ``"inactive_split_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        amount of inactive, non-releasable memory.
  
      For these core statistics, values are broken down as follows.
  
      Pool type:
  
      - ``all``: combined statistics across all memory pools.
      - ``large_pool``: statistics for the large allocation pool
        (as of October 2019, for size >= 1MB allocations).
      - ``small_pool``: statistics for the small allocation pool
        (as of October 2019, for size < 1MB allocations).
  
      Metric type:
  
      - ``current``: current value of this metric.
      - ``peak``: maximum value of this metric.
      - ``allocated``: historical total increase in this metric.
      - ``freed``: historical total decrease in this metric.
  
      In addition to the core statistics, we also provide some simple event
      counters:
  
      - ``"num_alloc_retries"``: number of failed ``cudaMalloc`` calls that
        result in a cache flush and retry.
      - ``"num_ooms"``: number of out-of-memory errors thrown.
  
      The caching allocator can be configured via ENV to not split blocks larger than a
      defined size (see Memory Management section of the Cuda Semantics documentation).
      This helps avoid memory fragmentation but may have a performance
      penalty. Additional outputs to assist with tuning and evaluating impact:
  
      - ``"max_split_size"``: blocks above this size will not be split.
      - ``"oversize_allocations.{current,peak,allocated,freed}"``:
        number of over-size allocation requests received by the memory allocator.
      - ``"oversize_segments.{current,peak,allocated,freed}"``:
        number of over-size reserved segments from ``cudaMalloc()``.
  
      The caching allocator can be configured via ENV to round memory allocations in order
      to reduce fragmentation. Sometimes the overhead from rounding can be higher than
      the fragmentation it helps reduce. The following stat can be used to check if
      rounding adds too much overhead:
  
      - ``"requested_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
        memory requested by client code, compare this with allocated_bytes to check if
        allocation rounding adds too much overhead.
  
      Args:
          device (torch.device or int, optional): selected device. Returns
              statistics for the current device, given by :func:`~torch.cuda.current_device`,
              if :attr:`device` is ``None`` (default).
  
      .. note::
          See :ref:`cuda-memory-management` for more details about GPU memory
          management.
  
      .. note::
          With :ref:`backend:cudaMallocAsync<cuda-memory-envvars>`, some stats are not
          meaningful, and are always reported as zero.
      """
      result = []
  
      def _recurse_add_to_result(prefix, obj):
          if isinstance(obj, dict):
              if len(prefix) > 0:
                  prefix += "."
              for k, v in obj.items():
                  _recurse_add_to_result(prefix + k, v)
          else:
              result.append((prefix, obj))
  
      stats = memory_stats_as_nested_dict(device=device)
      _recurse_add_to_result("", stats)
      result.sort()
  
      return collections.OrderedDict(result)
  
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 7 for calling a global function with example usage: memory_stats(device) and returns: collections