class_docstr: Represents a convenience wrapper for :class:`profile`.
class_docstr_tok: ["Represents","a","convenience","wrapper","for",":","class",":","`","profile","`","."]
docstr: :param skip_first:
      The number of steps to skip at the beginning. The last skipped step
      will be treated as the warm-up step.
  :param active:
      The number of steps with active recording.
  :param log_dir:
      The TensorBoard log directory under which to store the trace files.
  :param gang:
      The associated gang.
  :param enabled:
      If ``False``, skips recording and becomes a no-op.
docstr_tok: [":","param","skip_first",":","The","number","of","steps","to","skip","at","the","beginning",".","The","last","skipped","step","will","be","treated","as","the","warm-up","step",".",":","param","active",":","The","number","of","steps","with","active","recording",".",":","param","log_dir",":","The","TensorBoard","log","directory","under","which","to","store","the","trace","files",".",":","param","gang",":","The","associated","gang",".",":","param","enabled",":","If","``","False","``",",","skips","recording","and","becomes","a","no-op","."]
code: def __init__(
      self,
      skip_first: int,
      active: int,
      log_dir: PathLike,
      gang: Gang,
      enabled: bool = False,
  ) -> None:
      """
      :param skip_first:
          The number of steps to skip at the beginning. The last skipped step
          will be treated as the warm-up step.
      :param active:
          The number of steps with active recording.
      :param log_dir:
          The TensorBoard log directory under which to store the trace files.
      :param gang:
          The associated gang.
      :param enabled:
          If ``False``, skips recording and becomes a no-op.
      """
      if skip_first <= 0:
          raise ValueError("`skip_first` must be greater than zero.")
  
      if not enabled:
          self._profile = None
  
          return
  
      activities = [ProfilerActivity.CPU, ProfilerActivity.CUDA]
  
      schedule_ = schedule(
          skip_first=skip_first - 1,
          wait=0,
          warmup=1,
          active=active,
          repeat=1,
      )
  
      trace_handler = tensorboard_trace_handler(
          str(log_dir), worker_name=f"rank_{gang.rank}"
      )
  
      self._profile = profile(
          activities=activities,
          schedule=schedule_,
          on_trace_ready=trace_handler,
          record_shapes=True,
          with_stack=True,
      )
  
imports: from __future__ import annotations
  from time import perf_counter
  from typing import Any, Optional
  import torch
  from torch.profiler import ProfilerActivity, profile, schedule, tensorboard_trace_handler
  from typing_extensions import Self
  from fairseq2.data.typing import PathLike
  from fairseq2.gang import Gang
  from fairseq2.typing import Device
label_desc: Use image node_id 1 to create a new Profiler object  with example: obj = Profiler(skip_first, active, log_dir, gang, enabled)