class_docstr: Collect and plot the memory stats at operator level.
  
  Includes ``memories_allocated``, ``memories_active`` and ``memories_reserved``.
  It also prints a summary for the top 20 operators that generate the most memories.
  
  Example usage:
  
      >>> # xdoctest: +SKIP(failing)
      >>> net.cuda()
      >>> input = input.cuda()
  
      >>> mem_tracker = MemoryTracker()
      >>> mem_tracker.start_monitor(net)
  
      >>> net.zero_grad(True)
      >>> loss = net(input)
      >>> if isinstance(loss, dict):
      >>>    loss = loss['out']
      >>> loss.sum().backward()
      >>> net.zero_grad(set_to_none=True)
  
      >>> mem_tracker.stop()
      >>> mem_tracker.summary()
      >>> mem_tracker.show_traces()
class_docstr_tok: ["Collect","and","plot","the","memory","stats","at","operator","level",".","Includes","``","memories_allocated","``",",","``","memories_active","``","and","``","memories_reserved","``",".","It","also","prints","a","summary","for","the","top","20","operators","that","generate","the","most","memories",".","Example","usage",":",">",">",">","#","xdoctest",":","+SKIP","(","failing",")",">",">",">","net.cuda","(",")",">",">",">","input","=","input.cuda","(",")",">",">",">","mem_tracker","=","MemoryTracker","(",")",">",">",">","mem_tracker.start_monitor","(","net",")",">",">",">","net.zero_grad","(","True",")",">",">",">","loss","=","net","(","input",")",">",">",">","if","isinstance","(","loss",",","dict",")",":",">",">",">","loss","=","loss","[","'out","'","]",">",">",">","loss.sum","(",")",".backward","(",")",">",">",">","net.zero_grad","(","set_to_none=True",")",">",">",">","mem_tracker.stop","(",")",">",">",">","mem_tracker.summary","(",")",">",">",">","mem_tracker.show_traces","(",")"]
code: def show_traces(self, path: str = "") -> None:
      import matplotlib.pyplot as plt
  
      def _plot_figure(x, y_values, labels):
          min_val = min(list(chain(*y_values))) * 0.999
          max_val = max(list(chain(*y_values))) * 1.001
          plt.figure()
          for y, label in zip(y_values, labels):
              plt.plot(x, y, label=label)
          plt.xlabel("# Operator Calls")
          plt.ylabel("Memory (MB)")
          plt.legend()
          for marker_name, marker in self._markers.items():
              if marker_name == "fw_bw_boundary":
                  plt.plot(
                      [marker, marker],
                      [min_val, max_val],
                      "r",
                      lw=2,
                      label=marker_name,
                  )
              else:
                  plt.plot(
                      [marker, marker],
                      [min_val, max_val],
                      "k-",
                      lw=2,
                      label=marker_name,
                  )
  
      if path != "":
          self.load(path)
  
      y_1 = [gb for (name, gb) in self.memories_allocated.values()]
      y_2 = [gb for (name, gb) in self.memories_active.values()]
      y_3 = [gb for (name, gb) in self.memories_reserved.values()]
      x = list(range(len(y_1)))
      # Split figures when there is big difference between
      # "reserved_memory" and "allocated_memory" or "active_memory".
      _plot_figure(
          x,
          [list(y_1), list(y_2), list(y_3)],
          ["allocated_memory", "active_memory", "reserved_memory"],
      )
      _plot_figure(x, [list(y_1)], ["allocated_memory"])
      _plot_figure(x, [list(y_2)], ["active_memory"])
      _plot_figure(x, [list(y_3)], ["reserved_memory"])
  
imports: from collections import defaultdict
  from itertools import chain
  import pickle
  from typing import Any, Callable, Dict, List, no_type_check, Sequence
  import torch
  import torch.nn
  from torch.utils.hooks import RemovableHandle
  from torch.utils._python_dispatch import TorchDispatchMode
label_desc: Use image node_id 5 for calling the MemoryTracker obj's underlying member method code with example usage: obj.show_traces(path) without return types