forked from Lyken17/pytorch-OpCounter
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: UltralyticsAssistant <[email protected]>
- Loading branch information
1 parent
43c064a
commit 4052703
Showing
21 changed files
with
193 additions
and
193 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Ultralytics 🚀 - AGPL-3.0 license | ||
# Ultralytics Actions https://github.com/ultralytics/actions | ||
# This workflow automatically formats code and documentation in PRs to official Ultralytics standards | ||
|
||
name: Ultralytics Actions | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request_target: | ||
branches: [main] | ||
types: [opened, closed, synchronize] | ||
|
||
jobs: | ||
format: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Run Ultralytics Formatting | ||
uses: ultralytics/actions@main | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} # automatically generated, do not modify | ||
python: true # format Python code and docstrings | ||
markdown: true # format Markdown | ||
prettier: true # format YAML | ||
spelling: true # check spelling | ||
links: false # check broken links | ||
summary: true # print PR summary with GPT4 (requires 'openai_api_key' or 'openai_azure_api_key' and 'openai_azure_endpoint') | ||
openai_azure_api_key: ${{ secrets.OPENAI_AZURE_API_KEY }} | ||
openai_azure_endpoint: ${{ secrets.OPENAI_AZURE_ENDPOINT }} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
TODOs. | ||
|
||
1. A more user-friendly warning for un-defined modules. [Done] | ||
1. A more user-friendly warning for un-defined modules. \[Done\] | ||
2. Supports for models in torchvision (e.g., residual add). | ||
3. Layer wise printing | ||
|
||
Integration with torchprofile? | ||
Integration with torchprofile? |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
# MACs, FLOPs, what is the difference? | ||
|
||
`FLOPs` is abbreviation of **floating operations** which includes mul / add / div ... etc. | ||
`FLOPs` is abbreviation of **floating operations** which includes mul / add / div ... etc. | ||
|
||
`MACs` stands for **multiply–accumulate operation** that performs `a <- a + (b x c)`. | ||
`MACs` stands for **multiply–accumulate operation** that performs `a <- a + (b x c)`. | ||
|
||
As shown in the text, one `MACs` has one `mul` and one `add`. That is why in many places `FLOPs` is nearly two times as `MACs`. | ||
|
||
However, the application in real world is far more complex. Let's consider a matrix multiplication example. | ||
`A` is an matrix of dimension `mxn` and `B` is an vector of `nx1`. | ||
However, the application in real world is far more complex. Let's consider a matrix multiplication example. `A` is an matrix of dimension `mxn` and `B` is an vector of `nx1`. | ||
|
||
```python | ||
for i in range(m): | ||
for j in range(n): | ||
C[i][j] += A[i][j] * B[j] # one mul-add | ||
``` | ||
``` | ||
|
||
It would be `mn` `MACs` and `2mn` `FLOPs`. But such implementation is slow and parallelization is necessary to run faster | ||
|
||
|
||
```python | ||
```python | ||
for i in range(m): | ||
parallelfor j in range(n): | ||
d[j] = A[i][j] * B[j] # one mul | ||
C[i][j] = sum(d) # n adds | ||
parallelfor j in range(n): | ||
d[j] = A[i][j] * B[j] # one mul | ||
C[i][j] = sum(d) # n adds | ||
``` | ||
|
||
Then the number of `MACs` is no longer `mn` . | ||
|
||
Then the number of `MACs` is no longer `mn` . | ||
|
||
When comparing MACs /FLOPs, we want the number to be implementation-agnostic and as general as possible. Therefore in THOP, **we only consider the number of multiplications** and ignore all other operations. | ||
When comparing MACs /FLOPs, we want the number to be implementation-agnostic and as general as possible. Therefore in THOP, **we only consider the number of multiplications** and ignore all other operations. | ||
|
||
PS: The FLOPs is approximated by multiplying two. | ||
PS: The FLOPs is approximated by multiplying two. |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
|
||
from thop import profile | ||
|
||
|
||
class TestUtils: | ||
def test_matmul_case2(self): | ||
n, in_c, out_c = 1, 100, 200 | ||
net = nn.Linear(in_c, out_c) | ||
flops, params = profile(net, inputs=(torch.randn(n, in_c), )) | ||
flops, params = profile(net, inputs=(torch.randn(n, in_c),)) | ||
print(flops, params) | ||
assert flops == n * in_c * out_c | ||
|
||
def test_matmul_case2(self): | ||
for i in range(10): | ||
n, in_c, out_c = torch.randint(1, 500, (3,)).tolist() | ||
net = nn.Linear(in_c, out_c) | ||
flops, params = profile(net, inputs=(torch.randn(n, in_c), )) | ||
flops, params = profile(net, inputs=(torch.randn(n, in_c),)) | ||
print(flops, params) | ||
assert flops == n * in_c * out_c | ||
|
||
def test_conv2d(self): | ||
n, in_c, out_c = torch.randint(1, 500, (3,)).tolist() | ||
net = nn.Linear(in_c, out_c) | ||
flops, params = profile(net, inputs=(torch.randn(n, in_c), )) | ||
flops, params = profile(net, inputs=(torch.randn(n, in_c),)) | ||
print(flops, params) | ||
assert flops == n * in_c * out_c | ||
|
Oops, something went wrong.