Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmarks/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def build_index(cfg, base, path):
"assignment": "add routing",
}
elif method == "rabitq-hnsw":
from rabitqlib import FinalAssignmentMode, HnswIndex, RaBitQKMeans
from rabitqlib import HnswIndex, RaBitQKMeans

cluster_count = cfg["hnsw_clusters"]
kmeans = RaBitQKMeans(
Expand All @@ -152,7 +152,7 @@ def build_index(cfg, base, path):
spherical=cfg["metric"] == "ip",
seed=42,
min_points_per_centroid=1,
final_assignment=FinalAssignmentMode.Exact,
final_assignment="exact",
)
stage_start = time.perf_counter()
kmeans.train(base)
Expand Down
10 changes: 5 additions & 5 deletions benchmarks/run_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def load_base(cfg):

def train_python(method, x, k, threads, seed):
if method == "qgkmeans":
from rabitqlib import FinalAssignmentMode, QGKMeans
from rabitqlib import QGKMeans

start = time.perf_counter()
model = QGKMeans(
Expand All @@ -74,13 +74,13 @@ def train_python(method, x, k, threads, seed):
num_threads=threads,
seed=seed,
spherical=False,
quantization_bits=0,
graph_degree=32,
ef_build=240,
nbits=32,
max_degree=32,
ef_construction=240,
ef_search=16,
graph_build_iterations=1,
early_stop_threshold=0.0,
final_assignment=FinalAssignmentMode.Exact,
final_assignment="exact",
)
model.train(x)
total = time.perf_counter() - start
Expand Down
5 changes: 4 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,10 @@ Public names and Python keywords are compatibility contracts. For example,
`degree_bound()`/`set_ef()` and `QGKMeansParameters::ef_build`
retain their existing spellings, as do the Python vocabulary `random_seed`/`seed`,
`train`/`build`, `obj`, `nsplit`, `final_obj` and `niter`. Python's search breadth is
`ef_search` since 0.8.5; `ef` is deprecated and removed in 0.9 (#232). C++ parameter names may be clarified without
`ef_search` since 0.8.5; `ef` is deprecated and removed in 0.9 (#232). Python's `QGKMeans`
and `QGKMeansParameters` take `nbits`, `max_degree` and `ef_construction` since 0.8.5;
`quantization_bits`, `graph_degree` and `ef_build` are deprecated there and removed in
0.9 (#233), while the C++ struct keeps them until 0.9. C++ parameter names may be clarified without
changing types, order or defaults. Keep declarations, definitions and comments
in agreement; do not rename methods or literals by textual substitution.

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/benchmarks/kmeans.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ search. Centroids are not normalized.

| Method | Additional settings |
| --- | --- |
| `rabitqlib.QGKMeans` | `quantization_bits=0`, degree 32, `ef_build=240`, `ef_search=16`, one graph refinement, `early_stop_threshold=0`, `FinalAssignmentMode.Exact` |
| `rabitqlib.QGKMeans` | raw centroid storage (`nbits=32`; `quantization_bits=0` before 0.8.5), degree 32, `ef_construction=240` (`ef_build` before 0.8.5), `ef_search=16`, one graph refinement, `early_stop_threshold=0`, exact final assignment |
| `faiss.Kmeans` | CPU, `nredo=1`, `max_points_per_centroid=N`, `spherical=False`, `early_stop_threshold=0`; exact final `index.search(x, 1)` |
| `superkmeans.SuperKMeans` | v0.2.0, `quantizer="f32"`, `hierarchical=False`, `sampling_fraction=1`, `angular=False`, `use_blas_only=False`, `early_termination=True`, `tol=1e-4`; exact final `assign(x, centroids)` |

Expand Down
33 changes: 20 additions & 13 deletions docs/docs/clustering.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ per centroid; the classes below default to training on every vector.
| Class | Assignment | Recommended use | Cluster count |
| --- | --- | --- | --- |
| [RaBitQKMeans](#rabitqkmeans) | Flat RaBitQ scan | Small cluster counts | `n >= k >= 1` |
| [QGKMeans](#qgkmeans) | SymphonyQG centroid graph | Larger cluster counts | `n >= k > graph_degree` (default 32) |
| [QGKMeans](#qgkmeans) | SymphonyQG centroid graph | Larger cluster counts | `n >= k > max_degree` (C++ `graph_degree`; default 32) |

## Python

Expand Down Expand Up @@ -101,7 +101,7 @@ Set Python keyword arguments or fields of the corresponding C++ parameter type:
| `min_points_per_centroid` | `39` | Warn below this training-points-per-centroid ratio when verbose. |
| `max_points_per_centroid` | `0` | When positive, train on a random sample above `k * max_points_per_centroid` vectors; `0` trains on all vectors. See [sampling](#training-set-sampling). |
| `verbose` | `false` | Print training progress and warnings. |
| `final_assignment` | `Approximate` | Use the selected method for final labels, or `Exact` for nearest-centroid labels. |
| `final_assignment` | `'approximate'` (C++ `Approximate`) | Use the selected method for final labels, or `'exact'` (C++ `Exact`) for nearest-centroid labels. |

`num_threads` sets an upper limit, not a fixed worker count for every stage.
QGKMeans may use fewer workers to build small centroid graphs. That additional
Expand Down Expand Up @@ -164,18 +164,21 @@ encodes new vectors around it.

Each iteration compares every training point with every centroid. The batch size of 32
is not a cluster-count limit. This method uses one-bit point codes throughout;
it has no graph parameters or `quantization_bits` setting.
it has no graph parameters or `nbits` (C++ `quantization_bits`) setting.

### QGKMeans

QGKMeans builds a SymphonyQG graph over the centroids and uses previous
assignments as search hints. Its additional parameters are:
assignments as search hints. Its additional parameters are below, under their Python
names since 0.8.5. C++ and Python before 0.8.5 name the first three
`quantization_bits` (with `0` for raw storage), `graph_degree` and `ef_build`; in
Python those names still work, with a `DeprecationWarning`, until 0.9.

| Parameter | Default | Meaning |
| --- | --- | --- |
| `quantization_bits` | `0` | Graph centroid storage: raw, or `4`/`8` bits. |
| `graph_degree` | `32` | A positive multiple of 32, strictly smaller than `k`. |
| `ef_build`, `ef_search` | `240`, `16` | Positive graph construction and search window sizes. |
| `nbits` | `32` | Graph centroid storage: `32` for raw vectors, or `4`/`8` bits. |
| `max_degree` | `32` | A positive multiple of 32, strictly smaller than `k`. |
| `ef_construction`, `ef_search` | `240`, `16` | Positive graph construction and search window sizes. |
| `graph_build_iterations` | `1` | PiPNN initialization followed by one refinement; values ≥2 request that many graph passes, ending in refinement. Independent of `niter`. |

The seed also controls fallback neighbors; PiPNN uses fixed internal seeds.
Expand All @@ -194,9 +197,8 @@ their sum (WCSS); spherical mode sums `1 - inner_product`. `obj` and
`iteration_stats` record objectives before each centroid update; Python
`train()` returns the last such objective.

`FinalAssignmentMode.Approximate` (default) uses the selected class's assignment
algorithm. The legacy name `FinalAssignmentMode.SymphonyQG` remains an alias for
`Approximate`. Labels need not identify the exact nearest centroids, but reported
`final_assignment='approximate'` (default) uses the selected class's assignment
algorithm. Labels need not identify the exact nearest centroids, but reported
distances and WCSS remain exact for those labels. Without sampling, quantized
QGKMeans final assignment retains a point's last-iteration label when it is
closer. After sampling, most points have no such label, so every point uses the
Expand All @@ -205,12 +207,17 @@ same graph search. Approximate training objectives need not decrease monotonical
To require exact nearest-centroid **final** assignments with either class:

```python
from rabitqlib import FinalAssignmentMode, RaBitQKMeans
from rabitqlib import RaBitQKMeans

kmeans = RaBitQKMeans(d, 16, final_assignment=FinalAssignmentMode.Exact)
kmeans = RaBitQKMeans(d, 16, final_assignment="exact")
kmeans.train(x)
```

Python takes the strings `'approximate'` and `'exact'` since 0.8.5. The
`FinalAssignmentMode` members `Approximate`, `Exact` and the legacy alias `SymphonyQG`
are still accepted until 0.9, and `cp.final_assignment` reads back as a member
until then; importing `FinalAssignmentMode` raises a `DeprecationWarning`.

In C++, set `parameters.final_assignment` to
`rabitqlib::rabitqkmeans::FinalAssignmentMode::Exact` or
`rabitqlib::qgkmeans::FinalAssignmentMode::Exact`; both names refer to the same
Expand Down Expand Up @@ -240,7 +247,7 @@ C++ callers that change `centroids` or graph parameters cause one rebuild on
the next call. Each call still validates the `k * d` centroid coordinates.

For the training vectors, `assign` reproduces `assignments` and `distances`
with `FinalAssignmentMode.Exact`, for RaBitQKMeans, and whenever training
with exact final assignment, for RaBitQKMeans, and whenever training
sampled the input, because the sampled final pass is the same assignment.
Unsampled approximate QGKMeans training also uses previous labels as graph
search hints, so its final labels can differ.
Expand Down
33 changes: 30 additions & 3 deletions docs/docs/python_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,37 @@ removes it.
| `nprobe=0`, `ef=0` for the default | `None`, or omit the argument; 0.9 raises `ValueError` for 0, while `num_threads=0` keeps meaning every available CPU | 0.8.2 |
| `HnswIndex(seed=s)` with `s` above `2**32 - 1` | A seed in `[0, 2**32 - 1]`, the range of the other two classes | Every 0.8 release |

The k-means classes take the names of the index parameters
([#233](https://github.com/VectorDB-NTU/RaBitQ-Library/issues/233)), as keywords and as
attributes of `cp`:

| 0.8 | Use instead | Works since |
| --- | --- | --- |
| `QGKMeans(quantization_bits=0 \| 4 \| 8)` | `nbits=32 \| 4 \| 8`: 32, not 0, keeps raw vectors | 0.8.5 |
| `QGKMeans(graph_degree=...)` | `max_degree=...` | 0.8.5 |
| `QGKMeans(ef_build=...)` | `ef_construction=...` | 0.8.5 |
| `final_assignment=FinalAssignmentMode.Approximate \| .Exact`, also as `index.cp.final_assignment` on `IvfIndex` and `HnswIndex` | `final_assignment='approximate' \| 'exact'`; `'approximate'` also replaces the alias `FinalAssignmentMode.SymphonyQG` | 0.8.5 |
| `QGKMeansIterationStats`, `RaBitQKMeansIterationStats` | `KMeansIterationStats`, the one class both always were; since 0.8.5 it also reports that name | 0.8.5 |

Passing a `FinalAssignmentMode` member does not warn by itself; the warning comes where
one of the three old names is imported or read from `rabitqlib`, including through
`from rabitqlib import *`.

Until 0.9, `cp.final_assignment` still reads back as a `FinalAssignmentMode` member,
whose `.name` is `'Approximate'` or `'Exact'`; 0.9 returns `'approximate'` or
`'exact'`. Reading it does not warn. Code that reads it on both releases can
normalize the value:

```python
mode = cp.final_assignment
mode = mode if isinstance(mode, str) else mode.name.lower()
```

Code that must also run on releases before 0.8.5 needs a version check for the rows
marked 0.8.5; the other replacements work on every release named.

Two changes come without a warning in 0.9. `HnswIndex()` without `seed=` draws a seed,
Three changes come without a warning in 0.9. `HnswIndex()` without `seed=` draws a seed,
as `IvfIndex` and `SymqgIndex` already do, instead of using 100: pass `seed=` (and
`num_threads=1` to `add`) if you rely on a reproducible build. And `repr(index)` prints
`capacity=` and `routing=` where it prints `max_elements=` and `initializer=`.
`num_threads=1` to `add`) if you rely on a reproducible build. `repr(index)` prints
`capacity=` and `routing=` where it prints `max_elements=` and `initializer=`. And
`cp.final_assignment` reads back as a string, as described above.
14 changes: 14 additions & 0 deletions docs/docs/releases/0.8.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ site. The [migration guide](../python_migration.md#from-08-to-09) lists each cha
argument. `num_threads=0` keeps meaning every available CPU.
- `HnswIndex` seeds above `2**32 - 1` are deprecated.

The k-means classes take the names of the index parameters (#233):

- `QGKMeans` and `QGKMeansParameters` take `nbits` (32, 4 or 8), `max_degree` and
`ef_construction` for `quantization_bits` (0, 4 or 8), `graph_degree` and `ef_build`.
- `final_assignment` takes `'approximate'` or `'exact'`, also as
`index.cp.final_assignment` on `IvfIndex` and `HnswIndex`. `FinalAssignmentMode`
members are still accepted, and the attribute still reads back as a member until 0.9.
- `KMeansIterationStats` is the one name of the statistics class.
`QGKMeansIterationStats`, `RaBitQKMeansIterationStats` and `FinalAssignmentMode` warn
when imported or read from the package. The class now reports its name as
`KMeansIterationStats`.

## Python: other changes

- `IvfIndex.set_centroids` takes `num_threads` (#231). Its default follows
Expand All @@ -35,3 +47,5 @@ site. The [migration guide](../python_migration.md#from-08-to-09) lists each cha
build.
- `repr(index)` will print `capacity=` and `routing=` where it prints `max_elements=`
and `initializer=`.
- `cp.final_assignment` will read back as `'approximate'` or `'exact'` instead of a
`FinalAssignmentMode` member.
Loading
Loading