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.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

sparse_emb_util-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

sparse_emb_util-0.2.2-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.2-cp311-cp311-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

sparse_emb_util-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

sparse_emb_util-0.2.2-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.2-cp310-cp310-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

sparse_emb_util-0.2.2-cp310-cp310-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

sparse_emb_util-0.2.2-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.2-cp39-cp39-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

sparse_emb_util-0.2.2-cp39-cp39-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

sparse_emb_util-0.2.2-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.2-cp38-cp38-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

sparse_emb_util-0.2.2-cp38-cp38-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

Details for the file sparse_emb_util-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 529d923331cf099ee7c37fbc964385008759c09cb7608dd98c08c9f43f23271a
MD5 bf01d3e34a9ea96b45fef422f5500a70
BLAKE2b-256 0bf05641e1c3114abe2d0c77f534bf9ab53c1ef32b1f373c5f89cd2e3a1350e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 62b145787468d965047fac73c93b83165adfde6731510d7c60971be7a2a52f13
MD5 800313242d4dee1044cfbeccf87ea65e
BLAKE2b-256 41a5708955d27e97aaf72e0f0178249870ab20ec7babe294fba9272c6d75c9ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 04ec1b259d773c45d7579300ca947497d59a70d5b2dfcb4bc750042496c632ed
MD5 649ee85694048b507123e9f8cd9ee57e
BLAKE2b-256 852b55bca8e8032e89270b81fe014c779a319cbb6486ec85d22a5da943b3d264

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bec1832b29f855f7fd7d6f52843559f7c7440f5aa7c974d6a3beb8dd13b85a6c
MD5 7c70704e37685924fc558949bdcadabc
BLAKE2b-256 373d91e15a2c360dc649e3be59ad914c5f7c2b33eaa1dc48b7318d110e845e5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 84cf29a404ea54f983bdbead9bd8aebf0ff8eb3d0867304f7ebda12c436cb191
MD5 1d4dd9c1296ff79029644acb58a37e5a
BLAKE2b-256 cf85fc02325b4711a3603f07408e32b6218610c3ad509dde236da24df47a368e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 51f85b94d55afbcca409dad807f9ce77bffd9d53ea6f0170a07c5cc83aed3d79
MD5 32ac2e7fc84b0489072d7bc736940193
BLAKE2b-256 f4f4b67d539ad9da39b74f8352199e6224f5d01a149889d65185fce3b4720fa6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c4282ab7285f099dc720a5b3434a63480eaa6c73b580b0e79b95329eb5d40628
MD5 59926e19672d7d2aec27e31d5d8470d9
BLAKE2b-256 aab33c50051fe31e1ba2a0655be8585516b055be05482d8991638590c6756327

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 40348737c2997de7d4ce48218b826feec9ce822a24f754ee004deee834042b40
MD5 eea040defa24f92c303249f016c25997
BLAKE2b-256 979928d64c371461235bf4f89c7a0e4b360055b3188866e28b783b9e0c4f04fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ab1e9a802b2fa89ed08e4d7a20633196b8b91471cf02e965582e33d5ecc68186
MD5 d69dd5ba9f84985b6585cbf8925fd07a
BLAKE2b-256 773924eef40b65d5a9b120aa1c12d9f9c8fe5f44eec776618097b8c25665bb01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c418f93604ee4aaffa3a185e4f723cac76896b89578f45a642beca10a190536c
MD5 ddebfe08a7e581b9c4f6d19bfb79b505
BLAKE2b-256 779f8a30056c9f7550e79c79a27ef8d89e7cc0ca169e075276e0833ae46e098b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2490b5108e3610dbd9a0361f4144bd74f47860dd937456c4c38066b6cdb9f77a
MD5 41ef632fe72c577d15ceca8063629674
BLAKE2b-256 c9cd480c221d584e77559ef26e9116581af538edb5e4fca9f3d04795428196af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bc26226f449a8408a74eec57b472654c0320db2c396d5d21d82116b2c3e39207
MD5 0456ee84a44df87c28411264a568a647
BLAKE2b-256 444c500cea1f04d9617e5834312297dcbccc51c14d6a041f6f1f50d385393496

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66917b1d107b171dcaa4045dbd6849685ff9ecb75281778c32640906652fc193
MD5 70b1405fff773aa456238aa3cd80d221
BLAKE2b-256 d127f1d805876651808fbeda9d14eb39d747d9d7ba09d7625832a61724fcfaae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7f86a5fc951e713a5172b63465044a12c646b1911fbf8823a95f396a39c36cc2
MD5 2857db3835068f3147e9e96e78fba0a7
BLAKE2b-256 09c7e0f6b95c5593b826734d68fd18057b937f406beb1213deb72b2b3da4db24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sparse_emb_util-0.2.2-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e4c9ca11a333dca27270196d7f74ba603d5bd5ac34f06cbd071717089d08059b
MD5 97d0667869ea908bb684686e250d1550
BLAKE2b-256 ed7c0f9e253e4d5a79fa6030ac29a322bccd1ced53f574b60a906c0c3eb4e09d

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