Skip to content

[maxtext] Add block-causal attention - #4743

Merged
copybara-service[bot] merged 1 commit into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-v2-pr2-attention
Aug 7, 2026
Merged

copybara-service[bot] merged 1 commit into
AI-Hypercomputer:mainfrom
ethannnnnn:block-diffusion-maxtext-v2-pr2-attention

Conversation

@ethannnnnn

@ethannnnnn ethannnnnn commented Aug 5, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Block diffusion needs attention that is causal across blocks while remaining
bidirectional within the block currently being corrupted or denoised. This is
an attention-kernel concern and can be reviewed independently of corruption,
training objectives, rollout, and RL.

Scope

This PR adds only:

  • an opt-in block_diffusion attention type and positive
    causal_block_size configuration;
  • equivalent dense dot-product and TPU Tokamax Splash block-causal masks;
  • load-balanced context-parallel mask support;
  • validation that rejects packing and unsupported attention/hardware
    combinations; and
  • focused mask, configuration, and autoregressive-regression tests.

It does not depend on the block-diffusion primitive PR, corrupt training data,
change a loss function, add a model-specific branch, or enable diffusion by
default.

Mask semantics

For query position q and key position k, attention is allowed when

floor(q / block_size) >= floor(k / block_size)

Therefore, all positions in the current block can attend to one another and to
all earlier blocks, but no earlier block can attend to a later block. Segment
boundaries remain enforced. Packing is rejected because document-local block
origins are not yet represented by the attention metadata. Autoregressive
decode keeps the existing causal mask path unchanged.

Compatibility

The default remains attention_type=global. The new behavior is activated only
when attention_type=block_diffusion is selected explicitly. Specialized
per-layer attention declarations continue to override the global configuration.

This implementation remains code-independent from the primitives landed in
#4737: it does not import or activate them. The branch is rebased onto the
current main containing #4737 so reviewers see only this attention change.

Tests

python -m pytest -q \
  tests/unit/attention_test.py \
  tests/unit/configs_value_test.py \
  tests/unit/tokamax_splash_attention_mask_test.py

76 passed, 48 skipped, 36 subtests passed

The skipped cases require unavailable platform-specific kernels. Pyink, Python
compilation, and git diff --check also pass for the changed files.

Focused branch coverage for the changed executable code in
src/maxtext/layers/attention_op.py is complete: 53/53 changed lines and
26/26 changed branches are covered.

Checklist

  • I have performed a self-review of my code.
  • I added comments or docstrings for the non-obvious block-mask invariants.
  • I added focused dense, Splash, load-balanced, configuration, and autoregressive-regression tests.
  • End-to-end model training is not applicable to this default-off attention layer; the affected CPU suites are listed above.
  • No standalone documentation page or toctree change is needed; the public design document is linked below.

Design document:
https://docs.google.com/document/d/1N7KcCoAIErB2CV9EJ2G1u_mdN-AQgqwNUYSvM0mtqMI/edit

@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-v2-pr2-attention branch from ff2b021 to 04d6f47 Compare August 5, 2026 18:39
@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-v2-pr2-attention branch from 04d6f47 to 61d4ae5 Compare August 5, 2026 21:46
Comment thread src/maxtext/layers/attentions.py Resolved
@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-v2-pr2-attention branch from 61d4ae5 to b827f6d Compare August 6, 2026 04:04

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

Thank you for the contribution! Added a few documentation suggestions to make it easier to navigate for future users / maintainers.

Comment thread src/maxtext/layers/attention_op.py Resolved
Comment thread src/maxtext/configs/types.py Resolved
Comment thread src/maxtext/layers/attention_op.py Resolved
Comment thread src/maxtext/configs/base.yml Resolved
@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-v2-pr2-attention branch from b827f6d to 6bf2279 Compare August 6, 2026 15:04
@ethannnnnn
ethannnnnn force-pushed the block-diffusion-maxtext-v2-pr2-attention branch from 6bf2279 to df9c7da Compare August 6, 2026 21:09

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

The github actions are temporarily down so the PR tests are queued. Let's watch out for this.

"vocab_size=256",
"max_target_length=2048",
"hardware=cpu",
"packing=False",

@hengtaoguo hengtaoguo Aug 7, 2026 •

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.

I'm seeing an internal test failure related to jax distributed initialization. So allow me add a new flag here to assist the bug fix skip_jax_distributed_system=True.

cc @entrpn The error message is here.

"vocab_size": 256,
"max_target_length": 2048,
"hardware": "cpu",
"packing": False,

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.

Add "skip_jax_distributed_system": True here too.

Add a `block_diffusion` attention type that is causal across blocks and bidirectional within each block.

The dense dot-product and TPU Splash/Tokamax paths share the same mask semantics. Existing autoregressive decode behavior remains unchanged, unsupported kernels fail during config validation, and packed training remains outside this feature scope.

Test Plan:

- block mask unit tests for dense, Splash, load-balanced CP, rectangular queries, and AR fallback

- block attention config validation tests

- focused branch tests cover every changed executable line and branch in `attention_op.py`

fix: skip_jax_distributed_system
@hengtaoguo
hengtaoguo force-pushed the block-diffusion-maxtext-v2-pr2-attention branch from 1bb8c3e to 7a4b75e Compare August 7, 2026 07:27
@copybara-service
copybara-service Bot merged commit 2d872a9 into AI-Hypercomputer:main Aug 7, 2026
53 of 54 checks passed
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