Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Ggml org Ggml Ggml backend load all

From Leeroopedia


Implementation Metadata
Function Name ggml_backend_load_all
Repository ggml-org/ggml
Source File src/ggml-backend-reg.cpp
Lines L535-537 (delegates to ggml_backend_load_all_from_path(nullptr) at L539-565)
Language C (with C++ implementation)
Domain Tags ML_Infrastructure, Hardware_Abstraction
Status Active
Last Updated 2025-05-15 12:00 GMT
Knowledge Sources ggml-org/ggml repository

Overview

ggml_backend_load_all is the primary entry point for discovering and loading all available GGML backend plugins at runtime. It takes no parameters and scans the filesystem for backend shared libraries, registering each one it finds into a global backend registry. This function is the concrete realization of the Backend Plugin Loading principle within the GGML framework.

Description

The function is a thin wrapper that delegates to ggml_backend_load_all_from_path(nullptr). When called with a nullptr path, the underlying implementation searches the default locations: the directory specified by the GGML_BACKEND_DIR compile-time macro (if defined), the executable's directory, and the current working directory.

For each backend name in a fixed ordered list, the function calls the internal ggml_backend_load_best helper, which:

  1. Enumerates files matching the pattern [lib]ggml-{name}-*.[so|dll] in the search paths.
  2. Temporarily loads each matching file using dlopen (POSIX) or LoadLibraryW (Windows).
  3. Calls the ggml_backend_score exported symbol to rank candidates.
  4. Selects the highest-scoring variant and fully loads it via ggml_backend_init.
  5. Verifies API version compatibility (GGML_BACKEND_API_VERSION, currently 2).
  6. Registers the backend and its devices into the global ggml_backend_registry singleton.

The backends are loaded in the following fixed order: blas, zendnn, cann, cuda, hip, metal, rpc, sycl, vulkan, virtgpu, opencl, hexagon, musa, cpu. After processing this list, the function also checks the GGML_BACKEND_PATH environment variable to load a single out-of-tree backend if specified.

Usage

This function is typically called once at application startup before any backend-dependent operations. It is used extensively in GGML examples and tests:

#include "ggml-backend.h"

int main(void) {
    ggml_backend_load_all();
    // All discoverable backends are now registered.
    // Proceed with backend selection and computation.
    ggml_backend_t backend = ggml_backend_init_best();
    // ...
}

Code Reference

Source Location

Repository File Lines
ggml-org/ggml src/ggml-backend-reg.cpp L535-537 (wrapper), L539-565 (implementation in ggml_backend_load_all_from_path)

Signature

GGML_API void ggml_backend_load_all(void);

The function accepts no parameters and returns no value. Its effect is entirely through side effects on the global backend registry.

Import

#include "ggml-backend.h"

Dependencies

Dependency Purpose
ggml-backend-dl.h / ggml-backend-dl.cpp Platform-abstracted dynamic library loading (dl_load_library, dl_get_sym, dl_error)
dlfcn.h (Linux/macOS) POSIX dynamic linking: dlopen, dlsym, dlclose, dlerror
windows.h (Windows) Windows dynamic linking: LoadLibraryW, GetProcAddress, FreeLibrary
<filesystem> (C++17) Directory traversal and path manipulation for backend discovery
ggml-backend-impl.h Backend interface types (ggml_backend_reg_t, ggml_backend_init_t, ggml_backend_score_t, GGML_BACKEND_API_VERSION)

I/O Contract

Inputs

None. The function takes no parameters. Discovery is automatic, scanning:

  • The GGML_BACKEND_DIR compile-time path (if defined).
  • The executable's directory (resolved via /proc/self/exe on Linux, _NSGetExecutablePath on macOS, GetModuleFileNameW on Windows).
  • The current working directory.
  • The GGML_BACKEND_PATH environment variable (for a single out-of-tree backend).

Outputs

No return value. The function operates entirely through side effects:

  • Populates the global ggml_backend_registry singleton with all discoverable backend registrations (ggml_backend_reg_t).
  • Registers all devices exposed by each loaded backend (ggml_backend_dev_t).
  • Backend shared library handles are held open for the lifetime of the process (released in the registry destructor).

After the call completes, the registered backends and devices are accessible through the enumeration API:

size_t n_backends = ggml_backend_reg_count();
size_t n_devices  = ggml_backend_dev_count();

Usage Examples

Basic initialization and best-backend selection:

#include "ggml-backend.h"

int main(void) {
    // Discover and register all available backends
    ggml_backend_load_all();

    // Select the best available backend (GPU > iGPU > CPU)
    ggml_backend_t backend = ggml_backend_init_best();

    // Use the backend for computation...

    ggml_backend_free(backend);
    return 0;
}

Enumerating registered backends after loading:

#include "ggml-backend.h"
#include <stdio.h>

int main(void) {
    ggml_backend_load_all();

    for (size_t i = 0; i < ggml_backend_reg_count(); i++) {
        ggml_backend_reg_t reg = ggml_backend_reg_get(i);
        printf("Backend: %s (%zu devices)\n",
               ggml_backend_reg_name(reg),
               ggml_backend_reg_dev_count(reg));
    }

    return 0;
}

Loading from a custom directory:

#include "ggml-backend.h"

int main(void) {
    // Load backends from a specific directory instead of default paths
    ggml_backend_load_all_from_path("/opt/ggml/backends");

    ggml_backend_t backend = ggml_backend_init_best();
    // ...
    ggml_backend_free(backend);
    return 0;
}

Related Pages

Implements Principle

Page Connections

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