Acceleration and selection
Use this route when a local bm25s index needs optional Numba scoring/retrieval,
Numba-assisted CSC construction, NumPy/JAX top-k selection, or a diagnosis of
backend and optional-dependency behavior. Keep the ordinary indexing/retrieval
recipe in the core route; this route supplies the acceleration choices that are
safe to add to it.
Make the backend decision explicit
- The required path is CPU NumPy:
BM25(backend="numpy", csc_backend="numpy"), followed byretrieve(..., backend_selection="numpy"). The base package requires NumPy; optional speed packages are not a runtime prerequisite. backendselects scoring/retrieval execution."numba"requires Numba;"auto"resolves to Numba when the import is available and otherwise to NumPy. The resolved value is stored inretriever.backendat construction.csc_backendselects index construction:"numpy"is the dependency-light default,"scipy"usesscipy.sparse.csc_matrix, and"auto"resolves to SciPy when SciPy imports successfully and otherwise to NumPy. This choice is made while constructing the index, not while selecting top-k results.backend_selectionis a separate top-k selector. For a NumPy BM25 backend,"numpy","jax", and"auto"are meaningful."auto"uses JAX whenbm25s.selection.JAX_IS_AVAILABLEis true and NumPy otherwise. JAX is a CPU option here; this skill makes no CUDA or GPU-performance promise.- For a Numba BM25 backend, retrieval internally maps
backend_selection="auto"to"numba"; the Numba retrieval path requiresbackend_selection="numba". Do not combinebackend="numba"withbackend_selection="jax"or"numpy".
The complete parameter and error table is in references/api-reference.md. Use the bounded local checker at scripts/numba_smoke.py before benchmarking or changing a production configuration.
Install only what the chosen path needs
pip install bm25s # NumPy base path
pip install "bm25s[core]" # project core extras, including Numba
pip install "bm25s[indexing]" # SciPy CSC option
pip install "bm25s[selection]" # CPU JAX top-k option
The package metadata also exposes numba, scipy, and jax[cpu] through the
corresponding extras. full combines extras, but is not required for this
route. Install into the same interpreter that imports bm25s; report the
actual availability flags rather than assuming that an extra was installed.
Baseline, Numba, and selector recipes
Start with a baseline when correctness or portability matters:
retriever = bm25s.BM25(backend="numpy", csc_backend="numpy")
retriever.index(corpus_tokens, show_progress=False)
results = retriever.retrieve(
query_tokens, k=10, backend_selection="numpy", show_progress=False
)
For CPU Numba retrieval and optional Numba CSC construction, make compilation an explicit decision:
retriever = bm25s.BM25(
backend="numba", csc_backend="numpy", auto_compile=False
)
retriever.compile(activate_numba=True, warmup=False)
retriever.index(corpus_tokens, show_progress=False)
results = retriever.retrieve(
query_tokens, k=10, backend_selection="numba", show_progress=False
)
Call compile before index when Numba CSC construction should be used; call
it after loading an existing index when only retrieval scoring needs activation.
warmup=True runs small dummy CSC and scorer calls to pay JIT compilation before
the first real request. The first call can still have signature/cache overhead,
and Numba is CPU-only in this operating contract. See
references/numba-workflows.md for the timing,
thread, and fallback decisions.
For NumPy scoring with optional JAX selection:
retriever = bm25s.BM25(backend="numpy", csc_backend="numpy")
retriever.index(corpus_tokens, show_progress=False)
results = retriever.retrieve(
query_tokens, k=10, backend_selection="jax", show_progress=False
)
Use backend_selection="auto" only when a JAX-versus-NumPy choice is
acceptable. bm25s.selection.topk accepts one-dimensional score arrays and
raises ValueError for an invalid selector; an explicit JAX request raises
ImportError when JAX is unavailable. The Numba selector is a separate
bm25s.numba.selection.topk API and accepts only backend="numba".
Compile and fallback rules
auto_compileis a constructor control and defaults toTrue, but explicitcompile(...)is the reproducible choice when relying on JIT activation. This revision's constructor guard is environment-sensitive; withNUMBA_DISABLE_JITunset, inspection observed no automatic compiled wrapper despite the default. Do not infer compilation from the constructor flag; inspect or run a smoke retrieval.- If
NUMBA_DISABLE_JITis set, the scorer activation/warmup path is skipped. Unset it when JIT execution is intended. Keepauto_compile=Falsefor predictable setup and compile deliberately. - Missing Numba with
backend="auto"is a normal fallback to NumPy. Missing Numba withbackend="numba"orcompile(...)is an actionableImportError. - Missing SciPy with
csc_backend="auto"falls back to NumPy. Explicitcsc_backend="scipy"raisesImportError. - Missing JAX with selector
"auto"falls back to NumPy. Explicit"jax"raisesImportError; an invalid selector raisesValueError. - Do not present optional backends as CUDA support. If a dependency emits a GPU-availability warning but imports its CPU implementation, record that as CPU execution rather than claiming accelerator coverage.
For install/import, data/configuration, API/CLI, and workflow-specific failures, use references/troubleshooting.md. Do not use Hub/BEIR network examples as acceleration tests; keep checks local and bounded.
