-
Notifications
You must be signed in to change notification settings - Fork 31
PoC: Convert cond #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dayo09
wants to merge
9
commits into
Samsung:main
Choose a base branch
from
dayo09:0901-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
PoC: Convert cond #330
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8b176f
PoC: Convert cond
dayo09 329b6ac
Pass: test SimpleCond1
dayo09 f7c0c8b
Pass: test SimpleCond3
dayo09 0dea901
merge
dayo09 3ed898a
Pass: op.cond all test
dayo09 8764327
Refactor and format
dayo09 0defa5e
Refactor
dayo09 a65c438
temp
dayo09 a788f66
Pass: op.cond all test, but const prop don't work for subgraphs. This…
dayo09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,70 @@ | ||
| """ Example - circle model import/export """ | ||
|
|
||
| import pycircle | ||
|
|
||
| from pycircle.circleir.model import Model | ||
| from pycircle.circleir.subgraph import Subgraph | ||
| from pycircle.circleir.tensor import Tensor | ||
| from pycircle.circleir.operators import CircleAdd, CircleCall | ||
| from pycircle.util.alias import TensorType | ||
|
|
||
|
|
||
| ### subgraph 0 | ||
| ### input0, input1 -> call0 (subgraph 1) -> tensor0 | ||
| ### tensor0, weights0 -> add0 -> tensor1 | ||
| graph0 = Subgraph() | ||
| graph0.name = "graph0" | ||
| graph0.inputs = [ | ||
| Tensor("sub1_input0", [1, 3], TensorType.FLOAT32), | ||
| Tensor("sub1_input1", [1, 3], TensorType.FLOAT32), | ||
| ] | ||
|
|
||
| call0 = CircleCall() | ||
| call0.inputs = [graph0.inputs[0], graph0.inputs[1]] | ||
| call0.subgraph = 1 | ||
| call0.outputs(0).attribute("Call0", [1, 3], TensorType.FLOAT32) | ||
|
|
||
|
|
||
| add1 = CircleAdd() | ||
| weights0 = Tensor("weights0", [1, 3], TensorType.FLOAT32, [100., 100., 100.]) | ||
| add1.inputs = [call0.outputs(0), weights0] | ||
| add1.outputs(0).attribute("add0", [1, 3], TensorType.FLOAT32) | ||
|
|
||
| graph0.outputs = [add1.outputs(0)] | ||
|
|
||
| ### subgraph 1 | ||
| ### input0, input1 -> ADD -> output | ||
| graph1 = Subgraph() | ||
| graph1.name = "graph1" | ||
| graph1.inputs = [ | ||
| Tensor("input0", [1, 3], TensorType.FLOAT32), | ||
| Tensor("input1", [1, 3], TensorType.FLOAT32, [-100., -100., -100.]) | ||
| ] | ||
| sub_add = CircleAdd() | ||
| sub_add.inputs = [graph1.inputs[0], graph1.inputs[1]] | ||
| sub_add.outputs(0).attribute("SubAdd", [1, 3], TensorType.FLOAT32) | ||
| graph1.outputs = [sub_add.outputs(0)] | ||
|
|
||
| ### model | ||
| circle_model = Model() | ||
| circle_model.subgraphs = [graph0, graph1] | ||
| circle_model.signature_defs = { | ||
| "graph0": { | ||
| "subgraph_index": 0 | ||
| }, | ||
| "graph1": { | ||
| "subgraph_index": 1 | ||
| }, | ||
| } | ||
|
|
||
| pycircle.export_circle_model(circle_model, "call.circle") | ||
|
|
||
| import torch | ||
| try: | ||
| from onert import infer | ||
| except ImportError: | ||
| raise RuntimeError("The 'onert' package is required to run this function.") | ||
|
|
||
| session_float = infer.session("call.circle") | ||
| output = session_float.infer((torch.randn(1,3),torch.randn(1,3),), measure=True) | ||
| print(output) |
This file contains hidden or 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,76 @@ | ||
| """ Example - circle model import/export """ | ||
|
|
||
| import pycircle | ||
| from pycircle.circleir.model import Model | ||
| from pycircle.circleir.subgraph import Subgraph | ||
| from pycircle.circleir.tensor import Tensor | ||
| from pycircle.circleir.operators import CircleAdd, CircleIf | ||
| from pycircle.util.alias import TensorType | ||
|
|
||
| # 입력 텐서 및 상수 텐서 정의 | ||
| input_tensor0 = Tensor("input0", [1, 3], TensorType.FLOAT32) | ||
| input_tensor1 = Tensor("input1", [1, 3], TensorType.FLOAT32) | ||
| weight_add_100 = Tensor("constant0", [1, 3], TensorType.FLOAT32, [100, 100, 100]) | ||
| weight_sub_100 = Tensor("constant1", [1, 3], TensorType.FLOAT32, [-100, -100, -100]) | ||
|
|
||
| ### then_subgraph ### | ||
| then_subgraph = Subgraph() | ||
| then_subgraph.inputs = [Tensor("input0", [1, 3], TensorType.FLOAT32), weight_add_100] | ||
|
|
||
| add_op_then = CircleAdd() | ||
| add_op_then.inputs = [then_subgraph.inputs[0], then_subgraph.inputs[1]] | ||
| add_op_then.outputs(0).attribute("add_output_then", [1, 3], TensorType.FLOAT32) | ||
| then_subgraph.outputs = [add_op_then.outputs(0)] | ||
|
|
||
| ### else_subgraph ### | ||
| else_subgraph = Subgraph() | ||
| else_subgraph.inputs = [Tensor("input0", [1, 3], TensorType.FLOAT32), weight_sub_100] | ||
|
|
||
| add_op_else = CircleAdd() | ||
| add_op_else.inputs = [else_subgraph.inputs[0], Tensor("input0", [1, 3], TensorType.FLOAT32)] | ||
| add_op_else.outputs(0).attribute("add_output_else", [1, 3], TensorType.FLOAT32) | ||
| else_subgraph.outputs = [add_op_else.outputs(0)] | ||
|
|
||
| ### root_subgraph with CircleIf ### | ||
| root_subgraph = Subgraph() | ||
| root_subgraph.name = "root_subgraph" | ||
| condition_tensor = Tensor("condition", [1], TensorType.BOOL) | ||
| root_subgraph.inputs = [condition_tensor, input_tensor0, input_tensor1] | ||
|
|
||
| circle_if_op = CircleIf(1, 2) | ||
| circle_if_op.inputs = [condition_tensor, input_tensor0, input_tensor1] | ||
| circle_if_op.outputs(0).attribute("output_tensor", [1, 3], TensorType.FLOAT32) | ||
| circle_if_op.then_subgraph_index = 1 | ||
| circle_if_op.else_subgraph_index = 2 | ||
| root_subgraph.outputs = [circle_if_op.outputs(0)] | ||
|
|
||
| # 모델 구성 | ||
| circle_model = Model() | ||
| circle_model.description = "pycircle example : signature_def" | ||
| circle_model.subgraphs = [root_subgraph, then_subgraph, else_subgraph] | ||
| circle_model.signature_defs = { | ||
| "root_graph": {"subgraph_index": 0}, | ||
| "then_graph": {"subgraph_index": 1}, | ||
| "else_graph": {"subgraph_index": 2}, | ||
| } | ||
|
|
||
| # 모델 export | ||
| pycircle.export_circle_model(circle_model, "signature_def.circle") | ||
|
|
||
| # onert를 통한 추론 (Inference) | ||
| import torch | ||
| try: | ||
| from onert import infer | ||
| except ImportError: | ||
| raise RuntimeError("The 'onert' package is required to run this function.") | ||
|
|
||
| session = infer.session("signature_def.circle") | ||
| output = session.infer( | ||
| ( | ||
| torch.tensor([True]), # condition tensor | ||
| torch.randn(1, 3), # input tensor 0 | ||
| torch.tensor([[100., 100., 100.]]),# weights tensor | ||
| ), | ||
| measure=True | ||
| ) | ||
| print(output) |
This file contains hidden or 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,61 @@ | ||
| """ Example - circle model import/export """ | ||
|
|
||
| import pycircle | ||
|
|
||
| from pycircle.circleir.model import Model | ||
| from pycircle.circleir.subgraph import Subgraph | ||
| from pycircle.circleir.tensor import Tensor | ||
| from pycircle.circleir.operators import CircleAdd | ||
| from pycircle.util.alias import TensorType | ||
|
|
||
| subgraph1 = Subgraph() | ||
| subgraph1.name = "subgraph1" | ||
| subgraph1.inputs = [ | ||
| Tensor("subgraph1_input", [1, 3], TensorType.FLOAT32), | ||
| ] | ||
|
|
||
| weights1 = Tensor("constant1", [1, 3], TensorType.FLOAT32, [0.1, 0.2, 0.3]) | ||
|
|
||
| add1 = CircleAdd() | ||
| add1.inputs = [subgraph1.inputs[0], weights1] | ||
| add1.outputs(0).attribute("Add1", [1, 3], TensorType.FLOAT32) | ||
|
|
||
| subgraph1.outputs = [add1.outputs(0)] | ||
|
|
||
| subgraph2 = Subgraph() | ||
| subgraph2.name = "subgraph2" | ||
| subgraph2.inputs = [ | ||
| Tensor("subgraph2_input1", [1, 3], TensorType.FLOAT32), | ||
| Tensor("subgraph2_input2", [1, 3], TensorType.FLOAT32), | ||
| ] | ||
|
|
||
| add2 = CircleAdd() | ||
| add2.inputs = [subgraph2.inputs[0], subgraph2.inputs[1]] | ||
| add2.outputs(0).attribute("Add2", [1, 3], TensorType.FLOAT32) | ||
|
|
||
| subgraph2.outputs = [add2.outputs(0)] | ||
|
|
||
| circle_model = Model() | ||
| circle_model.description = "pycircle example : signature_def" | ||
| # circle_model.subgraphs = [subgraph2, subgraph1] | ||
| circle_model.subgraphs = [subgraph1, subgraph2] | ||
| circle_model.signature_defs = { | ||
| "add_constant": { | ||
| "subgraph_index": 0 | ||
| }, | ||
| "add_two_inputs": { | ||
| "subgraph_index": 1 | ||
| }, | ||
| } | ||
|
|
||
| pycircle.export_circle_model(circle_model, "signature_def_original.circle") | ||
|
|
||
| import torch | ||
| try: | ||
| from onert import infer | ||
| except ImportError: | ||
| raise RuntimeError("The 'onert' package is required to run this function.") | ||
|
|
||
| session_float = infer.session("signature_def_original.circle") | ||
| output = session_float.infer((torch.randn(1,3),)) | ||
| breakpoint() |
This file contains hidden or 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,8 @@ | ||
|
|
||
| def test(): | ||
| if 1 is 1: | ||
| pass | ||
|
|
||
| print("HI") | ||
|
|
||
| test() | ||
This file contains hidden or 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,107 @@ | ||
| # Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| # | ||
| # 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. | ||
|
|
||
| import torch | ||
|
|
||
| from test.modules.base import TestModuleBase | ||
| from test.utils.tag import use_onert | ||
|
|
||
| @use_onert | ||
| class SimpleCond1(TestModuleBase): | ||
| class Sin(torch.nn.Module): | ||
| def forward(self, x): | ||
| return torch.sin(x) + 1 | ||
|
|
||
| class Cos(torch.nn.Module): | ||
| def forward(self, x): | ||
| return torch.cos(x) - 1 | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.sin = self.Sin() | ||
| self.cos = self.Cos() | ||
|
|
||
| def forward(self, x, y): | ||
| return torch.cond(x.sum() + y.sum() > 0, | ||
| lambda x_: self.sin(x_), | ||
| lambda x_: self.cos(x_), | ||
| operands=(x,)) | ||
| def get_example_inputs(self): | ||
| return (torch.randn(3, 3), torch.randn(3, 3)), {} | ||
|
|
||
|
|
||
|
|
||
| @use_onert | ||
| class SimpleCond2(TestModuleBase): | ||
| class Sin(torch.nn.Module): | ||
| def forward(self, x, y): | ||
| return torch.sin(x) + 1 | ||
|
|
||
| class Cos(torch.nn.Module): | ||
| def forward(self, x, y): | ||
| return torch.cos(x) - 1 | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.sin = self.Sin() | ||
| self.cos = self.Cos() | ||
|
|
||
| def forward(self, x, y): | ||
| return torch.cond(x.sum() + y.sum() > 0, | ||
| lambda x, y: self.sin(x, y), | ||
| lambda x, y: self.cos(x, y), | ||
| operands=(x,y)) | ||
| def get_example_inputs(self): | ||
| return (torch.randn(3, 3), torch.randn(3, 3)), {} | ||
|
|
||
|
|
||
| @use_onert | ||
| class SimpleCond3(TestModuleBase): | ||
| class Sin(torch.nn.Module): | ||
| def forward(self, x, y): | ||
| return torch.sin(x) + torch.sin(y) | ||
|
|
||
| class Cos(torch.nn.Module): | ||
| def forward(self, x, y): | ||
| return torch.cos(x) - torch.cos(y) | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.sin = self.Sin() | ||
| self.cos = self.Cos() | ||
|
|
||
| def forward(self, x, y): | ||
| return torch.cond(x.sum() + y.sum() > 0, | ||
| lambda x, y: self.sin(x, y), | ||
| lambda x, y: self.cos(x, y), | ||
| operands=(x,y)) | ||
| def get_example_inputs(self): | ||
| return (torch.randn(3, 3), torch.randn(3, 3)), {} | ||
|
|
||
|
|
||
| # if __name__ == "__main__": | ||
| # model = SimpleCond2() | ||
| # x = torch.randn(3, 3) | ||
| # y = torch.randn(3, 3) | ||
|
|
||
| # # export (그래프 생성) | ||
| # exported_model = torch.export.export(model, (x, y)) | ||
|
|
||
| # # export된 모델 호출 테스트 | ||
| # output = exported_model.module()(x, y) | ||
| # exported_model.graph.print_tabular() | ||
| # print(exported_model.graph_signature.user_inputs) | ||
| # print(exported_model.graph_signature.user_outputs) | ||
| # print(output) | ||
| # breakpoint() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant file