Fix logsumexp fp16 overflow on ANE via stable max-shift decomposition#2726
Open
Ashutosh0x wants to merge 1 commit into
Open
Fix logsumexp fp16 overflow on ANE via stable max-shift decomposition#2726Ashutosh0x wants to merge 1 commit into
Ashutosh0x wants to merge 1 commit into
Conversation
…apple#2690) The native reduce_log_sum_exp MIL op computes log(sum(exp(x))), where exp(x) overflows in fp16 when x > log(65504/C) (approx 7.63 for C=32 channels) on Apple Neural Engine, causing a hard output collapse to 0. Replace with the numerically stable decomposition: logsumexp(x) = max(x) + log(sum(exp(x - max(x)))). By subtracting max first, all exp() arguments are <= 0, so exp() values are in (0, 1] and no overflow can occur. This matches the value_inference formula already used in coremltools' own reduce_log_sum_exp MIL op definition.
This was referenced May 29, 2026
Open
Open
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.
Problem
The native
reduce_log_sum_expMIL op computeslog(sum(exp(x))), whereexp(x)overflows in fp16 whenx > log(65504/C)on Apple Neural Engine. For a typical C=32 channel reduction, this means the output collapses to 0 atx ≈ 7.63— well below where the approximationlogsumexp(x) ≈ x + log(C)would kick in. CPU and GPU compute units are unaffected.Same class of bug as the softplus fp16 cliff in #2687 (fixed in #2725), but a different kernel and a different overflow threshold.
Solution
Replace the native
reduce_log_sum_expop with the numerically stable max-shift decomposition:\
logsumexp(x) = max(x) + log(Σ exp(x - max(x)))
\\
By subtracting
max(x)first, allexp()arguments are<= 0, soexp()values are in(0, 1]— no overflow can occur in any precision. This formula is already used by coremltools' ownreduce_log_sum_expMIL op value_inference.Changes
logsumexpcase in the unified reduction converter. Instead of emittingmb.reduce_log_sum_exp(), decompose intoreduce_max→sub→exp→reduce_sum→log→add. Handles bothkeep_dims=Trueandkeep_dims=Falsecases correctly.test_logsumexp_fp16_overflowregression test withC=32channels and input value8.0 > 7.63(the critical overflow point).Testing
test_logsumexpparametrized test cases remain (shapes, dims, frontends, backends)test_logsumexp_fp16_overflowspecifically validates correctness at the ANE fp16 overflow pointRelated
Fixes #2690