code: def _group_by(events, keyfn):
      event_groups = OrderedDict()
      for event in events:
          key = keyfn(event)
          key_events = event_groups.get(key, [])
          key_events.append(event)
          event_groups[key] = key_events
      return event_groups.items()
  
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 5 for calling a global function with example usage: _group_by(events, keyfn) and returns: event_groups