Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement numerically stable log_softmax() #812

Merged
merged 3 commits into from
Jul 1, 2024

Conversation

taha-yassine
Copy link
Contributor

A numerically stable implementation of log_softmax(). It's similar to the current implementation of softmax() and is what's used in PyTorch and SciPy too.

log_softmax([1000,1])

// Current implementation
// > Array [ 0, -Infinity ]

// Proposed implementation
// > Array [ 0, -999 ]

@xenova
Copy link
Owner

xenova commented Jun 22, 2024

Thanks so much! 🤗 Can you add a unit test for this to tests/maths.test.js? One normal case and one case where the unstable version will break? 🔥

@taha-yassine
Copy link
Contributor Author

Done :)

Copy link
Owner

@xenova xenova left a comment

Choose a reason for hiding this comment

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

Thanks so much! Approved!

As seen in #667, a standard for loop is faster for computing sums, so I've updated that. I tested both and indeed it is faster:
image
(code block 1 = reduce; code block 2 = normal for loop)

It also removes the need for @ts-ignore

src/utils/maths.js Outdated Show resolved Hide resolved
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova
Copy link
Owner

xenova commented Jun 23, 2024

(ignore failing tests; related to something else)

@xenova xenova merged commit 75f557b into xenova:main Jul 1, 2024
1 of 4 checks passed
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.

None yet

3 participants