From 0c7953cf9ca9fe2015f8fd9bff5daf068a8e003a Mon Sep 17 00:00:00 2001 From: xujiqiang <1915998056@qq.com> Date: Tue, 29 Dec 2020 14:57:29 +0800 Subject: [PATCH 1/4] imp_hardswish --- python/tvm/relay/frontend/pytorch.py | 12 ++++++++++++ tests/python/frontend/pytorch/test_forward.py | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 94ee9282e4fa..41081f59280c 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -790,6 +790,16 @@ def log_sigmoid(self, inputs, input_types): data = inputs[0] return _op.log(_op.tensor.sigmoid(data)) + def hard_swish(self, inputs, input_types): + input = inputs[0] + dtype = input_types[0] + + def _relu6(data): + return _op.tensor.clip(data, 0.0, 6.0) + + return input * _relu6(input + _expr.const(3.0, dtype=dtype)) / _expr.const(6.0, dtype=dtype) + + def adaptive_avg_pool_2d(self, inputs, input_types): data = inputs[0] output_size = inputs[1] @@ -2266,6 +2276,8 @@ def create_convert_map(self): "aten::bincount": self.bincount, "aten::scatter_add": self.scatter_add, "aten::__not__": self.logical_not, + "aten::hardswish_": self.hard_swish, + "aten::hardswish": self.hard_swish, } def update_convert_map(self, custom_map): diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 04f08b903bf1..e612cb6d4bad 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3437,6 +3437,16 @@ def test_fn(x, weights=None): verify_trace_model(test_fn, [inp, weights], targets) +def test_hard_swish(): + hard_swish = torch.nn.Hardswish() + hard_swish_inplace = torch.nn.Hardswish(inplace=True) + examples = [torch.rand(8), torch.rand(8, 8), torch.rand(8, 16), torch.rand(1, 1, 8)] + targets = ["llvm", "cuda"] + for input in examples: + verify_trace_model(hard_swish, [input], targets) + verify_trace_model(hard_swish_inplace, [input], targets) + + if __name__ == "__main__": # some structural tests test_forward_traced_function() @@ -3603,3 +3613,4 @@ def test_fn(x, weights=None): # Test convert torch script(jit) with specific inputs' types test_convert_torch_script_with_input_types() + test_hard_swish() From b1d04e41271418d687fb6627d05a407cb34447a5 Mon Sep 17 00:00:00 2001 From: xujiqiang <1915998056@qq.com> Date: Tue, 29 Dec 2020 15:34:13 +0800 Subject: [PATCH 2/4] format --- python/tvm/relay/frontend/pytorch.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 41081f59280c..c6c120dd88a4 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -797,8 +797,7 @@ def hard_swish(self, inputs, input_types): def _relu6(data): return _op.tensor.clip(data, 0.0, 6.0) - return input * _relu6(input + _expr.const(3.0, dtype=dtype)) / _expr.const(6.0, dtype=dtype) - + return input * _relu6(input + _expr.const(3.0, dtype=dtype)) / _expr.const(6.0, dtype=dtype) def adaptive_avg_pool_2d(self, inputs, input_types): data = inputs[0] From 43e1bfa4410819e2316ac8197069f3bd79f3d114 Mon Sep 17 00:00:00 2001 From: xujiqiang <1915998056@qq.com> Date: Tue, 29 Dec 2020 16:40:24 +0800 Subject: [PATCH 3/4] fix --- python/tvm/relay/frontend/pytorch.py | 8 ++++---- tests/python/frontend/pytorch/test_forward.py | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index c6c120dd88a4..8e69739544e5 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -791,13 +791,13 @@ def log_sigmoid(self, inputs, input_types): return _op.log(_op.tensor.sigmoid(data)) def hard_swish(self, inputs, input_types): - input = inputs[0] + data = inputs[0] dtype = input_types[0] - def _relu6(data): - return _op.tensor.clip(data, 0.0, 6.0) + def _relu6(input_tensor): + return _op.tensor.clip(input_tensor, 0.0, 6.0) - return input * _relu6(input + _expr.const(3.0, dtype=dtype)) / _expr.const(6.0, dtype=dtype) + return data * _relu6(data + _expr.const(3.0, dtype=dtype)) / _expr.const(6.0, dtype=dtype) def adaptive_avg_pool_2d(self, inputs, input_types): data = inputs[0] diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index e612cb6d4bad..106ee4adefe8 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -3438,13 +3438,9 @@ def test_fn(x, weights=None): def test_hard_swish(): - hard_swish = torch.nn.Hardswish() - hard_swish_inplace = torch.nn.Hardswish(inplace=True) - examples = [torch.rand(8), torch.rand(8, 8), torch.rand(8, 16), torch.rand(1, 1, 8)] - targets = ["llvm", "cuda"] + examples = [torch.rand(8).float(), torch.rand(8, 10).float(), torch.rand(1, 1, 10).float()] for input in examples: - verify_trace_model(hard_swish, [input], targets) - verify_trace_model(hard_swish_inplace, [input], targets) + verify_model(torch.nn.Hardswish().eval(), input_data=input) if __name__ == "__main__": From a774b99f9e2e427868cd628543a2357fb8449447 Mon Sep 17 00:00:00 2001 From: xujiqiang <1915998056@qq.com> Date: Tue, 29 Dec 2020 18:31:46 +0800 Subject: [PATCH 4/4] hard_swish_inplace test case --- tests/python/frontend/pytorch/test_forward.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index 106ee4adefe8..f76c697a2c81 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -181,14 +181,14 @@ def verify_model(model_name, input_data=[], custom_convert_map={}, rtol=1e-5, at baseline_input = [inp.cuda() for inp in baseline_input] with torch.no_grad(): - baseline_outputs = baseline_model(*baseline_input) + baseline_outputs = baseline_model(*[input.clone() for input in baseline_input]) if isinstance(baseline_outputs, tuple): baseline_outputs = tuple(out.cpu().numpy() for out in baseline_outputs) else: baseline_outputs = (baseline_outputs.cpu().numpy(),) - trace = torch.jit.trace(baseline_model, baseline_input) + trace = torch.jit.trace(baseline_model, [input.clone() for input in baseline_input]) if isinstance(baseline_model, torch.nn.Module): trace = trace.float().eval() @@ -200,7 +200,7 @@ def verify_model(model_name, input_data=[], custom_convert_map={}, rtol=1e-5, at input_names = ["input{}".format(idx) for idx, inp in enumerate(baseline_input)] input_shapes = list(zip(input_names, [inp.shape for inp in baseline_input])) mod, params = relay.frontend.from_pytorch(trace, input_shapes, custom_convert_map) - compiled_input = dict(zip(input_names, [inp.cpu().numpy() for inp in baseline_input])) + compiled_input = dict(zip(input_names, [inp.clone().cpu().numpy() for inp in baseline_input])) with tvm.transform.PassContext(opt_level=3): for target, ctx in tvm.testing.enabled_targets(): @@ -3441,6 +3441,7 @@ def test_hard_swish(): examples = [torch.rand(8).float(), torch.rand(8, 10).float(), torch.rand(1, 1, 10).float()] for input in examples: verify_model(torch.nn.Hardswish().eval(), input_data=input) + verify_model(torch.nn.Hardswish(inplace=True).eval(), input_data=input) if __name__ == "__main__":