(args=None)
| 17 | |
| 18 | |
| 19 | def main(args=None) -> None: |
| 20 | # 1. Load data |
| 21 | data = read_fvecs(args.data_file) |
| 22 | print(f"Data shape: {data.shape}") |
| 23 | |
| 24 | # 2. Cluster with FAISS |
| 25 | centroids, cluster_ids = cluster_data(data, args.num_clusters, args.metric, args.num_threads) |
| 26 | print(f"Centroids: {centroids.shape}, cluster_ids: {cluster_ids.shape}") |
| 27 | |
| 28 | # 3. Build HNSW index |
| 29 | n, dim = data.shape |
| 30 | print(f"\nBuilding HNSW index: n={n}, dim={dim}, M={args.degree}, " |
| 31 | f"ef={args.ef_construction}, bits={args.total_bits}, metric={args.metric}") |
| 32 | |
| 33 | idx = HnswIndex( |
| 34 | dim=dim, |
| 35 | max_elements=n, |
| 36 | M=args.degree, |
| 37 | ef_construction=args.ef_construction, |
| 38 | nbits=args.total_bits, |
| 39 | metric=args.metric, |
| 40 | ) |
| 41 | |
| 42 | t0 = time() |
| 43 | idx.build( |
| 44 | data, |
| 45 | centroids, |
| 46 | cluster_ids, |
| 47 | num_threads=args.num_threads, |
| 48 | fast_quantization=args.faster_quant, |
| 49 | ) |
| 50 | print(f"Indexing time: {time() - t0:.2f}s") |
| 51 | |
| 52 | idx.save(args.index_file) |
| 53 | print(f"Index saved → {args.index_file}") |
| 54 | |
| 55 | |
| 56 | if __name__ == "__main__": |
no test coverage detected