From c2a40f76d8ad65edc15c7e5f8778635d6c492975 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Wed, 2 Dec 2026 08:55:18 +0000 Subject: [PATCH 1/2] CUDA: Inkling 2 depth mix with 1024-thread blocks when there are few blocks Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/inkling2.cu | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ggml/src/ggml-cuda/inkling2.cu b/ggml/src/ggml-cuda/inkling2.cu index b96270396774..ecfdeee67c54 100644 --- a/ggml/src/ggml-cuda/inkling2.cu +++ b/ggml/src/ggml-cuda/inkling2.cu @@ -280,16 +280,24 @@ void ggml_cuda_op_depth_mix(ggml_backend_cuda_context & ctx, const ggml_tensor * 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, 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 + if (Q*T <= ggml_cuda_info().devices[ggml_cuda_get_device()].nsm && n >= 2048) { + launch(depth_mix_f32<1024>, 1024); + } else { + launch(depth_mix_f32<256>, 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 From feac8685e8fba828412c31103e5685ccd283191c Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Wed, 2 Dec 2026 09:43:08 +0000 Subject: [PATCH 2/2] inkling2: depth mix result as [n_embd, Q, n_tokens], no copies of contiguous rows The mix of a query was a column of [Q, n_embd, n_tokens] and every Q, K, V and controller input was a copy of it, also for the one query of the SWA layers. Now each mix is a row; the fused depth mix kernel writes either layout. Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/fusion.cu | 9 ++++++--- ggml/src/ggml-cuda/inkling2.cu | 19 ++++++++++++------- ggml/src/ggml-cuda/inkling2.cuh | 4 ++-- src/models/inkling2.cpp | 17 ++++++++++++++--- src/models/models.h | 2 ++ tests/test-backend-ops.cpp | 18 ++++++++++++++---- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index 3c1e89b5a560..9647bef3b762 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -1703,8 +1703,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; } @@ -1721,7 +1724,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; } @@ -1731,7 +1734,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; } diff --git a/ggml/src/ggml-cuda/inkling2.cu b/ggml/src/ggml-cuda/inkling2.cu index ecfdeee67c54..93f1605a9886 100644 --- a/ggml/src/ggml-cuda/inkling2.cu +++ b/ggml/src/ggml-cuda/inkling2.cu @@ -163,7 +163,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 +template 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, @@ -264,12 +264,16 @@ 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]; @@ -277,7 +281,7 @@ void ggml_cuda_op_depth_mix(ggml_backend_cuda_context & ctx, const ggml_tensor * 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 dim3 grid(Q, T, 1); @@ -293,10 +297,11 @@ void ggml_cuda_op_depth_mix(ggml_backend_cuda_context & ctx, const ggml_tensor * }; // few blocks (decode): wide blocks, so that each thread loops over fewer elements of the rows - if (Q*T <= ggml_cuda_info().devices[ggml_cuda_get_device()].nsm && n >= 2048) { - launch(depth_mix_f32<1024>, 1024); + 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 { - launch(depth_mix_f32<256>, 256); + out_nq ? launch(depth_mix_f32<256, true>, 256) : launch(depth_mix_f32<256, false>, 256); } } diff --git a/ggml/src/ggml-cuda/inkling2.cuh b/ggml/src/ggml-cuda/inkling2.cuh index dcffe3e60995..47947e70d6cb 100644 --- a/ggml/src/ggml-cuda/inkling2.cuh +++ b/ggml/src/ggml-cuda/inkling2.cuh @@ -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, diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp index 87cb21a3d7ab..12b15a4b0fea 100644 --- a/src/models/inkling2.cpp +++ b/src/models/inkling2.cpp @@ -201,8 +201,19 @@ ggml_tensor * llama_model_inkling2::graph::mix(const std::vector 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 @@ -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); @@ -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); } } diff --git a/src/models/models.h b/src/models/models.h index 02d7d264f79a..4e9525197ead 100644 --- a/src/models/models.h +++ b/src/models/models.h @@ -2722,6 +2722,8 @@ struct llama_model_inkling2 : public llama_model_base { std::map, ggml_tensor *> mix_stacks; std::map zero_sinks; // by head count + + ggml_tensor * mixed_row(ggml_tensor * mixed, int64_t q); }; std::unique_ptr build_arch_graph(const llm_graph_params & params) const override; diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 94e490476473..c2c89332d9d7 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -5097,6 +5097,7 @@ struct test_short_conv : 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 verify; @@ -5109,11 +5110,11 @@ struct test_depth_mix : public test_case { std::vector 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; @@ -5139,7 +5140,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; @@ -12942,6 +12947,11 @@ static std::vector> 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 }) { test_cases.emplace_back(new test_loop_ctrl(2304, 64, T));