Skip to content

M12: Zero-copy to_binary via resource binary - #22

Merged
ausimian merged 1 commit into
mainfrom
m12-zero-copy-binary
Apr 16, 2026
Merged

M12: Zero-copy to_binary via resource binary#22
ausimian merged 1 commit into
mainfrom
m12-zero-copy-binary

Conversation

@ausimian

Copy link
Copy Markdown
Owner

Summary

  • to_binary now returns a BEAM resource binary aliasing MLX Metal storage directly — no memcpy. The binary pins a Tensor resource so the MLX buffer survives until the binary is GC'd.
  • from_binary zero-copy deferred to M12.5 (MLX's allocator::Buffer stores MTL::Buffer* on Metal; wrapping a BEAM heap pointer is unsound without MTL::Device::newBufferWithBytesNoCopy).
  • EMILY_ASAN=1 Makefile flag for sanitizer-enabled OTP builds. ASan CI job deferred (macOS SIP blocks DYLD_INSERT_LIBRARIES; requires OTP built with --enable-sanitizers=address).
  • Existing memory soak updated with periodic GC — resource binaries have tiny ProcBins (~64 B) so BEAM's binary-vheap heuristics don't account for the aliased external data.

Test plan

  • mix precommit — 285 tests, 0 failures
  • mix test --only conformance — 17 conformance tests (Qwen3, ViT, Whisper, DistilBERT)
  • All *_full tags — 311 tests, 0 failures (including training convergence)
  • New lifecycle test: aliased binary survives after source tensor GC'd
  • New zero-copy soak: MLX active memory and BEAM binary heap stay flat across 200 to_binary calls
  • Existing memory soak passes with periodic GC

🤖 Generated with Claude Code

to_binary now returns a BEAM resource binary aliasing the MLX Metal
buffer directly, eliminating the memcpy that previously copied every
tensor's bytes into a BEAM-heap binary. The resource binary pins a
fresh Tensor resource so the MLX buffer survives until the binary is
GC'd.

from_binary zero-copy is deferred to M12.5 — MLX's allocator::Buffer
stores an MTL::Buffer* on Metal, so wrapping a BEAM heap pointer is
unsound without MTL::Device::newBufferWithBytesNoCopy.

Also adds EMILY_ASAN=1 Makefile flag (requires sanitizer-enabled OTP)
and a deferred ASan CI note (macOS SIP blocks DYLD_INSERT_LIBRARIES).
@ausimian
ausimian merged commit 1d4f73a into main Apr 16, 2026
1 check passed
@ausimian
ausimian deleted the m12-zero-copy-binary branch April 16, 2026 05:09
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