Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions call.py
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)
76 changes: 76 additions & 0 deletions if.py
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)
61 changes: 61 additions & 0 deletions signature.py
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()
8 changes: 8 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def test():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant file

if 1 is 1:
pass

print("HI")

test()
107 changes: 107 additions & 0 deletions test/modules/op/cond.py
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()
Loading