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 Internal utilities

From Leeroopedia


Metadata

Field Value
Page Type Implementation (Internal Header)
Knowledge Sources GGML
Domains ML_Infrastructure, Tensor_Computing
Last Updated 2026-02-10 12:00 GMT

Overview

Internal implementation header providing shared utility functions, data structures, logging infrastructure, and hash set operations used across GGML's core source files and backend implementations.

Description

ggml-impl.h (719 lines) is the internal utility layer shared by ggml.c, ggml-alloc.c, ggml-backend.cpp, and all backend implementations. It provides:

Alignment and layout utilities:

  • ggml_up32(n) -- rounds n up to the nearest multiple of 32
  • ggml_up(n, m) -- rounds n up to the nearest multiple of m (power of 2)
  • ggml_are_same_layout(a, b) -- checks if two tensors have identical type, shape, and strides
  • ggml_op_is_empty(op) -- returns true for zero-cost operations (NONE, RESHAPE, TRANSPOSE, VIEW, PERMUTE)
  • TENSOR_ALIGNMENT (32) -- alignment requirement for mmap compatibility

Math helpers:

  • ggml_compute_softplus_f32(x) -- numerically stable softplus: log(1 + exp(x)) with large-input bypass

Logging infrastructure:

  • ggml_log_internal(level, format, ...) -- core logging function
  • Level-based macros: GGML_LOG_INFO, GGML_LOG_WARN, GGML_LOG_ERROR, GGML_LOG_DEBUG, GGML_LOG_CONT
  • Debug-level conditional macros controlled by GGML_DEBUG compile-time constant

Operation parameter helpers:

  • ggml_set_op_params(tensor, params, size) -- copies parameters into a tensor's op_params buffer
  • ggml_get_op_params_i32/f32(tensor, i) -- reads typed parameters from the buffer
  • ggml_set_op_params_i32/f32(tensor, i, value) -- writes typed parameters
  • Custom op parameter structs: ggml_map_custom1_op_params, ggml_map_custom2_op_params, ggml_map_custom3_op_params, ggml_custom_op_params

Bitset:

  • Compact bit array using uint32_t words with ggml_bitset_get, ggml_bitset_set, ggml_bitset_clear operations

Hash set:

  • Open-addressing hash table with linear probing, keyed on tensor pointer addresses (shifted right by 4 bits for alignment)
  • ggml_hash_set_new/free/reset for lifecycle management
  • ggml_hash_find, ggml_hash_insert, ggml_hash_find_or_insert, ggml_hash_contains for lookups

Usage

Include this header in internal GGML source files and backend implementations that need access to utility functions, logging, operation parameters, or the hash set. This header is not part of the public API.

Code Reference

Source Location

GGML repo, file: src/ggml-impl.h, 719 lines.

Signature

// Alignment utilities
static inline int ggml_up32(int n);
static inline int ggml_up(int n, int m);
static bool ggml_are_same_layout(const struct ggml_tensor * a,
                                  const struct ggml_tensor * b);
static bool ggml_op_is_empty(enum ggml_op op);

// Logging
GGML_API void ggml_log_internal(enum ggml_log_level level,
                                 const char * format, ...);

// Op params
static void    ggml_set_op_params(struct ggml_tensor * tensor,
                                   const void * params, size_t params_size);
static int32_t ggml_get_op_params_i32(const struct ggml_tensor * tensor, uint32_t i);
static float   ggml_get_op_params_f32(const struct ggml_tensor * tensor, uint32_t i);

// Hash set
struct ggml_hash_set ggml_hash_set_new(size_t size);
void                 ggml_hash_set_free(struct ggml_hash_set * hash_set);
static size_t ggml_hash_find(const struct ggml_hash_set * hash_set,
                              const struct ggml_tensor * key);
static size_t ggml_hash_insert(struct ggml_hash_set * hash_set,
                                struct ggml_tensor * key);

Import

#include "ggml-impl.h"

Dependencies

  • ggml.h -- core GGML public types and tensor definitions
  • gguf.h -- GGUF file format types
  • Platform-specific SIMD headers (ARM NEON, ARM SVE) when available

I/O Contract

Inputs

Parameter Type Required Description
tensor ggml_tensor * Yes (for op params) Tensor whose op_params buffer to read or write.
key ggml_tensor * Yes (for hash set) Tensor pointer used as the hash key.
n int Yes (for alignment) Value to round up to alignment boundary.

Outputs

Output Type Description
Aligned value int Value rounded up to the nearest multiple of the specified alignment.
Hash index size_t Index into the hash set where the key is located or should be inserted.
Op parameter int32_t or float Typed parameter value read from the tensor's op_params buffer.

Usage Examples

Reading Operation Parameters

#include "ggml-impl.h"

// Read integer parameters from a tensor operation
int32_t axis = ggml_get_op_params_i32(tensor, 0);
float eps    = ggml_get_op_params_f32(tensor, 1);

Using the Hash Set

#include "ggml-impl.h"

struct ggml_hash_set visited = ggml_hash_set_new(1024);

// Check if tensor has been visited
if (!ggml_hash_contains(&visited, tensor)) {
    ggml_hash_insert(&visited, tensor);
    // process tensor...
}

ggml_hash_set_free(&visited);

Checking Operation Type

#include "ggml-impl.h"

// Skip no-op operations during graph traversal
if (ggml_op_is_empty(node->op)) {
    continue;  // RESHAPE, VIEW, PERMUTE, TRANSPOSE are zero-cost
}

Related Pages

Page Connections

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