Implementation:Ollama Ollama ParseTensors
| Knowledge Sources | |
|---|---|
| Domains | Format_Conversion, Data_Processing |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Concrete tool for reading and name-mapping model tensors from SafeTensors files provided by the convert package.
Description
parseTensors dispatches to format-specific readers based on file extensions. parseSafetensors reads the SafeTensors header JSON, extracts tensor metadata, and creates lazy-loading tensor objects with memory-mapped data.
Architecture-specific Tensors methods (e.g., llamaModel.Tensors) perform additional transformations like Q/K weight repacking and tensor splitting/merging. The Replacements method provides the name mapping pairs.
Usage
Called internally by ConvertModel as part of the conversion pipeline.
Code Reference
Source Location
- Repository: ollama
- File: convert/reader.go (parseTensors), convert/reader_safetensors.go (parseSafetensors), convert/convert_llama.go (llamaModel.Tensors, Replacements)
- Lines: reader.go:L73-96, reader_safetensors.go:L26-210, convert_llama.go:L129-221
Signature
func parseTensors(fsys fs.FS, replacer *strings.Replacer) ([]Tensor, error)
func parseSafetensors(fsys fs.FS, replacer *strings.Replacer, ps ...string) ([]Tensor, error)
func (p *llamaModel) Tensors(ts []Tensor) []*ggml.Tensor
func (p *llamaModel) Replacements() []string
Import
import "github.com/ollama/ollama/convert"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| fsys | fs.FS | Yes | Filesystem with .safetensors or .bin files |
| replacer | *strings.Replacer | Yes | Name mapping rules from architecture converter |
Outputs
| Name | Type | Description |
|---|---|---|
| []Tensor | []Tensor | Parsed tensors with remapped names, shapes, dtypes, and lazy data readers |
| error | error | Non-nil if file parsing fails |
Usage Examples
Internal Usage
// From convert/convert.go
replacer := strings.NewReplacer(converter.Replacements()...)
tensors, err := parseTensors(fsys, replacer)
if err != nil {
return err
}
ggufTensors := converter.Tensors(tensors)