Skip to content

armgit / ente

Public Imported from GitHub
0

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

Drop pet indexes from MlStore

laurenspriemcaebed96aa6a
diff --git a/rust/crates/photos/src/ml_db/mod.rs b/rust/crates/photos/src/ml_db/mod.rs
index 5714cce87e..e4bb606aa3 100644
--- a/rust/crates/photos/src/ml_db/mod.rs
+++ b/rust/crates/photos/src/ml_db/mod.rs
@@ -98,7 +98,6 @@ pub(crate) mod tests {
}
pub(crate) use super::queries::clusters::tests::deny_cluster_summary_inserts_after;
- pub(crate) use super::queries::pets::tests::seed as seed_pet_rows;
pub(super) use cases;
pub(super) fn open() -> (TempDir, MlDb) {
diff --git a/rust/crates/photos/src/ml_db/queries/pets.rs b/rust/crates/photos/src/ml_db/queries/pets.rs
index 628fb9db02..fb56a29056 100644
--- a/rust/crates/photos/src/ml_db/queries/pets.rs
+++ b/rust/crates/photos/src/ml_db/queries/pets.rs
@@ -340,7 +340,7 @@ fn read_pet_body(row: &Row<'_>) -> db::Result<PetBodyRow> {
}
#[cfg(test)]
-pub(crate) mod tests {
+pub(in crate::ml_db) mod tests {
use std::collections::HashMap;
use super::{MlDb, PetBodyRow, PetFaceRow};
@@ -375,7 +375,7 @@ pub(crate) mod tests {
}
}
- pub(crate) fn seed(db: &MlDb) {
+ pub(in crate::ml_db) fn seed(db: &MlDb) {
db.bulk_insert_pet_faces(&[
pet_face(1, 0, 0),
pet_face(1, 1, -1),
diff --git a/rust/crates/photos/src/ml_store/index.rs b/rust/crates/photos/src/ml_store/index.rs
index 8873ed36ac..0ca18c14c8 100644
--- a/rust/crates/photos/src/ml_store/index.rs
+++ b/rust/crates/photos/src/ml_store/index.rs
@@ -9,49 +9,20 @@ use super::{Error, IndexResult, Result, state};
use crate::ml_db::{CLIP_EMBEDDING_DIMENSIONS, MlDb};
pub const CLUSTER_CENTROID_DIMENSIONS: usize = 192;
-pub const PET_FACE_DIMENSIONS: usize = 128;
-pub const PET_BODY_DIMENSIONS: usize = 192;
-
-#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
-pub enum Species {
- Dog,
- Cat,
-}
-
-impl Species {
- pub fn from_sql(species: i64) -> Option<Self> {
- match species {
- 0 => Some(Self::Dog),
- 1 => Some(Self::Cat),
- _ => None,
- }
- }
-}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Index {
Clip,
ClusterCentroid,
- PetFace(Species),
- PetBody(Species),
}
impl Index {
- pub const ALL: [Self; 6] = [
- Self::Clip,
- Self::ClusterCentroid,
- Self::PetFace(Species::Dog),
- Self::PetFace(Species::Cat),
- Self::PetBody(Species::Dog),
- Self::PetBody(Species::Cat),
- ];
+ pub const ALL: [Self; 2] = [Self::Clip, Self::ClusterCentroid];
pub fn dims(self) -> usize {
match self {
Self::Clip => CLIP_EMBEDDING_DIMENSIONS,
Self::ClusterCentroid => CLUSTER_CENTROID_DIMENSIONS,
- Self::PetFace(_) => PET_FACE_DIMENSIONS,
- Self::PetBody(_) => PET_BODY_DIMENSIONS,
}
}
@@ -59,10 +30,6 @@ impl Index {
match self {
Self::Clip => "clip",
Self::ClusterCentroid => "cluster_centroid",
- Self::PetFace(Species::Dog) => "pet.dog_face",
- Self::PetFace(Species::Cat) => "pet.cat_face",
- Self::PetBody(Species::Dog) => "pet.dog_body",
- Self::PetBody(Species::Cat) => "pet.cat_body",
}
}
@@ -70,10 +37,6 @@ impl Index {
match self {
Self::Clip => 0,
Self::ClusterCentroid => 1,
- Self::PetFace(Species::Dog) => 2,
- Self::PetFace(Species::Cat) => 3,
- Self::PetBody(Species::Dog) => 4,
- Self::PetBody(Species::Cat) => 5,
}
}
@@ -220,9 +183,8 @@ mod tests {
use crate::ml_db::CLIP_EMBEDDING_DIMENSIONS;
use crate::ml_store::tests::{
DB_FILE, centroid, clips, index_path, live_count, lose_index_files, meta, one_hot, open,
- pet,
};
- use crate::ml_store::{Error, FillOutcome, FillReport, FillState, MlStore, Species};
+ use crate::ml_store::{Error, FillOutcome, FillReport, FillState, MlStore};
#[test]
fn index_positions_follow_the_all_order() {
@@ -328,16 +290,6 @@ mod tests {
}
);
assert_eq!(live_count(&store, Index::ClusterCentroid), 1);
-
- let dims = Index::PetFace(Species::Dog).dims();
- store
- .store_pet_face_embeddings(&[
- pet("", Species::Dog, 1, dims),
- pet("p", Species::Dog, 2, dims),
- ])
- .unwrap();
- assert_eq!(live_count(&store, Index::PetFace(Species::Dog)), 1);
- assert!(store.contains(Index::PetFace(Species::Dog), "p").unwrap());
}
#[test]
diff --git a/rust/crates/photos/src/ml_store/mod.rs b/rust/crates/photos/src/ml_store/mod.rs
index beb7f842ba..6c73fc359d 100644
--- a/rust/crates/photos/src/ml_store/mod.rs
+++ b/rust/crates/photos/src/ml_store/mod.rs
@@ -13,11 +13,8 @@ use crate::ml_db::MlDb;
pub use ente_vecdb::{KeyMatches, Match, SearchParams, Stats};
pub use fill::{FillOutcome, FillReport};
-pub use index::{
- CLUSTER_CENTROID_DIMENSIONS, Index, PET_BODY_DIMENSIONS, PET_FACE_DIMENSIONS, Species,
-};
+pub use index::{CLUSTER_CENTROID_DIMENSIONS, Index};
pub use state::FillState;
-pub use writes::PetEmbedding;
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -149,7 +146,7 @@ mod tests {
use ente_vecdb::{OpenCost, SearchParams, VecDb};
use tempfile::TempDir;
- use super::{FillOutcome, FillReport, FillState, Index, MlStore, PetEmbedding, Species};
+ use super::{FillOutcome, FillReport, FillState, Index, MlStore};
use crate::ml_db::vector_encoding::encode_evector;
use crate::ml_db::{CLIP_EMBEDDING_DIMENSIONS, ClipEmbedding, ClusterSummary};
@@ -210,14 +207,6 @@ mod tests {
(cluster_id.to_string(), summary)
}
- pub(super) fn pet(id: &str, species: Species, hot: usize, dims: usize) -> PetEmbedding {
- PetEmbedding {
- id: id.to_string(),
- species,
- embedding: one_hot(dims, hot),
- }
- }
-
pub(super) fn nearest(store: &MlStore, index: Index, query: &[f32]) -> String {
let params = SearchParams {
limit: Some(1),
@@ -275,14 +264,6 @@ mod tests {
store.fill_state(Index::ClusterCentroid).unwrap(),
FillState::Stale
);
- assert_eq!(
- store.fill_state(Index::PetFace(Species::Dog)).unwrap(),
- FillState::Filled
- );
- assert_eq!(
- store.fill_state(Index::PetBody(Species::Cat)).unwrap(),
- FillState::Filled
- );
}
#[test]
diff --git a/rust/crates/photos/src/ml_store/state.rs b/rust/crates/photos/src/ml_store/state.rs
index b526e79125..5fe5452c01 100644
--- a/rust/crates/photos/src/ml_store/state.rs
+++ b/rust/crates/photos/src/ml_store/state.rs
@@ -18,25 +18,21 @@ struct Keys {
cursor: &'static str,
}
-fn keys(index: Index) -> Option<Keys> {
+fn keys(index: Index) -> Keys {
match index {
- Index::Clip => Some(Keys {
+ Index::Clip => Keys {
fill: "clip.fill",
cursor: "clip.cursor",
- }),
- Index::ClusterCentroid => Some(Keys {
+ },
+ Index::ClusterCentroid => Keys {
fill: "cluster_centroid.fill",
cursor: "cluster_centroid.cursor",
- }),
- Index::PetFace(_) | Index::PetBody(_) => None,
+ },
}
}
pub(super) fn read(db: &MlDb, index: Index) -> Result<FillState> {
- let Some(keys) = keys(index) else {
- return Ok(FillState::Filled);
- };
- Ok(match db.get_meta(keys.fill)?.as_deref() {
+ Ok(match db.get_meta(keys(index).fill)?.as_deref() {
Some(FILLED) => FillState::Filled,
Some(FILLING) => FillState::Filling,
_ => FillState::Stale,
@@ -44,45 +40,37 @@ pub(super) fn read(db: &MlDb, index: Index) -> Result<FillState> {
}
pub(super) fn cursor(db: &MlDb, index: Index) -> Result<Option<String>> {
- match keys(index) {
- Some(keys) => Ok(db.get_meta(keys.cursor)?),
- None => Ok(None),
- }
+ Ok(db.get_meta(keys(index).cursor)?)
}
pub(super) fn set_cursor(db: &MlDb, index: Index, key: &str) -> Result<()> {
- if let Some(keys) = keys(index) {
- db.set_meta(keys.cursor, key)?;
- }
+ db.set_meta(keys(index).cursor, key)?;
Ok(())
}
pub(super) fn mark_filling(db: &MlDb, index: Index) -> Result<()> {
- if let Some(keys) = keys(index) {
- db.delete_meta(keys.cursor)?;
- db.set_meta(keys.fill, FILLING)?;
- }
+ let keys = keys(index);
+ db.delete_meta(keys.cursor)?;
+ db.set_meta(keys.fill, FILLING)?;
Ok(())
}
pub(super) fn mark_filled(db: &MlDb, index: Index) -> Result<()> {
- if let Some(keys) = keys(index) {
- db.set_meta(keys.fill, FILLED)?;
- db.delete_meta(keys.cursor)?;
- }
+ let keys = keys(index);
+ db.set_meta(keys.fill, FILLED)?;
+ db.delete_meta(keys.cursor)?;
Ok(())
}
pub(super) fn mark_stale(db: &MlDb, index: Index) -> Result<()> {
- if let Some(keys) = keys(index) {
- db.delete_meta(keys.fill)?;
- db.delete_meta(keys.cursor)?;
- }
+ let keys = keys(index);
+ db.delete_meta(keys.fill)?;
+ db.delete_meta(keys.cursor)?;
Ok(())
}
pub(super) fn invalidate_lost_index(db: &MlDb, index: Index, path: &Path) -> Result<()> {
- if keys(index).is_none() || read(db, index)? == FillState::Stale {
+ if read(db, index)? == FillState::Stale {
return Ok(());
}
log::warn!("{}: index files missing, refill required", path.display());
diff --git a/rust/crates/photos/src/ml_store/writes.rs b/rust/crates/photos/src/ml_store/writes.rs
index 191ceace88..6f6f1c3391 100644
--- a/rust/crates/photos/src/ml_store/writes.rs
+++ b/rust/crates/photos/src/ml_store/writes.rs
@@ -3,15 +3,8 @@ use std::collections::HashMap;
use ente_vecdb::VecDb;
use super::fill::decode_centroid;
-use super::{Error, FillState, Index, IndexResult, MlStore, Result, Species, state};
-use crate::ml_db::{ClipEmbedding, ClusterSummary, PetRowsForFiles};
-
-#[derive(Clone, Debug, PartialEq)]
-pub struct PetEmbedding {
- pub id: String,
- pub species: Species,
- pub embedding: Vec<f32>,
-}
+use super::{Error, FillState, Index, IndexResult, MlStore, Result, state};
+use crate::ml_db::{ClipEmbedding, ClusterSummary};
type Vectors = (Vec<String>, Vec<Vec<f32>>);
@@ -78,36 +71,6 @@ impl MlStore {
self.reset_index(Index::ClusterCentroid)
}
- pub fn store_pet_face_embeddings(&self, embeddings: &[PetEmbedding]) -> Result<()> {
- self.store_pet_embeddings(embeddings, Index::PetFace)
- }
-
- pub fn store_pet_body_embeddings(&self, embeddings: &[PetEmbedding]) -> Result<()> {
- self.store_pet_embeddings(embeddings, Index::PetBody)
- }
-
- pub fn delete_pet_data_for_files(&self, file_ids: &[i64]) -> Result<()> {
- if file_ids.is_empty() {
- return Ok(());
- }
- let _mutations = self.lock_mutations();
- let rows = self.db.get_pet_rows_for_files(file_ids)?;
- for (index, keys) in pet_keys(&rows) {
- if let Err(error) = self.with_index(index, |vecdb| vecdb.bulk_remove(&keys)) {
- log::warn!(
- "failed to remove {} vectors from {}: {error}",
- keys.len(),
- index.name()
- );
- }
- }
- let face_ids: Vec<String> = rows.faces.into_iter().map(|row| row.pet_face_id).collect();
- let body_ids: Vec<String> = rows.bodies.into_iter().map(|row| row.pet_body_id).collect();
- self.db
- .delete_pet_rows_for_files(file_ids, &face_ids, &body_ids)?;
- Ok(())
- }
-
pub fn clear_all(&self) -> Result<()> {
let _mutations = self.lock_mutations();
self.db.clear_non_pet_tables()?;
@@ -119,21 +82,6 @@ impl MlStore {
Ok(())
}
- fn store_pet_embeddings(
- &self,
- embeddings: &[PetEmbedding],
- index_of: fn(Species) -> Index,
- ) -> Result<()> {
- if embeddings.is_empty() {
- return Ok(());
- }
- let _mutations = self.lock_mutations();
- for (index, (keys, vectors)) in pet_vectors(embeddings, index_of) {
- self.index_write(index, |vecdb| vecdb.bulk_add(&keys, &vectors))?;
- }
- Ok(())
- }
-
fn index_is_stale(&self, index: Index) -> Result<bool> {
Ok(state::read(&self.db, index)? == FillState::Stale)
}
@@ -180,51 +128,20 @@ fn centroid_vectors(summary: &HashMap<String, ClusterSummary>) -> Vectors {
.unzip()
}
-fn pet_vectors(
- embeddings: &[PetEmbedding],
- index_of: fn(Species) -> Index,
-) -> HashMap<Index, Vectors> {
- let mut grouped: HashMap<Index, Vectors> = HashMap::new();
- for embedding in embeddings {
- let index = index_of(embedding.species);
- if !index.accepts(&embedding.id, &embedding.embedding) {
- continue;
- }
- let (keys, vectors) = grouped.entry(index).or_default();
- keys.push(embedding.id.clone());
- vectors.push(embedding.embedding.clone());
- }
- grouped
-}
-
-fn pet_keys(rows: &PetRowsForFiles) -> HashMap<Index, Vec<String>> {
- let faces = rows.faces.iter().filter_map(|row| {
- Species::from_sql(row.species).map(|species| (Index::PetFace(species), &row.pet_face_id))
- });
- let bodies = rows.bodies.iter().filter_map(|row| {
- Species::from_sql(row.species).map(|species| (Index::PetBody(species), &row.pet_body_id))
- });
- let mut grouped: HashMap<Index, Vec<String>> = HashMap::new();
- for (index, id) in faces.chain(bodies) {
- grouped.entry(index).or_default().push(id.clone());
- }
- grouped
-}
-
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use ente_vecdb::{OpenCost, SearchParams, VecDb, VecDbError};
- use crate::ml_db::tests::{deny_cluster_summary_inserts_after, seed_pet_rows};
+ use crate::ml_db::tests::deny_cluster_summary_inserts_after;
use crate::ml_db::vector_encoding::encode_evector;
use crate::ml_db::{CLIP_EMBEDDING_DIMENSIONS, ClusterSummary};
use crate::ml_store::tests::{
DB_FILE, centroid, clips, empty, hot_position, index_path, live_count, meta, nearest,
- one_hot, open, open_with_unusable_clip_index, pet,
+ one_hot, open, open_with_unusable_clip_index,
};
- use crate::ml_store::{Error, FillOutcome, FillState, Index, Species};
+ use crate::ml_store::{Error, FillOutcome, FillState, Index};
#[test]
fn put_clip_round_trips_through_sql_and_index() {
@@ -427,62 +344,6 @@ mod tests {
assert_eq!(store.db().count_cluster_summaries().unwrap(), 0);
}
- #[test]
- fn pet_embeddings_are_stored_searched_and_deleted_per_species() {
- let (_directory, store) = open();
- seed_pet_rows(store.db());
- let face_dims = Index::PetFace(Species::Dog).dims();
- let body_dims = Index::PetBody(Species::Dog).dims();
- store
- .store_pet_face_embeddings(&[
- pet("1_pet_0", Species::Dog, 1, face_dims),
- pet("2_pet_0", Species::Cat, 2, face_dims),
- pet("bad", Species::Dog, 3, face_dims / 2),
- ])
- .unwrap();
- store
- .store_pet_body_embeddings(&[pet("1_body_0", Species::Dog, 1, body_dims)])
- .unwrap();
- store.store_pet_body_embeddings(&[]).unwrap();
- assert_eq!(
- nearest(&store, Index::PetFace(Species::Dog), &one_hot(face_dims, 1)),
- "1_pet_0"
- );
- assert!(
- store
- .contains(Index::PetFace(Species::Cat), "2_pet_0")
- .unwrap()
- );
- assert!(!store.contains(Index::PetFace(Species::Dog), "bad").unwrap());
- assert_eq!(live_count(&store, Index::PetFace(Species::Dog)), 1);
- assert!(
- store
- .contains(Index::PetBody(Species::Dog), "1_body_0")
- .unwrap()
- );
-
- store.delete_pet_data_for_files(&[]).unwrap();
- store.delete_pet_data_for_files(&[1]).unwrap();
- assert!(
- !store
- .contains(Index::PetFace(Species::Dog), "1_pet_0")
- .unwrap()
- );
- assert!(
- !store
- .contains(Index::PetBody(Species::Dog), "1_body_0")
- .unwrap()
- );
- assert!(
- store
- .contains(Index::PetFace(Species::Cat), "2_pet_0")
- .unwrap()
- );
- assert!(store.db().get_pet_faces_for_file_id(1).unwrap().is_empty());
- assert!(store.db().get_pet_bodies_for_file_id(1).unwrap().is_empty());
- assert_eq!(store.db().get_pet_indexed_file_count(1).unwrap(), 1);
- }
-
#[test]
fn clear_all_removes_rows_meta_and_index_files() {
let (directory, store) = open();
@@ -492,14 +353,6 @@ mod tests {
store
.cluster_summary_update(&HashMap::from([centroid("c1", 1)]))
.unwrap();
- store
- .store_pet_face_embeddings(&[pet(
- "p",
- Species::Cat,
- 1,
- Index::PetFace(Species::Cat).dims(),
- )])
- .unwrap();
store.clear_all().unwrap();
for index in Index::ALL {
@@ -520,7 +373,6 @@ mod tests {
assert_eq!(store.db().count_clip_rows().unwrap(), 0);
assert_eq!(store.db().count_cluster_summaries().unwrap(), 0);
assert!(!store.contains(Index::Clip, "1").unwrap());
- assert!(!store.contains(Index::PetFace(Species::Cat), "p").unwrap());
assert_eq!(live_count(&store, Index::Clip), 0);
assert_eq!(
store.fill_clip_index(false).unwrap(),