Skip to content
Draft
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
1 change: 1 addition & 0 deletions docs/benchmarks_developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [How to define and register a new test](#how-to-define-and-register-a-new-test)
* [Fixtures](#fixtures)
- [Benchmarks fixture classes hierarchy](#benchmarks-fixture-classes-hierarchy)
* [Training the RAM HNSW policy selector](hnsw_policy_training.md)

# Directory structure
The benchmark directory contains several types of files as follows:
Expand Down
122 changes: 122 additions & 0 deletions docs/hnsw_policy_training.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Training the RAM HNSW hybrid policy tree

`scripts/HNSW_batches_clf.py` fits a fixed decision tree that predicts the
initial choice between `ADHOC_BF` and `BATCHES` for filtered KNN. Use paired
measurements to make a candidate tree, inspect workload-level validation, and
optionally export a standalone C++17 header.

## Run the trainer

Run these commands from the root of the VectorSimilarity checkout. The trainer
requires Python 3.10 or newer, NumPy, and scikit-learn.

```sh
python3 -m venv .venv
. .venv/bin/activate
python -m pip install numpy scikit-learn

python scripts/HNSW_batches_clf.py \
--measurements controlled.jsonl customer-replay.jsonl \
--holdout-workload customer-replay \
--report policy-evaluation.json \
--cpp hnsw_policy_candidate.h
```

The script prints the tree and writes a JSON report with the input SHA-256
checksums, library versions, arguments, validation results, and tree. The
optional C++ header exposes `preferAdHocSearchCandidate(input)`. It returns
`true` for `ADHOC_BF` and `false` for `BATCHES`. Count features are converted
to float32 before evaluation, and the emitted constants preserve the fitted
thresholds. A runtime integration can call this helper and retain the
existing mode reporting and switching logic.

Run the script with no arguments to fit and plot the historical labels. That
mode requires Matplotlib. Its archived feature arrays encode `k` as 5, 10, and
15, while the comments describe 5, 50, and 500. The historical mode preserves
the archived encoding. Measurement mode uses the actual query `k`.

## Record paired measurements

Collect both forced policies on the same RAM HNSW snapshot, query vector,
predicate, `k`, search settings, and deadline. Rotate the order across
repetitions. Verify the effective policy outside the timed region. Use exact
filtered top-k over the same stored vectors to compute recall. Also verify
result completeness, eligibility, and document uniqueness. When only three
documents qualify for `k=10`, the reference contains those three documents.
Use vector-stage results from customer replays before application grouping
or ranking.

Store one object per line in JSONL. The following object is formatted across
lines for readability. Its timings are illustrative.

```json
{
"workload": "customer-replay",
"case": "query-01",
"cohort": "numeric-and",
"estimate_source": "planner",
"k": 2000,
"index_size": 57000,
"label_count": 57000,
"dim": 1024,
"m": 16,
"subset_size": 8500,
"actual_matches": 3400,
"runs": [{
"ADHOC_BF": {"status": "ok", "mode": "ADHOC_BF", "ms": 12.1, "recall": 1.0},
"BATCHES": {"status": "ok", "mode": "BATCHES", "ms": 15.3, "recall": 0.97},
"AUTO": {"status": "ok", "mode": "BATCHES_TO_ADHOC_BF", "ms": 18.7, "recall": 1.0}
}]
}
```

- **`workload`** identifies an independent corpus or replay family. Keep
variants of the same corpus, including different `k` values and index
sizes, in the same workload. Use `cohort` to break those variants out in the
report. Use a unique `case` within each workload.
- **`index_size` and `label_count`** count vectors and distinct vector
labels, respectively. The five model features are `k`, index size, vector
dimension, graph `M`, and estimated selectivity. The selectivity calculation
mirrors RAM HNSW by clamping `subset_size` to index size and dividing by
label count.
- **`subset_size`** is the initial filter estimate supplied to the selector.
Record `actual_matches` when available to audit estimation error. For a
separate exact-count experiment, set both fields to the actual count, set
`estimate_source` to `exact`, and pass `--estimate-source exact`.
The trainer rejects mixed estimate sources.
- **`runs`** contains all paired repetitions for the case. Include both
forced policies and, optionally, AUTO in every repetition. Use
`{"status": "timeout"}` for a measured timeout. A successful forced policy
must report its requested mode. AUTO can report `ADHOC_BF`, `BATCHES`, or
`BATCHES_TO_ADHOC_BF`.

## How the tree is fitted

| Choice | Default and rationale |
| --- | --- |
| Recall floor | `--min-recall 0.95`. A policy qualifies only when every paired repetition completes and reaches the floor. An exact run with low recall invalidates the case. An exact timeout can favor BATCHES if BATCHES completes and qualifies. |
| Latency | Take the median across repetitions for each qualified policy. Each query is one training case, regardless of repetition count. |
| Minimum speedup | `--min-speedup 0.05`. When both policies qualify, require the slower-to-faster latency ratio to exceed 1.05 to use the case in fitting. Close cases contribute to evaluation. |
| Training weights | Give more weight to a costly wrong decision using the logarithm of the latency ratio, capped at a 10× ratio. Give each workload the same total weight so a large synthetic sweep cannot dominate a smaller replay by case count. |
| Tree size | `--max-leaf-nodes 20` preserves the historical size cap. `--min-samples-leaf 3` limits splits that depend on a few measurements. Use smaller trees when held-out quality and latency are comparable. |
| Leaf policy | Prefer the policy with fewer training quality or deadline failures in that leaf. Break ties using the fitted decision. |

Use `--holdout-workload NAME` once per reserved workload. The final fit
excludes those measurements. The report also evaluates leave-one-workload-out
folds among the training workloads. A single training workload cannot supply
a workload-level fold on its own.

## Review the candidate

Start with the held-out reports. Check recall or timeout failures separately
from latency ratios. The latency metrics compare the selected forced policy
with the best qualifying forced policy and with measured AUTO when available.
Measured AUTO may switch mid-query. Validate an integrated candidate to
measure its own switching costs and end-to-end latency.

Use the cohort and conflicting-feature reports to decide what to measure next.
For a wider sweep, vary index sizes and `k` around existing thresholds and
customer values, then vary selectivity and predicate shape. Include filters
with the same selectivity whose matches are near, far from, or independent of
the query vector. If they favor different policies despite identical model
inputs, the current features cannot distinguish those cases.
26 changes: 21 additions & 5 deletions scripts/HNSW_batches_clf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
# Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
# the Server Side Public License v1 (SSPLv1).

import matplotlib.pyplot as plt
import numpy as np
import sys

from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
'''------------------- batches HNSW -----------------'''


def print_tree(clf):
import matplotlib.pyplot as plt
import numpy as np
from sklearn import tree

tree.plot_tree(clf)
plt.savefig('batches_vs_adhoc-HNSW.pdf')
plt.show()
Expand Down Expand Up @@ -49,11 +50,17 @@ def print_tree(clf):
feature=feature[i], threshold=threshold[i], right=children_right[i]))


if __name__ == '__main__':
def train_legacy():
import numpy as np
from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier(max_leaf_nodes=20)

# features: (k, index_size, dim, M, r)
# labels: 1: adhoc better, -1: batches better
# Preserve the historical encoding (k = 5, 10, 15 in X_1 through X_4).
# The experimental comments say 5, 50, 500. The script only records
# the winning policy for each comparison. Measured mode takes the actual k.
y = []

# The following labels represent the results for experiments that were held for the following combinations:
Expand Down Expand Up @@ -343,3 +350,12 @@ def print_tree(clf):
print_tree(clf)
# (k, index_size, dim, M, r)
print(clf.predict([[10, 800000, 100, 50, 0.0001]]))


if __name__ == '__main__':
if len(sys.argv) == 1:
train_legacy()
else:
from hnsw_policy_training import main

main()
Loading
Loading