Skip to main content
Every computation in ggml operates on tensors. A tensor is a typed, multi-dimensional array backed by a contiguous (or strided) memory region. ggml supports up to 4 dimensions and a wide range of numeric types, from 32-bit floats down to sub-4-bit quantized formats.

The ggml_tensor struct

The core data structure is defined in ggml.h:
GGML_MAX_DIMS is 4, so every tensor is at most 4-dimensional. ne[0] is the innermost (fastest-varying) dimension — the number of columns in a matrix.

Data types

ggml_type covers floating-point formats, integer formats, and a large family of quantized types:
Use ggml_type_name(type) to get a human-readable string, and ggml_is_quantized(type) to test whether a type uses block quantization.

Creating tensors

Tensors are always allocated from a ggml_context. The context owns a fixed-size memory buffer; every tensor carves out space from it.
Dimension ordering follows column-major convention: ne[0] is the number of elements in the fastest-varying (innermost) dimension. For a matrix, ne[0] is columns and ne[1] is rows.
You can also use the generic allocator when the rank is determined at runtime:

Reading and writing values

For CPU-resident tensors the scalar helpers from ggml-cpu.h are the safest way to access individual elements:
For bulk initialization you can write directly through tensor->data using the stride fields:
Or copy an existing array in one call:

Tensor metadata

Each tensor carries metadata that describes how it was produced: The src array lets you walk the computation graph upward:
Name a tensor for easier debugging:

Contiguous vs strided tensors

ggml supports non-contiguous tensors produced by operations such as ggml_transpose, ggml_permute, and ggml_view_*. A tensor is contiguous when its elements are laid out in memory with no gaps and in the expected order.
Related predicates:
All ggml operations are written to respect nb strides and do not assume contiguity. If you need a contiguous copy for an external library, call ggml_cont:

Utility functions