Repository: ollama/ollama
File: convert/convert_gemma3.go (Go)

Code before:
	Architecture string
	TextModel    struct {
		HeadDim          uint32 `json:"head_dim"`
		HiddenSize       uint32 `json:"hidden_size"`
		HiddenLayers     uint32 `json:"num_hidden_layers"`
		IntermediateSize uint32 `json:"intermediate_size"`
		SlidingWindow    uint32 `json:"sliding_window"`
	} `json:"text_config"`
	VisionModel struct {
		NumAttentionHeads uint32  `json:"num_attention_heads"` // attention.head_count 16
		LayerNormEpsilon  float32 `json:"layer_norm_eps"`      // attention.layer_norm_epsilon 1e-05
		NumHiddenLayers   uint32  `json:"num_hidden_layers"`   // block_count 32
		HiddenSize        uint32  `json:"hidden_size"`         // embedding_length 1280
		IntermediateSize  uint32  `json:"intermediate_size"`   // feed_forward_length 5120
		ImageSize         uint32  `json:"image_size"`          // image_size 560
		NumChannels       uint32  `json:"num_channels"`        // num_channels 3
		PatchSize         uint32  `json:"patch_size"`          // patch_size 14
	} `json:"vision_config"`
	MaxPositionEmbeddings    uint32  `json:"max_position_embeddings"`
	NumAttentionHeads        uint32  `json:"num_attention_heads"`
	NumKeyValueHeads         uint32  `json:"num_key_value_heads"`
	RMSNormEPS               float32 `json:"rms_norm_eps"`
	HeadDim                  uint32  `json:"head_dim"`
	FinalLogitSoftcap        float32 `json:"final_logit_softcapping"`
	RopeLocalTheta           float32 `json:"rope_local_base_freq"`
	RopeTheta                float32 `json:"rope_theta"`
	SlidingWindow            uint32  `json:"sliding_window"`
	SlidingWindowPattern     uint32  `json:"sliding_window_pattern"`
	MultiModalTokensPerImage uint32  `json:"mm_tokens_per_image"`
	RopeScaling              *struct {
		Type                          string  `json:"rope_type"`
		Factor                        float32 `json:"factor"`
		OriginalMaxPositionEmbeddings uint32  `json:"original_max_position_embeddings"`
		ExtrapolationFactor           float32 `json:"extrapolation_factor"`
		BetaFast                      float32 `json:"beta_fast"`
		BetaSlow                      float32 `json:"beta_slow"`
	} `json:"rope_scaling"`
}

const (
	gemma4BLayerCount  = 34
	gemma12BLayerCount = 48
	gemma27BLayerCount = 62
)

func (p *gemma3Model) KV(t *Tokenizer) ggml.KV {
	kv := p.ModelParameters.KV(t)
	kv["general.architecture"] = "gemma3"

	numBlocks := cmp.Or(p.HiddenLayers, p.TextModel.HiddenLayers)
	kv["gemma3.block_count"] = numBlocks

Review comment (suggestion):
I didn't see this `rope_global_base_freq` existing anywhere in a `config.json`. I'm not sure where it originated from

Code after:
	gemmaModel
	Architecture string
	TextModel    struct {
		HeadDim          uint32 `json:"head_dim"`
		HiddenSize       uint32 `json:"hidden_size"`
		HiddenLayers     uint32 `json:"num_hidden_layers"`
		IntermediateSize uint32 `json:"intermediate_size"`
		SlidingWindow    uint32 `json:"sliding_window"`
	} `json:"text_config"`
	VisionModel struct {
		NumAttentionHeads uint32  `json:"num_attention_heads"` // attention.head_count 16
		LayerNormEpsilon  float32 `json:"layer_norm_eps"`      // attention.layer_norm_epsilon 1e-05
		NumHiddenLayers   uint32  `json:"num_hidden_layers"`   // block_count 32
		HiddenSize        uint32  `json:"hidden_size"`         // embedding_length 1280
		IntermediateSize  uint32  `json:"intermediate_size"`   // feed_forward_length 5120
		ImageSize         uint32  `json:"image_size"`          // image_size 560
		NumChannels       uint32  `json:"num_channels"`        // num_channels 3
		PatchSize         uint32  `json:"patch_size"`          // patch_size 14
	} `json:"vision_config"`
	MaxPositionEmbeddings    uint32   `json:"max_position_embeddings"`
	NumAttentionHeads        uint32   `json:"num_attention_heads"`
	NumKeyValueHeads         uint32   `json:"num_key_value_heads"`
	RMSNormEPS               float32  `json:"rms_norm_eps"`
	HeadDim                  uint32   `json:"head_dim"`
	FinalLogitSoftcap        float32  `json:"final_logit_softcapping"`
	RopeLocalTheta           float32  `json:"rope_local_base_freq"`
	RopeTheta                float32  `json:"rope_theta"`
	SlidingWindow            uint32   `json:"sliding_window"`
	SlidingWindowPattern     *uint32  `json:"sliding_window_pattern"`
	LayerTypes               []string `json:"layer_types"`
	MultiModalTokensPerImage uint32   `json:"mm_tokens_per_image"`
	RopeScaling              *struct {
		Type                          string  `json:"rope_type"`
		Factor                        float32 `json:"factor"`
		OriginalMaxPositionEmbeddings uint32  `json:"original_max_position_embeddings"`
		ExtrapolationFactor           float32 `json:"extrapolation_factor"`
		BetaFast                      float32 `json:"beta_fast"`
		BetaSlow                      float32 `json:"beta_slow"`
	} `json:"rope_scaling"`
}

const (
	gemma4BLayerCount  = 34
	gemma12BLayerCount = 48
	gemma27BLayerCount = 62
)

func (p *gemma3Model) KV(t *Tokenizer) ggml.KV {
	kv := p.ModelParameters.KV(t)
	kv["general.architecture"] = "gemma3"