Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 13, 2025

Implements tensor adapter for MLX arrays following the established TorchTensor pattern.

Implementation

  • MlxArray class: Wraps MLX arrays to implement TensorProtocol

    • numpy(): Converts via np.from_dlpack() for efficient zero-copy transfer
    • tobytes(), tofile(): Standard serialization methods
    • __array__(): NumPy protocol support
    • Special handling for bfloat16 dtype
  • Dtype conversion: from_mlx_dtype() / to_mlx_dtype()

    • Supports 13 dtypes: bool_, uint8-64, int8-64, float16, float32, bfloat16, complex64
    • Lazy imports to avoid hard MLX dependency
  • Test infrastructure: Enhanced skip_if_no() to handle import failures and platform checks

    • Tests automatically skip on Windows where MLX is not supported
    • Improved error handling for missing parent modules
  • Platform-specific installation: MLX only installed on non-Windows platforms via mlx; sys_platform != "win32" in requirements-dev.txt

Usage

import mlx.core as mx
import onnx_ir as ir

mlx_array = mx.array([1, 2, 3], dtype=mx.float32)
ir_tensor = ir.tensor_adapters.MlxArray(mlx_array)

# Use in graph as any other tensor
attr = ir.AttrTensor("x", ir_tensor)
proto = ir.serde.serialize_tensor(ir_tensor)

66 new tests covering all dtypes and operations. All existing tests pass. Tests gracefully skip on platforms without MLX support.

Original prompt

This section details on the original issue you should resolve

<issue_title>Create tensor adaptor for mlx</issue_title>
<issue_description>Create a tensor adapter for mlx arrays in src/onnx_ir/tensor_adapters.py. Implement all the methods and follow existing examples. Create proper tests.</issue_description>

<agent_instructions>You can find mlx documentation at https://ml-explore.github.io/mlx/build/html/usage/quick_start.html</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits December 13, 2025 18:39
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot AI changed the title [WIP] Create tensor adapter for mlx arrays Add MLX tensor adapter with dtype conversion and comprehensive tests Dec 13, 2025
Copilot AI requested a review from justinchuby December 13, 2025 18:47
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Dec 13, 2025

❌ 4 Tests Failed:

Tests completed Failed Passed Skipped
5921 4 5917 0
View the top 3 failed test(s) by shortest run time
src.onnx_ir.tensor_adapters_test.MlxArrayTest::test_numpy_returns_correct_dtype_11_mlx_core_bfloat16
Stack Traces | 0.001s run time
..../test/lib/python3.10.../site-packages/parameterized/parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
src/onnx_ir/tensor_adapters_test.py:255: in test_numpy_returns_correct_dtype
    self.assertEqual(tensor.numpy().dtype, np_dtype)
src/onnx_ir/tensor_adapters.py:303: in numpy
    return np.asarray(self.raw)
E   RuntimeError: Item size 2 for PEP 3118 buffer format string B does not match the dtype B item size 1.
src.onnx_ir.tensor_adapters_test.MlxArrayTest::test_tobytes_11_mlx_core_bfloat16
Stack Traces | 0.001s run time
..../test/lib/python3.10.../site-packages/parameterized/parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
src/onnx_ir/tensor_adapters_test.py:284: in test_tobytes
    self.assertEqual(tensor.tobytes(), tensor.numpy().tobytes())
src/onnx_ir/tensor_adapters.py:313: in tobytes
    return self.numpy().tobytes()
src/onnx_ir/tensor_adapters.py:303: in numpy
    return np.asarray(self.raw)
E   RuntimeError: Item size 2 for PEP 3118 buffer format string B does not match the dtype B item size 1.
src.onnx_ir.tensor_adapters_test.MlxArrayTest::test_tofile_11_mlx_core_bfloat16
Stack Traces | 0.001s run time
..../test/lib/python3.10.../site-packages/parameterized/parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
src/onnx_ir/tensor_adapters_test.py:329: in test_tofile
    tensor.tofile(temp_file)
src/onnx_ir/tensor_adapters.py:317: in tofile
    return self.numpy().tofile(file)
src/onnx_ir/tensor_adapters.py:303: in numpy
    return np.asarray(self.raw)
E   RuntimeError: Item size 2 for PEP 3118 buffer format string B does not match the dtype B item size 1.
src.onnx_ir.tensor_adapters_test.MlxArrayTest::test_tofile_method_exists_and_works
Stack Traces | 0.001s run time
src/onnx_ir/tensor_adapters_test.py:295: in test_tofile_method_exists_and_works
    tensor.tofile(buffer)
src/onnx_ir/tensor_adapters.py:317: in tofile
    return self.numpy().tofile(file)
E   io.UnsupportedOperation: fileno

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@justinchuby justinchuby marked this pull request as ready for review December 13, 2025 18:53
@justinchuby justinchuby requested review from a team and titaiwangms as code owners December 13, 2025 18:53
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@justinchuby
Copy link
Member

@copilot install mlx only when platform is not Windows. Skip the tests on Windows because mlx is not available.

- Add mlx to requirements-dev.txt with sys_platform != "win32" condition
- Update skip_if_no decorator to skip MLX tests on Windows
- Improve error handling in skip_if_no for missing parent modules

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 13, 2025

@copilot install mlx only when platform is not Windows. Skip the tests on Windows because mlx is not available.

MLX is now installed only on non-Windows platforms via mlx; sys_platform != "win32" in requirements-dev.txt. The skip_if_no decorator checks for Windows and skips MLX tests with message "mlx is not available on Windows". Also improved error handling for missing parent modules. (commit 94e1ef0)

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.

Create tensor adaptor for mlx

2 participants