Commit ba16f3f8c for llama.cpp
commit ba16f3f8c9ba560657c211d507039c3856d38d05
Author: Leon <3077740163@qq.com>
Date: Tue Dec 1 17:01:54 2026 +0800
vulkan : fix large ROLL shifts (#32876)
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index d82c5ae7a..7f38fe896 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -12446,16 +12446,14 @@ void ggml_vk_pad_reflect_1d(ggml_backend_vk_context * ctx, vk_context& subctx, c
}
void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
- const int32_t s0 = ggml_get_op_params_i32(dst, 0);
- const int32_t s1 = ggml_get_op_params_i32(dst, 1);
- const int32_t s2 = ggml_get_op_params_i32(dst, 2);
- const int32_t s3 = ggml_get_op_params_i32(dst, 3);
- const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
- const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
-
vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
- memcpy(&p.param1, &s01_packed, sizeof(float));
- memcpy(&p.param2, &s23_packed, sizeof(float));
+ float * params[] = { &p.param1, &p.param2, &p.param3, &p.param4 };
+ for (int i = 0; i < 4; ++i) {
+ const int64_t shift = ggml_get_op_params_i32(dst, i);
+ const uint32_t normalized = uint32_t(shift < 0 ? shift + dst->ne[i] : shift);
+ // The shader reads the integer bits from the float slots.
+ memcpy(params[i], &normalized, sizeof(normalized));
+ }
ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p));
}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp b/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp
index dae811ad9..293c489fb 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp
@@ -5,13 +5,8 @@
layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
-uint wrap_idx(int i, uint ne) {
- if (i < 0) {
- return i + ne;
- } else if (i >= ne) {
- return i - ne;
- }
- return i;
+uint roll_idx(uint i, uint shift, uint ne) {
+ return i >= shift ? i - shift : ne - (shift - i);
}
void main() {
@@ -27,17 +22,10 @@ void main() {
const uint i1 = fastdiv(idx - i3_offset - i2_offset, p.ne1_0mp, fastdiv_L(p.ne1_Ls, 2));
const uint i0 = idx - i3_offset - i2_offset - i1*p.ne10;
- const uint p1 = floatBitsToUint(p.param1);
- const uint p2 = floatBitsToUint(p.param2);
- const int s0 = int(p1 >> 16) - 0x8000;
- const int s1 = int(p1 & 0xFFFF) - 0x8000;
- const int s2 = int(p2 >> 16) - 0x8000;
- const int s3 = int(p2 & 0xFFFF) - 0x8000;
-
- const uint i00 = wrap_idx(int(i0) - s0, p.ne10);
- const uint i01 = wrap_idx(int(i1) - s1, p.ne11);
- const uint i02 = wrap_idx(int(i2) - s2, p.ne12);
- const uint i03 = wrap_idx(int(i3) - s3, p.ne13);
+ const uint i00 = roll_idx(i0, floatBitsToUint(p.param1), p.ne10);
+ const uint i01 = roll_idx(i1, floatBitsToUint(p.param2), p.ne11);
+ const uint i02 = roll_idx(i2, floatBitsToUint(p.param3), p.ne12);
+ const uint i03 = roll_idx(i3, floatBitsToUint(p.param4), p.ne13);
const uint a_idx = i03*p.nb03 + i02*p.nb02 + i01*p.nb01 + i00*p.nb00;
const uint d_idx = i3 *p.nb13 + i2 *p.nb12 + i1 *p.nb11 + i0 *p.nb10;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 310b014b9..d0f2bf496 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -14804,6 +14804,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_roll(0, 0, 0, 0, true));
test_cases.emplace_back(new test_roll(256, 0, 0, 0, false, {257, 1, 1, 1}));
test_cases.emplace_back(new test_roll(-16, 6, -2, 1, false, {17, 7, 3, 2}));
+ test_cases.emplace_back(new test_roll(0, 32768, 0, 0, false, {2, 32770, 1, 1}));
+ test_cases.emplace_back(new test_roll(0, 0, -32769, 0, false, {1, 1, 32770, 1}));
test_cases.emplace_back(new test_roll(4, 0, 0, 0, false, {42, 512, 1, 1}));
test_cases.emplace_back(new test_roll(4, 0, 0, 0, false, {380, 512, 1, 1}));
test_cases.emplace_back(new test_arange());