Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use the new server enum

Manav Rathia7e0c5b61d1d
diff --git a/web/packages/new/photos/services/ml/embedding.ts b/web/packages/new/photos/services/ml/embedding.ts
index 18d71ed87e..6da4e765d1 100644
--- a/web/packages/new/photos/services/ml/embedding.ts
+++ b/web/packages/new/photos/services/ml/embedding.ts
@@ -10,31 +10,24 @@ import { type RemoteCLIPIndex } from "./clip";
import { type RemoteFaceIndex } from "./face";
/**
- * The embeddings that we (the current client) knows how to handle.
+ * [Note: Derived embeddings and other metadata]
*
- * This is an exhaustive set of values we pass when GET-ing or PUT-ting
- * encrypted embeddings from remote. However, we should be prepared to receive a
- * {@link RemoteEmbedding} with a model value different from these.
+ * The APIs they deal with derived data started in a ML context, and would store
+ * embeddings generated by particular models. Thus the API endpoints use the
+ * name "embedding", and are parameterized by a "model" enum.
*
- * [Note: Embedding/model vs derived data]
+ * Next step in the evolution was that instead of just storing the embedding,
+ * the code also started storing various additional data generated by the ML
+ * model. For example, the face indexing process generates multiple face
+ * embeddings per file, each with an associated detection box. So instead of
+ * storing just a singular embedding, the data that got stored was this entire
+ * face index structure containing multiple embeddings and associated data.
*
- * Historically, this has been called an "embedding" or a "model" in the API
- * terminology. However, it is more like derived data.
- *
- * It started off being called as "model", but strictly speaking it was not just
- * the model, but the embedding produced by a particular ML model when used with
- * a particular set of pre-/post- processing steps and related hyperparameters.
- * It is better thought of as an "type" of embedding produced or consumed by the
- * client, e.g. the "face" embedding, or the "clip" embedding.
- *
- * Even the word embedding is a synedoche, since it might have other data. For
- * example, for faces, it in not just the face embedding, but also the detection
- * regions, landmarks etc: What we've come to refer as the "face index" in our
- * client code terminology.
- *
- * Later on, to avoid the proliferation of small files (one per embedding), we
- * combined all these embeddings into a single "embedding", which is a map of
- * the form:
+ * Further down, it was realized that the fan out caused on remote when trying
+ * to fetch all derived data - both ML ("clip", "face") and non-ML ("exif") -
+ * was problematic, and also their raw JSON was unnecessarily big. To deal with
+ * these better, we now have a single "derived" model type, whose data is a
+ * gzipped map of the form:
*
* {
* "face": ... the face indexing result ...
@@ -42,26 +35,12 @@ import { type RemoteFaceIndex } from "./face";
* "exif": ... the Exif extracted from the file ...
* ... more in the future ...
* }
- *
- * Thus, now this is best thought of a tag for a particular format of encoding
- * all the derived data associated with a file.
*/
-// TODO-ML: Fix name to "combined" before release
-const wipModelName = process.env.NEXT_PUBLIC_ENTE_ENABLE_WIP_ML_DONT_USE ?? "";
-// type EmbeddingModel = "xxxx-xxxx" /* Combined format */;
-type EmbeddingModel = string; // "xxxx-xxxx" /* Combined format */;
+type EmbeddingModel = "derived";
const RemoteEmbedding = z.object({
/** The ID of the file whose embedding this is. */
fileID: z.number(),
- /**
- * The embedding "type".
- *
- * This can be an arbitrary string since there might be models the current
- * client does not know about; we limit our interactions to values that are
- * one of {@link EmbeddingModel}.
- */
- model: z.string(),
/**
* Base64 representation of the encrypted (model specific) embedding JSON.
*/
@@ -84,7 +63,7 @@ export type ParsedRemoteDerivedData = Partial<{
}>;
/**
- * The decrypted payload of a {@link RemoteEmbedding} for the "combined"
+ * The decrypted payload of a {@link RemoteEmbedding} for the "derived"
* {@link EmbeddingModel}.
*
* [Note: Preserve unknown derived data fields]
@@ -202,8 +181,7 @@ const ParsedRemoteDerivedData = z.object({
export const fetchDerivedData = async (
filesByID: Map<number, EnteFile>,
): Promise<Map<number, RemoteDerivedData>> => {
- // TODO-ML: Fix name to "combined" before release
- const remoteEmbeddings = await fetchEmbeddings(wipModelName, [
+ const remoteEmbeddings = await fetchEmbeddings("derived", [
...filesByID.keys(),
]);
@@ -212,7 +190,7 @@ export const fetchDerivedData = async (
const { fileID } = remoteEmbedding;
const file = filesByID.get(fileID);
if (!file) {
- log.warn(`Ignoring derived data for unknown fileID ${fileID}`);
+ log.warn(`Ignoring derived data for unknown file id ${fileID}`);
continue;
}
@@ -225,13 +203,13 @@ export const fetchDerivedData = async (
const jsonString = await gunzip(decryptedBytes);
result.set(fileID, remoteDerivedDataFromJSONString(jsonString));
} catch (e) {
- // This shouldn't happen. Likely some client has uploaded a
- // corrupted embedding. Ignore it so that it gets reindexed and
- // uploaded correctly again.
- log.warn(`Ignoring unparseable embedding for ${fileID}`, e);
+ // This shouldn't happen. Best guess is that some client has
+ // uploaded a corrupted embedding. Ignore it so that it gets
+ // reindexed and uploaded correctly again.
+ log.warn(`Ignoring unparseable embedding for file id ${fileID}`, e);
}
}
- log.debug(() => `Fetched ${result.size} combined embeddings`);
+ log.debug(() => `Fetched derived data for ${result.size} files`);
return result;
};
@@ -295,13 +273,7 @@ const fetchEmbeddings = async (
export const putDerivedData = async (
enteFile: EnteFile,
derivedData: RawRemoteDerivedData,
-) =>
- // TODO-ML: Fix name to "combined" before release
- putEmbedding(
- enteFile,
- wipModelName,
- await gzip(JSON.stringify(derivedData)),
- );
+) => putEmbedding(enteFile, "derived", await gzip(JSON.stringify(derivedData)));
/**
* Upload an embedding to remote.