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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

sparse_emb_util-0.2.1-cp312-cp312-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

sparse_emb_util-0.2.1-cp312-cp312-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

sparse_emb_util-0.2.1-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.1-cp311-cp311-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

sparse_emb_util-0.2.1-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.1-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.1-cp310-cp310-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

sparse_emb_util-0.2.1-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.1-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.1-cp39-cp39-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

sparse_emb_util-0.2.1-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.1-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.1-cp38-cp38-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

sparse_emb_util-0.2.1-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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0defd87b3a7ed2c5dd517e9d0d68928be717d8a146d9d285827f17293c95abde
MD5 63b256133e2b88986a2e8289c2d386f3
BLAKE2b-256 5c4e3bfc941a79408e5e2b5271776910df9de35e9f958242c7387be24343e1a7

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7f3f2ef0d7a529db16802b560bcbecf3cea2c6f64f539e2f8e1d94c7165134ef
MD5 dd2e12eb32f2e393e29e27ff532dd443
BLAKE2b-256 6d44e799f50c0058539892f42538172f39a73722c34beb18f3723eb55c30cdd8

See more details on using hashes here.

File details

Details for the file sparse_emb_util-0.2.1-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0cbb3b3468655fc1823333087f57f74be846ac576b5f85bc5ccab420cd7ac79b
MD5 45b6d5c5a693b6183db4997216673ade
BLAKE2b-256 c69ffbeaeaabddb379f9c6cc413016b19fe0b7064eeb735db6f54fa214adbffb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8e603e7365b06f14f9fd4d5acee91dcca4f28a314dd7a9a0e7c3ab00e27874f7
MD5 5ed9624645af117ff9f19f94e795a449
BLAKE2b-256 a4a916fff0ab04bac4f6634389a76bcef8c0bdb3e757957257f145bff1382034

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b802256d9af0660e45758326891826a239c68005b5fabc7780bd88cb000ce06e
MD5 1e609e328d4d385b5f4a0e9028a0612d
BLAKE2b-256 bcfe0fb9811f0fc7afb9f18ed82454d294b3b077198806428ad42b823e342238

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 acc09dc20ee827636de1edca9065fadfd3f9d68e80bc18c0d793a5a52d9a544a
MD5 0e57a206b23b889aaa72270c786ec9c6
BLAKE2b-256 e562ae2ff758c7c72d16d2e45fa2ff04b9a6ebb713d3ccebe01aeef6dc04c5b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 92c0efea1cab77c3993186f870f9c6801dfb42d1fd082c3797da391600bc05fe
MD5 2ad637fe8da8ffc807a70b71d0ffbd3b
BLAKE2b-256 24a580157cd302b3c4adde501d2c2ae502692061fd748cb9e783616a60d8929c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4b6f7958aeea864d8d6a8e2f9603ab901d9c57e57ed8527a7363e1ccf9955c7
MD5 ee5c828c6476092703c786a9b932326c
BLAKE2b-256 239df444bc61845b0431d20a3371d328c8905354c1a460376aa77e70358928cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 18292048012f1d641a374365b4d10acd2a815703c92c357a7d0aa1ef51fb3f2f
MD5 c5475695b2183550207334c1813b2997
BLAKE2b-256 ed9061339809f1925e09a3d1e2cb7be61e3311909e5b4480edba4328e4d1ed20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 46e9335677d813235954230ac5dfc64250c908b96696ada855ab5cd55a65d22d
MD5 691c2f73b5a117ec4384c7262ff0a338
BLAKE2b-256 160c09e1bd46d4a84eeece3c03129438f2a44167f42b37801d5b8d78e1d4c742

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fb889066f66be3e7d6c3f702a92fbe26e63d270f246c8b34282dd85a4d6d48eb
MD5 b2d3881244a25dd07742f9fd48c75c27
BLAKE2b-256 1997fb310245edb256af7ec56f69a5d593599ad8c6bd4670569b2dc9f9ff797c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 92e80cb53e4bcb96f1d7c7972848632ecc42fb77a73d2ec790343ccb0bf6ca9c
MD5 bf4dd64a0c8c7dd3564fddfb9cd458e2
BLAKE2b-256 c9ac1b80297ef3d52148068bdd3ed93df5a90e8abd293a3a26de73d8fc6c49e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea778bd74c84d45b2a215a32e47aab5a6e1f7fe1ec9c7892c0174db5827a965a
MD5 75bb83446cda5a27096996b2f9d5959b
BLAKE2b-256 59bc829fa6825bfec7179da721726d0d82315163d1241aa188958081fbc1805b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 52decc2bc39483e855442ca3f812b9ce5877a7c485790ac399e750a5a3308571
MD5 e95d2f95b7d596ad7946485cfe064b14
BLAKE2b-256 09228db95baed92867fb90bae60e88007750c37a19e908c4472bddd959d2ebdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.1-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 426de22cc4267629108840637580826cc6459a3e793dac6dc671ffce794e6386
MD5 64febb0cfe26069a882c3c9703694b4b
BLAKE2b-256 f60b02fb522ab9dfacd30276d43fc9590cf7e2067430b44d078810e7b5da4d8b

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