[Stacked PR 5/5] Add granular GDN remat policy and 8k/64k latency/memory benchmark suite - #5098
Rohan-Bierneni wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a fused analytical Pallas Gated Delta Net (GDN) kernel and backward pipeline, integrating it into the Qwen3 model. Key changes include the addition of the hybrid_bwd_analytical_pipeline and a suite of supporting local GDN kernel files to handle causal Conv1D and Gated Delta Rule operations with cached triangular inverse matrices. Additionally, sublane tiling logic is adjusted in the GMM and TGMM kernels. Feedback on the changes highlights a numerical inconsistency where an epsilon of 1e-12 is used for L2 normalization in the backward pass instead of the 1e-6 used in the forward pass, as well as an opportunity to simplify the sublane alignment assignment in the TGMM kernel.
|
|
||
| if use_qk_norm_in_gdn: | ||
| d_q_scaled = d_q_proj * scale | ||
| r_q = jnp.sqrt(jnp.sum(q_orig**2, axis=-1, keepdims=True) + 1e-12) |
There was a problem hiding this comment.
The epsilon value 1e-12 used here for calculating the norm of q is inconsistent with the value 1e-6 used in the forward pass (l2norm at line 521). For numerical stability and correctness of the gradient, the same epsilon value should be used in both forward and backward computations.
| r_q = jnp.sqrt(jnp.sum(q_orig**2, axis=-1, keepdims=True) + 1e-12) | |
| r_q = jnp.sqrt(jnp.sum(q_orig**2, axis=-1, keepdims=True) + 1e-6) |
| - q_unit * jnp.sum(d_q_scaled * q_unit, axis=-1, keepdims=True) | ||
| ) / r_q | ||
|
|
||
| r_k = jnp.sqrt(jnp.sum(k_orig**2, axis=-1, keepdims=True) + 1e-12) |
There was a problem hiding this comment.
Similar to the calculation for r_q, the epsilon value 1e-12 used here for r_k is inconsistent with the 1e-6 used in the forward pass. This should be corrected to 1e-6 to ensure numerical consistency.
| r_k = jnp.sqrt(jnp.sum(k_orig**2, axis=-1, keepdims=True) + 1e-12) | |
| r_k = jnp.sqrt(jnp.sum(k_orig**2, axis=-1, keepdims=True) + 1e-6) |
| common_sublane = min(size_lhs_sublane, size_rhs_sublane) | ||
| size_lhs_sublane = common_sublane | ||
| size_rhs_sublane = common_sublane |
There was a problem hiding this comment.
The logic to ensure size_lhs_sublane and size_rhs_sublane are equal can be simplified into a single line, improving readability.
| common_sublane = min(size_lhs_sublane, size_rhs_sublane) | |
| size_lhs_sublane = common_sublane | |
| size_rhs_sublane = common_sublane | |
| size_lhs_sublane = size_rhs_sublane = min(size_lhs_sublane, size_rhs_sublane) |
83ce15c to
5a5586c
Compare
5a5586c to
307676e
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
d9b661b to
a127aa0
Compare
aeece03 to
56758af
Compare
56758af to
a967a1d
Compare
a967a1d to
1af4dc3
Compare
b8c824e to
d8f0b06
Compare
…ory benchmark suite - Add native granular remat policy for GatedDeltaNet (gdn and gdn_conv in base.yml, types.py, and maxtext_utils.py) to prevent redundant forward kernel recomputations (~17 ms) during backward autodiff. - Add cross-field validation requiring use_gdn_kernel=True when gdn or gdn_conv is set to 'device' or 'offload', preventing silent misconfiguration with Pure JAX. - Tag core GDN activations (gdn_core_attn_out, gdn_chunk_states, gdn_t_inv, gdn_qkv, gdn_b, gdn_a) and out_proj with jax.ad_checkpoint.checkpoint_name in gdn_bwd_pallas.py and qwen3.py. - Add _get_gdn_aware_remat_policy to ensure forward recurrence residuals are preserved under custom remat (tensors_on_device: ['decoder_layer_input', 'gdn']) while MLP/MoE layers remain rematerialized. - Add test_gdn_custom_remat_policy_preserves_residuals and test_gdn_granular_remat_requires_gdn_kernel to test suite validating gradient invariance, residual preservation, and configuration validation. - Add standalone 8k and 64k Ghostfish Cloud TPU latency and memory benchmark suite (gdn_benchmark_test.py), including isolated Pallas forward/backward kernel profiling and full 1-chip monolithic decoder layer benchmarking (Forward: 68.18 ms, Backward: 185.99 ms, Total: 254.17 ms; Pallas Forward: 37.55 ms, Pallas Backward: 93.59 ms).
d8f0b06 to
1831da6
Compare
1831da6 to
3b39ca9
Compare
Stacked PR Chain
rbierneni-gdn-1-ci-hygienemainrbierneni-gdn-2-fwd-kernelrbierneni-gdn-1-ci-hygienerbierneni-gdn-3-bwd-kernelrbierneni-gdn-2-fwd-kernelrbierneni-gdn-4-model-integrationrbierneni-gdn-3-bwd-kernelrbierneni-gdnv3-bwdrbierneni-gdn-4-model-integrationDescription
This is PR 5 of 5 (the capstone PR #5098) in the stacked series establishing the canonical Pallas Gated Delta Net (GDN) implementation in MaxText.
This PR introduces:
Granular Native GDN Rematerialization Policy:
gdn: RematLocationandgdn_conv: RematLocation('remat','device','offload') tobase.yml,types.py, andmaxtext_utils.py.validate_gdn_remat_requires_kernel) enforcing that settinggdnorgdn_convto'device'or'offload'requiresuse_gdn_kernel=True. This prevents silent misconfiguration where Pure JAX would otherwise ignore the setting and redundantly recompute activations._expand_gdn_remat_namesinmaxtext_utils.pyexpanding"gdn"to individual named residuals:["gdn_core_attn_out", "gdn_chunk_states", "gdn_t_inv", "gdn_qkv", "gdn_b", "gdn_a"]and"gdn_conv"to["gdn_conv_out", "gdn_fwd_conv"].jax.ad_checkpoint.checkpoint_nameingdn_bwd_pallas.pyandqwen3.py._get_gdn_aware_remat_policyinqwen3.pyensuring that when custom remat is enabled (tensors_on_device: ["decoder_layer_input", "gdn"]), forward recurrence residuals are preserved in device memory, completely eliminating redundant forward kernel recomputation (~17 ms) during backward autodiff while keeping all Feed-Forward/MoE layers 100% rematerialized.gdn_bwd_pallas_test.pyandpyconfig_test.pycovering gradient invariance, residual preservation, and configuration validation.Cloud TPU Latency & Memory Benchmark Suite (
tests/unit/gdn_benchmark_test.py):64k Ghostfish Cloud TPU Hardware Performance (1,024 Chunks, S=65,536, B=1, H_k=16, H_v=64, D=128):
Files Changed
src/maxtext/configs/base.yml: Addedgdn: 'remat'andgdn_conv: 'remat'custom remat options.src/maxtext/configs/types.py: Addedgdnandgdn_convfields inRematAndOffload, included in custom remat lists, and added cross-field validator requiringuse_gdn_kernel=True.src/maxtext/utils/maxtext_utils.py: Added_expand_gdn_remat_namesand integrated withget_save_and_offload_names.src/maxtext/models/kernels/gdn/gdn_bwd_pallas.py: Tagged output and residuals withcheckpoint_name.src/maxtext/models/qwen3.py: Added_GDN_SAVED_NAMES,_get_gdn_aware_remat_policy, and taggedgdn_core_attn_outandout_proj.tests/unit/gdn_bwd_pallas_test.py: Addedtest_gdn_custom_remat_policy_preserves_residualsandtest_gdn_granular_remat_requires_gdn_kernel.tests/unit/pyconfig_test.py: Addedtest_gdn_granular_remat_requires_gdn_kernelandtest_gdn_granular_remat_accepts_gdn_kernel.tests/unit/gdn_benchmark_test.py: Standalone 8k and 64k Cloud TPU latency and memory benchmark suite with isolated kernel timings.Tests
pre-commit run --files ...(all hooks: pylint, pyink, codespell, yamllint passed cleanly).-c opt://third_party/py/maxtext/tests/unit:gdn_bwd_pallas_test,//third_party/py/maxtext/tests/unit:pyconfig_test, and//third_party/py/maxtext/tests/unit:gdn_benchmark_ghostfish_test.Checklist