Skip to main content

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-means
  • add(vectors) - Add vectors to the index
  • search(queries, k) - Find k nearest neighbors

Properties:

  • ntotal - Number of indexed vectors
  • nprobe - 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

torch_similarity_search-0.0.1.tar.gz (49.8 kB view details)

Uploaded Source

Built Distribution

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

torch_similarity_search-0.0.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

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

Hashes for torch_similarity_search-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7f08085e72b62abe21d73bf4370c9133bbf5d009d4896fa2270477da02a338ff
MD5 65650a188f1e9136ae3e03edafe4148e
BLAKE2b-256 07a73a3815b41e4231db0580f7ce61d3ab5d6b2e221ba6e4608e01a5e3af0c14

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_similarity_search-0.0.1.tar.gz:

Publisher: publish.yml on mwang633/torch-similarity-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_similarity_search-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_similarity_search-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8eb51c29c8fafbde95977c6d280b6f27e501f17996c2a4b302d8c90a7a098f3
MD5 7d90ec5995ace631a260111f560cf12c
BLAKE2b-256 ec0d8b0cfb5f878a0b9f7c848a7709f38b4f742851fa5b5c487dcb2530638f69

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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