Repository navigation
Interop: copy and device semantics, explicit dtypes and a Julia adapter - #127
Merged
Merged
Conversation
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.
Copy and device semantics
copy=None | True | Falseon every interop conversion (as in the array API):Falseraises when the conversion cannot share memory,Truealways copies.__dlpack_device__(Apple's unified memory counts as host).from_frameworkcopies between host and device explicitly instead of falling back toasarraysilently; a framework that cannot take an array's device raises aValueErrornaming the device.assert_no_transferschecks zero copy), host↔device recordsto_host/to_device, a copy within one memorydevice_copy.from_jaxraises a clearTypeErrorfor traced values.Explicit dtypes
jax_enable_x64, PyTorch on MPS and MLX for the GPU raise aTypeErrorwith advice. MLX keeps float64 fordevice=mlx.core.cpu.supports_float64(device)in the JAX, PyTorch and MLX adapters,interop.jax.enable_x64().to_mlx(device=...), which passeddevicetomlx.core.array(it takes none).Julia adapter (host only)
cunumpy.interop.julia(extracunumpy[julia], juliacall):to_julia/from_juliabetween NumPy arrays and JuliaArrays sharing memory, kept alive across both GCs. C order arrives with reversed dimensions (a component-major(3, n)array is a Julian×3matrix), F order with the same;from_julia(order="C" | "F"). Strided or read-only arrays are copied; non-ArrayJulia arrays are collected.Tests
Shared rules tested with fake device arrays; the framework and Julia tests run with the real JAX, PyTorch, MLX and juliacall and are skipped without them (CI installs none of them yet, except MLX on macOS). Locally: 810 passed, interop coverage 98%.