Skip to content

Move the build environment variable reference into CONTRIBUTING.md - #194745

Open
zklaus wants to merge 3 commits into
pytorch:mainfrom
zklaus:build-options-docs
Open

zklaus wants to merge 3 commits into
pytorch:mainfrom
zklaus:build-options-docs

Conversation

@zklaus

@zklaus zklaus commented Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator

Linked issue or supporting maintainer

Part of #201664

BC-breaking?

No

Summary (human written only)

This moves the central source of truth for the documentation of build option environment variables from its current location as a comment in cmake/EnvVarForwarding.cmake into CONTRIBUTING.md, giving it better visibility and the option for better typesetting using markdown.

Agent details (if applicable)

The list of build environment variables lived in a comment block of about 120 lines at the top of cmake/EnvVarForwarding.cmake; README.md (5 places), CONTRIBUTING.md, .github/copilot-instructions.md and the spin develop / spin install help texts pointed readers into that file. Read CONTRIBUTING.md first: the new Build environment variable reference section carries the forwarding rule, how to set the variables, and the variables themselves, grouped as they were in the module. The remaining files are pointer updates.

Subsection Contents
How to set them the precedence rule from #201666: environment over -C flags over pyproject.toml over the cache
What the annotations mean forwarded by prefix, passthrough, alias, read from the environment, CMake-native
Everyday knobs DEBUG, REL_WITH_DEB_INFO, MAX_JOBS, CC/CXX/CFLAGS, CMAKE_GENERATOR, ...
Feature toggles the USE_* / BUILD_* set
Architecture selection TORCH_CUDA_ARCH_LIST, TORCH_XPU_ARCH_LIST, PYTORCH_ROCM_ARCH, TORCH_CK_GEMM_ARCH_LIST
Library and backend selection BLAS, MKL_THREADING, ATEN_THREADING, ...
Library location hints CUDA_HOME, CUDNN_LIBRARY, MIOPEN_PATH, NCCL_ROOT, CAFFE2_CUSTOM_PROTOC_EXECUTABLE, ...
Handled outside the forwarding module TORCH_NATIVE_AOT, PYTORCH_BUILD_VERSION / PYTORCH_BUILD_NUMBER
CMake-only options DEBUG_CUDA, CUDA_DEVICE_DEBUG

The section also absorbs the shorter toggle list that Build only what you need carried, so the two can no longer disagree; that section keeps its prose and the BUILD_CONFIG alias and points down at the reference. EnvVarForwarding.cmake keeps the forwarding rule and the precedence paragraph, since those describe what the code below them does, plus a pointer to the reference.

The three commits are the move, the changes from @janeyx99's review (annotations defined, "Removed with setup.py" dropped), and the rebase onto #201666. The third one names -C cmake.fresh=true as the documented cache reset, replaces the description of the earlier override-versus-seed split with the single rule, drops the "seeds the cache only" annotations as the #201664 checklist asks, and adds the five variables documented in the old places since August (USE_ROCM_CK_SDPA_GFX11, USE_ROCM_CK_SDPA_GFX12, TORCH_CK_GEMM_ARCH_LIST, CAFFE2_CUSTOM_PROTOC_EXECUTABLE, TORCH_NATIVE_AOT).

Test plan: documentation and help-text change only. lintrunner -a is clean.

The rules stated in "How to set them" are the ones tools/test/test_env_var_forwarding.py (added in #201666) checks with real configures: the environment wins over a flag and re-applies over the cache, the cache keeps a value when the environment is silent, an alias yields to its canonical name, and the first listed alias wins.

python -m pytest tools/test/test_env_var_forwarding.py
python -m pytest tools/test/test_native_aot_tools.py -k documented_skip_reasons

Both pass (8 tests in the first; the second is one test that checks the wording of the TORCH_NATIVE_AOT skip conditions). -C cmake.fresh=true was exercised on a toy project with scikit-build-core 1.1.2 (a cached option returns to its default), not on a PyTorch build tree.

Every in-page link in CONTRIBUTING.md resolves to a heading; this prints no broken anchors:

python3 - <<'PY'
import re
s = open("CONTRIBUTING.md").read()
heads = {re.sub(r"[^a-z0-9 -]", "", h.lower()).strip().replace(" ", "-") for h in re.findall(r"^#+ (.*)$", s, flags=re.M)}
print([a for a in re.findall(r"\]\(#([^)]+)\)", s) if a not in heads] or "no broken anchors")
PY

No documented variable was dropped in the move. Of the 71 names in the old comment block and the old toggle list on viable/strict, this prints only CMAKE_FRESH and CMAKE_ONLY, which went with the "Removed with setup.py" list:

export LC_ALL=C
sed -n '/^### Build environment variable reference/,/^### Profile build time/p' CONTRIBUTING.md > new.txt
{ git show origin/viable/strict:cmake/EnvVarForwarding.cmake | sed -n '1,/^set(_ENV_PASSTHROUGH/p' \
    | grep -oE '^#   [A-Za-z_][A-Za-z0-9_]*' | awk '{print $2}'
  git show origin/viable/strict:CONTRIBUTING.md \
    | grep -oE '^- .[A-Z][A-Z0-9_]+=' | grep -oE '[A-Z][A-Z0-9_]+'; } | sort -u > old.txt
comm -23 old.txt <(grep -oE '[A-Za-z][A-Za-z0-9_]+' new.txt | sort -u)

🤖 Generated with Claude Code

@pytorch-bot

pytorch-bot Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit c3f1e16 with merge base 1e562ee (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added the release notes: releng release notes category label Aug 25, 2026
@atalman

atalman commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

cc @albanD @janeyx99 for review

@atalman
atalman marked this pull request as ready for review August 25, 2026 15:43
@atalman
atalman requested a review from a team as a code owner August 25, 2026 15:43
Comment thread CONTRIBUTING.md Resolved
Comment thread CONTRIBUTING.md Resolved
Comment thread CONTRIBUTING.md
[`cmake/EnvVarForwarding.cmake`](./cmake/EnvVarForwarding.cmake) for the environment variables the build forwards):
[Build environment variable reference](#build-environment-variable-reference) for the environment variables the build forwards):
```bash
ENV_KEY1=ENV_VAL1[, ENV_KEY2=ENV_VAL2]* python -m pip install --no-build-isolation -v -e .

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.

do all the env vars below support this style? if not, we should document the ways to trigger behavior with examples

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sorry for leaving this one open so long. In August the answer had a wrinkle, because some variables overrode the build cache and others only seeded it, and I wanted that settled before answering here. #201666 has now put all of them under one rule (#201664).

So: yes, with one exception. Everything in the reference is set this way except the two CMake-only options (DEBUG_CUDA, CUDA_DEVICE_DEBUG), which take -C cmake.define.<NAME>=<value> and sit in their own subsection. The "How to set them" subsection says so, with an example command, and states the rule: the environment wins over -C flags, which win over pyproject.toml, and all three win over the cache.

@liangel-02 liangel-02 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Oct 27, 2026
@zklaus
zklaus force-pushed the build-options-docs branch from 294e93a to 8cad597 Compare November 11, 2026 14:38
@zklaus
zklaus requested a review from janeyx99 November 11, 2026 15:00
@zklaus zklaus removed the Stale label Nov 11, 2026
The full list of build environment variables -- what each one does and how it
reaches CMake -- lived in a ~110 line comment block at the top of
cmake/EnvVarForwarding.cmake, with README.md, CONTRIBUTING.md and the Copilot
instructions all pointing readers into that file. Build configuration is
developer-facing documentation, so it belongs in CONTRIBUTING.md; a reader
looking for build options should not have to open a CMake module to find them.

Read CONTRIBUTING.md first: the new "Build environment variable reference"
section carries the forwarding rule and the variables, grouped as they were in
the module. The remaining files are pointer updates.

The section also absorbs the shorter toggle list that "Build only what you need"
carried, so the two no longer disagree. Five toggles that existed only in that
list -- USE_XNNPACK, BUILD_LAZY_TS_BACKEND, USE_PYTORCH_QNNPACK,
USE_CPU_VECTORIZATION and USE_COLORIZE_OUTPUT -- are documented in the reference
for the first time. That section keeps its prose and the BUILD_CONFIG alias, and
now points down at the reference instead of repeating a subset of it.

EnvVarForwarding.cmake keeps the forwarding rule itself, since that describes
what the code below it does, plus a pointer to the reference and a note to keep
the two in sync. Two headings are shortened relative to their old comment
titles ("CMake-only options", "Removed with setup.py") so their anchors stay
readable.

An alternative was a standalone build-options page linked from CONTRIBUTING.md,
which would avoid growing CONTRIBUTING.md by ~175 lines. It was not taken: the
point of the change is that build options are findable where contributors
already look, and a third location is what created the split in the first place.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

Documentation-only change; no build or runtime behaviour is affected. The
comment block removed from EnvVarForwarding.cmake sat above the first
`set(...)` call, so no forwarding logic is touched.

Lint:

```
lintrunner --paths-cmd='git diff --name-only origin/viable/strict'
```

Verified that every in-document link in CONTRIBUTING.md resolves to a heading
that exists, including the nine new table-of-contents entries:

```
python3 - <<'PY'
import re
s = open('CONTRIBUTING.md').read()
def anchor(t):
    t = re.sub(r'`', '', t.strip().lower())
    return re.sub(r'\s', '-', re.sub(r'[^\w\s-]', '', t))
have = {anchor(t) for _, t in re.findall(r'^(#{2,4}) (.+)$', s, re.M)}
print([a for a in re.findall(r'\]\(#([^)]+)\)', s) if a not in have] or 'no broken anchors')
PY
```

prints `no broken anchors`.

Verified that no variable was dropped in the move, by diffing the variable
names in the old comment block and the old toggle list against the new section:

```
sed -n '/^### Build environment variable reference/,/^### Profile build time/p' \
    CONTRIBUTING.md > new.txt
{ git show origin/viable/strict:cmake/EnvVarForwarding.cmake | head -118 \
    | grep -oE '^#   [A-Za-z_][A-Za-z0-9_]*' | awk '{print $2}'
  git show origin/viable/strict:CONTRIBUTING.md \
    | grep -oE '^- .[A-Z][A-Z0-9_]+=' | grep -oE '[A-Z][A-Z0-9_]+'; } | sort -u > old.txt
comm -23 old.txt <(grep -oE '[A-Z][A-Z0-9_]{2,}' new.txt | sort -u)
```

Of the 66 names collected, the only one reported is CC, which the new section
documents as `CC` / `CXX` / `CFLAGS` and which the three-character-minimum
pattern in the check does not match.
…-vars section

Three points from janeyx99's review of the reference section.

The forwarding mechanism was described in shorthand -- "forwarded by prefix",
"passthrough", "read from env", "CMake-native" -- without saying what those mean.
They are now defined once, framed as what to check when a variable appears not to
take effect, which is the only reason a reader needs the distinction.

Whether every variable can be set inline on the build command needed answering,
and the answer has a wrinkle worth stating. All of them can except the CMake-only
options, but what happens on the *next* build differs: prefix-forwarded names are
re-applied every configure and override the cache, while alias and passthrough
names only seed an entry that does not exist yet, so changing them later has no
effect until the cache is edited or deleted. Both are sticky if simply dropped
from the command line, since only variables actually present in the environment
are forwarded at all.

That asymmetry is called out as provisional rather than documented as design. It
is anomaly (a) in the build-config precedence draft, whose proposal is to bring
both classes under one rule, and this section will change when that lands. The
review question is itself evidence the current split is confusing.

The "Removed with setup.py" section is gone. Listing variables that no longer do
anything does not belong in a reference of current options. What was still useful
moved: USE_NINJA's replacement is now a CMAKE_GENERATOR entry under everyday
knobs, which also fills a real gap -- CMAKE_GENERATOR=Ninja appears in the
BUILD_CONFIG alias a few paragraphs earlier but was documented nowhere -- and
CMAKE_FRESH's advice is folded into the cache discussion. CMAKE_ONLY is dropped.

One paragraph outside the moved text changed too. The existing "options passed
for the first time will persist" claim predates pytorch#188242 and is now wrong for the
prefix-forwarded majority; it would have sat three paragraphs above the section
contradicting it, so it now points at the new subsection instead. README.md
carries a similar stale claim, left alone here to keep this a documentation move.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

Documentation-only change.

The override-versus-seed split was read out of cmake/EnvVarForwarding.cmake
rather than assumed: prefix-forwarded names go through `_envfwd_apply`, which
does `set(... CACHE STRING ... FORCE)` when the cached value differs, while the
`_ENV_ALIASES` and `_ENV_PASSTHROUGH` loops are guarded by `NOT DEFINED
${_var}`. The enumeration emits assignments only for names present in
`os.environ`, which is why omitting a variable leaves the cached value in both
cases.

Anchors, including the two new table-of-contents entries and the two new
cross-references, all resolve:

```
python3 - <<'PY'
import re
s = open('CONTRIBUTING.md').read()
def anchor(t):
    t = re.sub(r'`', '', t.strip().lower())
    return re.sub(r'\s', '-', re.sub(r'[^\w\s-]', '', t))
have = {anchor(t) for _, t in re.findall(r'^(#{2,4}) (.+)$', s, re.M)}
print([a for a in re.findall(r'\]\(#([^)]+)\)', s) if a not in have] or 'no broken anchors')
PY
```

Lint:

```
lintrunner --paths-cmd='git diff --name-only origin/viable/strict'
```
The reference this PR moves into CONTRIBUTING.md kept changing in its old
place while the PR waited, so the rebase has to carry those changes over.

pytorch#201666 put every forwarded variable under one precedence rule. The "How
to set them" subsection described the earlier split, where prefix-matched
names overrode the cache and alias and passthrough names only seeded it,
and called it provisional. It now states the single rule: the environment
wins over -C flags, which win over pyproject.toml, and all three win over
the cache; an alias applies only when its canonical name is not set. The
annotations no longer say "seeds the cache only", and the forwarding rule
no longer names the removed _LOW_PRIORITY_ALIASES list. The module header
keeps the short precedence paragraph pytorch#201666 added next to the forwarding
rule.

Five variables were documented in the old comment block or the old
CONTRIBUTING list since the PR's base and are added to the reference:
USE_ROCM_CK_SDPA_GFX11, USE_ROCM_CK_SDPA_GFX12, TORCH_CK_GEMM_ARCH_LIST,
CAFFE2_CUSTOM_PROTOC_EXECUTABLE and TORCH_NATIVE_AOT. The TORCH_NATIVE_AOT
entry is the existing CONTRIBUTING text, moved, with its skip conditions
laid out as a list. The wording of each condition is unchanged, since
test_native_aot_tools.py checks CONTRIBUTING.md for it.

The `spin develop` and `spin install` help texts pointed at the module for
the list of variables, like the README did; they now point at the
reference too.

The reference told readers to delete build/CMakeCache.txt to configure
from scratch, as the old comment block did for the removed CMAKE_FRESH.
It now names `-C cmake.fresh=true`, which scikit-build-core has had since
1.1, the floor here, and which pytorch#201664 names as the reset to document.

Test Plan:

Documentation and help-text change only.

```
lintrunner -a
git grep -n "_LOW_PRIORITY_ALIASES\|seeds the cache"
python -m pytest tools/test/test_native_aot_tools.py -k documented_skip_reasons
```

The grep finds nothing and the test passes. All in-page anchors in
CONTRIBUTING.md resolve, checked with a script that compares every
`](#...)` link against the headings.

`cmake.fresh` was exercised on a toy project with scikit-build-core 1.1.2
and a persistent build-dir, with one cached option MYOPT whose default is
"default". It was not run against a PyTorch build tree.

```
python -m build --wheel --no-isolation -Ccmake.define.MYOPT=first   # first
python -m build --wheel --no-isolation                              # first
python -m build --wheel --no-isolation -Ccmake.fresh=true           # default
```

Authored with the help of an AI assistant (Claude Code).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

open source release notes: releng release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants