[PT FE] Add aten::all implementation (#16901)
* [PT FE] aten::all implementation with tests * [PT FE] Add non-dynamic type check * [PT FE] Update tests, temporarily turn off uint8 tests * [PT FE] Fix dtype for uint8 * [PT FE] Apply suggested optimizations --------- Co-authored-by: Andrei Kochin <andrei.kochin@intel.com>
This commit is contained in:
parent
56290fee01
commit
1c286e4636
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright (C) 2018-2023 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
#include "openvino/frontend/pytorch/node_context.hpp"
|
||||
#include "openvino/opsets/opset10.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace ov {
|
||||
namespace frontend {
|
||||
namespace pytorch {
|
||||
namespace op {
|
||||
|
||||
OutputVector translate_all(const NodeContext& context) {
|
||||
num_inputs_check(context, 1, 3);
|
||||
const auto input_tensor = context.get_input(0);
|
||||
|
||||
element::Type output_dtype = element::boolean;
|
||||
if (input_tensor.get_element_type() == element::u8) {
|
||||
output_dtype = element::u8;
|
||||
}
|
||||
|
||||
bool keep_dims = false;
|
||||
ov::Output<ov::Node> axes;
|
||||
if (context.get_input_size() == 1) {
|
||||
axes = get_axes_range(context, 0);
|
||||
} else {
|
||||
const auto dim = context.const_input<int64_t>(1);
|
||||
axes = context.mark_node(opset10::Constant::create(element::i64, Shape{1}, {dim}));
|
||||
if (!context.input_is_none(2)) {
|
||||
keep_dims = context.const_input<bool>(2);
|
||||
}
|
||||
}
|
||||
|
||||
const auto all_nonzero = context.mark_node(std::make_shared<opset10::ReduceProd>(input_tensor, axes, keep_dims));
|
||||
return {context.mark_node(std::make_shared<opset10::Convert>(all_nonzero, output_dtype))};
|
||||
};
|
||||
|
||||
} // namespace op
|
||||
} // namespace pytorch
|
||||
} // namespace frontend
|
||||
} // namespace ov
|
||||
|
|
@ -19,6 +19,7 @@ OP_CONVERTER(translate_adaptive_max_pool2d);
|
|||
OP_CONVERTER(translate_add);
|
||||
OP_CONVERTER(translate_addcmul);
|
||||
OP_CONVERTER(translate_addmm);
|
||||
OP_CONVERTER(translate_all);
|
||||
OP_CONVERTER(translate_arange);
|
||||
OP_CONVERTER(translate_argsort);
|
||||
OP_CONVERTER(translate_as_tensor);
|
||||
|
|
@ -159,6 +160,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
|
|||
{"aten::add_", op::inplace_op<op::translate_add>},
|
||||
{"aten::addcmul", op::translate_addcmul},
|
||||
{"aten::addmm", op::translate_addmm},
|
||||
{"aten::all", op::translate_all},
|
||||
{"aten::argsort", op::translate_argsort},
|
||||
{"aten::arange", op::translate_arange},
|
||||
{"aten::as_tensor", op::translate_as_tensor},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from pytorch_layer_test_class import PytorchLayerTest
|
||||
|
||||
class aten_all_noparam(torch.nn.Module):
|
||||
def __init__(self) -> None:
|
||||
torch.nn.Module.__init__(self)
|
||||
|
||||
def forward(self, input_tensor):
|
||||
return torch.all(input_tensor)
|
||||
|
||||
class aten_all(torch.nn.Module):
|
||||
def __init__(self, dim, keepdim) -> None:
|
||||
torch.nn.Module.__init__(self)
|
||||
self.dim = dim
|
||||
self.keepdim = keepdim
|
||||
|
||||
def forward(self, input_tensor):
|
||||
return torch.all(
|
||||
input_tensor,
|
||||
dim = self.dim
|
||||
) if self.keepdim is None else torch.all(
|
||||
input_tensor,
|
||||
dim = self.dim,
|
||||
keepdim = self.keepdim
|
||||
)
|
||||
|
||||
class TestAll(PytorchLayerTest):
|
||||
def _prepare_input(self):
|
||||
return (self.input_tensor,)
|
||||
|
||||
@pytest.mark.parametrize("input_tensor", [
|
||||
np.eye(5,5),
|
||||
np.zeros((5, 5)),
|
||||
np.zeros((9,8)) + 1,
|
||||
np.random.randint(0, 2, (5, 9, 7)),
|
||||
np.random.randint(0, 2, (10, 13, 11)),
|
||||
np.random.randint(0, 2, (8, 7, 6, 5, 4)),
|
||||
np.random.randint(0, 2, (11, 11), dtype=np.uint8),
|
||||
np.random.randint(0, 2, (7, 7), dtype=np.uint8),
|
||||
])
|
||||
@pytest.mark.nightly
|
||||
@pytest.mark.precommit
|
||||
def test_all_noparams(self, input_tensor, ie_device, precision, ir_version):
|
||||
self.input_tensor = input_tensor
|
||||
self._test(aten_all_noparam(), None, "aten::all",
|
||||
ie_device, precision, ir_version, trace_model=True, freeze_model=False)
|
||||
|
||||
@pytest.mark.parametrize("input_tensor", [
|
||||
np.eye(5,5),
|
||||
np.zeros((5, 5)),
|
||||
np.zeros((9,8)) + 1,
|
||||
np.random.randint(0, 2, (5, 9, 7)),
|
||||
np.random.randint(0, 2, (10, 13, 11)),
|
||||
np.random.randint(0, 2, (8, 7, 6, 5, 4)),
|
||||
np.random.randint(0, 2, (11, 11), dtype=np.uint8),
|
||||
np.random.randint(0, 2, (7, 7), dtype=np.uint8),
|
||||
])
|
||||
@pytest.mark.parametrize("keepdim", [
|
||||
True,
|
||||
False,
|
||||
None
|
||||
])
|
||||
@pytest.mark.nightly
|
||||
@pytest.mark.precommit
|
||||
def test_all(self, input_tensor, keepdim, ie_device, precision, ir_version):
|
||||
self.input_tensor = input_tensor
|
||||
for dim in range(len(input_tensor.shape)):
|
||||
self._test(aten_all(dim, keepdim), None, "aten::all",
|
||||
ie_device, precision, ir_version, trace_model=True, freeze_model=False)
|
||||
Loading…
Reference in New Issue