# Contributing Gemma 4 support and ONNX export optimizations

**URL:** <https://discuss.huggingface.co/t/contributing-gemma-4-support-and-onnx-export-optimizations/177523>\
**Category:** 🤗Transformers\
**Created:** [July 6, 2026, 9:40pm UTC](https://discuss.huggingface.co/t/contributing-gemma-4-support-and-onnx-export-optimizations/177523 "2026-07-06T21:40:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjcollier](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/cjcollier/32/61416_2.png) [@cjcollier](https://discuss.huggingface.co/u/cjcollier)\
**Post date:** [July 6, 2026, 9:40pm UTC](https://discuss.huggingface.co/t/contributing-gemma-4-support-and-onnx-export-optimizations/177523/1 "2026-07-06T21:40:24Z")

</div>

Hello everyone,

I am preparing to open a pull request to contribute upstream support for the Gemma 4 model family, alongside ONNX export tracing optimizations for custom hardware attention plugins. Before submitting, I wanted to open this topic to coordinate with the maintainers and share the technical details of the changes.

Our work focuses on resolving architecture-specific limitations during ONNX tracing, as well as correcting layer calculations for heterogeneous model variants (MoE and Dense).

### **Key Technical Contributions:**

1. **RMSNorm Formulation Realignment:** Gemma 4 reverts to the standard Llama-style scale-multiplication (output = normed\_states \* weight) rather than the Gemma 2 style (weight + 1.0) offset. We have gated the use\_gemma2\_format property inside the default RMSNorm layer based on model type to prevent activation scaling errors when instantiating Gemma 4 configurations.

2. \*\*ONNX Tracing Compatibility (GQA & MoE Attention):  
\*\*

3. **Symbolic Tensor Casting:** Added scalar type-checking and int() conversion safeguards in sdpa\_mask and flex\_attention\_mask to prevent JIT compilation failures when processing symbolic scalar tensors during dynamic export tracing.

4. **Storage-Aware Shard Consolidation:** Added an optional storage-level consolidation pass inside PreTrainedModel.save\_pretrained for MoE configurations. This prevents pathological file-sharding patterns (such as generating tens of thousands of tiny individual files) by consolidating shared parameter storage into contiguous 1GB binary buffers on local unified memory architectures.

5. **Robust Tokenizer Attribute Resolution:** Implemented MRO class-hierarchy attribute lookup inside PreTrainedTokenizerBase.\__getattr_\_ to prevent erroneous AttributeError exceptions when instantiating custom tokenizer subclasses that inject class-level properties.

### **Current Verification Status:**

We have built and verified the updated package locally. The core model architectures load and compile correctly. We also executed the gemma2 PyTorch modeling test suite to verify backwards-compatibility, resulting in 157 passed test cases (and 1,157 subtests passed).

I would appreciate any initial feedback or coordination from @ArthurZucker, @CyrilVallez, and vasqu regarding these changes before I open the pull request.

---

<div class="post-metadata">

**Author:** ![cjcollier](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/cjcollier/32/61416_2.png) [@cjcollier](https://discuss.huggingface.co/u/cjcollier)\
**Post date:** [July 7, 2026, 4:59pm UTC](https://discuss.huggingface.co/t/contributing-gemma-4-support-and-onnx-export-optimizations/177523/2 "2026-07-07T16:59:39Z")

</div>

Well, if nobody’s going to respond I guess I’ll just file an issue and submit the PR
