Skip to content

Fix Softplus precision issue#10576

Merged
skottmckay merged 1 commit into
masterfrom
gwang-msft/fix_soft_plus
Feb 27, 2022
Merged

Fix Softplus precision issue#10576
skottmckay merged 1 commit into
masterfrom
gwang-msft/fix_soft_plus

Conversation

@guoyu-wang
Copy link
Copy Markdown
Contributor

@guoyu-wang guoyu-wang commented Feb 16, 2022

Description: Fix Softplus precision issue

Motivation and Context

  • Issue Onnx runtime accuracy result #10540
  • The issue is that fp32 log is not very precise for log(1+x) if the number x is small. Eigen provides the log1p for log 1 plus with higher precision

ConstEigenVectorArrayMap<T> xm(this->input + first, len);
EigenVectorArrayMap<T> ym(output_ptr, len);
ym = (xm > 0).select(xm + ((-xm).exp() + 1.0f).log(), ((xm).exp() + 1.0f).log());
ym = (xm > 0).select(xm + ((-xm).exp()).log1p(), ((xm).exp()).log1p());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is a bit concern here, since Eigen log use SSE2 and AVX, while log1p use std::log1p, this may cause the perf loss on certain platform.

See, https://eigen.tuxfamily.org/dox/group__CoeffwiseMathFunctions.html

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@yufenglee is there anything in mlas that could be used here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MLAS has MlasComputeExp and MlasComputeLogistic, but doesn't have one for Softplus. If we compute Softplus with MlasComputeExp and MlasComputeLogistic, it will go though memory multiple times.

Looks like this activation is not used commonly. If it is, we can add optimization for it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AFAIK Softmax is typically used as a post-processing step and often done outside of the model or as a final step inside the model. As such perf may not be critical.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is for Softplus, not Softmax

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ahh sorry - I have never seen a model that uses Softplus.

Correctness has to come first. If there's a perf issue that affects a production model we can look at adding something to MLAS in the future. That said, from some very brief research it seems like Relu is generally preferred over Softplus, so any perf issue could potentially be addressed by switching to that.

@guoyu-wang guoyu-wang requested review from snnn and xadupre February 18, 2022 19:40
@skottmckay skottmckay merged commit 240f31e into master Feb 27, 2022
@skottmckay skottmckay deleted the gwang-msft/fix_soft_plus branch February 27, 2022 23:27
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.

3 participants