Repository navigation
Allow model imports without attn_gym - #5231
Conversation
[ghstack-poisoned]
| @@ -0,0 +1,151 @@ | |||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I don't have any final solution yet. Let's add a TODO to find a final solution after PTC, lol.
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
[ghstack-poisoned]
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
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.
…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)
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.