Skip to content

Precise sigmoid#2659

Merged
awni merged 2 commits intomainfrom
precise_sigmoid
Oct 10, 2025
Merged

Precise sigmoid#2659
awni merged 2 commits intomainfrom
precise_sigmoid

Conversation

@awni
Copy link
Member

@awni awni commented Oct 9, 2025

This changes the sigmoid computation to match PyTorch:

import mlx.core as mx
import torch
print(mx.sigmoid(mx.array(-8.0).astype(mx.float16)).item())
print(torch.sigmoid(torch.tensor(-8.0).to(torch.float16)).item())

It's not obviously more accurate. Here is a relative error plot for the old and the new (compared to double). You can see it's a bit worse close to zero but a quite a bit better from > 1 or so.

Screenshot 2025-10-09 at 12 37 54 PM Screenshot 2025-10-09 at 12 36 57 PM

@awni awni requested a review from angeloskath October 9, 2025 20:06
Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

👍

@awni awni merged commit 630350a into main Oct 10, 2025
6 checks passed
@awni awni deleted the precise_sigmoid branch October 10, 2025 17:05
faisalmemon pushed a commit to faisalmemon/mlx that referenced this pull request Oct 30, 2025
* bump patch

* Sigmoid matches PyTorch and is more precise on tails
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