Skip to content

Commit

Permalink
[Feature] add common_subexpression_elimination doc (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
inisis authored Mar 20, 2024
1 parent 71ff5f8 commit de7ac80
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/common_subexpression_elimination/README.md
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)
20 changes: 20 additions & 0 deletions examples/common_subexpression_elimination/cse_demo.py
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)
Binary file added images/after_cse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/before_cse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit de7ac80

Please sign in to comment.