Skip to content

Honor reference return policies for JAX CPU arrays - #1468

Open
1akashkalita wants to merge 2 commits into
wjakob:masterfrom
1akashkalita:fix-jax-reference-copy
Open

1akashkalita wants to merge 2 commits into
wjakob:masterfrom
1akashkalita:fix-jax-reference-copy

Conversation

@1akashkalita

Copy link
Copy Markdown

A CPU binding returning nb::ndarray<nb::jax, ...> with rv_policy::reference can return a detached JAX array. For example, passing four floats starting one element past a 64-byte boundary succeeded on JAX 0.12.1, but np.shares_memory(np.asarray(result), input) was false.

Request zero-copy DLPack import for explicit CPU reference policies on JAX versions that accept the keyword. Check that nonempty results share the source buffer because JAX 0.6.2 accepts copy=False and can still allocate an aligned copy. On JAX 0.4.26 the legacy API can share the unaligned buffer directly.

The regression test covers aligned, empty, and unaligned inputs. The unaligned case accepts a shared view or an explicit refusal to copy.

Tested on arm64 macOS with the JAX and ndarray test modules.

  • JAX 0.4.26 passed 58 tests with 15 skipped.
  • JAX 0.6.2 passed 59 tests with 14 skipped.
  • On JAX 0.12.1 the new test failed on master and passes here. The full modules had 55 passes, 14 skips and four existing failures involving read-only JAX inputs. Those four also fail on master and are covered by Update ndarray tests for JAX, which has immutable arrays #1465.

@hpkfft

hpkfft commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

At first glance, it seems to me that you're trying to do something that JAX is not intended to support.
See: Pure functions

@1akashkalita

Copy link
Copy Markdown
Author

The use case is handing a read-only CPU buffer to JAX without allocating a second buffer. Neither the binding nor the test modifies the data after export. The test checks whether the two arrays share storage.

JAX exposes this distinction through from_dlpack(copy=False). On JAX 0.12.1 an aligned buffer works, and an unaligned one raises when copying is explicitly disabled. Today nanobind omits that argument even when the binding specifies rv_policy::reference, so the same unaligned input silently allocates. That allocation is the behavior this PR addresses.

@hpkfft

hpkfft commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Neither the binding nor the test modifies the data after export.

Then why do you care if JAX chooses to make a copy? The C++ code can read from its copy, and JAX can read from its copy, and since both copies are immutable, everybody always reads the same values. Whether JAX does or not make a copy is an optimization decision that can change from one release of JAX to the next, because, philosophically, functions are pure. It can even change based on the CPU. (I'm not saying it does, just that it can. Hypothetically, arm64 could have a bigger performance penalty for unaligned reads than x86_64 has. So, JAX might have different heuristics regarding when to make a copy based on the hardware.) I don't see that it's useful for nanobind to check whether the two arrays share storage and raise an exception if they do not. These exceptions might occur unpredictably. JAX could (theoretically) do something like garbage collection and copy its live arrays to somewhere else when memory usage gets high for the purpose of memory compaction.

Suppose my C++ program has, at top-level, static const float global[] { 1, 2, 3, 4 };. I would return this to JAX using nb::rv_policy::reference (and not specify an owner) so nanobind knows that the array storage remains valid forever. Nanobind will not cause a copy to be made. That's good. From my point of view, as the C++ programmer, I see no reason to cause a copy to be made. If JAX wants to use some special SIMD instructions on some special hardware that can only read from 64B-aligned memory, JAX is welcome to make a copy in order to use those instructions and get better performance. I do NOT want a runtime exception if it does this.

When I write nb::rv_policy::reference, I'm giving JAX a pointer to my data and promising it won't go away. I'm not telling JAX what to do with that data. It can read from it once, or repeatedly, whenever it wants. It can make multiple copies of the data (e.g., one copy for each NUMA domain) and then never read from my data again.

Also, I don't want my bindings to become slower by testing whether the two arrays share storage.

@1akashkalita

Copy link
Copy Markdown
Author

The static-buffer example makes the compatibility concern clear. Under that interpretation of rv_policy::reference, the PR would turn a working binding into an exception on some platforms. It also adds a DLPack round trip to check sharing. I agree those are real costs. The use case I was targeting is a caller that explicitly requires zero-copy at import time. I’ll leave the patch pending maintainer guidance on whether that guarantee belongs in an opt-in API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants