Implementation:Ollama Ollama XCreate
| Knowledge Sources | |
|---|---|
| Domains | Model Creation, Safetensors |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Core model creation logic for the experimental safetensors format, providing model type detection, manifest management, and the creation pipeline for both LLM and image generation models.
Description
Resolves model names to manifest paths using the registry.ollama.ai convention. Loads and parses manifests and model configs to detect model types (safetensors LLM vs. image gen) via capabilities. CreateSafetensorsModel iterates safetensors files, extracts individual tensors, optionally quantizes them, and creates blob layers via callback functions (LayerCreator, QuantizingTensorLayerCreator, ManifestWriter) to avoid import cycles. Supports architecture detection for parser/renderer selection, thinking capability detection, and expert group packing for MoE models.
Usage
Central package for the experimental safetensors model format, used by both client-side and server-side creation paths.
Code Reference
Source Location
- Repository: Ollama
- File: x/create/create.go
- Lines: 1-588
Signature
type ModelConfig struct {
ModelFormat string `json:"model_format"`
Capabilities []string `json:"capabilities"`
}
type LayerInfo struct {
Digest string
Size int64
MediaType string
Name string
}
type LayerCreator func(r io.Reader, mediaType, name string) (LayerInfo, error)
type QuantizingTensorLayerCreator func(r io.Reader, name, dtype string, shape []int32, quantize string) ([]LayerInfo, error)
type ManifestWriter func(layers []LayerInfo) error
func IsSafetensorsModel(modelName string) bool
func IsSafetensorsLLMModel(modelName string) bool
func IsImageGenModel(modelName string) bool
func IsSafetensorsModelDir(dir string) bool
func CreateSafetensorsModel(modelName, modelDir, quantize string, ...) error
Import
import "github.com/ollama/ollama/x/create"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| modelName | string | Yes | Target model name (e.g. "my-model:latest") |
| modelDir | string | Yes | Path to directory containing safetensors files |
| quantize | string | No | Quantization type: "int4", "int8", "nvfp4", or "mxfp8" |
Outputs
| Name | Type | Description |
|---|---|---|
| error | error | Non-nil if model creation fails |
Usage Examples
// Check if a model uses safetensors format
if create.IsSafetensorsLLMModel("my-model:latest") {
// Use MLX runner instead of llama.cpp
}
// Detect model directory type
if create.IsSafetensorsModelDir("/path/to/model") {
// Has config.json + .safetensors files
}