persistence-and-corpus-io
Persist and reopen bm25s indexes and JSONL corpora safely, including custom filenames, mmap loading, JsonlCorpus access, vocabulary loading, and large-index memory planning.
- License 许可
- MIT
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- persistence-and-corpus-io
- license
- MIT
- metadata
- { "disco-role": "operating" }
- description
- Persist and reopen bm25s indexes and JSONL corpora safely, including custom filenames, mmap loading, JsonlCorpus access, vocabulary loading, and large-index memory planning.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/file-format-and-api.md7.0 KB
- 参考文档references/large-corpus.md5.3 KB
- 参考文档references/troubleshooting.md6.2 KB
- 脚本scripts/save_reload_smoke.py2.8 KB
03 SKILL.md 正文
Persistence and corpus I/O
Use this route when an index already exists or must be written for later
retrieval. It owns the low-level BM25.save, BM25.load, and
BM25.load_scores contracts, the optional saved corpus, and on-disk memory
behavior. It does not replace tokenization, scoring, high-level file ingestion,
Hub transfer, or evaluation workflows.
Choose the loading mode
- Normal local reload:
BM25.load(index_dir, load_corpus=False)loads the numeric arrays and BM25 parameters into memory. Addload_corpus=Trueonly when result documents should be returned from the saved corpus. - Large index: use
mmap=Truefor the three CSC score arrays. If documents are also needed,load_corpus=True, mmap=TrueusesJsonlCorpusrather than reading the whole JSONL file into a list. - IDs-only retrieval: leave
load_corpus=False; retrieval returns document IDs. Keep the separately managed corpus in exactly the same index order if it will be applied later. - Scores-only inspection: create a BM25 object and call
load_scoreswith the saved array names and the knownnum_docs. This does not load parameters, vocabulary, or corpus metadata; useloadfor a usable retriever.
Read file-format-and-api.md for exact signatures, filenames, serialization rules, and custom-name examples. Read large-corpus.md before loading a large index or batching retrieval. Use troubleshooting.md for failure diagnosis. Run the bounded local check in scripts/save_reload_smoke.py after changing a persistence workflow.
Safe save/reload recipe
- Tokenize and index the documents with one stable vocabulary. The corpus used for result display is not the tokenized index: it is a positional document store, so keep its order and length aligned with the indexed documents.
- Save the index directory.
savecreates parent directories and writes the three numeric CSC arrays, BM25 parameters, and BM25 vocabulary. Passcorpus=...(or setBM25(corpus=...)) to write a JSONL corpus as well. - If a
Tokenizerwill be reconstructed independently, save its tokenizer vocabulary under its defaultvocab.tokenizer.jsonor another distinct name. Do not overwrite the BM25 model's defaultvocab.index.json. - Reload using the same custom filenames used during save. For textual query tokenization, also restore the matching tokenizer vocabulary; for integer token IDs, ensure the IDs came from the saved BM25 vocabulary.
- Verify one small query and, when
load_corpus=True, verify that the returned document at each result ID is the expected positional entry.
import bm25s
index = bm25s.BM25(method="bm25+")
index.index(bm25s.tokenize(["red fox", "blue whale"], stopwords=[]))
index.save(
"local-index",
corpus=[{"id": "r", "text": "red fox"}, {"id": "b", "text": "blue whale"}],
data_name="scores.npy", indices_name="rows.npy", indptr_name="columns.npy",
vocab_name="model-vocab.json", params_name="model-params.json",
nnoc_name="nonoccurrence.npy", corpus_name="documents.jsonl",
)
reloaded = bm25s.BM25.load(
"local-index", load_corpus=True, mmap=True,
data_name="scores.npy", indices_name="rows.npy", indptr_name="columns.npy",
vocab_name="model-vocab.json", params_name="model-params.json",
nnoc_name="nonoccurrence.npy", corpus_name="documents.jsonl",
)
The mmap flag is a loading choice, not a save format. It maps data,
indices, and indptr with NumPy read mode; it does not make indexing or
serialization incremental. The BM25L/BM25+ non-occurrence array is loaded
separately and is required for those methods.
Corpus and file invariants
- The default files are
data.csc.index.npy,indices.csc.index.npy,indptr.csc.index.npy,vocab.index.json,params.index.json,nonoccurrence_array.index.npy, andcorpus.jsonl. params.index.jsonis required to reconstruct the model. The three CSC arrays and the vocabulary are required for a normal usable load. Missing files should be repaired or the correct custom names supplied; do not create empty placeholders.load_vocab=Falsedeliberately skips the BM25 vocabulary. In this version the resultingvocab_dictandunique_token_ids_setare empty, so the object is suitable for metadata/array inspection but not normal retrieval. Useload_vocab=Truefor retrieval, or explicitly restore a verified matching vocabulary before issuing queries.load_corpus=Trueis best effort: if the named corpus file is absent, load still returns the index with no loaded corpus. It does not validate that the corpus length or order equals the indexed document count.- Strings are written as
{"id": <position>, "text": <string>}. Top-level dictionaries, lists, and tuples are accepted and are written as JSON. Nested values must be JSON-serializable (null, booleans, numbers, strings, arrays, and objects); sets, bytes, arbitrary Python objects, and most NumPy objects are not portable JSON values. - A document that cannot be serialized is warned about and skipped, which can
make the saved corpus shorter than the index. Treat that warning as a data
integrity failure, not as a harmless partial save. A non-iterable corpus is
not a valid
saveinput. - A JSONL companion index is written beside the corpus. For
documents.jsonlthe companion isdocuments.mmindex.json;JsonlCorpususes an existing companion without checking that it is fresh. Regenerate it after editing the JSONL file.
Security and compatibility
Keep allow_pickle=False unless a trusted legacy index explicitly requires
object-array loading. Setting it to True permits NumPy pickle handling and
must never be used to load an untrusted index directory. Numeric arrays written
by the normal save path should work with the default False setting. The
stdlib JSON fallback and the optional orjson implementation both produce the
same documented JSON-shaped corpus contract; orjson is not a reason to enable
pickle.
Install the base package for NumPy-backed local persistence. The core extra
adds optional helpers such as orjson, progress reporting, stemming, and
Numba; it is not required for the save/load file contract. Keep optional
backends explicit and CPU-safe. Do not download a benchmark corpus merely to
validate a local index.
Operational handoff
Report the index directory, exact filenames, whether mmap and
load_corpus were used, the model method, corpus count versus
scores["num_docs"], vocabulary strategy, and any skipped-document warnings.
Close a loaded JsonlCorpus when a long-lived process no longer needs it. For a
missing BM25L/BM25+ non-occurrence file or a corpus/index order mismatch, stop
retrieval and repair the artifacts rather than silently returning plausible but
wrong scores or documents.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).