Add Liger Kernel support for Gemma 4 Unified text (gemma4_unified_text) - #1311
Add Liger Kernel support for Gemma 4 Unified text (gemma4_unified_text)#1311lefft wants to merge 1 commit into
Conversation
Mirrors the gemma4_text (omni) support: apply_liger_kernel_to_gemma4_unified_text with an FLCE causal forward for Gemma4UnifiedForCausalLM, RMSNorm/GeGLU class swaps targeting modeling_gemma4_unified (reusing the Gemma4 wrapper classes), the shared LigerGemma4UnifiedCausalLMOutputWithPast output class, a registry entry for model_type gemma4_unified_text, monkey-patch unit tests, and bf16 convergence coverage (FLCE-enabled text rows). Multimodal support (apply_liger_kernel_to_gemma4_unified for Gemma4UnifiedForConditionalGeneration) follows in a stacked PR, mirroring how gemma4 shipped (linkedin#1196 text -> linkedin#1203 multimodal). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
0470783 to
e0f914b
Compare
|
Rebased onto current #1313 landed after this PR was opened and reworked the same convergence files, which moved the Re-ran the gemma4-filtered suites on the rebased HEAD at transformers 5.10.1 and 5.14.1
|
|
Hi @Mecoli1219 , can the workflow run here be approved so I can see the CI report? Happy to address any issues that arise. This PR is self-contained, but #1312 is stacked on top of it, so I've flipped that to draft until this one is resolved. (And BTW I'm new here -- I've read |
Summary
Adds Liger Kernel support for Gemma 4 Unified text models (
model_type: gemma4_unified_text):apply_liger_kernel_to_gemma4_unified_text, with RMSNorm, GeGLU, CrossEntropyLoss, andFusedLinearCrossEntropy.
Liger Kernel already supports the other Gemma 4 families —
gemma4_text(dense text, #1196) andgemma4(omni multimodal, #1203). Butgoogle/gemma-4-12B/gemma-4-12B-itship as a distinctmodel_type: gemma4_unifiedwith their ownmodeling_gemma4_unifiedmodule, so those existingpatches don't reach them —
_apply_liger_kernelno-ops withno Liger kernels supported for model type: gemma4_unified. This PR adds the text half of thatmissing coverage; the multimodal class the 12B checkpoints actually load as is added in the
follow-on PR (see Details).
Part of #1308.
Details
Gemma4UnifiedRMSNorm/Gemma4UnifiedTextMLPare implementation-identical togemma4's (ones-init no-offset RMSNorm with fp32 compute and a
with_scale=Falsevariant; GeGLUwith the double-wide KV-shared handling), so the existing
LigerRMSNormForGemma4/LigerGEGLUMLPForGemma4wrappers are reused — only the patch targets change(
modeling_gemma4_unified.*). Cross-module application of the gemma4 patch functions is notpossible (different class objects), hence the parallel apply function.
causal_forwardreturns the upstreamGemma4UnifiedCausalLMOutputWithPast(extended with liger'stoken_accuracy/predicted_tokens), passingshared_kv_statesthrough — unlike the gemma4sibling's causal path, which uses the generic
LigerCausalLMOutputWithPast. The model-specificclass is used deliberately so the unified model's
shared_kv_statesfield isn't dropped.final_logit_softcapping(30.0 on the 12B checkpoint) flows through both the fused and non-fusedloss paths.
Gemma4UnifiedForCausalLM— a bareGemma4UnifiedTextModelhas nolm_head, so binding the causal forward to it would crash on firstuse. (The gemma4 sibling has the unguarded version of this; happy to align it in a follow-up.)
roperemains an accepted no-op (upstream uses a single-tensorapply_rotary_pos_emb,incompatible with
liger_rotary_pos_emb), matching gemma4.main(post-Fix convergence test failures #1313). The newmini_gemma4_unified_textconvergence rowscarry the same
logprobs_atol(7e-1) that Fix convergence test failures #1313 calibrated for themini_gemma4_textsibling —same architecture family, same bf16 near-tie fragility in the eval top-k logprobs metric.
apply_liger_kernel_to_gemma4_unifiedforGemma4UnifiedForConditionalGeneration, the classgemma-4-12B-itloads as) stacks on this one —Add Liger Kernel support for Gemma 4 Unified multimodal (gemma4_unified) #1312. This mirrors how gemma4 shipped ([Gemma 4] Add apply_liger_kernel_to_gemma4_text (dense text, 31B-targeted) #1196 text → [Gemma 4] Add multimodal support (apply_liger_kernel_to_gemma4 for Gemma4ForConditionalGeneration) #1203 multimodal).
pytest -k gemma4now also matches the gemma4_unified tests; use-k "gemma4 and not unified"to isolate the omni family.Testing Done
Environment: Modal H100, torch 2.13.0+cu130, triton 3.7.1; all suites re-run after rebasing onto
main@ ed08f6e, at transformers 5.10.1 and 5.14.1 (current.[dev]resolve). Resultsidentical at both versions: every gemma4-filtered command exits 0.
make checkstyle:test/transformers/test_monkey_patch.py -k gemma4— 3/3 (the new text instance test plus bothgemma4 omni sibling tests as regression check):
Convergence, new text rows — these run FLCE-enabled, so this PR exercises the fused path on its
own (result lines condensed from the
-vlive logs; the siblingmini_gemma4_textrows are includedby the same filter and pass alongside):
The multimodal convergence files are also swept by the
-k gemma4filter; on this branch theycontain only the
mini_gemma4sibling row, which lands as thexfail#1313 marked it:make testto ensure correctness — run on the follow-on multimodal PR's branch tip, whose tree is the union of both PRs and so covers everything here (transformers 5.14.1): 3907 passed, 1181 skipped, 14 xfailed in 43:06 — zero failuresmake checkstyleto ensure code stylemake test-convergenceto ensure convergence — gemma4-filtered rows as above; all commands exit 0 at both transformers versions