[Gemma 3n]MatFormer Lab
MatFormer Lab:
Gemma 3n is a multimodal, multilingual model from the Gemma family of models. You can read about Gemma 3n in its docs and launch blog post. It is a unique model that is natively elastic, which means you have nested models! Gemma 3n was trained as a E4B model (effectively loads 4B parameters) with 35 layers and a 16,384 FFN hidden dimension. It has a E2B (30 layers and 8,192 FFN hidden dimension) nested within it and jointly trained as a MatFormer.
MatFormer (🪆Matryoshka Transformer) architecture is a novel nested transformer built for elastic inference. Think of it like Matryoshka dolls: a larger model contains smaller, fully functional versions of itself. This approach extends the concept of Matryoshka Representation Learning from just embeddings to all transformer components.
Saving memory by having E2B nested within E4B is practically useful, but what makes MatFormer so powerful is its capability to smoothly span the entire Pareto-optimal accuracy-vs-model size cruve between E2B and E4B without any additional training. Using a simple technique called Mix-n-Match one can extract a model of any size between E2B and E4B from the main E4B model.
Why would you slice a model? Given your specific deployment requirements, the E2B and E4B may not be the right fit. You may want to get an E3B, for example, which has quality higher than the E2B while requiring less compute than E4B.
In this notebook, you get to play with MatFormers and Mix-n-Match. You will specify which configuration you want for the submodel based on FFN dimension and skp layers, and then you will export the model to Hugging Face, enabling you to use with your favorite tools.
Setup
Slicing configuration
As part of the Gemma 3n release, we share optimal slicing configurations as a Hugging Face dataset repository, although you can also explore with your own configurations below.
Each configuration specifies:
- The hidden dimensions of the FFN
- Which layers, if any, to skip
- A MMLU accuracy associated with the pre-trained checkpoints
layer-level and block-level configurations are a result of varying the hidden dimensions of the FFN either at a layer-level (fine-grained) or at a block-level (4 local + 1 global layers).
At a layer-level, we found that having the global layers (instead of local layers) have higher capacity helps with accuracy for the same model size.
At a block-level, we found that the block skipped for E2B (ie., layers 20-24) would benfit from higher capacity when not skipped and earlier blocks can work well with lower-capacity compared to the later blocks.
We invite the community to find even better configurations that lie on the Pareto-optimal curve between E2B and E4B!
Based on your deployment scenarios, you may want to pick a different config. Select below your preferred one.
Config for official E2B Model Layers Skipped: [20, 21, 22, 23, 24] FFN Hidden Dims: [2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4, 2_048 * 4]
If you want to set up your own configuration, please uncomment the following cell and assign values
Slicing
Load the model config and verify slicing configuration
Note: we do not load the model at this stage, just verify that the slicing configuration is possible
config.json: 0%| | 0.00/4.11k [00:00<?, ?B/s]
Update configuration
Save the configuration and the unchanged tokenizer
tokenizer_config.json: 0%| | 0.00/1.20M [00:00<?, ?B/s]
tokenizer.model: 0%| | 0.00/4.70M [00:00<?, ?B/s]
tokenizer.json: 0%| | 0.00/33.4M [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/769 [00:00<?, ?B/s]
chat_template.jinja: 0%| | 0.00/1.63k [00:00<?, ?B/s]
New config saved to my_modified_gemma_3n_model Final number of layers: 30
Load the model checkpoints
Note: we are saving the model to disk, so there's no need to have a large CPU/GPU.
Fetching 4 files: 0%| | 0/4 [00:00<?, ?it/s]
model-00001-of-00004.safetensors: 0%| | 0.00/3.08G [00:00<?, ?B/s]
model-00002-of-00004.safetensors: 0%| | 0.00/4.97G [00:00<?, ?B/s]
model-00004-of-00004.safetensors: 0%| | 0.00/2.66G [00:00<?, ?B/s]
model-00003-of-00004.safetensors: 0%| | 0.00/4.99G [00:00<?, ?B/s]
Slice the model!
Processing shards: 0%| | 0/4 [00:00<?, ?it/s]
Saving shard model-00001-of-XXXXX.safetensors (size: 4.02 GB) Saving shard model-00002-of-XXXXX.safetensors (size: 4.29 GB)
Saving final shard model-00003-of-XXXXX.safetensors
300
--- 3. Finalizing Model Save --- ✅ Model slicing complete. New model saved in: my_modified_gemma_3n_model
README.md: 0%| | 0.00/23.4k [00:00<?, ?B/s]
Prepended custom description to the model card content. New README.md saved to 'my_modified_gemma_3n_model/README.md'
Push the model to Hugging Face
Loading checkpoint shards: 0%| | 0/3 [00:00<?, ?it/s]
WARNING:accelerate.big_modeling:Some parameters are on the meta device because they were offloaded to the cpu and disk.
Total Parameters: 5,976,833,408 Total Text Parameters: 4,456,156,768 Effective Parameters (excluding vision, audio, and Per-Layer-Embeddings): 1,905,495,648
Citation
@article{gemma_3n_2025,
title={Gemma 3n MatFormer Lab},
url={https://github.com/google-gemini/gemma-cookbook/blob/main/Gemma/%5BGemma_3n%5DMatFormer_Lab.ipynb},
publisher={Google DeepMind},
author={Puranjay Datta, Aditya Kusupati, Ryan Mullins, Omar Sanseviero, Rakesh Shivanna, Gemma Team},
year={2025}
}
Run in Google Colab