Fix np bfloat16 misinterpreted as complex#3146
Open
kellen-sun wants to merge 17 commits intoml-explore:mainfrom
Open
Fix np bfloat16 misinterpreted as complex#3146kellen-sun wants to merge 17 commits intoml-explore:mainfrom
kellen-sun wants to merge 17 commits intoml-explore:mainfrom
Conversation
awni
reviewed
Feb 19, 2026
zcbenz
reviewed
Mar 18, 2026
Contributor
Author
|
thanks for the feedback! |
zcbenz
reviewed
Mar 29, 2026
Collaborator
|
I pushed a change to reuse |
Contributor
Author
|
Thanks for the help, this builds and runs fine on my machine (the only tests I had were the ones in Also, I really enjoyed working on the binding and types for this PR. I'm looking for another issue to pick up, but the public ones seem pretty sparse right now. I was wondering if the mlx team had any feature buildouts targeted (maybe internally) that are open to community contribution? Perhaps I could work on something there? |
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.
Proposed changes
Fixes #1075
Bug: The bug happens when converting
np.array(1., dtype=ml_dtypes.bfloat16)andnp.array([1.], dtype=ml_dtypes.bfloat16)tomx.array(x). For the former case, it'll silently be caught as astd::complexas part ofArrayInitTypeand get converted as such (see related issue for code). For the latter, it'll be interpreted as anArrayLike, not be able to make the conversion tomx.array()and raise aValueError.The Fix: We need to catch this case before it gets filtered by
ArrayInitType. I made thearray.__init__more generic to catch this and checked the dtype to match bfloat16, then manually construct the array. Otherwise, we fallback to the originalArrayInitTypecase.Note: bfloat16, is the only current ml_dtype that mlx supports.
Verification: Verified locally (macOS 26.2, MLX 0.30.7, Apple M2) with the additional test case I provided. If this is run from main, it raises the bug mentioned above:
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes