Skip to content

[profiler] add opt-in Cuspy profiling backend - #5525

Merged
pianpwk merged 6 commits into
mainfrom
gh/pianpwk/115/head
Oct 20, 2026
Merged

pianpwk merged 6 commits into
mainfrom
gh/pianpwk/115/head

Conversation

@pianpwk

@pianpwk pianpwk commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Profiler.Config.cuspy switches CUDA activity collection from Kineto to Cuspy
(PM sampling, environment counters, sync events, graph dependencies, event-node
ids, and optional Perfetto protobuf output). Profiler startup raises a
ValueError if torch or libcupti lacks Cuspy support.

With CUDA graphs on, kernels replayed from graphs do not carry annotations in
the Cuspy trace yet; the next PR in the stack captures graphs with a CUPTI
annotation config to fix that.

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 19, 2026
@pianpwk
pianpwk marked this pull request as ready for review October 19, 2026 06:07
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 19, 2026
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread docs/debugging.md

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 feel we should put this file as torchtitan/observability/README.md, at least the profiling sections.

@tianyu-l

Copy link
Copy Markdown
Contributor

@claude review

@claude

claude Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 18s —— View job


Review: [profiler] add opt-in Cuspy profiling backend

  • Read the PR diff (origin/gh/pianpwk/115/base...HEAD)
  • Read torchtitan/observability/profiler.py, the tests and the docs
  • Post review

Overall the PR is clean. It's opt-in, and with cuspy=None the Kineto path behaves exactly as before: the activities and annotation export are the same, and XPU and Neuron are untouched. The capability probe avoids taking a CUPTI subscription, which is a careful touch. The tests cover the error path, the file naming and the real torch config build. My comments below are mostly small.

Worth addressing

  1. CUDA graph annotations are dropped without a warning (profiler.py:442-443). With cuspy set, the handler calls prof.export_chrome_trace(output_file) and never checks get_cuda_graph_annotations(). The PR description says the next PR in the stack fixes this. Until then, someone running Cuspy with CUDA graphs gets a trace without kernel annotations and no explanation. CLAUDE.md asks for a warning whenever a code path silently skips something, and the Kineto branch already logs one in the same situation. A one-time logger.warning would match, e.g. "Cuspy traces do not yet carry CUDA graph kernel annotations" whenever get_cuda_graph_annotations() returns something. It can be removed in the follow-up PR.
    Fix this →

  2. trace_format is never passed to torch (profiler.py:436-443). It only picks the filename suffix (.pftrace.gz vs .json.gz). Both formats then go through the same prof.export_chrome_trace(output_file) call. So pftrace output only works if torch chooses the encoder from the file extension, and gzips after encoding. If that's how it's meant to work, please add a short comment here saying so; a reader expects trace_format to reach some torch API. If torch actually needs an explicit argument, or a different export method for pftrace, then right now this would write JSON into a .pftrace.gz file. test_cuspy_activity_and_trace_file mocks torch.profiler.profile, so it can't catch either case.

Minor

  1. Redundant failure reason when torch has no Cuspy (_cuspy_unavailable_reasons). If torch.profiler.CuspyConfig is missing, the function still goes on to from torch.profiler._cuspy import cupti_python. On that same torch build the import fails too, so the error lists a second reason, No module named 'torch.profiler._cuspy'. That second line is noise and suggests a package is missing. Return right after the first reason.

  2. Missing cupti-python gets a bare error. The ModuleNotFoundError branch appends str(error) and nothing else. The docs already say pip install cupti-python; putting that hint in the error message would save users a lookup.

  3. Top-level import torch._C._profiler (profiler.py:18). It's only used in the pftrace probe. A local import inside _cuspy_unavailable_reasons, next to the other private-module import, would keep the private dependency in one place. Not blocking.

  4. No bounds check on the sampling values. CuspyProfilerConfig.__post_init__ checks that the PM fields come with performance_metrics, but accepts pm_sampling_interval_ms <= 0 and pm_lookback_window_ms < pm_sampling_interval_ms. These fields are user-facing config, and a ValueError here would be clearer than whatever CUPTI returns. Optional; skip it if torch already validates these values with a clear message.

  5. Error message for a missing GPU. raise ValueError("profiler.cuspy requires CUDA.") is correct. Something like "profiler.cuspy is set but no CUDA device is available" would tell the user which config caused it. Nit.

Docs

  • I agree with @tianyu-l's inline comment. The profiling docs, including this new Cuspy section, would sit better in torchtitan/observability/README.md, next to the code they describe. That could be a follow-up.
  • The "PyTorch nightly from 2026-09-21 or later" requirement will go stale. Once a release ships, consider pinning to the release version only.

Tests

  • The tests use self.assertRaisesRegex on a multiline string. That works, but re.escape would make it robust if a reason ever contains regex metacharacters. Nit.
  • test_cuspy_activity_config is skipped on builds without Cuspy, so CI only exercises it if a Cuspy-capable nightly is installed. Worth checking that at least one CI job really runs it, or the "torch API changes fail here" guarantee in its docstring doesn't hold.

I didn't run the tests or the linter. My tool permissions in this job blocked running Python, so I couldn't inspect the installed torch's export_chrome_trace to settle point 2.

[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 20, 2026
Profiler.Config.cuspy switches CUDA activity collection from Kineto to Cuspy
(PM sampling, environment counters, sync events, graph dependencies, event-node
ids, and optional Perfetto protobuf output). Profiler startup raises a
ValueError if torch or libcupti lacks Cuspy support.

The profiling docs (memory snapshots, and the new Cuspy section) move from
docs/debugging.md to torchtitan/observability/README.md, next to the code.

ghstack-source-id: 77117d8
Pull Request resolved: pytorch/torchtitan#5525
@pianpwk
pianpwk changed the base branch from gh/pianpwk/115/base to main October 20, 2026 02:58
@pianpwk
pianpwk merged commit 789740c into main Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants