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