Skip to content

fix: Increase inverse softmax LUT resolution to reduce argmax flips (closes #1512) - #1513

Closed
botbikamordehai2-sketch wants to merge 2 commits into
fastmachinelearning:mainfrom
botbikamordehai2-sketch:fix/issue-1512-1785667422
Closed

botbikamordehai2-sketch wants to merge 2 commits into
fastmachinelearning:mainfrom
botbikamordehai2-sketch:fix/issue-1512-1785667422

Conversation

@botbikamordehai2-sketch

Copy link
Copy Markdown

What

In hls4ml 1.3.0, converting models with a softmax output layer to HLS can cause a large fraction of argmax classification decisions to differ from the float32 reference, despite correctly normalized probabilities (Σp = 1). The issue is traced to the softmax LUT approximation in the QSoftmax converter, which for some weight distributions has insufficient resolution in the inverse (1/exp) table.

Fix

Ensure that the input fixed-point precision for the inverse table has at least 16 bits of total width, which provides sufficient LUT entries to accurately represent exp(-x) across a wide range of inputs. The patch modifies the QSoftmaxHandler to upgrade the inverse table input precision to at least 16 bits when the original width is smaller. This is a minimal change that affects only the LUT resolution and should reduce argmax flips without breaking normalization.

Closes #1512

@niteshg97

Copy link
Copy Markdown
Contributor

@botbikamordehai2-sketch thank you for the quick PR,

One note: my reproduction script uses a standard Keras
Dense(2, activation='softmax') layer, which goes through
nnet_activation.h. ..... not the QSoftmaxHandler in the HGQ
extension. The two code paths are
separate, so the fix may not resolve the issue for standard
Keras models.

Could you confirm whether this PR also addresses the
standard (non-HGQ) softmax path? A simple test would be
running the reproduction script from #1512 with the patch
applied and checking whether the 94–200/200 argmax flip
rates reduce.

@JanFSchulte

Copy link
Copy Markdown
Contributor

pre-commit.ci autofix

@JanFSchulte JanFSchulte added the please test Trigger testing by creating local PR branch label Aug 3, 2026
@jmitrevs

jmitrevs commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Check also for compatibility with #1494 / #1476. They partially try to fix the same issue.

@calad0i

calad0i commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

This diverges from the configured precision in the qat model, and you won't get the same results seen at qat though.

@JanFSchulte

Copy link
Copy Markdown
Contributor

The conclusion is that this would lead to a regression in performance for models trained using QAT in HGQ2, and is therefore not desired. Softmax performance is being improved by other PRs such as #1542 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

please test Trigger testing by creating local PR branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] hls4ml 1.3.0: HLS softmax LUT causes argmax flip rate vs float32 despite correct normalization.

5 participants