from typing import Tuple import gc try: import faiss except ImportError as e: print("Error loading faiss:", e) faiss = None import torch import numpy as np def paired_kmeans_faiss( q: torch.Tensor, X: torch.Tensor, k: int, max_iters: int = 100, n_redo: int = 3, seed: int = 42, ) -> Tuple[torch.Tensor, torch.Tensor]: # https://github.com/facebookresearch/faiss/blob/dafdff110489db7587b169a0afee8470f220d295/faiss/python/extra_wrappers.py#L437 # https://github.com/facebookresearch/faiss/blob/dafdff110489db7587b169a0afee8470f220d295/faiss/Clustering.cpp#L56 # https://github.com/facebookresearch/faiss/blob/main/faiss/Clustering.h assert q.shape == X.shape print("[paired_kmeans_faiss]", q.shape, X.shape, k) paired_vectors = torch.cat( [ torch.cat((q, X), dim=0), torch.cat((X, q), dim=0), ], dim=1, ) paired_vectors /= paired_vectors.norm(dim=1, keepdim=True, p=2) paired_vectors = paired_vectors.cpu() dim = paired_vectors[0].numel() # TODO: How to make kmeans use more gpu mem? print( f"[paired_kmeans_faiss] initializing Kmeans object (gpu={torch.cuda.is_available()})" ) gc.collect() torch.cuda.empty_cache() kmeans = faiss.Kmeans( dim, k, niter=max_iters, nredo=n_redo, gpu=torch.cuda.is_available(), verbose=True, spherical=True, decode_block_size=2**27, seed=seed, ) # otherwise the kmeans implementation sub-samples the training set # to <= 256 points per centroid kmeans.max_points_per_centroid = k * 2 print("[paired_kmeans_faiss] calling kmeans.train()") paired_vectors = np.array(paired_vectors) kmeans.train(paired_vectors) queries = paired_vectors[: len(q)] _distances, assignments = kmeans.index.search(queries, 1) assert assignments.shape == (q.shape[0], 1) print("Finished kmeans. Average distance:", _distances.mean()) centroids = torch.tensor(kmeans.centroids) assert centroids.shape == (k, paired_vectors.shape[1]) return centroids, assignments if __name__ == "__main__": def test_cluster_tiny(): torch.manual_seed(42) d = 8 A = torch.randn((d,), dtype=torch.float32) * 100 B = torch.randn((d,), dtype=torch.float32) * 100 C = torch.randn((d,), dtype=torch.float32) c1_size = 6 c1 = A + torch.randn((c1_size, d), dtype=torch.float32) c2_size = 9 c2 = B + torch.randn((c2_size, d), dtype=torch.float32) c3_size = 5 c3 = C + torch.randn((c3_size, d), dtype=torch.float32) points = torch.cat([c1, c2, c3], dim=0) _centroids, assignments = paired_kmeans_faiss( q=points, X=points, k=3, max_iters=10, seed=42 ) assignments = torch.tensor(assignments.flatten()) assert ( assignments == torch.tensor( [2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1] ) ).all() test_cluster_tiny()