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 Create Imagegen

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

Overview

Imports HuggingFace image generation models into Ollama's per-tensor blob storage format with optional quantization.

Description

The imagegen.go file in x/create implements CreateImageGenModel, which converts a HuggingFace diffusers model directory into Ollama's manifest format. It processes three components (text_encoder, transformer, vae), extracting individual tensors from each .safetensors file via TensorExtractor and storing them as per-tensor blobs in safetensors format (88 bytes header overhead) for native mmap loading. Quantization is supported for linear weights in transformer and text_encoder components (int4, int8, nvfp4, mxfp8) while VAE weights are kept in original precision. The function imports config files (model_index.json, tokenizer.json, component configs), normalizes model_index.json to Ollama format with pipeline class detection and total parameter counting, and writes the final manifest via callbacks. ShouldQuantize determines which tensors are eligible for quantization based on naming patterns.

Usage

Used by "ollama create" when importing an image generation model from a local HuggingFace diffusers directory.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/create/imagegen.go
  • Lines: 1-230

Signature

func CreateImageGenModel(
	modelName string,
	modelDir string,
	quantize string,
	createLayer LayerCreator,
	createTensorLayer QuantizingTensorLayerCreator,
	writeManifest ManifestWriter,
	fn func(status string),
) error

func ShouldQuantize(tensorName, component string) bool

Import

import "github.com/ollama/ollama/x/create"

I/O Contract

Inputs

Name Type Required Description
modelName string Yes Target Ollama model name
modelDir string Yes Path to HuggingFace diffusers model directory
quantize string No Quantization type: "", "int4", "int8", "nvfp4", "mxfp8"
createLayer LayerCreator Yes Callback to create config layers
createTensorLayer QuantizingTensorLayerCreator Yes Callback to create tensor layers (with optional quantization)
writeManifest ManifestWriter Yes Callback to write the final manifest
fn func(string) Yes Status callback for progress reporting

Outputs

Name Type Description
error error Error from tensor extraction, quantization, or manifest writing

Usage Examples

err := create.CreateImageGenModel(
    "my-flux-model",
    "/path/to/flux-klein",
    "int4", // quantize to 4-bit
    myLayerCreator,
    myTensorLayerCreator,
    myManifestWriter,
    func(status string) {
        fmt.Println(status)
    },
)

Related Pages

Page Connections

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