Skip to content

[graph_trainer] Import the paged stash Triton kernels at launch - #5611

Merged
SherlockNoMad merged 2 commits into
pytorch:mainfrom
mkhilari:graph-trainer-triton-lazy
Nov 6, 2026
Merged

SherlockNoMad merged 2 commits into
pytorch:mainfrom
mkhilari:graph-trainer-triton-lazy

Conversation

@mkhilari

@mkhilari mkhilari commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

paged_stash_memory_policy.py imported triton at module level, and graph_trainer.trainer imports it through graph_pp/pipeline.py -> graph_builder.py -> graph_builder_utils.py. On a platform without Triton wheels (macOS, CPU only) GraphTrainer therefore did not import, and three CPU test modules did not collect: tests/unit_tests/cpu/test_trainer.py, config/test_config_validation.py, config/test_cuda_graph_validation_config.py (noted as the remaining collection errors in #5565, now merged).

  • The two Megatron Core kernels (paged_stash_copy_kernel, paged_stash_pop_kernel) and GLOBAL_BLOCK_SIZE move verbatim, with their "copied from Megatron Core 0.19.0, do not clean up" header, into paged_stash_kernels.py. Nothing outside PagedStashBuffer.launch_copy / launch_pop referenced them (they were in __all__, which no longer lists them).
  • launch_copy and launch_pop import the kernel module at launch. The policy module imports everywhere, a launch on a machine without Triton raises Python's own ModuleNotFoundError: No module named 'triton', and the kernels can only run on CUDA anyway.
  • No change where Triton is installed: the same kernels run from the same launch sites, imported once from sys.modules after the first launch.

Test plan

New torchtitan/experiments/graph_trainer/tests/test_optional_imports.py, in a subprocess that blocks triton on sys.meta_path, GraphTrainer and paged_stash_memory_policy import with triton never loaded, importing paged_stash_kernels and calling PagedStashBuffer.launch_copy raise ModuleNotFoundError with name == "triton". Fails on main (the policy import raises), passes with this change.

On macOS 15 (no Triton), torch 2.16.0.dev20261022 CPU, torch_checkpointing 0.1.1, on main 3ed5aa9 (with #5565 merged):

  • pytest tests/unit_tests/cpu --collect-only, 3 errors / 1847 tests collected on main (test_trainer.py, config/test_config_validation.py, config/test_cuda_graph_validation_config.py, all the Triton import) -> 0 errors / 1926 tests collected with this change.
  • tests/unit_tests/cpu/test_trainer.py, config/test_config_validation.py, config/test_cuda_graph_validation_config.py, 79 passed.
  • The new test, 1 passed.
  • pre commit on the three files passes every hook except pyrefly, which reports the import triton lines of the new module (no Triton locally) and diagnostics that pre date this change in paged_stash_memory_policy.py.

The GPU paged stash tests (torchtitan/experiments/graph_trainer/tests/test_paged_stash.py) import only the policy module's public names, which are unchanged, I could not run them here (no CUDA).

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 24, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 24, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

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.

should be put under graph_trainer folder

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.

@tianyu-l Sure, moved to torchtitan/experiments/graph_trainer/tests/test_optional_imports.py with no other change. One note, the CPU unit test workflow only collects tests/unit_tests/cpu and the graph_trainer workflows run named files, so nothing in CI executes it from here. Happy to add a line to integration_test_8gpu_graph_trainer.yaml if you want it run (it blocks triton inside its subprocess, so it passes on the GPU runners as well).

@mkhilari

mkhilari commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@SherlockNoMad

Copy link
Copy Markdown
Contributor

@claude is this just code move?

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @SherlockNoMad's task in 56s —— View job


Yes. It's a code move plus two lazy imports: the Triton kernels are byte-identical, and the only behavioral change is when triton gets imported.

What I checked (diff vs base 3ed5aa9)

paged_stash_memory_policy.py (+15/-273)

  • The 265 lines removed (the GLOBAL_BLOCK_SIZE constant, paged_stash_copy_kernel, paged_stash_pop_kernel and the "copied verbatim from Megatron-Core 0.19.0" header) match lines 21-283 of the new paged_stash_kernels.py exactly. Kernel bodies, comments and docstrings are unchanged.
  • The module-level import triton / import triton.language as tl are removed.
  • PagedStashBuffer.launch_copy and launch_pop now import GLOBAL_BLOCK_SIZE and their kernel from paged_stash_kernels inside the method. After the first launch this is a sys.modules lookup. The kernels are still the same @triton.jit objects, launched from the same call sites with the same arguments.
  • __all__ no longer lists GLOBAL_BLOCK_SIZE, paged_stash_copy_kernel or paged_stash_pop_kernel. Nothing else in the repo imported them from the policy module.
  • One docstring line now names paged_stash_kernels.

paged_stash_kernels.py (new)

  • It adds a module docstring and from __future__ import annotations on top of the moved code. The old policy module already had from __future__ import annotations (line 210), so the tl.constexpr annotations were already strings under PEP 563 and Triton sees the same thing as before.

tests/test_optional_imports.py (new, 56 lines)

  • In a subprocess that blocks triton, it checks that GraphTrainer imports without loading triton, and that a launch raises ModuleNotFoundError(name="triton").
  • As the author noted in the thread, no CI job collects this path (the CPU job only collects tests/unit_tests/cpu, and the graph_trainer jobs run named files). So it only runs if someone runs it by hand, or if it gets added to a workflow.

Numerics / perf: no change where Triton is installed: same kernels, same launch sites, same arguments.

@SherlockNoMad SherlockNoMad 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.

Thanks for the fix!

@SherlockNoMad

Copy link
Copy Markdown
Contributor

I see you are working at Microsoft, wondering what's your interests in GraphTrainer?

@mkhilari

mkhilari commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@SherlockNoMad Thanks for the review.

I'm contributing to GraphTrainer to build up my open source contributions, and eventually work in AI training and research, at Pytorch, Meta or elsewhere.

GraphTrainer stood out because the idea of training large models from a single compiled graph instead of eager PyTorch was cool.

@SherlockNoMad
SherlockNoMad merged commit e06b11e into pytorch:main Nov 6, 2026
3 checks passed
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.

3 participants