---
slug: api-reference/cpp-api-neighbors-scann
---
# Scann
_Source header: `cuvs/neighbors/scann.hpp`_
## ScaNN index build parameters
### neighbors::scann::index_params
ANN parameters used by ScaNN to build index
```cpp
struct index_params : cuvs::neighbors::index_params {
uint32_t n_leaves;
int64_t kmeans_n_rows_train;
uint32_t kmeans_n_iters;
float partitioning_eta;
float soar_lambda;
uint32_t n_coarse_clusters;
uint32_t pq_dim;
uint32_t pq_bits;
int64_t pq_n_rows_train;
uint32_t pq_train_iters;
bool reordering_bf16;
float reordering_noise_shaping_threshold;
};
```
**Fields**
| Name | Type | Description |
| --- | --- | --- |
| `n_leaves` | `uint32_t` | the number of leaves in the tree * |
| `kmeans_n_rows_train` | `int64_t` | the number of rows for training the tree structures * |
| `kmeans_n_iters` | `uint32_t` | the max number of iterations for training the tree structure * |
| `partitioning_eta` | `float` | the value of eta for AVQ adjustment during partitioning * |
| `soar_lambda` | `float` | the value of lambda for SOAR spilling * |
| `n_coarse_clusters` | `uint32_t` | the number of intermediate nodes in a two-level tree. When == 0, only a single level tree with root + leaves if built.
Recommend sqrt(n_leaves), for a reasonable trade-off between latency/recall |
| `pq_dim` | `uint32_t` | the dimension of pq subspaces (must divide dataset dimension)* |
| `pq_bits` | `uint32_t` | the number of bits for pq codes (must be 4 or 8, for 16 and 256 codes respectively) * |
| `pq_n_rows_train` | `int64_t` | the number of rows for PQ training (internally capped to 100k) * |
| `pq_train_iters` | `uint32_t` | the max number of iterations for PQ training * |
| `reordering_bf16` | `bool` | whether to apply bf16 quantization of dataset vectors * |
| `reordering_noise_shaping_threshold` | `float` | Threshold T for computing AVQ eta = (dim - 1) ( T^2 / \|\| x \|\|^2) / ( 1 - T^2 / \|\| x \|\|^2)
When quantizing a vector x to x_q, AVQ minimizes the loss function L(x, x_q) = eta * \|\| r_para \|\|^2 + \|\| r_perp \|\|^2, where r = x - x_q, r_para = <r, x> * x / \|\| x \|\|^2, r_perp = r - r_para
Compared to L2 loss, This produces an x_q which better approximates the dot product of a query vector with x
If the threshold is NAN, AVQ is not performed during bfloat16 quant |
## ScaNN index type
### neighbors::scann::index
ScaNN index.
The index stores the dataset and the ScaNN graph in device memory.
```cpp
template
struct index;
```
### neighbors::scann::index::metric
Distance metric used for clustering.
```cpp
[[nodiscard]] constexpr inline auto metric() const noexcept -> cuvs::distance::DistanceType;
```
**Returns**
[`cuvs::distance::DistanceType`](/api-reference/cpp-api-distance-distance#distance-distancetype)
### neighbors::scann::index::size
Total length of the index (number of vectors).
```cpp
IdxT size() const noexcept;
```
**Returns**
`IdxT`
### neighbors::scann::index::dim
Dimensionality of the data.
```cpp
[[nodiscard]] constexpr inline auto dim() const noexcept -> uint32_t;
```
**Returns**
`uint32_t`
### neighbors::scann::index::centers
The leaf node centers
```cpp
raft::device_matrix_view centers() noexcept;
```
**Returns**
`raft::device_matrix_view`
**Additional overload:** `neighbors::scann::index::centers`
The const leaf node centers
```cpp
raft::device_matrix_view centers() const noexcept;
```
**Returns**
`raft::device_matrix_view`
### neighbors::scann::index::labels
The assignment of dataset vectors to the nearest leaf center
```cpp
raft::device_vector_view labels() noexcept;
```
**Returns**
`raft::device_vector_view`
**Additional overload:** `neighbors::scann::index::labels`
the const assignment of dataset vectors to the nearest leaf center
```cpp
raft::device_vector_view labels() const noexcept;
```
**Returns**
`raft::device_vector_view`
### neighbors::scann::index::soar_labels
Spilled assignment of dataset vectors to nearest leaf center by
```cpp
raft::device_vector_view soar_labels() noexcept;
```
minimizing SOAR loss
**Returns**
`raft::device_vector_view`
**Additional overload:** `neighbors::scann::index::soar_labels`
const spilled assignment of dataset vectors to nearest leaf center by
```cpp
raft::device_vector_view soar_labels() const noexcept;
```
minimizing SOAR loss
**Returns**
`raft::device_vector_view`
### neighbors::scann::index::coarse_centers
coarse kmeans centers for two-level trees
```cpp
raft::device_matrix_view coarse_centers() noexcept;
```
**Returns**
`raft::device_matrix_view`
**Additional overload:** `neighbors::scann::index::coarse_centers`
const coarse kmeans centers for two-level trees
```cpp
raft::device_matrix_view coarse_centers() const noexcept;
```
**Returns**
`raft::device_matrix_view`
### neighbors::scann::index::coarse_labels
assignment of leaf centers to coarse centers in two-level tree
```cpp
raft::device_vector_view coarse_labels() noexcept;
```
**Returns**
`raft::device_vector_view`
**Additional overload:** `neighbors::scann::index::coarse_labels`
const assignment of leaf centers to coarse centers in two-level tree
```cpp
raft::device_vector_view coarse_labels() const noexcept;
```
**Returns**
`raft::device_vector_view`
### neighbors::scann::index::coarse_soar_labels
spilled assignment of leaf centers to coarse centers by minimizing
```cpp
raft::device_vector_view coarse_soar_labels() noexcept;
```
SOAR loss in two-level tree
**Returns**
`raft::device_vector_view`
**Additional overload:** `neighbors::scann::index::coarse_soar_labels`
const spilled assighment of leaf centers to coarse centers by minimizing
```cpp
raft::device_vector_view coarse_soar_labels() const noexcept;
```
SOAR loss in two-level tree
**Returns**
`raft::device_vector_view`
### neighbors::scann::index::n_rows
number of rows in dataset
```cpp
uint32_t n_rows() const noexcept;
```
**Returns**
`uint32_t`
### neighbors::scann::index::n_leaves
number of leaf nodes in kmeans tree
```cpp
uint32_t n_leaves() const noexcept;
```
**Returns**
`uint32_t`
### neighbors::scann::index::pq_dim
dimension of pq subspaces
```cpp
uint32_t pq_dim() const noexcept;
```
**Returns**
`uint32_t`
### neighbors::scann::index::pq_codebook
const codebook for pq quanization
```cpp
raft::device_matrix_view pq_codebook() const noexcept;
```
**Returns**
`raft::device_matrix_view`
**Additional overload:** `neighbors::scann::index::pq_codebook`
codebook for pq quantization
```cpp
raft::device_matrix_view pq_codebook() noexcept;
```
**Returns**
`raft::device_matrix_view`
### neighbors::scann::index::quantized_residuals
const pq quantized residuals of dataset vectors
```cpp
raft::host_matrix_view quantized_residuals() const noexcept;
```
**Returns**
`raft::host_matrix_view`
**Additional overload:** `neighbors::scann::index::quantized_residuals`
pq quanntized residuals of dataset vectors
```cpp
raft::host_matrix_view quantized_residuals() noexcept;
```
**Returns**
`raft::host_matrix_view`
### neighbors::scann::index::quantized_soar_residuals
pq quantized residuals of dataset vectors using SOAR assignment
```cpp
raft::host_matrix_view quantized_soar_residuals()
const noexcept;
```
**Returns**
`raft::host_matrix_view`
**Additional overload:** `neighbors::scann::index::quantized_soar_residuals`
const pq quantized residuals of dataset vectors using SOAR assignment
```cpp
raft::host_matrix_view quantized_soar_residuals() noexcept;
```
**Returns**
`raft::host_matrix_view`
### neighbors::scann::index::bf16_dataset
bf16 quantized dataset
```cpp
raft::host_matrix_view bf16_dataset() noexcept;
```
**Returns**
`raft::host_matrix_view`
**Additional overload:** `neighbors::scann::index::bf16_dataset`
const bf16 quantized dataset
```cpp
raft::host_matrix_view bf16_dataset() const noexcept;
```
**Returns**
`raft::host_matrix_view`
## ScaNN index build functions
### neighbors::scann::build
Build the index from the dataset for efficient search.
```cpp
auto build(raft::resources const& handle,
const cuvs::neighbors::scann::index_params& params,
raft::device_matrix_view dataset)
-> cuvs::neighbors::scann::index;
```
**Parameters**
| Name | Direction | Type | Description |
| --- | --- | --- | --- |
| `handle` | | `raft::resources const&` | |
| `params` | | [`const cuvs::neighbors::scann::index_params&`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index-params) | |
| `dataset` | | `raft::device_matrix_view` | |
**Returns**
[`cuvs::neighbors::scann::index`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index)
**Additional overload:** `neighbors::scann::build`
Build the index from the dataset for efficient search.
```cpp
auto build(raft::resources const& handle,
const cuvs::neighbors::scann::index_params& params,
raft::device_matrix_view dataset)
-> cuvs::neighbors::scann::index;
```
**Parameters**
| Name | Direction | Type | Description |
| --- | --- | --- | --- |
| `handle` | | `raft::resources const&` | |
| `params` | | [`const cuvs::neighbors::scann::index_params&`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index-params) | |
| `dataset` | | `raft::device_matrix_view` | |
**Returns**
[`cuvs::neighbors::scann::index`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index)
**Additional overload:** `neighbors::scann::build`
Build the index from the dataset for efficient search.
```cpp
auto build(raft::resources const& handle,
const cuvs::neighbors::scann::index_params& params,
raft::device_matrix_view dataset)
-> cuvs::neighbors::scann::index;
```
**Parameters**
| Name | Direction | Type | Description |
| --- | --- | --- | --- |
| `handle` | | `raft::resources const&` | |
| `params` | | [`const cuvs::neighbors::scann::index_params&`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index-params) | |
| `dataset` | | `raft::device_matrix_view` | |
**Returns**
[`cuvs::neighbors::scann::index`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index)
**Additional overload:** `neighbors::scann::build`
Build the index from the dataset for efficient search.
```cpp
auto build(raft::resources const& handle,
const cuvs::neighbors::scann::index_params& params,
raft::device_matrix_view dataset)
-> cuvs::neighbors::scann::index;
```
**Parameters**
| Name | Direction | Type | Description |
| --- | --- | --- | --- |
| `handle` | | `raft::resources const&` | |
| `params` | | [`const cuvs::neighbors::scann::index_params&`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index-params) | |
| `dataset` | | `raft::device_matrix_view` | |
**Returns**
[`cuvs::neighbors::scann::index`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index)
### neighbors::scann::serialize
Save the index to files in a directory
```cpp
void serialize(raft::resources const& handle,
const std::string& file_prefix,
const cuvs::neighbors::scann::index& index);
```
This serializes the index into a list of files for integration into OSS ScaNN for use with search
NOTE: the implementation of ScaNN index build is EXPERIMENTAL and currently not subject to comprehensive, automated testing. Accuracy and performance are not guaranteed, and could diverge without warning.
**Parameters**
| Name | Direction | Type | Description |
| --- | --- | --- | --- |
| `handle` | | `raft::resources const&` | |
| `file_prefix` | | `const std::string&` | |
| `index` | | [`const cuvs::neighbors::scann::index&`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index) | |
**Returns**
`void`
## ScaNN serialize functions
**Additional overload:** `neighbors::scann::serialize`
Save the index to files in a directory
```cpp
void serialize(raft::resources const& handle,
const std::string& file_prefix,
const cuvs::neighbors::scann::index& index);
```
This serializes the index into a list of files for integration into OSS ScaNN for use with search
NOTE: the implementation of ScaNN index build is EXPERIMENTAL and currently not subject to comprehensive, automated testing. Accuracy and performance are not guaranteed, and could diverge without warning.
**Parameters**
| Name | Direction | Type | Description |
| --- | --- | --- | --- |
| `handle` | | `raft::resources const&` | |
| `file_prefix` | | `const std::string&` | |
| `index` | | [`const cuvs::neighbors::scann::index&`](/api-reference/cpp-api-neighbors-scann#neighbors-scann-index) | |
**Returns**
`void`