docstr: This profiler uses PyTorch's Autograd Profiler and lets you inspect the cost of.
  
  different operators inside your model - both on the CPU and GPU
  
  Args:
      dirpath: Directory path for the ``filename``. If ``dirpath`` is ``None`` but ``filename`` is present, the
          ``trainer.log_dir`` (from :class:`~lightning.pytorch.loggers.tensorboard.TensorBoardLogger`)
          will be used.
  
      filename: If present, filename where the profiler results will be saved instead of printing to stdout.
          The ``.txt`` extension will be used automatically.
  
      group_by_input_shapes: Include operator input shapes and group calls by shape.
  
      emit_nvtx: Context manager that makes every autograd operation emit an NVTX range
          Run::
  
              nvprof --profile-from-start off -o trace_name.prof -- <regular command here>
  
          To visualize, you can either use::
  
              nvvp trace_name.prof
              torch.autograd.profiler.load_nvprof(path)
  
      export_to_chrome: Whether to export the sequence of profiled operators for Chrome.
          It will generate a ``.json`` file which can be read by Chrome.
  
      row_limit: Limit the number of rows in a table, ``-1`` is a special value that
          removes the limit completely.
  
      sort_by_key: Attribute used to sort entries. By default
          they are printed in the same order as they were registered.
          Valid keys include: ``cpu_time``, ``cuda_time``, ``cpu_time_total``,
          ``cuda_time_total``, ``cpu_memory_usage``, ``cuda_memory_usage``,
          ``self_cpu_memory_usage``, ``self_cuda_memory_usage``, ``count``.
  
      record_module_names: Whether to add module names while recording autograd operation.
  
      table_kwargs: Dictionary with keyword arguments for the summary table.
  
      \**profiler_kwargs: Keyword arguments for the PyTorch profiler. This depends on your PyTorch version
  
  Raises:
      MisconfigurationException:
          If arg ``sort_by_key`` is not present in ``AVAILABLE_SORT_KEYS``.
          If arg ``schedule`` is not a ``Callable``.
          If arg ``schedule`` does not return a ``torch.profiler.ProfilerAction``.
docstr_tok: ["This","profiler","uses","PyTorch","'s","Autograd","Profiler","and","lets","you","inspect","the","cost","of",".","different","operators","inside","your","model","-","both","on","the","CPU","and","GPU","Args",":","dirpath",":","Directory","path","for","the","``","filename","``",".","If","``","dirpath","``","is","``","None","``","but","``","filename","``","is","present",",","the","``","trainer.log_dir","``","(","from",":","class",":","`","~lightning.pytorch.loggers.tensorboard.TensorBoardLogger","`",")","will","be","used",".","filename",":","If","present",",","filename","where","the","profiler","results","will","be","saved","instead","of","printing","to","stdout",".","The","``",".txt","``","extension","will","be","used","automatically",".","group_by_input_shapes",":","Include","operator","input","shapes","and","group","calls","by","shape",".","emit_nvtx",":","Context","manager","that","makes","every","autograd","operation","emit","an","NVTX","range","Run",":",":","nvprof","--","profile-from-start","off","-o","trace_name.prof","--","<","regular","command","here",">","To","visualize",",","you","can","either","use",":",":","nvvp","trace_name.prof","torch.autograd.profiler.load_nvprof","(","path",")","export_to_chrome",":","Whether","to","export","the","sequence","of","profiled","operators","for","Chrome",".","It","will","generate","a","``",".json","``","file","which","can","be","read","by","Chrome",".","row_limit",":","Limit","the","number","of","rows","in","a","table",",","``","-1","``","is","a","special","value","that","removes","the","limit","completely",".","sort_by_key",":","Attribute","used","to","sort","entries",".","By","default","they","are","printed","in","the","same","order","as","they","were","registered",".","Valid","keys","include",":","``","cpu_time","``",",","``","cuda_time","``",",","``","cpu_time_total","``",",","``","cuda_time_total","``",",","``","cpu_memory_usage","``",",","``","cuda_memory_usage","``",",","``","self_cpu_memory_usage","``",",","``","self_cuda_memory_usage","``",",","``","count","``",".","record_module_names",":","Whether","to","add","module","names","while","recording","autograd","operation",".","table_kwargs",":","Dictionary","with","keyword","arguments","for","the","summary","table",".","\\","*","*","profiler_kwargs",":","Keyword","arguments","for","the","PyTorch","profiler",".","This","depends","on","your","PyTorch","version","Raises",":","MisconfigurationException",":","If","arg","``","sort_by_key","``","is","not","present","in","``","AVAILABLE_SORT_KEYS","``",".","If","arg","``","schedule","``","is","not","a","``","Callable","``",".","If","arg","``","schedule","``","does","not","return","a","``","torch.profiler.ProfilerAction","``","."]
code: def __init__(
      self,
      dirpath: Optional[Union[str, Path]] = None,
      filename: Optional[str] = None,
      group_by_input_shapes: bool = False,
      emit_nvtx: bool = False,
      export_to_chrome: bool = True,
      row_limit: int = 20,
      sort_by_key: Optional[str] = None,
      record_module_names: bool = True,
      table_kwargs: Optional[Dict[str, Any]] = None,
      **profiler_kwargs: Any,
  ) -> None:
      r"""This profiler uses PyTorch's Autograd Profiler and lets you inspect the cost of.
  
      different operators inside your model - both on the CPU and GPU
  
      Args:
          dirpath: Directory path for the ``filename``. If ``dirpath`` is ``None`` but ``filename`` is present, the
              ``trainer.log_dir`` (from :class:`~lightning.pytorch.loggers.tensorboard.TensorBoardLogger`)
              will be used.
  
          filename: If present, filename where the profiler results will be saved instead of printing to stdout.
              The ``.txt`` extension will be used automatically.
  
          group_by_input_shapes: Include operator input shapes and group calls by shape.
  
          emit_nvtx: Context manager that makes every autograd operation emit an NVTX range
              Run::
  
                  nvprof --profile-from-start off -o trace_name.prof -- <regular command here>
  
              To visualize, you can either use::
  
                  nvvp trace_name.prof
                  torch.autograd.profiler.load_nvprof(path)
  
          export_to_chrome: Whether to export the sequence of profiled operators for Chrome.
              It will generate a ``.json`` file which can be read by Chrome.
  
          row_limit: Limit the number of rows in a table, ``-1`` is a special value that
              removes the limit completely.
  
          sort_by_key: Attribute used to sort entries. By default
              they are printed in the same order as they were registered.
              Valid keys include: ``cpu_time``, ``cuda_time``, ``cpu_time_total``,
              ``cuda_time_total``, ``cpu_memory_usage``, ``cuda_memory_usage``,
              ``self_cpu_memory_usage``, ``self_cuda_memory_usage``, ``count``.
  
          record_module_names: Whether to add module names while recording autograd operation.
  
          table_kwargs: Dictionary with keyword arguments for the summary table.
  
          \**profiler_kwargs: Keyword arguments for the PyTorch profiler. This depends on your PyTorch version
  
      Raises:
          MisconfigurationException:
              If arg ``sort_by_key`` is not present in ``AVAILABLE_SORT_KEYS``.
              If arg ``schedule`` is not a ``Callable``.
              If arg ``schedule`` does not return a ``torch.profiler.ProfilerAction``.
  
      """
      super().__init__(dirpath=dirpath, filename=filename)
  
      self._group_by_input_shapes = (
          group_by_input_shapes
          and profiler_kwargs.get("record_shapes", False)
      )
      self._emit_nvtx = emit_nvtx
      self._export_to_chrome = export_to_chrome
      self._row_limit = row_limit
      self._sort_by_key = (
          sort_by_key
          or f"{'cuda' if profiler_kwargs.get('use_cuda', False) else 'cpu'}_time_total"
      )
      self._record_module_names = record_module_names
      self._profiler_kwargs = profiler_kwargs
      self._table_kwargs = (
          table_kwargs if table_kwargs is not None else {}
      )
  
      self.profiler: Optional[_PROFILER] = None
      self.function_events: Optional["EventList"] = None
      self._lightning_module: Optional[
          "LightningModule"
      ] = None  # set by ProfilerConnector
      self._register: Optional[RegisterRecordFunction] = None
      self._parent_profiler: Optional[ContextManager] = None
      self._recording_map: Dict[str, record_function] = {}
      self._start_action_name: Optional[str] = None
      self._schedule: Optional[ScheduleWrapper] = None
  
      if _KINETO_AVAILABLE:
          self._init_kineto(profiler_kwargs)
  
      if self._sort_by_key not in self.AVAILABLE_SORT_KEYS:
          raise MisconfigurationException(
              f"Found sort_by_key: {self._sort_by_key}. Should be within {self.AVAILABLE_SORT_KEYS}. "
          )
  
      for key in self._table_kwargs:
          if key in {"sort_by", "row_limit"}:
              raise KeyError(
                  f"Found invalid table_kwargs key: {key}. This is already a positional argument of the Profiler."
              )
          valid_table_keys = set(
              inspect.signature(EventList.table).parameters.keys()
          ) - {
              "self",
              "sort_by",
              "row_limit",
          }
          if key not in valid_table_keys:
              raise KeyError(
                  f"Found invalid table_kwargs key: {key}. Should be within {valid_table_keys}."
              )
  
imports: import inspect
  import logging
  import os
  from functools import lru_cache, partial
  from pathlib import Path
  from typing import TYPE_CHECKING, Any, Callable, ContextManager, Dict, List, Optional, Type, Union
  import torch
  from torch import Tensor, nn
  from torch.autograd.profiler import EventList, record_function
  from torch.profiler import ProfilerAction, ProfilerActivity, tensorboard_trace_handler
  from torch.utils.hooks import RemovableHandle
  from typing_extensions import override
  from lightning.fabric.accelerators.cuda import is_cuda_available
  from lightning.pytorch.profilers.profiler import Profiler
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.rank_zero import WarningCache, rank_zero_warn
label_desc: Use image node_id 1 to create a new PyTorchProfiler object from inherited base classes: Profiler with example: obj = PyTorchProfiler(dirpath, filename, group_by_input_shapes, emit_nvtx, export_to_chrome, row_limit, sort_by_key, record_module_names, table_kwargs)