{"record":{"id":"86483c15d16179a6","repo":"Lightning-AI/pytorch-lightning","slug":"found-sort-by-key-self-sort-by-key-should-be","errorCode":null,"errorMessage":"Found sort_by_key: {self._sort_by_key}. Should be within {self.AVAILABLE_SORT_KEYS}.","messagePattern":"Found sort_by_key: (.+?)\\. Should be within (.+?)\\.","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/profilers/pytorch.py","lineNumber":318,"sourceCode":"        self._sort_by_key = sort_by_key or _default_sort_by_key(profiler_kwargs)\n        self._record_module_names = record_module_names\n        self._profiler_kwargs = profiler_kwargs\n        self._table_kwargs = table_kwargs if table_kwargs is not None else {}\n\n        self.profiler: Optional[_PROFILER] = None\n        self.function_events: Optional[EventList] = None\n        self._lightning_module: Optional[LightningModule] = None  # set by ProfilerConnector\n        self._register: Optional[RegisterRecordFunction] = None\n        self._parent_profiler: Optional[AbstractContextManager] = None\n        self._recording_map: dict[str, record_function] = {}\n        self._start_action_name: Optional[str] = None\n        self._schedule: Optional[ScheduleWrapper] = None\n\n        if _KINETO_AVAILABLE:\n            self._init_kineto(profiler_kwargs)\n\n        if self._sort_by_key not in self.AVAILABLE_SORT_KEYS:\n            raise MisconfigurationException(\n                f\"Found sort_by_key: {self._sort_by_key}. Should be within {self.AVAILABLE_SORT_KEYS}. \"\n            )\n\n        for key in self._table_kwargs:\n            if key in {\"sort_by\", \"row_limit\"}:\n                raise KeyError(\n                    f\"Found invalid table_kwargs key: {key}. This is already a positional argument of the Profiler.\"\n                )\n            valid_table_keys = set(inspect.signature(EventList.table).parameters.keys()) - {\n                \"self\",\n                \"sort_by\",\n                \"row_limit\",\n            }\n            if key not in valid_table_keys:\n                raise KeyError(f\"Found invalid table_kwargs key: {key}. Should be within {valid_table_keys}.\")\n\n    def _init_kineto(self, profiler_kwargs: Any) -> None:\n        has_schedule = \"schedule\" in profiler_kwargs","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/profilers/pytorch.py#L300-L336","documentation":"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.","triggerScenarios":"PyTorchProfiler(sort_by_key=\"self_cpu_time_total\") or Trainer(profiler=PyTorchProfiler(sort_by_key=...)) with a string not in PyTorchProfiler.AVAILABLE_SORT_KEYS.","commonSituations":"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.","solutions":["Pick a key from PyTorchProfiler.AVAILABLE_SORT_KEYS (inspect it: print(PyTorchProfiler.AVAILABLE_SORT_KEYS))","Fix typos: common valid keys include 'cpu_time', 'cuda_time', 'cpu_time_total', 'cuda_time_total', 'self_cpu_time_total' etc.","Remove sort_by_key to use the default if unsure"],"exampleFix":"# before\nprofiler = PyTorchProfiler(sort_by_key=\"flops\")  # invalid\n\n# after\nprofiler = PyTorchProfiler(sort_by_key=\"cpu_time_total\")","handlingStrategy":"validation","validationCode":"from lightning.pytorch.profilers import PyTorchProfiler\nassert sort_by_key in PyTorchProfiler.AVAILABLE_SORT_KEYS, PyTorchProfiler.AVAILABLE_SORT_KEYS","typeGuard":"def is_valid_sort_key(k: str) -> bool:\n    from lightning.pytorch.profilers import PyTorchProfiler\n    return k in PyTorchProfiler.AVAILABLE_SORT_KEYS","tryCatchPattern":"try:\n    PyTorchProfiler(sort_by_key=k)\nexcept MisconfigurationException:\n    PyTorchProfiler(sort_by_key=\"cpu_time_total\")","preventionTips":["Print AVAILABLE_SORT_KEYS when configuring the profiler","Validate config dicts against the class constant before Trainer construction","Avoid inventing keys from other tools' docs"],"tags":["profiler","invalid-argument","pytorch-lightning"],"backgroundTag":"invalid-argument-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}