Implementation:Ollama Ollama Convert Gemma
| Knowledge Sources | |
|---|---|
| Domains | Model Conversion, GGUF Format |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the GGUF model converter for the Google Gemma (v1) architecture, applying the add-one normalization weight correction and emitting FIM (fill-in-middle) token IDs.
Description
The gemmaModel struct implements ModelConverter with KV metadata for Gemma-specific parameters including head dimensions and special FIM token IDs (EOT, middle, prefix, suffix). The Tensors method applies a custom repacker (addOne) to all _norm.weight tensors that adds 1.0 to every element, compensating for Gemma's convention of storing normalization weights as offsets from 1 rather than absolute values. This struct serves as the base type embedded by gemma3Model for reuse.
Usage
Invoked automatically when the model's architecture matches GemmaForCausalLM. Also serves as the base struct for Gemma 2 and Gemma 3 converters.
Code Reference
Source Location
- Repository: Ollama
- File: convert/convert_gemma.go
- Lines: 1-100
Signature
type gemmaModel struct {
ModelParameters
MaxPositionEmbeddings uint32 `json:"max_position_embeddings"`
HiddenSize uint32 `json:"hidden_size"`
HiddenLayers uint32 `json:"num_hidden_layers"`
IntermediateSize uint32 `json:"intermediate_size"`
NumAttentionHeads uint32 `json:"num_attention_heads"`
NumKeyValueHeads uint32 `json:"num_key_value_heads"`
RMSNormEPS float32 `json:"rms_norm_eps"`
HeadDim uint32 `json:"head_dim"`
}
func (p *gemmaModel) KV(t *Tokenizer) KV
func (p *gemmaModel) Tensors(ts []Tensor) []*ggml.Tensor
func (p *gemmaModel) Replacements() []string
Import
import "github.com/ollama/ollama/convert"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| t | *Tokenizer | Yes | Tokenizer data for GGUF metadata |
| ts | []Tensor | Yes | Source tensors, norm weights will be repacked with +1 |
Outputs
| Name | Type | Description |
|---|---|---|
| KV | KV | GGUF metadata with gemma.* keys and FIM token IDs |
| []*ggml.Tensor | slice | Converted tensors with add-one applied to norm weights |
Usage Examples
// Converter registered for GemmaForCausalLM
// m := &gemmaModel{}
// json.Unmarshal(configData, m)
// kv := m.KV(tokenizer)
// tensors := m.Tensors(sourceTensors)
// All _norm.weight tensors have 1.0 added to each element