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:Ggml org Ggml Backend impl interface

From Leeroopedia


Implementation Metadata
File Name src/ggml-backend-impl.h
Repository ggml-org/ggml
Lines 255
Language C
Domain Tags ML_Infrastructure, Hardware_Abstraction, API_Design
Status Active
Last Updated 2025-05-15 12:00 GMT
Knowledge Sources ggml-org/ggml repository

Overview

src/ggml-backend-impl.h is the internal header defining the vtable-style interface structs that backend implementations must fulfill to integrate with GGML's backend system. This is the contract that every hardware backend (CPU, CUDA, Metal, Vulkan, Hexagon, WebGPU, etc.) must implement, enabling a clean plugin architecture where new backends can be added without modifying core GGML code.

Description

The file defines a four-level abstraction hierarchy through C function-pointer structs:

  1. ggml_backend_buffer_type_i -- Buffer type interface: get_name, alloc_buffer, get_alignment, get_max_size, get_alloc_size, is_host
  2. ggml_backend_buffer_i -- Buffer instance interface: free_buffer, get_base, init_tensor, memset_tensor, set_tensor, get_tensor, cpy_tensor, clear, reset
  3. ggml_backend_i -- Backend (stream) interface: get_name, free, set_tensor_async, get_tensor_async, cpy_tensor_async, synchronize, graph_compute
  4. ggml_backend_device_i -- Device interface: capability queries, memory reporting, backend initialization, operation support checks

Also defines:

  • ggml_backend_reg_i -- Backend registration with device enumeration
  • GGML_BACKEND_API_VERSION = 2 -- ABI compatibility checking
  • GGML_BACKEND_DL_IMPL / GGML_BACKEND_DL_SCORE_IMPL -- Macros for dynamic loading entry points

Usage

Backend implementors include this header and fill in the function pointer structs:

#include "ggml-backend-impl.h"

static struct ggml_backend_buffer_type_i my_buffer_type_iface = {
    .get_name      = my_buft_get_name,
    .alloc_buffer  = my_buft_alloc_buffer,
    .get_alignment = my_buft_get_alignment,
    // ...
};

Code Reference

Source Location

Repository File Lines
ggml-org/ggml src/ggml-backend-impl.h 255

Key Signatures

#define GGML_BACKEND_API_VERSION 2

struct ggml_backend_buffer_type_i {
    const char *          (*get_name)(ggml_backend_buffer_type_t buft);
    ggml_backend_buffer_t (*alloc_buffer)(ggml_backend_buffer_type_t buft, size_t size);
    size_t                (*get_alignment)(ggml_backend_buffer_type_t buft);
    size_t                (*get_max_size)(ggml_backend_buffer_type_t buft);
    size_t                (*get_alloc_size)(ggml_backend_buffer_type_t buft, const struct ggml_tensor * tensor);
    bool                  (*is_host)(ggml_backend_buffer_type_t buft);
};

struct ggml_backend_buffer_i {
    void         (*free_buffer)(ggml_backend_buffer_t buffer);
    void *       (*get_base)(ggml_backend_buffer_t buffer);
    enum ggml_status (*init_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
    void         (*set_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor,
                     const void * data, size_t offset, size_t size);
    void         (*get_tensor)(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor,
                     void * data, size_t offset, size_t size);
    bool         (*cpy_tensor)(ggml_backend_buffer_t buffer, const struct ggml_tensor * src,
                     struct ggml_tensor * dst);
    void         (*clear)(ggml_backend_buffer_t buffer, uint8_t value);
};

struct ggml_backend_i {
    const char * (*get_name)(ggml_backend_t backend);
    void         (*free)(ggml_backend_t backend);
    enum ggml_status (*graph_compute)(ggml_backend_t backend, struct ggml_cgraph * cgraph);
};

GGML_API ggml_backend_buffer_t ggml_backend_buffer_init(
    ggml_backend_buffer_type_t buft, struct ggml_backend_buffer_i iface,
    void * context, size_t size);

I/O Contract

Inputs

  • Interface structs -- Function pointer tables filled in by backend implementors
  • Context pointers -- Backend-specific context data attached to each struct

Outputs

  • Registered backend -- A complete backend usable through the public GGML API
  • ABI compatibility -- Version checked at load time via GGML_BACKEND_API_VERSION

Usage Examples

Implementing a minimal custom backend:

#include "ggml-backend-impl.h"

// 1. Define buffer type interface
static struct ggml_backend_buffer_type_i my_buft_iface = {
    .get_name      = my_get_name,
    .alloc_buffer  = my_alloc_buffer,
    .get_alignment = my_get_alignment,
};

// 2. Define buffer interface
static struct ggml_backend_buffer_i my_buf_iface = {
    .free_buffer = my_free_buffer,
    .get_base    = my_get_base,
    .set_tensor  = my_set_tensor,
    .get_tensor  = my_get_tensor,
    .clear       = my_clear,
};

// 3. Define backend interface
static struct ggml_backend_i my_backend_iface = {
    .get_name      = my_backend_name,
    .free          = my_backend_free,
    .graph_compute = my_graph_compute,
};

// 4. For dynamic loading
GGML_BACKEND_DL_IMPL(my_backend_reg)
GGML_BACKEND_DL_SCORE_IMPL(my_backend_score)

Related Pages

Implements Principle

Related Implementations

Page Connections

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