forked from buddy-compiler/buddy-mlir
-
Notifications
You must be signed in to change notification settings - Fork 0
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: xtayex <liyuliang2022_sei@stu.ecnu.edu.cn> Co-authored-by: zhanghb97 <hongbin2019@iscas.ac.cn>
- Loading branch information
1 parent
0d38057
commit d34f521
Showing
69 changed files
with
3,766 additions
and
2,241 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,23 @@ | ||
# Buddy Graph Representation Examples | ||
|
||
## Run the Examples | ||
|
||
0. Enter your Python Env | ||
``` | ||
(base)$ conda activate buddy | ||
(buddy)$ ... | ||
``` | ||
1. Build Python Packages | ||
2. Configure Python Path | ||
``` | ||
(buddy)$ cd buddy-mlir/build | ||
(buddy)$ export BUDDY_MLIR_BUILD_DIR=$PWD | ||
(buddy)$ export LLVM_MLIR_BUILD_DIR=$PWD/../llvm/build | ||
(buddy)$ export PYTHONPATH=${LLVM_MLIR_BUILD_DIR}/tools/mlir/python_packages/mlir_core:${BUDDY_MLIR_BUILD_DIR}/python_packages:${PYTHONPATH} | ||
``` | ||
3. Run the Examples | ||
``` | ||
(buddy)$ cd examples/BuddyGraph | ||
(buddy)$ python import-dynamo-break.py | ||
``` |
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,63 @@ | ||
# ===- import-dynamo-break.py -------------------------------------------------- | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ===--------------------------------------------------------------------------- | ||
# | ||
# The example for dynamo graph break, import, and execute. | ||
# | ||
# ===--------------------------------------------------------------------------- | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
from torch._functorch.aot_autograd import aot_autograd_decompositions | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
class TestModule(torch.nn.Module): | ||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
|
||
def forward(self, b, c): | ||
if torch.nn.functional.silu(b)[0][0]: | ||
return torch.add(b, c) | ||
else: | ||
return torch.matmul(b, c) | ||
|
||
# Define a PyTorch model and run it with PyTorch runtime. | ||
model = TestModule() | ||
a, b = torch.randn((1024, 1024)), torch.randn((1024, 1024)) | ||
print(model(a, b)) | ||
|
||
# JIT Mode | ||
# Initialize Buddy Dynamo Compiler to compile and execute the PyTorch model. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=aot_autograd_decompositions | ||
) | ||
model_opt = torch.compile(model, backend=dynamo_compiler) | ||
print(model_opt(a, b)) | ||
|
||
torch._dynamo.reset() | ||
|
||
# AOT Mode | ||
# Import PyTorch model to Buddy Graph and MLIR/LLVM IR. | ||
graphs = dynamo_compiler.importer( | ||
model, a, b | ||
) | ||
for g in graphs: | ||
g.lower_to_top_level_ir() | ||
print(g._imported_module) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# ===- import-resnet18.py ------------------------------------------------------ | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ===--------------------------------------------------------------------------- | ||
# | ||
# This is the test of resnet18 model. | ||
# | ||
# ===--------------------------------------------------------------------------- | ||
|
||
import torch | ||
import torchvision | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
model = torchvision.models.resnet18() | ||
model = model.eval() | ||
|
||
# Initialize Dynamo Compiler with specific configurations as an importer. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
data = torch.randn([1, 3, 224, 224]) | ||
# Import the model into MLIR module and parameters. | ||
with torch.no_grad(): | ||
graphs = dynamo_compiler.importer(model, data) | ||
|
||
assert len(graphs) == 1 | ||
graphs[0].lower_to_top_level_ir(do_params_pack=True) | ||
print(graphs[0]._imported_module) |
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
Oops, something went wrong.