Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Convert Bert

From Leeroopedia
Knowledge Sources
Domains Model Conversion, GGUF Format
Last Updated 2025-02-15 00:00 GMT

Overview

Implements the GGUF model converter for the BERT embedding model architecture, handling pooling configuration detection, WordPiece-to-phantom-space token conversion, and tensor filtering.

Description

The bertModel struct implements both the ModelConverter and moreParser interfaces. The parseMore method reads modules.json to detect sentence-transformer pooling mode (mean tokens vs. CLS token) and normalization settings. KV emits BERT-specific metadata including block count, context length, embedding dimensions, and attention head counts, handling multiple JSON field name variants via cmp.Or. It converts WordPiece tokenizer tokens to phantom-space format by replacing ## prefixes and prepending the Unicode block character. Tensors filters out non-GGUF tensors like embeddings.position_ids and pooler.dense.

Usage

Invoked automatically when the model's architecture matches BERT-family models. Enables importing sentence-transformer embedding models for semantic search and embeddings use cases.

Code Reference

Source Location

  • Repository: Ollama
  • File: convert/convert_bert.go
  • Lines: 1-178

Signature

type bertModel struct {
    ModelParameters
    NLayers               uint32  `json:"n_layers"`
    NumHiddenLayers       uint32  `json:"num_hidden_layers"`
    HiddenSize            uint32  `json:"hidden_size"`
    IntermediateSize      uint32  `json:"intermediate_size"`
    NumAttentionHeads     uint32  `json:"num_attention_heads"`
    LayerNormEPS          float32 `json:"layer_norm_eps"`
    PoolingType           uint32
}

func (p *bertModel) parseMore(fsys fs.FS) error
func (p *bertModel) KV(t *Tokenizer) KV
func (p *bertModel) Tensors(ts []Tensor) []*ggml.Tensor
func (bertModel) Replacements() []string

Import

import "github.com/ollama/ollama/convert"

I/O Contract

Inputs

Name Type Required Description
t *Tokenizer Yes Tokenizer with tokens to convert to phantom-space format
ts []Tensor Yes Source model tensors to convert and filter
fsys fs.FS Yes Filesystem for reading modules.json and pooling config

Outputs

Name Type Description
KV KV GGUF metadata with bert.* keys for architecture, pooling, and normalization
[]*ggml.Tensor slice Filtered tensors excluding position_ids and pooler.dense

Usage Examples

// Converter is registered for BERT architectures
// m := &bertModel{}
// json.Unmarshal(configData, m)
// m.parseMore(fsys)  // reads modules.json for pooling config
// kv := m.KV(tokenizer)  // emits bert.* metadata
// tensors := m.Tensors(sourceTensors)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment