From 552c142f419f31e13a77013bd42ae014f06cdd26 Mon Sep 17 00:00:00 2001 From: Vedaanta Agarwalla Date: Tue, 1 Sep 2026 11:01:37 -0700 Subject: [PATCH 1/5] AGENTS.md: add THD Stats packed-layout rule, editable-install gotcha, PR review section - python/cudnn/AGENTS.md: codify Rule 6 (THD/packed Stats must stay token-major or head-major, never dense-padded), citing the existing _checked_lse_view validation and stats_layout-parametrized tests. - AGENTS.md: note that pip -e installs pin one checkout via sys.meta_path, so edits in a worktree/second clone can silently be untested; add a "Reviewing a PR" section pointing reviewers (human or agent) at the numbered Hard Rules per directory. - .github/pull_request_template.md: add a checklist item to review the relevant AGENTS.md Hard Rules before submitting. --- .github/pull_request_template.md | 1 + AGENTS.md | 6 ++++++ python/cudnn/AGENTS.md | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index a426a7425d..eca6b06531 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -2,6 +2,7 @@ - [ ] I agree to license this contribution under the terms of [LICENSE.txt](https://github.com/NVIDIA/cudnn-frontend/blob/develop/LICENSE.txt). - [ ] I ran `pre-commit run` and committed any formatting changes. +- [ ] I reviewed the Hard Rules in the `AGENTS.md` for each directory this PR touches (see [root AGENTS.md § Reviewing a PR](https://github.com/NVIDIA/cudnn-frontend/blob/develop/AGENTS.md#reviewing-a-pr-human-or-agent)) and my changes comply, or I explain the exception below. - [ ] I added GitHub labels: one `cat-*`, one or more `mod-*`, and one `orig-*` (see [label list](https://github.com/NVIDIA/cudnn-frontend/labels)). - [ ] I set the **Milestone** and **Projects** fields in the sidebar (required to merge; maintainers can set these for external contributions). diff --git a/AGENTS.md b/AGENTS.md index 553598023e..18c5fc6aa9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -52,6 +52,8 @@ pip install --group jax # + jax for the CuTeDSL APIs (jax >= 0.5; XLA ent `setup.py` honors env vars: `CUDNN_PATH`, `CUDA_PATH` / `CUDAToolkit_ROOT`, `DEBUG=1` (debug build), `CMAKE_BUILD_PARALLEL_LEVEL`, `CMAKE_GENERATOR`. +**Editable installs pin one checkout — a worktree or second clone is silently not under test.** `pip install -e .` installs a `sys.meta_path` finder whose `MAPPING` hard-codes the absolute path of the checkout it was installed from; meta-path finders run before `sys.path`, so `PYTHONPATH` cannot shadow it. Before trusting any measurement from a worktree or second clone, confirm the import actually resolves there: `python -c "import cudnn; print(cudnn.__file__)"`. If a deliberately destructive probe changes nothing, suspect the import path before the code. + ## Test ```bash @@ -104,3 +106,7 @@ Reusable task recipes live in `skills/` (auto-discovered by Claude Code via `.cl When opening a pull request, apply **at minimum one label from each group**: one `cat-*` (change type), one or more `mod-*` (affected module), and one `orig-*` (originator). See the full label list at . Leave `closed-*` and `open-*` labels for maintainers. + +## Reviewing a PR (human or agent) + +Each Hard Rule is numbered so it can be cited by number in review comments. Before approving or requesting changes on a diff, check it against every Hard Rules section whose directory it touches: [python/cudnn/AGENTS.md](python/cudnn/AGENTS.md) (Rules 1-6, `execute()`/import-time), [include/cudnn_frontend/AGENTS.md](include/cudnn_frontend/AGENTS.md) (header-only, warnings-as-errors), plus the conventions in [test/AGENTS.md](test/AGENTS.md) and [samples/AGENTS.md](samples/AGENTS.md). Where a rule names a detector (grep pattern, `set_sync_debug_mode` snippet, a `RED-then-green` test), prefer running or citing it over an eyeballed read. When a review surfaces a new concrete, checkable technique or trap that these guides don't already cover, land it in the relevant `AGENTS.md` in the same PR rather than leaving it in a review comment. diff --git a/python/cudnn/AGENTS.md b/python/cudnn/AGENTS.md index babedb9ffa..d85bf79bad 100644 --- a/python/cudnn/AGENTS.md +++ b/python/cudnn/AGENTS.md @@ -193,6 +193,24 @@ the kernel that reads it). explicit handle stream, which is exactly why tests miss it. Order the work by construction rather than relying on the common case. +**Rule 6 — THD/packed Stats (LSE) must stay packed: token-major or +head-major, never dense-padded.** + +- Consumers read Stats through the same `cu_seqlen` packing as Q/O: TE and + Megatron take token-major `(T, H)` (cuDNN's TH1 recipe) natively, FA-style + callers take head-major `(H, head_stride)`. A dense-padded declaration + (per-sequence stride) mis-addresses under that packing — it must be + rejected at validation time, not silently accepted and mis-read. +- Validate by stride, not by a `thd`/`packed` flag: token-major is + `stride_h == 1 and stride_s == H`; head-major is `stride_s == 1 and + stride_h >= H`; anything else raises. See `_checked_lse_view` in + `sdpa/fwd/api_dsl.py` (duplicated at the two THD compile-key call sites — + extract rather than re-copy if you touch it, per Rule 3's "suspect + duplicated logic first"). +- Covered by `test_fwd_probe_rejects_invalid_stats_metadata` and the + `stats_layout`-parametrized THD tests (`test_dsl_sm100_thd_stats` and + siblings) in `test/python/sdpa/frost/`. + ## Frontend-only kernel package layout From 69cdb4c5d793688dd0b381a5f8e27edec220b0af Mon Sep 17 00:00:00 2001 From: Vedaanta Agarwalla Date: Tue, 1 Sep 2026 11:14:45 -0700 Subject: [PATCH 2/5] AGENTS.md: land recurring review lessons mined from PR review history MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Distilled from reviewer comments (mostly Anerudhan's) across PRs #246, #266, #280, #517, #553, #747, #797, #811, #814 — each verified against the original review thread: - python/cudnn Rule 1: overlapping optional declarations (ragged vs cu_seqlen vs seq_len) are validated as a set; ambiguous combos error out. - python/cudnn Rule 4: compile keys carry exactly the contract-relevant set — under-keying reuses a wrong artifact, over-keying recompiles. - python/cudnn Rule 5: device context is implicit state like the stream; pointer args validated for device-residency + dtype. - include/: version-gated APIs declare unconditionally, gate in the body at runtime (conditional declarations bake the build-time version in). - root: append-only public API signatures; never delete log statements in cleanups; SPDX header on new files. - test/: check module-level pytestmark before adding per-test markers. --- AGENTS.md | 3 +++ include/cudnn_frontend/AGENTS.md | 2 +- python/cudnn/AGENTS.md | 22 ++++++++++++++++++++++ test/AGENTS.md | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 18c5fc6aa9..6c2de5b9f9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -87,6 +87,9 @@ First invocation builds the hook environments and can take >5 minutes; later run - Frontend-only OSS APIs are experimental; keep the `[cutedsl]` optional-dependency boundary intact (no eager `torch`/`cutlass` imports at `cudnn` import time). - Version lives in three places that must stay in sync: `CMakeLists.txt` (`project(... VERSION ...)`), `include/cudnn_frontend_version.h`, `python/cudnn/__init__.py` (`__version__`). - Runtime debugging: set `CUDNN_FRONTEND_LOG_INFO=1` and `CUDNN_FRONTEND_LOG_FILE=stderr` for FE logs; backend logs via `CUDNN_LOGLEVEL_DBG=3 CUDNN_LOGDEST_DBG=stderr`. +- Public-API signatures evolve **append-only**: new parameters go at the end (with defaults), never inserted mid-signature — positional callers across C++, pybind, and Python wrappers break silently otherwise (review on PR #266). +- Never delete an existing log or diagnostic statement in a cleanup/refactor — several were added after repeated hard-to-repro failures and are the only tripwire for a recurrence (review on PR #280). If one looks redundant, ask before removing. +- Every new source file needs the repo's SPDX/license header (flagged in review on PR #747). ## Agent skills diff --git a/include/cudnn_frontend/AGENTS.md b/include/cudnn_frontend/AGENTS.md index 223e30a2be..e9d0dc413c 100644 --- a/include/cudnn_frontend/AGENTS.md +++ b/include/cudnn_frontend/AGENTS.md @@ -7,7 +7,7 @@ The header-only C++ library (CMake INTERFACE target `cudnn_frontend`, C++17). Um - **Header-only**: no `.cpp` files, no link-time dependencies beyond cuDNN/CUDA. New third-party code must be vendored under `thirdparty/` (currently only `nlohmann/json.hpp`, excludable via `CUDNN_FRONTEND_SKIP_JSON_LIB`). - Builds with `-Wall -Wextra -Wpedantic -Werror` (GCC/Clang) and `/W4 /WX` (MSVC) — code must be warning-clean on both. - C++17 only in `include/` (the pybind11 layer under `python/` is C++20). -- Guard anything needing a newer cuDNN with runtime `detail::get_backend_version()` checks (compare against `CUDNN_FRONTEND_VERSION`-style integers, e.g. 9.12.0 → 91200); the same headers must compile against older cuDNN 9.x. +- Guard anything needing a newer cuDNN with runtime `detail::get_backend_version()` checks (compare against `CUDNN_FRONTEND_VERSION`-style integers, e.g. 9.12.0 → 91200); the same headers must compile against older cuDNN 9.x. **Declare unconditionally, gate in the body**: an `#if`-conditional declaration bakes the build-time cuDNN version into the artifact, so a wheel built against 9.23 silently loses the symbol on a 9.24 system — declare the API always and return not-supported at runtime instead (review on PR #246). - clang-format (Google-based, indent 4, 120 cols, `SortIncludes: false`) via `pre-commit run`. ## Layering diff --git a/python/cudnn/AGENTS.md b/python/cudnn/AGENTS.md index d85bf79bad..a64f46a1a2 100644 --- a/python/cudnn/AGENTS.md +++ b/python/cudnn/AGENTS.md @@ -38,6 +38,12 @@ Numbered so reviews can cite them; the list grows — append, never renumber. launches, and a second copy of the semantics that can drift). If a packed extent would be zero, bind a never-dereferenced dummy view over storage the contract already guarantees. +- **Overlapping optional declarations are validated as a set, not one by + one.** When two mechanisms can declare the same thing (ragged offsets vs + `cu_seqlen` vs plain `seq_len` tensors), each combination is either + defined or explicitly rejected — an unhandled overlap is an untested code + path with unspecified semantics, and "both supplied" is exactly the case + no per-argument check catches (raised in review on PR #266). **Rule 2 — `execute()` launches exactly the kernels the plan promised: serve the declared layout natively, or decline — never adapt.** @@ -168,6 +174,15 @@ not become a compile key. (#493) still keys `SQ`/`SKV` under `THD_VARLEN` — migrate it to dynamic token extents like the SM100/SM120 THD compiles rather than copying its pattern. +- **Key on exactly the contract-relevant set — no more, no less.** Both + failure modes shipped on PR #553 and were caught in review: *under-keying* + (the cache keyed only `x.shape`/`w.shape` while `check_support()` + validated weight, RoPE, and scale descriptors — a hit can return an + artifact compiled for a different contract, i.e. wrong results) and + *over-keying* (`alpha` passed at launch, `m`/`n`/`k` on a shape-generic + kernel — every miss is a spurious multi-second recompile). Enumerate what + `check_support()` validates and what the kernel specializes on; the key + is that set. **Rule 5 — every torch operation on the execute path is ordered on the LAUNCH stream, never implicitly on torch's current stream.** @@ -192,6 +207,13 @@ the kernel that reads it). context is a no-op — the race only bites direct graph-API users with an explicit handle stream, which is exactly why tests miss it. Order the work by construction rather than relying on the common case. +- **The device is implicit state exactly like the stream.** A `torch.empty` + (or any allocator call) without a device context silently allocates on the + *current* GPU, not the input tensor's — wrap execute-path allocations in + the right device context as well as the stream context. And a raw pointer + argument is a contract: validate device-residency and dtype (a CUDA int64 + tensor, not a host tensor) before handing its address to a kernel — both + flagged in review on PR #517. **Rule 6 — THD/packed Stats (LSE) must stay packed: token-major or head-major, never dense-padded.** diff --git a/test/AGENTS.md b/test/AGENTS.md index ff216bfca8..af652bdaff 100644 --- a/test/AGENTS.md +++ b/test/AGENTS.md @@ -43,6 +43,7 @@ pytest fe_api/gemm/ # OSS kernel tests ### Conventions for new tests - Mark with a level (`@pytest.mark.L0` ... `L4`): L0 must stay fast (default CI smoke); big parameter sweeps go to higher levels. +- **Check for a module-level `pytestmark` before adding per-test markers.** Many files apply a level or capability marker file-wide (`pytestmark = ...` near the top); duplicating it on each test is noise, and suggesting it in review wastes a round-trip (recurred on PRs #814, #811, #797). - Gate on capability, don't assume it: skip via `check_support()` failures, `cudnn.backend_version()`, and `torch.cuda.get_device_capability()`. - Compare against a reference implementation (see existing `*_ref.py` / `*_reference.py` patterns) with dtype-appropriate tolerances. - **Scale the tolerance to the tensor, not to the dtype alone.** A fixed absolute bound quietly becomes wrong when magnitudes grow: GQA dK/dV sum over `h_q/h_kv` query heads, so at a group size of 4 the *relative* error stays ~0.5% while `|dv|` peaks near 9.6 and blows a bound that passed at `h_kv == h_q`. Compare against `TOL * max(|ref|.max(), 1.0)`, or the next GQA ratio someone adds will look like a correctness regression. From 9fd4f5bb07b15a6175806130eae3bb2e980d1741 Mon Sep 17 00:00:00 2001 From: Vedaanta Agarwalla Date: Tue, 1 Sep 2026 14:21:37 -0700 Subject: [PATCH 3/5] Address review: move THD rule to sdpa guide, automate SPDX, fix PR template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Slack + PR review feedback on #843: - Yang: THD Stats rule was out of place among the generally-applicable rules — moved to a new python/cudnn/sdpa/AGENTS.md as Rule S1 (SDPA rules get their own S-numbering so citations stay unambiguous). - Yang: automate the SPDX check — added an spdx-license-header pre-commit hook (pygrep, fails any staged C++/CUDA/Python file missing an SPDX-License-Identifier line) and added the header to the 8 tracked source files that were missing it, so the hook is clean repo-wide. Verified: hook fails a header-less probe file, passes --all-files. - Anerudhan + Vedaanta: Milestone/Projects are set by reviewers/ maintainers, not authors — dropped the checklist item; label groups are cat-* / area:*+op:* / orig-* (not mod-*) in the template and AGENTS.md. --- .github/pull_request_template.md | 3 +-- .pre-commit-config.yaml | 9 +++++++ AGENTS.md | 8 +++--- python/cudnn/AGENTS.md | 20 +++----------- .../frost/kernel/gdn2_bprop_f16.py | 17 ++++++++++++ .../frost/kernel/kda_bprop_f16.py | 17 ++++++++++++ python/cudnn/ops/fft_causal_conv1d.py | 3 +++ python/cudnn/sdpa/AGENTS.md | 26 +++++++++++++++++++ .../cpp/causal_conv1d/fft_causal_conv1d.cpp | 1 + .../test_grouped_gemm_glu_hadamard_quant.py | 3 +++ .../frost/test_template_epilogue_parity.py | 3 +++ test/python/test_fft_causal_conv1d.py | 3 +++ .../python/test_sdpa_benchmark_single_sdpa.py | 3 +++ 13 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 python/cudnn/sdpa/AGENTS.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index eca6b06531..be59dd82d5 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -3,8 +3,7 @@ - [ ] I agree to license this contribution under the terms of [LICENSE.txt](https://github.com/NVIDIA/cudnn-frontend/blob/develop/LICENSE.txt). - [ ] I ran `pre-commit run` and committed any formatting changes. - [ ] I reviewed the Hard Rules in the `AGENTS.md` for each directory this PR touches (see [root AGENTS.md § Reviewing a PR](https://github.com/NVIDIA/cudnn-frontend/blob/develop/AGENTS.md#reviewing-a-pr-human-or-agent)) and my changes comply, or I explain the exception below. -- [ ] I added GitHub labels: one `cat-*`, one or more `mod-*`, and one `orig-*` (see [label list](https://github.com/NVIDIA/cudnn-frontend/labels)). -- [ ] I set the **Milestone** and **Projects** fields in the sidebar (required to merge; maintainers can set these for external contributions). +- [ ] I added GitHub labels: one `cat-*`, one or more `area:*` / `op:*`, and one `orig-*` (see [label list](https://github.com/NVIDIA/cudnn-frontend/labels)). ## Affected area diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5a87729d36..da323cf616 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,3 +30,12 @@ repos: types_or: [python, pyi, jupyter] additional_dependencies: [".[jupyter]"] args: ["--line-length", "160"] +- repo: local + hooks: + - id: spdx-license-header + name: SPDX license header + description: "Every first-party source file carries an SPDX-License-Identifier line" + language: pygrep + entry: 'SPDX-License-Identifier:' + args: [--negate] + types_or: [c++, c, cuda, python] diff --git a/AGENTS.md b/AGENTS.md index 6c2de5b9f9..dfdb656951 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -89,7 +89,7 @@ First invocation builds the hook environments and can take >5 minutes; later run - Runtime debugging: set `CUDNN_FRONTEND_LOG_INFO=1` and `CUDNN_FRONTEND_LOG_FILE=stderr` for FE logs; backend logs via `CUDNN_LOGLEVEL_DBG=3 CUDNN_LOGDEST_DBG=stderr`. - Public-API signatures evolve **append-only**: new parameters go at the end (with defaults), never inserted mid-signature — positional callers across C++, pybind, and Python wrappers break silently otherwise (review on PR #266). - Never delete an existing log or diagnostic statement in a cleanup/refactor — several were added after repeated hard-to-repro failures and are the only tripwire for a recurrence (review on PR #280). If one looks redundant, ask before removing. -- Every new source file needs the repo's SPDX/license header (flagged in review on PR #747). +- Every new source file needs the repo's SPDX/license header (flagged in review on PR #747) — enforced by the `spdx-license-header` pre-commit hook. ## Agent skills @@ -106,10 +106,10 @@ Reusable task recipes live in `skills/` (auto-discovered by Claude Code via `.cl ## PR labels -When opening a pull request, apply **at minimum one label from each group**: one `cat-*` (change type), one or more `mod-*` (affected module), and one `orig-*` (originator). See the full label list at . +When opening a pull request, apply **at minimum one label from each group**: one `cat-*` (change type), one or more `area:*` / `op:*` (affected area), and one `orig-*` (originator). See the full label list at . -Leave `closed-*` and `open-*` labels for maintainers. +Leave `closed-*` and `open-*` labels for maintainers; Milestone/Projects sidebar fields are set by reviewers/maintainers, not authors. ## Reviewing a PR (human or agent) -Each Hard Rule is numbered so it can be cited by number in review comments. Before approving or requesting changes on a diff, check it against every Hard Rules section whose directory it touches: [python/cudnn/AGENTS.md](python/cudnn/AGENTS.md) (Rules 1-6, `execute()`/import-time), [include/cudnn_frontend/AGENTS.md](include/cudnn_frontend/AGENTS.md) (header-only, warnings-as-errors), plus the conventions in [test/AGENTS.md](test/AGENTS.md) and [samples/AGENTS.md](samples/AGENTS.md). Where a rule names a detector (grep pattern, `set_sync_debug_mode` snippet, a `RED-then-green` test), prefer running or citing it over an eyeballed read. When a review surfaces a new concrete, checkable technique or trap that these guides don't already cover, land it in the relevant `AGENTS.md` in the same PR rather than leaving it in a review comment. +Each Hard Rule is numbered so it can be cited by number in review comments. Before approving or requesting changes on a diff, check it against every Hard Rules section whose directory it touches: [python/cudnn/AGENTS.md](python/cudnn/AGENTS.md) (Rules 1-5, `execute()`/import-time), [python/cudnn/sdpa/AGENTS.md](python/cudnn/sdpa/AGENTS.md) (SDPA Rules S1+), [include/cudnn_frontend/AGENTS.md](include/cudnn_frontend/AGENTS.md) (header-only, warnings-as-errors), plus the conventions in [test/AGENTS.md](test/AGENTS.md) and [samples/AGENTS.md](samples/AGENTS.md). Where a rule names a detector (grep pattern, `set_sync_debug_mode` snippet, a `RED-then-green` test), prefer running or citing it over an eyeballed read. When a review surfaces a new concrete, checkable technique or trap that these guides don't already cover, land it in the relevant `AGENTS.md` in the same PR rather than leaving it in a review comment. diff --git a/python/cudnn/AGENTS.md b/python/cudnn/AGENTS.md index a64f46a1a2..cc4de0106d 100644 --- a/python/cudnn/AGENTS.md +++ b/python/cudnn/AGENTS.md @@ -215,23 +215,9 @@ the kernel that reads it). tensor, not a host tensor) before handing its address to a kernel — both flagged in review on PR #517. -**Rule 6 — THD/packed Stats (LSE) must stay packed: token-major or -head-major, never dense-padded.** - -- Consumers read Stats through the same `cu_seqlen` packing as Q/O: TE and - Megatron take token-major `(T, H)` (cuDNN's TH1 recipe) natively, FA-style - callers take head-major `(H, head_stride)`. A dense-padded declaration - (per-sequence stride) mis-addresses under that packing — it must be - rejected at validation time, not silently accepted and mis-read. -- Validate by stride, not by a `thd`/`packed` flag: token-major is - `stride_h == 1 and stride_s == H`; head-major is `stride_s == 1 and - stride_h >= H`; anything else raises. See `_checked_lse_view` in - `sdpa/fwd/api_dsl.py` (duplicated at the two THD compile-key call sites — - extract rather than re-copy if you touch it, per Rule 3's "suspect - duplicated logic first"). -- Covered by `test_fwd_probe_rejects_invalid_stats_metadata` and the - `stats_layout`-parametrized THD tests (`test_dsl_sm100_thd_stats` and - siblings) in `test/python/sdpa/frost/`. +SDPA-specific hard rules (cited as Rule S1, S2, ...) live in +[sdpa/AGENTS.md](sdpa/AGENTS.md) — read it before touching anything under +`python/cudnn/sdpa/`. ## Frontend-only kernel package layout diff --git a/python/cudnn/linear_attention/frost/kernel/gdn2_bprop_f16.py b/python/cudnn/linear_attention/frost/kernel/gdn2_bprop_f16.py index 464416d1d2..3ff24299f6 100644 --- a/python/cudnn/linear_attention/frost/kernel/gdn2_bprop_f16.py +++ b/python/cudnn/linear_attention/frost/kernel/gdn2_bprop_f16.py @@ -1,3 +1,20 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# This kernel is derived from cuDNN, NVIDIA Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from dataclasses import dataclass from functools import lru_cache from typing import NamedTuple, Optional, Type diff --git a/python/cudnn/linear_attention/frost/kernel/kda_bprop_f16.py b/python/cudnn/linear_attention/frost/kernel/kda_bprop_f16.py index bfc27b92de..73cd6a5072 100644 --- a/python/cudnn/linear_attention/frost/kernel/kda_bprop_f16.py +++ b/python/cudnn/linear_attention/frost/kernel/kda_bprop_f16.py @@ -1,3 +1,20 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# This kernel is derived from cuDNN, NVIDIA Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from dataclasses import dataclass from functools import lru_cache from typing import NamedTuple, Optional, Type diff --git a/python/cudnn/ops/fft_causal_conv1d.py b/python/cudnn/ops/fft_causal_conv1d.py index 61c62bf7c5..bebbbd5e46 100644 --- a/python/cudnn/ops/fft_causal_conv1d.py +++ b/python/cudnn/ops/fft_causal_conv1d.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + from typing import List, Tuple import torch diff --git a/python/cudnn/sdpa/AGENTS.md b/python/cudnn/sdpa/AGENTS.md new file mode 100644 index 0000000000..dc2c23cf83 --- /dev/null +++ b/python/cudnn/sdpa/AGENTS.md @@ -0,0 +1,26 @@ +# python/cudnn/sdpa — Agent Guide + +SDPA-specific hard rules, on top of the package-wide Rules 1-5 in +[../AGENTS.md](../AGENTS.md). Numbered `S1, S2, ...` so reviews can cite them +without colliding with the package-wide numbering; the list grows — append, +never renumber. + +## Hard rules + +**Rule S1 — THD/packed Stats (LSE) must stay packed: token-major or +head-major, never dense-padded.** + +- Consumers read Stats through the same `cu_seqlen` packing as Q/O: TE and + Megatron take token-major `(T, H)` (cuDNN's TH1 recipe) natively, FA-style + callers take head-major `(H, head_stride)`. A dense-padded declaration + (per-sequence stride) mis-addresses under that packing — it must be + rejected at validation time, not silently accepted and mis-read. +- Validate by stride, not by a `thd`/`packed` flag: token-major is + `stride_h == 1 and stride_s == H`; head-major is `stride_s == 1 and + stride_h >= H`; anything else raises. See `_checked_lse_view` in + `fwd/api_dsl.py` (duplicated at the two THD compile-key call sites — + extract rather than re-copy if you touch it, per Rule 3's "suspect + duplicated logic first"). +- Covered by `test_fwd_probe_rejects_invalid_stats_metadata` and the + `stats_layout`-parametrized THD tests (`test_dsl_sm100_thd_stats` and + siblings) in `test/python/sdpa/frost/`. diff --git a/samples/cpp/causal_conv1d/fft_causal_conv1d.cpp b/samples/cpp/causal_conv1d/fft_causal_conv1d.cpp index 2704e6b495..1f695f44d2 100644 --- a/samples/cpp/causal_conv1d/fft_causal_conv1d.cpp +++ b/samples/cpp/causal_conv1d/fft_causal_conv1d.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. + * SPDX-License-Identifier: MIT * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), diff --git a/test/python/fe_api/grouped_gemm/test_grouped_gemm_glu_hadamard_quant.py b/test/python/fe_api/grouped_gemm/test_grouped_gemm_glu_hadamard_quant.py index 5d5b20e3e3..7daaa6c1b1 100644 --- a/test/python/fe_api/grouped_gemm/test_grouped_gemm_glu_hadamard_quant.py +++ b/test/python/fe_api/grouped_gemm/test_grouped_gemm_glu_hadamard_quant.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + """Tests for grouped GEMM GLU + Hadamard + Quant forward fusion (SM100+).""" from typing import Dict, Optional diff --git a/test/python/gemm/frost/test_template_epilogue_parity.py b/test/python/gemm/frost/test_template_epilogue_parity.py index d000958ab0..4d91965c35 100644 --- a/test/python/gemm/frost/test_template_epilogue_parity.py +++ b/test/python/gemm/frost/test_template_epilogue_parity.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + """The epilogue drain is ONE logic across all 20 kernel templates. Source-level only -- no GPU, no render, no JIT. See CLAUDE.md diff --git a/test/python/test_fft_causal_conv1d.py b/test/python/test_fft_causal_conv1d.py index f718902399..ae5c5611f3 100644 --- a/test/python/test_fft_causal_conv1d.py +++ b/test/python/test_fft_causal_conv1d.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + import math import pytest diff --git a/test/python/test_sdpa_benchmark_single_sdpa.py b/test/python/test_sdpa_benchmark_single_sdpa.py index 1ff794d494..cbc821c792 100644 --- a/test/python/test_sdpa_benchmark_single_sdpa.py +++ b/test/python/test_sdpa_benchmark_single_sdpa.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + import sys from pathlib import Path from typing import Optional From 856a3f2367b69a2556b6e944b43c6adf22468216 Mon Sep 17 00:00:00 2001 From: Vedaanta Agarwalla Date: Tue, 1 Sep 2026 15:13:29 -0700 Subject: [PATCH 4/5] Address CodeRabbit review: strict SPDX pattern, correct head-major stride bound MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - .pre-commit-config.yaml: the SPDX hook now requires a *commented* SPDX-License-Identifier line with a non-empty identifier ('^\s*(?:#|//|\*|/\*)\s*SPDX-License-Identifier:\s*\S+'), so a string literal mentioning the marker no longer satisfies it. Verified: fails a 'marker = "SPDX-License-Identifier: MIT"' probe and a header-less probe, passes --all-files. - python/cudnn/sdpa/AGENTS.md Rule S1: the head-major non-overlap bound is the packed token count T (stride_h >= H was wrong — per-head slices alias when T > stride_h). Documented why plan time can only classify (stride_s == 1, stride_h >= 1): T is a runtime total, so the capacity check is execute-time. --- .pre-commit-config.yaml | 4 ++-- python/cudnn/sdpa/AGENTS.md | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index da323cf616..6dabf552eb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,8 +34,8 @@ repos: hooks: - id: spdx-license-header name: SPDX license header - description: "Every first-party source file carries an SPDX-License-Identifier line" + description: "Every first-party source file carries a commented SPDX-License-Identifier line with a non-empty identifier" language: pygrep - entry: 'SPDX-License-Identifier:' + entry: '^\s*(?:#|//|\*|/\*)\s*SPDX-License-Identifier:\s*\S+' args: [--negate] types_or: [c++, c, cuda, python] diff --git a/python/cudnn/sdpa/AGENTS.md b/python/cudnn/sdpa/AGENTS.md index dc2c23cf83..ab3b3df5cd 100644 --- a/python/cudnn/sdpa/AGENTS.md +++ b/python/cudnn/sdpa/AGENTS.md @@ -16,11 +16,17 @@ head-major, never dense-padded.** (per-sequence stride) mis-addresses under that packing — it must be rejected at validation time, not silently accepted and mis-read. - Validate by stride, not by a `thd`/`packed` flag: token-major is - `stride_h == 1 and stride_s == H`; head-major is `stride_s == 1 and - stride_h >= H`; anything else raises. See `_checked_lse_view` in - `fwd/api_dsl.py` (duplicated at the two THD compile-key call sites — - extract rather than re-copy if you touch it, per Rule 3's "suspect - duplicated logic first"). + `stride_h == 1 and stride_s == H`; head-major is `stride_s == 1` with + `stride_h` the declared head stride, which must cover the **packed token + count `T`** — `stride_h >= H` is not the bound; at `T > stride_h` the + per-head slices alias. `T` is a runtime total, so plan-time + classification can only check `stride_s == 1 and stride_h >= 1`; the + packed total fitting under the declared head stride is an execute-time + capacity check (the dense path's `_checked_lse_view` rejects via + `as_strided` when the backing storage is too small). See the THD + classification sites in `fwd/api_dsl.py` (duplicated at the two THD + compile-key call sites — extract rather than re-copy if you touch it, + per Rule 3's "suspect duplicated logic first"). - Covered by `test_fwd_probe_rejects_invalid_stats_metadata` and the `stats_layout`-parametrized THD tests (`test_dsl_sm100_thd_stats` and siblings) in `test/python/sdpa/frost/`. From b85f3856c61e944989615f98b63f36ce697e4dc6 Mon Sep 17 00:00:00 2001 From: Vedaanta Agarwalla Date: Tue, 1 Sep 2026 15:29:50 -0700 Subject: [PATCH 5/5] Rule S1: head-major stride_h >= T is caller contract in THD, not adapter-checked MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CodeRabbit correctly noted as_strided bounds-checks storage capacity, never overlap, so an in-bounds stride_h < T head-major view would alias. But the packed total is a device value in the THD path — Rule 3 bans the host read that a host-side stride_h >= T check would need, which is why _thd_lse_view's docstring declares covering the packed total as caller contract. State that precisely instead of implying an execute-time check exists, and warn against "fixing" it with a host-side length read. --- python/cudnn/sdpa/AGENTS.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/python/cudnn/sdpa/AGENTS.md b/python/cudnn/sdpa/AGENTS.md index ab3b3df5cd..eedeb04f47 100644 --- a/python/cudnn/sdpa/AGENTS.md +++ b/python/cudnn/sdpa/AGENTS.md @@ -20,13 +20,15 @@ head-major, never dense-padded.** `stride_h` the declared head stride, which must cover the **packed token count `T`** — `stride_h >= H` is not the bound; at `T > stride_h` the per-head slices alias. `T` is a runtime total, so plan-time - classification can only check `stride_s == 1 and stride_h >= 1`; the - packed total fitting under the declared head stride is an execute-time - capacity check (the dense path's `_checked_lse_view` rejects via - `as_strided` when the backing storage is too small). See the THD - classification sites in `fwd/api_dsl.py` (duplicated at the two THD - compile-key call sites — extract rather than re-copy if you touch it, - per Rule 3's "suspect duplicated logic first"). + classification can only check `stride_s == 1 and stride_h >= 1`. In the + THD path the packed total is a *device* value — Rule 3 bans reading it + back, so `stride_h >= T` is **caller contract** (stated in + `_thd_lse_view`'s docstring), not something the adapter verifies: + `as_strided` bounds-checks storage capacity, never overlap. Do not "fix" + this with a host-side length read; an in-kernel assert is the only + legal detector. See the THD classification sites in `fwd/api_dsl.py` + (duplicated at the two THD compile-key call sites — extract rather than + re-copy if you touch it, per Rule 3's "suspect duplicated logic first"). - Covered by `test_fwd_probe_rejects_invalid_stats_metadata` and the `stats_layout`-parametrized THD tests (`test_dsl_sm100_thd_stats` and siblings) in `test/python/sdpa/frost/`.