MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / lookup_code_index

Function lookup_code_index

csrc/cpu_ops.cpp:20–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18constexpr int kCodebookSize = 256;
19
20inline unsigned char lookup_code_index(const float* codebook, float value) {
21 value = std::clamp(value, -1.0f, 1.0f);
22 const float* begin = codebook;
23 const float* end = codebook + kCodebookSize;
24 const float* right = std::lower_bound(begin, end, value);
25 if (right == begin) {
26 return 0;
27 }
28 if (right == end) {
29 return static_cast<unsigned char>(kCodebookSize - 1);
30 }
31 const float* left = right - 1;
32 const float dist_left = std::fabs(value - *left);
33 const float dist_right = std::fabs(*right - value);
34 const unsigned char idx = static_cast<unsigned char>(right - begin);
35 return dist_right < dist_left ? idx : idx - 1;
36}
37
38} // namespace
39

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected