Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Clean up

Manav Rathie2c0501e3d00
diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.ts
index 11058ec4c5..b4c70bdd59 100644
--- a/web/packages/new/photos/services/ml/clip.ts
+++ b/web/packages/new/photos/services/ml/clip.ts
@@ -2,7 +2,7 @@ import type { ElectronMLWorker } from "@/base/types/ipc";
import type { ImageBitmapAndData } from "./blob";
import { clipIndexes } from "./db";
import { pixelRGBBilinear } from "./image";
-import { dotProductF32, normF32 } from "./math";
+import { dotProductF32, norm } from "./math";
import type { CLIPMatches } from "./worker-types";
/**
@@ -158,7 +158,7 @@ const convertToCLIPInput = (imageData: ImageData) => {
};
const normalized = (embedding: Float32Array) => {
- const n = normF32(embedding);
+ const n = norm(embedding);
return embedding.map((v) => v / n);
};
diff --git a/web/packages/new/photos/services/ml/math.ts b/web/packages/new/photos/services/ml/math.ts
index c34385cc07..65c191456d 100644
--- a/web/packages/new/photos/services/ml/math.ts
+++ b/web/packages/new/photos/services/ml/math.ts
@@ -12,18 +12,38 @@ export const clamp = (value: number, min: number, max: number) =>
*
* Precondition: The two vectors must be of the same length.
*
+ * [Note: Dot product is cosine similarity for normalized vectors]
+ *
+ * The cosine similarity of two vectors is defined as
+ *
+ * dotProduct(v1, v2) / (norm(v1) * norm(v2));
+ *
+ * In particular, when both the vectors are normalized, this is equal to the dot
+ * product. When we're computing dot products in a hot loop, skipping over that
+ * unnecessary renormalization matters.
+ *
+ * [Note: Cosine similarity and cosine distance]
+ *
+ * The cosine similarity of two vectors is [-1, 1] (inclusive), indicating how
+ * similar (1), orthogonal (0) and dissimilar (1) the two vectors are in
+ * direction. A related concept is cosine distance, which is defined as
+ *
+ * 1 - cosine similarity
+ *
+ * in an attempt to convert it into a (pseudo) distance metric.
+ *
* [Note: Dot product performance]
*
* In theory, WASM SIMD instructions should give us a huge boost for computing
* dot products. In practice, we can get to roughly around the same performance
* by using Float32Arrays instead of number[], and letting the JS JIT do the
- * optimizations for us. (This assertion was made on Chrome on macOS on Sep
- * 2023, and may not hold in the future).
+ * optimizations for us (This assertion was made on Chrome on macOS on Sep 2023,
+ * and may not hold in the future).
*
- * We can get an extra 2x speedup over this by using some library that directly
+ * We can get a further 2x speedup over this by using some library that directly
* uses the SIMD intrinsics provided by the architecture instead of limiting
- * itself to the WASM's set. But that requires bundling native code, so as a
- * tradeoff to avoid complexity we live with leaving that 1x on the table.
+ * itself to WASM's set. But that'll require bundling native code, so as a
+ * tradeoff to avoid complexity we currently leave that 1x on the table.
*/
export const dotProduct = (v1: number[], v2: number[]) => {
if (v1.length != v2.length)
@@ -34,6 +54,11 @@ export const dotProduct = (v1: number[], v2: number[]) => {
return d;
};
+/**
+ * Float32Array variant of {@link dotProduct}.
+ *
+ * See: [Note: Dot product performance].
+ */
export const dotProductF32 = (v1: Float32Array, v2: Float32Array) => {
if (v1.length != v2.length)
throw new Error(`Length mismatch ${v1.length} ${v2.length}`);
@@ -50,19 +75,5 @@ export const dotProductF32 = (v1: Float32Array, v2: Float32Array) => {
* vector. It can also be thought of as the sqrt of the dot product of the
* vector with itself.
*/
-export const norm = (v: number[]) =>
- Math.sqrt(v.reduce((a, x) => a + x * x, 0));
-
-export const normF32 = (v: Float32Array) =>
+export const norm = (v: Float32Array) =>
Math.sqrt(v.reduce((a, x) => a + x * x, 0));
-
-/**
- * Return the cosine similarity of the two given vectors.
- *
- * The result is a value between [-1, 1] (inclusive), indicating how similar
- * (1), orthogonal (0) and dissimilar (1) the two vectors are in direction.
- *
- * Precondition: The two vectors must be of the same length.
- */
-export const cosineSimilarity = (v1: number[], v2: number[]) =>
- dotProduct(v1, v2) / (norm(v1) * norm(v2));