Skip to content

Commit 2005469

Browse files
authored
Add Q4_3 support to cuBLAS (#1086)
1 parent 8a1756a commit 2005469

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ ifdef LLAMA_OPENBLAS
102102
endif
103103
ifdef LLAMA_CUBLAS
104104
CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include
105-
LDFLAGS += -lcublas_static -lculibos -lcudart_static -lcublasLt_static -lpthread -ldl -lrt -L/usr/local/cuda/lib64
105+
LDFLAGS += -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64
106106
OBJS += ggml-cuda.o
107107
ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
108108
nvcc -arch=native -c -o $@ $<

ggml-cuda.cu

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ static_assert(sizeof(block_q4_1) == sizeof(float) * 2 + QK4_1 / 2, "wrong q4_1 b
2222

2323
#define QK4_2 16
2424
typedef struct {
25-
__half d; // delta
25+
__half d; // delta
2626
uint8_t qs[QK4_2 / 2]; // nibbles / quants
2727
} block_q4_2;
2828
static_assert(sizeof(block_q4_2) == sizeof(ggml_fp16_t) + QK4_2 / 2, "wrong q4_2 block size/padding");
2929

30+
#define QK4_3 16
31+
typedef struct {
32+
__half d; // delta
33+
__half m; // min
34+
uint8_t qs[QK4_3 / 2]; // nibbles / quants
35+
} block_q4_3;
36+
static_assert(sizeof(block_q4_3) == 2 * sizeof(ggml_fp16_t) + QK4_3 / 2, "wrong q4_3 block size/padding");
37+
38+
3039

3140
static __global__ void dequantize_block_q4_0(const void * vx, float * y) {
3241
const block_q4_0 * x = (const block_q4_0 *) vx;
@@ -98,6 +107,30 @@ static __global__ void dequantize_block_q4_2(const void * vx, float * y) {
98107
}
99108
}
100109

110+
static __global__ void dequantize_block_q4_3(const void * vx, float * y) {
111+
const block_q4_3 * x = (const block_q4_3 *) vx;
112+
113+
const int i = blockIdx.x;
114+
115+
const float d = x[i].d;
116+
const float m = x[i].m;
117+
118+
const uint8_t * pp = x[i].qs;
119+
120+
for (int l = 0; l < QK4_3; l += 2) {
121+
const uint8_t vi = pp[l/2];
122+
123+
const int8_t vi0 = vi & 0xf;
124+
const int8_t vi1 = vi >> 4;
125+
126+
const float v0 = vi0*d + m;
127+
const float v1 = vi1*d + m;
128+
129+
y[i*QK4_3 + l + 0] = v0;
130+
y[i*QK4_3 + l + 1] = v1;
131+
}
132+
}
133+
101134
extern "C" {
102135
__host__ void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
103136
const int nb = k / QK4_0;
@@ -113,4 +146,9 @@ extern "C" {
113146
const int nb = k / QK4_2;
114147
dequantize_block_q4_2<<<nb, 1, 0, stream>>>(vx, y);
115148
}
149+
150+
__host__ void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
151+
const int nb = k / QK4_3;
152+
dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y);
153+
}
116154
}

ggml-cuda.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern "C" {
55
void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream);
66
void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream);
77
void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream);
8+
void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream);
89

910
#ifdef __cplusplus
1011
}

0 commit comments

Comments
 (0)