# Overview of Gemma series > 系统梳理 Google Gemma 系列开源模型(Gemma 3、Gemma 4)的技术演进,重点解析滑动窗口注意力配比、多模态图像切分与编码处理、数据格式规范及模型架构设计细节。 - **URL**: https://maosong.website/blog/gemma/ - **Authors**: Mao Song - **Published**: 2025-03-15 11:15:29+0800 - **Modified**: 2025-03-15 11:15:29+0800 --- ## Gemma 3 作者提出了Gemma3系列大模型 [@Gemma3],包括 1B, 4B, 12B, 27B 四个 size. 4B, 12B, 27B 三个 size 均支持多模态,128K 的上下文长度以及 140 种语言。 ### 数据格式 ``` [BOS]user Who are you? model My name is Gemma! user What is 2+2? model 2+2=4. ``` pre-train 和 SFT 的区别在于,pre-train 时模型输出以 `` 结束,SFT 时模型输出以 `` 结束。 ### 图片处理 输入的图片都会被 resize 到 $896\times 896$, 如果图片精度过大或者不是正方形,则会通过 Pan & Scan 技巧裁剪为多个子图,然后每个子图分别进行 resize. 核心处理代码为: ```python crop_size_w = int(math.ceil(width / num_crops_w)) crop_size_h = int(math.ceil(height / num_crops_h)) # Don't apply PaS if crop size is too small. if min(crop_size_w, crop_size_h) 与 PaliGemma 2 的表现对比 ### 消融实验 **Local attention layers** 1. `sliding_window_pattern` 对模型的表现影响不大 2. sliding window size 对模型的表现也不是很大,如下图 3. 使用 sliding window attention 可以降低 KV cache 的内存占用 **Long context ablation** 结论为 long context 会降低模型的性能 **知识蒸馏** 训练 token 个数比较少的时候,使用小的教师模型效果更好;训练 token 个数比较多的时候,使用大的教师模型效果更好。 **Pan & Scan** 使用图片原始的aspect ratio训练的模型效果更好。 **memorization** memorization指模型输出的文本与训练数据中文本的重复率。结果发现Gemma3的memorization要更低一些。 ## Gemma 4 | Feature | E2B | E4B | 26B 4A | 31B | | :-------------- | :------------------ | :------------------ | :-------------- | :----------- | | Architecture | Dense | Dense | MoE (4B Active) | Dense | | Context Window | 128K | 128K | 256K | 256K | | Modalities | Text, Vision, Audio | Text, Vision, Audio | Text, Vision | Text, Vision | | Target Hardware | Pixel & Qualcomm | Pixel & Qualcomm | MacBook / Cloud | V100 32GB | Gemma 4 31B: - architecture: wide dense model with 60 layers - benchmark: #3 spot on the global Arena AI leaderboard - context: 256k context length, 1024 sliding window size, 5:1 local global layer ratio - agentic foundation: thinking mode, function calling, structured json output Gemma 4 26B A4B - expert mix: 128 total experts, 1 shared expert, 8 active experts - active parameters: 3.8b parameters - context: 256k context length, 1024 sliding window size, 5:1 local global layer ratio - kv cache reuse: shared kv cache across 8 layers Gemma 4 E4B - PLE integration: per layer embedding - multimodal native: audio, vision, Textcontext: 128k context length, 512 sliding window size ### architecture - 5:1 ratio of local to global layers (4:1 for E2B models) - sliding window attention ### multimodal - 550M ViT for 31B/26B models - 150M ViT for E2B/E4B models variable aspect ratios and variable resolution in images: 70, 140, 280, 560, 1120 soft token budgets audio - 305M audio encoder: audio conformer -