Skip to content

Optimize erf function with expm1f in Metal backend - #3025

Merged
awni merged 1 commit into
ml-explore:mainfrom
bjornefisk:optimize-metal-erf-expm1f
Jan 20, 2026
Merged

Optimize erf function with expm1f in Metal backend#3025
awni merged 1 commit into
ml-explore:mainfrom
bjornefisk:optimize-metal-erf-expm1f

Conversation

@bjornefisk

@bjornefisk bjornefisk commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Replaces 1.0f - metal::exp(r) with -expm1f(r) in the Metal [erf] kernel for better numerical accuracy. This addresses the TODO comment on line 26 of [mlx/backend/metal/kernels/erf.h].
Both expressions are mathematically equivalent, but [expm1f] provides superior precision for values near zero.

Please include a description of the problem or feature this PR is addressing. If there is a corresponding issue, include the issue #.

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)

@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.

LGTM, thanks!

@awni
awni merged commit 634b148 into ml-explore:main Jan 20, 2026
15 checks passed
@bjornefisk
bjornefisk deleted the optimize-metal-erf-expm1f branch January 21, 2026 16:59
jzdziarski pushed a commit to jzdziarski/mlx that referenced this pull request Jul 26, 2026
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