Skip to content

Conversation

min-jean-cho
Copy link
Collaborator

@min-jean-cho min-jean-cho commented Mar 18, 2024

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

For those interested :) 1_dim_vector_norm_proof1 1_dim_vector_norm_proof2

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

Copy link

pytorch-bot bot commented Mar 18, 2024

🔗 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 Failures

As of commit 4ff9918 with merge base 61ff41f (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: linalg_frontend release notes category label Mar 18, 2024
@min-jean-cho min-jean-cho changed the title simplify Lp norm for 1-dimensional vector [CPU] optimize Lp norm for 1-dimensional vector Mar 18, 2024
@min-jean-cho min-jean-cho marked this pull request as ready for review March 19, 2024 23:33
@min-jean-cho min-jean-cho added module: cpu CPU specific problem (e.g., perf, algorithm) module: linear algebra Issues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmul labels Mar 19, 2024
@min-jean-cho min-jean-cho requested review from jgong5 and mingfeima and removed request for IvanYashchuk, lezcano and nikitaved March 19, 2024 23:34
Copy link
Collaborator

@lezcano lezcano left a 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?

Copy link
Collaborator

@lezcano lezcano left a 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?

@min-jean-cho
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

@min-jean-cho
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

pytorch-bot bot pushed a commit that referenced this pull request Apr 22, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: cpu CPU specific problem (e.g., perf, algorithm) module: linear algebra Issues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmul open source release notes: linalg_frontend release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vector norm significantly slower on 1D data for CPU code

4 participants