Implementation:Ollama Ollama ConvertAdapter
| Knowledge Sources | |
|---|---|
| Domains | Model_Architecture, Fine_Tuning |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Concrete tool for converting LoRA adapters from SafeTensors to GGUF format provided by the convert package.
Description
ConvertAdapter reads a LoRA adapter directory (containing adapter_config.json and safetensors files), detects the adapter architecture (LLaMA, Gemma2, etc.), converts tensor names and layouts to GGUF format, and writes the result as a GGUF file.
Architecture-specific converters like llamaAdapter handle name remapping (e.g., model.layers.*.self_attn.q_proj.lora_A to blk.*.attn_q.weight.loraA) and weight repacking (Q/K head interleaving for LLaMA models).
Usage
Called by CreateHandler when processing an ADAPTER directive during model creation.
Code Reference
Source Location
- Repository: ollama
- File: convert/convert.go (ConvertAdapter), convert/convert_llama_adapter.go (llamaAdapter)
- Lines: convert.go:L217-253 (ConvertAdapter), convert_llama_adapter.go:L22-170 (llamaAdapter)
Signature
func ConvertAdapter(fsys fs.FS, f *os.File, baseKV ofs.Config) error
func (p *llamaAdapter) KV(baseKV fs.Config) KV
func (p *llamaAdapter) Tensors(ts []Tensor) []*ggml.Tensor
func (p *llamaAdapter) Replacements() []string
Import
import "github.com/ollama/ollama/convert"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| fsys | fs.FS | Yes | Filesystem rooted at adapter directory (with adapter_config.json and .safetensors) |
| f | *os.File | Yes | Output file for GGUF-formatted adapter |
| baseKV | ofs.Config | Yes | Base model configuration (architecture, dimensions) needed for correct mapping |
Outputs
| Name | Type | Description |
|---|---|---|
| error | error | Non-nil if adapter format is unsupported or conversion fails |
| Side effect | GGUF file | Converted adapter written to output file |
Usage Examples
Internal Usage in CreateHandler
// From server/create.go
adapterFS := os.DirFS(adapterPath)
outFile, _ := os.CreateTemp("", "adapter-*.gguf")
err := convert.ConvertAdapter(adapterFS, outFile, baseModelConfig)
if err != nil {
return err
}
// Use the GGUF adapter file in the model manifest