Skip to content

Commit 5edf159

Browse files
authored
vulkan : fix out-of-bounds access in argmax kernel (#15342)
ggml-ci
1 parent db3010b commit 5edf159

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8392,7 +8392,7 @@ static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx,
83928392
}
83938393

83948394
static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
8395-
ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
8395+
ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], 0.0f, 0.0f }, dryrun);
83968396
}
83978397

83988398
static void ggml_vk_count_equal(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {

ggml/src/ggml-vulkan/vulkan-shaders/argmax.comp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#extension GL_EXT_control_flow_attributes : enable
77

8+
#define FLT_MAX 3.402823466e+38F
9+
810
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
911

1012
layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
@@ -19,19 +21,26 @@ void main() {
1921
const uint row = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x;
2022
const uint col = gl_LocalInvocationID.x;
2123

22-
if (col >= p.KX) {
24+
if (row >= p.KY) {
2325
return;
2426
}
25-
A_TYPE amax = data_a[row*p.KX + col];
26-
tmp[col] = col;
27+
28+
A_TYPE amax = -FLT_MAX;
29+
uint acol = col;
30+
31+
if (col < p.KX) {
32+
amax = data_a[row*p.KX + col];
33+
}
2734

2835
for (uint i = col + BLOCK_SIZE; i < p.KX; i += BLOCK_SIZE) {
2936
A_TYPE val = data_a[row*p.KX + i];
3037
if (val > amax) {
3138
amax = val;
32-
tmp[col] = i;
39+
acol = i;
3340
}
3441
}
42+
43+
tmp[col] = acol;
3544
tmpmax[col] = amax;
3645

3746
barrier();

tests/test-backend-ops.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5528,6 +5528,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
55285528
test_cases.emplace_back(new test_count_equal(GGML_TYPE_F32, {4, 5000, 1, 1}));
55295529

55305530
test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {32, 1, 1, 1}));
5531+
test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {32, 513, 1, 1}));
55315532
test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {100, 10, 1, 1}));
55325533
test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {1024, 10, 1, 1}));
55335534
test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {1024, 12, 1, 1}));

0 commit comments

Comments
 (0)