Skip to content

Commit 0c52585

Browse files
jeffbolznvmglambda
authored andcommitted
vulkan: optimize coopmat2 q2_k dequant function (ggml-org#11130)
1 parent 2fe8f06 commit 0c52585

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,25 @@ layout(buffer_reference, std430, buffer_reference_align = 4) buffer decodeBufQ2_
101101
block_q2_K block;
102102
};
103103

104+
layout(buffer_reference, std430, buffer_reference_align = 16) buffer decodeBufQ2_K_packed16 {
105+
block_q2_K_packed16 block;
106+
};
107+
104108
float16_t dequantFuncQ2_K(const in decodeBufQ2_K bl, const in uint blockCoords[2], const in uint coordInBlock[2])
105109
{
110+
decodeBufQ2_K_packed16 bl16 = decodeBufQ2_K_packed16(bl);
106111
const f16vec2 d = bl.block.d;
107112
const uint idx = coordInBlock[1];
108-
const uint iqs = idx;
109113

110-
const uint qsi = (iqs / 128) * 32 + (iqs % 32); // 0..31
111-
const uint scalesi = iqs / 16; // 0..15
112-
const uint qsshift = ((iqs % 128) / 32) * 2; // 0,2,4,6
114+
const uint scalesi = (idx & 0xF0) >> 4; // 0..15
115+
const uint qsshift = (idx & 0x60) >> 4; // 0,2,4,6
116+
117+
uint qs = uint32_t(bl16.block.qs[((idx & 0x80) >> 3) + ((idx & 0x1E) >> 1)]);
118+
qs = (qs >> qsshift) & 0x0303;
119+
qs = unpack8(qs)[idx & 1];
113120

114-
uint32_t qs = bl.block.qs[qsi];
115121
const uint scales = bl.block.scales[scalesi];
116-
float16_t ret = d.x * float16_t(scales & 0xF) * float16_t((qs >> qsshift) & 3) - d.y * float16_t(scales >> 4);
122+
float16_t ret = d.x * float16_t(scales & 0xF) * float16_t(qs) - d.y * float16_t(scales >> 4);
117123
return ret;
118124
}
119125

0 commit comments

Comments
 (0)