Skip to content
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

[converter] support aten::{maximum, minimum} #269

Merged
merged 2 commits into from
Dec 5, 2023
Merged
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
2 changes: 2 additions & 0 deletions docs/op_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ Operators that are implemented in Python
| `aten::matmul` | |
| `aten::max` | |
| `aten::max_pool2d` | Only dilation == 1 is supported |
| `aten::maximum` | |
| `aten::mean` | |
| `aten::meshgrid` | aten::meshgrid for dynamic tensors is not supported |
| `aten::min` | |
| `aten::minimum` | |
| `aten::mish` | |
| `aten::mm` | |
| `aten::mul` | |
Expand Down
14 changes: 11 additions & 3 deletions tests/converter_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def test_sign(self):

def model(x):
return torch.sign(x)

model_path = get_model_path()
converter = TFLiteConverter(model, dummy_input, model_path, nchw_transpose=False)
converter.convert()

dummy_output = model(dummy_input)
tfl_output = tfl_run_model(model_path, dummy_input, dummy_output)
assert_close(dummy_output, tfl_output)
assert_close(dummy_output, tfl_output)

def test_masked_fill(self):
class TestModel(nn.Module):
Expand Down Expand Up @@ -188,6 +188,8 @@ def test_binary_elementwise_same_dtype(self):
(torch, 'eq'),
(torch, 'ne'),
(torch, 'rsub'),
(torch, 'maximum'),
(torch, 'minimum'),
]

funcs = [getattr(ns, attr) for ns, attr in func_names if hasattr(ns, attr)]
Expand Down Expand Up @@ -224,6 +226,8 @@ def test_binary_elementwise_constant_same_dtype(self):
(torch, 'eq'),
(torch, 'ne'),
(torch, 'rsub'),
(torch, 'maximum'),
(torch, 'minimum'),
]

funcs = [getattr(ns, attr) for ns, attr in func_names if hasattr(ns, attr)]
Expand Down Expand Up @@ -259,6 +263,8 @@ def test_binary_elementwise_different_dtype(self):
(torch, 'eq'),
(torch, 'ne'),
(torch, 'rsub'),
(torch, 'maximum'),
(torch, 'minimum'),
]

funcs = [getattr(ns, attr) for ns, attr in func_names if hasattr(ns, attr)]
Expand Down Expand Up @@ -295,6 +301,8 @@ def test_binary_elementwise_constant_different_dtype(self):
(torch, 'eq'),
(torch, 'ne'),
(torch, 'rsub'),
(torch, 'maximum'),
(torch, 'minimum'),
]

funcs = [getattr(ns, attr) for ns, attr in func_names if hasattr(ns, attr)]
Expand Down Expand Up @@ -5294,7 +5302,7 @@ def model(x):
dummy_output = model(dummy_input)
tfl_output = tfl_run_model(model_path, dummy_input, dummy_output)
assert_close(dummy_output, tfl_output, atol=256.0, rtol=256.0)

@unittest.skipIf(not hasattr(torch, 'norm'), "Norm is not supported")
def test_norm_p1(self):
dummy_input = torch.randn(10, 10, dtype=torch.float32)
Expand Down
2 changes: 2 additions & 0 deletions tinynn/converter/operators/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@
"aten::baddbmm": ATenBaddbmmOperator,
"aten::linalg_vector_norm": ATenLinalgVectorNormOperator,
"aten::broadcast_tensors": ATenBroadcastTensorsOperator,
"aten::maximum": ATenMaximumOperator,
"aten::minimum": ATenMinimumOperator,
# quantized
"aten::quantize_per_tensor": ATenQuantizePerTensorOperator,
"aten::fake_quantize_per_tensor_affine": ATenFakeQuantizePerTensorAffineOperator,
Expand Down
26 changes: 24 additions & 2 deletions tinynn/converter/operators/torch/aten.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class AtenSignOperator(ATenSignSchema):
def parse(self, node, attrs, args, graph_converter):

super().parse(node, attrs, args, graph_converter)

self.run(node)
Expand Down Expand Up @@ -3453,6 +3453,28 @@ def parse_common(self, graph_converter, input_idx=0, mask_idx=1, other_idx=2, ou
graph_converter.add_operator(op)


class ATenMaximumOperator(ATenMaximumSchema):
def parse(self, node, attrs, args, graph_converter):
super().parse(node, attrs, args, graph_converter)

self.run(node)
if type(self.input_tensors[1]) != torch.Tensor:
self.input_tensors[1] = self.torch_tensor_from_scalar(self.input_tensors[0], self.input_tensors[1])

self.elementwise_binary(tfl.MaximumOperator, graph_converter, True)


class ATenMinimumOperator(ATenMinimumSchema):
def parse(self, node, attrs, args, graph_converter):
super().parse(node, attrs, args, graph_converter)

self.run(node)
if type(self.input_tensors[1]) != torch.Tensor:
self.input_tensors[1] = self.torch_tensor_from_scalar(self.input_tensors[0], self.input_tensors[1])

self.elementwise_binary(tfl.MinimumOperator, graph_converter, True)


class ATenGtOperator(ATenGtSchema):
def parse(self, node, attrs, args, graph_converter):
super().parse(node, attrs, args, graph_converter)
Expand Down Expand Up @@ -3792,7 +3814,7 @@ def parse(self, node, attrs, args, graph_converter):
self.run(node)
self.parse_common(node, attrs, args, graph_converter)


class ATenLinalgVectorNormOperator(ATenLinalgVectorNormSchema):
def parse(self, node, attrs, args, graph_converter):
super().parse(node, attrs, args, graph_converter)
Expand Down
Loading