code: def _build_measure_tuple(
      events: List, occurrences: List
  ) -> NamedTuple:
      # memory profiling supported in torch >= 1.6
      self_cpu_memory = None
      has_self_cpu_memory = any(
          hasattr(e, "self_cpu_memory_usage") for e in events
      )
      if has_self_cpu_memory:
          self_cpu_memory = sum(
              [getattr(e, "self_cpu_memory_usage", 0) for e in events]
          )
      cpu_memory = None
      has_cpu_memory = any(
          hasattr(e, "cpu_memory_usage") for e in events
      )
      if has_cpu_memory:
          cpu_memory = sum(
              [getattr(e, "cpu_memory_usage", 0) for e in events]
          )
      self_cuda_memory = None
      has_self_cuda_memory = any(
          hasattr(e, "self_cuda_memory_usage") for e in events
      )
      if has_self_cuda_memory:
          self_cuda_memory = sum(
              [getattr(e, "self_cuda_memory_usage", 0) for e in events]
          )
      cuda_memory = None
      has_cuda_memory = any(
          hasattr(e, "cuda_memory_usage") for e in events
      )
      if has_cuda_memory:
          cuda_memory = sum(
              [getattr(e, "cuda_memory_usage", 0) for e in events]
          )
  
      # self CUDA time supported in torch >= 1.7
      self_cuda_total = None
      has_self_cuda_time = any(
          hasattr(e, "self_cuda_time_total") for e in events
      )
      if has_self_cuda_time:
          self_cuda_total = sum(
              [getattr(e, "self_cuda_time_total", 0) for e in events]
          )
  
      return Measure(
          self_cpu_total=sum([e.self_cpu_time_total for e in events]),
          cpu_total=sum([e.cpu_time_total for e in events]),
          self_cuda_total=self_cuda_total,
          cuda_total=sum([e.cuda_time_total for e in events]),
          self_cpu_memory=self_cpu_memory,
          cpu_memory=cpu_memory,
          self_cuda_memory=self_cuda_memory,
          cuda_memory=cuda_memory,
          occurrences=occurrences,
      )
  
imports: import functools
  from collections import OrderedDict, defaultdict, namedtuple
  from typing import Any, List, NamedTuple, Optional, Tuple
  import torch
  import torch.profiler
label_desc: Use image node_id 3 for calling a global function with example usage: _build_measure_tuple(events, occurrences) and returns: Measure