Skip to main content

Utilities for sparse embedding processing using Rust + Python.

Project description

Sparse Emb Util

Efficient Sparse Embedding Utilities for IR Research

Rust + PyO3 + Maturin | Multi-threaded | NumPy Compatible

🔬 This library is part of the official implementation of
LightRetriever: A LLM-based Hybrid Retrieval Architecture with 1000× Faster Query Inference.


Introduction

sparse_emb_util is a high-performance Python extension written in Rust, designed to efficiently process sparse embeddings in sparse retrieval systems. It enables:

  • Multi-threaded quantization of float16/float32 sparse vectors
  • Conversion to JSON or pseudo-text format
  • Regex-based and Unicode-based multilingual tokenization
  • Lightweight answer annotation via substring token match

Installation

Install from PyPI

pip install sparse_emb_util

Or build from source:

pip install maturin
git clone https://github.com/ma787639046/sparse_emb_util.git
cd sparse_emb_util
maturin develop --release

API Overview

Converter: Multi-threaded Quantization of Float16/Float32 Sparse Vectors

from sparse_emb_util import Converter
import numpy as np

converter = Converter(vocab_dict={0: "the", 1: "world"})
reps = np.array([[0.2, 0.8]], dtype=np.float32)

# Convert to quantized JSON (`json_reps == {'the': 20, 'world': 80}`)
json_reps = converter.convert_sparse_reps_to_json(reps, convert_id_to_token=True)

# Convert to pseudo text (`text_reps == the the ... (Repeat x20) world world world ... (Repeat x80)`)
text_reps = converter.convert_sparse_reps_to_pseudo_text(reps, convert_id_to_token=True)

Converter Methods

Method Input Type Output Description
convert_sparse_reps_to_json np.ndarray[np.float32] List[Dict[str, int]] Convert float32 sparse vectors to quantized JSON format
convert_sparse_reps_to_json_f32 np.ndarray[np.float32] List[Dict[str, int]] Same as above, explicitly for float32
convert_sparse_reps_to_json_f16 np.ndarray[np.float16] List[Dict[str, int]] Convert float16 sparse vectors to quantized JSON format
convert_sparse_reps_to_pseudo_text np.ndarray[np.float32] List[str] Convert float32 sparse vectors to quantized pseudo text
convert_sparse_reps_to_pseudo_text_f32 np.ndarray[np.float32] List[str] Same as above, explicitly for float32
convert_sparse_reps_to_pseudo_text_f16 np.ndarray[np.float16] List[str] Convert float16 sparse vectors to quantized pseudo text
convert_json_reps_to_pseudo_text List[Dict[str, int]] List[str] Convert JSON representations back into pseudo-text format

Optional kwargs (supported by all functions):

  • quantization_factor: Quantization Factor for upscale before flooring (e.g., 100)

  • convert_id_to_token: Whether to use vocab_dict to convert IDs to strings

  • allow_negative_values: Allow neg_ prefixed keys

  • negative_prefix: Customize the prefix for negative values

RegexTokenizer: Regex-Based Tokenization

Mimics Facebook DPR / DrQA regex logic.

from sparse_emb_util import RegexTokenizer

tokenizer = RegexTokenizer(pattern=None, lowercase=True, normalize=True, normalization_from="NFD")
tokens = tokenizer.tokenize("Hello, World!")
  • Use batch_tokenize() for multiple strings

  • Callable: tokenizer(["string1", "string2"])

ICUWordPreTokenizer: Unicode-Aware Tokenizer

Uses ICU4X for multilingual word boundary detection.

from sparse_emb_util import ICUWordPreTokenizer

tokenizer = ICUWordPreTokenizer(stopword_sets={"the", "is"})
tokens = tokenizer.tokenize("これは日本語とEnglishの混合文です。")
  • Supports control-sequence removal, stopword filtering, and lowercasing

  • Use batch_tokenize() or __call__() for batched input

QAAnnotator: Question-Answer Relevance Judging

Match answers against pre-tokenized corpus via multi-thread sub-list matching for simple QA supervision.

from sparse_emb_util import QAAnnotator

annotator = QAAnnotator(
    docid_to_tokenized_corpus={"docid1": ["hello", "world", "my", "friend", "!"]},
    pattern=None,
    lowercase=True,
    normalize=True,
    normalization_from="NFD"
)

# ["hello", "world"] is a sub-list of ["hello", "world", "my", "friend", "!"]
# Return `{"qid1": {"docid1": 1}}`
qrels = annotator.annotate(
    qid_to_docids={"qid1": ["docid1"]},
    qid_to_answers={"qid1": ["hello", "world"]}
)

# ["hi", "friend"] is not a sub-list of ["hello", "world", "my", "friend", "!"]
# Return `{"qid1": {"docid1": 0}}`
qrels = annotator.annotate(
    qid_to_docids={"qid1": ["docid1"]},
    qid_to_answers={"qid1": ["hi", "friend"]}
)

Citation

If you use this library, please cite the following paper:

@misc{Ma2025LightRetriever,
    title={LightRetriever: A LLM-based Hybrid Retrieval Architecture with 1000x Faster Query Inference}, 
    author={Guangyuan Ma and Yongliang Ma and Xuanrui Gou and Zhenpeng Su and Ming Zhou and Songlin Hu},
    year={2025},
    eprint={2505.12260},
    archivePrefix={arXiv},
    primaryClass={cs.IR},
    url={https://arxiv.org/abs/2505.12260}, 
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sparse_emb_util-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

sparse_emb_util-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

sparse_emb_util-0.2.0-cp311-cp311-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

sparse_emb_util-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

sparse_emb_util-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

sparse_emb_util-0.2.0-cp310-cp310-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

sparse_emb_util-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

sparse_emb_util-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

sparse_emb_util-0.2.0-cp39-cp39-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

sparse_emb_util-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

sparse_emb_util-0.2.0-cp38-cp38-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

sparse_emb_util-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

Details for the file sparse_emb_util-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39d63dfd65e95dc0991b55dd6e89934c14fd354847f841ae99cf918d7500174c
MD5 1b9df1f7532a03fc8964c265c6a2bcb6
BLAKE2b-256 9d22f69c2ce9c7e9909a70624d7022cbb572b618bbf99307eef406485570a58a

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9478a8e7995ee14b5caa0b1a7ac314f319107cafb2c7866c38ed30a9739ba056
MD5 91ded3eb854629f7b32c35634b1ddcaf
BLAKE2b-256 1bb25ee32e6f66eeda66e079054b6c5589825ca4dcf42db36067aa020c7c7d6a

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 99893e328d895a6d7317f15d044a1fe710da9885f88b28c0ae2f5934915d6965
MD5 1fb769bc5347b26fd25899568f411d51
BLAKE2b-256 dbf853c624ced80781c22abc731e23a79f0873f9c2a890f592a0488ea0f9c833

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1932a56b5806260739027e8db6940ec07077e54215f0398e870261cf82ddd392
MD5 233bfc0c01c6c833d065ad27733c3aa2
BLAKE2b-256 54ec325043ffde9cda3850862bf9ae9abe97349e279e4b0a13fb3054751e2cc7

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e5c99bc38490dd67bd4659fa4276865dfd0c53b358e8e2c9c42b96a2f6f153f
MD5 9361af1adae2db2231641a9d7167c49c
BLAKE2b-256 ef2c316e2ca659dcdce7bbacd4067fc28b9634729bdc80347eefe30bae555b06

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 86e3d2e6f97980f6c1d88b7dcb8182966fec72cf09c7a6490eb9c03f1de80f9b
MD5 b8110749bb9d898f81f842b93e3b74e0
BLAKE2b-256 ae2ea200b521386a7f5baddef216119b443b042bc672bee03e82b172cbe6604d

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a82ffed8220124b5ce8e9e456f693b93e46075edd08a70d44aee21afc0ca3e68
MD5 af797a842ad8b040b788687fb2239ad4
BLAKE2b-256 2e6ffec8465ce6f323f7c3f351a5c7f2f413f9944159c63bc5e386a540b08de4

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a68fc702e4ed5b6b64812687c90dbb66fc4da33e8e4019e65a0f108583928437
MD5 f024cf80b09222a33a31f6e6b9e21d0e
BLAKE2b-256 1dd4792c38d1c740f331fe23701d092026464a0b3d0cc5ca44f553307f30bbb8

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dc82bf1cfed2b4bbc48d730cc05792cfca9bc16b82b0a88491ec472d71ff2b3c
MD5 aaaf109da3e2f1925c064501fd056b8c
BLAKE2b-256 f068dcfd6d1b2745d88448635ac908278d95dbc8179c39146af19d232d2db927

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e18013da878e5d63dcd86613512f97560328cce3cefcbe9ada8b836994cdc111
MD5 3e0f59ca767f778479f1f7ab6b51b7bc
BLAKE2b-256 acbd58163bfea894a4bbdf8fca2bf0a6f37bb2dffa7ae5ad2cc206b243dea4e7

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cb7c795577b88f3ca8a61774bfbf4ac140ee535caf10d65bfff76d42f4dcaa04
MD5 670d5b601bd2d1413b367e0236ccade7
BLAKE2b-256 baa3b57d2cacfafb3f6157498ca03637ae366c1a17c3bff1d6277d8ced71e517

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.0-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c370d0cd6ae92422304829a34e4cfead36ccf08c3f7be979386e36c44c840c8b
MD5 5496dbd307490aead545949f611371e7
BLAKE2b-256 ab7e2b8a6662ce63a0d9f55d8c58dfac5968bcf481d9a083a4d4bf19cf0eb7ec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page