entangle-jax
A generic JAX primitive that is opaque to XLA reordering optimizations.
entangle(payload, *witnesses) returns payload unchanged, but ordered under
jit after every witness's producer. Useful for sequencing native
side-effecting calls that XLA would otherwise be free to reorder, since they
share no ordinary data dependency.
Install with uv add entangle-jax or pip install entangle-jax.
Why
When interacting with external native libraries, it is sometimes necessary to interact with objects they manage through opaque pointers. This conflicts with the XLA memory model where everything is a static-sized buffer that can be arbitrarily copied or reused when needed. One way to achieve this interaction is by creating a token that XLA can pass around that references this object, but XLA is not aware that e.g. multiple copies of this token in fact reference the same memory, and so will perform optimizations that are at worst unsafe and at best inefficient.
Unfortunately, there is no way to tell XLA that a buffer will be modified in-place and that it should maintain consistent order of operations involving this buffer. Marking a primitive as side-effecting will stop XLA from removing it as dead code, but it will still assume that the input and output buffers are distinct, independent objects. At most, it is possible to create an in-place modifying primitive by using input-output aliasing, in which case the input and output buffers are the same, but then XLA will simply copy the input buffer if it is used by another function to ensure both calls are safe.
To work around this, we can create an artificial data dependency with
entangle. The contents of a custom call are not visible to the XLA optimizer, so
it has to treat the output payload as a new distinct variable from the input
payload that depends on all witness values. A standard-library alternative
would be jax.lax.optimization_barrier, but this is currently
unreliable on CPU without a non-default XLA flag.
The actual runtime behavior of the primitive is a no-op: it uses aliasing to tell XLA that the input and output payload should be the same buffer, so it does literally nothing. This also means it is supported on every platform. The usual pattern is to overwrite the payload variable so that its buffer can always be donated, but if it cannot be (for example because it is owned by a caller outside of the JIT context) XLA will insert a copy of the payload buffer.
Example
import jax.numpy as jnp
token1 = jnp.asarray(0, jnp.int32)
x = some_native_read(token1)
token2 = modify_token(token1)
y = some_native_read(token2)
Here token* is a value that has some library object associated with it, and modify_token changes something about this backing object. Even if modify_token is marked as side-effecting, x = some_native_read(token1) and token2 = modify_token(token1) read the same token variable. This means XLA could choose to reorder them, which would lead to the following order:
# Equivalent reordering:
token1 = jnp.asarray(0, jnp.int32)
token2 = modify_token(token1)
x = some_native_read(token1)
y = some_native_read(token2)
In this case, both x and y read the same value. To enforce ordering, we can entangle x and the token used in its computation, before modifying it.
from entangle_jax import entangle
token1 = jnp.asarray(0, jnp.int32)
x = some_native_read(token1)
token2 = entangle(token1, x)
token3 = modify_token(token2)
y = some_native_read(token3)
This means that modify_token cannot be reordered to be before the line that determines x, since it uses a value that depends on x.
# Invalid reordering:
token1 = jnp.asarray(0, jnp.int32)
token2 = entangle(token1, x) # x doesn't exist yet!
token3 = modify_token(token2)
x = some_native_read(token1)
y = some_native_read(token3)
Development
uv sync
uv run pytest
uv run ruff check .
uv run ruff format --check .
uv run ty check
The compiled extension rebuilds automatically on import while developing, so the
default editable install (uv sync) is what you want day to day.
Metadata
Release files for entangle-jax 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 | |
|---|---|---|---|
| entangle_jax-0.1.1.tar.gz | 59.6 kB | Details |
Built distributions (wheels)
Total release size: 1.1 MB
Release files / entangle_jax-0.1.1.tar.gz
| Download URL | entangle_jax-0.1.1.tar.gz |
|---|---|
| Size | 59.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
abda134ca5d412dad34e8ee9e6ace30bfbccc5a4fc4b44531bd55537bf852c17
|
|
BLAKE2b-256 checksum How to use checksums |
5aeca15e770a7e92876dd8f4c6fd876eec9ffd829107f34a395d44e7baab431a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp313-cp313-win_amd64.whl
| Download URL | entangle_jax-0.1.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 227.5 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
32f520b561f100ea3aa02a46de17d608232bbb1b93cc65f56056ef7186de2698
|
|
BLAKE2b-256 checksum How to use checksums |
63cd293efe42cd437376f6d49b109b3913030460c3fefff128bf7131e6b4e3e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | entangle_jax-0.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 64.9 kB |
| Tags | CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
c5ba4d58ee0c02c945f6ae03f98703d65088692fff88fdd85c644b92207b5afa
|
|
BLAKE2b-256 checksum How to use checksums |
12841fadefb54fa4b335b480418bc1b1ba84d173bfcca1ac0e975a45338966c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | entangle_jax-0.1.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 42.8 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
aa1b3759813eb7244b9bd48239adc8b3b5dbfbf56e593ee848d23f2a01fb5bfe
|
|
BLAKE2b-256 checksum How to use checksums |
517daa489f2e29eef4db048620ffdaa54b0bd5f93edbc35db43929e5819587a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp312-cp312-win_amd64.whl
| Download URL | entangle_jax-0.1.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 228.3 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
9437e36982b9331fd81b0a255ddc468d9f40a373c01249fac8e6114bf5d2e5cd
|
|
BLAKE2b-256 checksum How to use checksums |
ae8915d898aa39494eb389d9a85dd27a1b59b968ab5cf6899d197663e675ab49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | entangle_jax-0.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 65.5 kB |
| Tags | CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
9f62edefd93387c33008cbcb4996ab2d22581f5a14255505b8ec84bdc2b805cc
|
|
BLAKE2b-256 checksum How to use checksums |
240abf2bd02e6a238849f077faa4f9bdcb09f3354a1c9c82aaaf8295e4f6fe9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | entangle_jax-0.1.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 43.5 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5e6556ed66ccd4d83fc284f27bc06901073c032debe015c29b39948cf852b6f9
|
|
BLAKE2b-256 checksum How to use checksums |
97fa8a29f11f157d29b4f0d470f10cdf66e4afb90d63f10dedad518a385279d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp311-cp311-win_amd64.whl
| Download URL | entangle_jax-0.1.1-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 228.1 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
32761ff64f98c9d64fffc0fc546027929bf3aab20e90060f44d930106d193964
|
|
BLAKE2b-256 checksum How to use checksums |
82c1a069000021691357ad45f20d56f0e713f089999189f22fa9e1d8a70ff66a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | entangle_jax-0.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 64.8 kB |
| Tags | CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
a5273788746aa2ae759521c526f62ed70f3b5443e9128405f43daf752746b307
|
|
BLAKE2b-256 checksum How to use checksums |
de6371ffee02ac9a1b8d30d14c5144d535dc71faa67483726caf89c69e142f69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency logRelease files / entangle_jax-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | entangle_jax-0.1.1-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 43.2 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
a0741fa2a9977ddd895f0d1f27b9ef554a117ae6f856f929bc5e043a0272a1c4
|
|
BLAKE2b-256 checksum How to use checksums |
68f8b1f5921117c4ac71d4ef5947f5302cd31687d3cdfb01b0238def62718753
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 27, 2026.
Transparency log