Skip to content

armgit / ente

Public Imported from GitHub
0

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

Drop redundant normalization of the CLIP text embedding

laurenspriemceeaa2e553b2
diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.ts
index fc5180dc2a..6c36897c6f 100644
--- a/web/packages/new/photos/services/ml/clip.ts
+++ b/web/packages/new/photos/services/ml/clip.ts
@@ -1,6 +1,6 @@
import type { ElectronMLWorker } from "ente-base/types/ipc";
import { savedCLIPIndexes } from "./db";
-import { dotProduct, norm } from "./math";
+import { dotProduct } from "./math";
import type { CLIPMatches } from "./worker-types";
/**
@@ -84,11 +84,6 @@ export type LocalCLIPIndex = CLIPIndex & {
fileID: number;
};
-const normalized = (embedding: Float32Array) => {
- const n = norm(embedding);
- return embedding.map((v) => v / n);
-};
-
/**
* Find the files whose CLIP embedding "matches" the given {@link searchPhrase}.
*
@@ -99,10 +94,10 @@ export const _clipMatches = async (
searchPhrase: string,
electron: ElectronMLWorker,
): Promise<CLIPMatches | undefined> => {
- const t = await electron.computeCLIPTextEmbeddingIfAvailable(searchPhrase);
- if (!t) return undefined;
-
- const textEmbedding = normalized(t);
+ // The native pipeline returns the embedding already normalized.
+ const textEmbedding =
+ await electron.computeCLIPTextEmbeddingIfAvailable(searchPhrase);
+ if (!textEmbedding) return undefined;
const items = (await cachedOrReadCLIPIndexes()).map(
({ fileID, embedding }) =>
// The dot product gives us cosine similarity here since both the
diff --git a/web/packages/new/photos/services/ml/math.ts b/web/packages/new/photos/services/ml/math.ts
index b4506f0fff..4f786f5944 100644
--- a/web/packages/new/photos/services/ml/math.ts
+++ b/web/packages/new/photos/services/ml/math.ts
@@ -64,13 +64,3 @@ export const dotProduct = (v1: Float32Array, v2: Float32Array) => {
for (let i = 0; i < v1.length; i++) d += v1[i]! * v2[i]!;
return d;
};
-
-/**
- * Return the L2-norm ("magnitude") of the given vector.
- *
- * L2-norm is the sqrt of the sum of the squares of the components of the
- * vector. It can also be thought of as the sqrt of the dot product of the
- * vector with itself.
- */
-export const norm = (v: Float32Array) =>
- Math.sqrt(v.reduce((a, x) => a + x * x, 0));