MCPcopy Create free account
hub / github.com/dblalock/bolt / lut_dists_12b

Function lut_dists_12b

cpp/src/quantize/multi_codebook.hpp:433–476  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

431
432template<int NBytes, class dist_t>
433inline void lut_dists_12b(const uint8_t* codes, const dist_t* luts,
434 dist_t* dists_out, int64_t N)
435{
436 static constexpr int ncodebook_bits = 12;
437 static constexpr int total_bits = 8 * NBytes;
438 static constexpr int ncodewords = total_bits / ncodebook_bits;
439 static constexpr int codebook_sz = 1 << ncodebook_bits;
440 static constexpr bool nbytes_multiple_of_8 = NBytes % 8 == 0;
441 static constexpr uint16_t idx_mask = (1 << ncodebook_bits) - 1;
442 static constexpr uint8_t low_4bits_mask = 0x0F;
443 static constexpr uint8_t high_4bits_mask = 0xF0;
444 static_assert(NBytes >= 2, "NBytes < 2 is not meaningful");
445
446 for (int64_t i = 0; i < N; i++) {
447 dists_out[i] = 0;
448 for (int j = 0; j < ncodewords; j += 2) {
449 auto lut_ptr0 = luts + codebook_sz * j;
450 auto lut_ptr1 = lut_ptr0 + codebook_sz;
451
452 // packed indices format:
453 //
454 // codes[j] codes[j+1] codes[j+2]
455 // -------------------- ----------|---------- --------------------
456 // idx0[:8] idx0[8:12] idx1[8:12] idx1[:8]
457 uint16_t low_bits = codes[3*j+1] & low_4bits_mask;
458 uint16_t high_bits = codes[3*j+1] & high_4bits_mask;
459 uint16_t idx0 = codes[3*j] + (low_bits << 8);
460 uint16_t idx1 = codes[3*j+2] + (high_bits << 4);
461
462 dists_out[i] += lut_ptr0[idx0];
463 dists_out[i] += lut_ptr1[idx1];
464
465 // codes += 3;
466 }
467 if (ncodewords % 2 != 0) {
468 auto lut_ptr = luts + codebook_sz * (ncodewords - 1);
469 uint16_t low_bits = codes[NBytes-1] & low_4bits_mask; // use last byte
470 auto idx = codes[NBytes-2] + (low_bits << 8); // use 2nd to last byte
471 dists_out[i] += lut_ptr[idx];
472 }
473 codes += NBytes;
474 // codes += (NBytes % 3);
475 }
476}
477template<int B, int NBytes, class dist_t>
478inline void lut_dists_12b_vertical(const uint8_t* codes, const dist_t* luts,
479 dist_t* dists_out, int64_t N)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected