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_new_tensor

From Leeroopedia


Template:Implementation

Summary

The ggml_new_tensor family of functions is the primary API for allocating typed, multi-dimensional tensor objects within a GGML context. Four convenience wrappers cover the common 1-D through 4-D cases, each delegating to a shared internal allocation routine that records shape, stride, element type, and metadata on a context-owned arena.

Import

#include "ggml.h"

Dependencies

  • ggml.h -- public header defining struct ggml_tensor, enum ggml_type, and the tensor creation prototypes.

Function Signatures

ggml_new_tensor_1d

struct ggml_tensor * ggml_new_tensor_1d(
    struct ggml_context * ctx,
    enum ggml_type        type,
    int64_t               ne0);

ggml_new_tensor_2d

struct ggml_tensor * ggml_new_tensor_2d(
    struct ggml_context * ctx,
    enum ggml_type        type,
    int64_t               ne0,
    int64_t               ne1);

ggml_new_tensor_3d

struct ggml_tensor * ggml_new_tensor_3d(
    struct ggml_context * ctx,
    enum ggml_type        type,
    int64_t               ne0,
    int64_t               ne1,
    int64_t               ne2);

ggml_new_tensor_4d

struct ggml_tensor * ggml_new_tensor_4d(
    struct ggml_context * ctx,
    enum ggml_type        type,
    int64_t               ne0,
    int64_t               ne1,
    int64_t               ne2,
    int64_t               ne3);

Source: src/ggml.c:L1749-1784

Parameters

Parameter Type Description
ctx struct ggml_context * The GGML context that owns the tensor allocation. Memory for the tensor header (and optionally data) is drawn from the context's arena.
type enum ggml_type Element type of the tensor. Common values: GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, among 30+ supported types.
ne0 int64_t Size of the first (innermost / fastest-varying) dimension.
ne1 int64_t Size of the second dimension. (2-D, 3-D, 4-D variants)
ne2 int64_t Size of the third dimension. (3-D, 4-D variants)
ne3 int64_t Size of the fourth dimension. (4-D variant only)

Return Value

Returns a struct ggml_tensor * with the following metadata populated:

  • ne[0..3] -- dimension sizes (unused dimensions set to 1).
  • nb[0..3] -- byte strides computed from the element type's block size.
  • type -- the requested enum ggml_type.
  • op -- set to GGML_OP_NONE (no computation attached yet).
  • data -- pointer to the raw data buffer (may be NULL if the context was created without a data buffer; backends allocate storage separately).

Populating Tensor Data

After creation, tensor data is written through the backend API:

ggml_backend_tensor_set

void ggml_backend_tensor_set(
    struct ggml_tensor * tensor,
    const void         * data,
    size_t               offset,
    size_t               size);

Source: src/ggml-backend.cpp:L282-295

Parameter Type Description
tensor struct ggml_tensor * Target tensor whose data buffer will be written.
data const void * Pointer to the source data to copy into the tensor.
offset size_t Byte offset into the tensor's data buffer at which to begin writing.
size size_t Number of bytes to copy from data into the tensor.

This function abstracts over the backing storage: it works for CPU memory, GPU buffers, and any other registered backend.

Usage Example

#include "ggml.h"
#include "ggml-backend.h"

// 1. Create a context with enough space
struct ggml_init_params params = {
    .mem_size   = 16 * 1024 * 1024,  // 16 MB arena
    .mem_buffer = NULL,               // let ggml allocate
    .no_alloc   = false,
};
struct ggml_context * ctx = ggml_init(params);

// 2. Allocate a 2-D float32 tensor (768 x 512)
struct ggml_tensor * weight = ggml_new_tensor_2d(
    ctx, GGML_TYPE_F32, 768, 512);

// 3. Allocate a 1-D float16 tensor (768 elements)
struct ggml_tensor * bias = ggml_new_tensor_1d(
    ctx, GGML_TYPE_F16, 768);

// 4. Populate weight data (assuming CPU backend)
float host_data[768 * 512];
// ... fill host_data ...
ggml_backend_tensor_set(weight, host_data, 0,
    768 * 512 * sizeof(float));

// 5. Clean up
ggml_free(ctx);

Related

Page Connections

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