gollama

package module
v0.0.0-...-9fe0e2e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 26 Imported by: 0

README

librallama.cpp

Go Reference Release

A high-performance Go binding for llama.cpp using purego and libffi for cross-platform compatibility without CGO.

Features

  • Pure Go: No CGO required, uses purego and libffi for C interop
  • Cross-Platform: Supports macOS (CPU/Metal), Linux (CPU/NVIDIA/AMD), Windows (CPU/NVIDIA/AMD)
  • Struct Support: Uses libffi for calling C functions with struct parameters/returns on all platforms
  • Performance: Direct bindings to llama.cpp shared libraries
  • Compatibility: Version-synchronized with llama.cpp releases
  • Easy Integration: Simple Go API for LLM inference
  • GPU Acceleration: Supports Metal, CUDA, HIP, Vulkan, OpenCL, SYCL, and other backends
  • Embedded Runtime Libraries: Optional go:embed bundle for all supported platforms
  • GGML Bindings: Low-level GGML tensor library bindings for advanced use cases

Used By

librallama.cpp is the official Go binding for llama.cpp in the openclaw and hermes memory ecosystems:

Supported Platforms

librallama.cpp uses a platform-specific architecture with build tags to ensure optimal compatibility and performance across all operating systems.

✅ Fully Supported Platforms
macOS
  • CPU: Intel x64, Apple Silicon (ARM64)
  • GPU: Metal (Apple Silicon)
  • Status: Full feature support with purego
  • Build Tags: Uses !windows build tag
Linux
  • CPU: x86_64, ARM64
  • GPU: NVIDIA (CUDA/Vulkan), AMD (HIP/ROCm/Vulkan), Intel (SYCL/Vulkan)
  • Status: Full feature support with purego and libffi
  • Build Tags: Uses !windows build tag
Windows
  • CPU: x86_64, ARM64
  • GPU: NVIDIA (CUDA/Vulkan), AMD (HIP/Vulkan), Intel (SYCL/Vulkan), Qualcomm Adreno (OpenCL)
  • Status: Full feature support with libffi
  • Build Tags: Uses windows build tag with syscall-based library loading
  • Current State:
    • ✅ Compiles without errors on Windows
    • ✅ Cross-compilation from other platforms works
    • ✅ Runtime functionality fully enabled via libffi and GetProcAddress
    • ✅ Full struct parameter/return support through function registration
    • 🚧 GPU acceleration being tested

Windows runtime notes

  • The loader now adds the DLL's directory to the Windows DLL search path and uses LoadLibraryExW with safe search flags to reliably resolve sibling dependencies (ggml, libomp, libcurl, etc.).
  • When a symbol isn't found in llama.dll, resolution automatically searches sibling DLLs from the same directory (e.g., ggml*.dll). This matches how upstream splits exports on Windows and fixes missing llama_backend_* on some builds.
  • If you see “The specified module could not be found.” while loading llama.dll, it often indicates a missing system runtime (e.g., Microsoft Visual C++ Redistributable 2015–2022). Installing the latest x64/x86 redistributable typically resolves it.
  • CI runners set PATH for later steps, but the downloader verifies loading immediately after download; the improved loader handles dependency resolution without relying on PATH.
Platform-Specific Implementation Details

Our platform abstraction layer uses Go build tags to provide:

  • Unix-like systems (!windows): Uses purego for dynamic library loading
  • Windows (windows): Uses native Windows syscalls (LoadLibraryW, FreeLibrary, GetProcAddress)
  • All platforms: Uses libffi for calling C functions with struct parameters/returns
  • Cross-compilation: Supports building for any platform from any platform
  • Automatic detection: Runtime platform capability detection

Installation

go get github.com/xDarkicex/librallama.cpp

The Go module automatically downloads pre-built llama.cpp libraries from the official ggml-org/llama.cpp releases on first use. No manual compilation required!

Embedding Libraries

For reproducible builds you can embed the pre-built libraries directly into the Go module. A helper Makefile target downloads the configured llama.cpp build (LLAMA_CPP_BUILD) for every supported platform and synchronises the ./libs directory which is picked up by go:embed:

# Download all platform builds for the configured llama.cpp version and populate ./libs
make populate-libs

# Alternatively, use the CLI directly
go run ./cmd/gollama-download -download-all -version b6862 -copy-libs

Only a single llama.cpp version is stored in ./libs at a time. Running populate-libs removes outdated directories automatically. Subsequent go build invocations embed the freshly synchronised libraries and LoadLibraryWithVersion("") will prefer the embedded bundle.

Cross-Platform Development

Build Compatibility Matrix

Our CI system tests compilation across all platforms:

Target Platform Build From Linux Build From macOS Build From Windows
Linux (amd64) ✅ ✅ ✅
Linux (arm64) ✅ ✅ ✅
macOS (amd64) ✅ ✅ ✅
macOS (arm64) ✅ ✅ ✅
Windows (amd64) ✅ ✅ ✅
Windows (arm64) ✅ ✅ ✅
Development Workflow
# Test cross-compilation for all platforms
make test-cross-compile

# Build for specific platform
GOOS=windows GOARCH=amd64 go build ./...
GOOS=linux GOARCH=arm64 go build ./...
GOOS=darwin GOARCH=arm64 go build ./...

# Run platform-specific tests
go test -v -run TestPlatformSpecific ./...

Quick Start

package main

import (
    "fmt"
    "log"

    "github.com/xDarkicex/librallama.cpp"
)

func main() {
    // Initialize the library
    gollama.Backend_init()
    defer gollama.Backend_free()

    // Load model
    params := gollama.Model_default_params()
    model, err := gollama.Model_load_from_file("path/to/model.gguf", params)
    if err != nil {
        log.Fatal(err)
    }
    defer gollama.Model_free(model)

    // Create context
    ctxParams := gollama.Context_default_params()
    ctx, err := gollama.Init_from_model(model, ctxParams)
    if err != nil {
        log.Fatal(err)
    }
    defer gollama.Free(ctx)

    // Tokenize and generate
    prompt := "The future of AI is"
    tokens, err := gollama.Tokenize(model, prompt, true, false)
    if err != nil {
        log.Fatal(err)
    }

    // Create batch and decode
    batch := gollama.Batch_init(len(tokens), 0, 1)
    defer gollama.Batch_free(batch)

    for i, token := range tokens {
        gollama.Batch_add(batch, token, int32(i), []int32{0}, false)
    }

    if err := gollama.Decode(ctx, batch); err != nil {
        log.Fatal(err)
    }

    // Sample next token
    logits := gollama.Get_logits_ith(ctx, -1)
    candidates := gollama.Token_data_array_init(model)
    
    sampler := gollama.Sampler_init_greedy()
    defer gollama.Sampler_free(sampler)
    
    newToken := gollama.Sampler_sample(sampler, ctx, candidates)
    
    // Convert token to text
    text := gollama.Token_to_piece(model, newToken, false)
    fmt.Printf("Generated: %s\n", text)
}

Advanced Usage

GGML Low-Level API

For advanced use cases, gollama.cpp provides direct access to GGML (the tensor library powering llama.cpp):

// Check GGML type information
typeSize, err := gollama.Ggml_type_size(gollama.GGML_TYPE_F32)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("F32 type size: %d bytes\n", typeSize)

// Check if a type is quantized
isQuantized, err := gollama.Ggml_type_is_quantized(gollama.GGML_TYPE_Q4_0)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Q4_0 is quantized: %v\n", isQuantized)

// Enumerate backend devices
devCount, err := gollama.Ggml_backend_dev_count()
if err == nil && devCount > 0 {
    for i := uint64(0); i < devCount; i++ {
        dev, _ := gollama.Ggml_backend_dev_get(i)
        name, _ := gollama.Ggml_backend_dev_name(dev)
        fmt.Printf("Device %d: %s\n", i, name)
    }
}

Supported GGML Features:

  • 31 tensor type definitions (F32, F16, Q4_0, Q8_0, BF16, etc.)
  • Type size and quantization utilities
  • Backend device enumeration and management
  • Buffer allocation and management
  • Type information queries

Note: GGML functions may not be exported in all llama.cpp builds. The library gracefully handles missing functions without errors.

GPU Configuration

librallama.cpp automatically downloads the appropriate pre-built binaries with GPU support and configures the optimal backend:

// Automatic GPU detection and configuration
params := gollama.Context_default_params()
params.n_gpu_layers = 32 // Offload layers to GPU (if available)

// Detect available GPU backend
backend := gollama.DetectGpuBackend()
fmt.Printf("Using GPU backend: %s\n", backend.String())

// Platform-specific optimizations:
// - macOS: Uses Metal when available  
// - Linux: Supports CUDA, HIP, Vulkan, and SYCL
// - Windows: Supports CUDA, HIP, Vulkan, OpenCL, and SYCL
params.split_mode = gollama.LLAMA_SPLIT_MODE_LAYER
GPU Support Matrix
Platform GPU Type Backend Status
macOS Apple Silicon Metal ✅ Supported
macOS Intel/AMD CPU only ✅ Supported
Linux NVIDIA CUDA ✅ Available in releases
Linux NVIDIA Vulkan ✅ Available in releases
Linux AMD HIP/ROCm ✅ Available in releases
Linux AMD Vulkan ✅ Available in releases
Linux Intel SYCL ✅ Available in releases
Linux Intel/Other Vulkan ✅ Available in releases
Linux Intel/Other CPU ✅ Fallback
Windows NVIDIA CUDA ✅ Available in releases
Windows NVIDIA Vulkan ✅ Available in releases
Windows AMD HIP ✅ Available in releases
Windows AMD Vulkan ✅ Available in releases
Windows Intel SYCL ✅ Available in releases
Windows Qualcomm Adreno OpenCL ✅ Available in releases
Windows Intel/Other Vulkan ✅ Available in releases
Windows Intel/Other CPU ✅ Fallback

The library automatically downloads pre-built binaries from the official llama.cpp releases with the appropriate GPU support for your platform. The download happens automatically on first use!

Model Loading Options
params := gollama.Model_default_params()
params.n_ctx = 4096           // Context size
params.use_mmap = true        // Memory mapping
params.use_mlock = true       // Memory locking
params.vocab_only = false     // Load full model
Library Management

librallama.cpp automatically downloads pre-built binaries from the official llama.cpp releases. You can also manage libraries manually:

// Load a specific version
err := gollama.LoadLibraryWithVersion("b6862")

// Clean cache to force re-download
err := gollama.CleanLibraryCache()
Command Line Tools
# Download libraries for current platform
make download-libs

# Download libraries for all platforms  
make download-libs-all

# Test download functionality
make test-download

# Test GPU detection and functionality
make test-gpu

# Detect available GPU backends
make detect-gpu

# Clean library cache
make clean-libs
Available Library Variants

The downloader automatically selects the best variant for your platform:

  • macOS: Metal-enabled binaries (arm64/x64)
  • Linux: CPU-optimized binaries (CUDA/HIP/Vulkan/SYCL versions available)
  • Windows: CPU-optimized binaries (CUDA/HIP/Vulkan/OpenCL/SYCL versions available)
Cache Location

Downloaded libraries are cached in platform-specific locations:

  • Linux/macOS: ~/.cache/gollama/libs/
  • Windows: %LOCALAPPDATA%/gollama/libs/

You can customize the cache directory in several ways:

Environment Variable:

export GOLLAMA_CACHE_DIR=/custom/path/to/cache

Configuration File:

{
  "cache_dir": "/custom/path/to/cache"
}

Programmatically:

config := gollama.DefaultConfig()
config.CacheDir = "/custom/path/to/cache"
gollama.SetGlobalConfig(config)

To get the current cache directory:

cacheDir, err := gollama.GetLibraryCacheDir()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Using cache directory: %s\n", cacheDir)

Building from Source

Prerequisites
  • Go 1.21 or later
  • Make
Quick Start
# Clone and build
git clone https://github.com/xDarkicex/librallama.cpp
cd gollama.cpp

# Build for current platform
make build

# Run tests (downloads libraries automatically)
make test

# Build examples
make build-examples

# Run tests
make test

# Generate release packages
make release

Running Tests

make test

Tests use github.com/stretchr/testify/suite along with a shared BaseSuite (see test_base_suite_test.go) that automatically snapshots/restores configuration and environment variables and unloads the llama library after each test. See the Contributing guide for details.

GPU Detection Logic

The Makefile implements intelligent GPU detection:

  1. CUDA Detection: Checks for nvcc compiler and CUDA toolkit
  2. HIP Detection: Checks for hipconfig and ROCm installation
  3. Priority Order: CUDA > HIP > CPU (on Linux/Windows)
  4. Metal: Always enabled on macOS when Xcode is available

No manual configuration or environment variables required!

Version Compatibility

This library tracks llama.cpp versions. The version number format is:

vX.Y.Z-llamacpp.ABCD

Where:

  • X.Y.Z is the gollama.cpp semantic version
  • ABCD is the corresponding llama.cpp build number

For example: v0.2.0-llamacpp.b6862 uses llama.cpp build b6862.

Documentation

Examples

See the examples directory for complete examples:

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

Funding

If you find this project helpful and would like to support its development, you can:

  • ⭐ Star this repository on GitHub
  • 🐛 Report bugs and suggest improvements
  • 📖 Improve documentation

GitHub Sponsors

Your support helps maintain and improve this project for the entire community!

License

This project is licensed under the MIT License - see the LICENSE file for details.

This license is compatible with llama.cpp's MIT license.

Acknowledgments

  • llama.cpp - The underlying C++ library
  • purego - Pure Go C interop library
  • ggml - Machine learning tensor library

Support

  • Issues - Bug reports and feature requests
  • Discussions - Questions and community support

Documentation

Overview

Package gollama provides Go bindings for ggml (the tensor library used by llama.cpp). This file contains bindings for the core GGML tensor operations and utilities.

GGML (Georgi Gerganov Machine Learning) is the tensor library that powers llama.cpp. It provides low-level operations for neural network computation.

Usage

Most users should use the high-level llama.cpp API in gollama.go. Use the GGML bindings when you need direct access to tensor operations, type information, backend management, or low-level memory operations.

Important Note

GGML functions may not be exported in all llama.cpp builds. This package gracefully handles missing functions by returning errors instead of panicking, allowing code to compile and run even when GGML symbols are not available.

Example Usage

// Initialize the library
gollama.Backend_init()
defer gollama.Backend_free()

// Query type information
size, err := gollama.Ggml_type_size(gollama.GGML_TYPE_F32)
if err != nil {
    log.Printf("GGML function not available: %v", err)
    return
}
fmt.Printf("F32 size: %d bytes\n", size)

// Check if a type is quantized
isQuantized, err := gollama.Ggml_type_is_quantized(gollama.GGML_TYPE_Q4_0)
if err == nil {
    fmt.Printf("Q4_0 is quantized: %v\n", isQuantized)
}

// Enumerate backend devices
count, err := gollama.Ggml_backend_dev_count()
if err == nil && count > 0 {
    for i := uint64(0); i < count; i++ {
        dev, _ := gollama.Ggml_backend_dev_get(i)
        name, _ := gollama.Ggml_backend_dev_name(dev)
        fmt.Printf("Device %d: %s\n", i, name)
    }
}

For more details, see the GGML API documentation at: https://github.com/xDarkicex/librallama.cpp/blob/main/docs/GGML_API.md

Package gollama provides Go bindings for llama.cpp using purego. This package allows you to use llama.cpp functionality from Go without CGO.

The bindings are designed to be as close to the original llama.cpp C API as possible, while providing Go-friendly interfaces where appropriate.

Example usage:

// Initialize the library
gollama.Backend_init()
defer gollama.Backend_free()

// Load a model
params := gollama.Model_default_params()
model, err := gollama.Model_load_from_file("model.gguf", params)
if err != nil {
	log.Fatal(err)
}
defer gollama.Model_free(model)

// Create context and generate text
ctxParams := gollama.Context_default_params()
ctx, err := gollama.Init_from_model(model, ctxParams)
if err != nil {
	log.Fatal(err)
}
defer gollama.Free(ctx)

Index

Constants

View Source
const (
	LLAMA_ERR_SUCCESS         = 0
	LLAMA_ERR_FAIL            = -1
	LLAMA_ERR_INVALID_PARAM   = -2
	LLAMA_ERR_OUT_OF_MEMORY   = -3
	LLAMA_ERR_FILE_NOT_FOUND  = -4
	LLAMA_ERR_FILE_READ       = -5
	LLAMA_ERR_FILE_WRITE      = -6
	LLAMA_ERR_INVALID_FORMAT  = -7
	LLAMA_ERR_UNSUPPORTED     = -8
	LLAMA_ERR_BACKEND_INIT    = -9
	LLAMA_ERR_CONTEXT_FULL    = -10
	LLAMA_ERR_TOKEN_INVALID   = -11
	LLAMA_ERR_MODEL_CORRUPTED = -12
	LLAMA_ERR_GPU_UNAVAILABLE = -13
)

Error codes matching llama.cpp return values

View Source
const (
	// Version is the gollama.cpp version
	Version = "0.2.3"
	// LlamaCppBuild is the llama.cpp build number this version is based on
	LlamaCppBuild = "b6862"
	// FullVersion combines both version numbers
	FullVersion = "v" + Version + "-llamacpp." + LlamaCppBuild
)

Version information

View Source
const (
	LLAMA_DEFAULT_SEED = 0xFFFFFFFF
	LLAMA_TOKEN_NULL   = -1

	// File magic numbers
	LLAMA_FILE_MAGIC_GGLA = 0x67676c61
	LLAMA_FILE_MAGIC_GGSN = 0x6767736e
	LLAMA_FILE_MAGIC_GGSQ = 0x67677371

	// Session constants
	LLAMA_SESSION_MAGIC   = LLAMA_FILE_MAGIC_GGSN
	LLAMA_SESSION_VERSION = 9

	LLAMA_STATE_SEQ_MAGIC   = LLAMA_FILE_MAGIC_GGSQ
	LLAMA_STATE_SEQ_VERSION = 2
)

Constants from llama.h

Variables

View Source
var (
	// Library errors
	ErrLibraryNotLoaded   = errors.New("llama.cpp library not loaded")
	ErrLibraryLoadFailed  = errors.New("failed to load llama.cpp library")
	ErrFunctionNotFound   = errors.New("function not found in library")
	ErrInvalidLibraryPath = errors.New("invalid library path")

	// Model errors
	ErrModelNotLoaded       = errors.New("model not loaded")
	ErrModelLoadFailed      = errors.New("failed to load model")
	ErrModelSaveFailed      = errors.New("failed to save model")
	ErrInvalidModelPath     = errors.New("invalid model path")
	ErrModelCorrupted       = errors.New("model file corrupted")
	ErrUnsupportedModelType = errors.New("unsupported model type")

	// Context errors
	ErrContextNotCreated     = errors.New("context not created")
	ErrContextCreationFailed = errors.New("failed to create context")
	ErrInvalidContextSize    = errors.New("invalid context size")
	ErrContextFull           = errors.New("context is full")

	// Token errors
	ErrTokenizationFailed = errors.New("tokenization failed")
	ErrInvalidToken       = errors.New("invalid token")
	ErrTokenOutOfRange    = errors.New("token out of vocabulary range")

	// Generation errors
	ErrGenerationFailed      = errors.New("text generation failed")
	ErrSamplingFailed        = errors.New("token sampling failed")
	ErrInvalidSamplingParams = errors.New("invalid sampling parameters")

	// Memory errors
	ErrOutOfMemory            = errors.New("out of memory")
	ErrMemoryAllocationFailed = errors.New("memory allocation failed")
	ErrInvalidMemorySize      = errors.New("invalid memory size")

	// Configuration errors
	ErrInvalidConfig          = errors.New("invalid configuration")
	ErrConfigValidationFailed = errors.New("configuration validation failed")
	ErrUnsupportedPlatform    = errors.New("unsupported platform")

	// Backend errors
	ErrBackendNotAvailable = errors.New("backend not available")
	ErrBackendInitFailed   = errors.New("backend initialization failed")
	ErrGPUNotAvailable     = errors.New("GPU not available")
	ErrCUDANotAvailable    = errors.New("CUDA not available")
	ErrMetalNotAvailable   = errors.New("metal backend not available")
	ErrVulkanNotAvailable  = errors.New("vulkan backend not available")

	// File I/O errors
	ErrFileNotFound      = errors.New("file not found")
	ErrFileReadFailed    = errors.New("failed to read file")
	ErrFileWriteFailed   = errors.New("failed to write file")
	ErrInvalidFileFormat = errors.New("invalid file format")

	// Parameter errors
	ErrInvalidParameter    = errors.New("invalid parameter")
	ErrParameterOutOfRange = errors.New("parameter out of range")
	ErrMissingParameter    = errors.New("missing required parameter")

	// Thread/concurrency errors
	ErrThreadingFailed      = errors.New("threading operation failed")
	ErrConcurrencyViolation = errors.New("concurrency violation")
	ErrDeadlock             = errors.New("deadlock detected")
)

Error types for different categories of errors

Functions

func ApplyConfig

func ApplyConfig(config *Config) error

ApplyConfig applies the configuration to the library

func Backend_free

func Backend_free()

Backend_free frees the llama + ggml backend

func Backend_init

func Backend_init() error

Backend_init initializes the llama + ggml backend

func BatchRegisterFunctions

func BatchRegisterFunctions(libHandle uintptr, functions []FunctionRegistration, tryMode bool) error

BatchRegisterFunctions is a convenience wrapper that registers functions in batch and returns true if all required functions were registered successfully

func Batch_free

func Batch_free(batch LlamaBatch)

Batch_free frees a batch

func CheckResult

func CheckResult(result int, function string) error

CheckResult checks a result code and returns an appropriate error

func CleanLibraryCache

func CleanLibraryCache() error

CleanLibraryCache removes cached library files to force re-download

func Cleanup

func Cleanup()

Cleanup function to be called when the program exits

func Decode

func Decode(ctx LlamaContext, batch LlamaBatch) error

Decode decodes a batch

func Encode

func Encode(ctx LlamaContext, batch LlamaBatch) error

Encode encodes a batch

func ErrorfromCode

func ErrorfromCode(code int) error

ErrorfromCode converts a llama.cpp error code to a Go error

func Free

func Free(ctx LlamaContext)

Free frees a context

func GetConfigPath

func GetConfigPath() string

GetConfigPath returns the default configuration file path

func GetLibraryCacheDir

func GetLibraryCacheDir() (string, error)

GetLibraryCacheDir returns the directory where downloaded libraries are cached

func GetSHA256ForFile

func GetSHA256ForFile(filepath string) (string, error)

GetSHA256ForFile calculates the SHA256 checksum for a given file

func Get_embeddings

func Get_embeddings(ctx LlamaContext) *float32

Get_embeddings returns the embeddings for the context

func Get_embeddings_ith

func Get_embeddings_ith(ctx LlamaContext, i int32) *float32

Get_embeddings_ith returns the embeddings for the ith sequence in the context

func Get_logits

func Get_logits(ctx LlamaContext) *float32

Get_logits gets logits for all tokens

func Get_logits_ith

func Get_logits_ith(ctx LlamaContext, i int32) *float32

Get_logits_ith gets logits for a specific token

func Ggml_backend_buffer_free

func Ggml_backend_buffer_free(buffer GgmlBackendBuffer) error

Ggml_backend_buffer_free frees a backend buffer

func Ggml_backend_buffer_get_size

func Ggml_backend_buffer_get_size(buffer GgmlBackendBuffer) (uint64, error)

Ggml_backend_buffer_get_size returns the size of a backend buffer

func Ggml_backend_buffer_is_host

func Ggml_backend_buffer_is_host(buffer GgmlBackendBuffer) (bool, error)

Ggml_backend_buffer_is_host checks if a buffer is host memory

func Ggml_backend_buffer_name

func Ggml_backend_buffer_name(buffer GgmlBackendBuffer) (string, error)

Ggml_backend_buffer_name returns the name of a backend buffer

func Ggml_backend_dev_count

func Ggml_backend_dev_count() (uint64, error)

Ggml_backend_dev_count returns the number of available backend devices

func Ggml_backend_dev_description

func Ggml_backend_dev_description(device GgmlBackendDevice) (string, error)

Ggml_backend_dev_description returns the description of a backend device

func Ggml_backend_dev_memory

func Ggml_backend_dev_memory(device GgmlBackendDevice) (free uint64, total uint64, err error)

Ggml_backend_dev_memory returns the memory statistics of a backend device

func Ggml_backend_dev_name

func Ggml_backend_dev_name(device GgmlBackendDevice) (string, error)

Ggml_backend_dev_name returns the name of a backend device

func Ggml_backend_free

func Ggml_backend_free(backend GgmlBackend) error

Ggml_backend_free frees a backend

func Ggml_backend_is_cpu

func Ggml_backend_is_cpu(backend GgmlBackend) (bool, error)

Ggml_backend_is_cpu checks if a backend is CPU-based Note: This function is not available in current GGML builds

func Ggml_backend_load_all

func Ggml_backend_load_all() error

Ggml_backend_load_all loads all available backends

func Ggml_backend_load_all_from_path

func Ggml_backend_load_all_from_path(path string) error

Ggml_backend_load_all_from_path loads all available backends from a specific path

func Ggml_backend_name

func Ggml_backend_name(backend GgmlBackend) (string, error)

Ggml_backend_name returns the name of a backend

func Ggml_backend_unload

func Ggml_backend_unload(reg GgmlBackendReg) error

Ggml_backend_unload unloads a dynamically loaded backend and unregisters it

func Ggml_blck_size

func Ggml_blck_size(typ GgmlType) (int32, error)

Ggml_blck_size returns the block size of a GGML type

func Ggml_type_is_quantized

func Ggml_type_is_quantized(typ GgmlType) (bool, error)

Ggml_type_is_quantized returns whether a GGML type is quantized

func Ggml_type_name

func Ggml_type_name(typ GgmlType) (string, error)

Ggml_type_name returns the string name of a GGML type

func Ggml_type_size

func Ggml_type_size(typ GgmlType) (uint64, error)

Ggml_type_size returns the size in bytes of a GGML type element

func Ggml_type_sizef

func Ggml_type_sizef(typ GgmlType) (uint64, error)

Ggml_type_sizef returns the size in bytes of a GGML type (float version)

func HandleError

func HandleError(err error, context string) error

HandleError is a convenience function that uses the global error handler

func IsFatalError

func IsFatalError(err error) bool

IsFatalError checks if an error is fatal and should stop execution

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError checks if an error is retryable

func LoadLibraryWithVersion

func LoadLibraryWithVersion(version string) error

LoadLibraryWithVersion loads a specific version of the llama.cpp library

func Max_devices

func Max_devices() uint64

Max_devices returns the maximum number of devices

func Memory_clear

func Memory_clear(ctx LlamaContext, reset bool) bool

Memory_clear clears the KV cache

func MergeVariantLibraries

func MergeVariantLibraries(goos, goarch, version, libsDir string, variantDirs []string) error

MergeVariantLibraries merges library files from multiple variant directories into the single target libs directory for the given platform and version. If two variants contain a library with the same file name but different content, it returns an error to prevent ambiguous or conflicting embeddings.

Target layout: <libsDir>/<goos>_<goarch>_<version>/ Copied files: *.dylib, *.so, *.dll (base name only; subdir structure is not preserved)

func Model_free

func Model_free(model LlamaModel)

Model_free frees a model

func Model_n_embd

func Model_n_embd(model LlamaModel) int32

Model_n_embd returns the number of embedding dimensions for the model

func Must

func Must(err error)

Must panics if the error is not nil - use only for initialization

func PopulateLibDirectoryFromResults

func PopulateLibDirectoryFromResults(results []DownloadResult, version, libsDir string) error

PopulateLibDirectoryFromResults copies downloaded library artifacts into the local libs directory so they can be embedded in future builds. Only the llama.cpp build defined by LlamaCppBuild is supported.

func Print_system_info() string

Print_system_info prints system information

func RegisterFunction

func RegisterFunction(fptr interface{}, name string) error

RegisterFunction registers a function with the global library handle

func RegisterFunctionSet

func RegisterFunctionSet(libHandle uintptr, functions []FunctionRegistration, tryMode bool) error

RegisterFunctions registers multiple function pointers with error handling If any required function fails to register and we're not in try mode, it returns an error This consolidates repeated registerLibFunc calls and reduces code duplication

func Sampler_free

func Sampler_free(sampler LlamaSampler)

Sampler_free frees a sampler

func SetGlobalConfig

func SetGlobalConfig(config *Config) error

SetGlobalConfig sets the global configuration

func SetGlobalErrorHandler

func SetGlobalErrorHandler(handler *ErrorHandler)

SetGlobalErrorHandler sets the global error handler

func Set_causal_attn

func Set_causal_attn(ctx LlamaContext, causal bool)

Set_causal_attn sets whether to use causal attention

func Set_embeddings

func Set_embeddings(ctx LlamaContext, embeddings bool)

Set_embeddings sets whether to extract embeddings

func Supports_gpu_offload

func Supports_gpu_offload() bool

Supports_gpu_offload returns whether GPU offload is supported

func Supports_mlock

func Supports_mlock() bool

Supports_mlock returns whether mlock is supported

func Supports_mmap

func Supports_mmap() bool

Supports_mmap returns whether mmap is supported

func Token_to_piece

func Token_to_piece(model LlamaModel, token LlamaToken, special bool) string

Token_to_piece converts a token to its string representation using model

func Try

func Try(fn func() error) (err error)

Try executes a function and handles any panics as errors

func WrapError

func WrapError(err error, message string) error

WrapError wraps an error with additional context

func WrapErrorf

func WrapErrorf(err error, format string, args ...interface{}) error

WrapErrorf wraps an error with formatted additional context

Types

type Config

type Config struct {
	// Library settings
	LibraryPath   string `json:"library_path,omitempty"`
	CacheDir      string `json:"cache_dir,omitempty"`
	UseEmbedded   bool   `json:"use_embedded"`
	EnableLogging bool   `json:"enable_logging"`
	LogLevel      int    `json:"log_level"`

	// Performance settings
	NumThreads    int  `json:"num_threads"`
	EnableGPU     bool `json:"enable_gpu"`
	GPULayers     int  `json:"gpu_layers"`
	MetalEnabled  bool `json:"metal_enabled"`
	CUDAEnabled   bool `json:"cuda_enabled"`
	VulkanEnabled bool `json:"vulkan_enabled"`

	// Memory settings
	ContextSize       int  `json:"context_size"`
	BatchSize         int  `json:"batch_size"`
	UbatchSize        int  `json:"ubatch_size"`
	MemoryMapEnabled  bool `json:"memory_map_enabled"`
	MemoryLockEnabled bool `json:"memory_lock_enabled"`

	// Model settings
	ModelPath        string `json:"model_path,omitempty"`
	VocabOnly        bool   `json:"vocab_only"`
	UseQuantization  bool   `json:"use_quantization"`
	QuantizationType string `json:"quantization_type,omitempty"`

	// Backend settings
	BackendType string `json:"backend_type,omitempty"`
	DeviceID    int    `json:"device_id"`

	// Debug settings
	VerboseLogging bool `json:"verbose_logging"`
	DebugMode      bool `json:"debug_mode"`
}

Config holds configuration options for gollama

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a configuration with sensible defaults

func GetGlobalConfig

func GetGlobalConfig() *Config

GetGlobalConfig returns the global configuration

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig loads configuration from a JSON file

func LoadConfigFromEnv

func LoadConfigFromEnv() *Config

LoadConfigFromEnv loads configuration from environment variables

func LoadDefaultConfig

func LoadDefaultConfig() *Config

LoadDefaultConfig loads configuration from the default locations

func (*Config) SaveConfig

func (c *Config) SaveConfig(path string) error

SaveConfig saves configuration to a JSON file

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration

type DownloadResult

type DownloadResult struct {
	Platform     string
	Success      bool
	Error        error
	LibraryPath  string
	SHA256Sum    string
	ExtractedDir string
	Embedded     bool
}

DownloadResult represents the result of a download task

func DownloadLibrariesForPlatforms

func DownloadLibrariesForPlatforms(platforms []string, version string) ([]DownloadResult, error)

DownloadLibrariesForPlatforms downloads libraries for multiple platforms in parallel platforms should be in the format []string{"linux/amd64", "darwin/arm64", "windows/amd64"} version can be empty for latest version or specify a specific version like "b6862"

type DownloadTask

type DownloadTask struct {
	Platform     string
	AssetName    string
	DownloadURL  string
	TargetDir    string
	ExpectedSHA2 string
	ResultIndex  int
}

DownloadTask represents a single download task for parallel processing

type ErrorCategory

type ErrorCategory int

ErrorCategory represents different categories of errors

const (
	CategoryLibrary ErrorCategory = iota
	CategoryModel
	CategoryContext
	CategoryToken
	CategoryGeneration
	CategoryMemory
	CategoryConfig
	CategoryBackend
	CategoryFile
	CategoryParameter
	CategoryThread
)

func CategorizeError

func CategorizeError(err error) ErrorCategory

CategorizeError determines the category of an error

func (ErrorCategory) String

func (ec ErrorCategory) String() string

String returns the string representation of an error category

type ErrorHandler

type ErrorHandler struct {
	// contains filtered or unexported fields
}

ErrorHandler provides centralized error handling and logging

func GetGlobalErrorHandler

func GetGlobalErrorHandler() *ErrorHandler

GetGlobalErrorHandler returns the global error handler

func NewErrorHandler

func NewErrorHandler(enableLogging bool) *ErrorHandler

NewErrorHandler creates a new error handler

func (*ErrorHandler) HandleError

func (eh *ErrorHandler) HandleError(err error, context string) error

HandleError processes and logs an error

func (*ErrorHandler) SetLogCallback

func (eh *ErrorHandler) SetLogCallback(callback func(level int, message string))

SetLogCallback sets the log callback function

type FunctionRegistration

type FunctionRegistration struct {
	FunctionPtr interface{} // Pointer to the function pointer variable
	Name        string      // C function name
	Required    bool        // Whether registration must succeed
	OnlyDarwin  bool        // Register only on Darwin platform
}

FunctionRegistration holds metadata for a single function to register

type GgmlBackend

type GgmlBackend uintptr

GGML backend types

func Ggml_backend_init_best

func Ggml_backend_init_best() (GgmlBackend, error)

Ggml_backend_init_best initializes the best available backend (GPU or CPU)

func Ggml_backend_init_by_name

func Ggml_backend_init_by_name(name string, params string) (GgmlBackend, error)

Ggml_backend_init_by_name initializes a backend by name with optional parameters

func Ggml_backend_init_by_type

func Ggml_backend_init_by_type(deviceType GgmlBackendDevType, params string) (GgmlBackend, error)

Ggml_backend_init_by_type initializes a backend by device type with optional parameters

type GgmlBackendBuffer

type GgmlBackendBuffer uintptr

type GgmlBackendBufferType

type GgmlBackendBufferType uintptr

func Ggml_backend_cpu_buffer_type

func Ggml_backend_cpu_buffer_type() (GgmlBackendBufferType, error)

Ggml_backend_cpu_buffer_type returns the CPU buffer type

type GgmlBackendDevCaps

type GgmlBackendDevCaps struct {
	Async             bool // asynchronous operations
	HostBuffer        bool // pinned host buffer
	BufferFromHostPtr bool // creating buffers from host ptr
	Events            bool // event synchronization
}

GGML backend device capabilities

type GgmlBackendDevProps

type GgmlBackendDevProps struct {
	Name        string             // device name
	Description string             // device description
	MemoryFree  uint64             // device free memory in bytes
	MemoryTotal uint64             // device total memory in bytes
	Type        GgmlBackendDevType // device type
	DeviceID    string             // device id (e.g., PCI bus id)
	Caps        GgmlBackendDevCaps // device capabilities
}

GGML backend device properties

type GgmlBackendDevType

type GgmlBackendDevType int32

GGML backend device types

const (
	GGML_BACKEND_DEVICE_TYPE_CPU   GgmlBackendDevType = 0
	GGML_BACKEND_DEVICE_TYPE_GPU   GgmlBackendDevType = 1
	GGML_BACKEND_DEVICE_TYPE_IGPU  GgmlBackendDevType = 2
	GGML_BACKEND_DEVICE_TYPE_ACCEL GgmlBackendDevType = 3
)

type GgmlBackendDevice

type GgmlBackendDevice uintptr

func Ggml_backend_dev_get

func Ggml_backend_dev_get(index uint64) (GgmlBackendDevice, error)

Ggml_backend_dev_get returns a backend device by index

type GgmlBackendReg

type GgmlBackendReg uintptr

func Ggml_backend_load

func Ggml_backend_load(path string) (GgmlBackendReg, error)

Ggml_backend_load dynamically loads a backend from a library path and returns a backend registry

type GgmlContext

type GgmlContext uintptr

GGML context type

type GgmlCplan

type GgmlCplan uintptr

GGML compute plan

type GgmlGuid

type GgmlGuid [16]byte // ggml_guid_t

type GgmlObject

type GgmlObject int32

GGML object type

const (
	GGML_OBJECT_TENSOR GgmlObject = 0
	GGML_OBJECT_GRAPH  GgmlObject = 1
	GGML_OBJECT_WORK   GgmlObject = 2
)

type GgmlOp

type GgmlOp int32

GGML operation types

const (
	GGML_OP_NONE GgmlOp = 0
	GGML_OP_DUP  GgmlOp = 1
	GGML_OP_ADD  GgmlOp = 2
	GGML_OP_SUB  GgmlOp = 3
	GGML_OP_MUL  GgmlOp = 4
	GGML_OP_DIV  GgmlOp = 5
)

type GgmlTensor

type GgmlTensor uintptr

GGML tensor type

type GgmlType

type GgmlType int32

GGML tensor types

const (
	GGML_TYPE_F32     GgmlType = 0
	GGML_TYPE_F16     GgmlType = 1
	GGML_TYPE_Q4_0    GgmlType = 2
	GGML_TYPE_Q4_1    GgmlType = 3
	GGML_TYPE_Q5_0    GgmlType = 6
	GGML_TYPE_Q5_1    GgmlType = 7
	GGML_TYPE_Q8_0    GgmlType = 8
	GGML_TYPE_Q8_1    GgmlType = 9
	GGML_TYPE_Q2_K    GgmlType = 10
	GGML_TYPE_Q3_K    GgmlType = 11
	GGML_TYPE_Q4_K    GgmlType = 12
	GGML_TYPE_Q5_K    GgmlType = 13
	GGML_TYPE_Q6_K    GgmlType = 14
	GGML_TYPE_Q8_K    GgmlType = 15
	GGML_TYPE_IQ2_XXS GgmlType = 16
	GGML_TYPE_IQ2_XS  GgmlType = 17
	GGML_TYPE_IQ3_XXS GgmlType = 18
	GGML_TYPE_IQ1_S   GgmlType = 19
	GGML_TYPE_IQ4_NL  GgmlType = 20
	GGML_TYPE_IQ3_S   GgmlType = 21
	GGML_TYPE_IQ2_S   GgmlType = 22
	GGML_TYPE_IQ4_XS  GgmlType = 23
	GGML_TYPE_I8      GgmlType = 24
	GGML_TYPE_I16     GgmlType = 25
	GGML_TYPE_I32     GgmlType = 26
	GGML_TYPE_I64     GgmlType = 27
	GGML_TYPE_F64     GgmlType = 28
	GGML_TYPE_IQ1_M   GgmlType = 29
	GGML_TYPE_BF16    GgmlType = 30
	GGML_TYPE_COUNT   GgmlType = 31
)

func (GgmlType) String

func (t GgmlType) String() string

String returns the string representation of a GGML type

type LibraryDownloader

type LibraryDownloader struct {
	// contains filtered or unexported fields
}

LibraryDownloader handles downloading pre-built llama.cpp binaries

func NewLibraryDownloader

func NewLibraryDownloader() (*LibraryDownloader, error)

NewLibraryDownloader creates a new library downloader instance

func NewLibraryDownloaderWithCacheDir

func NewLibraryDownloaderWithCacheDir(customCacheDir string) (*LibraryDownloader, error)

NewLibraryDownloaderWithCacheDir creates a new library downloader instance with a custom cache directory

func (*LibraryDownloader) CleanCache

func (d *LibraryDownloader) CleanCache() error

CleanCache removes old cached library files

func (*LibraryDownloader) DownloadAllVariants

func (d *LibraryDownloader) DownloadAllVariants(release *ReleaseInfo, goos, goarch string) (*VariantDownloadResult, error)

DownloadAllVariants downloads all variants for a platform and verifies common files are identical

func (*LibraryDownloader) DownloadAndExtract

func (d *LibraryDownloader) DownloadAndExtract(downloadURL, filename string) (string, error)

DownloadAndExtract downloads and extracts the library archive

func (*LibraryDownloader) DownloadAndExtractWithChecksum

func (d *LibraryDownloader) DownloadAndExtractWithChecksum(downloadURL, filename, expectedChecksum string) (string, string, error)

DownloadAndExtractWithChecksum downloads and extracts the library archive with checksum verification

func (*LibraryDownloader) DownloadMultiplePlatforms

func (d *LibraryDownloader) DownloadMultiplePlatforms(platforms []string, version string) ([]DownloadResult, error)

DownloadMultiplePlatforms downloads libraries for multiple platforms in parallel

func (*LibraryDownloader) FindAllVariantAssets

func (d *LibraryDownloader) FindAllVariantAssets(release *ReleaseInfo, goos, goarch string) ([]VariantAsset, error)

FindAllVariantAssets finds all variant assets for a specific platform and architecture Pattern: llama-<version>-bin-<os>-<variant>[-<variant-version>]-<arch>.zip

func (*LibraryDownloader) FindAssetByPattern

func (d *LibraryDownloader) FindAssetByPattern(release *ReleaseInfo, pattern string) (string, string, error)

FindAssetByPattern finds an asset that matches the given pattern

func (*LibraryDownloader) FindLibraryPath

func (d *LibraryDownloader) FindLibraryPath(extractedDir string) (string, error)

FindLibraryPath finds the main library file in the extracted directory

func (*LibraryDownloader) FindLibraryPathForPlatform

func (d *LibraryDownloader) FindLibraryPathForPlatform(extractedDir, goos string) (string, error)

FindLibraryPathForPlatform finds the main library file for a specific platform

func (*LibraryDownloader) GetCacheDir

func (d *LibraryDownloader) GetCacheDir() string

GetCacheDir returns the cache directory being used

func (*LibraryDownloader) GetLatestRelease

func (d *LibraryDownloader) GetLatestRelease() (*ReleaseInfo, error)

GetLatestRelease fetches the latest release information from GitHub

func (*LibraryDownloader) GetPlatformAssetPattern

func (d *LibraryDownloader) GetPlatformAssetPattern() (string, error)

GetPlatformAssetPattern returns the asset name pattern for the current platform

func (*LibraryDownloader) GetPlatformAssetPatternForPlatform

func (d *LibraryDownloader) GetPlatformAssetPatternForPlatform(goos, goarch string) (string, error)

GetPlatformAssetPatternForPlatform returns the asset name pattern for a specific platform

func (*LibraryDownloader) GetReleaseByTag

func (d *LibraryDownloader) GetReleaseByTag(tag string) (*ReleaseInfo, error)

GetReleaseByTag fetches release information for a specific tag

type LibraryLoadInfo

type LibraryLoadInfo struct {
	Success bool
	Handle  uintptr
	Path    string
	Error   string
}

LibraryLoadInfo consolidates the result of a library load attempt

type LibraryLoader

type LibraryLoader struct {
	// contains filtered or unexported fields
}

Library loader manages the loading and lifecycle of llama.cpp shared libraries

func (*LibraryLoader) ApplyLibraryLoad

func (l *LibraryLoader) ApplyLibraryLoad(info *LibraryLoadInfo, rootPath string) error

ApplyLibraryLoad applies the result of a successful library load to the loader state

func (*LibraryLoader) GetHandle

func (l *LibraryLoader) GetHandle() uintptr

GetHandle returns the library handle

func (*LibraryLoader) IsLoaded

func (l *LibraryLoader) IsLoaded() bool

IsLoaded returns whether the library is loaded

func (*LibraryLoader) LoadLibrary

func (l *LibraryLoader) LoadLibrary() error

LoadLibrary loads the appropriate llama.cpp library for the current platform

func (*LibraryLoader) LoadLibraryWithDependencies

func (l *LibraryLoader) LoadLibraryWithDependencies(libPath string) (*LibraryLoadInfo, []string)

LoadLibraryWithDependencies encapsulates the common pattern of: 1. Preloading dependent libraries 2. Loading the main library 3. Setting up configuration on success This is used in multiple places in LoadLibraryWithVersion to reduce duplication

func (*LibraryLoader) LoadLibraryWithVersion

func (l *LibraryLoader) LoadLibraryWithVersion(version string) error

LoadLibraryWithVersion loads the llama.cpp library for a specific version If version is empty, it loads the default build version (LlamaCppBuild) Resolution order: 1) Embedded (only if version == LlamaCppBuild) 2) Local ./libs (only if version == LlamaCppBuild) 3) Cache directory entries matching current GOOS (best-effort scan) 4) Download + extract to cache 5) Return a detailed error if all fail

func (*LibraryLoader) UnloadLibrary

func (l *LibraryLoader) UnloadLibrary() error

UnloadLibrary unloads the library and cleans up resources

type LlamaAdapterLora

type LlamaAdapterLora uintptr

type LlamaAttentionType

type LlamaAttentionType int32
const (
	LLAMA_ATTENTION_TYPE_CAUSAL     LlamaAttentionType = 0
	LLAMA_ATTENTION_TYPE_NON_CAUSAL LlamaAttentionType = 1
)

type LlamaBatch

type LlamaBatch struct {
	NTokens int32        // number of tokens
	Token   *LlamaToken  // tokens
	Embd    *float32     // embeddings (if using embeddings instead of tokens)
	Pos     *LlamaPos    // positions
	NSeqId  *int32       // number of sequence IDs per token
	SeqId   **LlamaSeqId // sequence IDs
	Logits  *int8        // whether to compute logits for each token
}

func Batch_get_one

func Batch_get_one(tokens []LlamaToken) LlamaBatch

Batch_get_one creates a batch from a single set of tokens

func Batch_init

func Batch_init(nTokens, embd, nSeqMax int32) LlamaBatch

Batch_init creates a new batch

type LlamaChatMessage

type LlamaChatMessage struct {
	Role    *byte // role string
	Content *byte // content string
}

Chat message

type LlamaContext

type LlamaContext uintptr

func Init_from_model

func Init_from_model(model LlamaModel, params LlamaContextParams) (LlamaContext, error)

Init_from_model creates a context from a model

type LlamaContextParams

type LlamaContextParams struct {
	NCtx              uint32               // text context, 0 = from model
	NBatch            uint32               // logical maximum batch size
	NUbatch           uint32               // physical maximum batch size
	NSeqMax           uint32               // max number of sequences
	NThreads          int32                // number of threads to use for generation
	NThreadsBatch     int32                // number of threads to use for batch processing
	RopeScalingType   LlamaRopeScalingType // RoPE scaling type
	PoolingType       LlamaPoolingType     // pooling type for embeddings
	AttentionType     LlamaAttentionType   // attention type
	FlashAttnType     LlamaFlashAttnType   // when to enable Flash Attention
	RopeFreqBase      float32              // RoPE base frequency
	RopeFreqScale     float32              // RoPE frequency scaling factor
	YarnExtFactor     float32              // YaRN extrapolation mix factor
	YarnAttnFactor    float32              // YaRN magnitude scaling factor
	YarnBetaFast      float32              // YaRN low correction dim
	YarnBetaSlow      float32              // YaRN high correction dim
	YarnOrigCtx       uint32               // YaRN original context size
	DefragThold       float32              // [DEPRECATED] defragment the KV cache if holes/size > thold
	CbEval            uintptr              // evaluation callback
	CbEvalUserData    uintptr              // user data for evaluation callback
	TypeK             int32                // data type for K cache
	TypeV             int32                // data type for V cache
	AbortCallback     uintptr              // abort callback
	AbortCallbackData uintptr              // user data for abort callback
	Embeddings        uint8                // whether to extract embeddings, together with logits (bool as uint8)
	Offload_kqv       uint8                // whether to offload KQV ops (incl. KV cache) to GPU (bool as uint8)
	NoPerf            uint8                // whether to skip performance timings (bool as uint8)
	OpOffload         uint8                // offload host tensor operations to device (bool as uint8)
	SwaFull           uint8                // use full-size SWA cache (bool as uint8)
	KvUnified         uint8                // use a unified KV buffer across input sequences (bool as uint8)
}

Context parameters

Layout MUST match struct llama_context_params in llama.h for the bundled llama.cpp build (b6862). The struct is passed/returned BY VALUE across the FFI boundary, so any drift silently lands fields on the wrong C offsets.

func ContextDefaultParams

func ContextDefaultParams() LlamaContextParams

func Context_default_params

func Context_default_params() LlamaContextParams

Context_default_params returns default context parameters

type LlamaError

type LlamaError struct {
	Code     int    `json:"code"`
	Message  string `json:"message"`
	Function string `json:"function,omitempty"`
	File     string `json:"file,omitempty"`
	Line     int    `json:"line,omitempty"`
	Cause    error  `json:"cause,omitempty"`
}

LlamaError represents a structured error from the llama.cpp library

func NewLlamaError

func NewLlamaError(code int, message string) *LlamaError

NewLlamaError creates a new LlamaError

func NewLlamaErrorWithCause

func NewLlamaErrorWithCause(code int, message string, cause error) *LlamaError

NewLlamaErrorWithCause creates a new LlamaError with an underlying cause

func NewLlamaErrorWithContext

func NewLlamaErrorWithContext(code int, message, function string) *LlamaError

NewLlamaErrorWithContext creates a new LlamaError with function context

func (*LlamaError) Error

func (e *LlamaError) Error() string

Error implements the error interface

func (*LlamaError) Is

func (e *LlamaError) Is(target error) bool

Is implements error matching

func (*LlamaError) Unwrap

func (e *LlamaError) Unwrap() error

Unwrap returns the underlying cause

type LlamaFlashAttnType

type LlamaFlashAttnType int32
const (
	LLAMA_FLASH_ATTN_TYPE_AUTO     LlamaFlashAttnType = -1
	LLAMA_FLASH_ATTN_TYPE_DISABLED LlamaFlashAttnType = 0
	LLAMA_FLASH_ATTN_TYPE_ENABLED  LlamaFlashAttnType = 1
)

type LlamaFtype

type LlamaFtype int32
const (
	LLAMA_FTYPE_ALL_F32        LlamaFtype = 0
	LLAMA_FTYPE_MOSTLY_F16     LlamaFtype = 1
	LLAMA_FTYPE_MOSTLY_Q4_0    LlamaFtype = 2
	LLAMA_FTYPE_MOSTLY_Q4_1    LlamaFtype = 3
	LLAMA_FTYPE_MOSTLY_Q8_0    LlamaFtype = 7
	LLAMA_FTYPE_MOSTLY_Q5_0    LlamaFtype = 8
	LLAMA_FTYPE_MOSTLY_Q5_1    LlamaFtype = 9
	LLAMA_FTYPE_MOSTLY_Q2_K    LlamaFtype = 10
	LLAMA_FTYPE_MOSTLY_Q3_K_S  LlamaFtype = 11
	LLAMA_FTYPE_MOSTLY_Q3_K_M  LlamaFtype = 12
	LLAMA_FTYPE_MOSTLY_Q3_K_L  LlamaFtype = 13
	LLAMA_FTYPE_MOSTLY_Q4_K_S  LlamaFtype = 14
	LLAMA_FTYPE_MOSTLY_Q4_K_M  LlamaFtype = 15
	LLAMA_FTYPE_MOSTLY_Q5_K_S  LlamaFtype = 16
	LLAMA_FTYPE_MOSTLY_Q5_K_M  LlamaFtype = 17
	LLAMA_FTYPE_MOSTLY_Q6_K    LlamaFtype = 18
	LLAMA_FTYPE_MOSTLY_IQ2_XXS LlamaFtype = 19
	LLAMA_FTYPE_MOSTLY_IQ2_XS  LlamaFtype = 20
	LLAMA_FTYPE_MOSTLY_Q2_K_S  LlamaFtype = 21
	LLAMA_FTYPE_MOSTLY_IQ3_XS  LlamaFtype = 22
)

type LlamaGpuBackend

type LlamaGpuBackend int32
const (
	LLAMA_GPU_BACKEND_NONE   LlamaGpuBackend = 0
	LLAMA_GPU_BACKEND_CPU    LlamaGpuBackend = 1
	LLAMA_GPU_BACKEND_CUDA   LlamaGpuBackend = 2
	LLAMA_GPU_BACKEND_METAL  LlamaGpuBackend = 3
	LLAMA_GPU_BACKEND_HIP    LlamaGpuBackend = 4
	LLAMA_GPU_BACKEND_VULKAN LlamaGpuBackend = 5
	LLAMA_GPU_BACKEND_OPENCL LlamaGpuBackend = 6
	LLAMA_GPU_BACKEND_SYCL   LlamaGpuBackend = 7
)

func DetectGpuBackend

func DetectGpuBackend() LlamaGpuBackend

DetectGpuBackend detects the available GPU backend on the current system

func (LlamaGpuBackend) String

func (b LlamaGpuBackend) String() string

String returns the string representation of the GPU backend

type LlamaLogitBias

type LlamaLogitBias struct {
	Token LlamaToken
	Bias  float32
}

Logit bias

type LlamaMemory

type LlamaMemory uintptr

Common types matching llama.cpp

func Get_memory

func Get_memory(ctx LlamaContext) LlamaMemory

Get_memory returns the memory handle for the context

type LlamaModel

type LlamaModel uintptr

Opaque types (represented as pointers)

func Model_load_from_file

func Model_load_from_file(pathModel string, params LlamaModelParams) (LlamaModel, error)

Model_load_from_file loads a model from a file

type LlamaModelParams

type LlamaModelParams struct {
	Devices                  uintptr        // ggml_backend_dev_t * - NULL-terminated list of devices
	TensorBuftOverrides      uintptr        // const struct llama_model_tensor_buft_override *
	NGpuLayers               int32          // number of layers to store in VRAM
	SplitMode                LlamaSplitMode // how to split the model across multiple GPUs
	MainGpu                  int32          // the GPU that is used for the entire model
	TensorSplit              *float32       // proportion of the model to offload to each GPU
	ProgressCallback         uintptr        // llama_progress_callback function pointer
	ProgressCallbackUserData uintptr        // context pointer passed to the progress callback
	KvOverrides              uintptr        // const struct llama_model_kv_override *
	VocabOnly                uint8          // only load the vocabulary, no weights (bool as uint8)
	UseMmap                  uint8          // use mmap if possible (bool as uint8)
	UseMlock                 uint8          // force system to keep model in RAM (bool as uint8)
	CheckTensors             uint8          // validate model tensor data (bool as uint8)
	UseExtraBufts            uint8          // use extra buffer types (bool as uint8)
	NoHost                   uint8          // bypass host buffer allowing extra buffers to be used (bool as uint8)
}

Model parameters

func ModelDefaultParams

func ModelDefaultParams() LlamaModelParams

Helper functions for platforms where struct returns aren't supported - moved to ROADMAP "wait for purego struct support" section

func Model_default_params

func Model_default_params() LlamaModelParams

Model_default_params returns default model parameters

type LlamaModelQuantizeParams

type LlamaModelQuantizeParams struct {
	NThread              int32      // number of threads to use for quantizing
	Ftype                LlamaFtype // quantize to this llama_ftype
	OutputTensorType     int32      // output tensor type
	TokenEmbeddingType   int32      // itoken embeddings tensor type
	AllowRequantize      uint8      // allow quantizing non-f32/f16 tensors (bool as uint8)
	QuantizeOutputTensor uint8      // quantize output.weight (bool as uint8)
	OnlyF32              uint8      // quantize only f32 tensors (bool as uint8)
	PureF16              uint8      // disable k-quant mixtures and quantize all tensors to the same type (bool as uint8)
	KeepSplit            uint8      // keep split tensors (bool as uint8)
	IMatrix              *byte      // importance matrix data
	KqsWarning           uint8      // warning for quantization quality loss (bool as uint8)
}

Model quantize parameters

type LlamaPoolingType

type LlamaPoolingType int32
const (
	LLAMA_POOLING_TYPE_UNSPECIFIED LlamaPoolingType = -1
	LLAMA_POOLING_TYPE_NONE        LlamaPoolingType = 0
	LLAMA_POOLING_TYPE_MEAN        LlamaPoolingType = 1
	LLAMA_POOLING_TYPE_CLS         LlamaPoolingType = 2
	LLAMA_POOLING_TYPE_LAST        LlamaPoolingType = 3
	LLAMA_POOLING_TYPE_RANK        LlamaPoolingType = 4
)

type LlamaPos

type LlamaPos int32

Common types matching llama.cpp

type LlamaRopeScalingType

type LlamaRopeScalingType int32
const (
	LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED LlamaRopeScalingType = -1
	LLAMA_ROPE_SCALING_TYPE_NONE        LlamaRopeScalingType = 0
	LLAMA_ROPE_SCALING_TYPE_LINEAR      LlamaRopeScalingType = 1
	LLAMA_ROPE_SCALING_TYPE_YARN        LlamaRopeScalingType = 2
)

type LlamaSampler

type LlamaSampler uintptr

func Sampler_chain_init

func Sampler_chain_init(params LlamaSamplerChainParams) LlamaSampler

Sampler_chain_init creates a sampler chain

func Sampler_init_greedy

func Sampler_init_greedy() LlamaSampler

Sampler_init_greedy creates a greedy sampler

type LlamaSamplerChainParams

type LlamaSamplerChainParams struct {
	NoPerf uint8 // whether to measure performance timings (bool as uint8)
}

Sampler chain parameters

func SamplerChainDefaultParams

func SamplerChainDefaultParams() LlamaSamplerChainParams

func Sampler_chain_default_params

func Sampler_chain_default_params() LlamaSamplerChainParams

Sampler_chain_default_params returns default sampler chain parameters

type LlamaSeqId

type LlamaSeqId int32

Common types matching llama.cpp

type LlamaSplitMode

type LlamaSplitMode int32
const (
	LLAMA_SPLIT_MODE_NONE  LlamaSplitMode = 0
	LLAMA_SPLIT_MODE_LAYER LlamaSplitMode = 1
	LLAMA_SPLIT_MODE_ROW   LlamaSplitMode = 2
)

type LlamaToken

type LlamaToken int32

Common types matching llama.cpp

func Sampler_sample

func Sampler_sample(sampler LlamaSampler, ctx LlamaContext, idx int32) LlamaToken

Sampler_sample samples a token from the logits at the given index (-1 for last token)

func Tokenize

func Tokenize(model LlamaModel, text string, addSpecial, parseSpecial bool) ([]LlamaToken, error)

Tokenize tokenizes text

type LlamaTokenAttr

type LlamaTokenAttr int32
const (
	LLAMA_TOKEN_ATTR_UNDEFINED   LlamaTokenAttr = 0
	LLAMA_TOKEN_ATTR_UNKNOWN     LlamaTokenAttr = 1 << 0
	LLAMA_TOKEN_ATTR_UNUSED      LlamaTokenAttr = 1 << 1
	LLAMA_TOKEN_ATTR_NORMAL      LlamaTokenAttr = 1 << 2
	LLAMA_TOKEN_ATTR_CONTROL     LlamaTokenAttr = 1 << 3
	LLAMA_TOKEN_ATTR_USER_DEF    LlamaTokenAttr = 1 << 4
	LLAMA_TOKEN_ATTR_BYTE        LlamaTokenAttr = 1 << 5
	LLAMA_TOKEN_ATTR_LSTRIP      LlamaTokenAttr = 1 << 6
	LLAMA_TOKEN_ATTR_RSTRIP      LlamaTokenAttr = 1 << 7
	LLAMA_TOKEN_ATTR_SINGLE_WORD LlamaTokenAttr = 1 << 8
)

type LlamaTokenData

type LlamaTokenData struct {
	Id    LlamaToken // token id
	Logit float32    // log-odds of the token
	P     float32    // probability of the token
}

Structs

type LlamaTokenDataArray

type LlamaTokenDataArray struct {
	Data     *LlamaTokenData // pointer to token data array
	Size     uint64          // number of tokens
	Selected int64           // index of selected token (-1 if none)
	Sorted   uint8           // whether the array is sorted by probability (bool as uint8)
}

func Token_data_array_from_logits

func Token_data_array_from_logits(model LlamaModel, logits *float32) *LlamaTokenDataArray

Token_data_array_from_logits creates a token data array from logits

func Token_data_array_init

func Token_data_array_init(model LlamaModel) *LlamaTokenDataArray

Token_data_array_init creates a token data array (helper function)

type LlamaTokenType

type LlamaTokenType int32
const (
	LLAMA_TOKEN_TYPE_UNDEFINED LlamaTokenType = iota
	LLAMA_TOKEN_TYPE_NORMAL
	LLAMA_TOKEN_TYPE_UNKNOWN
	LLAMA_TOKEN_TYPE_CONTROL
	LLAMA_TOKEN_TYPE_USER_DEFINED
	LLAMA_TOKEN_TYPE_UNUSED
	LLAMA_TOKEN_TYPE_BYTE
)

type LlamaVocab

type LlamaVocab uintptr

type LlamaVocabType

type LlamaVocabType int32

Enums

const (
	LLAMA_VOCAB_TYPE_NONE LlamaVocabType = iota
	LLAMA_VOCAB_TYPE_SPM
	LLAMA_VOCAB_TYPE_BPE
	LLAMA_VOCAB_TYPE_WPM
	LLAMA_VOCAB_TYPE_UGM
	LLAMA_VOCAB_TYPE_RWKV
)

type ReleaseInfo

type ReleaseInfo = github.RepositoryRelease

ReleaseInfo represents GitHub release information Using go-github's Release type directly

type VariantAsset

type VariantAsset struct {
	AssetName   string
	DownloadURL string
	Variant     string // e.g., "cpu", "cuda-12.6.0", "vulkan", "hip-6.2"
}

VariantAsset represents a single variant asset for a platform

type VariantDownloadResult

type VariantDownloadResult struct {
	Platform      string
	Variants      []VariantInfo
	Success       bool
	Error         error
	CommonLibPath string // Path to verified common library files
}

VariantDownloadResult represents the result of downloading all variants for a platform

type VariantInfo

type VariantInfo struct {
	Variant      string
	ExtractedDir string
	SHA256Sum    string
	Success      bool
	Error        error
}

VariantInfo contains information about a downloaded variant

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL