code: def get_metrics_from_torch_profiler(
      profile: torch.profiler.profiler.profile,
  ) -> Dict[str, List[TorchProfilerMetrics]]:
      """Export time and resource usage metrics (CPU and CUDA) from a PyTorch profiler.
  
      The profiler keeps track of *torch operations* being executed in C++. It keeps track
      of what device they're executed on, their execution time, and memory usage.
      We only track the aforementioned metrics, but the torch profiler can keep track of
      the stack trace, FLOPs, and torch modules. Tracking each additional item adds overhead.
  
      The torch profiler surfaces these metrics that are tracked under the hood by `libkineto`.
      More on the Kineto project: https://github.com/pytorch/kineto
  
      :param profile: profiler object that contains all the events that
          were registered during the execution of the wrapped code block.
      """
      # events in both of these lists are in chronological order.
      kineto_events = profile.profiler.kineto_results.events()
      function_events = profile.profiler.function_events
      (
          main_kineto_events,
          main_function_events,
          memory_events,
          out_of_memory_events,
      ) = get_all_events(kineto_events, function_events)
  
      assert Counter(
          [event.name for event in main_function_events]
      ) == Counter([event.name() for event in main_kineto_events])
      info = initialize_stats_dict(main_function_events)
      info = get_resource_usage_report(
          main_kineto_events,
          main_function_events,
          memory_events,
          out_of_memory_events,
          info,
      )
      return info
  
file_path: ludwig/ludwig/benchmarking/reporting.py
imports: from collections import Counter, defaultdict
  from statistics import mean
  from typing import Any, Dict, List, Tuple, Union
  import torch
  from torch._C._autograd import _KinetoEvent
  from torch.autograd import DeviceType, profiler_util
  from ludwig.benchmarking.profiler_dataclasses import DeviceUsageMetrics, SystemResourceMetrics, TorchProfilerMetrics
  from ludwig.constants import LUDWIG_TAG
label_desc: Use image node_id 9 for calling a global function with example usage: get_metrics_from_torch_profiler(profile) and returns: info