JAX k-D
Find k-nearest neighbors using a k-d tree in JAX!
This is an implementation of two GPU-friendly tree algorithms [1, 2] using only JAX primitives. The core build_tree, query_neighbors, and count_neighbors operations are compatible with JIT and automatic differentiation. They are reasonably fast when vectorized on GPU/TPU, but will be slower than SciPy's KDTree on CPU. For small problems where a pairwise distance matrix fits in memory, check whether brute force is faster (see jaxkd.extras).
If query speed is the performance bottleneck and you only use Nvidia GPUs, the jaxkd-cuda extension can be installed as an optional dependency (see below) to enable more efficient tree operations, particularly traversal. The intention is to match the behavior of the pure-JAX version and integrate seamlessly with the cuda=True argument. Building the extension will require CMake and NVCC installed on your system. There may be some rough edges and the internal workings may change.
For even more power, flexibility, and speed, consider binding the original cudaKDTree library to JAX. Functionality will be different as described in the jaxkd-cuda repository, where example bindings can also be found and modified to your needs. Be warned that these will not spark joy. The advantage of the pure-JAX version is that it is portable and easy to use, with the ability to scale up to larger problems without the complexity of integrating non-JAX libraries. Try it out!
Usage
import jax
import jaxkd as jk
kp, kq = jax.random.split(jax.random.key(83))
points = jax.random.normal(kp, shape=(100_000, 3))
queries = jax.random.normal(kq, shape=(10_000, 3))
tree = jk.build_tree(points)
counts = jk.count_neighbors(tree, queries, r=0.1)
neighbors, distances = jk.query_neighbors(tree, queries, k=10)
There is also a one-step build_and_query for convenience, and all these functions accept cuda=True to use the CUDA extension if it is installed.
Additional helpful functionality can be found in jaxkd.extras.
query_neighbors_pairwiseandcount_neighbors_pairwisefor brute-force neighbor searchesk_meansfor clustering using k-means++ initialization, thanks to @NeilGirdhar for contributions
Suggestions and contributions for other extras are always welcome!
Installation
To install, use pip. The only dependency is jax.
python -m pip install jaxkd
Or with the CUDA extension.
python -m pip install jaxkd[cuda]
Or just grab tree.py.
Metadata
Release files for jaxkd 0.1.2
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 |
|---|---|---|---|---|
| jaxkd-0.1.2.tar.gz | 9.4 kB | With provenance | Details for jaxkd-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jaxkd-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for jaxkd-0.1.2-py3-none-any.whl |
Total release size: 20.8 kB
Release files / jaxkd-0.1.2.tar.gz
| Download URL | jaxkd-0.1.2.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
842e7837450feec39fa03bb6f5369fa4123ec526339dc359bfba7157884b77ec
|
|
BLAKE2b-256 checksum How to use checksums |
fc6b40de65a682f0212ff13facf0c2a9b1a07e1b0f719906c11b39a9855275b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Jul 15, 2025.
Transparency logRelease files / jaxkd-0.1.2-py3-none-any.whl
| Download URL | jaxkd-0.1.2-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
63560da791809a6c2cc7f3bf7e9d1509f2d880ba1bb7e6695727e6a41032bd98
|
|
BLAKE2b-256 checksum How to use checksums |
283153a56d0bb104b6726f6d6858304fe7f4fb63fb120112f446ebecae5168c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Jul 15, 2025.
Transparency logSecurity for jaxkd 0.1.2
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 2 files in this release have been uploaded with provenance metadata.
Attestation for jaxkd-0.1.2.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 Jul 15, 2025.
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.