Lightning-AI/pytorch-lightning · error · KeyError

Found invalid table_kwargs key: {key}. This is already a pos

Error message

Found invalid table_kwargs key: {key}. This is already a positional argument of the Profiler.

What it means

PyTorchProfiler passes table_kwargs through to torch.autograd.EventList.table, whose sort_by and row_limit are already dedicated positional/keyword parameters of the profiler itself. Supplying either key inside table_kwargs raises KeyError to avoid conflicting double specification.

Source

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

        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
        self._has_on_trace_ready = "on_trace_ready" in profiler_kwargs

        schedule = profiler_kwargs.get("schedule", None)
        if schedule is not None:
            if not callable(schedule):
                raise MisconfigurationException(f"Schedule should be a callable. Found: {schedule}")

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass sort_by via the profiler's sort_by_key argument and row_limit via the profiler's row_limit argument instead of table_kwargs
  2. Remove 'sort_by' and 'row_limit' entries from the table_kwargs dict
  3. Use only keys accepted by EventList.table for remaining entries

Example fix

# before
profiler = PyTorchProfiler(table_kwargs={"sort_by": "cpu_time", "row_limit": 10})

# after
profiler = PyTorchProfiler(sort_by_key="cpu_time", row_limit=10)
Defensive patterns

Strategy: validation

Validate before calling

table_kwargs.pop("sort_by", None)
table_kwargs.pop("row_limit", None)
assert not {"sort_by", "row_limit"} & table_kwargs.keys()

Try / catch

try:
    PyTorchProfiler(table_kwargs=kwargs)
except KeyError as e:
    kwargs.pop(e.args[0].split(':')[1].strip().strip('`'), None)
    PyTorchProfiler(table_kwargs=kwargs)

Prevention

When it happens

Trigger: PyTorchProfiler(table_kwargs={"sort_by": "cpu_time"}) or PyTorchProfiler(table_kwargs={"row_limit": 20}).

Common situations: Copy-pasting a torch.profiler.table(...) kwargs dict into Lightning's table_kwargs; assuming the profiler table function accepts the same arguments directly.

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/c0e2accadd40b2c6. Report an issue: GitHub.