Skip to content

Commit c6aa468

Browse files
committed
kompute: implement op_getrows_f32 (ggml-org#6403)
Signed-off-by: Jared Van Bortel <[email protected]>
1 parent f50a09c commit c6aa468

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ if (LLAMA_KOMPUTE)
720720
kompute-shaders/op_mul_mat_q4_0.comp
721721
kompute-shaders/op_mul_mat_q4_1.comp
722722
kompute-shaders/op_mul_mat_q6_k.comp
723+
kompute-shaders/op_getrows_f32.comp
723724
kompute-shaders/op_getrows_f16.comp
724725
kompute-shaders/op_getrows_q4_0.comp
725726
kompute-shaders/op_getrows_q4_1.comp
@@ -752,6 +753,7 @@ if (LLAMA_KOMPUTE)
752753
shaderop_mul_mat_q4_0.h
753754
shaderop_mul_mat_q4_1.h
754755
shaderop_mul_mat_q6_k.h
756+
shaderop_getrows_f32.h
755757
shaderop_getrows_f16.h
756758
shaderop_getrows_q4_0.h
757759
shaderop_getrows_q4_1.h

ggml-kompute.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "shaderop_mul_mat_q4_1.h"
2323
#include "shaderop_mul_mat_q6_k.h"
2424
#include "shaderop_mul_mat_mat_f32.h"
25+
#include "shaderop_getrows_f32.h"
2526
#include "shaderop_getrows_f16.h"
2627
#include "shaderop_getrows_q4_0.h"
2728
#include "shaderop_getrows_q4_1.h"
@@ -1228,6 +1229,14 @@ static void ggml_vk_get_rows(
12281229
seq.record<kp::OpAlgoDispatch>(s_algo);
12291230
}
12301231

1232+
template <typename... Args>
1233+
static void ggml_vk_get_rows_f32(Args&&... args) {
1234+
const static auto spirv = getSpirvShader(kp::shader_data::op_getrows_f32_comp_spv,
1235+
kp::shader_data::op_getrows_f32_comp_spv_len);
1236+
1237+
ggml_vk_get_rows(spirv, "f32", sizeof(float), 0, std::forward<Args>(args)...);
1238+
}
1239+
12311240
template <typename... Args>
12321241
static void ggml_vk_get_rows_f16(Args&&... args) {
12331242
const static auto spirv = getSpirvShader(kp::shader_data::op_getrows_f16_comp_spv,
@@ -1453,6 +1462,7 @@ static bool ggml_vk_supports_op(const struct ggml_tensor * op) {
14531462
return op->ne[3] == 1;
14541463
case GGML_OP_GET_ROWS:
14551464
switch (op->src[0]->type) {
1465+
case GGML_TYPE_F32:
14561466
case GGML_TYPE_F16:
14571467
case GGML_TYPE_Q4_0:
14581468
case GGML_TYPE_Q4_1:
@@ -1730,7 +1740,9 @@ static void ggml_vk_graph_compute(struct ggml_kompute_context * ctx, struct ggml
17301740
} break;
17311741
case GGML_OP_GET_ROWS:
17321742
{
1733-
if (src0t == GGML_TYPE_F16) {
1743+
if (src0t == GGML_TYPE_F32) {
1744+
ggml_vk_get_rows_f32(seq, id_src0, id_src1, id_dst, off_src0, off_src1, off_dst, ne00, nb01, nb1, ggml_nelements(src1));
1745+
} else if (src0t == GGML_TYPE_F16) {
17341746
ggml_vk_get_rows_f16(seq, id_src0, id_src1, id_dst, off_src0, off_src1, off_dst, ne00, nb01, nb1, ggml_nelements(src1));
17351747
} else if (src0t == GGML_TYPE_Q4_0) {
17361748
ggml_vk_get_rows_q4_0(seq, id_src0, id_src1, id_dst, off_src0, off_src1, off_dst, ne00, nb01, nb1, ggml_nelements(src1));

kompute-shaders/op_getrows_f32.comp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#version 450
2+
3+
#include "common.comp"
4+
5+
layout(local_size_x = 1) in;
6+
7+
layout (binding = 0) readonly buffer tensorInA { float inA[]; };
8+
layout (binding = 1) readonly buffer tensorInB { int inB[]; };
9+
layout (binding = 2) writeonly buffer tensorOut { float out_[]; };
10+
11+
layout (push_constant) uniform parameter {
12+
uint inAOff;
13+
uint inBOff;
14+
uint outOff;
15+
int ne00;
16+
int nb01;
17+
int nb1;
18+
} pcs;
19+
20+
void dequantize_row_f32(uint x /*Based from inA unaligned*/, uint y /*Based from out_*/, int k) {
21+
for (int j = 0; j < k; j++) {
22+
out_[y + j] = inA[x + j];
23+
}
24+
}
25+
26+
void main() {
27+
const uint i = gl_WorkGroupID.x;
28+
const int r = inB[i + pcs.inBOff];
29+
30+
dequantize_row_f32(r*pcs.nb01/4 + pcs.inAOff, i*pcs.nb1/4 + pcs.outOff, pcs.ne00);
31+
}

0 commit comments

Comments
 (0)