Skip to content

Studio: download the HNSW prebuilts (usearch + numkong) only when a knowledge base needs them - #16799

Merged
danielhanchen merged 35 commits into
mainfrom
studio/hnsw-prebuilt
Nov 19, 2026
Merged

danielhanchen merged 35 commits into
mainfrom
studio/hnsw-prebuilt

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Studio's HNSW tier (#16145) depends on usearch + numkong native wheels. Today they are plain lines in studio.txt / no-torch-runtime.txt, so:

  • every Studio install downloads and unpacks them (about 59 MB on disk on Linux x86_64), although only knowledge bases of 100k+ chunks ever use HNSW;
  • a host with no matching wheel (glibc 2.26 / 2.27 no-torch installs, a PyPI-blocked network) falls back to an sdist build and fails the whole Studio deps step;
  • the install manifest counts a missing usearch as a broken install (Desktop preflight included), and an install that skipped the pair never gets it.

This PR makes the pair an optional prebuilt that Studio and Desktop download themselves, only when a knowledge base is large enough to use it.

What it adds

  • On demand by default: installs and updates download nothing. When a scope reaches RAG_VEC_ANN_MIN_ROWS (100k) and the pair is missing, the backend starts one background install (never inside a SQLite transaction or the cache lock; search keeps answering from the exact cache / vec0), then rebuilds the waiting scopes as HNSW without a restart. UNSLOTH_HNSW_PREFETCH=1 installs it during install / update instead, for a machine that will be offline later.
  • studio/install_hnsw_prebuilt.py + studio/hnsw_prebuilt.txt, a hash-pinned requirements file with every usearch 2.26.6 / numkong 7.8.6 wheel for cp311 to cp314 (scripts/hnsw_catalog.py [--check] regenerates it from PyPI).
    • uv (Studio's private copy is found without PATH; pip as fallback) installs it with --require-hashes --only-binary :all: --no-deps --no-config: uv picks the wheel for the interpreter, only the pinned bytes are accepted, nothing is ever built from source, and a user's uv config or UV_CONSTRAINT cannot interfere. Proxies, UV_INDEX_URL / PIP_INDEX_URL and the OS trust store (macOS, Windows, Desktop, as Studio's other downloads) are honoured. A platform with no wheel reports unsupported and backs off.
    • Strips the DWARF debug info the PyPI Linux builds ship (about 90% of numkong) from the installed libraries in pure Python and rewrites RECORD, so uninstall stays clean; files are replaced with new inodes so uv's hardlinked cache is untouched. Each debug run is cut by a multiple of the segment alignment, so no loaded byte moves. Linux x86_64 goes from 58.7 MB to 10.2 MB installed, aarch64 from 32.2 MB to 6.5 MB; macOS (3.7 / 4.1 MB) and Windows (2.2 MB) are unchanged.
    • Installs only the half that is missing when one package is already there; a pair from an older install or the user's own is probed, never replaced.
    • Writes an unverified marker before touching the venv and probes the pair in a child interpreter (import, insert, search, save, memory-mapped restore) before Studio imports it. A failed probe uninstalls only what this run installed and writes failed (retried after 7 days or with --force); a hung probe stays unverified. vec_ann.usearch() never imports a failed, unverified or still-installing pair, so a bad native build cannot crash the backend.
    • Statuses: ready, already, missing (check-only), disabled (RAG_VEC_ANN_AUTO_INSTALL=0 or RAG_VEC_ANN=0), offline, unsupported, failed, busy. Exit 0 for all of them; --strict exits 1 unless the pair is ready.
  • The pair leaves studio.txt, no-torch-runtime.txt and the studio extra and is in no pip requirement at all (a test enforces it), so no install can fail over it; known_unmet_index ignores it.

Testing

  • test_hnsw_prebuilt.py: the hash file pins both packages; install flags and child env (constraints and secrets dropped, index / proxy / TLS kept); offline, opt-out, check-only; probe failure, unverified marker, an install killed halfway, no wheel for the platform, half a pair already installed; ELF debug stripping (synthetic ELF with a patchelf-style tail segment, unsafe layouts left alone) and in-place RECORD rewrite that leaves a hardlinked cache copy alone; and a real hash-pinned install, strip and probe of the pair that fails above 15 MB installed or with any debug section left.
  • test_rag_hnsw_runtime.py: one request per missing scope, switch to HNSW after the install without a write, backoff per status, markers and the install lock skipping the import, a module still initialising on another thread.
  • test_hnsw_prebuilt_step.py: installs and the setup.sh / setup.ps1 fast paths download nothing by default; with UNSLOTH_HNSW_PREFETCH=1 the step never raises and the fast paths stay local when nothing is owed (bash and pwsh).
  • Fresh venv with only numpy and tqdm: the backend's on-demand fetch was ready in 2.0 s, imported in process, 10.1 MB on disk, no wheel cache left.
  • Cross-platform runs: the installer reported ready and the tests passed on ubuntu x64 / arm64, macOS arm64 / x86_64 and Windows x64 / arm64; on AMD Strix Halo (gfx1151) Linux with ROCm and Windows 11 the pair installed and loaded and the HNSW suites passed (126 / 126).

Follow-ups: pre-stripped wheels to shrink the Linux download for the users who fetch it (12.9 MB today), manylinux2014 builds for glibc 2.17 to 2.27, and signed Windows .pyd files.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T08:26:55.685299Z 9a397de Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 5, 2026
danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 5, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…exact-cached waiting scopes to HNSW

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 314530bc96

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…outs like the backend, and baseline the installer load
…ng them, and drop the venv's wheel cache once the pair is ready

[skip ci]
danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 8, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 8, 2026
…ver uninstall the half that was already there

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 5ee6a31a27

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…update download it only with UNSLOTH_HNSW_PREFETCH=1

[skip ci]
@danielhanchen danielhanchen changed the title Studio: download the HNSW prebuilts (usearch + numkong) on install, update and first need Studio: download the HNSW prebuilts (usearch + numkong) only when a knowledge base needs them Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: e20aad14dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Conflicts:
#	tests/studio/install/test_windows_torch_flavor_invariant.py
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 8b1346882f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…only from PyPI (or UNSLOTH_HNSW_MIRROR) through the checked installer

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: cffdd2398b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…hashes --only-binary), debug info is stripped in place, and the hand-written wheel selection, downloader and JSON catalog go

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 692d7d51fe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 18, 2026
… and pip (#18358)

## Summary

On a runner whose own `certifi` or `pip` is below the CA trust floor and
has no `uv` on PATH, 59 cases in
`tests/studio/install/test_mlx_install.py` fail:

```
studio/install_python_stack.py:8272: in _ensure_trust_floor
studio/install_python_stack.py:9653: in pip_install_try
FileNotFoundError: [Errno 2] No such file or directory: 'uv'
```

This showed up on the macOS leg of the cross-platform staging run for
#16799 (runner certifi 2026.7.22, below the `certifi>=2026.10.19`
floor).

## Cause

`install_python_stack()` calls `_ensure_trust_floor()` on every setup
and update (#14887). That raises certifi, and pip on 3.10+, through
`pip_install_try` whenever the running interpreter does not already meet
the floor. The `_run_to_extras` helper stubs `pip_install`, `run`,
`_bootstrap_uv` and the torch steps, but not this path. So whether these
tests spawn a real installer depended on the runner image's certifi and
pip versions. On hosts that already meet the floor, nothing runs and the
tests pass.

The installer behaviour is correct. The test was not hermetic.

## Fix

Stub `_trust_floor_specs` to return `[]` in the helper, next to the
`_no_torch_pair` stub from #18354. The floor logic keeps its own
coverage in `tests/studio/install/test_ca_trust_floor.py`.

## Verification

Reproduced by raising the certifi floor above any installed version and
running with `uv` removed from PATH:

| Tree | floor unmet, no `uv` | normal |
|---|---|---|
| main | 59 failed, 15 passed | 74 passed |
| this branch | 74 passed | 74 passed |

`test_ca_trust_floor.py` passes unchanged, and ruff is clean.
# Conflicts:
#	tests/studio/test_rag_upload_requirements_cover_ci_imports.py

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 9a397de264

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 6eb3ac4 into main Nov 19, 2026
@danielhanchen
danielhanchen deleted the studio/hnsw-prebuilt branch November 19, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant