The ggml_tensor struct describes a tensor but does not contain the tensor data itself.
The struct definition
// n-dimensional tensor
struct ggml_tensor {
enum ggml_type type;
struct ggml_backend_buffer * buffer;
int64_t ne[GGML_MAX_DIMS];
size_t nb[GGML_MAX_DIMS];
enum ggml_op op;
int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
int32_t flags;
struct ggml_tensor * src[GGML_MAX_SRC];
struct ggml_tensor * view_src;
size_t view_offs;
void * data;
char name[GGML_MAX_NAME];
void * extra;
char padding[8];
};The struct fields
We’ll hit on the important parts.
typeis the datatype of the values in the tensor, likef16andQ4_K.bufferis a pointer to the [[|ggml_backend_buffer]] that owns this tensor’s bytes.ne(number of elements) describes the shape of the tensor.nb(number of bytes) describes the size of a stride (in bytes) along the respective dimension.- e.g. With FP32 data values and
ne = {4, 3, 1, 1}and, the correspondingnbisnb = {4, 16, 48, 48}.
- e.g. With FP32 data values and
opdescribes the GGML operator that produces this tensor.GGML_OP_NONEindicates that this tensor is a leaf node (no parent/producer), meaning it is an input or constant.
op_paramsis 64 bytes of scratch storage.- Operators can encode its scalars here.
- e.g. softmax scale
srcare the input tensors that feed into the operator to create this tensor.view_srcis a pointer to the original tensor.view_src != NULLindicates that this tensor is merely a view of an original tensor.view_src != NULLimplies thatdatainstead points to somewhere in the original tensor’s data- It is common for a view to instead be a subset of the original tensor.
view_offsdescribes the byte offset from the basedatapointer of the original tensor.tensor->data == (char *)tensor->view_src->data + tensor->view_offs
datapoints to the actual data of the tensor.