-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] add common_subexpression_elimination doc (#11)
- Loading branch information
Showing
5 changed files
with
57 additions
and
0 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,37 @@ | ||
# Common SubExpression Elimination | ||
|
||
## Introduction | ||
Common Subexpression Elimination (CSE) is a powerful optimization technique commonly employed in compilers to improve the efficiency of code execution. It targets redundant computations within a program by identifying and removing duplicate expressions, thus reducing both computational overhead and memory usage. By eliminating redundant computations, CSE enhances the overall performance of slimmed onnx model. | ||
|
||
## How CSE Works | ||
In many programs, certain expressions are computed multiple times within a given scope, even though their results remain constant across these computations. Common subexpressions refer to these redundant expressions. CSE identifies such common subexpressions and replaces subsequent occurrences with references to the original computation result. This process effectively reduces the number of computations required during program execution. | ||
|
||
For example, consider the following code snippet: | ||
``` | ||
int a = b + c; | ||
int x = b + c; | ||
``` | ||
In this code, b + c is a common subexpression computed twice. With CSE, the redundant computation of b + c would be eliminated, and both occurrences of x would directly reference the computation result of a. | ||
|
||
## Running the example | ||
|
||
```bash | ||
python cse_demo.py # to generate onnx model for demo | ||
``` | ||
|
||
![../../image/before_cse.png](../../images/before_cse.png) | ||
|
||
There are two identical blocks that are doing the same things. | ||
|
||
```bash | ||
onnxslim ln_cse.onnx slim.onnx | ||
``` | ||
|
||
After onnxslim, the output will look like this: | ||
|
||
![../../image/after_cse.png](../../images/after_cse.png) | ||
|
||
|
||
and the summary is as follow: | ||
|
||
![../../image/cse.png](../../images/cse.png) |
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,20 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
|
||
class Model(torch.nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
embedding_dim = 10 | ||
self.layer_norm = nn.LayerNorm(embedding_dim) | ||
|
||
def forward(self, x): | ||
return self.layer_norm(x) + F.layer_norm(x, [10]) | ||
|
||
|
||
layer_norm = Model() | ||
|
||
batch, sentence_length, embedding_dim = 20, 5, 10 | ||
embedding = torch.randn(batch, sentence_length, embedding_dim) | ||
torch.onnx.export(layer_norm, embedding, "ln_cse.onnx", opset_version=13) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.