Skip to main content
A ggml_tensor is the fundamental data container in ggml. Tensors are always allocated inside a ggml_context and support up to four dimensions (GGML_MAX_DIMS = 4). They store data in row-major order using stride-based addressing, which allows views, transposes, and permutations without copying.

ggml_type enum

Every tensor has an associated element type:

ggml_tensor struct

enum ggml_type
Element data type. Determines the size of each element and whether the tensor is quantized.
int64_t[4]
Number of elements in each dimension. ne[0] is the innermost (fastest-changing) dimension. Unused dimensions are set to 1.
size_t[4]
Stride in bytes for each dimension. nb[0] equals the element size; nb[1] equals the row size in bytes (may include padding). Strides allow non-contiguous memory layouts such as those produced by ggml_transpose and ggml_permute.
enum ggml_op
The operation that produced this tensor. GGML_OP_NONE for leaf tensors.
int32_t
Bitmask of ggml_tensor_flag values: GGML_TENSOR_FLAG_INPUT, GGML_TENSOR_FLAG_OUTPUT, GGML_TENSOR_FLAG_PARAM, GGML_TENSOR_FLAG_LOSS, GGML_TENSOR_FLAG_COMPUTE.
struct ggml_tensor *[GGML_MAX_SRC]
Pointers to the source tensors that this tensor was computed from. For example, after c = ggml_add(ctx, a, b), c->src[0] == a and c->src[1] == b.
void *
Raw pointer to the tensor’s element data. When no_alloc is true on the context, this is NULL until externally assigned.
char[GGML_MAX_NAME]
Human-readable label, up to 63 characters. Set with ggml_set_name() or ggml_format_name().

Creating tensors

All creation functions allocate the tensor struct (and, unless no_alloc is set, the backing data) from the context’s memory pool.

ggml_new_tensor

General-purpose tensor creation. ne is an array of n_dims element counts.
struct ggml_context *
required
Context that owns the new tensor.
enum ggml_type
required
Element data type.
int
required
Number of dimensions (1–4).
const int64_t *
required
Array of element counts, one per dimension. ne[0] is the innermost dimension.

Convenience constructors

These wrap ggml_new_tensor for common dimensionalities:

ggml_dup_tensor

Allocates a new tensor with the same shape and type as src, but with its own independent data buffer. The data is not copied.

ggml_view_tensor

Creates a new tensor that shares src’s data buffer (same shape, type, strides, and data pointer). Modifying the view’s data modifies src’s data.

Naming

ggml_set_name

Sets the tensor’s name (truncated to GGML_MAX_NAME - 1 characters). Returns tensor for chaining.

ggml_get_name

Returns the tensor’s name string.

ggml_format_name

Printf-style name assignment. Returns tensor.

Tensor flags

Flags mark tensors for special treatment during graph building and differentiation.

ggml_set_input

Marks the tensor as a graph input (GGML_TENSOR_FLAG_INPUT). Input tensors must be filled with data before ggml_graph_compute is called.

ggml_set_output

Marks the tensor as a graph output (GGML_TENSOR_FLAG_OUTPUT). Results are guaranteed to be written to host-accessible memory after computation.

ggml_set_param

Marks the tensor as a trainable parameter (GGML_TENSOR_FLAG_PARAM). The automatic differentiation engine tracks gradients for param tensors.

Data accessors

These functions are declared in ggml-cpu.h and operate on the CPU representation of tensor data.

Float accessors

Integer accessors

For non-F32 or non-I32 tensor types, the accessors perform implicit conversion. Direct stride-based pointer arithmetic is more efficient for bulk access:

Type query functions

ggml_nelements

Returns the total number of elements across all dimensions (ne[0] * ne[1] * ne[2] * ne[3]).

ggml_nbytes

Returns the total size of the tensor’s data in bytes.

ggml_is_quantized

Returns true if the type is a quantized format (any Q* or IQ* type).

ggml_is_contiguous

Returns true if the tensor elements can be iterated with a flat index — no gaps and no permutation. Equivalent to ggml_is_contiguous_0().

Additional query functions

Context enumeration

Iterate over all tensors in a context: