Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ggml/src/ggml-cuda/fusion.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1705,8 +1705,11 @@ static int ggml_cuda_try_fuse_depth_mix(ggml_backend_cuda_context * cuda_ctx, gg
const ggml_tensor * cur = has_norm ? node->src[0] : cc->src[1];
const ggml_tensor * query = has_rep ? rep->src[0] : mm1->src[1];

// the result as [Q, ne, T] (scores x states^T) or as [ne, Q, T] (states^T x scores)
const bool out_nq = mm2->src[0] == ct && mm2->src[1] == sm;
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) {
ggml_get_op_params_f32(sm, 1) != 0.0f || tr->src[0] != cc || ct->src[0] != tr ||
!(out_nq || (mm2->src[0] == sm && mm2->src[1] == ct))) {
return 0;
}

Expand All @@ -1723,7 +1726,7 @@ static int ggml_cuda_try_fuse_depth_mix(ggml_backend_cuda_context * cuda_ctx, gg
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)) {
mm2->ne[0] != (out_nq ? ne : Q) || mm2->ne[1] != (out_nq ? Q : ne) || mm2->ne[2] != T || !ggml_is_contiguous(mm2)) {
return 0;
}

Expand All @@ -1733,7 +1736,7 @@ static int ggml_cuda_try_fuse_depth_mix(ggml_backend_cuda_context * cuda_ctx, gg
}

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);
ggml_cuda_op_depth_mix(*cuda_ctx, stack, cur, has_norm, eps, query, ggml_get_op_params_f32(sm, 0), mm2, out_nq);
return n - 1;
}

Expand Down
37 changes: 25 additions & 12 deletions ggml/src/ggml-cuda/inkling2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void ggml_cuda_op_short_conv(ggml_backend_cuda_context & ctx, const ggml_cuda_sh

// 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>
template <int block_size, bool out_nq>
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,
Expand Down Expand Up @@ -265,32 +265,45 @@ static __global__ void depth_mix_f32(
}
}
o += p_cur*xc[i];
dst[((int64_t) t*n + i)*Q + q] = o;
if constexpr (out_nq) {
dst[((int64_t) t*Q + q)*n + i] = o;
} else {
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) {
bool norm_cur, float eps, const ggml_tensor * query, float scale, ggml_tensor * dst, bool out_nq) {
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(ggml_is_contiguous(dst) && dst->ne[0] == (out_nq ? n : Q) && dst->ne[1] == (out_nq ? Q : 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);
auto launch = [&](auto kernel, int block_size) {
const ggml_cuda_kernel_launch_params params(grid, dim3(block_size, 1, 1), 0, ctx.stream());
ggml_cuda_kernel_launch(kernel, 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);
};

// few blocks (decode): wide blocks, so that each thread loops over fewer elements of the rows
const bool wide = Q*T <= ggml_cuda_info().devices[ggml_cuda_get_device()].nsm && n >= 2048;
if (wide) {
out_nq ? launch(depth_mix_f32<1024, true>, 1024) : launch(depth_mix_f32<1024, false>, 1024);
} else {
out_nq ? launch(depth_mix_f32<256, true>, 256) : launch(depth_mix_f32<256, false>, 256);
}
}

// 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
Expand Down
4 changes: 2 additions & 2 deletions ggml/src/ggml-cuda/inkling2.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ struct ggml_cuda_short_conv_args {
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
// or with out_nq as [n, Q, 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);
bool norm_cur, float eps, const ggml_tensor * query, float scale, ggml_tensor * dst, bool out_nq = false);

// 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,
Expand Down
17 changes: 14 additions & 3 deletions src/models/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,19 @@ ggml_tensor * llama_model_inkling2::graph::mix(const std::vector<ggml_tensor *>

ggml_tensor * scores = ggml_mul_mat(ctx0, states, queries);
scores = ggml_soft_max_ext(ctx0, scores, nullptr, 1.0f/sqrtf(n_embd), 0.0f);
// [n_embd, n_queries, n_tokens]: the mix of one query is a row per token
// the scores first, then the transposed states and the product, so the chain is in one piece and in order for fusion
ggml_build_forward_expand(gf, scores);
ggml_tensor * res = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, states)), scores);
ggml_build_forward_expand(gf, res);

return ggml_mul_mat(ctx0, scores, ggml_cont(ctx0, ggml_transpose(ctx0, states))); // [n_queries, n_embd, n_tokens]
return res;
}

// the mix of query q, [n_embd, n_tokens]; a copy only when the rows of several tokens are apart
ggml_tensor * llama_model_inkling2::graph::mixed_row(ggml_tensor * mixed, int64_t q) {
ggml_tensor * row = ggml_view_2d(ctx0, mixed, n_embd, n_tokens, mixed->nb[2], q*mixed->nb[1]);
return ggml_is_contiguous(row) ? row : ggml_cont(ctx0, row);
}

// causal short convolution with a residual; dynamic adds input-dependent taps per head
Expand Down Expand Up @@ -309,7 +320,7 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens

// the message of controller ctrl, [n_embd, n_tokens]
auto message = [&](int ctrl) {
return ggml_reshape_2d(ctx0, ggml_cont(ctx0, ggml_view_3d(ctx0, messages, 1, n_embd, n_tokens, messages->nb[1], messages->nb[2], ctrl*messages->nb[0])), n_embd, n_tokens);
return mixed_row(messages, ctrl);
};

ggml_tensor * state = recr->mctx->get_r_l(il);
Expand All @@ -323,7 +334,7 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens
if (layer.attn_res_query) {
ggml_tensor * mixed = mix(blocks, cur, layer.attn_res_query);
for (int j = 0; j < 3; ++j) {
inputs[j] = ggml_reshape_2d(ctx0, ggml_cont(ctx0, ggml_view_3d(ctx0, mixed, 1, n_embd, n_tokens, mixed->nb[1], mixed->nb[2], (is_swa ? 0 : j)*mixed->nb[0])), n_embd, n_tokens);
inputs[j] = is_swa && j > 0 ? inputs[0] : mixed_row(mixed, j);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/models/models.h
Original file line number Diff line number Diff line change
Expand Up @@ -2723,6 +2723,8 @@ struct llama_model_inkling2 : public llama_model_base {

std::map<int64_t, ggml_tensor *> zero_sinks; // by head count
std::map<const ggml_tensor *, ggml_tensor *> rel_bias_padded; // the bias table + a zero bin, once per physical layer

ggml_tensor * mixed_row(ggml_tensor * mixed, int64_t q);
};

std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
Expand Down
18 changes: 14 additions & 4 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5131,6 +5131,7 @@ struct test_mul_mat_sub_col : public test_case {
struct test_depth_mix : public test_case {
const int64_t n, B, Q, T;
const bool shared_norm;
const bool out_nq; // the result as [n, Q, T]

std::vector<ggml_tensor *> verify;

Expand All @@ -5143,11 +5144,11 @@ struct test_depth_mix : public test_case {
std::vector<ggml_tensor *> fusion_test_nodes() override { return verify; }

std::string vars() override {
return VARS_TO_STR5(n, B, Q, T, shared_norm);
return VARS_TO_STR6(n, B, Q, T, shared_norm, out_nq);
}

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) {}
test_depth_mix(int64_t n = 2304, int64_t B = 8, int64_t Q = 1, int64_t T = 1, bool shared_norm = false, bool out_nq = false)
: n(n), B(B), Q(Q), T(T), shared_norm(shared_norm), out_nq(out_nq) {}

double max_nmse_err() override {
return 5e-4;
Expand All @@ -5173,7 +5174,11 @@ struct test_depth_mix : public test_case {
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)));
if (out_nq && gf) {
ggml_build_forward_expand(gf, scores);
}
ggml_tensor * out = out_nq ? ggml_mul_mat(ctx, ggml_cont(ctx, ggml_transpose(ctx, states)), scores) :
ggml_mul_mat(ctx, scores, ggml_cont(ctx, ggml_transpose(ctx, states)));
ggml_set_name(out, "out");
verify.push_back(out);
return out;
Expand Down Expand Up @@ -13180,6 +13185,11 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
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));
// the result as [n, Q, T]
for (int64_t Q : { 1, 3, 4 }) {
test_cases.emplace_back(new test_depth_mix(2304, 9, Q, T, false, true));
}
test_cases.emplace_back(new test_depth_mix(2304, 5, 3, T, true, true));
}
for (int64_t T : { 1, 4, 8, 9, 33, 512 }) {
test_cases.emplace_back(new test_loop_ctrl(2304, 64, T));
Expand Down
Loading