Skip to main content

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: points in the original order (not copied), indices to put the points in tree order, and split_dims which (if the tree is built with optimized=True) specify the splitting dimension independently for each node. If needed, the memory overhead could potentially be reduced by sorting points in-place.
  • The query_neighbors function 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.ipynb notebook 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

jaxkd-0.0.1.tar.gz (229.4 kB view details)

Uploaded Source

Built Distribution

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

jaxkd-0.0.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

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

Hashes for jaxkd-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b1e84c94ac83554a24e011e2a83125b8c334752d647f68735d532212daaded2a
MD5 7a7d66ba4306ea18e33102708921b80e
BLAKE2b-256 408b472cd6efea62c8dd0c09a05419cdd5a59049e82b6cd926ea97af2b0adaea

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on dodgebc/jaxkd

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

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

Hashes for jaxkd-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f91804bdf49e94a432a78ab6c8383a74565af21c7fa3ddf0c92787f24e73802d
MD5 57892f758ae69e77a55245bade0e52ed
BLAKE2b-256 c3c2b4adc41a978f65ae5356f755a7d96a0a6e7a9674647779ac00d234a53400

See more details on using hashes here.

Provenance

The following attestation bundles were made for jaxkd-0.0.1-py3-none-any.whl:

Publisher: publish.yml on dodgebc/jaxkd

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