Commit 994cc45e3 for llama.cpp
commit 994cc45e3e414cd1bada67d18ca32230fe34c25f
Author: Wooseok Shin <46666862+wooseok-shin@users.noreply.github.com>
Date: Thu Dec 3 15:35:22 2026 +0900
CUDA: vectorize Inkling 2 depth_mix with float4 (#33507)
* CUDA: vectorize Inkling 2 depth_mix with float4
Use 16-byte loads and (for Q == 1) stores in depth_mix_f32 when n is a multiple of 4 and the relevant strides and base addresses are 16-byte aligned, keeping the scalar path otherwise. Add DEPTH_MIX tests for boundary shapes and the scalar fallback cases.
Assisted-by: Claude
* CUDA: use 192 threads per block for depth_mix
At n = 2304 the VEC = 4 loops have 576 float4 elements, which 192 threads cover in exactly 3 iterations each, whereas 256 threads left six of eight warps idle in the last iteration. 576 threads were slower on prefill because only one block fits per SM.
Assisted-by: Claude
diff --git a/ggml/src/ggml-cuda/inkling2.cu b/ggml/src/ggml-cuda/inkling2.cu
index b96270396..53df40a7b 100644
--- a/ggml/src/ggml-cuda/inkling2.cu
+++ b/ggml/src/ggml-cuda/inkling2.cu
@@ -161,9 +161,41 @@ void ggml_cuda_op_short_conv(ggml_backend_cuda_context & ctx, const ggml_cuda_sh
}
}
+// ggml_cuda_memcpy_1 is not used here: its __restrict__ pointers give read-only loads, which are not safe with PDL
+template <int VEC>
+static __device__ __forceinline__ void depth_mix_load(float (&v)[VEC], const float * x) {
+ static_assert(VEC == 1 || VEC == 4, "depth_mix: VEC must be 1 or 4");
+ if constexpr (VEC == 4) {
+ const float4 x4 = *(const float4 *) x;
+ v[0] = x4.x;
+ v[1] = x4.y;
+ v[2] = x4.z;
+ v[3] = x4.w;
+ } else {
+ v[0] = x[0];
+ }
+}
+
+// the VEC elements are stride apart, they take one access only when they are adjacent
+template <int VEC>
+static __device__ __forceinline__ void depth_mix_store(float * x, const float (&v)[VEC], const int stride) {
+ static_assert(VEC == 1 || VEC == 4, "depth_mix: VEC must be 1 or 4");
+ if constexpr (VEC == 4) {
+ if (stride == 1) {
+ *(float4 *) x = make_float4(v[0], v[1], v[2], v[3]);
+ return;
+ }
+ }
+#pragma unroll
+ for (int j = 0; j < VEC; ++j) {
+ x[j*stride] = v[j];
+ }
+}
+
// 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>
+// a thread takes VEC adjacent elements per access, VEC = 4 needs n % 4 == 0 and 16 byte aligned rows
+template <int block_size, int VEC>
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,
@@ -191,17 +223,27 @@ static __global__ void depth_mix_f32(
ggml_cuda_pdl_sync();
- for (int i = tid; i < n; i += block_size) {
- const float qi = qv[i];
+ for (int i = tid*VEC; i < n; i += block_size*VEC) {
+ float qi[VEC];
+ depth_mix_load(qi, qv + i);
#pragma unroll
for (int r = 0; r < MAX; ++r) {
if (r < B) {
- acc[r] += st[r*stack_s1 + i]*qi;
+ float si[VEC];
+ depth_mix_load(si, st + r*stack_s1 + i);
+#pragma unroll
+ for (int j = 0; j < VEC; ++j) {
+ acc[r] += si[j]*qi[j];
+ }
}
}
- const float xi = xc[i];
- acc[MAX] += xi*qi;
- acc[MAX + 1] += xi*xi;
+ float xi[VEC];
+ depth_mix_load(xi, xc + i);
+#pragma unroll
+ for (int j = 0; j < VEC; ++j) {
+ acc[MAX] += xi[j]*qi[j];
+ acc[MAX + 1] += xi[j]*xi[j];
+ }
}
__shared__ float s_acc[NW][MAX + 2];
@@ -255,16 +297,28 @@ static __global__ void depth_mix_f32(
}
p_cur *= inv*sc;
- for (int i = tid; i < n; i += block_size) {
- float o = 0.0f;
+ for (int i = tid*VEC; i < n; i += block_size*VEC) {
+ float o[VEC] = {0.0f};
#pragma unroll
for (int r = 0; r < MAX; ++r) {
if (r < B) {
- o += acc[r]*st[r*stack_s1 + i];
+ float si[VEC];
+ depth_mix_load(si, st + r*stack_s1 + i);
+#pragma unroll
+ for (int j = 0; j < VEC; ++j) {
+ o[j] += acc[r]*si[j];
+ }
}
}
- o += p_cur*xc[i];
- dst[((int64_t) t*n + i)*Q + q] = o;
+ float xi[VEC];
+ depth_mix_load(xi, xc + i);
+#pragma unroll
+ for (int j = 0; j < VEC; ++j) {
+ o[j] += p_cur*xi[j];
+ }
+
+ // dst is [Q, n, T]
+ depth_mix_store(dst + ((int64_t) t*n + i)*Q + q, o, Q);
}
}
@@ -280,11 +334,16 @@ 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 int block_size = 192;
const dim3 grid(Q, T, 1);
+ // 16 byte accesses: n, the row strides and the base addresses must be multiples of 4 floats
+ const bool use_vec4 = n % 4 == 0 && (stack->nb[1] | stack->nb[2] | cur->nb[2] | query->nb[1]) % 16 == 0 &&
+ ((uintptr_t) stack->data | (uintptr_t) cur->data | (uintptr_t) query->data | (uintptr_t) dst->data) % 16 == 0;
+ auto kernel = use_vec4 ? depth_mix_f32<block_size, 4> : depth_mix_f32<block_size, 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,
+ 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)),
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 247a0e9ad..ac709934b 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -5053,6 +5053,8 @@ 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 int64_t cur_pad;
+ const int64_t cur_off;
std::vector<ggml_tensor *> verify;
@@ -5065,11 +5067,12 @@ 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_STR7(n, B, Q, T, shared_norm, cur_pad, cur_off);
}
- 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, int64_t cur_pad = 0,
+ int64_t cur_off = 0)
+ : n(n), B(B), Q(Q), T(T), shared_norm(shared_norm), cur_pad(cur_pad), cur_off(cur_off) {}
double max_nmse_err() override {
return 5e-4;
@@ -5078,12 +5081,16 @@ struct test_depth_mix : public test_case {
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 * cur = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n + cur_pad, 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);
+ // the rows of cur are cur_pad elements apart and start cur_off elements into the row (cur_off <= cur_pad)
+ ggml_tensor * cur_rows = cur_pad == 0 ? ggml_reshape_3d(ctx, cur, n, 1, T) :
+ ggml_view_3d(ctx, cur, n, 1, T, cur->nb[1], cur->nb[1], cur_off * sizeof(float));
+
+ ggml_tensor * norm = ggml_rms_norm(ctx, cur_rows, 1e-6f);
if (shared_norm) {
ggml_tensor * other = ggml_scale(ctx, norm, 2.0f);
if (gf) {
@@ -12359,13 +12366,24 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
for (int64_t T : { 1, 3 }) {
for (int64_t Q : { 1, 3, 4 }) {
- for (int64_t B : { 1, 3, 12 }) {
+ for (int64_t B : { 1, 3, 12, 16 }) {
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));
- }
+ // around one block of elements, with and without n % 4 == 0
+ for (int64_t n : { 256, 257, 1001 }) {
+ test_cases.emplace_back(new test_depth_mix(n, 4, 1, T));
+ test_cases.emplace_back(new test_depth_mix(n, 4, 3, T));
+ }
+ }
+ test_cases.emplace_back(new test_depth_mix(2304, 4, 1, 1, true));
+ // rows of cur that are not 16 byte aligned
+ test_cases.emplace_back(new test_depth_mix(2304, 4, 1, 3, false, 1));
+ // rows of cur that are 16 byte apart but start 4 bytes off a 16 byte boundary: the scalar fallback on the base address
+ test_cases.emplace_back(new test_depth_mix(2304, 4, 1, 3, false, 4, 1));
+ test_cases.emplace_back(new test_depth_mix(2304, 4, 3, 3, false, 4, 1));
for (int64_t T : { 1, 4, 8 }) {
test_cases.emplace_back(new test_loop_ctrl(2304, 64, T));
}