-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
124 additions
and
2 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
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,77 @@ | ||
import os | ||
import pytest | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
import onnx | ||
from onnxslim import slim | ||
from onnxslim import register_fusion_pattern | ||
from onnxslim.core.graph_rewriter import PatternMatcher, Pattern, PatternGenerator | ||
|
||
|
||
class TestPatternGenerator: | ||
def test_gelu(self, request): | ||
class PatternModel(nn.Module): | ||
def __init__(self): | ||
super(PatternModel, self).__init__() | ||
self.gelu = nn.GELU() | ||
|
||
def forward(self, x): | ||
x = self.gelu(x) | ||
return x | ||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
self.relu0 = nn.ReLU() | ||
self.pattern = PatternModel() | ||
self.relu1 = nn.ReLU() | ||
|
||
def forward(self, x): | ||
x = self.relu0(x) | ||
x = self.pattern(x) | ||
x = self.relu1(x) | ||
return x | ||
|
||
input = torch.randn(2) | ||
p = PatternModel() | ||
m = Model() | ||
directory = "tmp/" + request.node.name | ||
os.makedirs(directory, exist_ok=True) | ||
|
||
pattern_filename = f"{directory}/{request.node.name}.onnx" | ||
torch.onnx.export(p, input, pattern_filename) | ||
|
||
model_filename = f"{directory}/{request.node.name}.onnx" | ||
torch.onnx.export(m, input, model_filename) | ||
|
||
model = onnx.load(pattern_filename) | ||
pgen = PatternGenerator(model) | ||
template = pgen.generate() | ||
pattern = Pattern(template) | ||
|
||
class GeluMatcher(PatternMatcher): | ||
def __init__(self, pattern, priority): | ||
super().__init__(pattern, priority) | ||
|
||
@property | ||
def name(self): | ||
return "FusionGelu" | ||
|
||
def rewrite(self): | ||
raise Exception("Pattern Matched") | ||
|
||
register_fusion_pattern(GeluMatcher(pattern, 1)) | ||
slim(model_filename, f"{directory}/{request.node.name}_slim.onnx") | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main( | ||
[ | ||
"-p", | ||
"no:warnings", | ||
"-sv", | ||
"tests/test_pattern_generator.py", | ||
] | ||
) |