Skip to content

fix type cast error in item() for bfloat16 - #339

Merged
awni merged 1 commit into
ml-explore:mainfrom
Alex-X-W:mutexuan_fix_bfloat16_item
Jan 2, 2024
Merged

awni merged 1 commit into
ml-explore:mainfrom
Alex-X-W:mutexuan_fix_bfloat16_item

Conversation

@Alex-X-W

@Alex-X-W Alex-X-W commented Jan 2, 2024

Copy link
Copy Markdown
Contributor

Proposed changes

So previously for item() for bfloat16 is buggy... for example:

y = mx.array(1.0).astype(mx.bfloat16)
print(f"{y.item()=}")
# would print out 1.875

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)

Comment on lines +105 to +106
x = mx.array(1, mx.bfloat16)
self.assertEqual(x.item(), 1.0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

@awni
awni merged commit 350095c into ml-explore:main Jan 2, 2024
JayYun98 pushed a commit to JayYun98/mlx that referenced this pull request Jan 7, 2024
zcbenz pushed a commit to Ritabanm/mlx that referenced this pull request Sep 30, 2026
Testing `clip` converts array elements to builtin types and uses exact equaity.
Converting `out` from float32 to 64-bit builtin float incurs floating-point errors,
and further exact equality testing fails. So use `math.isclose` instead.

closes ml-exploregh-339
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.

2 participants