Skip to content

datadeps: never free the user's own object at region end - #839

Merged
jpsamaroo merged 5 commits into
masterfrom
jps/datadeps-never-free-user-data
Nov 29, 2026
Merged

jpsamaroo merged 5 commits into
masterfrom
jps/datadeps-never-free-user-data

Conversation

@jpsamaroo

Copy link
Copy Markdown
Member

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.

  1. One memory, first seen in the wrong space (built-in types only). transpose(X) has the same aliasing as X, but memory_space has no method for wrappers of device arrays, so transpose(X) of a CuArray counts as being in host memory. In a region where a host task reads In(transpose(X)) and then a device task writes InOut(X), the cache first records X's memory as a host original. The device task's slot for X is X itself, since it is already on the device, but it was stored as a device-side copy and freed when the region ended:

    using CUDA, Dagger, LinearAlgebra
    x = CUDA.ones(Float32, 4, 4)
    cpu = Dagger.scope(worker=1, thread=1)
    gpu = Dagger.scope(worker=1, cuda_gpu=1)
    Dagger.with_options(scope=Dagger.UnionScope(cpu, gpu)) do
        Dagger.spawn_datadeps() do
            Dagger.@spawn scope=cpu size(In(transpose(x)))
            Dagger.@spawn scope=gpu (y->(y .+= 1; nothing))(InOut(x))
        end
    end
    x.data.freed  # true on master
    Array(x)      # ArgumentError: Attempt to use a freed reference.
  2. A move that returns its input (user-defined processors). The processor docs invite users to define their own processors, and the default move between two processors is the identity. libc_backed copies an ordinary Array, but it passes through an Array that 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 Datadeps Libc.freed it.

The fix

When the cache stores a newly made slot, slot_holds_value checks 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.

  • It is cheap. It runs only when a slot is made, never when one is reused. A slot made on another worker fails the first ownership check, so it is never unwrapped.
  • It is rank-uniform under MPI. y === x gives the same answer on every rank, because both are the same uniform Chunk. Payloads are compared only when both values are in this process, and never under uniform execution, where no rank holds both.
  • It is the same guard that Datadeps: follow-ups to #792 (NoAliasing slots) #834 adds for the unkeyed copies of NoAliasing arguments, with the same name and meaning. It gains one line, the rank-uniform y === x check. 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":
    • unit: a copy function that hands back the argument's payload, and an in-place slot whose key was registered first in another space, are both recorded as originals. A real copy is still freeable.
    • end to end, CPU: a user-defined processor and memory space that use the default identity move, with a Libc-backed argument. On master it ends up Libc.freed.
  • test/gpu.jl, "Datadeps never frees a device argument" (each backend in GPU_SCOPES): the transpose(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.
  • MPI CPU, test/mpi/run.jl 2 2 test/mpi/cpu.jl: 1143 and 1146 pass on the two ranks.
  • CI_USE_CUDA=1 --test gpu on 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 for InOut(A), the second task receives a Transpose, and its write ends up transposed in A. In the reverse order, the first task receives an untransposed Matrix. Fixing that means changing how wrappers are keyed or moved (for example, giving Transpose/Adjoint move_rewrap parts), which is a separate change. AGENTS.md lesson 100 records both effects.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TRNkMLrH1mZo3YmkaHP8Jr

jpsamaroo and others added 3 commits November 28, 2026 16:40
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
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Dagger benchmarks: dirty vs master

Counts are benchmark metrics (time, allocations, bytes). Timing changes require non-overlapping median ± IQR bands; both revisions need at least five timed samples, and timing regressions need an independent confirmation run.

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
@jpsamaroo
jpsamaroo merged commit 79e3a8f into master Nov 29, 2026
51 of 59 checks passed
@jpsamaroo
jpsamaroo deleted the jps/datadeps-never-free-user-data branch November 29, 2026 20:03
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.

1 participant