Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions test/modules/op/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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


class SimpleItem(TestModuleBase):
def __init__(self):
super().__init__()

def forward(self, x, y):
return x.item() + y

def get_example_inputs(self):
return (torch.tensor(33), 55), {}
24 changes: 12 additions & 12 deletions tico/interpreter/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ def infer(circle_binary: bytes, *args: Any, **kwargs: Any) -> Any:
raise RuntimeError(
f"Mismatch input length: input({len(user_inputs)}) != circle model({len(model_input_shapes_np)})"
)
for input_idx, user_input in enumerate(user_inputs):
# Shape
if list(user_input.shape) != list(model_input_shapes_np[input_idx]):
raise RuntimeError(
f"Mismatch input {input_idx} shape : input({user_input.shape}) != circle model({model_input_shapes_np[input_idx]})"
)
# Data type
user_input_type_cm = to_circle_dtype(user_input.dtype)
if user_input_type_cm != model_input_types_cm[input_idx]:
raise RuntimeError(
f"Mismatch input {input_idx} data type : input({user_input_type_cm}) != circle model({model_input_types_cm[input_idx]})"
)
# for input_idx, user_input in enumerate(user_inputs):
# # Shape
# if list(user_input.shape) != list(model_input_shapes_np[input_idx]):
# raise RuntimeError(
# f"Mismatch input {input_idx} shape : input({user_input.shape}) != circle model({model_input_shapes_np[input_idx]})"
# )
# # Data type
# user_input_type_cm = to_circle_dtype(user_input.dtype)
# if user_input_type_cm != model_input_types_cm[input_idx]:
# raise RuntimeError(
# f"Mismatch input {input_idx} data type : input({user_input_type_cm}) != circle model({model_input_types_cm[input_idx]})"
# )

# Initialize interpreter
intp = Interpreter(circle_binary)
Expand Down
11 changes: 6 additions & 5 deletions tico/passes/remove_nop.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class RemoveNop(PassBase):
"""

target_ops = (
[
torch.ops.prims.view_of.default,
]
+ ops.aten.alias
ops.aten.alias
+ ops.aten.clone
+ ops.aten.detach
+ [torch.ops.aten.lift_fresh_copy.default]
+ [
torch.ops.prims.view_of.default,
torch.ops.aten.lift_fresh_copy.default,
torch.ops.aten._local_scalar_dense.default,
]
)

def __init__(self):
Expand Down
4 changes: 3 additions & 1 deletion tico/passes/remove_redundant_assert_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@


assert_node_targets = [
torch.ops.aten._assert_scalar.default,
torch.ops.aten._assert_tensor_metadata.default,
torch.ops.aten.sym_constrain_range_for_size.default, # Related to symbolic shape validation
]


@trace_graph_diff_on_pass
class RemoveRedundantAssertionNodes(PassBase):
"""
This removes redundant assertion nodes.
- `aten.assert_tensor_meta.default`
When assertion node is erased, related comparison nodes are also removed by DCE pass.
"""

def __init__(self):
Expand Down
5 changes: 4 additions & 1 deletion tico/serialize/circle_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ def to_circle_shape(
], # Sequence[int | torch.SymInt] is added for type covariance
) -> Tuple[List[int], Optional[List[int]]]:

if any(isinstance(s, torch.SymInt) for s in torch_shape):
if len(torch_shape) == 0:
# Follow static shape spec of scalar tensor
return [1], None
elif any(isinstance(s, torch.SymInt) for s in torch_shape):
# Follow dynamic shape spec
shape = []
shape_signature = []
Expand Down
19 changes: 15 additions & 4 deletions tico/serialize/circle_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,22 @@ def _export_tensors(graph: CircleSubgraph, ep: ExportedProgram) -> None:
if node.target in multiple_output_ops:
continue
node_val = node.meta["val"]
if node_val.layout != torch.strided:
raise RuntimeError(
f"Only support dense tensors (node layout: {node_val.layout})"

if isinstance(node_val, torch.SymInt):
# Add as a scalar tensor
graph.add_tensor_from_scratch(
node.name,
[],
None,
dtype=to_circle_dtype(torch.int64),
source_node=node,
)
graph.add_tensor_from_node(node)
elif isinstance(node_val, torch.fx.Node):
if node_val.layout != torch.strided:
raise RuntimeError(
f"Only support dense tensors (node layout: {node_val.layout})"
)
graph.add_tensor_from_node(node)
logger.debug(f"call_function: {node.name} tensor exported.")

elif node.op == "placeholder":
Expand Down
3 changes: 3 additions & 0 deletions tico/serialize/operators/op_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
if TYPE_CHECKING:
import torch._ops
import torch.fx
import operator

import torch
from circle_schema import circle

Expand All @@ -32,6 +34,7 @@ class AddVisitor(NodeVisitor):
target: List[torch._ops.OpOverload] = [
torch.ops.aten.add.Tensor,
torch.ops.aten.add.Scalar,
operator.add, # builtin operator
]

def __init__(self, op_codes: Dict[OpCode, int], graph: CircleSubgraph):
Expand Down
Loading