Fix incorrect uint8 -> int8 conversion in JS cosine similarity.
PiperOrigin-RevId: 505135368
This commit is contained in:
parent
1df4511e9d
commit
a6f6be9512
|
@ -70,12 +70,12 @@ describe('computeCosineSimilarity', () => {
|
||||||
|
|
||||||
it('succeeds with quantized embeddings', () => {
|
it('succeeds with quantized embeddings', () => {
|
||||||
const u: Embedding = {
|
const u: Embedding = {
|
||||||
quantizedEmbedding: new Uint8Array([255, 128, 128, 128]),
|
quantizedEmbedding: new Uint8Array([127, 0, 0, 0]),
|
||||||
headIndex: 0,
|
headIndex: 0,
|
||||||
headName: ''
|
headName: ''
|
||||||
};
|
};
|
||||||
const v: Embedding = {
|
const v: Embedding = {
|
||||||
quantizedEmbedding: new Uint8Array([0, 128, 128, 128]),
|
quantizedEmbedding: new Uint8Array([128, 0, 0, 0]),
|
||||||
headIndex: 0,
|
headIndex: 0,
|
||||||
headName: ''
|
headName: ''
|
||||||
};
|
};
|
||||||
|
|
|
@ -38,7 +38,7 @@ export function computeCosineSimilarity(u: Embedding, v: Embedding): number {
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertToBytes(data: Uint8Array): number[] {
|
function convertToBytes(data: Uint8Array): number[] {
|
||||||
return Array.from(data, v => v - 128);
|
return Array.from(data, v => v > 127 ? v - 256 : v);
|
||||||
}
|
}
|
||||||
|
|
||||||
function compute(u: number[], v: number[]) {
|
function compute(u: number[], v: number[]) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user