Skip to content

[torchci] Capture the test environment for test run reports - #200339

Closed
georgehong wants to merge 3 commits into
gh/georgehong/16/basefrom
gh/georgehong/16/head
Closed

georgehong wants to merge 3 commits into
gh/georgehong/16/basefrom
gh/georgehong/16/head

Conversation

@georgehong

@georgehong georgehong commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary
Adds torch/testing/_internal/torchci/environment.py, which captures what a test run report records about its process: machine identity (OS, CPU, Python, compiler, accelerator, devices), TestEnvironment flags, and properties. Nothing calls it yet; #200022 writes it into each report.

  • TestEnvironment.env_var_values (common_utils.py) holds each include_in_repro flag and setting as its env var is set, e.g. PYTORCH_CUDA_ALLOC_CONF: expandable_segments:True. Unset ones are "", except a flag that's on by default or implication, which is "1".
  • capture() runs in the test process before its tests, so it creates no device context (device names come from NVML, amdsmi or xpu-smi) and doesn't call torch.accelerator.

Testing
Unit tests added in this PR.

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit fee5881 with merge base 816fc3c (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:

  • Triviality
    • Adds a new 240-line module torch/testing/_internal/torchci/environment.py with new logic; no trivial class covers new functionality
    • _nvml_device loads libnvidia-ml.so.1 via ctypes and _xpu_smi_device runs xpu-smi in the test process, which deserves a reviewer's eye
  • Shared test infrastructure
    • TestEnvironment.def_flag and def_setting in common_utils.py now fill a new env_var_values dict that every test process builds
    • The repro_env_vars condition was restructured inside the new include_in_repro branch, and a reviewer should confirm the repro output stays the same
  • Socialization
    • The PR body says nothing calls capture() yet and #​200022 writes it into test run reports, a new output that HUD/torchci tooling will consume

reason: not_trivial
Reviewed commit: 9ef86ed (NOT the current head fee5881)

Inference job

🐛 Report a wrong verdict

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

return "cuda", torch.version.cuda
if torch.version.hip:
# The ROCm release (10.1), not HIP's own version (7.16).
return "rocm", ".".join((torch.version.rocm or "").split(".")[:2])

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.

Curious, why do we care to transform this? Why not just use what torch tells us? Same for xpu

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This retrieves major/minor version from the spec, with the intention of making grouping queries easier.

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.

I think it would be important to know the patch version too considering they often contain bug fixes, which can affect tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed async. Keeping default output, unless we find grouping granularity to be inconvenient.

"driver_version": driver,
"host_memory_mib": _host_memory_mib(),
"build_environment": os.environ.get("BUILD_ENVIRONMENT", ""),
"test_config": os.environ.get("TEST_CONFIG", ""),

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.

TEST_CONFIG should be part of the repro env. This is one of the most important environment variable controlling where tests run.

@georgehong georgehong Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Isn't TEST_CONFIG a bit of a PyTorch GH CI idiom that has heavy overlap with the environment flags that we have here? e.g. there's the dynamo_wrapped config, but also already a repro flag for "PYTORCH_TEST_WITH_DYNAMO"?

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.

It's not just GH, it's used by test/run_test.py. IMO it is one of the most important env that controls the reproducibility of tests, no?

@georgehong georgehong Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed async. Given that we're trying to move away from the config description in favor of primitives that describe the modalities, holding off on including it as an env. We'll still be able to refer to them in the source jsonls, and propagate it to the table as necessary.

return "", "", ""


def _nvml_device() -> tuple[str, str, str]:

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.

My agent says we can use nvidia-ml-py and torch.cuda._get_pynvml_handler to get this info. Can you look into that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since this needs to propagate through CI images as a dependency, this would be a good follow-up at pytorch/test-infra#9079

[ghstack-poisoned]
[ghstack-poisoned]
@georgehong

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 9, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200023

pytorchmergebot pushed a commit that referenced this pull request Oct 9, 2026
**Summary**
Adds a pytest plugin under `torch/testing/_internal/torchci` that writes one JSONL test run report per test process, for ingestion into the ClickHouse tests tables (schema: pytorch/test-infra `jathu/ci-tables`). Each report is a report line with the environment from #200339, then one line per test run.
- It lives under `torch/testing/_internal` because every test process must import it. `tools/` isn't on their `sys.path`, and adding the repo root would shadow the installed torch. It can move to a test-infra package once other repos use it.
- `report.py` is the format (standard library only). `plugin.py` is the writer: it derives each test's identity from its pytest item, and it turns itself off on its first error so it never changes a test's outcome. `environment.py` (#200339) is the only PyTorch-aware module.

**Testing**
`python test/torchci/test_testing.py` runs pytest with the plugin on `test/junit_xml_testdata/pytest_suite.py` and the fixture files in `test/torchci/testdata/` and checks the lines it writes, plus unit tests of the helpers.

*e2e*:
```
cd test  # in a venv where torch imports
python -m pytest junit_xml_testdata/pytest_suite.py -p torch.testing._internal.torchci.plugin \
    --torchci-report-prefix=/tmp/torchci-demo/pytest_suite -p no:cacheprovider -q
jq . /tmp/torchci-demo/pytest_suite-*.report.jsonl
```

Authored with the help of an AI assistant (Claude).
Pull Request resolved: #200022
Approved by: https://github.com/jathu
ghstack dependencies: #200339
pytorchmergebot pushed a commit that referenced this pull request Oct 9, 2026
…200023)

**Overview**
`--save-torchci-reports DIR` registers the torchci plugin, which writes each test process's report to `<DIR>/<file>-<uuid>.report.jsonl`. Off by default. *run_test.py* only forwards the flag when the installed torch has the writer, since tests sometimes run against a nightly.

**Test Plan**
```
python test/run_test.py -i test_type_info --save-torchci-reports /tmp/test/torchci-reports/
```

Authored with the help of an AI assistant (Claude).
Pull Request resolved: #200023
Approved by: https://github.com/jathu
ghstack dependencies: #200339, #200022
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
…200339)

**Summary**
Adds `torch/testing/_internal/torchci/environment.py`, which captures what a test run report records about its process: machine identity (OS, CPU, Python, compiler, accelerator, devices), `TestEnvironment` flags, and properties. Nothing calls it yet; pytorch#200022 writes it into each report.
 - `TestEnvironment.env_var_values` (`common_utils.py`) holds each `include_in_repro` flag and setting as its env var is set, e.g. `PYTORCH_CUDA_ALLOC_CONF: expandable_segments:True`. Unset ones are `""`, except a flag that's on by default or implication, which is `"1"`.
 - `capture()` runs in the test process before its tests, so it creates no device context (device names come from NVML, amdsmi or xpu-smi) and doesn't call `torch.accelerator`.

**Testing**
Unit tests added in this PR.

Authored with the help of an AI assistant (Claude).
Pull Request resolved: pytorch#200339
Approved by: https://github.com/jathu
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
**Summary**
Adds a pytest plugin under `torch/testing/_internal/torchci` that writes one JSONL test run report per test process, for ingestion into the ClickHouse tests tables (schema: pytorch/test-infra `jathu/ci-tables`). Each report is a report line with the environment from pytorch#200339, then one line per test run.
- It lives under `torch/testing/_internal` because every test process must import it. `tools/` isn't on their `sys.path`, and adding the repo root would shadow the installed torch. It can move to a test-infra package once other repos use it.
- `report.py` is the format (standard library only). `plugin.py` is the writer: it derives each test's identity from its pytest item, and it turns itself off on its first error so it never changes a test's outcome. `environment.py` (pytorch#200339) is the only PyTorch-aware module.

**Testing**
`python test/torchci/test_testing.py` runs pytest with the plugin on `test/junit_xml_testdata/pytest_suite.py` and the fixture files in `test/torchci/testdata/` and checks the lines it writes, plus unit tests of the helpers.

*e2e*:
```
cd test  # in a venv where torch imports
python -m pytest junit_xml_testdata/pytest_suite.py -p torch.testing._internal.torchci.plugin \
    --torchci-report-prefix=/tmp/torchci-demo/pytest_suite -p no:cacheprovider -q
jq . /tmp/torchci-demo/pytest_suite-*.report.jsonl
```

Authored with the help of an AI assistant (Claude).
Pull Request resolved: pytorch#200022
Approved by: https://github.com/jathu
ghstack dependencies: pytorch#200339
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
…ytorch#200023)

**Overview**
`--save-torchci-reports DIR` registers the torchci plugin, which writes each test process's report to `<DIR>/<file>-<uuid>.report.jsonl`. Off by default. *run_test.py* only forwards the flag when the installed torch has the writer, since tests sometimes run against a nightly.

**Test Plan**
```
python test/run_test.py -i test_type_info --save-torchci-reports /tmp/test/torchci-reports/
```

Authored with the help of an AI assistant (Claude).
Pull Request resolved: pytorch#200023
Approved by: https://github.com/jathu
ghstack dependencies: pytorch#200339, pytorch#200022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants