PyTorch-native similarity search: convert FAISS indexes to nn.Module
Project description
torch-similarity-search
PyTorch-native similarity search library. Convert trained FAISS indexes to pure nn.Module models for GPU inference.
Train with FAISS, serve with PyTorch.
Why?
- No numpy overhead - FAISS requires numpy conversion; this keeps tensors on GPU
- TorchScript/ONNX export - Unified inference stack without FAISS dependency
- GPU memory sharing - Vectors stay in GPU memory alongside embedding models
- Production ready - No FAISS required in deployment containers
Installation
pip install torch-similarity-search
Quick Start
From FAISS
import faiss
import torch_similarity_search as tss
# Train with FAISS (your existing workflow)
quantizer = faiss.IndexFlatL2(128)
index = faiss.IndexIVFFlat(quantizer, 128, 100)
index.train(vectors)
index.add(vectors)
# Convert to PyTorch
model = tss.from_faiss(index)
model = model.cuda()
model.nprobe = 10
# Search with PyTorch tensors
queries = torch.randn(32, 128).cuda()
distances, indices = model.search(queries, k=10)
From Scratch
import torch
import torch_similarity_search as tss
# Create and train index
index = tss.IVFFlatIndex(dim=128, nlist=100, metric="l2")
index.train(vectors)
index.add(vectors)
# Search
distances, indices = index.search(queries, k=10)
Export to TorchScript
# Export for deployment (no torch_similarity_search needed to load)
scripted = torch.jit.script(model)
scripted.save("index.pt")
# Load anywhere
model = torch.jit.load("index.pt")
distances, indices = model.search(queries, k=10)
Supported Index Types
| FAISS Index | PyTorch Module | Status |
|---|---|---|
IndexIVFFlat |
IVFFlatIndex |
Supported |
IndexIVFPQ |
IVFPQIndex |
Planned |
API
IVFFlatIndex
IVFFlatIndex(
dim: int, # Vector dimensionality
nlist: int, # Number of clusters
metric: str = "l2", # "l2" or "ip" (inner product)
nprobe: int = 1, # Clusters to search (accuracy vs speed)
)
Methods:
train(vectors)- Train centroids via k-meansadd(vectors)- Add vectors to the indexsearch(queries, k)- Find k nearest neighbors
Properties:
ntotal- Number of indexed vectorsnprobe- Number of clusters to probe (settable)
from_faiss(index)
Convert a trained FAISS index to PyTorch. Currently supports IndexIVFFlat.
Performance
- Batched queries:
(batch_size, dim)->(batch_size, k) - GPU optimized with
torch.cdist - TorchScript compatible for kernel fusion
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_similarity_search-0.0.1.tar.gz.
File metadata
- Download URL: torch_similarity_search-0.0.1.tar.gz
- Upload date:
- Size: 49.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f08085e72b62abe21d73bf4370c9133bbf5d009d4896fa2270477da02a338ff
|
|
| MD5 |
65650a188f1e9136ae3e03edafe4148e
|
|
| BLAKE2b-256 |
07a73a3815b41e4231db0580f7ce61d3ab5d6b2e221ba6e4608e01a5e3af0c14
|
Provenance
The following attestation bundles were made for torch_similarity_search-0.0.1.tar.gz:
Publisher:
publish.yml on mwang633/torch-similarity-search
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_similarity_search-0.0.1.tar.gz -
Subject digest:
7f08085e72b62abe21d73bf4370c9133bbf5d009d4896fa2270477da02a338ff - Sigstore transparency entry: 779414051
- Sigstore integration time:
-
Permalink:
mwang633/torch-similarity-search@97d657c170150d4d19fa4ea3103c7ed7de9c958d -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/mwang633
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97d657c170150d4d19fa4ea3103c7ed7de9c958d -
Trigger Event:
release
-
Statement type:
File details
Details for the file torch_similarity_search-0.0.1-py3-none-any.whl.
File metadata
- Download URL: torch_similarity_search-0.0.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8eb51c29c8fafbde95977c6d280b6f27e501f17996c2a4b302d8c90a7a098f3
|
|
| MD5 |
7d90ec5995ace631a260111f560cf12c
|
|
| BLAKE2b-256 |
ec0d8b0cfb5f878a0b9f7c848a7709f38b4f742851fa5b5c487dcb2530638f69
|
Provenance
The following attestation bundles were made for torch_similarity_search-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on mwang633/torch-similarity-search
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_similarity_search-0.0.1-py3-none-any.whl -
Subject digest:
a8eb51c29c8fafbde95977c6d280b6f27e501f17996c2a4b302d8c90a7a098f3 - Sigstore transparency entry: 779414052
- Sigstore integration time:
-
Permalink:
mwang633/torch-similarity-search@97d657c170150d4d19fa4ea3103c7ed7de9c958d -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/mwang633
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97d657c170150d4d19fa4ea3103c7ed7de9c958d -
Trigger Event:
release
-
Statement type: