Fix incorrect uint8 -> int8 conversion in JS cosine similarity.

PiperOrigin-RevId: 505135368
This commit is contained in:
MediaPipe Team 2023-01-27 09:20:07 -08:00 committed by Copybara-Service
parent 1df4511e9d
commit a6f6be9512
2 changed files with 3 additions and 3 deletions

View File

@ -70,12 +70,12 @@ describe('computeCosineSimilarity', () => {
it('succeeds with quantized embeddings', () => {
const u: Embedding = {
quantizedEmbedding: new Uint8Array([255, 128, 128, 128]),
quantizedEmbedding: new Uint8Array([127, 0, 0, 0]),
headIndex: 0,
headName: ''
};
const v: Embedding = {
quantizedEmbedding: new Uint8Array([0, 128, 128, 128]),
quantizedEmbedding: new Uint8Array([128, 0, 0, 0]),
headIndex: 0,
headName: ''
};

View File

@ -38,7 +38,7 @@ export function computeCosineSimilarity(u: Embedding, v: Embedding): 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[]) {