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 Imagegen Manifest Weights

From Leeroopedia
Knowledge Sources
Domains Image Generation, Model Loading
Last Updated 2025-02-15 00:00 GMT

Overview

Provides zero-copy weight loading from Ollama's manifest blob storage using native MLX safetensors mmap.

Description

The weights.go file implements ManifestWeights for loading model tensors from Ollama's content-addressed blob storage. Each tensor is stored as an individual safetensors blob, loaded via native MLX mmap (mlx_load_safetensors) for zero-copy GPU access. The Load method groups tensors by digest to avoid loading the same blob multiple times, handles component prefix stripping (e.g., "text_encoder/model.embed_tokens.weight" becomes "model.embed_tokens.weight"), and detects quantization metadata (quant_type, group_size) from blob headers. It supports three tensor formats: single-tensor blobs (keyed by name or legacy "data"), combined quantized blobs (weight + scale + bias), and packed blobs for expert groups containing multiple tensors.

Usage

Used by all image generation model loaders (Flux2, ZImage, text encoders) to load weights from Ollama's blob storage with zero-copy GPU mapping.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/imagegen/manifest/weights.go
  • Lines: 1-298

Signature

type ManifestWeights struct {
	manifest    *ModelManifest
	component   string
	tensors     map[string]ManifestLayer
	cache       map[string]*mlx.Array
	nativeCache []*mlx.SafetensorsFile
	quantType   string
	groupSize   int
}

func LoadWeightsFromManifest(manifest *ModelManifest, component string) (*ManifestWeights, error)
func (mw *ManifestWeights) Load(dtype mlx.Dtype) error
func (mw *ManifestWeights) Get(name string) *mlx.Array
func (mw *ManifestWeights) QuantType() string
func (mw *ManifestWeights) GroupSize() int

Import

import "github.com/ollama/ollama/x/imagegen/manifest"

I/O Contract

Inputs

Name Type Required Description
manifest *ModelManifest Yes Loaded model manifest
component string No Component filter (e.g., "transformer", "" for all)
dtype mlx.Dtype No Target dtype for non-quantized tensors (0 = original)

Outputs

Name Type Description
*ManifestWeights *ManifestWeights Weight loader with zero-copy mmap tensors
error error Error if no tensors found or blob loading fails

Usage Examples

manifest, _ := manifest.LoadManifest("flux2-klein:latest")

weights, err := manifest.LoadWeightsFromManifest(manifest, "transformer")
if err != nil {
    return err
}

if err := weights.Load(mlx.DtypeBFloat16); err != nil {
    return err
}

attnWeight := weights.Get("layers.0.attn.q_proj.weight")

Related Pages

Page Connections

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