Support importing xFormers on PyTorch builds without distributed support #1403
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1403"
general
weight: 0.35
lw
What does this PR do?
Fixes #1378.
Makes xFormers importable on PyTorch builds that were compiled without distributed support, so
torch.distributed.is_available()isFalse. This is the case for the official NVIDIA PyTorch containers for Jetson iGPU devices, for examplenvcr.io/nvidia/pytorch:25.03-py3-igpu. Todayimport xformers.opsdies with anAttributeErroron such a build, which takes the whole program down even for users who only want memory-efficient attention.The crash
When PyTorch is built without distributed support,
torch/distributed/__init__.pyskips its wholeif is_available():block and defines only two stubs.torch.distributed.Work,torch.distributed.ReduceOpand the submodules that wrap the C++ bindings do not exist.Repro, which also runs on a normal machine because it just removes the attribute that a real no-distributed build never has:
That is the exact traceback from the issue.
Blast radius, and why one line is not enough
Four modules under
xformers/ops/touchtorch.distributedat import time, and all four are pulled in byxformers/ops/__init__.py:1.
differentiable_collectives.pyannotates signatures withtorch.distributed.ProcessGroupandOptional[torch.distributed.Work]. On Python older than 3.14 those annotations are evaluated while the function is being defined, so the module cannot even be created. This is the reported failure.2.
modpar_layers.pyhas the same problem withtorch.distributed.ProcessGroup. It is the module that importsdifferentiable_collectives, so it is next in line.3.
seqpar.pydoesfrom torch.distributed.distributed_c10d import _resolve_process_group, GroupNameat module level.distributed_c10dstarts withfrom torch._C._distributed_c10d import ..., a C extension that is not built at all, so this is a hardModuleNotFoundErrorthat no annotation change can defer. On top of that,GroupNameappears intorch.library.custom_opsignatures, whose schemas are inferred eagerly at decoration time, so the name genuinely has to exist at import time.4.
sequence_parallel_fused_ops.pydoesfrom torch.distributed._symmetric_memory import get_symm_mem_workspace, which also reaches intotorch._C._distributed_c10d, and it annotates itscustom_opsignatures withdist.distributed_c10d.GroupName.Fixing only item 1 moves the crash to item 3 rather than resolving it. I verified this by simulating a no-distributed PyTorch with just the annotation fix in place: the next failure is
seqpar.pyline 10.The fix
Two parts.
Lazy annotations, which is the fix suggested in the issue.
differentiable_collectives.pyandmodpar_layers.pygetfrom __future__ import annotations, so their annotations are stored as strings (PEP 563) and evaluatingtorch.distributed.Workno longer happens at import. Nothing in either module needs annotations resolved at runtime: there is no dataclass field resolution, no pydantic, notyping.get_type_hintscall, and notorch.library.custom_opdecorator. Behavior when distributed is available is unchanged, andtyping.get_type_hintsstill resolves the same types:Skipping the ops that cannot exist.
xformers/ops/__init__.pynow guards the model-parallel and sequence-parallel imports withif _HAS_TORCH_DISTRIBUTED := torch.distributed.is_available():. These ops need NCCL collectives and symmetric memory, so they are unusable on such a build no matter what, and their modules cannot be imported at all for the reasons above. This mirrors the_HAS_MSLKpattern already used a few lines higher in the same file for the fmha andrope_paddedops, including the fact that the conditionally imported names stay listed in__all__. It also matches what the issue asks for: unavailable operators degrade to "not available" and partial usage keeps working.I deliberately did not touch
seqpar.pyorsequence_parallel_fused_ops.py. Making them importable would mean stubbingGroupNameand_resolve_process_groupso that theircustom_opregistrations still succeed, which is more code and more risk for ops that can never run there anyway.Tests
tests/test_import_without_torch_distributed.pyspawns a child interpreter withsys.executable, makestorch.distributedlook like a build without distributed support before xFormers is imported for the first time, then imports the target module and asserts the child exits cleanly. The simulation mirrors what PyTorch itself does whenis_available()isFalse:is_availablereturnsFalse, the availability-gated attributes are removed,GroupNameandProcessGroupare replaced by stubs, andtorch._C._distributed_c10d,torch.distributed.distributed_c10dandtorch.distributed._symmetric_memoryare made unimportable by puttingNoneinsys.modules. It is parametrized overxformers,xformers.opsandxformers.ops.differentiable_collectives, and skips cleanly if the child cannot set the simulation up.Note that on Python 3.14 the annotation half of this bug is hidden by PEP 649, since annotations are lazy by default there. The test still catches the import failures on 3.14 because of the eager submodule imports, and it catches the original
AttributeErroron 3.9 through 3.13, which is where the reporter is (Python 3.12).Fail before, pass after
Environment: macOS arm64, CPU-only, Python 3.12.12, torch 2.13.0 CPU wheel. The test only imports pure-Python modules, so the compiled extensions are not needed.
Base ref restored with
git checkout upstream/main -- xformers/ops/__init__.py xformers/ops/differentiable_collectives.py xformers/ops/modpar_layers.py:Fix restored with
git checkout HEAD -- <same files>:Same three tests also pass on Python 3.14.3.
No regression when distributed is available. Every name in
xformers.ops.__all__that resolved before still resolves,_HAS_TORCH_DISTRIBUTEDisTrue, andColumnParallelLinear,sequence_parallel_leading_matmulandfused_allgather_and_linearare all present. The existing suites that cover the touched modules still pass or skip as they did before, on CPU:The skips are the CUDA-only and multi-GPU cases, which this machine cannot run.
pre-commit run --files ...passes on every changed file with the pinned hook versions from.pre-commit-config.yaml, ufmt 2.8.0 with black 26.3.1 and usort 1.0.8.post1, flake8 6.1.0, mypy 1.10.0.Before submitting
- [x] N/A, no documented behavior changes on builds that have distributed support.
tests/test_import_without_torch_distributed.py.Fixedentry under 0.0.36.