ravendb
Version:
RavenDB client for Node.js
54 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VectorQuantizer = void 0;
class VectorQuantizer {
/**
* Converts a float array to an int8 array.
* Finds the maximum absolute value and scales all values to fit in int8 range (-127 to 127).
* Appends the maximum absolute value as a float at the end.
*
* @param rawEmbedding The float array to convert
* @returns A new array with the quantized values
*/
static toInt8(rawEmbedding) {
const length = rawEmbedding.length;
const result = new Array(length + 4);
let maxAbsValue = 0;
for (let i = 0; i < length; i++) {
maxAbsValue = Math.max(maxAbsValue, Math.abs(rawEmbedding[i]));
}
const scaleFactor = maxAbsValue === 0 ? 1 : 127 / maxAbsValue;
for (let i = 0; i < length; i++) {
result[i] = Math.round(rawEmbedding[i] * scaleFactor);
}
const buffer = new ArrayBuffer(4);
const dataView = new DataView(buffer);
dataView.setFloat32(0, maxAbsValue, true);
for (let i = 0; i < 4; i++) {
result[length + i] = dataView.getInt8(i);
}
return result;
}
/**
* Converts a float array to a binary representation where each value is represented by 1 bit.
* 1 if the value is non-negative, 0 if negative. Packs 8 values per byte.
*
* @param rawEmbedding The float array to convert
* @returns A new array with the binary-packed values
*/
static toInt1(rawEmbedding) {
const length = rawEmbedding.length;
const outputLength = Math.ceil(length / 8);
const result = new Uint8Array(outputLength);
for (let i = 0; i < length; i++) {
const byteIndex = Math.floor(i / 8);
const bitPosition = 7 - (i % 8);
if (rawEmbedding[i] >= 0) {
result[byteIndex] |= (1 << bitPosition);
}
}
return Array.from(result);
}
}
exports.VectorQuantizer = VectorQuantizer;
//# sourceMappingURL=VectorQuantizer.js.map