Skip to content

Support quantization for tokamax gmm v2 - #4451

Open
shuningjin wants to merge 31 commits into
mainfrom
gmm2_quantize
Open

Support quantization for tokamax gmm v2#4451
shuningjin wants to merge 31 commits into
mainfrom
gmm2_quantize

Conversation

@shuningjin

@shuningjin shuningjin commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Description

Integrate quantization of Tokamax GMM V2. This is joint effort with @CaptainO5, built on top of PR#4197.

Bug: b/534817371

What this does

  1. Integrate quantization in gmm v2
  • base.yml: use_gmm_v2=true should work for quantization
  • src/maxtext/kernels/megablox/ops.py: quantization and scaling integration of gmm2 / tgmm2
  1. Add unit tests for quantized gmm
  • tests.integration.tokamax_test: smoke train
  • tests.unit.moe_test: value and gradient equivalence
  1. Significant refactor of ops.py: modularize GMM fwd and bwd functions
  • Previously, _gmm_fwd and _gmm_bwd were large, monolithic functions containing complex branching and quantization logic. This is difficult to read and maintain.
  • Breaks them down into smaller, domain-specific sub-functions, significantly improving code readability and maintainability. Execution logic and runtime behavior remain identical, except for the aforementioned gmm v2 change.
  1. Other changes
  • Improve robustness of quantization. For example, gmm checks agaisnt empty rule to ensure quantization is applied; refactor condition use_qwix_quantization to be co-occur with quantization; fix broken link for quantization doc.
  • Add save_checkpoint_on_start to allow skipping for testing
  • update doc for moe configuration: add use_gmm_v2

Tests

Unit test

Unit test: smoke train

python3 -m pytest -v --pyargs tests.integration.tokamax_test -rP -s

https://paste.googleplex.com/6354648482054144

Unit test: value and gradient equivalence

python3 -m pytest -v --pyargs tests.unit.moe_test -rP -s -k "test_gmm_grad_equivalence"

https://paste.googleplex.com/4962768028565504

E2E test

We tested in b/534817371, convergence on par with bf16 for fsdp+ep or fsdp.

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.

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Comment thread src/maxtext/configs/types.py Outdated
# this scale inside the kernel.
dlhs_dout = _dlhs_scale_grad_by_rhs_scale(dlhs_dout, rhs, group_sizes, transpose_rhs)
rhs = rhs.qvalue

@CaptainO5 CaptainO5 Jul 28, 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.

lhs needs to be quantized here, since we are skipping it in the fwd pass when gmm_v2_fwd is enabled.

Suggested change
if quantization_rule.act_qtype and not isinstance(lhs, qpl.QArray):
lhs = qpl.quantize(
lhs,
quantization_rule.act_qtype,
channelwise_axes=[] if quantization_rule.disable_channelwise_axes else [0],
calibration_method=quantization_rule.act_calibration_method,
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the catch! I have corrected the asdtype to qpl.quantize for lhs quantization in bwd.

@shuningjin
shuningjin marked this pull request as draft July 29, 2026 11:48
@shuningjin
shuningjin marked this pull request as ready for review July 31, 2026 19:41
@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.

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

LGTM, just minor comments, related to tolerance.

("tokamax_v1_fp8", "fp8_full", False, 1),
("tokamax_v2_bf16", "", True, 1),
("tokamax_v2_fp8", "fp8_full", True, 1),
("tokamax_v2_bf16", "", True, 2),

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.

nit: tokamax_v2_bf16_ep, and similar to test name bellow

ici_expert_parallelism: int,
):
"""Smoke train with small config."""
sharding_tolerance = 0.22 if ici_expert_parallelism > 1 else 2e-2

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.

Do you need sharding_tolerance in your end-to-end run? If so, could we create a bug to track which part is unsharded. I think we should debug this with

debug_sharding: false # Prints model weights sharding info

Comment thread tests/unit/moe_test.py
"var_grad": grads_tgt,
}

relative_norm_diff_threshold = 0.22 if quantization else 0.012

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 tolerance seems high, and is this aligned with other existing quantization tests?

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.

6 participants