fortran-nlp-kernels
Exact lexical and dense-vector similarity search for Python with compiled Fortran, OpenMP, OpenBLAS, CUDA Fortran, and cuBLAS backends.
fortran-nlp-kernels provides exact word-shingle Jaccard search and exact flat vector search for inner product, squared L2 distance, and cosine similarity. It combines a Python API with native CPU and NVIDIA GPU implementations, deterministic top-k selection, and a benchmark-informed CPU-only backend="auto" resolver.
Release status
Version 0.1.1 adds explicit CUDA Fortran and cuBLAS vector-search backends for single-query and batched search. CUDA lexical Jaccard search remains available through the handwritten CUDA Fortran backend.
Version 0.1.0 introduced benchmark-informed CPU backend selection, optional machine-specific OpenMP and OpenBLAS thread calibration, versioned calibration profiles, runtime memory guardrails, and temporary thread control.
The package is intended for exact similarity search, systems experimentation, reproducible performance engineering, and integration into Python applications.
Platform support
CPU distribution
- Linux x86-64
- CPython 3.10-3.13
manylinux_2_28wheels- Serial Fortran, OpenMP Fortran, and OpenBLAS backends
CUDA companion distribution
- Linux x86-64
- CPython 3.12 for the initial
0.1.1CUDA wheel - NVIDIA GPU with a compatible driver
- CUDA 13.3 runtime and cuBLAS available on the host
- Any additional NVIDIA HPC SDK runtime libraries reported by the published wheel
- External-runtime packaging model: NVIDIA runtime libraries are not bundled into the wheel
The CUDA companion is distributed separately as fortran-nlp-kernels-cuda and installs _cuda beside the base package. The base CPU package remains usable without NVIDIA software.
Features
- Exact word-shingle Jaccard search, including batched search
- Exact inner-product, squared-L2, and cosine vector search
- Serial and OpenMP Fortran CPU kernels
- OpenBLAS SGEMV and SGEMM vector backends
- Handwritten CUDA Fortran lexical and vector kernels
- cuBLAS SGEMV and SGEMM vector backends
- Deterministic CPU top-k with stable ties and zero-based indices
- CPU workload-aware
backend="auto"dispatch - Optional machine-specific OpenMP and OpenBLAS thread calibration
- Runtime memory guardrails and temporary thread control
- Native Fortran, C ABI, nanobind, NumPy-reference, and public API tests
Installation: PyPI
CPU package
Install the stable CPU distribution:
python -m pip install fortran-nlp-kernels==0.1.1
The prebuilt CPU wheels do not require a Fortran compiler or OpenBLAS development package.
CUDA companion
Install the CUDA companion and its matching base package:
python -m pip install fortran-nlp-kernels-cuda==0.1.1
The companion declares an exact dependency on:
fortran-nlp-kernels==0.1.1
The CUDA wheel uses external NVIDIA runtime libraries. Before importing fortran_nlp._cuda, ensure that the supported NVIDIA driver, CUDA runtime, cuBLAS, and required NVIDIA HPC SDK runtime libraries are discoverable by the Linux dynamic loader.
Basic diagnostics:
nvidia-smi
After installation:
python - <<'PY'
from fortran_nlp import _cuda
print(_cuda.__file__)
PY
To inspect unresolved or externally loaded libraries:
ldd "$(python -c 'from fortran_nlp import _cuda; print(_cuda.__file__)')"
The release notes for the CUDA companion specify the exact build toolchain and runtime versions. The initial 0.1.1 CUDA wheel targets CUDA 13.3 and NVIDIA HPC SDK 26.9.
Editable CPU build
git clone https://github.com/suwesh/fortran-nlp-kernels.git
cd fortran-nlp-kernels
python -m pip install -e .
A CPU source build requires a supported Python environment, GFortran, a C++17 compiler, OpenMP, and OpenBLAS development files.
On Ubuntu:
sudo apt update
sudo apt install gfortran g++ libopenblas-dev libgomp1
Editable CUDA-enabled build
python -m pip install -e . \
--config-settings=setup-args="-Dfnlp_cuda=true" \
--config-settings=setup-args="-Dfnlp_cuda_arch=cc86" \
--config-settings=setup-args="-Dfnlp_cuda_toolkit=cuda13.3"
A CUDA source build additionally requires NVIDIA HPC SDK with nvfortran, a compatible CUDA toolkit, and an architecture value appropriate for the target GPU. The example uses cc86, which matches the development RTX 3060 system.
Quick start
Exact lexical Jaccard search
from fortran_nlp.lexical import JaccardIndex
index = JaccardIndex(
[
"reset the account password",
"update contact information",
"create a new insurance application",
],
backend="auto",
)
results = index.search(
"how to reset the account password",
k=2,
minimum_score=0.01,
)
for result in results:
print(result)
JaccardIndex.search_batch() compares multiple candidates against the same indexed corpus:
batch_results = index.search_batch(
[
"how to reset the account password",
"contact information update",
],
k=2,
minimum_score=0.01,
)
Example result:
JaccardResult(
document_id=0,
score=0.5,
intersection_count=2,
union_count=4
)
Text preprocessing performs Unicode normalization, case folding, fixed-size word shingling, deterministic 64-bit hashing, sorting, and deduplication before native execution.
Exact vector search
The Python-facing database layout is:
(vector_count, dimension)
All public vector-search inputs are converted to contiguous float32 arrays. The package internally stores the database in a Fortran-compatible (dimension, vector_count) layout.
import numpy as np
from fortran_nlp.vector_search import (
FlatIndexCosine,
FlatIndexIP,
FlatIndexL2,
)
database = np.array(
[
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[-1.0, 2.0, 1.0],
],
dtype=np.float32,
)
query = np.array([1.0, 2.0, 3.0], dtype=np.float32)
ip_index = FlatIndexIP(database, backend="auto")
scores, ids = ip_index.search(query, k=2)
l2_index = FlatIndexL2(database, backend="cuda_fortran")
distances, ids = l2_index.search(query, k=2)
cosine_index = FlatIndexCosine(database, backend="cuda_cublas")
scores, ids = cosine_index.search(query, k=2)
FlatIndexL2 returns squared Euclidean distance and does not calculate the square root. FlatIndexCosine normalizes the database and queries before inner-product search; zero vectors are invalid.
Batched vector search
With backend="auto", batched inner-product and cosine search selects between repeated OpenBLAS SGEMV and native OpenBLAS SGEMM according to query count, the active policy, and the runtime memory budget. Batched squared-L2 search uses the selected repeated serial, repeated OpenMP, or explicit BLAS execution path.
The public query layout is:
(query_count, dimension)
Batched search uses the same index:
queries = np.array(
[
[1.0, 2.0, 3.0],
[0.0, 1.0, 0.0],
],
dtype=np.float32,
)
scores, ids = ip_index.search_batch(queries, k=2)
print(scores.shape) # (query_count, k)
print(ids.shape) # (query_count, k)
Backends
Lexical search
fortran -> serial Fortran
fortran_openmp -> OpenMP Fortran
cuda_fortran -> handwritten CUDA Fortran
auto -> CPU workload-aware dispatch
Vector search
fortran -> serial Fortran
fortran_openmp -> OpenMP Fortran
fortran_blas -> OpenBLAS SGEMV/SGEMM
cuda_fortran -> handwritten CUDA Fortran scoring + CPU top-k
cuda_cublas -> cuBLAS scoring + CPU top-k
auto -> CPU workload-aware dispatch
CUDA backends are explicit in 0.1.1. backend="auto" remains CPU-only. Automatic CPU/GPU crossover dispatch is deferred until calibrated GPU performance data is available.
Automatic CPU backend selection
Automatic resolution happens at search time, when the complete workload is known. The original "auto" request remains visible through the index's backend property.
Policy precedence is:
1. Explicit backend request
2. Compatible machine calibration profile
3. Packaged benchmark-informed fallback policy
Packaged fallback policy
Without a compatible machine profile, 0.1.0 uses conservative workload rules derived from the packaged CPU performance findings.
Lexical single query
small workload -> serial Fortran
larger workload -> OpenMP Fortran
Lexical batch
small corpus -> repeated selected single-query path
scaled batch size 1 -> native batched OpenMP
scaled batch size 2-31 -> repeated OpenMP
scaled batch size >= 32 -> native batched OpenMP
IP and cosine single query -> OpenBLAS SGEMV
IP and cosine batch 1-7 -> repeated SGEMV
IP and cosine batch >= 8 -> OpenBLAS SGEMM
Squared-L2 single query -> OpenMP Fortran
Squared-L2 batch -> repeated OpenMP
Native batched lexical execution and vector SGEMM are selected only when their estimated allocations fit the runtime memory budget. Otherwise, automatic dispatch falls back to repeated execution.
Explicit backend override
Explicit backends always bypass automatic policy selection:
serial_index = FlatIndexIP(database, backend="fortran")
openmp_index = FlatIndexIP(database, backend="fortran_openmp")
blas_index = FlatIndexIP(database, backend="fortran_blas")
Explicit requests also preserve the user's existing OpenMP or OpenBLAS runtime settings.
Optional CPU calibration
The stable release includes explicit, bounded machine calibration for representative OpenMP and OpenBLAS thread counts.
Calibration is never run during installation, import, index construction, or ordinary search. It does not modify the packaged fallback policy.
Run calibration:
fortran-nlp-calibrate
Equivalent module invocation:
python -m fortran_nlp.calibration
Show the active compatible profile:
fortran-nlp-calibrate --show
Reset the selected user profile:
fortran-nlp-calibrate --reset
Use a custom profile path:
fortran-nlp-calibrate --output /path/to/cpu_profile.json
Or configure profile discovery:
export FORTRAN_NLP_PROFILE=/path/to/cpu_profile.json
The default profile location is:
~/.config/fortran-nlp-kernels/cpu_profile.json
Profiles are versioned and checked against the installed package and machine fingerprint. Missing, malformed, stale, or incompatible profiles safely fall back to the packaged policy.
The 0.1.0 calibration command tunes representative thread counts for:
- lexical OpenMP execution;
- squared-L2 OpenMP execution;
- OpenBLAS
SGEMV; - OpenBLAS
SGEMM.
The generated profile records candidate thread counts, selected counts, median timings, warm-ups, repeats, and the crossover-policy source. Machine-specific workload crossover calibration is deferred to a later release.
Thread control
Automatic execution temporarily applies the selected OpenMP or OpenBLAS thread count around the native call and restores the previous runtime setting afterward, including when an exception occurs.
Explicit backends preserve the user's runtime configuration. Environment variables remain useful for explicit execution and controlled benchmarks:
# Handwritten serial
OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 python benchmark.py
# Handwritten OpenMP
OMP_NUM_THREADS=6 OPENBLAS_NUM_THREADS=1 python benchmark.py
# OpenBLAS
OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=6 python benchmark.py
The package does not globally overwrite OMP_NUM_THREADS or OPENBLAS_NUM_THREADS.
Architecture
Python public API
↓
Validation, normalization, and workload description
↓
Explicit backend routing or CPU backend resolver
↓
nanobind C++ extension
↓
stable C ABI
↓
Fortran / OpenMP / OpenBLAS / CUDA Fortran / cuBLAS
↓
deterministic CPU top-k
The CPU vector path is separated into:
vector_search_core.f90 -> serial and OpenMP IP/L2 kernels
vector_search_blas.f90 -> OpenBLAS SGEMV/SGEMM
vector_search_c_api.f90 -> C ABI
cpu_bindings.cpp -> Python adapter
topk_core.f90 -> deterministic exact top-k
The CUDA vector path follows the same layered structure:
vector_search_cuda_core.cuf -> handwritten CUDA IP/L2 scoring
vector_search_cublas.cuf -> cuBLAS IP/L2 scoring
vector_search_cuda_c_api.cuf -> C ABI
cuda_bindings.cpp -> Python adapter
topk_core.f90 -> deterministic CPU top-k
For CUDA vector search in 0.1.1:
GPU scoring
-> copy the complete score array or matrix to host
-> deterministic CPU top-k
-> return values and zero-based indices
The cuBLAS squared-L2 path uses:
GPU squared-norm kernels
-> cuBLAS SGEMV/SGEMM inner products
-> GPU L2 norm-expansion kernel
-> host top-k
Correctness and validation
The implementation is validated across:
native Fortran tests
C ABI tests
nanobind extension tests
manual numerical examples
NumPy equivalence tests
CPU/CUDA equivalence tests
handwritten CUDA/cuBLAS equivalence tests
single-query and batched-query tests
cosine normalization tests
public Python API tests
input-validation and edge-case tests
deterministic tie-handling tests
fallback-policy and calibration-profile tests
memory-guardrail and thread-restoration tests
CPU-only and CUDA-enabled build tests
Validated 0.1.1 configurations include:
- CUDA-enabled editable build: 428 Python tests passed
- CUDA-focused suites: 223 tests passed
- Public CUDA vector-index suite: 116 tests passed
- Low-level CUDA vector suite: 84 tests passed
- CPU-only editable build: 205 tests passed with
_cudaabsent
Run the complete available suite:
python -m pytest tests -q
Native test programs and runners are available under:
tests/native/
Current limitations
- Linux x86-64 only
- Result IDs are zero-based positional indices
- Exact top-k selection currently uses an
O(Nk)implementation - CUDA wheels require external NVIDIA runtime libraries
- CUDA backends must be selected explicitly
backend="auto"is CPU-only- No GPU-resident database or corpus
- No GPU top-k or fused scoring-selection kernel
- No CPU/GPU crossover policy, multi-GPU execution, or mixed-precision scoring
- No CPU affinity, NUMA-aware dispatch, or hybrid-core-aware placement
Build and release system
The project uses:
meson-python -> Python build backend
Meson -> native build configuration
GFortran -> CPU Fortran compilation
nanobind -> compiled Python extensions
OpenMP -> handwritten multicore kernels
OpenBLAS -> CPU SGEMV and SGEMM
NVIDIA HPC SDK -> CUDA Fortran compilation
cuBLAS -> GPU SGEMV and SGEMM
Build and validate the CPU source distribution and wheel:
python -m pip install build twine
rm -rf dist build .mesonpy-*
python -m build
python -m twine check dist/*
Official CPU and CUDA distributions are built in GitHub Actions and published to separate PyPI projects through Trusted Publishing.
Research direction
The library is being developed as a reusable software artifact and performance-engineering study covering:
- exact lexical and dense-vector similarity search;
- Python/native interface overhead;
- serial and multicore CPU scaling;
- handwritten Fortran versus optimized BLAS;
- C++ and CUDA C++ research baselines;
- CUDA Fortran and cuBLAS backends;
- CPU/GPU crossover points;
- kernel-only and end-to-end performance.
Working technical-report title:
Fortran Kernels for NLP: Exact Lexical and Vector Similarity Search Across Multicore CPUs and NVIDIA GPUs
Roadmap
Follow-up work may include:
- faster exact top-k selection;
- persistent GPU-resident databases and corpora;
- GPU top-k and fused score-selection kernels;
- calibrated CPU/GPU crossover dispatch;
- broader Python, platform, and GPU-architecture support;
- CUDA C++ and FAISS research comparisons;
- multi-GPU and mixed-precision experiments.
License
This project is licensed under GPL-3.0-only. See LICENSE.
Author
Suwesh Prasad Sah
- Homepage: https://suwesh.github.io/
- Repository: https://github.com/suwesh/fortran-nlp-kernels
- Issues: https://github.com/suwesh/fortran-nlp-kernels/issues
Metadata
Release files for fortran-nlp-kernels 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fortran_nlp_kernels-0.1.1.tar.gz | 146.9 kB | With provenance | Details for fortran_nlp_kernels-0.1.1.tar.gz |
Built distributions (wheels)
Total release size: 49.0 MB
Release files / fortran_nlp_kernels-0.1.1.tar.gz
| Download URL | fortran_nlp_kernels-0.1.1.tar.gz |
|---|---|
| Size | 146.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8a682c3041353987b926cedfe90164fe436459d263384ca3c90cbff27abc77ec
|
|
BLAKE2b-256 checksum How to use checksums |
524617330dc751e5e21345ec35c3c16e78988473b09e40787e13f79a6ce12973
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logRelease files / fortran_nlp_kernels-0.1.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | fortran_nlp_kernels-0.1.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 12.2 MB |
| Tags | CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
bb64ef037b550bad5606a700d2390c77a881367051741eb8a48963b9cc249174
|
|
BLAKE2b-256 checksum How to use checksums |
da89bbbf48262195e184d08ead33c76a0f099e19ca985a426cc271cfd73d20e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logRelease files / fortran_nlp_kernels-0.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | fortran_nlp_kernels-0.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 12.2 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
f8851bea9abae96380cb34ed603823ebf69baf4cc889fa9f039cb6000cfbe759
|
|
BLAKE2b-256 checksum How to use checksums |
70c700ff4679d90e6940af7565fbd05d6d059d1538ad086db8aeaf88ee80c844
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logRelease files / fortran_nlp_kernels-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | fortran_nlp_kernels-0.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 12.2 MB |
| Tags | CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
a6d82fbce69c47f06b6f2f33b147105439db7ae8b2cf6f8ad65d9e47880de8a4
|
|
BLAKE2b-256 checksum How to use checksums |
f5ccf73ca6dcaf701892ee80d9b2586d12e363f3a35036f8b15c849f90262ce8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logRelease files / fortran_nlp_kernels-0.1.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | fortran_nlp_kernels-0.1.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 12.2 MB |
| Tags | CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
e9e68204a8d2daa8b1a76702564e9b64c69e2533a0282940940066c1a04d4779
|
|
BLAKE2b-256 checksum How to use checksums |
e0b04b67cb0bae3f925327049aacf0447acf822c3fd4e26f055c387943420dda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logSecurity for fortran-nlp-kernels 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 5 files in this release have been uploaded with provenance metadata.
Attestation for fortran_nlp_kernels-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.