Minimal JAX implementation of k-nearest neighbors using a k-d tree.
Project description
Minimal JAX implementation of k-nearest neighbors using a k-d tree!
This is essentially just a translation of two GPU-friendly tree algorithms [1, 2] into XLA primitives. It is convenient and lightweight, but the original CUDA implementation may be a better choice depending on the application.
The build_tree and query_neighbors operations are compatible with JIT and automatic differentiation. They are reasonably fast when vectorized on GPU, but will be much slower than scipy.spatial.KDTree on CPU. The main advantage is to avoid the complexity of using non-JAX libraries and potentially leaving JIT and the GPU when a scalable nearest neighbor search is needed as part of a larger JAX program.
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)
neighbors, distances = jk.query_neighbors(tree, queries, 10)
Notes:
- The tree structure is stored with a tuple of arrays:
pointsin the original order (not copied),indicesto put the points in tree order, andsplit_dimswhich (if the tree is built withoptimized=True) specify the splitting dimension independently for each node. If needed, the memory overhead could potentially be reduced by sortingpointsin-place. - The
query_neighborsfunction is intended for relatively small values of k and does not use a max heap for simplicity. If k is large enough that will become worthwhile. - The
demo.ipynbnotebook in the source repository has some additional examples, including gradient-based optimization using neighbors.
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 jaxkd-0.0.1.tar.gz.
File metadata
- Download URL: jaxkd-0.0.1.tar.gz
- Upload date:
- Size: 229.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1e84c94ac83554a24e011e2a83125b8c334752d647f68735d532212daaded2a
|
|
| MD5 |
7a7d66ba4306ea18e33102708921b80e
|
|
| BLAKE2b-256 |
408b472cd6efea62c8dd0c09a05419cdd5a59049e82b6cd926ea97af2b0adaea
|
Provenance
The following attestation bundles were made for jaxkd-0.0.1.tar.gz:
Publisher:
publish.yml on dodgebc/jaxkd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jaxkd-0.0.1.tar.gz -
Subject digest:
b1e84c94ac83554a24e011e2a83125b8c334752d647f68735d532212daaded2a - Sigstore transparency entry: 214607741
- Sigstore integration time:
-
Permalink:
dodgebc/jaxkd@5a8ec0340054d1f7f9e6d9d48c2cf0921ea76d48 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/dodgebc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5a8ec0340054d1f7f9e6d9d48c2cf0921ea76d48 -
Trigger Event:
push
-
Statement type:
File details
Details for the file jaxkd-0.0.1-py3-none-any.whl.
File metadata
- Download URL: jaxkd-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f91804bdf49e94a432a78ab6c8383a74565af21c7fa3ddf0c92787f24e73802d
|
|
| MD5 |
57892f758ae69e77a55245bade0e52ed
|
|
| BLAKE2b-256 |
c3c2b4adc41a978f65ae5356f755a7d96a0a6e7a9674647779ac00d234a53400
|
Provenance
The following attestation bundles were made for jaxkd-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on dodgebc/jaxkd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jaxkd-0.0.1-py3-none-any.whl -
Subject digest:
f91804bdf49e94a432a78ab6c8383a74565af21c7fa3ddf0c92787f24e73802d - Sigstore transparency entry: 214607744
- Sigstore integration time:
-
Permalink:
dodgebc/jaxkd@5a8ec0340054d1f7f9e6d9d48c2cf0921ea76d48 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/dodgebc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5a8ec0340054d1f7f9e6d9d48c2cf0921ea76d48 -
Trigger Event:
push
-
Statement type: