Skip to content

Fix np bfloat16 misinterpreted as complex#3146

Open
kellen-sun wants to merge 17 commits intoml-explore:mainfrom
kellen-sun:fix-bfloat16-misinterpreted
Open

Fix np bfloat16 misinterpreted as complex#3146
kellen-sun wants to merge 17 commits intoml-explore:mainfrom
kellen-sun:fix-bfloat16-misinterpreted

Conversation

@kellen-sun
Copy link
Copy Markdown
Contributor

@kellen-sun kellen-sun commented Feb 19, 2026

Proposed changes

Fixes #1075
Bug: The bug happens when converting np.array(1., dtype=ml_dtypes.bfloat16) and np.array([1.], dtype=ml_dtypes.bfloat16) to mx.array(x). For the former case, it'll silently be caught as a std::complex as part of ArrayInitType and get converted as such (see related issue for code). For the latter, it'll be interpreted as an ArrayLike, not be able to make the conversion to mx.array() and raise a ValueError.
The Fix: We need to catch this case before it gets filtered by ArrayInitType. I made the array.__init__ more generic to catch this and checked the dtype to match bfloat16, then manually construct the array. Otherwise, we fallback to the original ArrayInitType case.
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:

    @unittest.skipIf(not has_ml_dtypes, "requires ml_dtypes")
    def test_conversion_ml_dtypes(self):
        x_scalar = np.array(1.5, dtype=ml_dtypes.bfloat16)
        a_scalar = mx.array(x_scalar)
>       self.assertEqual(a_scalar.dtype, mx.bfloat16)
E       AssertionError: mlx.core.complex64 != mlx.core.bfloat16

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@kellen-sun kellen-sun requested a review from awni February 21, 2026 02:07
@kellen-sun
Copy link
Copy Markdown
Contributor Author

thanks for the feedback!

@kellen-sun kellen-sun requested a review from zcbenz March 29, 2026 19:30
@zcbenz
Copy link
Copy Markdown
Collaborator

zcbenz commented Mar 30, 2026

I pushed a change to reuse nd_array_to_mlx for conversion, can you check if everything still works correctly? (I will fix the windows CI later)

@kellen-sun
Copy link
Copy Markdown
Contributor Author

Thanks for the help, this builds and runs fine on my machine (the only tests I had were the ones in test_bf16.py).

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?

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.

[BUG] np.ndarray of bfloat16 using ml_dtypes is being interpreted as complex64

3 participants