Skip to content

Allow model imports without attn_gym - #5231

Merged
fegin merged 5 commits into
mainfrom
gh/fegin/183/head
Oct 10, 2026
Merged

fegin merged 5 commits into
mainfrom
gh/fegin/183/head

Conversation

@fegin

@fegin fegin commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Installing attn_gym does not always work for all users. It requires a specific cutedsl version. Not all models currently require attn_gym. But the attention module started exporting KDA metadata after #4639, which makes attn_gym import a requirement for all users -- users who were not able to install attn_gym couldn't run TorchTitan anymore.

This PR adds a proxy module to avoid this. In the longer term, we need to revisit how we import and use attn_gym.

Validation: 8 CPU regression tests passed. Muse Glimmer text completed 10 steps on 8 H100 GPUs with FSDP in a conda environment that doesn't have attn_gym installed.

fegin added 2 commits October 9, 2026 09:48
[ghstack-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 9, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 9, 2026
@fegin
fegin requested a review from jinsooihm October 9, 2026 16:48
@@ -0,0 +1,151 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

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.

Is torchtitan/attn_gym.py the best location for this file?

Also, how does this implementation compare to the optional import in torchtitan/models/common/dist_moe/__init__.py?

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

Is torchtitan/attn_gym.py the best location for this file?

Yes, that's a good point.

Also, how does this implementation compare to the optional import in torchtitan/models/common/dist_moe/init.py?

This implementation is more robust but more verbose. We have to explicitly define each used attribute. DistMoE doesn't need this yet

A better way to frame the difference:

  • Dist-MoE raises when code accesses the missing package.
  • This PR returns placeholders during import and raises when kernels are constructed or called.

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 actually don't mind it to be verbose torchtitan/models/common/attn_gym

  • it's AI writing code anyways
  • it's a reminder that we need to fix this

btw, I didn't see TODO in this PR, please don't tell me this is final solution, lol

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

I don't have any final solution yet. Let's add a TODO to find a final solution after PTC, lol.

[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 9, 2026
Installing attn_gym does not always work for all users. It requires a specific cutedsl version. Not all models currently require attn_gym. But the attention module started exporting KDA metadata after #4639, which makes attn_gym import a requirement for all users -- users who were not able to install attn_gym couldn't run TorchTitan anymore.

This PR adds a proxy module to avoid this. In the longer term, we need to revisit how we import and use attn_gym.

Validation: 8 CPU regression tests passed. Muse Glimmer text completed 10 steps on 8 H100 GPUs with FSDP in a conda environment that doesn't have attn_gym installed.
ghstack-source-id: 00ba7fd
Pull-Request: #5231
fegin added 2 commits October 9, 2026 11:54
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 9, 2026
Installing attn_gym does not always work for all users. It requires a specific cutedsl version. Not all models currently require attn_gym. But the attention module started exporting KDA metadata after #4639, which makes attn_gym import a requirement for all users -- users who were not able to install attn_gym couldn't run TorchTitan anymore.

This PR adds a proxy module to avoid this. In the longer term, we need to revisit how we import and use attn_gym.

Validation: 8 CPU regression tests passed. Muse Glimmer text completed 10 steps on 8 H100 GPUs with FSDP in a conda environment that doesn't have attn_gym installed.
ghstack-source-id: 17f9d18
Pull-Request: #5231
fegin added a commit that referenced this pull request Oct 9, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #5231
* __->__ #5230

To make DistMoE and CUDA graph work together, we need this commit to
avoid CoW.
@fegin
fegin changed the base branch from gh/fegin/183/base to main October 9, 2026 23:27
@fegin
fegin merged commit 913413c into main Oct 10, 2026
tianyu-l pushed a commit that referenced this pull request Oct 18, 2026
…5467)

## Summary

#5231 made the models importable when `attn_gym` is **absent**. On a
machine where `attn_gym` is **installed but one of its kernel backends
is not** (cutlass and triton have no macOS wheels, the same happens on
any non CUDA box without them), `attn_gym` itself imports fine and its
submodules raise `ModuleNotFoundError(name="cutlass")` /
`(name="triton")` at import time. The facade's `_load_module` only
tolerates `error.name == "attn_gym"`, so the error escapes,
`torchtitan.models.common` fails to import, and none of
`tests/unit_tests/cpu` can collect.

This keeps the existing contract from #5231 and extends it to that case:

- `_load_module` records a `ModuleNotFoundError` raised from inside an
`attn_gym.*` import (keyed by module) and resolves the export to
`_MissingKernel`, so the identity checks in `kda.py` / `gdn.py` /
`linear_attention.py` / `cp_linear_attention.py` still skip SPMD
registration.
- `_raise_missing` re-raises the recorded backend error (same `name` and
message) when a placeholder kernel is called, and `require_attn_gym()`
raises it when a kernel is built. The existing
`test_linear_attention_reports_missing_dependency[cutlass-*]` cases
therefore still see `error.name == "cutlass"`.
- No change when `attn_gym` is missing, and no change when every backend
imports.
- `require_attn_gym()` is deliberately coarse, any recorded backend
failure makes it raise, which matches the previous behaviour where the
module import itself failed. Per kernel granularity can come later if a
backend mix (e.g. cuDNN present, cutlass absent) turns out to matter.

Related: #5181 (@githubsgi) handles the same symptom for the KDA import
in `attention/__init__.py`, this does it once at the facade for every
export, including the `linear_attention.py` import that still fails
without it.

## Test plan

New
`test_common_attention_imports_with_broken_attention_gym_backend[cutlass|triton]`
in `tests/unit_tests/cpu/models/common/test_optional_imports.py`: an
installed `attn_gym` whose submodules all fail on the backend import,
the common package and every model debug config still import/build,
`_MissingKernel` is not registered with SPMD, and calling a kernel,
`require_attn_gym()`, or building `KDAKernel` / `GDNKernel` raises the
backend's own `ModuleNotFoundError`.

On macOS 15 (Apple Silicon, torch 2.16.0.dev20261014+cpu, attn-gym
0.0.17 without cutlass/triton):
- before: `pytest tests/unit_tests/cpu/components/checkpointer` → 2
errors during collection (`No module named 'cutlass'`, then `'triton'`)
- after: `tests/unit_tests/cpu/models/common/test_optional_imports.py`
11 passed; `tests/unit_tests/cpu/components/checkpointer` 120 passed, 2
failed (unrelated: torch_checkpointing uses `os.O_DIRECT`, reported
separately); `torchtitan/experiments/torchft/tests` 26 passed
- `pre-commit run --files` on both files: all hooks pass (flake8, µfmt,
pydoclint, codespell, pyrefly)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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