Support quantization for tokamax gmm v2#4451
Conversation
2f59d6c to
f814413
Compare
| False, | ||
| description="Whether to use the Tokamax library for GMM kernel implementation.", | ||
| ) | ||
| use_gmm_v2: bool = Field( |
There was a problem hiding this comment.
Can we still have this config that sets all fwd, dlhs and drhs to true in types.py when true?
|
🤖 Hi @RissyRan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
There was a problem hiding this comment.
This pull request introduces quantization support for Tokamax GMM V2, enabling memory-efficient Mixture of Experts (MoE) operations with robust performance profiles. The implementation is highly structured and includes comprehensive value/gradient equivalence unit tests and integration smoke training. While the overall quality of the code is high, a critical logic issue in the backward-pass selection needs to be resolved prior to merging.
🔍 General Feedback
- Comprehensive Testing: Excellent inclusion of both value/gradient equivalence checks and TPU integration smoke tests. This is a very robust testing strategy.
- Robust Parameter Validation: The added configuration checks in
types.pyare thorough and prevent invalid parameter combinations cleanly. - Temporary Code Cleanup: There are a couple of left-over development configurations and TODOs that should be reverted before finalizing the merge.
| # TODO: revert before merge | ||
| from absl import logging | ||
| logging.set_verbosity(logging.INFO) |
There was a problem hiding this comment.
| # TODO: revert before merge | |
| from absl import logging | |
| logging.set_verbosity(logging.INFO) |
| import qwix.pallas as qpl | ||
| import tokamax | ||
| from tokamax._src.ops.ragged_dot import pallas_mosaic_tpu_kernel as tokamax_backend | ||
|
|
There was a problem hiding this comment.
| # TODO: revert before merge | ||
| # logger.setLevel(os.environ.get("LOGLEVEL", "INFO")) |
There was a problem hiding this comment.
| # TODO: revert before merge | |
| # logger.setLevel(os.environ.get("LOGLEVEL", "INFO")) | |
| logger.setLevel(os.environ.get("LOGLEVEL", "INFO")) |
| implementation="mosaic", | ||
| **dlhs_kwargs, | ||
| ) | ||
| if not use_gmm_v2_dlhs and use_gmm_v2_fwd: |
There was a problem hiding this comment.
Please change the second if to elif to make the conditional branches mutually exclusive.
| if not use_gmm_v2_dlhs and use_gmm_v2_fwd: | |
| elif not use_gmm_v2_dlhs and use_gmm_v2_fwd: |
There was a problem hiding this comment.
Thanks for catching this! I have updated. It previously affected v1 path. My experiments with current head is around v2+v1+v2 or v2+v2+v2. Therefore, they may not be affected.
| raise ValueError("Reference and actual pytrees must have the same structure.") | ||
|
|
||
| log_lines = ["CALCULATE DIFF"] | ||
| for (path, a), (_, b) in zip(leaves_a, leaves_b): |
There was a problem hiding this comment.
| for (path, a), (_, b) in zip(leaves_a, leaves_b): | |
| if a is None or b is None: | |
| assert a is None and b is None, f"Structure mismatch: one leaf is None at {path}" | |
| continue | |
| assert a.dtype in (jnp.float32, jnp.bfloat16, jnp.float16) |
RissyRan
left a comment
There was a problem hiding this comment.
Thanks for the change!
|
|
||
| # Use tokamax library for gmm kernel implementation | ||
| use_tokamax_gmm: false | ||
| # Whether to use GMM v2 for MoE. (Requires use_tokamax_gmm: true) |
There was a problem hiding this comment.
Wondering if we should simplify back to use_gmm_v2 config for users? What's the gain for using different combination?
There was a problem hiding this comment.
Also, could you help update this file when applies?
| False, | ||
| description="Whether to use GMM v2 for MoE backward pass (drhs). (Requires use_tokamax_gmm: true)", | ||
| ) | ||
| num_moe_emb_chunks: int = Field( |
|
|
||
| if scale.ndim == 2: # Per-Channel quantization | ||
| rhs_scale = jnp.expand_dims(scale, axis=(1, 2)) | ||
| elif scale.ndim == 3: # Block-wise quantization |
There was a problem hiding this comment.
Does this mean we could support block-wise?
| # Forked from: | ||
| # https://github.com/openxla/tokamax/blob/3f332fcf85dcb87aab661d00228ed71a09b5fd56/ | ||
| # tokamax/_src/ops/ragged_dot/pallas_mosaic_tpu_v2_gmm_kernel.py | ||
| # https://github.com/openxla/tokamax/blob/3f332fcf85dcb87aab661d00228ed71a09b5fd56/tokamax/_src/ops/ragged_dot/pallas_mosaic_tpu_v2_gmm_kernel.py |
There was a problem hiding this comment.
When will we deprecate and move?
| transpose_rhs=not transpose_rhs, | ||
| interpret=interpret, | ||
| input_buffer_count=input_buffer_count[1], | ||
| ) |
There was a problem hiding this comment.
| ) | |
| **dlhs_kwargs, | |
| ) |
| if self.use_gmm_v2 and not self.use_tokamax_gmm: | ||
| if (self.use_gmm_v2_fwd or self.use_gmm_v2_dlhs or self.use_gmm_v2_drhs) and not self.use_tokamax_gmm: | ||
| raise ValueError("GMM v2 requires `use_tokamax_gmm=true`.") | ||
|
|
There was a problem hiding this comment.
Shall we also add a check to disable use_gmm_v2* and use_manual_quantization?
Description
Integrate quantization of Tokamax GMM V2. This is joint effort with @CaptainO5, built on top of PR#4197.
Fix: b/534817371
What this does
base.yml: introduce flagsuse_gmm_v2_fwd,use_gmm_v2_dlhs,use_gmm_v2_drhs:src/maxtext/kernels/megablox/ops.py: quantization and scaling integration of gmm2 / tgmm2tests.integration.tokamax_test: smoke traintests.unit.moe_test: value and gradient equivalencegmmchecks agaisnt empty rule to ensure quantization is applied;use_qwix_quantizationonly whenquantizationis enabled.save_checkpoint_on_startto allow skipping for testingTests
Unit test
Unit test: smoke train
https://paste.googleplex.com/6354648482054144
Unit test: value and gradient equivalence
https://paste.googleplex.com/4962768028565504
E2E test
Detail: b/534817371#comment3
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.