Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Remove unused cosine distance functions

laurenspriem877c660ba9e9
diff --git a/mobile/lib/services/machine_learning/face_ml/face_clustering/cosine_distance.dart b/mobile/lib/services/machine_learning/face_ml/face_clustering/cosine_distance.dart
deleted file mode 100644
index 17b783558d..0000000000
--- a/mobile/lib/services/machine_learning/face_ml/face_clustering/cosine_distance.dart
+++ /dev/null
@@ -1,87 +0,0 @@
-import 'dart:math' show sqrt;
-
-import "package:ml_linalg/linalg.dart";
-
-/// Calculates the cosine distance between two embeddings/vectors using SIMD from ml_linalg
-///
-/// WARNING: This assumes both vectors are already normalized!
-/// WARNING: For even more performance, consider calculating the logic below inline!
-@pragma("vm:prefer-inline")
-double cosineDistanceSIMD(Vector vector1, Vector vector2) {
- return 1 - vector1.dot(vector2);
-}
-
-/// Calculates the cosine distance between two embeddings/vectors using SIMD from ml_linalg
-///
-/// WARNING: Only use when you're not sure if vectors are normalized. If you're sure they are, use [cosineDistanceSIMD] instead for better performance, or inline for best performance.
-double cosineDistanceSIMDSafe(Vector vector1, Vector vector2) {
- if (vector1.length != vector2.length) {
- throw ArgumentError('Vectors must be the same length');
- }
-
- return vector1.distanceTo(vector2, distance: Distance.cosine);
-}
-
-/// Calculates the cosine distance between two embeddings/vectors.
-///
-/// Throws an ArgumentError if the vectors are of different lengths or
-/// if either of the vectors has a magnitude of zero.
-double cosineDistance(List<double> vector1, List<double> vector2) {
- if (vector1.length != vector2.length) {
- throw ArgumentError('Vectors must be the same length');
- }
-
- double dotProduct = 0.0;
- double magnitude1 = 0.0;
- double magnitude2 = 0.0;
-
- for (int i = 0; i < vector1.length; i++) {
- dotProduct += vector1[i] * vector2[i];
- magnitude1 += vector1[i] * vector1[i];
- magnitude2 += vector2[i] * vector2[i];
- }
-
- magnitude1 = sqrt(magnitude1);
- magnitude2 = sqrt(magnitude2);
-
- // Avoid division by zero. This should never happen. If it does, then one of the vectors contains only zeros.
- if (magnitude1 == 0 || magnitude2 == 0) {
- throw ArgumentError('Vectors must not have a magnitude of zero');
- }
-
- final double similarity = dotProduct / (magnitude1 * magnitude2);
-
- // Cosine distance is the complement of cosine similarity
- return 1.0 - similarity;
-}
-
-// cosineDistForNormVectors calculates the cosine distance between two normalized embeddings/vectors.
-@pragma('vm:entry-point')
-double cosineDistForNormVectors(List<double> vector1, List<double> vector2) {
- if (vector1.length != vector2.length) {
- throw ArgumentError('Vectors must be the same length');
- }
- double dotProduct = 0.0;
- for (int i = 0; i < vector1.length; i++) {
- dotProduct += vector1[i] * vector2[i];
- }
- return 1.0 - dotProduct;
-}
-
-/// NOTE: This function assumes that both embeddings are normalized!
-@pragma("vm:prefer-inline")
-double computeCosineSimilarity(
- List<double> embedding1,
- List<double> embedding2,
-) {
- assert(
- embedding1.length == embedding2.length,
- "The two embeddings should have the same length",
- );
- double cosineSimilarity = 0;
- final length = embedding1.length;
- for (int index = 0; index < length; index++) {
- cosineSimilarity += embedding1[index] * embedding2[index];
- }
- return cosineSimilarity;
-}