ggml : fix UB in q5_0 and q5_1 quantize code

ggml.c:1033:39: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

ggml.c:1081:39: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

ggml-ci
This commit is contained in:
Georgi Gerganov 2023-10-04 12:47:18 +03:00
parent 23277774df
commit 43bbfb7a47
No known key found for this signature in database
GPG key ID: 449E073F9DC10735

8
ggml.c
View file

@ -1032,8 +1032,8 @@ static void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * r
y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4); y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
// get the 5-th bit and store it in qh at the right position // get the 5-th bit and store it in qh at the right position
qh |= ((xi0 & 0x10) >> 4) << (j + 0); qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
qh |= ((xi1 & 0x10) >> 4) << (j + qk/2); qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
} }
memcpy(&y[i].qh, &qh, sizeof(qh)); memcpy(&y[i].qh, &qh, sizeof(qh));
@ -1080,8 +1080,8 @@ static void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * r
y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4); y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
// get the 5-th bit and store it in qh at the right position // get the 5-th bit and store it in qh at the right position
qh |= ((xi0 & 0x10) >> 4) << (j + 0); qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
qh |= ((xi1 & 0x10) >> 4) << (j + qk/2); qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
} }
memcpy(&y[i].qh, &qh, sizeof(y[i].qh)); memcpy(&y[i].qh, &qh, sizeof(y[i].qh));