Skip to content

armgit / ente

Public Imported from GitHub
0

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

8.9 KB
Raw Blame
use std::collections::HashMap;
use std::num::NonZeroUsize;

use super::helpers::pair;
use crate::db::{self, Row};
use crate::ml_db::schema;
use crate::ml_db::vector_encoding::{decode_f32, encode_f32};
use crate::ml_db::{MlDb, Result};

pub const CLIP_ML_VERSION: i64 = 1;

pub const CLIP_EMBEDDING_DIMENSIONS: usize = 512;
pub const CLIP_EMBEDDING_BYTES_LENGTH: i64 = CLIP_EMBEDDING_DIMENSIONS as i64 * 4;

#[derive(Clone, Debug, PartialEq)]
pub struct ClipEmbedding {
    pub file_id: i64,
    pub embedding: Vec<f64>,
    pub version: i64,
}

#[derive(Clone, Debug, PartialEq)]
pub struct EmbeddingVector {
    pub file_id: i64,
    pub embedding: Vec<f32>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClipRow {
    pub file_id: i64,
    pub embedding: Vec<u8>,
}

impl MlDb {
    pub fn get_all_clip_vectors(&self) -> Result<Vec<EmbeddingVector>> {
        let rows: Vec<(i64, Vec<u8>)> =
            self.read_all("SELECT file_id, embedding FROM clip", (), pair)?;
        Ok(rows
            .into_iter()
            .map(|(file_id, embedding)| EmbeddingVector {
                file_id,
                embedding: decode_f32(&embedding),
            })
            .filter(|vector| !vector.embedding.is_empty())
            .collect())
    }

    pub fn clip_indexed_file_with_version(&self) -> Result<HashMap<i64, i64>> {
        self.read_all("SELECT file_id , ml_version FROM clip", (), pair)
    }

    pub fn get_clip_indexed_file_count(&self, minimum_ml_version: i64) -> Result<i64> {
        self.read_value(
            "SELECT COUNT(DISTINCT file_id) as count FROM clip WHERE ml_version >= ?",
            [minimum_ml_version],
        )
    }

    pub fn get_clip_vectorizable_file_count(&self, minimum_ml_version: i64) -> Result<i64> {
        self.read_value(
            r#"
                SELECT COUNT(DISTINCT file_id) as count
                FROM clip
                WHERE ml_version >= ?
                    AND LENGTH(embedding) = ?
                "#,
            [minimum_ml_version, CLIP_EMBEDDING_BYTES_LENGTH],
        )
    }

    pub fn insert_clip_rows(&self, embeddings: &[ClipEmbedding]) -> Result<()> {
        if let [embedding] = embeddings {
            self.execute(
                "INSERT OR REPLACE INTO clip (file_id, embedding, ml_version) VALUES (?, ?, ?)",
                clip_row(embedding),
            )?;
            return Ok(());
        }
        self.write_batch_atomic(
            "INSERT OR REPLACE INTO clip (file_id, embedding, ml_version) values(?, ?, ?)",
            embeddings.iter().map(clip_row),
        )
    }

    pub fn delete_clip_rows(&self, file_ids: &[i64]) -> Result<()> {
        self.execute_chunked_in("DELETE FROM clip WHERE file_id IN ({})", file_ids)
    }

    pub fn delete_all_clip_rows(&self) -> Result<()> {
        self.execute_statements([schema::DELETE_CLIP_EMBEDDINGS])
    }

    pub fn count_clip_rows(&self) -> Result<i64> {
        self.read_value("SELECT COUNT(file_id) as total FROM clip", ())
    }

    pub fn get_clip_rows_page(&self, limit: i64, offset: i64) -> Result<Vec<ClipRow>> {
        self.read_all(
            "SELECT file_id, embedding FROM clip ORDER BY file_id DESC LIMIT ? OFFSET ?",
            [limit, offset],
            read_clip_row,
        )
    }

    pub fn get_clip_rows_before(
        &self,
        before_file_id: Option<i64>,
        limit: NonZeroUsize,
    ) -> Result<Vec<ClipRow>> {
        let limit = limit.get() as i64;
        match before_file_id {
            None => self.read_all(
                "SELECT file_id, embedding FROM clip ORDER BY file_id DESC LIMIT ?",
                [limit],
                read_clip_row,
            ),
            Some(before_file_id) => self.read_all(
                r#"
                SELECT file_id, embedding
                FROM clip
                WHERE file_id < ?
                ORDER BY file_id DESC
                LIMIT ?
                "#,
                (before_file_id, limit),
                read_clip_row,
            ),
        }
    }
}

fn read_clip_row(row: &Row<'_>) -> db::Result<ClipRow> {
    Ok(ClipRow {
        file_id: row.get(0)?,
        embedding: row.get(1)?,
    })
}

fn clip_row(embedding: &ClipEmbedding) -> (i64, Vec<u8>, i64) {
    (
        embedding.file_id,
        encode_f32(embedding.embedding.iter().map(|value| *value as f32)),
        embedding.version,
    )
}

#[cfg(test)]
pub(in crate::ml_db) mod tests {
    use std::collections::HashMap;
    use std::num::NonZeroUsize;

    use super::{CLIP_EMBEDDING_DIMENSIONS, ClipEmbedding, ClipRow, MlDb};
    use crate::ml_db::tests::{cases, check, open};
    use crate::ml_db::vector_encoding::encode_f32;
    use tempfile::TempDir;

    fn clip(file_id: i64, embedding: Vec<f64>) -> ClipEmbedding {
        ClipEmbedding {
            file_id,
            embedding,
            version: 1,
        }
    }

    pub(in crate::ml_db) fn full_clip(file_id: i64) -> ClipEmbedding {
        clip(file_id, vec![0.25; CLIP_EMBEDDING_DIMENSIONS])
    }

    pub(in crate::ml_db) fn seed(db: &MlDb) {
        db.insert_clip_rows(&[
            full_clip(1),
            ClipEmbedding {
                version: 2,
                ..full_clip(2)
            },
            clip(3, vec![1.0, 2.0]),
            clip(4, vec![]),
        ])
        .unwrap();
    }

    fn seeded() -> (TempDir, MlDb) {
        let (directory, db) = open();
        seed(&db);
        (directory, db)
    }

    #[test]
    fn seeded_counts() {
        let (_directory, db) = seeded();
        check(
            &db,
            &cases![
                "clip files": 4 => |db| db.get_clip_indexed_file_count(1),
                "clip files v2": 1 => |db| db.get_clip_indexed_file_count(2),
                "clip vectorizable": 2 => |db| db.get_clip_vectorizable_file_count(1),
                "clip vectorizable v2": 1 => |db| db.get_clip_vectorizable_file_count(2),
                "clip rows": 4 => |db| db.count_clip_rows(),
            ],
        );
    }

    #[test]
    fn seeded_versions() {
        let (_directory, db) = seeded();
        check(
            &db,
            &cases![
                "clip versions": HashMap::from([(1, 1), (2, 2), (3, 1), (4, 1)]) =>
                    |db| db.clip_indexed_file_with_version(),
            ],
        );
    }

    #[test]
    fn seeded_rows() {
        let (_directory, db) = seeded();
        let mut vectors = db.get_all_clip_vectors().unwrap();
        vectors.sort_by_key(|vector| vector.file_id);
        let dimensions: Vec<(i64, usize)> = vectors
            .iter()
            .map(|v| (v.file_id, v.embedding.len()))
            .collect();
        assert_eq!(
            dimensions,
            vec![
                (1, CLIP_EMBEDDING_DIMENSIONS),
                (2, CLIP_EMBEDDING_DIMENSIONS),
                (3, 2)
            ]
        );
        assert_eq!(vectors[2].embedding, vec![1.0f32, 2.0]);
        let page = db.get_clip_rows_page(2, 1).unwrap();
        let clip_row = |file_id, embedding: Vec<f32>| ClipRow {
            file_id,
            embedding: encode_f32(embedding),
        };
        assert_eq!(
            page,
            vec![
                clip_row(3, vec![1.0, 2.0]),
                clip_row(2, vec![0.25; CLIP_EMBEDDING_DIMENSIONS])
            ]
        );
    }

    #[test]
    fn clip_rows_page_by_keyset() {
        let (_directory, db) = seeded();
        let page_size = NonZeroUsize::new(2).unwrap();
        let file_ids =
            |page: &[ClipRow]| -> Vec<i64> { page.iter().map(|row| row.file_id).collect() };
        let first_page = db.get_clip_rows_before(None, page_size).unwrap();
        assert_eq!(file_ids(&first_page), [4, 3]);
        assert_eq!(first_page[1].embedding, encode_f32([1.0, 2.0]));
        let next_page = db.get_clip_rows_before(Some(3), page_size).unwrap();
        assert_eq!(file_ids(&next_page), [2, 1]);
        assert!(
            db.get_clip_rows_before(Some(1), page_size)
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn clip_rows_replace_and_delete() {
        let (_directory, db) = seeded();
        db.insert_clip_rows(&[]).unwrap();
        db.insert_clip_rows(&[clip(1, vec![1.0])]).unwrap();
        db.insert_clip_rows(&[clip(3, vec![3.0]), full_clip(5)])
            .unwrap();
        assert_eq!(
            db.clip_indexed_file_with_version().unwrap(),
            HashMap::from([(1, 1), (2, 2), (3, 1), (4, 1), (5, 1)])
        );
        let mut vectors = db.get_all_clip_vectors().unwrap();
        vectors.sort_by_key(|vector| vector.file_id);
        assert_eq!(vectors[0].embedding, vec![1.0f32]);
        assert_eq!(vectors[2].embedding, vec![3.0f32]);
        assert_eq!(db.get_clip_vectorizable_file_count(1).unwrap(), 2);

        db.delete_clip_rows(&[]).unwrap();
        db.delete_clip_rows(&[1, 3]).unwrap();
        assert_eq!(db.count_clip_rows().unwrap(), 3);
        db.delete_all_clip_rows().unwrap();
        assert_eq!(db.count_clip_rows().unwrap(), 0);
    }
}