Fix logits_to_logprobs for 2-D and 3-D logits#1002
Merged
abetlen merged 3 commits intoabetlen:mainfrom Dec 16, 2023
kddubey:kddubey/fix-logits-to-logprobs
Merged
Fix logits_to_logprobs for 2-D and 3-D logits#1002abetlen merged 3 commits intoabetlen:mainfrom kddubey:kddubey/fix-logits-to-logprobs
abetlen merged 3 commits intoabetlen:mainfrom
kddubey:kddubey/fix-logits-to-logprobs
Conversation
kddubey
commented
Dec 12, 2023
| test = [ | ||
| "pytest>=7.4.0", | ||
| "httpx>=0.24.1", | ||
| "scipy>=1.10", |
Contributor
Author
There was a problem hiding this comment.
This is the oldest version compatible with numpy>=1.20.0
source: https://docs.scipy.org/doc/scipy/dev/toolchain.html#numpy
Owner
|
@kddubey thank you, yes that's a good idea wrt vectorizing the logits -> logprobs calculation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The implementation in main (from this PR) only works for 1-D logits. It silently fails for 2-D or 3-D logits. The implementation in this PR works out-of-the-box for 1-D, 2-D, and 3-D logits. (3-D is possible in the future w/ batch inference and
logits_all=True.) This feature might be useful b/c there are some places in the code where we can save time by vectorizing / not converting data to lists. I'll do that in a future PR.The minimal and sufficient fix is to set
axis=-1in thenp.maxcall, and setkeepdims=Truein thenp.sumcall. I decided to instead go with a more robust implementation. It's almost copy-pasted fromscipy.special.log_softmax. I decided against addingscipyas a required dependency b/c it's not lightweight—the latest version is ~37 MB.How has this been tested?
Script
Install the new test dependency,
scipy, which contains a correct implementationCheckout main
Run this script in main to verify that the current implementation is silently wrong for 2-D logits
Checkout this branch
Run the same script with
atol=1e-6. No error should be raised.New unit tests