class_docstr: Low-level profiler wrap the autograd profile
  
  Args:
      activities (iterable): list of activity groups (CPU, CUDA) to use in profiling, supported values:
          ``torch.profiler.ProfilerActivity.CPU``, ``torch.profiler.ProfilerActivity.CUDA``.
          Default value: ProfilerActivity.CPU and (when available) ProfilerActivity.CUDA.
      record_shapes (bool): save information about operator's input shapes.
      profile_memory (bool): track tensor memory allocation/deallocation (see ``export_memory_timeline``
          for more details).
      with_stack (bool): record source information (file and line number) for the ops.
      with_flops (bool): use formula to estimate the FLOPS of specific operators
          (matrix multiplication and 2D convolution).
      with_modules (bool): record module hierarchy (including function names)
          corresponding to the callstack of the op. e.g. If module A's forward call's
          module B's forward which contains an aten::add op,
          then aten::add's module hierarchy is A.B
          Note that this support exist, at the moment, only for TorchScript models
          and not eager mode models.
  
      experimental_config (_ExperimentalConfig) : A set of experimental options
          used by profiler libraries like Kineto. Note, backward compatibility is not guaranteed.
  
  .. note::
      This API is experimental and subject to change in the future.
  
      Enabling shape and stack tracing results in additional overhead.
      When record_shapes=True is specified, profiler will temporarily hold references to the tensors;
      that may further prevent certain optimizations that depend on the reference count and introduce
      extra tensor copies.
class_docstr_tok: ["Low-level","profiler","wrap","the","autograd","profile","Args",":","activities","(","iterable",")",":","list","of","activity","groups","(","CPU",",","CUDA",")","to","use","in","profiling",",","supported","values",":","``","torch.profiler.ProfilerActivity.CPU","``",",","``","torch.profiler.ProfilerActivity.CUDA","``",".","Default","value",":","ProfilerActivity.CPU","and","(","when","available",")","ProfilerActivity.CUDA",".","record_shapes","(","bool",")",":","save","information","about","operator","'s","input","shapes",".","profile_memory","(","bool",")",":","track","tensor","memory","allocation\/deallocation","(","see","``","export_memory_timeline","``","for","more","details",")",".","with_stack","(","bool",")",":","record","source","information","(","file","and","line","number",")","for","the","ops",".","with_flops","(","bool",")",":","use","formula","to","estimate","the","FLOPS","of","specific","operators","(","matrix","multiplication","and","2D","convolution",")",".","with_modules","(","bool",")",":","record","module","hierarchy","(","including","function","names",")","corresponding","to","the","callstack","of","the","op",".","e.g",".","If","module","A","'s","forward","call's","module","B","'s","forward","which","contains","an","aten",":",":add","op",",","then","aten",":",":add","'s","module","hierarchy","is","A.B","Note","that","this","support","exist",",","at","the","moment",",","only","for","TorchScript","models","and","not","eager","mode","models",".","experimental_config","(","_ExperimentalConfig",")",":","A","set","of","experimental","options","used","by","profiler","libraries","like","Kineto",".","Note",",","backward","compatibility","is","not","guaranteed",".","..","note",":",":","This","API","is","experimental","and","subject","to","change","in","the","future",".","Enabling","shape","and","stack","tracing","results","in","additional","overhead",".","When","record_shapes=True","is","specified",",","profiler","will","temporarily","hold","references","to","the","tensors",";","that","may","further","prevent","certain","optimizations","that","depend","on","the","reference","count","and","introduce","extra","tensor","copies","."]
docstr: Export memory event information from the profiler collected
  tree for a given device, and export a timeline plot. There are 3
  exportable files using ``export_memory_timeline``, each controlled by the
  ``path``'s suffix.
  
  - For an HTML compatible plot, use the suffix ``.html``, and a memory timeline
    plot will be embedded as a PNG file in the HTML file.
  
  - For plot points consisting of ``[times, [sizes by category]]``, where
    ``times`` are timestamps and ``sizes`` are memory usage for each category.
    The memory timeline plot will be saved a JSON (``.json``) or gzipped JSON
    (``.json.gz``) depending on the suffix.
  
  - For raw memory points, use the suffix ``.raw.json.gz``. Each raw memory
    event will consist of ``(timestamp, action, numbytes, category)``, where
    ``action`` is one of ``[PREEXISTING, CREATE, INCREMENT_VERSION, DESTROY]``,
    and ``category`` is one of the enums from
    ``torch.profiler._memory_profiler.Category``.
  
  Output: Memory timeline written as gzipped JSON, JSON, or HTML.
code: def export_memory_timeline(
      self, path: str, device: Optional[str] = None
  ) -> None:
      """Export memory event information from the profiler collected
      tree for a given device, and export a timeline plot. There are 3
      exportable files using ``export_memory_timeline``, each controlled by the
      ``path``'s suffix.
  
      - For an HTML compatible plot, use the suffix ``.html``, and a memory timeline
        plot will be embedded as a PNG file in the HTML file.
  
      - For plot points consisting of ``[times, [sizes by category]]``, where
        ``times`` are timestamps and ``sizes`` are memory usage for each category.
        The memory timeline plot will be saved a JSON (``.json``) or gzipped JSON
        (``.json.gz``) depending on the suffix.
  
      - For raw memory points, use the suffix ``.raw.json.gz``. Each raw memory
        event will consist of ``(timestamp, action, numbytes, category)``, where
        ``action`` is one of ``[PREEXISTING, CREATE, INCREMENT_VERSION, DESTROY]``,
        and ``category`` is one of the enums from
        ``torch.profiler._memory_profiler.Category``.
  
      Output: Memory timeline written as gzipped JSON, JSON, or HTML.
      """
      # Default to device 0, if unset. Fallback on cpu.
      if (
          device is None
          and self.use_device
          and self.use_device != "cuda"
      ):
          device = self.use_device + ":0"
  
      if device is None:
          device = "cuda:0" if torch.cuda.is_available() else "cpu"
  
      # Construct the memory timeline plot data
      self.mem_tl = MemoryProfileTimeline(self._memory_profile())
  
      # Depending on the file suffix, save the data as json.gz or json.
      # For html, we can embed the image into an HTML file.
      if path.endswith(".html"):
          self.mem_tl.export_memory_timeline_html(path, device)
      elif path.endswith(".gz"):
          fp = tempfile.NamedTemporaryFile(
              "w+t", suffix=".json", delete=False
          )
          fp.close()
          if path.endswith("raw.json.gz"):
              self.mem_tl.export_memory_timeline_raw(fp.name, device)
          else:
              self.mem_tl.export_memory_timeline(fp.name, device)
          with open(fp.name) as fin:
              with gzip.open(path, "wt") as fout:
                  fout.writelines(fin)
          os.remove(fp.name)
      else:
          self.mem_tl.export_memory_timeline(path, device)
  
imports: import gzip
  import json
  import os
  import tempfile
  from enum import Enum
  from functools import partial
  from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
  from warnings import warn
  import torch
  import torch.autograd.profiler
  from torch._C import _get_privateuse1_backend_name
  from torch._C._profiler import _add_execution_trace_observer, _disable_execution_trace_observer, _enable_execution_trace_observer, _ExperimentalConfig, _remove_execution_trace_observer
  from torch.autograd import kineto_available, ProfilerActivity
  from torch.profiler._memory_profiler import MemoryProfile, MemoryProfileTimeline
label_desc: Use image node_id 15 for calling the _KinetoProfile obj's underlying member method code with example usage: obj.export_memory_timeline(path, device) without return types