mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-11-26 23:29:34 +00:00
CUDA: fuse the Inkling 2 short conv, depth mix, loop controller, L2 clip and situ chains
This commit is contained in:
parent
71091234a0
commit
5e8f72c8d7
6 changed files with 1508 additions and 0 deletions
|
|
@ -4,6 +4,7 @@
|
|||
#include "dsv4-hc.cuh"
|
||||
#include "fp8.cuh"
|
||||
#include "gated_delta_net.cuh"
|
||||
#include "inkling2.cuh"
|
||||
#include "mmq.cuh"
|
||||
#include "mmq-few.cuh"
|
||||
#include "mmvf.cuh"
|
||||
|
|
@ -1385,8 +1386,615 @@ static bool ggml_cuda_can_fuse(const struct ggml_cgraph * cgraph,
|
|||
return false;
|
||||
}
|
||||
|
||||
// fused op chains of the Inkling 2 graph (inkling2.cuh, ggml_cuda_op_l2_clip)
|
||||
|
||||
static bool ggml_cuda_tensors_overlap(const ggml_tensor * a, const ggml_tensor * b) {
|
||||
const char * a0 = (const char *) a->data;
|
||||
const char * b0 = (const char *) b->data;
|
||||
const char * a1 = a0 + ggml_backend_buft_get_alloc_size(a->buffer->buft, a);
|
||||
const char * b1 = b0 + ggml_backend_buft_get_alloc_size(b->buffer->buft, b);
|
||||
return a0 < b1 && b0 < a1;
|
||||
}
|
||||
|
||||
// a dimension of src broadcasts to dst if it is 1 or equal
|
||||
static bool ggml_cuda_can_bcast_to(const ggml_tensor * src, const ggml_tensor * dst, int first_dim) {
|
||||
for (int d = first_dim; d < GGML_MAX_DIMS; ++d) {
|
||||
if (src->ne[d] != 1 && src->ne[d] != dst->ne[d]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// [MUL by a per-column weight] -> L2_NORM -> SCALE [-> MUL by a per-channel/sample factor]
|
||||
static int ggml_cuda_try_fuse_l2_clip(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
const ggml_tensor * node = cgraph->nodes[i];
|
||||
const bool has_w = node->op == GGML_OP_MUL;
|
||||
if (!has_w && node->op != GGML_OP_L2_NORM) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const ggml_tensor * x = node->src[0];
|
||||
const ggml_tensor * w = has_w ? node->src[1] : nullptr;
|
||||
if (x->type != GGML_TYPE_F32 || x->nb[0] != sizeof(float)) {
|
||||
return 0;
|
||||
}
|
||||
if (has_w && (!ggml_are_same_shape(x, node) || w->type != GGML_TYPE_F32 || w->nb[0] != sizeof(float) ||
|
||||
w->ne[0] != x->ne[0] || !ggml_cuda_can_bcast_to(w, x, 1))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (const bool has_f : { true, false }) {
|
||||
ggml_op ops[4];
|
||||
int n = 0;
|
||||
if (has_w) {
|
||||
ops[n++] = GGML_OP_MUL;
|
||||
}
|
||||
ops[n++] = GGML_OP_L2_NORM;
|
||||
ops[n++] = GGML_OP_SCALE;
|
||||
if (has_f) {
|
||||
ops[n++] = GGML_OP_MUL;
|
||||
}
|
||||
|
||||
if (!ggml_can_fuse(cgraph, i, ops, n)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const ggml_tensor * l2 = cgraph->nodes[i + n - (has_f ? 3 : 2)];
|
||||
const ggml_tensor * sc = cgraph->nodes[i + n - (has_f ? 2 : 1)];
|
||||
ggml_tensor * dst = cgraph->nodes[i + n - 1];
|
||||
const ggml_tensor * f = nullptr;
|
||||
|
||||
if (l2->type != GGML_TYPE_F32 || sc->type != GGML_TYPE_F32 || sc->src[0] != l2 || (has_w && l2->src[0] != node) ||
|
||||
!ggml_is_contiguous(dst)) {
|
||||
continue;
|
||||
}
|
||||
if (has_f) {
|
||||
f = dst->src[1];
|
||||
if (dst->src[0] != sc || f->type != GGML_TYPE_F32 || f->ne[0] != 1 || f->ne[1] != 1 || !ggml_cuda_can_bcast_to(f, dst, 2)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// a block reads its row before it writes it, so dst may be x itself but must not overlap it otherwise
|
||||
const bool x_in_place = x->data == dst->data && ggml_is_contiguous(x);
|
||||
if ((!x_in_place && ggml_cuda_tensors_overlap(x, dst)) || (w && ggml_cuda_tensors_overlap(w, dst)) ||
|
||||
(f && ggml_cuda_tensors_overlap(f, dst))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ggml_cuda_op_l2_clip(*cuda_ctx, x, w, l2, sc, f, dst);
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// situ on the halves of a merged gate_up: SCALE -> TANH -> SCALE, SIGMOID, MUL, (up view) SCALE -> TANH -> SCALE, MUL
|
||||
static int ggml_cuda_try_fuse_situ(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
static constexpr ggml_op ops[] = {
|
||||
GGML_OP_SCALE, GGML_OP_UNARY, GGML_OP_SCALE, GGML_OP_UNARY, GGML_OP_MUL, GGML_OP_VIEW,
|
||||
GGML_OP_SCALE, GGML_OP_UNARY, GGML_OP_SCALE, GGML_OP_MUL,
|
||||
};
|
||||
constexpr int n = sizeof(ops)/sizeof(ops[0]);
|
||||
|
||||
if (i + n > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
for (int k = 0; k < n; ++k) {
|
||||
if (cgraph->nodes[i + k]->op != ops[k]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor ** nd = cgraph->nodes + i;
|
||||
|
||||
const ggml_tensor * gate = nd[0]->src[0];
|
||||
const ggml_tensor * up = nd[5];
|
||||
|
||||
const auto is_mul_of = [](const ggml_tensor * t, const ggml_tensor * a, const ggml_tensor * b) {
|
||||
return (t->src[0] == a && t->src[1] == b) || (t->src[0] == b && t->src[1] == a);
|
||||
};
|
||||
|
||||
if (ggml_get_unary_op(nd[1]) != GGML_UNARY_OP_TANH || ggml_get_unary_op(nd[3]) != GGML_UNARY_OP_SIGMOID ||
|
||||
ggml_get_unary_op(nd[7]) != GGML_UNARY_OP_TANH ||
|
||||
nd[1]->src[0] != nd[0] || nd[2]->src[0] != nd[1] || nd[3]->src[0] != gate || !is_mul_of(nd[4], nd[2], nd[3]) ||
|
||||
nd[6]->src[0] != up || nd[7]->src[0] != nd[6] || nd[8]->src[0] != nd[7] || !is_mul_of(nd[9], nd[4], nd[8])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (gate->type != GGML_TYPE_F32 || up->type != GGML_TYPE_F32 || gate->nb[0] != sizeof(float) || up->nb[0] != sizeof(float) ||
|
||||
!ggml_are_same_shape(gate, up) || !ggml_are_same_shape(gate, nd[9]) || !ggml_is_contiguous(nd[9])) {
|
||||
return 0;
|
||||
}
|
||||
for (int k = 0; k < n; ++k) {
|
||||
if (nd[k]->type != GGML_TYPE_F32) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// the up view is a view of a node outside the group, it is not part of the subgraph check
|
||||
const int idxs[] = { i, i + 1, i + 2, i + 3, i + 4, i + 6, i + 7, i + 8, i + 9 };
|
||||
const ggml_op ops_ext[] = { ops[0], ops[1], ops[2], ops[3], ops[4], ops[6], ops[7], ops[8], ops[9] };
|
||||
const int out = i + n - 1;
|
||||
if (!ggml_can_fuse_subgraph_ext(cgraph, idxs, 9, ops_ext, &out, 1) || !ggml_cuda_check_fusion_memory_ranges(cgraph, i, n, &out, 1)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const float s[4] = {
|
||||
ggml_get_op_params_f32(nd[0], 0), ggml_get_op_params_f32(nd[2], 0), ggml_get_op_params_f32(nd[6], 0), ggml_get_op_params_f32(nd[8], 0),
|
||||
};
|
||||
const float b[4] = {
|
||||
ggml_get_op_params_f32(nd[0], 1), ggml_get_op_params_f32(nd[2], 1), ggml_get_op_params_f32(nd[6], 1), ggml_get_op_params_f32(nd[8], 1),
|
||||
};
|
||||
|
||||
ggml_cuda_op_situ(*cuda_ctx, gate, up, s, b, nd[9]);
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
// depth mix: [RMS_NORM(cur)] -> CONCAT(stack, norm) -> [REPEAT(query)] -> MUL_MAT -> SOFT_MAX, TRANSPOSE -> CONT -> MUL_MAT
|
||||
static int ggml_cuda_try_fuse_depth_mix(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
const ggml_tensor * node = cgraph->nodes[i];
|
||||
const bool has_norm = node->op == GGML_OP_RMS_NORM;
|
||||
|
||||
const int j = i + (has_norm ? 1 : 0);
|
||||
if (j + 1 >= cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const ggml_tensor * cc = cgraph->nodes[j];
|
||||
if (cc->op != GGML_OP_CONCAT || ggml_get_op_params_i32(cc, 0) != 1 || (has_norm && cc->src[1] != node) ||
|
||||
(!has_norm && cc->src[1]->op != GGML_OP_RMS_NORM)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const bool has_rep = cgraph->nodes[j + 1]->op == GGML_OP_REPEAT;
|
||||
|
||||
ggml_op ops[8];
|
||||
int n = 0;
|
||||
if (has_norm) {
|
||||
ops[n++] = GGML_OP_RMS_NORM;
|
||||
}
|
||||
ops[n++] = GGML_OP_CONCAT;
|
||||
if (has_rep) {
|
||||
ops[n++] = GGML_OP_REPEAT;
|
||||
}
|
||||
ops[n++] = GGML_OP_MUL_MAT;
|
||||
ops[n++] = GGML_OP_SOFT_MAX;
|
||||
ops[n++] = GGML_OP_TRANSPOSE;
|
||||
ops[n++] = GGML_OP_CONT;
|
||||
ops[n++] = GGML_OP_MUL_MAT;
|
||||
|
||||
if (i + n > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
for (int k = 0; k < n; ++k) {
|
||||
if (cgraph->nodes[i + k]->op != ops[k]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const ggml_tensor * rep = has_rep ? cgraph->nodes[j + 1] : nullptr;
|
||||
const ggml_tensor * mm1 = cgraph->nodes[i + n - 5];
|
||||
const ggml_tensor * sm = cgraph->nodes[i + n - 4];
|
||||
const ggml_tensor * tr = cgraph->nodes[i + n - 3];
|
||||
const ggml_tensor * ct = cgraph->nodes[i + n - 2];
|
||||
ggml_tensor * mm2 = cgraph->nodes[i + n - 1];
|
||||
|
||||
const ggml_tensor * stack = cc->src[0];
|
||||
const ggml_tensor * cur = has_norm ? node->src[0] : cc->src[1];
|
||||
const ggml_tensor * query = has_rep ? rep->src[0] : mm1->src[1];
|
||||
|
||||
if (mm1->src[0] != cc || mm1->src[1] != (has_rep ? rep : query) || sm->src[0] != mm1 || sm->src[1] || sm->src[2] ||
|
||||
ggml_get_op_params_f32(sm, 1) != 0.0f || tr->src[0] != cc || ct->src[0] != tr || mm2->src[0] != sm || mm2->src[1] != ct) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int64_t ne = cur->ne[0];
|
||||
const int64_t T = cur->ne[2];
|
||||
const int64_t B = stack->ne[1];
|
||||
const int64_t Q = query->ne[1];
|
||||
|
||||
for (const ggml_tensor * t : std::initializer_list<const ggml_tensor *>{ stack, cur, query, cc, mm1, sm, mm2 }) {
|
||||
if (t->type != GGML_TYPE_F32) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (cur->nb[0] != sizeof(float) || cur->ne[1] != 1 || cur->ne[3] != 1 || !ggml_is_contiguous(stack) ||
|
||||
stack->ne[0] != ne || stack->ne[2] != T || stack->ne[3] != 1 || B < 1 || B > GGML_CUDA_DEPTH_MIX_MAX_ROWS ||
|
||||
!ggml_is_contiguous(query) || query->ne[0] != ne || query->ne[2] != 1 || query->ne[3] != 1 || (!has_rep && T != 1) ||
|
||||
mm2->ne[0] != Q || mm2->ne[1] != ne || mm2->ne[2] != T || !ggml_is_contiguous(mm2)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int out = i + n - 1;
|
||||
if (!ggml_can_fuse_subgraph(cgraph, i, n, ops, &out, 1) || !ggml_cuda_check_fusion_memory_ranges(cgraph, i, n, &out, 1)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const float eps = has_norm ? ggml_get_op_params_f32(node, 0) : 0.0f;
|
||||
ggml_cuda_op_depth_mix(*cuda_ctx, stack, cur, has_norm, eps, query, ggml_get_op_params_f32(sm, 0), mm2);
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
// loop controller: CONCAT(cur, msg) -> RMS_NORM -> RESHAPE -> MUL_MAT(down) -> MUL_MAT(forget) -> TANH -> MUL(cur) -> SUB(cur),
|
||||
// MUL_MAT(gain) -> TANH -> MUL(msg) -> ADD
|
||||
static int ggml_cuda_try_fuse_loop_ctrl(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
static constexpr ggml_op ops[] = {
|
||||
GGML_OP_CONCAT, GGML_OP_RMS_NORM, GGML_OP_RESHAPE, GGML_OP_MUL_MAT, GGML_OP_MUL_MAT, GGML_OP_UNARY, GGML_OP_MUL, GGML_OP_SUB,
|
||||
GGML_OP_MUL_MAT, GGML_OP_UNARY, GGML_OP_MUL, GGML_OP_ADD,
|
||||
};
|
||||
constexpr int n = sizeof(ops)/sizeof(ops[0]);
|
||||
|
||||
if (i + n > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
for (int k = 0; k < n; ++k) {
|
||||
if (cgraph->nodes[i + k]->op != ops[k]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor ** nd = cgraph->nodes + i;
|
||||
|
||||
const ggml_tensor * cc = nd[0];
|
||||
if (ggml_get_op_params_i32(cc, 0) != 1 || cc->src[0]->op != GGML_OP_RESHAPE || cc->src[1]->op != GGML_OP_RESHAPE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const ggml_tensor * cur = cc->src[0]->src[0];
|
||||
const ggml_tensor * msg = cc->src[1]->src[0];
|
||||
const ggml_tensor * down = nd[3];
|
||||
const ggml_tensor * wd = down->src[0];
|
||||
const ggml_tensor * wf = nd[4]->src[0];
|
||||
const ggml_tensor * wg = nd[8]->src[0];
|
||||
ggml_tensor * dst = nd[11];
|
||||
|
||||
if (nd[1]->src[0] != cc || nd[2]->src[0] != nd[1] || down->src[1] != nd[2] || nd[4]->src[1] != down ||
|
||||
ggml_get_unary_op(nd[5]) != GGML_UNARY_OP_TANH || nd[5]->src[0] != nd[4] ||
|
||||
nd[6]->src[0] != nd[5] || nd[6]->src[1] != cur || nd[7]->src[0] != cur || nd[7]->src[1] != nd[6] ||
|
||||
nd[8]->src[1] != down || ggml_get_unary_op(nd[9]) != GGML_UNARY_OP_TANH || nd[9]->src[0] != nd[8] ||
|
||||
nd[10]->src[0] != nd[9] || nd[10]->src[1] != msg || dst->src[0] != nd[7] || dst->src[1] != nd[10]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int64_t ne = cur->ne[0];
|
||||
const int64_t T = cur->ne[1];
|
||||
const int64_t rank = wd->ne[1];
|
||||
|
||||
if (T > GGML_CUDA_LOOP_CTRL_MAX_TOKENS || !ggml_is_contiguous(cur) || !ggml_is_contiguous(msg) || !ggml_are_same_shape(cur, msg) ||
|
||||
cur->ne[2] != 1 || cur->ne[3] != 1 || !ggml_are_same_shape(cur, dst) || !ggml_is_contiguous(dst) ||
|
||||
!ggml_is_contiguous(down) || down->ne[0] != rank || down->ne[1] != T) {
|
||||
return 0;
|
||||
}
|
||||
for (const ggml_tensor * t : std::initializer_list<const ggml_tensor *>{ cur, msg, wd, wf, wg, down, dst }) {
|
||||
if (t->type != GGML_TYPE_F32) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
for (const ggml_tensor * w : { wd, wf, wg }) {
|
||||
if (!ggml_is_contiguous(w) || w->ne[2] != 1 || w->ne[3] != 1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (wd->ne[0] != 2*ne || wf->ne[0] != rank || wg->ne[0] != rank || wf->ne[1] != ne || wg->ne[1] != ne) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// the down product is written by the first kernel and read by the second, so the output must not overlap it
|
||||
const int outs[] = { i + 3, i + n - 1 };
|
||||
if (!ggml_can_fuse_subgraph(cgraph, i, n, ops, outs, 2) || !ggml_cuda_check_fusion_memory_ranges(cgraph, i, n, outs, 2) ||
|
||||
ggml_cuda_tensors_overlap(dst, down)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ggml_cuda_op_loop_ctrl(*cuda_ctx, cur, msg, ggml_get_op_params_f32(nd[1], 0), wd, wg, wf, nd[3], dst);
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
// short conv: CONCAT(past, cur^T) -> the state store -> SSM_CONV -> RESHAPE -> [taps] -> ADD(cur) -> SCALE [-> ADD]
|
||||
// each tap: VIEW(x) -> TRANSPOSE -> CONT -> RESHAPE, [RESHAPE(dyn)], VIEW(dyn) -> MUL -> RESHAPE -> ADD
|
||||
static int ggml_cuda_try_fuse_short_conv(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
// the subgraph nodes: 5 for the concat, state store and conv, 7 per tap, 3 for the adds and the scale
|
||||
constexpr int MAX_NODES = 8 + 7*GGML_CUDA_SHORT_CONV_MAX_K;
|
||||
|
||||
const ggml_tensor * cc = cgraph->nodes[i];
|
||||
if (cc->op != GGML_OP_CONCAT || ggml_get_op_params_i32(cc, 0) != 0 || i + 8 > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const ggml_tensor * past = cc->src[0];
|
||||
const ggml_tensor * xin = cc->src[1];
|
||||
|
||||
if (cc->type != GGML_TYPE_F32 || past->type != GGML_TYPE_F32 || past->nb[0] != sizeof(float) || cc->ne[3] != 1 ||
|
||||
!ggml_is_contiguous(cc) || xin->op != GGML_OP_TRANSPOSE || xin->src[0]->op != GGML_OP_RESHAPE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int64_t K1 = past->ne[0];
|
||||
const int64_t C = cc->ne[1];
|
||||
const int64_t S = cc->ne[2];
|
||||
const int64_t Ts = cc->ne[0] - K1;
|
||||
const int64_t K = K1 + 1;
|
||||
|
||||
const ggml_tensor * cur = xin->src[0]->src[0];
|
||||
if (K < 2 || K > GGML_CUDA_SHORT_CONV_MAX_K || Ts < 1 || past->ne[1] != C || past->ne[2] != S || xin->ne[0] != Ts ||
|
||||
cur->type != GGML_TYPE_F32 || !ggml_is_contiguous(cur) || cur->ne[0] != C || ggml_nelements(cur) != C*Ts*S) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int idxs[MAX_NODES];
|
||||
ggml_op ops[MAX_NODES];
|
||||
int n_sub = 0;
|
||||
auto add_node = [&](int k) {
|
||||
idxs[n_sub] = k;
|
||||
ops[n_sub] = cgraph->nodes[k]->op;
|
||||
n_sub++;
|
||||
};
|
||||
|
||||
int k = i;
|
||||
add_node(k++);
|
||||
|
||||
// the state store: the last K - 1 columns of x copied to the cell of each sequence (VIEW, VIEW, CPY), or with rollback
|
||||
// slots the windows that end at the last kw tokens set into slots kw - 1 .. 0 (VIEW, ARANGE, SCALE, CPY, VIEW, VIEW, SET_ROWS)
|
||||
const ggml_tensor * cell = nullptr;
|
||||
int st_idx = -1;
|
||||
int64_t kw = 1;
|
||||
int64_t slot_s = 0;
|
||||
int64_t ch_s = K1;
|
||||
int64_t seq_s = 0;
|
||||
if (cgraph->nodes[k + 1]->op == GGML_OP_ARANGE) {
|
||||
if (k + 7 > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
const ggml_tensor * win = cgraph->nodes[k];
|
||||
const ggml_tensor * ar = cgraph->nodes[k + 1];
|
||||
const ggml_tensor * sb = cgraph->nodes[k + 2];
|
||||
const ggml_tensor * cs = cgraph->nodes[k + 3];
|
||||
const ggml_tensor * ord = cgraph->nodes[k + 4];
|
||||
const ggml_tensor * sl = cgraph->nodes[k + 5];
|
||||
const ggml_tensor * sr = cgraph->nodes[k + 6];
|
||||
|
||||
const int64_t n_slots = ar->ne[0];
|
||||
kw = ord->ne[0];
|
||||
|
||||
// the slot order {n_slots - 1, ..., 0}, its last kw entries
|
||||
if (win->op != GGML_OP_VIEW || sb->op != GGML_OP_SCALE || cs->op != GGML_OP_CPY || ord->op != GGML_OP_VIEW ||
|
||||
sl->op != GGML_OP_VIEW || sr->op != GGML_OP_SET_ROWS ||
|
||||
ggml_get_op_params_f32(ar, 0) != 0.0f || ggml_get_op_params_f32(ar, 2) != 1.0f || sb->src[0] != ar ||
|
||||
ggml_get_op_params_f32(sb, 0) != -1.0f || ggml_get_op_params_f32(sb, 1) != (float) (n_slots - 1) ||
|
||||
cs->src[0] != sb || cs->type != GGML_TYPE_I32 || ord->view_src != cs ||
|
||||
ord->view_offs != (size_t) (n_slots - kw)*sizeof(int32_t) || kw < 1 || kw > n_slots || kw > Ts) {
|
||||
return 0;
|
||||
}
|
||||
// window w is x[Ts - (kw - 1) + w, +K - 1) of each channel
|
||||
if (win->view_src != cc || win->ne[0] != K1 || win->ne[1] != kw || win->ne[2] != C || win->ne[3] != S ||
|
||||
win->nb[0] != sizeof(float) || win->nb[1] != sizeof(float) || win->nb[2] != cc->nb[1] || win->nb[3] != cc->nb[2] ||
|
||||
win->view_offs != (size_t) (Ts - (kw - 1))*sizeof(float)) {
|
||||
return 0;
|
||||
}
|
||||
// SET_ROWS: src[0] the rows, src[1] the indices, src[2] the destination
|
||||
if (sr->src[0] != win || sr->src[1] != ord || sr->src[2] != sl || sl->type != GGML_TYPE_F32 || sl->nb[0] != sizeof(float) ||
|
||||
sl->ne[0] != K1 || sl->ne[1] != n_slots || sl->ne[2] != C || sl->ne[3] != S) {
|
||||
return 0;
|
||||
}
|
||||
// the slot order is a constant the kernel does not need; the cast is its own src[1], so its uses are checked here
|
||||
if (ggml_node_get_use_count(cgraph, k + 1) != 1 || ggml_node_get_use_count(cgraph, k + 2) != 1 ||
|
||||
ggml_node_get_use_count(cgraph, k + 3) != 1 + (cs->src[1] == cs ? 1 : 0) || ggml_node_get_use_count(cgraph, k + 4) != 1) {
|
||||
return 0;
|
||||
}
|
||||
cell = sl;
|
||||
st_idx = k + 6;
|
||||
slot_s = sl->nb[1]/sizeof(float);
|
||||
ch_s = sl->nb[2]/sizeof(float);
|
||||
seq_s = sl->nb[3]/sizeof(float);
|
||||
add_node(k); // windows
|
||||
add_node(k + 6); // set_rows, the slots are a view of the state outside the group
|
||||
k += 7;
|
||||
} else {
|
||||
const ggml_tensor * tail = cgraph->nodes[k];
|
||||
const ggml_tensor * cpy = cgraph->nodes[k + 2];
|
||||
cell = cgraph->nodes[k + 1];
|
||||
if (tail->op != GGML_OP_VIEW || cell->op != GGML_OP_VIEW || cpy->op != GGML_OP_CPY ||
|
||||
tail->view_src != cc || tail->view_offs != (size_t) Ts*sizeof(float) || tail->ne[0] != K1 || tail->ne[1] != C ||
|
||||
tail->ne[2] != S || tail->nb[1] != cc->nb[1] || tail->nb[2] != cc->nb[2] || cpy->src[0] != tail || cpy->src[1] != cell ||
|
||||
cell->type != GGML_TYPE_F32 || cell->nb[0] != sizeof(float) || cell->ne[0] != K1*C || ggml_nrows(cell) != S ||
|
||||
ggml_get_op_params_i32(cpy, 0) != 0) {
|
||||
return 0;
|
||||
}
|
||||
st_idx = k + 2;
|
||||
seq_s = cell->nb[1]/sizeof(float);
|
||||
add_node(k); // tail
|
||||
add_node(k + 2); // cpy, the cell is a view of the state outside the group
|
||||
k += 3;
|
||||
}
|
||||
const int cpy_idx = st_idx;
|
||||
|
||||
if (k + 4 > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
const ggml_tensor * conv = cgraph->nodes[k];
|
||||
const ggml_tensor * w = conv->src[1];
|
||||
const ggml_tensor * out = cgraph->nodes[k + 1];
|
||||
if (conv->op != GGML_OP_SSM_CONV || conv->src[0] != cc || w->type != GGML_TYPE_F32 || !ggml_is_contiguous(w) ||
|
||||
w->ne[0] != K || w->ne[1] != C || out->op != GGML_OP_RESHAPE || out->src[0] != conv || out->ne[0] != C || out->ne[1] != Ts*S) {
|
||||
return 0;
|
||||
}
|
||||
add_node(k);
|
||||
add_node(k + 1);
|
||||
k += 2;
|
||||
|
||||
// the input-dependent taps, one per lag
|
||||
const ggml_tensor * dyn = nullptr;
|
||||
int64_t n_heads = 0;
|
||||
if (cgraph->nodes[k]->op == GGML_OP_VIEW && cgraph->nodes[k]->view_src == cc) {
|
||||
for (int64_t lag = 0; lag < K; ++lag) {
|
||||
if (k + 9 > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
const ggml_tensor * win = cgraph->nodes[k];
|
||||
const ggml_tensor * tr = cgraph->nodes[k + 1];
|
||||
const ggml_tensor * ct = cgraph->nodes[k + 2];
|
||||
const ggml_tensor * wr = cgraph->nodes[k + 3];
|
||||
if (win->op != GGML_OP_VIEW || tr->op != GGML_OP_TRANSPOSE || ct->op != GGML_OP_CONT || wr->op != GGML_OP_RESHAPE ||
|
||||
win->view_src != cc || win->view_offs != (size_t) (K1 - lag)*sizeof(float) || win->ne[0] != Ts || win->ne[1] != C ||
|
||||
win->ne[2] != S || win->nb[1] != cc->nb[1] || win->nb[2] != cc->nb[2] ||
|
||||
tr->src[0] != win || ct->src[0] != tr || wr->src[0] != ct) {
|
||||
return 0;
|
||||
}
|
||||
for (int m = 0; m < 4; ++m) {
|
||||
add_node(k + m);
|
||||
}
|
||||
k += 4;
|
||||
|
||||
if (lag == 0) {
|
||||
const ggml_tensor * d3 = cgraph->nodes[k];
|
||||
if (d3->op != GGML_OP_RESHAPE || d3->ne[0] != K || d3->ne[2] != Ts*S) {
|
||||
return 0;
|
||||
}
|
||||
dyn = d3->src[0];
|
||||
n_heads = d3->ne[1];
|
||||
if (dyn->type != GGML_TYPE_F32 || !ggml_is_contiguous(dyn) || dyn->ne[0] != K*n_heads || n_heads < 1 || C % n_heads != 0) {
|
||||
return 0;
|
||||
}
|
||||
k += 1; // a view of the taps input outside the group
|
||||
}
|
||||
|
||||
const ggml_tensor * tap = cgraph->nodes[k];
|
||||
const ggml_tensor * mul = cgraph->nodes[k + 1];
|
||||
const ggml_tensor * term = cgraph->nodes[k + 2];
|
||||
const ggml_tensor * add = cgraph->nodes[k + 3];
|
||||
if (tap->op != GGML_OP_VIEW || mul->op != GGML_OP_MUL || term->op != GGML_OP_RESHAPE || add->op != GGML_OP_ADD ||
|
||||
tap->view_src != dyn || tap->view_offs != dyn->view_offs + (size_t) lag*sizeof(float) || tap->ne[0] != 1 ||
|
||||
tap->ne[1] != n_heads || tap->nb[1] != dyn->nb[0]*K || tap->nb[2] != dyn->nb[1] ||
|
||||
wr->ne[0] != C/n_heads || wr->ne[1] != n_heads ||
|
||||
mul->src[0] != wr || mul->src[1] != tap || term->src[0] != mul || add->src[0] != out || add->src[1] != term) {
|
||||
return 0;
|
||||
}
|
||||
add_node(k + 1);
|
||||
add_node(k + 2);
|
||||
add_node(k + 3);
|
||||
k += 4;
|
||||
out = add;
|
||||
}
|
||||
}
|
||||
|
||||
if (k + 2 > cgraph->n_nodes) {
|
||||
return 0;
|
||||
}
|
||||
const ggml_tensor * res_add = cgraph->nodes[k];
|
||||
ggml_tensor * sc = cgraph->nodes[k + 1];
|
||||
if (res_add->op != GGML_OP_ADD || sc->op != GGML_OP_SCALE || res_add->src[0] != cur || res_add->src[1] != out ||
|
||||
sc->src[0] != res_add || !ggml_is_contiguous(sc) || sc->type != GGML_TYPE_F32) {
|
||||
return 0;
|
||||
}
|
||||
add_node(k);
|
||||
add_node(k + 1);
|
||||
k += 2;
|
||||
|
||||
// the residual add that follows the attention and FFN output convs
|
||||
ggml_tensor * dst = sc;
|
||||
const ggml_tensor * resid = nullptr;
|
||||
if (k < cgraph->n_nodes && cgraph->nodes[k]->op == GGML_OP_ADD &&
|
||||
(cgraph->nodes[k]->src[0] == sc || cgraph->nodes[k]->src[1] == sc)) {
|
||||
ggml_tensor * add = cgraph->nodes[k];
|
||||
const ggml_tensor * r = add->src[0] == sc ? add->src[1] : add->src[0];
|
||||
if (r != sc && r->type == GGML_TYPE_F32 && ggml_is_contiguous(r) && ggml_are_same_shape(r, sc) &&
|
||||
ggml_is_contiguous(add) && add->type == GGML_TYPE_F32) {
|
||||
int outs[] = { cpy_idx, k };
|
||||
add_node(k);
|
||||
if (ggml_can_fuse_subgraph_ext(cgraph, idxs, n_sub, ops, outs, 2)) {
|
||||
dst = add;
|
||||
resid = r;
|
||||
k += 1;
|
||||
} else {
|
||||
n_sub--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int n = k - i;
|
||||
const int outs[] = { cpy_idx, k - 1 };
|
||||
if (!ggml_can_fuse_subgraph_ext(cgraph, idxs, n_sub, ops, outs, 2)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// the kernel reads past, cur, w, the taps and the residual and writes the state and dst. a thread reads the residual
|
||||
// and cur at a token right before it writes dst there, so dst may be either of them in place when one thread has
|
||||
// the whole sequence; any other overlap is rejected
|
||||
const bool cur_in_place = cur->data == dst->data;
|
||||
if ((cur_in_place && Ts > 64) || (!cur_in_place && ggml_cuda_tensors_overlap(cur, dst)) ||
|
||||
(resid && resid->data != dst->data && ggml_cuda_tensors_overlap(resid, dst)) ||
|
||||
ggml_cuda_tensors_overlap(cell, dst) || (resid && ggml_cuda_tensors_overlap(resid, cell))) {
|
||||
return 0;
|
||||
}
|
||||
for (const ggml_tensor * t : { cell, (const ggml_tensor *) dst }) {
|
||||
for (const ggml_tensor * src : { past, w, dyn }) {
|
||||
if (src && ggml_cuda_tensors_overlap(t, src)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ggml_cuda_tensors_overlap(cell, cur)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ggml_cuda_short_conv_args args = {};
|
||||
args.past = (const float *) past->data;
|
||||
args.past_s1 = past->nb[1]/sizeof(float);
|
||||
args.past_s2 = past->nb[2]/sizeof(float);
|
||||
args.cur = (const float *) cur->data;
|
||||
args.w = (const float *) w->data;
|
||||
args.dyn = dyn ? (const float *) dyn->data : nullptr;
|
||||
args.dyn_s1 = dyn ? dyn->nb[1]/sizeof(float) : 0;
|
||||
args.n_heads = n_heads;
|
||||
args.res = resid ? (const float *) resid->data : nullptr;
|
||||
args.dst = (float *) dst->data;
|
||||
args.state = (float *) cell->data;
|
||||
args.state_slot_s = slot_s;
|
||||
args.state_ch_s = ch_s;
|
||||
args.state_seq_s = seq_s;
|
||||
args.kw = kw;
|
||||
args.run = cur_in_place || Ts <= 8 ? Ts : 1;
|
||||
args.C = C;
|
||||
args.K = K;
|
||||
args.Ts = Ts;
|
||||
args.S = S;
|
||||
args.scale = ggml_get_op_params_f32(sc, 0);
|
||||
args.bias = ggml_get_op_params_f32(sc, 1);
|
||||
|
||||
ggml_cuda_op_short_conv(*cuda_ctx, args);
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
static int ggml_cuda_try_fuse_inkling2(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
switch (cgraph->nodes[i]->op) {
|
||||
case GGML_OP_CONCAT:
|
||||
if (int n = ggml_cuda_try_fuse_short_conv(cuda_ctx, cgraph, i)) {
|
||||
return n;
|
||||
}
|
||||
if (int n = ggml_cuda_try_fuse_loop_ctrl(cuda_ctx, cgraph, i)) {
|
||||
return n;
|
||||
}
|
||||
return ggml_cuda_try_fuse_depth_mix(cuda_ctx, cgraph, i);
|
||||
case GGML_OP_RMS_NORM:
|
||||
return ggml_cuda_try_fuse_depth_mix(cuda_ctx, cgraph, i);
|
||||
case GGML_OP_MUL:
|
||||
case GGML_OP_L2_NORM:
|
||||
return ggml_cuda_try_fuse_l2_clip(cuda_ctx, cgraph, i);
|
||||
case GGML_OP_SCALE:
|
||||
return ggml_cuda_try_fuse_situ(cuda_ctx, cgraph, i);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// try and fuse nodes and return the number of nodes to skip
|
||||
int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
|
||||
if (int n = ggml_cuda_try_fuse_inkling2(cuda_ctx, cgraph, i)) {
|
||||
return n;
|
||||
}
|
||||
|
||||
ggml_tensor * node = cgraph->nodes[i];
|
||||
|
||||
if (node->op == GGML_OP_MUL_MAT_ID && cuda_ctx->stream_context().concurrent_events.empty() &&
|
||||
|
|
|
|||
396
ggml/src/ggml-cuda/inkling2.cu
Normal file
396
ggml/src/ggml-cuda/inkling2.cu
Normal file
|
|
@ -0,0 +1,396 @@
|
|||
#include "inkling2.cuh"
|
||||
|
||||
// the fused kernels keep the operation order of the unfused graph where it is cheap to do so,
|
||||
// __fmul_rn/__fadd_rn stop the compiler from contracting a separate MUL and ADD into one FMA
|
||||
|
||||
static __global__ void situ_f32(
|
||||
const float * gate, const float * up, float * dst, const int ne0, const int ne1, const int ne2, const int64_t nrows,
|
||||
const int64_t g_s1, const int64_t g_s2, const int64_t g_s3, const int64_t u_s1, const int64_t u_s2, const int64_t u_s3,
|
||||
const float s0, const float b0, const float s1, const float b1, const float s2, const float b2, const float s3, const float b3) {
|
||||
ggml_cuda_pdl_lc();
|
||||
const int i0 = blockIdx.x*blockDim.x + threadIdx.x;
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
if (i0 >= ne0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int64_t row = blockIdx.y; row < nrows; row += gridDim.y) {
|
||||
const int64_t i1 = row % ne1;
|
||||
const int64_t i2 = (row / ne1) % ne2;
|
||||
const int64_t i3 = row / ((int64_t) ne1*ne2);
|
||||
|
||||
const float g = gate[i3*g_s3 + i2*g_s2 + i1*g_s1 + i0];
|
||||
const float u = up [i3*u_s3 + i2*u_s2 + i1*u_s1 + i0];
|
||||
|
||||
const float act = s1*tanhf(s0*g + b0) + b1;
|
||||
const float sig = 1.0f/(1.0f + expf(-g));
|
||||
const float lin = s3*tanhf(s2*u + b2) + b3;
|
||||
|
||||
dst[row*ne0 + i0] = __fmul_rn(__fmul_rn(act, sig), lin);
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_cuda_op_situ(ggml_backend_cuda_context & ctx, const ggml_tensor * gate, const ggml_tensor * up,
|
||||
const float * s, const float * b, ggml_tensor * dst) {
|
||||
GGML_ASSERT(gate->type == GGML_TYPE_F32 && up->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(gate->nb[0] == sizeof(float) && up->nb[0] == sizeof(float) && ggml_is_contiguous(dst));
|
||||
GGML_ASSERT(ggml_are_same_shape(gate, up) && ggml_are_same_shape(gate, dst));
|
||||
|
||||
const int64_t ne0 = gate->ne[0];
|
||||
const int64_t nrows = ggml_nrows(gate);
|
||||
|
||||
const int block_size = 256;
|
||||
const dim3 grid((ne0 + block_size - 1)/block_size, std::min<int64_t>(nrows, 65535), 1);
|
||||
|
||||
const ggml_cuda_kernel_launch_params params(grid, dim3(block_size, 1, 1), 0, ctx.stream());
|
||||
ggml_cuda_kernel_launch(situ_f32, params,
|
||||
(const float *) gate->data, (const float *) up->data, (float *) dst->data, (int) ne0, (int) gate->ne[1], (int) gate->ne[2], nrows,
|
||||
(int64_t) (gate->nb[1]/sizeof(float)), (int64_t) (gate->nb[2]/sizeof(float)), (int64_t) (gate->nb[3]/sizeof(float)),
|
||||
(int64_t) (up->nb[1]/sizeof(float)), (int64_t) (up->nb[2]/sizeof(float)), (int64_t) (up->nb[3]/sizeof(float)),
|
||||
s[0], b[0], s[1], b[1], s[2], b[2], s[3], b[3]);
|
||||
}
|
||||
|
||||
// one thread per (channel, run of tokens of one sequence). the last K columns of x stay in registers, right-aligned in
|
||||
// M = GGML_CUDA_SHORT_CONV_MAX_K slots with zero weights in front, so K needs no template and the sums keep their order.
|
||||
// a thread reads cur at a token before it writes dst there and never again, so dst may be cur itself when a thread has
|
||||
// the whole sequence
|
||||
template <bool has_dyn, bool has_res>
|
||||
static __global__ void short_conv_f32(const ggml_cuda_short_conv_args a) {
|
||||
ggml_cuda_pdl_lc();
|
||||
constexpr int M = GGML_CUDA_SHORT_CONV_MAX_K;
|
||||
|
||||
const int K = a.K;
|
||||
const int K1 = K - 1;
|
||||
const int c = blockIdx.x*blockDim.x + threadIdx.x;
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
if (c >= a.C) {
|
||||
return;
|
||||
}
|
||||
|
||||
float w[M];
|
||||
#pragma unroll
|
||||
for (int m = 0; m < M; ++m) {
|
||||
w[m] = m >= M - K ? a.w[c*K + m - (M - K)] : 0.0f;
|
||||
}
|
||||
|
||||
const int n_runs = (a.Ts + a.run - 1)/a.run;
|
||||
for (int r = blockIdx.y; r < a.S*n_runs; r += gridDim.y) {
|
||||
const int s = r / n_runs;
|
||||
const int t0 = (r - s*n_runs)*a.run;
|
||||
const int t1 = min(t0 + a.run, a.Ts);
|
||||
|
||||
const float * pc = a.past + c*a.past_s1 + s*a.past_s2;
|
||||
const float * xc = a.cur + (int64_t) s*a.Ts*a.C + c;
|
||||
|
||||
// x[M - K + k] is column ts + k of [past; cur^T]
|
||||
float x[M];
|
||||
#pragma unroll
|
||||
for (int m = 0; m < M - 1; ++m) {
|
||||
const int j = t0 + m - (M - K);
|
||||
x[m] = j < 0 ? 0.0f : j < K1 ? pc[j] : xc[(int64_t) (j - K1)*a.C];
|
||||
}
|
||||
|
||||
for (int ts = t0; ts < t1; ++ts) {
|
||||
const int64_t t = (int64_t) s*a.Ts + ts;
|
||||
|
||||
x[M - 1] = xc[(int64_t) ts*a.C];
|
||||
|
||||
float y = 0.0f;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < M; ++m) {
|
||||
y += x[m]*w[m];
|
||||
}
|
||||
|
||||
if constexpr (has_dyn) {
|
||||
const float * d = a.dyn + t*a.dyn_s1 + (c / (a.C / a.n_heads))*K;
|
||||
#pragma unroll
|
||||
for (int lag = 0; lag < M; ++lag) {
|
||||
if (lag < K) {
|
||||
y = __fadd_rn(y, __fmul_rn(x[M - 1 - lag], d[lag]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float o = a.scale*__fadd_rn(x[M - 1], y) + a.bias;
|
||||
if constexpr (has_res) {
|
||||
o = __fadd_rn(a.res[t*a.C + c], o);
|
||||
}
|
||||
a.dst[t*a.C + c] = o;
|
||||
|
||||
#pragma unroll
|
||||
for (int m = 0; m < M - 1; ++m) {
|
||||
x[m] = x[m + 1];
|
||||
}
|
||||
|
||||
// x now holds the K - 1 columns that end at token ts: the conv state of slot Ts - 1 - ts, if it is kept
|
||||
if (ts >= a.Ts - a.kw) {
|
||||
float * st = a.state + (a.Ts - 1 - ts)*a.state_slot_s + c*a.state_ch_s + s*a.state_seq_s;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < M - 1; ++m) {
|
||||
if (m >= M - K) {
|
||||
st[m - (M - K)] = x[m];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_cuda_op_short_conv(ggml_backend_cuda_context & ctx, const ggml_cuda_short_conv_args & a) {
|
||||
GGML_ASSERT(a.K >= 2 && a.K <= GGML_CUDA_SHORT_CONV_MAX_K);
|
||||
|
||||
const int block_size = 128;
|
||||
const int n_runs = (a.Ts + a.run - 1)/a.run;
|
||||
const dim3 grid((a.C + block_size - 1)/block_size, std::min(a.S*n_runs, 65535), 1);
|
||||
|
||||
const ggml_cuda_kernel_launch_params params(grid, dim3(block_size, 1, 1), 0, ctx.stream());
|
||||
if (a.dyn) {
|
||||
if (a.res) {
|
||||
ggml_cuda_kernel_launch(short_conv_f32<true, true>, params, a);
|
||||
} else {
|
||||
ggml_cuda_kernel_launch(short_conv_f32<true, false>, params, a);
|
||||
}
|
||||
} else {
|
||||
if (a.res) {
|
||||
ggml_cuda_kernel_launch(short_conv_f32<false, true>, params, a);
|
||||
} else {
|
||||
ggml_cuda_kernel_launch(short_conv_f32<false, false>, params, a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// one block per (query, token): the dot products of the B stack rows and of cur with the query, the softmax over the
|
||||
// B + 1 rows and the weighted sum; the norm of cur is a scale of its dot product
|
||||
template <int block_size>
|
||||
static __global__ void depth_mix_f32(
|
||||
const float * stack, const float * cur, const float * query, float * dst, const int n, const int B, const int Q,
|
||||
const int64_t stack_s1, const int64_t stack_s2, const int64_t cur_s1, const int64_t query_s1,
|
||||
const bool norm_cur, const float eps, const float scale) {
|
||||
ggml_cuda_pdl_lc();
|
||||
constexpr int MAX = GGML_CUDA_DEPTH_MIX_MAX_ROWS;
|
||||
constexpr int NW = block_size/WARP_SIZE;
|
||||
|
||||
const int q = blockIdx.x;
|
||||
const int t = blockIdx.y;
|
||||
const int tid = threadIdx.x;
|
||||
const int lane = tid % WARP_SIZE;
|
||||
const int warp = tid / WARP_SIZE;
|
||||
|
||||
const float * qv = query + q*query_s1;
|
||||
const float * st = stack + t*stack_s2;
|
||||
const float * xc = cur + t*cur_s1;
|
||||
|
||||
// [0, B): the stack rows, MAX: cur, MAX + 1: the sum of squares of cur
|
||||
float acc[MAX + 2];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX + 2; ++r) {
|
||||
acc[r] = 0.0f;
|
||||
}
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
|
||||
for (int i = tid; i < n; i += block_size) {
|
||||
const float qi = qv[i];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX; ++r) {
|
||||
if (r < B) {
|
||||
acc[r] += st[r*stack_s1 + i]*qi;
|
||||
}
|
||||
}
|
||||
const float xi = xc[i];
|
||||
acc[MAX] += xi*qi;
|
||||
acc[MAX + 1] += xi*xi;
|
||||
}
|
||||
|
||||
__shared__ float s_acc[NW][MAX + 2];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX + 2; ++r) {
|
||||
if (r < B || r >= MAX) {
|
||||
acc[r] = warp_reduce_sum(acc[r]);
|
||||
if (lane == 0) {
|
||||
s_acc[warp][r] = acc[r];
|
||||
}
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX + 2; ++r) {
|
||||
if (r < B || r >= MAX) {
|
||||
acc[r] = 0.0f;
|
||||
#pragma unroll
|
||||
for (int w = 0; w < NW; ++w) {
|
||||
acc[r] += s_acc[w][r];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const float sc = norm_cur ? rsqrtf(acc[MAX + 1]/n + eps) : 1.0f;
|
||||
|
||||
float mx = scale*(sc*acc[MAX]);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX; ++r) {
|
||||
if (r < B) {
|
||||
acc[r] = scale*acc[r];
|
||||
mx = fmaxf(mx, acc[r]);
|
||||
}
|
||||
}
|
||||
|
||||
float p_cur = expf(scale*(sc*acc[MAX]) - mx);
|
||||
float sum = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX; ++r) {
|
||||
if (r < B) {
|
||||
acc[r] = expf(acc[r] - mx);
|
||||
sum += acc[r];
|
||||
}
|
||||
}
|
||||
sum += p_cur;
|
||||
|
||||
const float inv = 1.0f/sum;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX; ++r) {
|
||||
acc[r] *= inv;
|
||||
}
|
||||
p_cur *= inv*sc;
|
||||
|
||||
for (int i = tid; i < n; i += block_size) {
|
||||
float o = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < MAX; ++r) {
|
||||
if (r < B) {
|
||||
o += acc[r]*st[r*stack_s1 + i];
|
||||
}
|
||||
}
|
||||
o += p_cur*xc[i];
|
||||
dst[((int64_t) t*n + i)*Q + q] = o;
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_cuda_op_depth_mix(ggml_backend_cuda_context & ctx, const ggml_tensor * stack, const ggml_tensor * cur,
|
||||
bool norm_cur, float eps, const ggml_tensor * query, float scale, ggml_tensor * dst) {
|
||||
const int64_t n = cur->ne[0];
|
||||
const int64_t B = stack->ne[1];
|
||||
const int64_t T = stack->ne[2];
|
||||
const int64_t Q = query->ne[1];
|
||||
|
||||
GGML_ASSERT(B >= 1 && B <= GGML_CUDA_DEPTH_MIX_MAX_ROWS);
|
||||
GGML_ASSERT(stack->nb[0] == sizeof(float) && cur->nb[0] == sizeof(float) && query->nb[0] == sizeof(float));
|
||||
GGML_ASSERT(ggml_is_contiguous(dst) && dst->ne[0] == Q && dst->ne[1] == n && dst->ne[2] == T);
|
||||
GGML_ASSERT(T <= 65535 && Q <= 65535);
|
||||
|
||||
const int block_size = 256;
|
||||
const dim3 grid(Q, T, 1);
|
||||
|
||||
const ggml_cuda_kernel_launch_params params(grid, dim3(block_size, 1, 1), 0, ctx.stream());
|
||||
ggml_cuda_kernel_launch(depth_mix_f32<block_size>, params,
|
||||
(const float *) stack->data, (const float *) cur->data, (const float *) query->data, (float *) dst->data,
|
||||
(int) n, (int) B, (int) Q,
|
||||
(int64_t) (stack->nb[1]/sizeof(float)), (int64_t) (stack->nb[2]/sizeof(float)),
|
||||
(int64_t) (cur->nb[2]/sizeof(float)), (int64_t) (query->nb[1]/sizeof(float)),
|
||||
norm_cur, eps, scale);
|
||||
}
|
||||
|
||||
// one block per (rank row, token): the sums of squares of cur and msg and their dot products with the two halves of the row
|
||||
template <int block_size>
|
||||
static __global__ void loop_ctrl_down_f32(
|
||||
const float * cur, const float * msg, const float * w, float * gate, const int n,
|
||||
const int64_t cur_s1, const int64_t msg_s1, const int64_t w_s1, const int64_t gate_s1, const float eps) {
|
||||
ggml_cuda_pdl_lc();
|
||||
const int r = blockIdx.x;
|
||||
const int t = blockIdx.y;
|
||||
const int tid = threadIdx.x;
|
||||
|
||||
const float * xc = cur + t*cur_s1;
|
||||
const float * xm = msg + t*msg_s1;
|
||||
const float * wr = w + r*w_s1;
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
|
||||
float2 ss = make_float2(0.0f, 0.0f);
|
||||
float2 dd = make_float2(0.0f, 0.0f);
|
||||
for (int i = tid; i < n; i += block_size) {
|
||||
const float c = xc[i];
|
||||
const float m = xm[i];
|
||||
ss.x += c*c;
|
||||
ss.y += m*m;
|
||||
dd.x += wr[i]*c;
|
||||
dd.y += wr[n + i]*m;
|
||||
}
|
||||
|
||||
__shared__ float2 s_ss[32];
|
||||
__shared__ float2 s_dd[32];
|
||||
ss = block_reduce<block_reduce_method::SUM, block_size>(ss, s_ss);
|
||||
dd = block_reduce<block_reduce_method::SUM, block_size>(dd, s_dd);
|
||||
|
||||
if (tid == 0) {
|
||||
gate[t*gate_s1 + r] = rsqrtf(ss.x/n + eps)*dd.x + rsqrtf(ss.y/n + eps)*dd.y;
|
||||
}
|
||||
}
|
||||
|
||||
// one warp per (output channel, token): the gain and forget dot products over the rank and the combine
|
||||
static __global__ void loop_ctrl_up_f32(
|
||||
const float * cur, const float * msg, const float * gate, const float * wg, const float * wf, float * dst,
|
||||
const int n, const int rank, const int64_t cur_s1, const int64_t msg_s1, const int64_t gate_s1,
|
||||
const int64_t wg_s1, const int64_t wf_s1, const int64_t dst_s1) {
|
||||
ggml_cuda_pdl_lc();
|
||||
const int i = blockIdx.x*blockDim.y + threadIdx.y;
|
||||
const int t = blockIdx.y;
|
||||
const int lane = threadIdx.x;
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
if (i >= n) {
|
||||
return;
|
||||
}
|
||||
|
||||
const float * gt = gate + t*gate_s1;
|
||||
|
||||
float g = 0.0f;
|
||||
float f = 0.0f;
|
||||
for (int k = lane; k < rank; k += WARP_SIZE) {
|
||||
const float x = gt[k];
|
||||
g += wg[i*wg_s1 + k]*x;
|
||||
f += wf[i*wf_s1 + k]*x;
|
||||
}
|
||||
g = warp_reduce_sum(g);
|
||||
f = warp_reduce_sum(f);
|
||||
|
||||
if (lane == 0) {
|
||||
const float c = cur[t*cur_s1 + i];
|
||||
const float m = msg[t*msg_s1 + i];
|
||||
const float a = __fsub_rn(c, __fmul_rn(tanhf(f), c));
|
||||
dst[t*dst_s1 + i] = __fadd_rn(a, __fmul_rn(tanhf(g), m));
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_cuda_op_loop_ctrl(ggml_backend_cuda_context & ctx, const ggml_tensor * cur, const ggml_tensor * msg, float eps,
|
||||
const ggml_tensor * w_down, const ggml_tensor * w_gain, const ggml_tensor * w_forget, ggml_tensor * gate, ggml_tensor * dst) {
|
||||
const int64_t n = cur->ne[0];
|
||||
const int64_t T = cur->ne[1];
|
||||
const int64_t rank = w_down->ne[1];
|
||||
|
||||
GGML_ASSERT(cur->nb[0] == sizeof(float) && msg->nb[0] == sizeof(float) && gate->nb[0] == sizeof(float) && dst->nb[0] == sizeof(float));
|
||||
GGML_ASSERT(w_down->ne[0] == 2*n && w_gain->ne[0] == rank && w_forget->ne[0] == rank && w_gain->ne[1] == n && w_forget->ne[1] == n);
|
||||
GGML_ASSERT(T <= GGML_CUDA_LOOP_CTRL_MAX_TOKENS);
|
||||
|
||||
cudaStream_t stream = ctx.stream();
|
||||
|
||||
{
|
||||
const int block_size = 256;
|
||||
const ggml_cuda_kernel_launch_params params(dim3(rank, T, 1), dim3(block_size, 1, 1), 0, stream);
|
||||
ggml_cuda_kernel_launch(loop_ctrl_down_f32<block_size>, params,
|
||||
(const float *) cur->data, (const float *) msg->data, (const float *) w_down->data, (float *) gate->data, (int) n,
|
||||
(int64_t) (cur->nb[1]/sizeof(float)), (int64_t) (msg->nb[1]/sizeof(float)),
|
||||
(int64_t) (w_down->nb[1]/sizeof(float)), (int64_t) (gate->nb[1]/sizeof(float)), eps);
|
||||
}
|
||||
{
|
||||
const int rows_per_block = 8;
|
||||
const ggml_cuda_kernel_launch_params params(dim3((n + rows_per_block - 1)/rows_per_block, T, 1), dim3(WARP_SIZE, rows_per_block, 1), 0, stream);
|
||||
ggml_cuda_kernel_launch(loop_ctrl_up_f32, params,
|
||||
(const float *) cur->data, (const float *) msg->data, (const float *) gate->data,
|
||||
(const float *) w_gain->data, (const float *) w_forget->data, (float *) dst->data, (int) n, (int) rank,
|
||||
(int64_t) (cur->nb[1]/sizeof(float)), (int64_t) (msg->nb[1]/sizeof(float)), (int64_t) (gate->nb[1]/sizeof(float)),
|
||||
(int64_t) (w_gain->nb[1]/sizeof(float)), (int64_t) (w_forget->nb[1]/sizeof(float)), (int64_t) (dst->nb[1]/sizeof(float)));
|
||||
}
|
||||
}
|
||||
55
ggml/src/ggml-cuda/inkling2.cuh
Normal file
55
ggml/src/ggml-cuda/inkling2.cuh
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include "common.cuh"
|
||||
|
||||
// fused op chains of the Inkling 2 graph
|
||||
|
||||
// largest conv width and stack depth the fused kernels handle
|
||||
#define GGML_CUDA_SHORT_CONV_MAX_K 8
|
||||
#define GGML_CUDA_DEPTH_MIX_MAX_ROWS 16
|
||||
|
||||
// the most tokens the fused loop controller handles, larger batches use the matrix multiplications
|
||||
#define GGML_CUDA_LOOP_CTRL_MAX_TOKENS 8
|
||||
|
||||
// situ(g, u) = (s1*tanh(s0*g + b0) + b1)*sigmoid(g)*(s3*tanh(s2*u + b2) + b3), with s and b of the four SCALE nodes
|
||||
void ggml_cuda_op_situ(ggml_backend_cuda_context & ctx, const ggml_tensor * gate, const ggml_tensor * up,
|
||||
const float * s, const float * b, ggml_tensor * dst);
|
||||
|
||||
// causal short conv: x = [past; cur^T], y = conv(x, w) (+ input-dependent taps per head), dst = s*(cur + y) + b (+ res)
|
||||
// the K - 1 columns of x that end at token Ts - 1 - slot are the conv state of slot, for the last kw slots (kw = 1 without rollback)
|
||||
// each thread does `run` tokens of a sequence in order, run == Ts lets dst be cur
|
||||
struct ggml_cuda_short_conv_args {
|
||||
const float * past; // [K - 1, C, S], strides in elements
|
||||
int64_t past_s1;
|
||||
int64_t past_s2;
|
||||
const float * cur; // [C, Ts*S], contiguous
|
||||
const float * w; // [K, C], contiguous
|
||||
const float * dyn; // [K*H, Ts*S] taps or nullptr
|
||||
int64_t dyn_s1;
|
||||
int n_heads;
|
||||
const float * res; // [C, Ts*S], contiguous, or nullptr
|
||||
float * dst; // [C, Ts*S], contiguous
|
||||
float * state; // [K - 1, slot, C, S], strides in elements
|
||||
int64_t state_slot_s;
|
||||
int64_t state_ch_s;
|
||||
int64_t state_seq_s;
|
||||
int kw;
|
||||
int run;
|
||||
int C;
|
||||
int K;
|
||||
int Ts;
|
||||
int S;
|
||||
float scale;
|
||||
float bias;
|
||||
};
|
||||
|
||||
void ggml_cuda_op_short_conv(ggml_backend_cuda_context & ctx, const ggml_cuda_short_conv_args & args);
|
||||
|
||||
// depth mix: states = [stack; rms_norm(cur)], p = softmax(scale*(states . query)), dst = p . states, written as [Q, n, T]
|
||||
// norm_cur false: cur is already normalized
|
||||
void ggml_cuda_op_depth_mix(ggml_backend_cuda_context & ctx, const ggml_tensor * stack, const ggml_tensor * cur,
|
||||
bool norm_cur, float eps, const ggml_tensor * query, float scale, ggml_tensor * dst);
|
||||
|
||||
// loop controller: gate = w_down . [rms_norm(cur); rms_norm(msg)], dst = cur - tanh(w_forget . gate)*cur + tanh(w_gain . gate)*msg
|
||||
void ggml_cuda_op_loop_ctrl(ggml_backend_cuda_context & ctx, const ggml_tensor * cur, const ggml_tensor * msg, float eps,
|
||||
const ggml_tensor * w_down, const ggml_tensor * w_gain, const ggml_tensor * w_forget, ggml_tensor * gate, ggml_tensor * dst);
|
||||
|
|
@ -399,6 +399,54 @@ static __global__ void l2_norm_f32(
|
|||
}
|
||||
}
|
||||
|
||||
// l2_norm with an optional per-column weight before it and SCALE (+ an optional MUL by a per-channel/sample factor) after it,
|
||||
// with the reduction of l2_norm_f32
|
||||
template <int block_size, bool has_w, bool has_f>
|
||||
static __global__ void l2_clip_f32(
|
||||
const float * x, const float * w, const float * f, float * dst, const int ncols, const int nchannels, const int nsamples,
|
||||
const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample,
|
||||
const int64_t w_stride_row, const int64_t w_stride_channel, const int64_t w_stride_sample,
|
||||
const int64_t f_stride_channel, const int64_t f_stride_sample, const float eps, const float scale, const float bias) {
|
||||
ggml_cuda_pdl_lc();
|
||||
const int nrows = gridDim.x;
|
||||
const int row = blockIdx.x;
|
||||
const int tid = threadIdx.x;
|
||||
|
||||
extern __shared__ float s_sum[];
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
|
||||
for (int sample = blockIdx.z; sample < nsamples; sample += gridDim.z) {
|
||||
for (int channel = blockIdx.y; channel < nchannels; channel += gridDim.y) {
|
||||
const float * xc = x + sample*stride_sample + channel*stride_channel + row*stride_row;
|
||||
const float * wc = has_w ? w + sample*w_stride_sample + channel*w_stride_channel + row*w_stride_row : nullptr;
|
||||
float * dstc = dst + ((sample*nchannels + channel)*nrows + row)*ncols;
|
||||
|
||||
float tmp = 0.0f;
|
||||
|
||||
for (int col = tid; col < ncols; col += block_size) {
|
||||
const float xi = has_w ? xc[col]*wc[col] : xc[col];
|
||||
tmp += xi * xi;
|
||||
}
|
||||
|
||||
tmp = block_reduce<block_reduce_method::SUM, block_size>(tmp, s_sum);
|
||||
|
||||
const float l2 = rsqrtf(fmaxf(tmp, eps * eps));
|
||||
const float fc = has_f ? f[sample*f_stride_sample + channel*f_stride_channel] : 1.0f;
|
||||
|
||||
for (int col = tid; col < ncols; col += block_size) {
|
||||
const float xi = has_w ? xc[col]*wc[col] : xc[col];
|
||||
const float yi = scale*(l2*xi) + bias;
|
||||
dstc[col] = has_f ? yi*fc : yi;
|
||||
}
|
||||
|
||||
if constexpr (block_size > WARP_SIZE) {
|
||||
__syncthreads();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <bool do_multiply = false, bool do_add = false>
|
||||
static void norm_f32_cuda(const float * x,
|
||||
float * dst,
|
||||
|
|
@ -623,6 +671,71 @@ static void l2_norm_f32_cuda(
|
|||
}
|
||||
}
|
||||
|
||||
template <bool has_w, bool has_f>
|
||||
static void l2_clip_f32_cuda(
|
||||
const float * x, const float * w, const float * f, float * dst, const int ncols, const int nrows, const int nchannels, const int nsamples,
|
||||
const int64_t stride_row, const int64_t stride_channel, const int64_t stride_sample,
|
||||
const int64_t w_stride_row, const int64_t w_stride_channel, const int64_t w_stride_sample,
|
||||
const int64_t f_stride_channel, const int64_t f_stride_sample, const float eps, const float scale, const float bias, cudaStream_t stream) {
|
||||
const dim3 blocks_num(nrows, MIN(nchannels, UINT16_MAX), MIN(nsamples, UINT16_MAX));
|
||||
if (ncols < 1024) {
|
||||
const ggml_cuda_kernel_launch_params params(blocks_num, dim3(WARP_SIZE, 1, 1), 0, stream);
|
||||
ggml_cuda_kernel_launch(l2_clip_f32<WARP_SIZE, has_w, has_f>, params, x, w, f, dst, ncols, nchannels, nsamples,
|
||||
stride_row, stride_channel, stride_sample, w_stride_row, w_stride_channel, w_stride_sample,
|
||||
f_stride_channel, f_stride_sample, eps, scale, bias);
|
||||
} else {
|
||||
const ggml_cuda_kernel_launch_params params(blocks_num, dim3(1024, 1, 1), 32 * sizeof(float), stream);
|
||||
ggml_cuda_kernel_launch(l2_clip_f32<1024, has_w, has_f>, params, x, w, f, dst, ncols, nchannels, nsamples,
|
||||
stride_row, stride_channel, stride_sample, w_stride_row, w_stride_channel, w_stride_sample,
|
||||
f_stride_channel, f_stride_sample, eps, scale, bias);
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_cuda_op_l2_clip(ggml_backend_cuda_context & ctx, const ggml_tensor * x, const ggml_tensor * w, const ggml_tensor * l2_norm,
|
||||
const ggml_tensor * scale, const ggml_tensor * f, ggml_tensor * dst) {
|
||||
GGML_ASSERT(x->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 && x->nb[0] == sizeof(float));
|
||||
GGML_ASSERT(ggml_is_contiguous(dst) && ggml_are_same_shape(x, dst));
|
||||
|
||||
GGML_TENSOR_LOCALS(int64_t, ne0, x, ne);
|
||||
GGML_TENSOR_LOCALS(size_t, nb0, x, nb);
|
||||
|
||||
// broadcast dimensions of w and f have a stride of 0
|
||||
auto bcast_stride = [](const ggml_tensor * t, int dim) -> int64_t {
|
||||
return t->ne[dim] == 1 ? 0 : (int64_t) (t->nb[dim]/sizeof(float));
|
||||
};
|
||||
|
||||
const int64_t ws1 = w ? bcast_stride(w, 1) : 0;
|
||||
const int64_t ws2 = w ? bcast_stride(w, 2) : 0;
|
||||
const int64_t ws3 = w ? bcast_stride(w, 3) : 0;
|
||||
const int64_t fs2 = f ? bcast_stride(f, 2) : 0;
|
||||
const int64_t fs3 = f ? bcast_stride(f, 3) : 0;
|
||||
|
||||
const float * x_d = (const float *) x->data;
|
||||
const float * w_d = w ? (const float *) w->data : nullptr;
|
||||
const float * f_d = f ? (const float *) f->data : nullptr;
|
||||
float * d_d = (float *) dst->data;
|
||||
|
||||
const float eps = ggml_get_op_params_f32(l2_norm, 0);
|
||||
const float s = ggml_get_op_params_f32(scale, 0);
|
||||
const float b = ggml_get_op_params_f32(scale, 1);
|
||||
|
||||
const int64_t s01 = nb01/sizeof(float);
|
||||
const int64_t s02 = nb02/sizeof(float);
|
||||
const int64_t s03 = nb03/sizeof(float);
|
||||
|
||||
cudaStream_t stream = ctx.stream();
|
||||
|
||||
if (w && f) {
|
||||
l2_clip_f32_cuda<true, true >(x_d, w_d, f_d, d_d, ne00, ne01, ne02, ne03, s01, s02, s03, ws1, ws2, ws3, fs2, fs3, eps, s, b, stream);
|
||||
} else if (w) {
|
||||
l2_clip_f32_cuda<true, false>(x_d, w_d, f_d, d_d, ne00, ne01, ne02, ne03, s01, s02, s03, ws1, ws2, ws3, fs2, fs3, eps, s, b, stream);
|
||||
} else if (f) {
|
||||
l2_clip_f32_cuda<false, true >(x_d, w_d, f_d, d_d, ne00, ne01, ne02, ne03, s01, s02, s03, ws1, ws2, ws3, fs2, fs3, eps, s, b, stream);
|
||||
} else {
|
||||
l2_clip_f32_cuda<false, false>(x_d, w_d, f_d, d_d, ne00, ne01, ne02, ne03, s01, s02, s03, ws1, ws2, ws3, fs2, fs3, eps, s, b, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_cuda_op_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst, ggml_tensor * mul, ggml_tensor * add) {
|
||||
const ggml_tensor * src0 = dst->src[0];
|
||||
const float * src0_d = (const float *) src0->data;
|
||||
|
|
|
|||
|
|
@ -21,3 +21,7 @@ void ggml_cuda_op_rms_norm_fused_add(ggml_backend_cuda_context & ctx,
|
|||
void ggml_cuda_op_rms_norm_back(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
||||
|
||||
void ggml_cuda_op_l2_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
||||
|
||||
// [MUL by w ->] L2_NORM -> SCALE [-> MUL by f], w broadcasts over the rows, f is one value per channel/sample
|
||||
void ggml_cuda_op_l2_clip(ggml_backend_cuda_context & ctx, const ggml_tensor * x, const ggml_tensor * w, const ggml_tensor * l2_norm,
|
||||
const ggml_tensor * scale, const ggml_tensor * f, ggml_tensor * dst);
|
||||
|
|
|
|||
|
|
@ -4834,6 +4834,297 @@ struct test_unary_scale_fusion : public test_case {
|
|||
// SNAKE activation fusion: y = x + sin(a*x)^2 * inv_b
|
||||
// CUDA backend matches the naive 5-op chain (mul, sin, sqr, mul, add)
|
||||
// and dispatches a single fused kernel.
|
||||
// the situ activation of the Inkling 2 experts on the halves of a merged gate_up, as build_moe_ffn builds it
|
||||
struct test_situ : public test_case {
|
||||
const std::array<int64_t, 3> ne; // n_ff, n_expert_used, n_tokens
|
||||
const float beta;
|
||||
const float lb;
|
||||
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "SITU";
|
||||
}
|
||||
|
||||
bool run_whole_graph() override { return true; }
|
||||
|
||||
std::string vars() override {
|
||||
return VARS_TO_STR3(ne, beta, lb);
|
||||
}
|
||||
|
||||
test_situ(std::array<int64_t, 3> ne = {1152, 8, 1}, float beta = 4.0f, float lb = 25.0f) : ne(ne), beta(beta), lb(lb) {}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx) override {
|
||||
ggml_tensor * gu = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 2*ne[0], ne[1], ne[2]);
|
||||
ggml_set_name(gu, "gate_up");
|
||||
gu = ggml_scale(ctx, gu, 1.0f);
|
||||
|
||||
ggml_tensor * g = ggml_view_3d(ctx, gu, ne[0], ne[1], ne[2], gu->nb[1], gu->nb[2], 0);
|
||||
ggml_tensor * u = ggml_view_3d(ctx, gu, ne[0], ne[1], ne[2], gu->nb[1], gu->nb[2], ne[0]*gu->nb[0]);
|
||||
|
||||
ggml_tensor * act = ggml_scale(ctx, ggml_tanh(ctx, ggml_scale(ctx, g, 1.0f/beta)), beta);
|
||||
act = ggml_mul(ctx, act, ggml_sigmoid(ctx, g));
|
||||
u = ggml_scale(ctx, ggml_tanh(ctx, ggml_scale(ctx, u, 1.0f/lb)), lb);
|
||||
ggml_tensor * out = ggml_mul(ctx, act, u);
|
||||
ggml_set_name(out, "out");
|
||||
return out;
|
||||
}
|
||||
|
||||
void initialize_tensors(ggml_context * ctx) override {
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
init_tensor_uniform(t, -40.0f, 40.0f);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// the row L2 clip of Inkling 2: [x*w ->] l2_norm(x, t)*t [-> *f with one f per token]
|
||||
struct test_l2_clip : public test_case {
|
||||
const std::array<int64_t, 4> ne;
|
||||
const bool with_w;
|
||||
const bool with_f;
|
||||
const float thr;
|
||||
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "L2_CLIP";
|
||||
}
|
||||
|
||||
bool run_whole_graph() override { return true; }
|
||||
|
||||
std::string vars() override {
|
||||
return VARS_TO_STR4(ne, with_w, with_f, thr);
|
||||
}
|
||||
|
||||
test_l2_clip(std::array<int64_t, 4> ne = {128, 24, 3, 1}, bool with_w = true, bool with_f = false, float thr = 22.6f)
|
||||
: ne(ne), with_w(with_w), with_f(with_f), thr(thr) {}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx) override {
|
||||
ggml_tensor * a = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne.data());
|
||||
ggml_set_name(a, "a");
|
||||
ggml_tensor * cur = ggml_scale(ctx, a, 1.0f);
|
||||
if (with_w) {
|
||||
ggml_tensor * w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne[0], ne[1]);
|
||||
ggml_set_name(w, "w");
|
||||
cur = ggml_mul(ctx, cur, w);
|
||||
}
|
||||
cur = ggml_scale(ctx, ggml_l2_norm(ctx, cur, thr), thr);
|
||||
if (with_f) {
|
||||
ggml_tensor * f = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, 1, 1, ne[2], ne[3]);
|
||||
ggml_set_name(f, "f");
|
||||
cur = ggml_mul(ctx, cur, f);
|
||||
}
|
||||
ggml_set_name(cur, "out");
|
||||
return cur;
|
||||
}
|
||||
|
||||
void initialize_tensors(ggml_context * ctx) override {
|
||||
// rows with norms both below and above the threshold
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
init_tensor_uniform(t, -4.0f, 4.0f);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// the causal short conv of Inkling 2 as convolve() builds it: CONCAT(past, cur^T), the state store (a CPY of the last
|
||||
// columns, or SET_ROWS of the windows of n_slots rollback slots), SSM_CONV, the optional input-dependent taps of H heads,
|
||||
// the residual ADD and SCALE, and an optional residual add after it
|
||||
struct test_short_conv : public test_case {
|
||||
const int64_t C, Ts, S, H, n_slots;
|
||||
const bool res;
|
||||
const int64_t K; // the conv width
|
||||
|
||||
std::vector<ggml_tensor *> verify;
|
||||
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "SHORT_CONV";
|
||||
}
|
||||
|
||||
bool run_whole_graph() override { return true; }
|
||||
std::vector<ggml_tensor *> fusion_test_nodes() override { return verify; }
|
||||
|
||||
std::string vars() override {
|
||||
return VARS_TO_STR7(C, Ts, S, H, n_slots, res, K);
|
||||
}
|
||||
|
||||
test_short_conv(int64_t C = 1024, int64_t Ts = 1, int64_t S = 1, int64_t H = 0, int64_t n_slots = 0, bool res = false, int64_t K = 4)
|
||||
: C(C), Ts(Ts), S(S), H(H), n_slots(n_slots), res(res), K(K) {}
|
||||
|
||||
// the inputs only: the rollback windows are an overlapping view
|
||||
void initialize_tensors(ggml_context * ctx) override {
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
if (t->op == GGML_OP_NONE && t->view_src == NULL) {
|
||||
init_tensor_uniform(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx) override {
|
||||
verify.clear();
|
||||
const int64_t K1 = K - 1;
|
||||
const int64_t T = Ts*S;
|
||||
|
||||
ggml_tensor * hist = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, K1, C + 64, S);
|
||||
ggml_tensor * past = ggml_view_3d(ctx, hist, K1, C, S, hist->nb[1], hist->nb[2], 32*hist->nb[1]);
|
||||
ggml_tensor * cur = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, C, T), 0.5f);
|
||||
ggml_tensor * w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, K, C);
|
||||
ggml_set_name(hist, "hist");
|
||||
|
||||
ggml_tensor * dyn = nullptr;
|
||||
if (H) {
|
||||
dyn = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, K*H, T), 1.0f);
|
||||
if (gf) {
|
||||
ggml_build_forward_expand(gf, dyn);
|
||||
}
|
||||
}
|
||||
|
||||
const int64_t n_planes = std::max<int64_t>(1, n_slots);
|
||||
ggml_tensor * state = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, K1*(C + 64), S*n_planes);
|
||||
ggml_set_name(state, "state");
|
||||
const size_t row_size = state->nb[1];
|
||||
|
||||
ggml_tensor * x = ggml_concat(ctx, past, ggml_transpose(ctx, ggml_reshape_3d(ctx, cur, C, Ts, S)), 0);
|
||||
|
||||
ggml_tensor * st;
|
||||
if (n_slots == 0) {
|
||||
ggml_tensor * tail = ggml_view_3d(ctx, x, K1, C, S, x->nb[1], x->nb[2], Ts*sizeof(float));
|
||||
ggml_tensor * cell = ggml_view_2d(ctx, state, K1*C, S, row_size, 32*K1*sizeof(float));
|
||||
st = ggml_cpy(ctx, tail, cell);
|
||||
} else {
|
||||
const int64_t kw = std::min(Ts, n_slots);
|
||||
ggml_tensor * order = ggml_arange(ctx, 0.0f, (float) n_slots, 1.0f);
|
||||
order = ggml_cast(ctx, ggml_scale_bias(ctx, order, -1.0f, (float) (n_slots - 1)), GGML_TYPE_I32);
|
||||
order = ggml_view_1d(ctx, order, kw, (n_slots - kw)*sizeof(int32_t));
|
||||
ggml_tensor * windows = ggml_view_4d(ctx, x, K1, kw, C, S, sizeof(float), x->nb[1], x->nb[2], (Ts - (kw - 1))*sizeof(float));
|
||||
ggml_tensor * slots = ggml_view_4d(ctx, state, K1, n_slots, C, S, S*row_size, K1*sizeof(float), row_size, 32*K1*sizeof(float));
|
||||
st = ggml_set_rows(ctx, slots, windows, order);
|
||||
}
|
||||
if (gf) {
|
||||
ggml_build_forward_expand(gf, st);
|
||||
}
|
||||
verify.push_back(st);
|
||||
|
||||
ggml_tensor * out = ggml_reshape_2d(ctx, ggml_ssm_conv(ctx, x, w), C, T);
|
||||
if (dyn) {
|
||||
ggml_tensor * d3 = ggml_reshape_3d(ctx, dyn, K, H, T);
|
||||
for (int64_t lag = 0; lag < K; ++lag) {
|
||||
ggml_tensor * tap = ggml_view_3d(ctx, d3, 1, H, T, d3->nb[1], d3->nb[2], lag*d3->nb[0]);
|
||||
ggml_tensor * window = ggml_view_3d(ctx, x, Ts, C, S, x->nb[1], x->nb[2], (K1 - lag)*x->nb[0]);
|
||||
window = ggml_reshape_3d(ctx, ggml_cont(ctx, ggml_transpose(ctx, window)), C/H, H, T);
|
||||
out = ggml_add(ctx, out, ggml_reshape_2d(ctx, ggml_mul(ctx, window, tap), C, T));
|
||||
}
|
||||
}
|
||||
out = ggml_scale(ctx, ggml_add(ctx, cur, out), 1.0f/sqrtf((float) ((H ? 2 : 1)*K + 1)));
|
||||
if (res) {
|
||||
out = ggml_add(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, C, T), out);
|
||||
}
|
||||
ggml_set_name(out, "out");
|
||||
verify.push_back(out);
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
// the depth mix of Inkling 2 as mix() builds it: states = [stack; rms_norm(cur)], softmax of the scores against Q queries,
|
||||
// the weighted sum as [Q, n, T]; shared_norm: the norm of cur also has another use, so it is computed before the mix
|
||||
struct test_depth_mix : public test_case {
|
||||
const int64_t n, B, Q, T;
|
||||
const bool shared_norm;
|
||||
|
||||
std::vector<ggml_tensor *> verify;
|
||||
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "DEPTH_MIX";
|
||||
}
|
||||
|
||||
bool run_whole_graph() override { return true; }
|
||||
std::vector<ggml_tensor *> fusion_test_nodes() override { return verify; }
|
||||
|
||||
std::string vars() override {
|
||||
return VARS_TO_STR5(n, B, Q, T, shared_norm);
|
||||
}
|
||||
|
||||
test_depth_mix(int64_t n = 2304, int64_t B = 8, int64_t Q = 1, int64_t T = 1, bool shared_norm = false)
|
||||
: n(n), B(B), Q(Q), T(T), shared_norm(shared_norm) {}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx) override {
|
||||
verify.clear();
|
||||
ggml_tensor * stack = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n, B, T);
|
||||
ggml_tensor * cur = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, T);
|
||||
ggml_tensor * query = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, Q);
|
||||
ggml_set_name(stack, "stack");
|
||||
ggml_set_name(cur, "cur");
|
||||
|
||||
ggml_tensor * norm = ggml_rms_norm(ctx, ggml_reshape_3d(ctx, cur, n, 1, T), 1e-6f);
|
||||
if (shared_norm) {
|
||||
ggml_tensor * other = ggml_scale(ctx, norm, 2.0f);
|
||||
if (gf) {
|
||||
ggml_build_forward_expand(gf, other);
|
||||
}
|
||||
verify.push_back(other);
|
||||
}
|
||||
|
||||
ggml_tensor * states = ggml_concat(ctx, stack, norm, 1);
|
||||
ggml_tensor * queries = T == 1 ? query : ggml_repeat(ctx, query, ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n, Q, T));
|
||||
ggml_tensor * scores = ggml_soft_max_ext(ctx, ggml_mul_mat(ctx, states, queries), nullptr, 1.0f/sqrtf((float) n), 0.0f);
|
||||
ggml_tensor * out = ggml_mul_mat(ctx, scores, ggml_cont(ctx, ggml_transpose(ctx, states)));
|
||||
ggml_set_name(out, "out");
|
||||
verify.push_back(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void initialize_tensors(ggml_context * ctx) override {
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
init_tensor_uniform(t, -3.0f, 3.0f);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// the loop controller of Inkling 2 as control() builds it: one rms_norm of [cur; msg], the rank r down projection, the gain
|
||||
// and forget projections and (cur - tanh(forget)*cur) + tanh(gain)*msg
|
||||
struct test_loop_ctrl : public test_case {
|
||||
const int64_t n, r, T;
|
||||
|
||||
std::string op_desc(ggml_tensor * t) override {
|
||||
GGML_UNUSED(t);
|
||||
return "LOOP_CTRL";
|
||||
}
|
||||
|
||||
bool run_whole_graph() override { return true; }
|
||||
|
||||
std::string vars() override {
|
||||
return VARS_TO_STR3(n, r, T);
|
||||
}
|
||||
|
||||
test_loop_ctrl(int64_t n = 2304, int64_t r = 64, int64_t T = 1) : n(n), r(r), T(T) {}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx) override {
|
||||
ggml_tensor * cur = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, T), 1.0f);
|
||||
ggml_tensor * msg = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, T), 1.0f);
|
||||
ggml_tensor * wd = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 2*n, r);
|
||||
ggml_tensor * wg = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, r, n);
|
||||
ggml_tensor * wf = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, r, n);
|
||||
ggml_set_name(cur, "cur");
|
||||
ggml_set_name(msg, "msg");
|
||||
|
||||
ggml_tensor * gate = ggml_concat(ctx, ggml_reshape_3d(ctx, cur, n, 1, T), ggml_reshape_3d(ctx, msg, n, 1, T), 1);
|
||||
gate = ggml_reshape_2d(ctx, ggml_rms_norm(ctx, gate, 1e-6f), 2*n, T);
|
||||
gate = ggml_mul_mat(ctx, wd, gate);
|
||||
|
||||
ggml_tensor * gain = ggml_tanh(ctx, ggml_mul_mat(ctx, wg, gate));
|
||||
ggml_tensor * forget = ggml_tanh(ctx, ggml_mul_mat(ctx, wf, gate));
|
||||
|
||||
ggml_tensor * out = ggml_add(ctx, ggml_sub(ctx, cur, ggml_mul(ctx, forget, cur)), ggml_mul(ctx, gain, msg));
|
||||
ggml_set_name(out, "out");
|
||||
return out;
|
||||
}
|
||||
|
||||
void initialize_tensors(ggml_context * ctx) override {
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
init_tensor_uniform(t, -0.5f, 0.5f);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct test_snake_fuse : public test_case {
|
||||
const ggml_type type;
|
||||
const std::array<int64_t, 4> ne; // [T, C, D2, D3]
|
||||
|
|
@ -11746,6 +12037,47 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
|
|||
test_cases.emplace_back(new test_snake_fuse(type, { 64, 32, 2, 3})); // ne[2] > 1 and ne[3] > 1
|
||||
}
|
||||
|
||||
// the fused op chains of the Inkling 2 graph
|
||||
for (int64_t n_tokens : { 1, 3, 32 }) {
|
||||
test_cases.emplace_back(new test_situ({1152, 8, n_tokens}, 4.0f, 25.0f));
|
||||
}
|
||||
test_cases.emplace_back(new test_situ({64, 2, 5}, 2.0f, 10.0f));
|
||||
for (bool with_w : { false, true }) {
|
||||
for (bool with_f : { false, true }) {
|
||||
for (std::array<int64_t, 4> ne : std::initializer_list<std::array<int64_t, 4>>{ {128, 24, 1, 1}, {128, 4, 7, 1}, {16, 24, 3, 1}, {1500, 2, 3, 2} }) {
|
||||
test_cases.emplace_back(new test_l2_clip(ne, with_w, with_f, ne[0] == 16 ? 8.0f : 22.6f));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int64_t H : { 0, 8 }) {
|
||||
for (int64_t n_slots : { 0, 4 }) {
|
||||
for (bool res : { false, true }) {
|
||||
for (int64_t Ts : { 1, 3, 5, 33 }) {
|
||||
for (int64_t S : { 1, 2 }) {
|
||||
test_cases.emplace_back(new test_short_conv(1024, Ts, S, H, n_slots, res));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int64_t K : { 2, 3, 8 }) {
|
||||
test_cases.emplace_back(new test_short_conv(256, 5, 2, 4, 0, true, K));
|
||||
test_cases.emplace_back(new test_short_conv(256, 3, 1, 0, 6, false, K));
|
||||
}
|
||||
for (int64_t T : { 1, 3 }) {
|
||||
for (int64_t Q : { 1, 3, 4 }) {
|
||||
for (int64_t B : { 1, 3, 12 }) {
|
||||
test_cases.emplace_back(new test_depth_mix(2304, B, Q, T));
|
||||
}
|
||||
}
|
||||
test_cases.emplace_back(new test_depth_mix(2304, 5, 3, T, true));
|
||||
test_cases.emplace_back(new test_depth_mix(100, 2, 2, T));
|
||||
}
|
||||
for (int64_t T : { 1, 4, 8 }) {
|
||||
test_cases.emplace_back(new test_loop_ctrl(2304, 64, T));
|
||||
}
|
||||
test_cases.emplace_back(new test_loop_ctrl(256, 40, 3));
|
||||
|
||||
test_cases.emplace_back(new test_dsv4_hc_comb(1, 1));
|
||||
test_cases.emplace_back(new test_dsv4_hc_comb(17, 4));
|
||||
test_cases.emplace_back(new test_dsv4_hc_comb(257, 8));
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue