Skip to content

Commit d6d0ce8

Browse files
authored
vulkan: reduce iq1 shared memory usage for mul_mm (ggml-org#24287)
1 parent b4e3dc6 commit d6d0ce8

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3394,7 +3394,9 @@ static bool ggml_vk_matmul_shmem_support(const vk_device& device, const std::vec
33943394
switch (src0_type) {
33953395
case GGML_TYPE_IQ1_S:
33963396
case GGML_TYPE_IQ1_M:
3397-
lut_size = 2*2048 + 4*2048;
3397+
// Regular matmul uses the compact uint16_t IQ1 grid; the expanded
3398+
// uint32_t grid is only enabled for the q8_1/int-dot vector path.
3399+
lut_size = 2*2048;
33983400
break;
33993401
case GGML_TYPE_IQ2_XXS:
34003402
lut_size = 8*256;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#extension GL_EXT_integer_dot_product : require
55

66
#define MMQ
7+
#define NEEDS_IQ1S_GRID_GPU
78
#define B_TYPE block_q8_1_x4
89

910
#include "mul_mat_vec_base.glsl"

ggml/src/ggml-vulkan/vulkan-shaders/types.glsl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,10 @@ const uint[1024] iq1s_grid_const = {
598598
0x55dd55df, 0x55d555d7, 0x5503550c, 0x557f5501, 0x5577557d, 0x55405575, 0x555d555f, 0x55555557
599599
};
600600

601+
#if defined(NEEDS_IQ1S_GRID_GPU)
601602
// Same content as iq1s_grid_const except each 2-bit value is expanded to 4-bit
602603
// and has 1 added to it (allows packed values to be extracted with & 0x0F0F0F0F
603-
// and 0xF0F0F0F0).
604+
// and 0xF0F0F0F0). This is only used by the q8_1/int-dot vector path.
604605
const uint32_t[2048] iq1s_grid_gpu_const = {
605606
0x00000000, 0x00000002, 0x00000101, 0x00000200, 0x00000202, 0x00010001, 0x00010101, 0x00020000,
606607
0x00020002, 0x00020200, 0x00020202, 0x01000101, 0x01010001, 0x01010100, 0x01010102, 0x01020101,
@@ -859,9 +860,12 @@ const uint32_t[2048] iq1s_grid_gpu_const = {
859860
0x20222020, 0x20222022, 0x20222220, 0x20222222, 0x21212021, 0x21212120, 0x21212122, 0x22202020,
860861
0x22202022, 0x22202220, 0x22202222, 0x22212121, 0x22222020, 0x22222022, 0x22222220, 0x22222222,
861862
};
863+
#endif
862864

863865
shared uint16_t iq1s_grid[2048];
866+
#if defined(NEEDS_IQ1S_GRID_GPU)
864867
shared uint32_t iq1s_grid_gpu[2048];
868+
#endif
865869

866870
#define NEEDS_INIT_IQ_SHMEM
867871
void init_iq_shmem(uvec3 wgsize)
@@ -875,12 +879,14 @@ void init_iq_shmem(uvec3 wgsize)
875879
iq1s_grid[2*idx+1] = g.y;
876880
}
877881
}
882+
#if defined(NEEDS_IQ1S_GRID_GPU)
878883
[[unroll]] for (uint i = 0; i < iq1s_grid_gpu_const.length(); i += wgsize.x) {
879884
uint idx = i + gl_LocalInvocationIndex.x;
880885
if (iq1s_grid_gpu_const.length() % wgsize.x == 0 || idx < iq1s_grid_gpu_const.length()) {
881886
iq1s_grid_gpu[idx] = iq1s_grid_gpu_const[idx];
882887
}
883888
}
889+
#endif
884890
barrier();
885891
}
886892
#endif

0 commit comments

Comments
 (0)