-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[CPU] optimize Lp norm for 1-dimensional vector #122143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CPU] optimize Lp norm for 1-dimensional vector #122143
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/122143
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 4ff9918 with merge base 61ff41f ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
…-jean-cho/pytorch into minjean/speedup_norm_one_dim
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair, but the implementation can be improved and simplified.
Can you also add a test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, now that you are at it, can you update the decomposition as well?
80a98bf
to
10fc75d
Compare
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 8 jobs have failed, first few of them are: trunk / win-vs2019-cpu-py3 / test (default, 1, 3, windows.4xlarge.nonephemeral), trunk / win-vs2019-cpu-py3 / test (default, 2, 3, windows.4xlarge.nonephemeral), trunk / win-vs2019-cpu-py3 / test (default, 3, 3, windows.4xlarge.nonephemeral), trunk / linux-focal-cuda12.1-py3.10-gcc9 / test (nogpu_AVX512, 1, 1, linux.2xlarge), trunk / linux-focal-cuda12.1-py3.10-gcc9 / test (nogpu_NO_AVX2, 1, 1, linux.2xlarge) Details for Dev Infra teamRaised by workflow job |
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Fixes #120229 - Optimize vector norm by simplifying vector norm formula for 1-dimensional vector. - Vector norm formula for 1-dimensional vector simplifies to `abs(x)`. See below for proof. - Next step, we can similarly optimize matrix norm (`torch.linalg.matrix_norm`) for 1 x 1 matrix. - Additionally, avoids overflow in power, `abs(x) ** p` for large `p` or `x`, for 1-dimensional vector. ### Performance Avg Latency (ms) of `torch.norm` and `torch.linalg.vector_norm` for `torch.norm(torch.randn(2**18, 1), ord, -1)` `torch.linalg.vector_norm(torch.randn(2**18, 1), ord, -1)` Tested on 28 physical cores/socket, 1 socket on Skylake. | | | | | **Avg Latency (ms)** | | | |-------------------------- |----------------- |--------- |--------- |----------------------- |----------------------- |---------------------------------------- | | **op** | **input shape** | **dim** | **ord** | **baseline (master)** | **optimized (7102f1e)** | **speedup ratio (baseline/optimized)** | | torch.norm | (2**18, 1) | -1 | fro | 34.3755531 | 0.0125408 | 2741.094 | | | | | inf | 34.0952635 | 0.0122237 | 2789.271 | | | | | -inf | 34.3674493 | 0.0120759 | 2845.953 | | | | | 0 | 34.1004515 | 0.0175261 | 1945.69 | | | | | 1 | 34.1688442 | 0.0121593 | 2810.089 | | | | | -1 | 33.949492 | 0.0120282 | 2822.487 | | | | | 2 | 34.3669581 | 0.0120401 | 2854.366 | | | | | -2 | 33.9252067 | 0.0121069 | 2802.139 | | | | | | | | | | torch.linalg.vector_norm | (2**18, 1) | -1 | inf | 34.090879 | 0.0095105 | 3584.545 | | | | | -inf | 34.3708754 | 0.0099111 | 3467.931 | | | | | 0 | 34.0880775 | 0.0141716 | 2405.38 | | | | | 1 | 34.1392851 | 0.0093174 | 3664.036 | | | | | -1 | 33.925395 | 0.0092483 | 3668.302 | | | | | 2 | 34.3854165 | 0.0092459 | 3719.002 | | | | | -2 | 33.932972 | 0.0093007 | 3648.429 | ### Proof <details> <summary>For those interested :)</summary> <img width="382" alt="1_dim_vector_norm_proof1" src="https://github.com/pytorch/pytorch/assets/93151422/59b1e00b-8fcd-47cb-877d-d31403b5195b"> <img width="432" alt="1_dim_vector_norm_proof2" src="https://github.com/pytorch/pytorch/assets/93151422/236bea15-2dd5-480b-9871-58b2e3b24322"> </details> Pull Request resolved: #122143 Approved by: https://github.com/lezcano
Fixes #120229
abs(x)
. See below for proof.torch.linalg.matrix_norm
) for 1 x 1 matrix.abs(x) ** p
for largep
orx
, for 1-dimensional vector.Performance
Avg Latency (ms) of
torch.norm
andtorch.linalg.vector_norm
fortorch.norm(torch.randn(2**18, 1), ord, -1)
torch.linalg.vector_norm(torch.randn(2**18, 1), ord, -1)
Tested on 28 physical cores/socket, 1 socket on Skylake.
Proof
For those interested :)
cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @jianyuh @nikitaved @pearu @mruberry @walterddr @xwang233 @lezcano @voznesenskym @penguinwu @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @aakhundov @ColinPeppler @amjames @desertfire @chauhang