Skip to content

[CLAUDE.md] Keep long implementation notes out of C++/CUDA headers - #202251

Open
ezyang wants to merge 4 commits into
gh/ezyang/3633/basefrom
gh/ezyang/3633/head
Open

ezyang wants to merge 4 commits into
gh/ezyang/3633/basefrom
gh/ezyang/3633/head

Conversation

@ezyang

@ezyang ezyang commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Editing a header, even just a comment, rebuilds every translation unit
that includes it. For widely included ATen headers that means long
rebuilds for contributors, so a long Note explaining how a kernel works
shouldn't live in a header where every wording tweak costs a rebuild.

Add a coding style rule for agents: long explanations of how or why an
implementation works (e.g. a Note [...]) go in the .cpp/.cu file that
implements the logic, with a short // See Note [...] pointer in the
header. Documentation callers need (API docs, interface contracts) stays
in the header. The rule covers new or substantially rewritten comments;
existing ones can stay.

Add a matching item to the pr-review skill's Code Clarity checklist so
the automated reviewer checks for it.

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Oct 27, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

PR Status: in progress

A maintainer requested changes. Please address their feedback before the PR returns to maintainer review.

To become ready for review, the change must pass automated review and every maintainer comment must be addressed, either with a code change or a reply explaining why no change is needed. Any running CI must finish and every failure must be classified as unrelated to your PR (see status below in this comment).

To minimize iteration time, feel free to run the pr-review skill from the repo locally. If an automated review comment is irrelevant, react with 👎 on that specific comment to reject it, and leave a short reply explaining why. To trigger a fresh automated review, make a new push or comment @pytorchbot review. To bypass automated review, please add the “no automated review” label.

🟡 AUTOMATED REVIEW - Changes requested:

The PR adds a rule to CLAUDE.md and the pr-review checklist: long implementation
notes go in the source file that implements the logic, and the header keeps only
a short `See Note [...]` pointer. Recommendation: Request Changes. A blocking
finding from the previous review is still unaddressed: the rule has no carve-out
for header-only code and names only `.cpp`/`.cu`. Three other points are
non-blocking.

Findings (5):

[major] CLAUDE.md:235
Continuing, blocking: the rule has no answer for header-only code, and it names
only `.cpp`/`.cu`. It sends notes to "the `.cpp`/`.cu` file that implements the
logic", but when the header *is* the implementation, that file does not exist.
`torch/headeronly/` has no .cpp/.cu/.mm files, and
`torch/headeronly/cuda/Atomic.h` already holds Notes (lines 279, 315, 356).
`torch/csrc/stable/` is also headers only. In `c10/util/intrusive_ptr.h`, the
template logic and its Notes are in the header, while `intrusive_ptr.cpp` holds
little more than a static_assert. `torch/headeronly/REVIEW.md:24-25` asks for
justification in code comments, and in that directory those can only go in
headers. An agent following the rule has to put the note in an unrelated
translation unit or drop it. The reviewer will also flag correctly placed header
notes. Separately, `.mm` (MPS) is left out. Fix: name the target generically, as
"the source file (`.cpp`, `.cu`, `.mm`, ...) that implements the logic", and add
a carve-out: "If the logic lives only in headers (e.g. `torch/headeronly/`,
`torch/csrc/stable/`, templates), the note stays in the header."

[major] .agents/skills/pr-review/review-checklist.md:39
Continuing, blocking (same issue as CLAUDE.md:235): the checklist item sends
notes to "the `.cpp`/`.cu` file that implements the logic". It has no carve-out
for header-only code (`torch/headeronly/`, `torch/csrc/stable/`, template
headers like `c10/util/intrusive_ptr.h`) and leaves out `.mm`. The automated
reviewer will flag every new note correctly placed in a header-only header. Fix
in the same edit as CLAUDE.md: name the target generically (`.cpp`, `.cu`,
`.mm`, ...) and add a carve-out saying that when the logic lives only in
headers, the note stays in the header.

[minor] CLAUDE.md:236
Non-blocking: no guidance for Notes that mix caller contracts with rationale
(also review-checklist.md:39). The exemption covers "API docs, interface
contracts" but does not say how to treat a Note that contains both. Example:
`torch/headeronly/cuda/Atomic.h:356-363` (Note [HIP unsafeAtomicAdd]) mostly
explains the implementation choice, but it also says the caller must pass GPU
memory, otherwise "the result is a silent no-op". An agent that substantially
rewrites such a Note could move all of it, precondition included, out of the
header. Suggest adding "preconditions/invariants callers rely on" to the
parenthetical, which would also line up with the checklist's "Interface
documentation" item (line 12).

[minor] CLAUDE.md:232
Non-blocking: unclear precedence over the "match existing patterns" rule.
`CLAUDE.md:217` ("Match existing code style and architectural patterns") points
the other way when a new note is added to a header that keeps all its notes
inline, such as `Atomic.h` or `intrusive_ptr.h`. A short clause like "even if
the header already contains notes" would settle which rule wins.

[minor] CLAUDE.md:236
Non-blocking wording (also review-checklist.md:39): "Documentation callers need
(API docs, ...) stays in the header" first reads as the noun phrase
"Documentation callers". "Documentation that callers need" is clearer.

Reviewed commit: ccaf24c

Review run

🐛 Report a bad review

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/202251

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit ccaf24c with merge base 33ce05e (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR requires human review:

  • Never trivial: adds a rule
    • CLAUDE.md gains a new coding-style restriction on where long implementation notes may live in C++/CUDA headers
    • Documentation that adds a rule or restriction is never trivial, whatever its merit
  • Never trivial: edits reviewer steering
    • .agents/skills/pr-review/review-checklist.md adds a checklist item the automated pr-review skill enforces on every PR
    • Changing an agent skill's instructions alters later reviews, so a human should approve it

reason: not_trivial
Reviewed commit: ccaf24c

Inference job

🐛 Report a wrong verdict

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Oct 27, 2026
@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 27, 2026
github-actions[bot]

This comment was marked as outdated.

… interface contracts, use path-less See Note pointers

[ghstack-poisoned]
ezyang added a commit that referenced this pull request Oct 27, 2026
Any edit to a header, including a comment-only one, recompiles every
translation unit that includes it. For widely included ATen headers that
means long rebuilds for contributors and build cache misses in CI, so a
wording tweak to a Note shouldn't live in a header.

Add a coding style rule for agents: new or substantially rewritten
comments in .h/.cuh/.hpp files stay short (at most a one-line description
per declaration plus a `// See Note [Foo]` pointer), and long
explanations, including `Note [...]` blocks and interface contracts, go
in the source file that implements the op or uses the code, even when
that code is a template or inline function defined in the header.
Doxygen docs in the headers that docs/cpp/source/Doxyfile feeds to
Doxygen (or their aten/src/ATen/templates/ sources), and header-only
code with no natural source file, are exempt.
Existing long comments in headers can stay and take small edits.

Add a matching item to the pr-review skill's Code Clarity checklist so
the automated reviewer checks for it.

ghstack-source-id: 5c53194
Pull-Request: #202251
github-actions[bot]

This comment was marked as outdated.

…files, caller-facing docs stay in headers

[ghstack-poisoned]
@ezyang ezyang changed the title [CLAUDE.md] Keep long comments out of C++/CUDA headers [CLAUDE.md] Keep long implementation notes out of C++/CUDA headers Oct 28, 2026
ezyang added a commit that referenced this pull request Oct 28, 2026
Editing a header, even just a comment, rebuilds every translation unit
that includes it. For widely included ATen headers that means long
rebuilds for contributors, so a long Note explaining how a kernel works
shouldn't live in a header where every wording tweak costs a rebuild.

Add a coding style rule for agents: long explanations of how or why an
implementation works (e.g. a `Note [...]`) go in the .cpp/.cu file that
implements the logic, with a short `// See Note [...]` pointer in the
header. Documentation callers need (API docs, interface contracts) stays
in the header. The rule covers new or substantially rewritten comments;
existing ones can stay.

Add a matching item to the pr-review skill's Code Clarity checklist so
the automated reviewer checks for it.

ghstack-source-id: ab39cca
Pull-Request: #202251
@ezyang ezyang added in progress PR is being worked on by the author, until the automated review accepts it and removed in progress PR is being worked on by the author, until the automated review accepts it labels Oct 28, 2026
github-actions[bot]

This comment was marked as outdated.

…ll-writer checklist update

[ghstack-poisoned]
ezyang added a commit that referenced this pull request Oct 29, 2026
Rules in CLAUDE.md, skills and REVIEW.md files are applied literally by
agents. When a rule tries to settle its edge cases with lists of paths,
exemptions or hard limits, each entry is a factual claim that can be
wrong or go stale, and every gap or counterexample then reads as a
defect. On #202251 this led to a string of review rounds, each adding a
more specific exemption that the next round found a counterexample to,
until the rule was cut down to a short one.

Add an "Agent Guidance" section to CLAUDE.md asking agents that add or
rewrite instructions for agents to keep them short and state the intent,
spelling out only the edge cases that are known pitfalls. It sits in its
own section rather than in Coding Style Guidelines, which covers code
changes.

Update the skill-writer skill to match: its skeleton template asked for
an "edge cases" step and its validation checklist for "edge cases
handled". Drop the template step (the template's best practices already
list common pitfalls) and make both of its validation checklists ask for
stated intent, with edge cases only for known pitfalls.

This only changes guidance for authors. The pr-review skill's own rules
are unchanged: changing what the automated reviewer flags needs a
maintainer's explicit approval and belongs in a separate PR.

Test Plan: documentation-only change. Checked the other agent guidance
that mentions edge cases for conflicts with the new rule:

```
git grep -n -i "edge case" -- CLAUDE.md '*REVIEW.md' .agents
```

This PR was authored with the help of an AI assistant (Claude).

ghstack-source-id: ffacba2
Pull-Request: #202334
github-actions[bot]

This comment was marked as outdated.

pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
…ted (#202455)

✴️ iz2: Two prompt fixes to Stage B of the hardened PR review. In both cases a real blocking review was rejected by `extract_verdict.py` and nothing was posted. Part of pytorch/test-infra#9260.

1. **Needs Discussion with a blocker.** Stage A can recommend Needs Discussion while naming a defect that must be fixed. Stage B mapped Needs Discussion to `ready_for_human_review` and the defect to `major`, and the sanitizer refuses that combination. This happened on #202251 and #201766. Now such a defect is `major` and the verdict is `changes_requested`; open questions, requests for data or discussion, and unreviewed files are not such defects. The summary keeps the Recommendation and says when a defect overrode it.
2. **Findings on files with no anchorable line.** `/tmp/pr-files.txt` lists deletions, pure renames, mode-only changes and binary files, but their diffs show no added or context line, so a finding on one is discarded and a change request left with none is rejected (#201671). The prompt now names these and points Stage B at a changed line instead.

This does not fix PRs whose every file is such a file, since they have no anchorable line at all. A separate change to the sanitizer covers that case.

Test plan: `python3 -m unittest test_workflow_contract` in `scripts/pr_review` (174 pass). Two new tests pin both rules and fail on the old prompt.
Pull Request resolved: #202455
Approved by: https://github.com/huydhn
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…ted (pytorch#202455)

✴️ iz2: Two prompt fixes to Stage B of the hardened PR review. In both cases a real blocking review was rejected by `extract_verdict.py` and nothing was posted. Part of pytorch/test-infra#9260.

1. **Needs Discussion with a blocker.** Stage A can recommend Needs Discussion while naming a defect that must be fixed. Stage B mapped Needs Discussion to `ready_for_human_review` and the defect to `major`, and the sanitizer refuses that combination. This happened on pytorch#202251 and pytorch#201766. Now such a defect is `major` and the verdict is `changes_requested`; open questions, requests for data or discussion, and unreviewed files are not such defects. The summary keeps the Recommendation and says when a defect overrode it.
2. **Findings on files with no anchorable line.** `/tmp/pr-files.txt` lists deletions, pure renames, mode-only changes and binary files, but their diffs show no added or context line, so a finding on one is discarded and a change request left with none is rejected (pytorch#201671). The prompt now names these and points Stage B at a changed line instead.

This does not fix PRs whose every file is such a file, since they have no anchorable line at all. A separate change to the sanitizer covers that case.

Test plan: `python3 -m unittest test_workflow_contract` in `scripts/pr_review` (174 pass). Two new tests pin both rules and fail on the old prompt.
Pull Request resolved: pytorch#202455
Approved by: https://github.com/huydhn
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…ted (pytorch#202455)

✴️ iz2: Two prompt fixes to Stage B of the hardened PR review. In both cases a real blocking review was rejected by `extract_verdict.py` and nothing was posted. Part of pytorch/test-infra#9260.

1. **Needs Discussion with a blocker.** Stage A can recommend Needs Discussion while naming a defect that must be fixed. Stage B mapped Needs Discussion to `ready_for_human_review` and the defect to `major`, and the sanitizer refuses that combination. This happened on pytorch#202251 and pytorch#201766. Now such a defect is `major` and the verdict is `changes_requested`; open questions, requests for data or discussion, and unreviewed files are not such defects. The summary keeps the Recommendation and says when a defect overrode it.
2. **Findings on files with no anchorable line.** `/tmp/pr-files.txt` lists deletions, pure renames, mode-only changes and binary files, but their diffs show no added or context line, so a finding on one is discarded and a change request left with none is rejected (pytorch#201671). The prompt now names these and points Stage B at a changed line instead.

This does not fix PRs whose every file is such a file, since they have no anchorable line at all. A separate change to the sanitizer covers that case.

Test plan: `python3 -m unittest test_workflow_contract` in `scripts/pr_review` (174 pass). Two new tests pin both rules and fail on the old prompt.
Pull Request resolved: pytorch#202455
Approved by: https://github.com/huydhn
@ezyang ezyang added in progress PR is being worked on by the author, until the automated review accepts it and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 10, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of ccaf24c: changes requested before human review.

The PR adds a rule to CLAUDE.md and the pr-review checklist: long implementation notes go in the source file that implements the logic, and the header keeps only a short See Note \[...\] pointer. Recommendation: Request Changes. A blocking finding from the previous review is still unaddressed: the rule has no carve-out for header-only code and names only .cpp/.cu. Three other points are non-blocking.

🔴 2 blocking findings are attached to the code below.

⚪ 3 non-blocking findings

`// See Note [...]` pointer in the header. Documentation callers need (API

Non-blocking: no guidance for Notes that mix caller contracts with rationale (also review-checklist.md:39). The exemption covers "API docs, interface contracts" but does not say how to treat a Note that contains both. Example: torch/headeronly/cuda/Atomic.h:356-363 (Note [HIP unsafeAtomicAdd]) mostly explains the implementation choice, but it also says the caller must pass GPU memory, otherwise "the result is a silent no-op". An agent that substantially rewrites such a Note could move all of it, precondition included, out of the header. Suggest adding "preconditions/invariants callers rely on" to the parenthetical, which would also line up with the checklist's "Interface documentation" item (line 12).


- Don't put long explanations of how or why an implementation works (e.g. a

Non-blocking: unclear precedence over the "match existing patterns" rule. CLAUDE.md:217 ("Match existing code style and architectural patterns") points the other way when a new note is added to a header that keeps all its notes inline, such as Atomic.h or intrusive_ptr.h. A short clause like "even if the header already contains notes" would settle which rule wins.


`// See Note [...]` pointer in the header. Documentation callers need (API

Non-blocking wording (also review-checklist.md:39): "Documentation callers need (API docs, ...) stays in the header" first reads as the noun phrase "Documentation callers". "Documentation that callers need" is clearer.

Please address the findings and push; a new commit normally gets a fresh automated review.

Disagree with a finding? Reply with your reasoning, then comment @pytorchbot review to request a re-review. This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

Comment thread CLAUDE.md
- Don't put long explanations of how or why an implementation works (e.g. a
`Note [...]`) in C++/CUDA headers (`.h`, `.cuh`, `.hpp`): editing a header,
even just a comment, rebuilds every file that includes it. Put them in the
`.cpp`/`.cu` file that implements the logic and leave a short

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Continuing, blocking: the rule has no answer for header-only code, and it names only .cpp/.cu. It sends notes to "the .cpp/.cu file that implements the logic", but when the header is the implementation, that file does not exist. torch/headeronly/ has no .cpp/.cu/.mm files, and torch/headeronly/cuda/Atomic.h already holds Notes (lines 279, 315, 356). torch/csrc/stable/ is also headers only. In c10/util/intrusive_ptr.h, the template logic and its Notes are in the header, while intrusive_ptr.cpp holds little more than a static_assert. torch/headeronly/REVIEW.md:24-25 asks for justification in code comments, and in that directory those can only go in headers. An agent following the rule has to put the note in an unrelated translation unit or drop it. The reviewer will also flag correctly placed header notes. Separately, .mm (MPS) is left out. Fix: name the target generically, as "the source file (.cpp, .cu, .mm, ...) that implements the logic", and add a carve-out: "If the logic lives only in headers (e.g. torch/headeronly/, torch/csrc/stable/, templates), the note stays in the header."

Report this finding


- [ ] **Self-explanatory code** - Variable and function names convey intent; minimal comments needed
- [ ] **Useful comments only** - Comments explain non-obvious context that cannot be inferred locally. For large comment use the `# Note [Good note title]` and `See Note [Good note title]` to write larger comments that can be referenced from multiple places in the codebase.
- [ ] **Implementation notes out of headers** - New or substantially rewritten long explanations of how or why an implementation works (e.g. a `Note [...]`) go in the `.cpp`/`.cu` file that implements the logic, with a short `See Note [...]` pointer in the header, since any header edit rebuilds every includer. Documentation callers need (API docs, interface contracts) stays in the header; existing comments need not move

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Continuing, blocking (same issue as CLAUDE.md:235): the checklist item sends notes to "the .cpp/.cu file that implements the logic". It has no carve-out for header-only code (torch/headeronly/, torch/csrc/stable/, template headers like c10/util/intrusive_ptr.h) and leaves out .mm. The automated reviewer will flag every new note correctly placed in a header-only header. Fix in the same edit as CLAUDE.md: name the target generically (.cpp, .cu, .mm, ...) and add a carve-out saying that when the logic lives only in headers, the note stays in the header.

Report this finding

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GL/human GreenLight marked as needing human review in progress PR is being worked on by the author, until the automated review accepts it release notes: devx topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants