Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

Speedup and fix PyTorch CI build - #1428

Closed
scotts wants to merge 10 commits into
pytorch:mainfrom
scotts:speedup-pytorch-ci-build
Closed

scotts wants to merge 10 commits into
pytorch:mainfrom
scotts:speedup-pytorch-ci-build

Conversation

@scotts

@scotts scotts commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem
Kineto's PyTorch-build CI jobs timeout. Two phases dominate the run:

  1. Installing cmake takes ~62 minutes because setup.sh adds the conda-forge channel with strict priority, which forces conda's classic solver to re-solve the entire base environment onto conda-forge.
  2. The PyTorch build then recompiles all ~8k objects from scratch on every run because no compiler cache is configured.

Addressing that also exposed more problems:

  1. Infinite directory creation.
  2. New actual failing tests.

Fixes

  1. Install cmake from PyPI instead of conda. The cmake wheel ships a prebuilt binary and installs in seconds, and this removes conda from the critical path for all nine workflows that source setup.sh.
  2. Enable sccache backed by PyTorch's S3 cache for the PyTorch build. Most Kineto PRs touch only Kineto, so the bulk of PyTorch's objects become cache hits on warm runs.
  3. We also eliminate an infinite directory problem where we symlinked Kineto in the cloned PyTorch, but we cloned PyTorch in the Kineto directory.
  4. Manually deselect the failing tests. We're created Fix TestMetadataJsonFormat test failures #1430 and Fix TestProfilerDevice test failures #1429 to track.

scotts added 2 commits June 9, 2026 07:48
**Problem**
Kineto's PyTorch-build CI jobs hit the 180-minute timeout. Two phases
dominate the run. Installing cmake takes ~62 minutes because setup.sh adds
the conda-forge channel with strict priority, which forces conda's classic
solver to re-solve the entire base environment onto conda-forge. The
PyTorch build then recompiles all ~8k objects from scratch on every run
because no compiler cache is configured.

**Fix**
Install cmake from PyPI instead of conda. The cmake wheel ships a prebuilt
binary and installs in seconds, and this removes conda from the critical
path for all nine workflows that source setup.sh.

Enable sccache backed by PyTorch's S3 cache for the PyTorch build. Most
Kineto PRs touch only Kineto, so the bulk of PyTorch's objects become cache
hits on warm runs. PyTorch's CMake picks up the CMAKE_*_COMPILER_LAUNCHER
vars, so we only install the binary and point it at S3; credentials come
from the runner's AWS instance role. The binary is selected by host
architecture, and an unrecognized arch builds uncached rather than failing.
A sccache --show-stats after the build makes cache behavior visible.
@meta-cla meta-cla Bot added the cla signed label Jun 9, 2026
@scotts scotts changed the title Speedup pytorch ci build Speedup pytorch CI build Jun 9, 2026
@scotts scotts changed the title Speedup pytorch CI build Speedup PyTorch CI build Jun 9, 2026
**Problem**
The ROCm and XPU PyTorch-build jobs fail at cmake configure. The sccache
change routed every compile through sccache via CMAKE_*_COMPILER_LAUNCHER,
but the ROCm and XPU runners are not AWS instances and have no credentials
for the S3 cache bucket. sccache then errors on cmake's compiler-probe
compile, which cmake reports as a broken compiler and aborts the build.
Because the launchers were exported even when the cache was unreachable, the
failure was fatal instead of degrading to an uncached build.

**Fix**
Reachability of the S3 cache is a property of the runner, so move that
decision into the per-architecture config files: the AWS-hosted cpu and cuda
runners opt in with KINETO_USE_SCCACHE=1, and the off-AWS rocm and xpu
runners opt out with 0. pytorch_build_test.sh installs and enables sccache
only when the active config opts in, and sets the compiler launchers only if
the sccache server actually starts, so a cache problem now degrades to an
uncached build rather than breaking it.
@meta-cla meta-cla Bot added the cla signed label Jun 9, 2026
scotts added 7 commits June 9, 2026 11:00
**Problem**
The PyTorch-build jobs clone PyTorch into the Kineto checkout (KINETO_DIR/pytorch)
and then symlink PyTorch's third_party/kineto back to KINETO_DIR. Because the
symlink target contains the clone, the tree forms an endless path cycle
(third_party/kineto/pytorch/third_party/kineto/pytorch/...). setuptools follows
it while collecting license files during the editable-wheel build, emitting
thousands of ever-deepening paths and burning several minutes per run. It does
not error, so it went unnoticed until sccache made the compile fast enough for
the walk to dominate.

**Fix**
Clone PyTorch as a sibling of the Kineto checkout instead of inside it, so the
third_party/kineto symlink no longer points at a directory that contains the
clone and the cycle cannot form. The build still runs inside the cloned tree, so
relative paths are unchanged.
**Problem**
The CUDA PyTorch-build job times out at the 180-minute limit. A single test,
TestExperimentalUtils::test_profiler_conv2d_bias_followed_by_batchnorm2d_pattern,
hangs for the final ~87 minutes with no output until the job is cancelled. It
profiles with stacks enabled and runs pattern matching, exercising stack
symbolization, which appears to hang on these runners (its sibling
test_fuzz_symbolize is already deselected for the same area). Nothing bounds an
individual test, so one hang consumes the entire job budget.

**Fix**
Run the profiler suite under pytest-timeout with a per-test limit. A hang now
fails that single test in five minutes, with a stack dump at the hang point,
instead of burning the whole timeout. The thread method is required because the
hang holds the GIL in native code, which the signal method cannot interrupt.
**Problem**
The per-test timeout used pytest-timeout's thread method, which runs a watchdog
thread for every test. The profiler captures Python calls on all live threads,
so that watchdog thread appears in the trace and inflates the distinct-thread
counts that the test_source_multithreaded tests assert on, failing all seven
with an off-by-one (expected N, got N+1).

**Fix**
Switch to the signal method, which uses SIGALRM and adds no thread, so the
thread-counting tests pass again. The known tradeoff is that signal cannot
interrupt a hang that holds the GIL in native code (the CUDA symbolization
hang), so such a hang still rides the job timeout; that gap is left for a
follow-up.
**Problem**
PyTorch pull request 182434 added a device-generic TestProfilerDevice suite
via instantiate_device_type_tests. Its CUDA instances do not work on our GPU
runners: every one errors during setup, on ROCm/HIP one also asserts a
CUDA-specific event count that HIP does not produce, and its forked_process
test deadlocks once a GPU runtime is initialized in the process. Because the
job pins viable/strict, this upstream test breakage fails the CUDA and ROCm
jobs even though Kineto itself is fine.

**Fix**
Deselect the TestProfilerDeviceCUDA class and the forked_process CPU test in
the CUDA and ROCm configs. The class node ID covers the whole erroring set
because pytest matches --deselect by node-ID prefix. The CPU-only and XPU
configs are left alone: they either do not build the CUDA device tests or do
not exhibit the fork deadlock. These remain until upstream fixes the suite.
**Problem**
On the CUDA runner the profiler captures no GPU kernel events at runtime even
though CUPTI is compiled in (the build logs Found CUPTI / HAS_CUPTI). Every
test that inspects kernel activity in the trace therefore fails. Four of these
are the TestMetadataJsonFormat kernel-metadata tests, which fail with 'No
kernel activity found in trace'. The same root cause also explains several
already-deselected TestProfilerDeviceCUDA failures. The tests pass on ROCm, so
this is specific to the CUDA runner's CUPTI runtime, not a Kineto change.

**Fix**
Deselect the four TestMetadataJsonFormat tests in the CUDA config. Listed
individually rather than by class so the known-failing set is explicit and
easy to re-enable once GPU event capture works on the runner. Left out of the
other configs, where these tests pass.
@scotts scotts changed the title Speedup PyTorch CI build Speedup and fix PyTorch CI build Jun 10, 2026
@meta-codesync

meta-codesync Bot commented Jun 10, 2026

Copy link
Copy Markdown

@scotts has imported this pull request. If you are a Meta employee, you can view this in D108136613.

@scotts
scotts marked this pull request as ready for review June 10, 2026 12:58
test/profiler/test_profiler.py::TestExperimentalUtils::test_fuzz_symbolize

# https://github.com/pytorch/kineto/issues/1429
test/profiler/test_profiler.py::TestProfilerDeviceCUDA

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.

weird... seems like this should've been automatically deselected for rocm 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.

I believe these get HIPified, so the CUDA tests are also ROCm tests.

git clone --recursive --branch viable/strict https://github.com/pytorch/pytorch.git
# Clone PyTorch as a sibling of the Kineto checkout, never inside it. Below we
# replace PyTorch's third_party/kineto with a symlink back to KINETO_DIR. If the
# PyTorch tree lived inside KINETO_DIR, that symlink would point at a directory

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.

small nit: missing "If"

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.

Hmm. I'm not sure what you mean?

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.

"If" PyTorch tree lived inside KINETO_DIR, that symlink would point at a directory...

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.

It's there, on the line above. :)

@meta-codesync

meta-codesync Bot commented Jun 10, 2026

Copy link
Copy Markdown

@scotts merged this pull request in e675bbb.

scotts added a commit to scotts/pytorch that referenced this pull request Jun 16, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Jun 16, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
Pull Request resolved: #187440
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
Pull Request resolved: pytorch#187440
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Includes the following commits:

- surface HSA queue ID (pytorch/kineto#1425) 1d43601
- Add TypedMetadata to CUPTI activities (pytorch/kineto#1434) 461a26c
- Move around methods so they are accurately described by the comments (pytorch/kineto#1435) ff95265
- Introduce TypedMetadata structs (pytorch/kineto#1433) 965a14d
- Add unit tests for sync/async interleaving behavior (pytorch/kineto#1431) 67612e6
- Restrict on-demand trace output path to a safe directory (pytorch/kineto#1426) 9ea826e
- Add a cancellation state to the state machine and remove syncTraceActive_ (pytorch/kineto#1355) 3289139
- Move state machine from profiler into async handler (pytorch/kineto#1352) eb578c5
- map kernel and copy events to logical stream ID instead of HSA queue ID (pytorch/kineto#1424) 25856f7
- Speedup and fix PyTorch CI build (pytorch/kineto#1428) e675bbb
- Add logging for async rejections (pytorch/kineto#1422) 14becfb
- Make sure we clear the logger observer before starting a new session (pytorch/kineto#1387) a8cae92
- Add a scope guard for logging to help us capture all exit paths (pytorch/kineto#1383) ce13b13
- Remove roctracer support (pytorch/kineto#1419) 2a3a002
Pull Request resolved: pytorch#187440
Approved by: https://github.com/ryanzhang22, https://github.com/Skylion007
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants