M12: Zero-copy to_binary via resource binary - #22
Merged
Conversation
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).
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.
Summary
to_binarynow returns a BEAM resource binary aliasing MLX Metal storage directly — no memcpy. The binary pins aTensorresource so the MLX buffer survives until the binary is GC'd.from_binaryzero-copy deferred to M12.5 (MLX'sallocator::BufferstoresMTL::Buffer*on Metal; wrapping a BEAM heap pointer is unsound withoutMTL::Device::newBufferWithBytesNoCopy).EMILY_ASAN=1Makefile flag for sanitizer-enabled OTP builds. ASan CI job deferred (macOS SIP blocksDYLD_INSERT_LIBRARIES; requires OTP built with--enable-sanitizers=address).Test plan
mix precommit— 285 tests, 0 failuresmix test --only conformance— 17 conformance tests (Qwen3, ViT, Whisper, DistilBERT)*_fulltags — 311 tests, 0 failures (including training convergence)to_binarycalls🤖 Generated with Claude Code