Fix logits_to_logprobs for 2-D and 3-D logits #1002
Merged
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=-1
in thenp.max
call, and setkeepdims=True
in thenp.sum
call. I decided to instead go with a more robust implementation. It's almost copy-pasted fromscipy.special.log_softmax
. I decided against addingscipy
as 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