Skip to content

Test wrapped external buffers after upstream mlx #4634 - #64

Merged
davidtai merged 1 commit into
mainfrom
test/wrapped-buffer-ownership
Oct 24, 2026
Merged

davidtai merged 1 commit into
mainfrom
test/wrapped-buffer-ownership

Conversation

@davidtai

Copy link
Copy Markdown

What

Tests only. No source change.

Upstream mlx 5dbe9991 (ml-explore/mlx#4634, in the fork since the 2026-10-22 sync) changed buffer ownership:

  • allocator::malloc returns an owning allocator::Data. A buffer wrapped with a caller deleter (MLXArray(rawPointer:), mlx_array_new_data_managed*) is "not owned".
  • is_donatable() is false for a buffer MLX does not own. The release of a wrapped buffer runs its deleter, not the allocator free.

An audit of the fork stack (mlx, mlx-c, mlx-swift, mlx-swift-lm, d-inference) found no errors from this change. But no test in mlx, mlx-c or mlx-swift covered the fork paths that touch wrapped buffers. These tests cover them:

Test What it proves
AllocationFootprintTests.testWrappedExternalBufferIsUniqueUntilShared A wrapped page is not copied, reports isUnique (mlx array::is_uniquely_referenced, 699649d3; mlx-c mlx_array_get_buffer_info) until a view shares it, and its finalizer runs once. mlx-swift-lm PagedKVSegmentBacking requires this for wrapped paged-KV pages (hostZeroedSegments, on by default in d-inference).
AllocationFootprintTests.testOpOnWrappedExternalBufferDoesNotWriteTheCallerPage An op on a wrapped page whose only reference is the graph writes a new allocation. The caller page does not change. Before #4634 this input was donated.
ProcessAllocationCapTests.testFreshProcessSwiftCap (extended) Under the process allocation cap, a wrapped page is charged while MLX holds it and refunded once after release. Charged, live, cached, pending and unreclaimed bytes return to the baseline. Before, only tests/standalone/process_allocation_cap_metal.cpp covered this, and no CI job runs it.

swift-format (the Lint job formats each changed file) also reformats existing lines in AllocationFootprintTests.swift.

Hooked up

  • Tests/MLXTests/*.swift is in the Tests section of fork.yaml.
  • ProcessAllocationCapTests runs in the isolated fresh-process step of Fork Tests (MLX_TEST_PROCESS_CAP=1).
  • AllocationFootprintTests runs in the normal Fork Tests shards.

🤖 Generated with Claude Code

Upstream mlx 5dbe9991 (#4634) marks a buffer wrapped with a caller
deleter as not owned by MLX: is_donatable() is false for it, and its
release runs the deleter, not the allocator free. No test in mlx,
mlx-c or mlx-swift covered the fork paths that touch such buffers.

- AllocationFootprintTests: a wrapped page reports isUnique until a
  view shares it (array::is_uniquely_referenced, mlx 699649d3), and the
  finalizer runs once.
- AllocationFootprintTests: an op on a wrapped page whose only
  reference is the graph writes a new allocation; the caller page does
  not change.
- ProcessAllocationCapTests: under the process allocation cap a wrapped
  page is charged while MLX holds it and refunded once after release;
  live, cached, pending and unreclaimed bytes return to the baseline.

swift-format also reformats the existing lines in AllocationFootprintTests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@davidtai
davidtai marked this pull request as ready for review October 24, 2026 16:30
@davidtai
davidtai merged commit 6959486 into main Oct 24, 2026
13 checks passed
@davidtai
davidtai deleted the test/wrapped-buffer-ownership branch October 24, 2026 17:35
jonathan308 added a commit that referenced this pull request Oct 26, 2026
…ack (#56)

Forward merge of the refreshed #54 (main 6959486 merged into the JACCL fixes
pin) into this branch. This branch already carried main up to the #58 squash
67dc752, so the new main content is the upstream ml-explore/mlx-swift merge
ce46846 (#59, with #60 and #62), the core and C repin with regenerated sources
(#63) and the wrapped external buffer test (#64).

Conflicts:
- Source/Cmlx/mlx and Source/Cmlx/mlx-c (gitlinks): kept this branch's pins,
  core 360d11b9 and mlx-c 8a50797b. They are restitched to the merged core and
  C heads separately.
- fork.yaml, "Submodule pins": one paragraph that states main's pins (mlx
  ff4bb35b, mlx-c 004c735f, merges of upstream main) and then what the
  cluster core and C add on top. Every other section merged without conflict;
  the cluster's startup-wait section is kept.

Merged without conflict and checked: the mirrored fast.h keeps the fenced
kernel and prepared launch declarations next to main's math-mode entry point,
distributed_group.h keeps the bootstrap, startup-wait and sealed-codec
declarations, and MLXArray.sharingArray() still calls copyContext(), which
main keeps. The tree is main plus this branch's own delta: the two gitlinks,
those two headers, MLXArray.swift, MLXFastKernel.swift,
docs/startup-wait-budget.md and fork.yaml.

Main's Transforms+Eval.swift now schedules under evalLock and waits outside
it (upstream ml-explore#474). withSerializedEvaluation still runs its body under
evalLock, so native evaluation, fences and collectives stay serialized against
scheduling; a Swift eval's wait no longer excludes them.

Tests (Ultra A, Xcode 27.1). These ran on this merge with the two follow-up
commits on top (swift-format and fork.yaml). The committed gitlinks are not
changed. For the build, the nested repos were checked out at merged heads:
core 2b5253ad (the core merge into mlx#35; 69c0a0f6 gave the same results)
and a temporary merge of mlx-c main 004c735f into 8a50797b. Its C sources
merge cleanly; CMakeLists.txt and fork.yaml are taken from 8a50797b.
- swift build --build-tests: complete.
- Fork Tests, all 3 shards of the CI plan, with an AOT mlx.metallib from
  that core:
  - XCTest: 852 listed, 848 passed, and the 2 permitted skips.
  - The fresh-process allocation-cap test and both multi-process ring
    suites passed.
  - Swift Testing: 749 of 749 passed.
  - test-shards.py verify: every test ran exactly once.
- check-generated-metal-sources.py passes against that core.
- The mirrored mlx/c headers are byte-identical to the merged C headers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Oct 26, 2026
Brings in main up to 6959486 (2026-10-24): the upstream ml-explore/mlx-swift
merge ce46846 (#59, with #60 and #62), the core and C repin with regenerated
sources (#63) and the wrapped external buffer test (#64).

Conflicts:
- Source/Cmlx/mlx and Source/Cmlx/mlx-c (gitlinks): kept this branch's pins,
  core fdae3174 and mlx-c 489e9654, as the cluster stack has them. Main pins
  core ff4bb35b and mlx-c 004c735f. The cluster pins are restitched to the
  merged core and C heads separately; the Swift layer of this merge needs
  main's C API and does not link against the old pins.

The merged tree is main plus this branch's own delta: the two gitlinks and the
mirrored distributed_group.h copies that declare
mlx_distributed_init_jaccl_with_bootstrap (main did not change those headers).

Tests (Ultra A, Xcode 27.1). The committed gitlinks are not changed. For
the build, the nested repos were checked out at merged heads: core
187b6c67 (the core merge of main f78f0fa9 into mlx#33) and a clean,
temporary merge of mlx-c main 004c735f into 489e9654.
- swift build --build-tests: complete.
- Fork Tests, all 3 shards of the CI plan, with an AOT mlx.metallib from
  that core:
  - XCTest: 852 listed, 848 passed, and the 2 permitted skips.
  - The fresh-process allocation-cap test and both multi-process ring
    suites passed.
  - Swift Testing: 749 of 749 passed.
  - test-shards.py verify: every test ran exactly once.
- check-generated-metal-sources.py passes against that core. At the
  committed core it fails, because main regenerated the quantized sources.
- check_forkdiff.py --upstream-ref upstream/main: 211 of 211 files
  described.
- Lint: this branch changes no Swift file against main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Oct 26, 2026
…rithmetic fix (#61)

Forward merge of the refreshed #56 into this branch. It carries main
6959486 through the refreshed #54, the swift-format fix of
MLXFastKernel.swift and the fork.yaml section for MLXArray.sharingArray().

Conflicts:
- Source/MLXNN/Quantized.swift, QuantizedLinear.constantCachedForward: kept
  this branch's independent-rows path (each FP32 row through the M=1 packed
  kernel) and main's NVFP4 global scale (upstream ml-explore#426), applied after either
  quantizedMM path. Without a global scale the scale step returns its input,
  so this branch's row arithmetic is unchanged.
- fork.yaml, "Submodule pins": the paragraph from the #56 merge, with this
  branch's fence-ordering, shared-event stream wait and caller-stream helper
  additions.

Gitlinks merged without conflict: Source/Cmlx/mlx stays at this branch's
2e16455f (the prompt-sized steel tiles on top of the fence-ordering core) and
Source/Cmlx/mlx-c at 8a50797b. HadamardLayerTests.swift auto-merged this
branch's row tests with main's formatting and its removal of the
setDefaultDevice() test helper; no test in the result calls it. The tree is the
refreshed #56 plus this branch's own delta.

Tests (Ultra A, Xcode 27.1). The committed gitlinks are not changed. For
the build, the nested repos were checked out at merged heads: core
668e46c6 (the core merge into mlx#41 with its steel-tile build fix;
dbe665ac before it gave the same results) and the temporary merge of mlx-c
main 004c735f into 8a50797b.
- swift build --build-tests: complete.
- Fork Tests, all 3 shards of the CI plan, with an AOT mlx.metallib from
  that core:
  - XCTest: 853 listed, 849 passed (including HadamardLayerTests and
    Float16ConstantCastTests), and the 2 permitted skips.
  - The fresh-process allocation-cap test and both multi-process ring
    suites passed.
  - Swift Testing: 749 of 749 passed.
  - test-shards.py verify: every test ran exactly once.
- The allocation-cap test is timing sensitive. Main's wrapped-page check
  (#64) failed in 2 of 34 fresh-process runs on this layer, both in the
  first minutes (23:21-23:23) while other jobs loaded this Mac. Buffers that
  Metal completion handlers released after the test's clearCache or baseline
  snapshot were still counted: 16 KiB cached in one run, 32 KiB live in the
  other. It passed the other 32 runs and 22 of 22 runs on #56. This layer's
  core delta over #56 does not touch that single-stream path.
- check-generated-metal-sources.py passes against that core.
- Lint (swift-format 603.0.0, as the Lint job runs it on the files this
  branch changes against #56): no changes.
- check_forkdiff.py --upstream-ref upstream/main: 213 of 213 files
  described.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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