Repository navigation
datadeps: never free the user's own object at region end - #839
Merged
Merged
Conversation
Datadeps frees only slots it allocated itself; user data, including an argument serving as its own slot, is never freed by it. The aliased-object cache stored whatever a slot's copy function returned as a Datadeps copy, and the region-end free loop (flat and hierarchical) `unsafe_free!`s every such entry. Two paths hand back the user's own object instead of a copy: - The in-place slot of `generate_slot!` (`Returns(data)`), when another argument with the same aliasing was registered first in another memory space. `transpose(X)` of a device array `X` does exactly that: it has `X`'s aliasing but defaults to the host space. A host task reading `In(transpose(X))` followed by a device task writing `InOut(X)` freed `X` at region end (reproduced with CUDA: "Attempt to use a freed reference"). - A `move` that is the identity between two memory spaces of one process, which is the default `move` for a user-defined processor. `libc_backed` passes an already Libc-backed `Array` through unchanged, so Datadeps `Libc.free`d such an argument. Record a slot whose payload is the argument's own (`slot_holds_value`) as an original, which the free loops already skip. `y === x` is rank-uniform; payloads are compared only when both are local, and never under uniform execution, as in #834's guard for unkeyed copies. The check runs only when a slot is created by `f`; a cross-worker copy returns after comparing the slot's owner with `myid()`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
Each case makes a slot that is the user's object, in a space other than the one where its aliasing was first registered, and fails on master: - unit: an aliased-object cache whose copy function hands back the argument's payload, and an in-place slot whose key was registered first in another space, must both be recorded as originals; - end to end, CPU: a user-defined processor and memory space relying on the default (identity) `move`, with a Libc-backed argument that Datadeps used to `Libc.free`; - end to end, GPU (every backend in GPU_SCOPES): a host task reading `transpose(X)` and a device task writing `X` in one region used to free `X`. Built-in processors and spaces only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
Record why the aliased-object cache can meet one memory under one key in two spaces of one process (`transpose(X)` of a device array is a host-space argument with `X`'s aliasing), which is what let Datadeps free a user's device array, and the related slot mix-up it causes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
Contributor
Dagger benchmarks:
|
| Backend | Regressions | Improvements | Within noise | Inconclusive time | Suites |
|---|---|---|---|---|---|
| Threads | 1 | 0 | 8 | 1 | 4/4 |
| Distributed | 0 | 0 | 0 | 1 | 4/4 |
| Distributed+Threads | 1 | 0 | 0 | 0 | 4/4 |
| MPI | 0 | 1 | 2 | 0 | 4/4 |
| MPI+Threads | 0 | 3 | 22 | 6 | 4/4 |
| Total | 2 | 4 | 32 | 8 | 20/20 |
Regressions across all backends
| Backend | Benchmark | Metric | Change |
|---|---|---|---|
| Distributed+Threads | array/dagger/N=1024 (block 512)/alloc (rand) |
time | +120.4% |
| Threads | array/dagger/N=256 (block 128)/norm |
time | +82.3% |
Improvements across all backends
| Backend | Benchmark | Metric | Change |
|---|---|---|---|
| MPI | linalg/dagger/N=256 (block 128)/lu |
time | -46.0% |
| MPI+Threads | linalg/dagger/N=1024 (block 512)/solve (A\b via lu) |
time | -45.7% |
| MPI+Threads | linalg/dagger/N=256 (block 128)/syrk (A'*A) |
time | -37.1% |
| MPI+Threads | linalg/dagger/N=1024 (block 512)/lu |
time | -36.1% |
Advisory allocation changes
Paired fixed-placement measurements gate Distributed CPU allocating LU counts and bytes and allocating X + X bytes. Review the advisory changes for costs specific to the allocating path. The original X + X count gate still applies.
| Backend | Benchmark | Metric | Change |
|---|---|---|---|
| Distributed | array/dagger/N=1024 (block 128)/add (X + X) |
memory | +29.5% |
Threads (1 process × 4 threads) — 1 regressions, 0 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 1 | 0 | 5 | 1 |
| linalg | 0 | 0 | 1 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 2 | 0 |
Regressions
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=256 (block 128)/norm |
time | +82.3% |
Within noise
| Benchmark | Metric | Change |
|---|---|---|
stencil/dagger/N=256 (block 128)/assign (const) |
time | +91.8% |
array/dagger/N=256 (block 128)/transpose (permutedims) |
time | +79.6% |
stencil/dagger/N=256 (block 128)/alloc (neighbors Wrap) |
time | +65.4% |
array/dagger/N=1024 (block 512)/add (X + X) |
time | +28.2% |
linalg/dagger/N=256 (block 256)/qr |
time | +27.2% |
array/dagger/N=256 (block 128)/reduce (sum) |
time | -25.1% |
array/dagger/N=256 (block 128)/add (X + X) |
time | -35.9% |
array/dagger/N=256 (block 128)/map (sin.(X)) |
time | -43.5% |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=1024 (block 512)/alloc (rand) |
time | +95.8% |
Distributed (4 processes × 1 thread) — 0 regressions, 0 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 0 | 1 |
| linalg | 0 | 0 | 0 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 0 | 0 |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=256 (block 128)/norm |
time | +39.0% |
Distributed+Threads (2 processes × 2 threads) — 1 regressions, 0 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 1 | 0 | 0 | 0 |
| linalg | 0 | 0 | 0 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 0 | 0 |
Regressions
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=1024 (block 512)/alloc (rand) |
time | +120.4% |
MPI (4 ranks × 1 thread) — 0 regressions, 1 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 0 | 0 |
| linalg | 0 | 1 | 2 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 0 | 0 |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=256 (block 128)/lu |
time | -46.0% |
Within noise
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=1024 (block 512)/qr |
time | +41.2% |
linalg/dagger/N=1024 (block 128)/syrk (A'*A) |
time | +39.2% |
MPI+Threads (2 ranks × 2 threads) — 0 regressions, 3 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 10 | 1 |
| linalg | 0 | 3 | 3 | 1 |
| sparse | 0 | 0 | 0 | 1 |
| stencil | 0 | 0 | 9 | 3 |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=1024 (block 512)/solve (A\b via lu) |
time | -45.7% |
linalg/dagger/N=256 (block 128)/syrk (A'*A) |
time | -37.1% |
linalg/dagger/N=1024 (block 512)/lu |
time | -36.1% |
Within noise
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=256 (block 128)/norm |
time | +130.6% |
array/dagger/N=256 (block 128)/add (X + X) |
time | +123.6% |
array/dagger/N=256 (block 256)/broadcast (X .+ 1) |
time | +83.6% |
stencil/dagger/N=256 (block 128)/neighbors (Pad) |
time | +80.6% |
stencil/dagger/N=256 (block 256)/multi-expr |
time | +65.9% |
array/dagger/N=1024 (block 128)/norm |
time | +61.5% |
stencil/dagger/N=1024 (block 128)/update (+) |
time | +61.2% |
linalg/dagger/N=256 (block 256)/matvec (A*x) |
time | +60.7% |
array/dagger/N=256 (block 128)/reduce (sum) |
time | +55.1% |
stencil/dagger/N=256 (block 256)/alloc (neighbors Wrap) |
time | +50.8% |
array/dagger/N=256 (block 128)/map (sin.(X)) |
time | +50.7% |
linalg/dagger/N=1024 (block 128)/matvec (A*x) |
time | +49.0% |
array/dagger/N=1024 (block 512)/norm |
time | +43.7% |
stencil/dagger/N=1024 (block 512)/update (+) |
time | +39.5% |
array/dagger/N=1024 (block 512)/map (sin.(X)) |
time | +38.8% |
array/dagger/N=256 (block 256)/norm |
time | +38.0% |
array/dagger/N=1024 (block 512)/transpose (permutedims) |
time | -35.9% |
stencil/dagger/N=1024 (block 128)/multi-expr |
time | -36.2% |
linalg/dagger/N=256 (block 256)/cholesky |
time | -37.2% |
stencil/dagger/N=1024 (block 512)/neighbors (Clamp) |
time | -37.9% |
stencil/dagger/N=256 (block 256)/update (+) |
time | -43.8% |
stencil/dagger/N=1024 (block 512)/neighbors (Pad) |
time | -49.2% |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
stencil/dagger/N=1024 (block 512)/assign (const) |
time | +306.2% |
stencil/dagger/N=256 (block 128)/assign (const) |
time | +192.3% |
array/dagger/N=256 (block 128)/transpose (permutedims) |
time | +190.0% |
stencil/dagger/N=256 (block 256)/neighbors (Clamp) |
time | +64.4% |
linalg/dagger/N=1024 (block 128)/lu |
time | +44.7% |
sparse/dagger/N=1024 (block 64)/spgemm (S*S) |
time | +39.0% |
Full results and plots (download the benchmark-results-* artifacts).
Review finding 1 on PR #839 (should-fix): The new docstring's reason for skipping the payload check under MPI is wrong: GPU moves can return their input, and the owning rank does hold both. Requested: as reported -- say the check is skipped under MPI because only the owning rank could compare the payloads, so ranks would disagree, not because MPI moves always copy or no rank holds both values. The docstring claimed no rank holds both payloads and that host/device moves always copy. Both are false: the owning rank holds both, and the CUDA/ROC moves return a device array already on the target device unchanged (ext/CUDAExt.jl:624-627, ext/ROCExt.jl:699-702). The skip itself is unchanged and still required for rank-uniformity; the text now says it leaves identity moves under MPI undetected rather than impossible. Docstring only. Written by an AI agent on jpsamaroo's review feedback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The rule
Datadeps frees only the slots it allocated itself. User data, including an argument that serves as its own slot, is never freed by Datadeps.
The bug
Datadeps keeps a cache of the slots it has made for each piece of memory, keyed by that memory's aliasing, one entry per memory space. At the end of a region it calls
unsafe_free!on every cache entry except the "original", which is the entry for the space where the memory was first seen. Every other entry was assumed to be a copy that Datadeps made.That assumption is wrong in two ways. In both, the cache stores the user's own object as a "copy" and frees it.
One memory, first seen in the wrong space (built-in types only).
transpose(X)has the same aliasing asX, butmemory_spacehas no method for wrappers of device arrays, sotranspose(X)of aCuArraycounts as being in host memory. In a region where a host task readsIn(transpose(X))and then a device task writesInOut(X), the cache first recordsX's memory as a host original. The device task's slot forXisXitself, since it is already on the device, but it was stored as a device-side copy and freed when the region ended:A
movethat returns its input (user-defined processors). The processor docs invite users to define their own processors, and the defaultmovebetween two processors is the identity.libc_backedcopies an ordinaryArray, but it passes through anArraythat is already Libc-backed. So for a user-defined processor with its own memory space, the slot for a Libc-backed argument is the argument itself, and DatadepsLibc.freed it.The fix
When the cache stores a newly made slot,
slot_holds_valuechecks whether the slot is the argument itself (y === x) or holds the argument's own payload. If so, the slot is recorded as an original, and the free loops already skip originals. The flat and hierarchical planners share these loops, so both are covered.y === xgives the same answer on every rank, because both are the same uniformChunk. Payloads are compared only when both values are in this process, and never under uniform execution, where no rank holds both.NoAliasingarguments, with the same name and meaning. It gains one line, the rank-uniformy === xcheck. Whichever PR merges second keeps a single definition.Tests
All of these fail on master and pass with this change.
test/datadeps.jl, "Datadeps never frees the user's own object":move, with a Libc-backed argument. On master it ends upLibc.freed.test/gpu.jl, "Datadeps never frees a device argument" (each backend inGPU_SCOPES): thetranspose(X)reproducer above, using only built-in processors and spaces. It was run against CUDA (RTX 5060): it errors on master with "Attempt to use a freed reference" and passes with the fix.Suites, all on this branch:
--test datadeps(3 workers, Julia 1.12): 1540 pass, 2 broken (the existing ones).--test allocations: 24/24.test/mpi/run.jl 2 2 test/mpi/cpu.jl: 1143 and 1146 pass on the two ranks.CI_USE_CUDA=1 --test gpuon an RTX 5060 (Julia 1.11): 301/301.Related, not fixed here
The same key sharing also gives one argument another argument's slot. On a plain two-worker CPU setup, if one task reads
In(transpose(A))and a later task on another worker asks forInOut(A), the second task receives aTranspose, and its write ends up transposed inA. In the reverse order, the first task receives an untransposedMatrix. Fixing that means changing how wrappers are keyed or moved (for example, givingTranspose/Adjointmove_rewrapparts), which is a separate change.AGENTS.mdlesson 100 records both effects.🤖 Generated with Claude Code
https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr