Skip to content

Add TPU Ulysses context parallelism - #4687

Merged
copybara-service[bot] merged 1 commit into
mainfrom
htn-ulysses-cp
Aug 10, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
htn-ulysses-cp

Conversation

@huytransformer

@huytransformer huytransformer commented Jul 31, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR adds context_parallel_strategy="ulysses", DeepSpeed-Ulysses style context parallelism for the TPU splash attention (https://arxiv.org/abs/2309.14509).

Motivations: (1) the existing approaches degrade when the context parallelism degree is large relative to the sequence length. (2) It is also the head-parallel half of a Ulysses x ring hybrid (USP), which can be built on top of this PR.

Performance

What we did: v5p-128 (64 chips), bf16, synthetic data, 2M tokens per step in every cell, remat_policy=custom context=device, dq_reduction_steps=3. Each uses its best known block sizes (2048 for Ulysses, 1024 for Ring and All-gather).

llama2-7b (MHA, 32 KV heads), 32K context, cp=16, 2K tokens per device.

Method Step time TFLOP/s/device MFU
Ulysses 8.90 s 239.4 52.2% Xprof
All-gather 8.83 s 242.7 52.9%
Ring 16.38 s 130.8 28.5% Xprof

Ulysses matches all-gather and runs 1.8x faster than ring, while holding 1/16 of all-gather's attention KV per device (2 of 32 heads).

llama3.1-8b (GQA, 8 KV heads), cp=4.

Context (chunk) Ulysses All-gather Ring
64K (16K) 243.3 / 53.0% Xprof 282.5 / 61.6% Xprof 277.4 / 60.4% Xprof
8K (2K) 259.0 / 56.4% Xprof 281.1 / 61.2% Xprof 274.7 / 59.8% Xprof

Correctness: 20 training steps against all_gather on llama3.1-8b at 64K, cp=4, identical data, max loss difference is 0.001.

Example repro

export LIBTPU_INIT_ARGS="--xla_tpu_scoped_vmem_limit_kib=65472 \
  --xla_tpu_use_enhanced_launch_barrier=true \
  --xla_tpu_enable_async_collective_fusion=true \
  --xla_tpu_enable_async_collective_fusion_fuse_all_gather=true \
  --xla_tpu_enable_async_collective_fusion_multiple_steps=true \
  --xla_tpu_overlap_compute_collective_tc=true \
  --xla_enable_async_all_gather=true \
  --xla_enable_async_collective_permute=true \
  --xla_tpu_enable_sparse_core_collective_offload_all_reduce=true \
  --xla_tpu_enable_sparse_core_collective_offload_reduce_scatter=true \
  --xla_tpu_enable_sparse_core_collective_offload_3d_all_gather=true"
python3 -m maxtext.trainers.pre_train.train src/maxtext/configs/base.yml \
  base_output_directory=${OUTPUT_DIR} run_name=ulysses_cp16 \
  dataset_type=synthetic enable_checkpointing=false steps=10 \
  model_name=llama2-7b max_target_length=32768 per_device_batch_size=1 \
  ici_context_parallelism=16 ici_fsdp_parallelism=4 \
  attention=flash use_tokamax_splash=true use_jax_splash=false \
  context_parallel_strategy=ulysses context_parallel_load_balance=false \
  packing=false dq_reduction_steps=3 remat_policy=custom context=device \
  num_vocab_tiling=16 \
  sa_block_q=2048 sa_block_kv=2048 sa_block_q_dkv=2048 sa_block_kv_dkv=2048

Tests

  • tests/unit/ulysses_attention_test.py: Passed.
  • tests/unit/ulysses_collective_test.py: Passed.
  • tests/unit/configs_value_test.py: Passed.
  • tests/integration/train_tests.py: train smoke test with ulysses cp=4. Passed.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @huytransformer, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions github-actions Bot 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.

## 📋 Review Summary

This Pull Request introduces highly performant TPU Ulysses context parallelism on the TPU Tokamax Splash attention training path. The implementation is exceptionally clean, well-integrated, and backed by comprehensive unit, collective, and integration tests that verify correctness against dense references and compiled HLO collectives.

🔍 General Feedback

  • Excellent Test Coverage: The newly added tests (including CPU multi-device mock checks and HLO collective pattern assertions) set a very high bar for correctness and robustness.
  • Robust Config Validation: The comprehensive validation in configs/types.py ensures that unsupported combinations (like MQA, packing, ragged attention, dropout, etc.) are proactively rejected with precise errors.
  • Clean Shared Utilities: Factoring out mesh and sharding operations into context_parallel_utils.py is a great design choice that improves maintainability.
  • Thorough Documentation: The update to sharding.md is incredibly detailed and accurately captures Ulysses constraints and architectural mechanics.

Comment thread src/maxtext/layers/attention_op.py Resolved
@codecov

codecov Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.10448% with 42 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/layers/attention_op.py 65.85% 23 Missing and 5 partials ⚠️
src/maxtext/utils/sharding.py 68.00% 5 Missing and 3 partials ⚠️
src/maxtext/kernels/attention/ulysses_attention.py 92.68% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@huytransformer
huytransformer marked this pull request as ready for review August 1, 2026 00:01
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Comment thread src/maxtext/layers/attention_op.py Resolved
Comment thread src/maxtext/layers/attention_op.py Resolved
Comment thread tests/unit/attention_test.py Resolved
Comment thread tests/unit/ulysses_attention_test.py Resolved
Comment thread tests/unit/hlo_test_utils_test.py Resolved
Comment thread tests/unit/ulysses_attention_test.py Resolved
Comment thread tests/unit/ulysses_collective_test.py Resolved
Comment thread tests/unit/ulysses_collective_test.py Resolved

@RissyRan RissyRan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM at high level. Some minor comments.

Comment thread src/maxtext/configs/types.py Resolved
Comment thread src/maxtext/configs/types.py Resolved
Comment thread src/maxtext/configs/types.py Resolved
Comment thread src/maxtext/layers/attention_op.py Resolved
Comment thread tests/unit/ulysses_collective_test.py Resolved
Comment thread src/maxtext/configs/types.py Resolved
Comment thread src/maxtext/configs/types.py Resolved
Comment thread src/maxtext/utils/sharding.py Resolved

@susanbao susanbao left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added the comments. Please check. It is great to have Ulysses in maxtext.

@copybara-service
copybara-service Bot merged commit 2bc4aa5 into main Aug 10, 2026
286 of 287 checks passed
@copybara-service
copybara-service Bot deleted the htn-ulysses-cp branch August 10, 2026 23:36
copybara-service Bot pushed a commit that referenced this pull request Aug 14, 2026
Imported from GitHub PR #4836

# Description

This PR introduces `context_parallel_strategy=usp` ([USP](https://arxiv.org/abs/2405.07719), Ulysses over ring). Follow up to #4687.

Currently does not support load balancing + sequence packing.

# Tests

`python3 -m pytest tests/unit/configs_value_test.py tests/unit/usp_attention_test.py tests/unit/usp_collective_test.py`

Passed.

`python3 -m pytest tests/unit/attention_test.py -k usp`

3 passed.

## Performance

llama3-8b, v5p (64 chips), CP64, bf16, synthetic data, global batch 1, no load-balancing

Reporting median step time (s)

|  | 64K | 128K | 256K |
|---|---|---|---|
| all-gather (CP64) | 1.31 | 4.03 | 14.33 |
| ring (CP64) | 1.68 | 4.63 | 15.35 |
| usp (16x4) | 1.31 | 4.02 | 13.92 |
| usp (8x8) | 1.44 | 4.05 | 13.65 |
| usp (32x2) | 1.65 | 4.52 | 14.72 |

Example repro command:

```
python3 -m maxtext.trainers.pre_train.train src/maxtext/configs/base.yml model_name=llama3-8b dataset_type=synthetic enable_checkpointing=False steps=20 per_device_batch_size=0.015625 packing=False attention=flash use_tokamax_splash=True use_jax_splash=False allow_split_physical_axes=True max_target_length=65536 context_parallel_strategy=usp ici_context_parallelism=16 ici_context_ulysses_parallelism=4 context_parallel_load_balance=False run_name=usp16x4_65536 base_output_directory=<output dir>
```

# Checklist

Before submitting this PR, please make sure (put X in square brackets):
- [X] I have performed a self-review of my code. For an optional AI review, add the `gemini-review` label.
- [X] I have necessary comments in my code, particularly in hard-to-understand areas.
- [X] I have run end-to-end tests tests and provided workload links above if applicable.
- [X] I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in [our documentation](https://maxtext.readthedocs.io/en/latest/development.html#adding-new-documentation-files).
Copybara import of the project:

--
e5c987dc7fdf70a6e266856eef47370f2ebc43c1 by continuousml <continuousml@gmail.com>:

Add TPU USP context parallelism

Merging this change closes #4836

COPYBARA_INTEGRATE_REVIEW=#4836 from AI-Hypercomputer:htn-usp-cp e5c987dc7fdf70a6e266856eef47370f2ebc43c1
PiperOrigin-RevId: 964446152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants