Skip to content

Commit f034aa1

Browse files
committed
ggml-quants : rename fields of TQ1_0 and TQ2_0 structs for consistency
1 parent 04eec58 commit f034aa1

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

ggml/src/ggml-common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,15 @@ static_assert(sizeof(block_q8_0x8) == 8 * sizeof(ggml_half) + QK8_0 * 8, "wrong
233233

234234
// 1.6875 bpw
235235
typedef struct {
236-
uint8_t q[(QK_K - 4 * QK_K / 64) / 5]; // 5 elements per byte (3^5 = 243 < 256)
237-
uint8_t qs[QK_K/64]; // 4 elements per byte
236+
uint8_t qs[(QK_K - 4 * QK_K / 64) / 5]; // 5 elements per byte (3^5 = 243 < 256)
237+
uint8_t qh[QK_K/64]; // 4 elements per byte
238238
ggml_half d;
239239
} block_tq1_0;
240240
static_assert(sizeof(block_tq1_0) == sizeof(ggml_half) + QK_K / 64 + (QK_K - 4 * QK_K / 64) / 5, "wrong tq1_0 block size/padding");
241241

242242
// 2.0625 bpw
243243
typedef struct {
244-
uint8_t q[QK_K/4]; // 2 bits per element
244+
uint8_t qs[QK_K/4]; // 2 bits per element
245245
ggml_half d;
246246
} block_tq2_0;
247247
static_assert(sizeof(block_tq2_0) == sizeof(ggml_half) + QK_K / 4, "wrong tq2_0 block size/padding");

ggml/src/ggml-quants.c

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,7 +3326,7 @@ void quantize_row_tq1_0_ref(const float * restrict x, block_tq1_0 * restrict y,
33263326
y[i].d = GGML_FP32_TO_FP16(d);
33273327

33283328
// 5 elements per byte, along 32 bytes
3329-
for (size_t j = 0; j < sizeof(y->q) - sizeof(y->q) % 32; j += 32) {
3329+
for (size_t j = 0; j < sizeof(y->qs) - sizeof(y->qs) % 32; j += 32) {
33303330
for (size_t m = 0; m < 32; ++m) {
33313331
uint8_t q = 0;
33323332
for (size_t n = 0; n < 5; ++n) {
@@ -3336,12 +3336,12 @@ void quantize_row_tq1_0_ref(const float * restrict x, block_tq1_0 * restrict y,
33363336
}
33373337
// ceiling division (243 == pow(3, 5))
33383338
q = ((uint16_t)q * 256 + (243 - 1)) / 243;
3339-
y[i].q[j + m] = q;
3339+
y[i].qs[j + m] = q;
33403340
}
33413341
x += 5*32;
33423342
}
33433343
// along 16 bytes
3344-
for (size_t j = sizeof(y->q) - sizeof(y->q) % 32; j < sizeof(y->q); j += 16) {
3344+
for (size_t j = sizeof(y->qs) - sizeof(y->qs) % 32; j < sizeof(y->qs); j += 16) {
33453345
for (size_t m = 0; m < 16; ++m) {
33463346
uint8_t q = 0;
33473347
for (size_t n = 0; n < 5; ++n) {
@@ -3351,26 +3351,26 @@ void quantize_row_tq1_0_ref(const float * restrict x, block_tq1_0 * restrict y,
33513351
}
33523352
// ceiling division (243 == pow(3, 5))
33533353
q = ((uint16_t)q * 256 + (243 - 1)) / 243;
3354-
y[i].q[j + m] = q;
3354+
y[i].qs[j + m] = q;
33553355
}
33563356
x += 5*16;
33573357
}
33583358
// 4 elements per byte
3359-
for (size_t j = 0; j < sizeof(y->qs); ++j) {
3359+
for (size_t j = 0; j < sizeof(y->qh); ++j) {
33603360
uint8_t q = 0;
33613361
for (size_t m = 0; m < 4; ++m) {
33623362
// -1, 0, 1 -> 0, 1, 2
3363-
int xi = nearest_int(x[j + m*sizeof(y->qs)] * id) + 1;
3363+
int xi = nearest_int(x[j + m*sizeof(y->qh)] * id) + 1;
33643364
q *= 3;
33653365
q += xi;
33663366
}
33673367
// shift the first value to the most significant trit
33683368
q *= 3;
33693369
// ceiling division (243 == pow(3, 5))
33703370
q = ((uint16_t)q * 256 + (243 - 1)) / 243;
3371-
y[i].qs[j] = q;
3371+
y[i].qh[j] = q;
33723372
}
3373-
x += 4*sizeof(y->qs);
3373+
x += 4*sizeof(y->qh);
33743374
}
33753375
}
33763376

@@ -3392,15 +3392,15 @@ void quantize_row_tq2_0_ref(const float * restrict x, block_tq2_0 * restrict y,
33923392
y[i].d = GGML_FP32_TO_FP16(d);
33933393

33943394
// TODO: should it be along 64 bytes instead for AVX512?
3395-
for (size_t j = 0; j < sizeof(y->q); j += 32) {
3395+
for (size_t j = 0; j < sizeof(y->qs); j += 32) {
33963396
for (size_t m = 0; m < 32; ++m) {
33973397
uint8_t q = 0;
33983398
for (size_t n = 0; n < 4; ++n) {
33993399
// -1, 0, 1 -> 0, 1, 2
34003400
int xi = nearest_int(x[m + n*32] * id) + 1;
34013401
q += (xi & 3) << (2*n);
34023402
}
3403-
y[i].q[j + m] = q;
3403+
y[i].qs[j + m] = q;
34043404
}
34053405
x += 4*32;
34063406
}
@@ -3444,28 +3444,28 @@ void dequantize_row_tq1_0(const block_tq1_0 * restrict x, float * restrict y, in
34443444

34453445
const float d = GGML_FP16_TO_FP32(x[i].d);
34463446

3447-
for (size_t j = 0; j < sizeof(x->q) - sizeof(x->q) % 32; j += 32) {
3447+
for (size_t j = 0; j < sizeof(x->qs) - sizeof(x->qs) % 32; j += 32) {
34483448
for (size_t n = 0; n < 5; ++n) {
34493449
for (size_t m = 0; m < 32; ++m) {
3450-
uint8_t q = x[i].q[j + m] * pow3[n];
3450+
uint8_t q = x[i].qs[j + m] * pow3[n];
34513451
int16_t xi = ((uint16_t) q * 3) >> 8;
34523452
*y++ = (float) (xi - 1) * d;
34533453
}
34543454
}
34553455
}
3456-
for (size_t j = sizeof(x->q) - sizeof(x->q) % 32; j < sizeof(x->q); j += 16) {
3456+
for (size_t j = sizeof(x->qs) - sizeof(x->qs) % 32; j < sizeof(x->qs); j += 16) {
34573457
for (size_t n = 0; n < 5; ++n) {
34583458
for (size_t m = 0; m < 16; ++m) {
3459-
uint8_t q = x[i].q[j + m] * pow3[n];
3459+
uint8_t q = x[i].qs[j + m] * pow3[n];
34603460
int16_t xi = ((uint16_t) q * 3) >> 8;
34613461
*y++ = (float) (xi - 1) * d;
34623462
}
34633463
}
34643464
}
34653465

34663466
for (size_t n = 0; n < 4; ++n) {
3467-
for (size_t j = 0; j < sizeof(x->qs); ++j) {
3468-
uint8_t q = x[i].qs[j] * pow3[n];
3467+
for (size_t j = 0; j < sizeof(x->qh); ++j) {
3468+
uint8_t q = x[i].qh[j] * pow3[n];
34693469
int16_t xi = ((uint16_t) q * 3) >> 8;
34703470
*y++ = (float) (xi - 1) * d;
34713471
}
@@ -3481,10 +3481,10 @@ void dequantize_row_tq2_0(const block_tq2_0 * restrict x, float * restrict y, in
34813481

34823482
const float d = GGML_FP16_TO_FP32(x[i].d);
34833483

3484-
for (size_t j = 0; j < sizeof(x->q); j += 32) {
3484+
for (size_t j = 0; j < sizeof(x->qs); j += 32) {
34853485
for (size_t l = 0; l < 4; ++l) {
34863486
for (size_t m = 0; m < 32; ++m) {
3487-
int8_t q = (x[i].q[j + m] >> (l*2)) & 3;
3487+
int8_t q = (x[i].qs[j + m] >> (l*2)) & 3;
34883488
*y++ = (float) (q - 1) * d;
34893489
}
34903490
}
@@ -5681,8 +5681,8 @@ void ggml_vec_dot_tq1_0_q8_K(int n, float * restrict s, size_t bs, const void *
56815681

56825682
// first 32 bytes of 5 elements
56835683
{
5684-
uint8x16_t qx0 = vld1q_u8(x[i].q + 0);
5685-
uint8x16_t qx1 = vld1q_u8(x[i].q + 16);
5684+
uint8x16_t qx0 = vld1q_u8(x[i].qs + 0);
5685+
uint8x16_t qx1 = vld1q_u8(x[i].qs + 16);
56865686
uint8x16_t qx2 = vmulq_u8(qx0, vdupq_n_u8(3));
56875687
uint8x16_t qx3 = vmulq_u8(qx1, vdupq_n_u8(3));
56885688
uint8x16_t qx4 = vmulq_u8(qx0, vdupq_n_u8(9));
@@ -5739,14 +5739,14 @@ void ggml_vec_dot_tq1_0_q8_K(int n, float * restrict s, size_t bs, const void *
57395739

57405740
// last 16 bytes of 5-element, along with the 4 bytes of 4 elements
57415741
{
5742-
uint8x16_t qx0 = vld1q_u8(x[i].q + 32);
5742+
uint8x16_t qx0 = vld1q_u8(x[i].qs + 32);
57435743
uint8x16_t qx1 = vmulq_u8(qx0, vdupq_n_u8(3));
57445744
uint8x16_t qx2 = vmulq_u8(qx0, vdupq_n_u8(9));
57455745
uint8x16_t qx3 = vmulq_u8(qx0, vdupq_n_u8(27));
57465746
uint8x16_t qx4 = vmulq_u8(qx0, vdupq_n_u8(81));
5747-
uint32_t qs;
5748-
memcpy(&qs, x[i].qs, sizeof(qs)); // potentially unaligned
5749-
uint8x16_t qx5 = vreinterpretq_u8_u32(vdupq_n_u32(qs));
5747+
uint32_t qh;
5748+
memcpy(&qh, x[i].qh, sizeof(qh)); // potentially unaligned
5749+
uint8x16_t qx5 = vreinterpretq_u8_u32(vdupq_n_u32(qh));
57505750
qx5 = vmulq_u8(qx5, shift);
57515751

57525752
// multiply by 3 and keep the 2 bits above 8 bits
@@ -5802,7 +5802,7 @@ void ggml_vec_dot_tq1_0_q8_K(int n, float * restrict s, size_t bs, const void *
58025802

58035803
// first 32 bytes of 5 elements
58045804
{
5805-
__m256i qx0 = _mm256_loadu_si256((const __m256i *) (x[i].q));
5805+
__m256i qx0 = _mm256_loadu_si256((const __m256i *) (x[i].qs));
58065806
// 8-bit multiplies with shifts, masks and adds
58075807
__m256i qx1 = _mm256_add_epi8(qx0, _mm256_add_epi8(qx0, qx0)); // 1 * 3
58085808
__m256i qx2 = _mm256_add_epi8(_mm256_and_si256(_mm256_slli_epi16(qx0, 3), _mm256_set1_epi8(-8)), qx0); // 1 * 9
@@ -5848,10 +5848,10 @@ void ggml_vec_dot_tq1_0_q8_K(int n, float * restrict s, size_t bs, const void *
58485848

58495849
// last 16 bytes of 5-element, along with the 4 bytes of 4 elements
58505850
{
5851-
__m128i qx0 = _mm_loadu_si128((const __m128i *) (x[i].q + 32));
5852-
uint32_t qs;
5853-
memcpy(&qs, x[i].qs, sizeof(qs)); // potentially unaligned
5854-
__m256i qx5_l = _mm256_cvtepu8_epi16(_mm_set1_epi32(qs));
5851+
__m128i qx0 = _mm_loadu_si128((const __m128i *) (x[i].qs + 32));
5852+
uint32_t qh;
5853+
memcpy(&qh, x[i].qh, sizeof(qh)); // potentially unaligned
5854+
__m256i qx5_l = _mm256_cvtepu8_epi16(_mm_set1_epi32(qh));
58555855
__m128i qx1 = _mm_add_epi8(qx0, _mm_add_epi8(qx0, qx0)); // 1 * 3
58565856
__m128i qx2 = _mm_add_epi8(_mm_and_si128(_mm_slli_epi16(qx0, 3), _mm_set1_epi8(-8)), qx0); // 1 * 9
58575857
__m128i qx3 = _mm_add_epi8(_mm_and_si128(_mm_slli_epi16(qx1, 3), _mm_set1_epi8(-8)), qx1); // 3 * 9
@@ -5911,30 +5911,30 @@ void ggml_vec_dot_tq1_0_q8_K(int n, float * restrict s, size_t bs, const void *
59115911
for (int i = 0; i < nb; ++i) {
59125912
int sum = 0;
59135913

5914-
for (size_t j = 0; j < sizeof(x->q) - sizeof(x->q) % 32; j += 32) {
5914+
for (size_t j = 0; j < sizeof(x->qs) - sizeof(x->qs) % 32; j += 32) {
59155915
for (size_t l = 0; l < 5; ++l) {
59165916
for (size_t m = 0; m < 32; ++m) {
5917-
uint8_t q = x[i].q[j + m] * pow3[l];
5917+
uint8_t q = x[i].qs[j + m] * pow3[l];
59185918
uint16_t xi = ((uint16_t) q * 3) >> 8;
59195919
sum += (xi - 1) * y[i].qs[j*5 + l*32 + m];
59205920
}
59215921
}
59225922
}
5923-
for (size_t j = sizeof(x->q) - sizeof(x->q) % 32; j < sizeof(x->q); j += 16) {
5923+
for (size_t j = sizeof(x->qs) - sizeof(x->qs) % 32; j < sizeof(x->qs); j += 16) {
59245924
for (size_t l = 0; l < 5; ++l) {
59255925
for (size_t m = 0; m < 16; ++m) {
5926-
uint8_t q = x[i].q[j + m] * pow3[l];
5926+
uint8_t q = x[i].qs[j + m] * pow3[l];
59275927
uint16_t xi = ((uint16_t) q * 3) >> 8;
59285928
sum += (xi - 1) * y[i].qs[j*5 + l*16 + m];
59295929
}
59305930
}
59315931
}
59325932

59335933
for (size_t l = 0; l < 4; ++l) {
5934-
for (size_t j = 0; j < sizeof(x->qs); ++j) {
5935-
uint8_t q = x[i].qs[j] * pow3[l];
5934+
for (size_t j = 0; j < sizeof(x->qh); ++j) {
5935+
uint8_t q = x[i].qh[j] * pow3[l];
59365936
uint16_t xi = ((uint16_t) q * 3) >> 8;
5937-
sum += (xi - 1) * y[i].qs[sizeof(x->q)*5 + l*sizeof(x->qs) + j];
5937+
sum += (xi - 1) * y[i].qs[sizeof(x->qs)*5 + l*sizeof(x->qh) + j];
59385938
}
59395939
}
59405940

@@ -5966,9 +5966,9 @@ void ggml_vec_dot_tq2_0_q8_K(int n, float * restrict s, size_t bs, const void *
59665966
int16x8_t sumi0 = vdupq_n_s16(0);
59675967
int16x8_t sumi1 = vdupq_n_s16(0);
59685968

5969-
for (size_t j = 0; j < sizeof(x->q); j += 32) {
5970-
uint8x16_t qx0 = vld1q_u8(x[i].q + j);
5971-
uint8x16_t qx1 = vld1q_u8(x[i].q + j + 16);
5969+
for (size_t j = 0; j < sizeof(x->qs); j += 32) {
5970+
uint8x16_t qx0 = vld1q_u8(x[i].qs + j);
5971+
uint8x16_t qx1 = vld1q_u8(x[i].qs + j + 16);
59725972
uint8x16_t qx2 = vshrq_n_u8(qx0, 2);
59735973
uint8x16_t qx3 = vshrq_n_u8(qx1, 2);
59745974
uint8x16_t qx4 = vshrq_n_u8(qx0, 4);
@@ -6033,8 +6033,8 @@ void ggml_vec_dot_tq2_0_q8_K(int n, float * restrict s, size_t bs, const void *
60336033
__m256i sumi0 = _mm256_setzero_si256();
60346034
__m256i sumi1 = _mm256_setzero_si256();
60356035

6036-
for (size_t j = 0; j < sizeof(x->q); j += 32) {
6037-
__m256i qx0 = _mm256_loadu_si256((const __m256i *) (x[i].q + j));
6036+
for (size_t j = 0; j < sizeof(x->qs); j += 32) {
6037+
__m256i qx0 = _mm256_loadu_si256((const __m256i *) (x[i].qs + j));
60386038
__m256i qx1 = _mm256_srli_epi16(qx0, 2);
60396039
__m256i qx2 = _mm256_srli_epi16(qx0, 4);
60406040
__m256i qx3 = _mm256_srli_epi16(qx0, 6);
@@ -6077,10 +6077,10 @@ void ggml_vec_dot_tq2_0_q8_K(int n, float * restrict s, size_t bs, const void *
60776077
for (int i = 0; i < nb; ++i) {
60786078
int32_t sumi = 0;
60796079

6080-
for (size_t j = 0; j < sizeof(x->q); j += 32) {
6080+
for (size_t j = 0; j < sizeof(x->qs); j += 32) {
60816081
for (size_t l = 0; l < 4; ++l) {
60826082
for (size_t k = 0; k < 32; ++k) {
6083-
sumi += y[i].qs[j*4 + l*32 + k] * (((x[i].q[j + k] >> (l*2)) & 3) - 1);
6083+
sumi += y[i].qs[j*4 + l*32 + k] * (((x[i].qs[j + k] >> (l*2)) & 3) - 1);
60846084
}
60856085
}
60866086
}

0 commit comments

Comments
 (0)