[PT FE]: support aten::amax, aten::amin, aten::clip, aten::clamp_ (#20338)
This commit is contained in:
@@ -11,11 +11,11 @@ class TestClamp(PytorchLayerTest):
|
||||
import numpy as np
|
||||
return (np.random.randn(1, 3, 224, 224).astype(np.float32),)
|
||||
|
||||
def create_model(self, minimum, maximum, as_tensors=False):
|
||||
def create_model(self, minimum, maximum, as_tensors=False, op_type='clamp'):
|
||||
import torch
|
||||
|
||||
class aten_clamp(torch.nn.Module):
|
||||
def __init__(self, minimum, maximum, as_tensors):
|
||||
def __init__(self, minimum, maximum, as_tensors, op_type="clamp"):
|
||||
super(aten_clamp, self).__init__()
|
||||
if minimum is not None and as_tensors:
|
||||
minimum = torch.tensor(minimum)
|
||||
@@ -23,20 +23,31 @@ class TestClamp(PytorchLayerTest):
|
||||
if maximum is not None and as_tensors:
|
||||
maximum = torch.tensor(maximum)
|
||||
self.max = maximum
|
||||
self.forward = getattr(self, f"forward_{op_type}")
|
||||
|
||||
def forward(self, x):
|
||||
def forward_clamp(self, x):
|
||||
return torch.clamp(x, self.min, self.max)
|
||||
|
||||
def forward_clip(self, x):
|
||||
return torch.clip(x, self.min, self.max)
|
||||
|
||||
def forward_clamp_(self, x):
|
||||
return x.clamp_(self.min, self.max), x
|
||||
|
||||
def forward_clip_(self, x):
|
||||
return x.clip_(self.min, self.max), x
|
||||
|
||||
ref_net = None
|
||||
op_name = "aten::clamp"
|
||||
return aten_clamp(minimum, maximum, as_tensors), ref_net, op_name
|
||||
op_name = f"aten::{op_type}"
|
||||
return aten_clamp(minimum, maximum, as_tensors, op_type), ref_net, op_name
|
||||
|
||||
@pytest.mark.parametrize("minimum,maximum",
|
||||
[(0., 1.), (-0.5, 1.5), (None, 10.), (None, -10.), (10., None), (-10., None), (100, 200)])
|
||||
@pytest.mark.parametrize("as_tensors", [True, False])
|
||||
@pytest.mark.parametrize("op_type", ["clamp", "clamp_"])
|
||||
@pytest.mark.nightly
|
||||
def test_clamp(self, minimum, maximum, as_tensors, ie_device, precision, ir_version):
|
||||
self._test(*self.create_model(minimum, maximum, as_tensors), ie_device, precision, ir_version)
|
||||
def test_clamp(self, minimum, maximum, as_tensors, op_type, ie_device, precision, ir_version):
|
||||
self._test(*self.create_model(minimum, maximum, as_tensors, op_type), ie_device, precision, ir_version)
|
||||
|
||||
@pytest.mark.xfail(reason='OpenVINO clamp does not support min > max')
|
||||
def test_clamp_min_greater(self, ie_device, precision, ir_version):
|
||||
|
||||
@@ -283,4 +283,57 @@ class TestMinimumMaximum(PytorchLayerTest):
|
||||
ie_device, precision, ir_version, kwargs_to_prepare_input=
|
||||
{"input_dtype": input_dtype, "second_input_dtype": input_dtype,
|
||||
"out": True}
|
||||
)
|
||||
|
||||
|
||||
class TestAminAmax(PytorchLayerTest):
|
||||
def _prepare_input(self, input_dtype="float32", out=False, axes=None, keep_dims=False):
|
||||
import numpy as np
|
||||
x = np.random.randn(1, 3, 10, 10).astype(input_dtype)
|
||||
if not out:
|
||||
return (x,)
|
||||
if isinstance(axes, list):
|
||||
axes = tuple(axes)
|
||||
out = np.zeros_like(np.max(x, axis=axes, keepdims=keep_dims), dtype=input_dtype)
|
||||
return (x, out)
|
||||
|
||||
def create_model(self, op_type, axis, keep_dims, out=False):
|
||||
import torch
|
||||
op_types = {
|
||||
"amax": torch.amax,
|
||||
"amin": torch.amin
|
||||
}
|
||||
|
||||
|
||||
op = op_types[op_type]
|
||||
|
||||
class aten_amin_amax(torch.nn.Module):
|
||||
def __init__(self, op, axis, keep_dims, out):
|
||||
super().__init__()
|
||||
self.op = op
|
||||
self.axis = axis
|
||||
self.keep_dims = keep_dims
|
||||
if out:
|
||||
self.forward = self.forward_out
|
||||
|
||||
def forward_out(self, x, y):
|
||||
return self.op(x, self.axis, self.keep_dims, out=y), y
|
||||
|
||||
def forward(self, x):
|
||||
return self.op(x, self.axis, self.keep_dims)
|
||||
|
||||
|
||||
model_cls = aten_amin_amax(op, axis, keep_dims, out)
|
||||
|
||||
return model_cls, None, f"aten::{op_type}"
|
||||
|
||||
@pytest.mark.parametrize("op_type", ["amin", "amax"])
|
||||
@pytest.mark.parametrize("axis", [0, -1, 1, [1, 2], [-1, -2], [2, 0, -1], [0, 1, 2, 3]])
|
||||
@pytest.mark.parametrize("keep_dims", [True, False])
|
||||
@pytest.mark.parametrize("out", [True, False])
|
||||
@pytest.mark.parametrize("input_dtype", ['float32', 'int32', 'int64', 'float64'])
|
||||
def test_amin_amax(self, op_type, input_dtype, axis, keep_dims, out, ie_device, precision, ir_version):
|
||||
self._test(*self.create_model(op_type, axis, keep_dims, out),
|
||||
ie_device, precision, ir_version, kwargs_to_prepare_input=
|
||||
{"input_dtype": input_dtype, "out": out, "axes": axis, "keep_dims": keep_dims}
|
||||
)
|
||||
Reference in New Issue
Block a user