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
27 changes: 13 additions & 14 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,15 +699,14 @@ its upper and base layers use `upper_layer_max_degree_` and
of 32. These settings are not interchangeable.

Public names and Python keywords are compatibility contracts. For example,
`Cluster::num()`, the protected `Initializer::num_cluster_`, QG's
`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). 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
`Cluster::num()`, the protected `Initializer::num_cluster_` and QG's
`degree_bound()`/`set_ef()` retain their existing spellings, as do the Python
vocabulary `seed`, `train`, `obj`, `nsplit`, `final_obj` and `niter`. Since 0.9, Python
spells each concept one way on the index and k-means classes: `ef_search`,
`ef_construction`, `max_degree`, `nbits` (32 for raw storage), `routing` and `capacity`
(#232, #233). `QGKMeansParameters` has the same field names in C++, converting
`nbits == 32` to the 0 that `QGAssigner` and QG take as `quantization_bits`. 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.

Storage-mode parameters also retain their contracts: IVF/HNSW use `nbits()`
Expand All @@ -730,8 +729,9 @@ public and protected names, Python keywords, field order and numerical operation
integer accumulation outputs. Retain `lut` in compact local kernels where its
role is clear.
- Clustering's private state uses `dim_`, `num_points_`, `num_clusters_`,
`num_workers_` and `data_`. Keep public `d`, `k`, `num_threads` and graph parameter
names compatible. The private graph iteration count is `graph_build_iterations_`.
`num_workers_` and `data_`. Keep public `d`, `k`, `num_threads` and the graph parameter
names (`ef_search`, `graph_build_iterations` and, since 0.9, `max_degree`,
`ef_construction` and `nbits`) compatible. The private graph iteration count is `graph_build_iterations_`.
Per-point centroid IDs are `assignments` (arrays) and `cluster_id` (scalars);
`label` stays reserved for HNSW's caller-visible labels.
- FHT backend arguments use `rotated_data`, `padded_dim`, `trunc_dim` and
Expand Down Expand Up @@ -876,9 +876,8 @@ checked-bit manipulation independently.
### Ownership during construction

Some index builders retain non-owning access to input vectors only during synchronous construction.
Do not extend such a pointer's lifetime without introducing explicit ownership. The HNSW Python
wrapper copies cluster IDs before calling the mutable C++ construction API; preserve that boundary
unless the C++ contract becomes const-correct.
Do not extend such a pointer's lifetime without introducing explicit ownership. The Python
wrappers pass `add` an owned, validated copy of the cluster IDs.

### Removal markers and search

Expand Down
21 changes: 10 additions & 11 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 > max_degree` (C++ `graph_degree`; default 32) |
| [QGKMeans](#qgkmeans) | SymphonyQG centroid graph | Larger cluster counts | `n >= k > max_degree` (default 32) |

## Python

Expand Down Expand Up @@ -164,15 +164,14 @@ 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 `nbits` (C++ `quantization_bits`) setting.
it has no graph parameters or `nbits` setting.

### QGKMeans

QGKMeans builds a SymphonyQG graph over the centroids and uses previous
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.
assignments as search hints. Its additional parameters are below; C++ and Python use
the same names. Before 0.9 the first three were `quantization_bits` (with `0` for raw
storage), `graph_degree` and `ef_build`.

| Parameter | Default | Meaning |
| --- | --- | --- |
Expand Down Expand Up @@ -213,15 +212,15 @@ 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`.
Python takes the strings `'approximate'` and `'exact'`, and `cp.final_assignment`
reads back as one of them. 0.9 removed the `FinalAssignmentMode` enum from the Python
package.

In C++, set `parameters.final_assignment` to
`rabitqlib::rabitqkmeans::FinalAssignmentMode::Exact` or
`rabitqlib::qgkmeans::FinalAssignmentMode::Exact`; both names refer to the same
enum. Training still uses the selected approximate assignment algorithm.
enum, whose values are `Approximate` and `Exact` (0.9 removed the alias
`SymphonyQG`). Training still uses the selected approximate assignment algorithm.

Exact final assignment screens all centroids with blocked GEMM and refines
possible winners in float64; ties select the smallest centroid index. Returned
Expand Down
25 changes: 13 additions & 12 deletions docs/docs/index/hnsw.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Run the [Quick start](../quick_start.md#build-and-search-an-ivf-index) to prepar
```python
from rabitqlib import HnswIndex

index = HnswIndex(data.shape[1], M=16, ef_construction=100) # nbits=7 by default
index.add(data, num_threads=1) # trains 16 centroids, then links; 1 thread is reproducible
index = HnswIndex(data.shape[1], M=16, ef_construction=100, seed=7) # nbits=7 by default
index.add(data, num_threads=1) # trains 16 centroids, then links; a seed and 1 thread are reproducible
ids, distances = index.search(queries, k=10) # ef_search defaults to max(k, 64)
index.save("hnsw.index")
loaded = HnswIndex.load("hnsw.index")
Expand All @@ -31,7 +31,7 @@ The first `add` trains an untrained index on the added vectors, as for
the row count. An empty first `add` raises `RuntimeError`.
`train` and `set_centroids` work as for [IVF-RaBitQ](ivf.md#training-centroids-in-python),
with `num_clusters` centroids (default 16) and always RaBitQKMeans; `index.cp`
starts from the same defaults with `seed`'s low 32 bits. The first `add` into the
starts from the same defaults with the index `seed`. The first `add` into the
empty index links vectors with their exact coordinates, like the 0.7 `build`;
later adds link through the stored codes (see [Recall after `add`](#recall-after-add)),
so add most vectors in the first call.
Expand Down Expand Up @@ -70,15 +70,16 @@ see [C++ integration](../cpp_integration.md).
| `nbits` / C++ `total_bits` | 1–9 total bits including the sign bit. Python's keyword-only `nbits` defaults to 7; C++ requires it. Raw-vector storage is not supported. |
| `num_clusters` | Python keyword, default 16: centroids `train` or the first `add` learns, or `set_centroids` supplies; the first `add` caps it at its row count. |
| `M`, `ef_construction` | Graph connectivity and construction search budget; Python defaults are 16 and 200, and the index reports both as properties. `M` must be at least 2 and is capped at 10,000; `ef_construction` is raised to at least `M`. The base layer keeps up to `2 * M` edges per node. |
| `metric`, `seed` | Python defaults are `"l2"` and 100; `"ip"` and [`"cosine"`](usage.md#the-cosine-metric) are also supported. `seed` was called `random_seed` before 0.8. A seed is in `[0, 2**32 - 1]`; larger values are deprecated since 0.8.5, and 0.9 draws a seed when it is omitted. |
| `capacity` (C++ `max_elements()`) | Python reports the current capacity, which `add` grows geometrically (at least doubling) and `reserve` can preallocate; they were named `max_elements` and `resize` before 0.8.5. C++ `add` never grows it. |
| `k`, `ef_search` | Positive result count and search window. `k` may exceed the stored point count; Python pads missing results, while C++ returns fewer pairs. An omitted or `None` `ef_search` uses `max(k, 64)` in Python (0 does the same and is deprecated since 0.8.5), a smaller value is raised to at least `k`, and a value above the stored point count is clamped to it. Python named it `ef` before 0.8.5. |
| `metric`, `seed` | `metric` defaults to `"l2"` in Python; `"ip"` and [`"cosine"`](usage.md#the-cosine-metric) are also supported. `seed` is the graph and rotation seed in `[0, 2**32 - 1]` and the initial `cp.seed`. Python's default `None` draws one at random (it was 100 before 0.9); `index.seed` reports it. |
| `capacity` (C++ `max_elements()`) | Python reports the current capacity, which `add` grows geometrically (at least doubling) and `reserve` can preallocate; they replace `max_elements` and `resize`, which 0.9 removed. C++ `add` never grows it. |
| `k`, `ef_search` | Positive result count and search window. `k` may exceed the stored point count; Python pads missing results, while C++ returns fewer pairs. An omitted or `None` `ef_search` uses `max(k, 64)` in Python (0 raises `ValueError`), a smaller value is raised to at least `k`, and a value above the stored point count is clamped to it. It replaces `ef`, which 0.9 removed. |

`seed` controls graph levels and the FHT/Kac rotation (using its low 32 bits).
With identical inputs and parameters, single-threaded construction is reproducible
within the same build environment. Python `train` and `add` default to every
available CPU, so **default builds are not reproducible**: pass `num_threads=1` to
`add` when you need the same graph again. Different toolchains or SIMD backends can
within the same build environment. Python draws a seed unless `seed=` is given, and
`train` and `add` default to every available CPU, so **default builds are not
reproducible**: pass `seed=` to the constructor and `num_threads=1` to `add` when you
need the same graph again. Different toolchains or SIMD backends can
still change results. Loading restores the saved rotation.

Results use squared L2 or `1 - dot` estimates, ordered from smallest to largest.
Expand Down Expand Up @@ -234,8 +235,8 @@ records still occupy capacity.
C++ `add` never grows the index: it rejects a whole batch whose size exceeds
`max_elements() - num_stored_points()`, and `resize` is the only way to raise
that. Python `add` grows the capacity first when the batch does not fit, to at least
double it, and `reserve(num_points)` preallocates it (since 0.8.5; `resize` still works,
with a `DeprecationWarning`, until 0.9, and `capacity` replaces `max_elements`). Reserving capacity avoids reallocating and copying the whole base-layer record storage during `add`.
double it, and `reserve(num_points)` preallocates it; `reserve` never shrinks the
capacity. Reserving capacity avoids reallocating and copying the whole base-layer record storage during `add`.
It does not make insertion allocation-free: batch scratch, upper-layer link lists,
and label-map entries still require allocations, and the label map may rehash.

Expand Down Expand Up @@ -282,7 +283,7 @@ exclusive access as other updates.
Removed points still take slots in the `ef_search` candidate set, so a search with
`ef_search` close to `k` finds fewer live points as removals accumulate. C++ search
uses `max(ef_search, k)`, clamped to `num_stored_points()`. Python uses `max(k, 64)`
when `ef_search` is omitted (it was named `ef` before 0.8.5); an explicit value is also raised to at least `k`,
when `ef_search` is omitted; an explicit value is also raised to at least `k`,
and both are clamped to the stored point count.
To keep about the same number of live candidates as before the removals, scale
the effective window by the share of live points (search clamps the result to
Expand Down
10 changes: 4 additions & 6 deletions docs/docs/index/ivf.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ See [C++ integration](../cpp_integration.md) for build setup and
| `nbits` / C++ `total_bits` | 1–9 total bits including the sign bit, or 32 for owned raw-vector reranking. Python's keyword-only `nbits` defaults to 7; C++ requires it. |
| `seed` | Rotation seed in `[0, 2**32 - 1]` and the initial `cp.seed`. Python's default `None` draws one at random; `index.seed` reports it, and passing it back reproduces the index. |
| `metric` | Python `"l2"`, `"ip"` or `"cosine"`; defaults to `"l2"`. See [the cosine metric](usage.md#the-cosine-metric). |
| `routing` | Defaults to `"auto"`; see [centroid routing](#centroid-routing). Named `initializer` before 0.8.5; the old name still works, with a `DeprecationWarning`, until 0.9. |
| `k`, `nprobe` | Positive result count and cluster probe count. `k` may exceed the stored point count; missing results are padded with `kPidMax` (`-1` in Python) and `+inf`. Python's `nprobe` defaults to `num_clusters / 16`, rounded and at least 1, when omitted or `None` (0 does the same and is deprecated since 0.8.5); C++ requires a positive `nprobe`. |
| `routing` | Defaults to `"auto"`; see [centroid routing](#centroid-routing). It replaces `initializer`, which 0.9 removed. |
| `k`, `nprobe` | Positive result count and cluster probe count. `k` may exceed the stored point count; missing results are padded with `kPidMax` (`-1` in Python) and `+inf`. Python's `nprobe` defaults to `num_clusters / 16`, rounded and at least 1, when omitted or `None` (0 raises `ValueError`); C++ requires a positive `nprobe`. |
| `high_accuracy` | Python defaults to `None`: HACC for 4–9 bits, standard FastScan otherwise. |

Results contain IDs and squared L2 or `1 - dot` distances, ordered from smallest
Expand Down Expand Up @@ -230,8 +230,7 @@ index.add(vectors)
vectors; once it stores any (including removed ones), both raise
`RuntimeError`. A failed call leaves the index unchanged.
- Before training, `add` with `cluster_ids`, `search`, `remove`, `reserve_cluster` and
`save` raise `RuntimeError`; `add` without `cluster_ids` trains first, except
on an index created with `max_elements`, which `build` fills. If that
`save` raise `RuntimeError`; `add` without `cluster_ids` trains first. If that
first `add` fails, the index stays untrained and empty; an empty first `add`
raises `RuntimeError`. A trained index without
vectors cannot be saved, and its `search` returns only padded results; add
Expand Down Expand Up @@ -398,8 +397,7 @@ removed = index.remove(new_ids[:10])
index.reserve_cluster(cluster_id, num_points)
```

Python names this method `reserve_cluster` since 0.8.5; `reserve` still works, with a
`DeprecationWarning`, until 0.9.
Python names this method `reserve_cluster`; it replaces `reserve`, which 0.9 removed.

Python `vectors` has shape `(n, index.dim)` and is converted to contiguous float32.
`add()` returns a uint32 array of the new IDs. Optional `cluster_ids` must be a
Expand Down
7 changes: 3 additions & 4 deletions docs/docs/index/qg.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ qg.save("qg_example.index");
| Parameter | Contract |
| --- | --- |
| `dim` | Original input dimension; Python supports 64–65,536 and pads internally. |
| `nbits` / C++ `quantization_bits` | Python keyword: 32 (default) for owned raw vectors, or 4/8 for quantized storage. C++ uses 0 for raw vectors, as Python's `quantization_bits` did before 0.8. |
| `nbits` / C++ `quantization_bits` | Python keyword: 32 (default) for owned raw vectors, or 4/8 for quantized storage. C++ uses 0 for raw vectors. |
| `max_degree` | Python keyword, default 32: a positive multiple of 32, strictly smaller than the point count. It is a hard degree bound, unlike HNSW-RaBitQ's `M`. |
| `ef_construction` | Python keyword, default 200: positive construction search window used by `add`. |
| `init` | Python keyword: `"pipnn"` (default) or `"random"`. |
| `metric` | Python keyword: `"l2"` (default), `"ip"` or [`"cosine"`](usage.md#the-cosine-metric). |
| `seed` | Python keyword: rotation and graph-construction seed in `[0, 2**32 - 1]`. The default `None` draws one at random; `index.seed` reports it, and passing it back with `num_threads=1` rebuilds the same graph from the same data. |
| `k`, `ef_search` | `k` is at least 1 and may exceed the point count; slots the graph cannot fill are padded with ID `-1` in Python (`kPidMax` in C++) and distance `+inf`. An omitted `ef_search` uses `max(k, 64)` in Python, and smaller values are raised to `k`, as for HNSW-RaBitQ. Python named it `ef` before 0.8.5. |
| `k`, `ef_search` | `k` is at least 1 and may exceed the point count; slots the graph cannot fill are padded with ID `-1` in Python (`kPidMax` in C++) and distance `+inf`. An omitted or `None` `ef_search` uses `max(k, 64)` in Python (0 raises `ValueError`), and smaller values are raised to `k`, as for HNSW-RaBitQ. It replaces `ef`, which 0.9 removed. |
| `num_threads` | Python `add` defaults to every available logical CPU, capped by `OMP_NUM_THREADS`, and `search` to 1; `None` selects these defaults. |

Python search returns `(ids, distances)` arrays. Distances are squared
Expand Down Expand Up @@ -183,8 +183,7 @@ Concurrent searches need separate outputs; do not modify the index or call
Python `search(queries, k, ef_search=None, *, num_threads=None)` accepts a two-dimensional
query array and returns `(ids, distances)` arrays of shape `(num_queries, k)`,
including empty batches. It defaults to one worker; `num_threads=0` selects the
available logical CPU count, capped by the number of queries. The 0.7 name
`search_batch` still works in 0.8 with a `DeprecationWarning`.
available logical CPU count, capped by the number of queries.
Queries use the original dimension, including when the internal rotation pads it.

Python searches use per-call `ef_search` values. For access rules, see the
Expand Down
Loading
Loading