Lightning-AI/pytorch-lightning · error · MisconfigurationException

Found sort_by_key: {self._sort_by_key}. Should be within {se

Error message

Found sort_by_key: {self._sort_by_key}. Should be within {self.AVAILABLE_SORT_KEYS}.

What it means

PyTorchProfiler validates its sort_by_key argument against the class-level AVAILABLE_SORT_KEYS (the columns the profiler table supports, e.g. cpu_time, cuda_time, etc.). Passing an unknown key at construction time raises MisconfigurationException.

Source

Thrown at src/lightning/pytorch/profilers/pytorch.py:318

        self._sort_by_key = sort_by_key or _default_sort_by_key(profiler_kwargs)
        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[AbstractContextManager] = 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}.")

    def _init_kineto(self, profiler_kwargs: Any) -> None:
        has_schedule = "schedule" in profiler_kwargs

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pick a key from PyTorchProfiler.AVAILABLE_SORT_KEYS (inspect it: print(PyTorchProfiler.AVAILABLE_SORT_KEYS))
  2. Fix typos: common valid keys include 'cpu_time', 'cuda_time', 'cpu_time_total', 'cuda_time_total', 'self_cpu_time_total' etc.
  3. Remove sort_by_key to use the default if unsure

Example fix

# before
profiler = PyTorchProfiler(sort_by_key="flops")  # invalid

# after
profiler = PyTorchProfiler(sort_by_key="cpu_time_total")
Defensive patterns

Strategy: validation

Validate before calling

from lightning.pytorch.profilers import PyTorchProfiler
assert sort_by_key in PyTorchProfiler.AVAILABLE_SORT_KEYS, PyTorchProfiler.AVAILABLE_SORT_KEYS

Type guard

def is_valid_sort_key(k: str) -> bool:
    from lightning.pytorch.profilers import PyTorchProfiler
    return k in PyTorchProfiler.AVAILABLE_SORT_KEYS

Try / catch

try:
    PyTorchProfiler(sort_by_key=k)
except MisconfigurationException:
    PyTorchProfiler(sort_by_key="cpu_time_total")

Prevention

When it happens

Trigger: PyTorchProfiler(sort_by_key="self_cpu_time_total") or Trainer(profiler=PyTorchProfiler(sort_by_key=...)) with a string not in PyTorchProfiler.AVAILABLE_SORT_KEYS.

Common situations: Copying a sort key from torch.profiler docs or another profiling tool that Lightning does not whitelist; typos like "cputime" instead of "cpu_time"; version changes that renamed keys.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/86483c15d16179a6. Report an issue: GitHub.