Appearance
GGML 张量库基础 — 代码走读
ggml/include/ggml.h — 公共 API
这是 GGML 的主要头文件,导出了所有张量操作。
张量创建
c
// 创建上下文(内存池)
struct ggml_context * ggml_init(struct ggml_init_params params);
// 创建张量
struct ggml_tensor * ggml_new_tensor_1d(struct ggml_context * ctx,
enum ggml_type type, int64_t ne0);
struct ggml_tensor * ggml_new_tensor_2d(struct ggml_context * ctx,
enum ggml_type type, int64_t ne0, int64_t ne1);
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);算术运算
c
// 逐元素运算
struct ggml_tensor * ggml_add(ctx, a, b);
struct ggml_tensor * ggml_mul(ctx, a, b);
struct ggml_tensor * ggml_div(ctx, a, b);
// 矩阵乘法
struct ggml_tensor * ggml_mul_mat(ctx, a, b); // b @ a^T
// 激活函数
struct ggml_tensor * ggml_gelu(ctx, a);
struct ggml_tensor * ggml_silu(ctx, a);
struct ggml_tensor * ggml_relu(ctx, a);计算图执行
c
// 构建计算图
struct ggml_cgraph * ggml_new_graph(struct ggml_context * ctx);
void ggml_build_forward_expand(struct ggml_cgraph * graph, struct ggml_tensor * tensor);
// 计算
void ggml_graph_compute(struct ggml_cgraph * graph);ggml/src/ggml.c — 核心实现
关键实现细节:
张量操作注册
每个张量操作定义为一个 ggml_op,包含:
- 前向函数指针
- 反向函数指针(用于自动微分)
- 参数映射(用于不同后端的分发)
内存布局
GGML 使用行主序 (row-major) 存储,nb[0] 是最小步长(单个元素的字节数):
对于一个 shape [ne0, ne1] 的张量:
nb[0] = sizeof(element)
nb[1] = nb[0] * ne0 (一行)关键函数索引
| 函数 | 说明 |
|---|---|
ggml_init | 创建上下文内存池 |
ggml_new_tensor_*d | 创建指定维度张量 |
ggml_mul_mat | 矩阵乘法(核心运算) |
ggml_new_graph | 创建计算图 |
ggml_graph_compute | 执行计算图 |
ggml_set_param | 标记可训练参数 |
ggml_build_backward_expand | 构建反向传播图 |