GatherND_8 reference implementation (#8257)

* Add GatherND_8 operation

* Update shape infer function and tests

* Initial commit for nGraph GatherND_8 operation

* Add GatherNDBase class implementation

* Fix base class errors

* Add missrd header

* Update base class

* Update GatherND_8 implementation

* Fix codestyle

* Fix wrong rank

* Implement tests for gatherND_8 shape inference function

* fix codestyle

* Add limitation to doc

* Siplyfy check in shape inference

* Add more test cases

* Update shape inference function

* Add more test cases to cover all case with dynamic input shapes

* Update shape inference function

* Refactor tests

* Initial commit for gatherND_8 reference implementation

* Add visitor tests for gatherND_8 operation

* Add visitor tests for gatherND_8 operation

* Correct comment

* Add additional check is shape inference function

* Update shape inference implementation for gathernd operartion

* Fix codestyle

* Remove restriction for data is fully defined

* Update shape inference functon

* Add gatherND_8 reference to evaluate map

* Add tests for gathernd_8 reference in batch_dims > 1

* Fix codestyle

* Fix codestyle

* Fix missed check for nonetype

* Remove redundant checks for batch_dims

* Use get_shape method instead of get_input_shape in GatherND reference implementation call

* update reference implementation call

* Properly add GatherND_8 to test opset table

* Update GatherND reference implementation for support opset8 version

* Add unit tests for gatherND_8 reference implementation

* Update gatherND reference implementation call in evaluate map

* Mark failed tests for missed plugin gatherND_8 implementations

* Fix codestyle

* Fix codestyle

* Fix codestyle

* Update tests

* Move common methods to base class

* Fix codestyle

* Revert clone_with_new_input function moving

* Add more test for reference

* partially revert API changes
This commit is contained in:
Anton Chetverikov 2021-11-16 10:58:06 +03:00 committed by GitHub
parent b6bdc4a567
commit d0f16d205a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 250 additions and 35 deletions

View File

@ -25,7 +25,7 @@ struct GatherNDParams {
std::string testcaseName;
};
class ReferenceGatherNDTest : public testing::TestWithParam<GatherNDParams>, public CommonReferenceTest {
class ReferenceGatherND5Test : public testing::TestWithParam<GatherNDParams>, public CommonReferenceTest {
public:
void SetUp() override {
auto params = GetParam();
@ -59,18 +59,18 @@ private:
PartialShape{params.dataTensor.shape});
const auto indices = std::make_shared<op::v0::Parameter>(params.indicesTensor.type,
PartialShape{params.indicesTensor.shape});
std::shared_ptr<op::v5::GatherND> gatherElement;
std::shared_ptr<op::v5::GatherND> gatherND;
if (params.batchDims == 0) {
gatherElement = std::make_shared<op::v5::GatherND>(data, indices);
gatherND = std::make_shared<op::v5::GatherND>(data, indices);
} else {
gatherElement = std::make_shared<op::v5::GatherND>(data, indices, params.batchDims);
gatherND = std::make_shared<op::v5::GatherND>(data, indices, params.batchDims);
}
function = std::make_shared<ov::Function>(NodeVector {gatherElement}, ParameterVector {data, indices});
function = std::make_shared<ov::Function>(NodeVector {gatherND}, ParameterVector {data, indices});
return function;
}
};
TEST_P(ReferenceGatherNDTest, CompareWithRefs) {
TEST_P(ReferenceGatherND5Test, CompareWithRefs) {
Exec();
}
@ -203,6 +203,207 @@ std::vector<GatherNDParams> generateCombinedParams() {
return combinedParams;
}
INSTANTIATE_TEST_SUITE_P(smoke_GatherND_With_Hardcoded_Refs, ReferenceGatherNDTest,
testing::ValuesIn(generateCombinedParams()), ReferenceGatherNDTest::getTestCaseName);
} // namespace
INSTANTIATE_TEST_SUITE_P(smoke_GatherND_With_Hardcoded_Refs, ReferenceGatherND5Test,
testing::ValuesIn(generateCombinedParams()), ReferenceGatherND5Test::getTestCaseName);
class ReferenceGatherND8Test : public testing::TestWithParam<GatherNDParams>, public CommonReferenceTest {
public:
void SetUp() override {
auto params = GetParam();
function = CreateFunction(params);
inputData = {params.dataTensor.data, params.indicesTensor.data};
refOutData = {params.expectedTensor.data};
}
static std::string getTestCaseName(const testing::TestParamInfo<GatherNDParams>& obj) {
auto param = obj.param;
std::ostringstream result;
result << "dType=" << param.dataTensor.type;
result << "_dShape=" << param.dataTensor.shape;
result << "_aType=" << param.indicesTensor.type;
result << "_aShape=" << param.indicesTensor.shape;
result << "_bDims=" << param.batchDims;
result << "_eType=" << param.expectedTensor.type;
if (param.testcaseName != "") {
result << "_eShape=" << param.expectedTensor.shape;
result << "_=" << param.testcaseName;
} else {
result << "_eShape=" << param.expectedTensor.shape;
}
return result.str();
}
private:
static std::shared_ptr<Function> CreateFunction(const GatherNDParams& params) {
std::shared_ptr<Function> function;
const auto data = std::make_shared<op::v0::Parameter>(params.dataTensor.type,
PartialShape{params.dataTensor.shape});
const auto indices = std::make_shared<op::v0::Parameter>(params.indicesTensor.type,
PartialShape{params.indicesTensor.shape});
std::shared_ptr<op::v8::GatherND> gatherND;
if (params.batchDims == 0) {
gatherND = std::make_shared<op::v8::GatherND>(data, indices);
} else {
gatherND = std::make_shared<op::v8::GatherND>(data, indices, params.batchDims);
}
function = std::make_shared<ov::Function>(NodeVector {gatherND}, ParameterVector {data, indices});
return function;
}
};
TEST_P(ReferenceGatherND8Test, CompareWithRefs) {
Exec();
}
template <element::Type_t IN_ET>
std::vector<GatherNDParams> generateParams_v8() {
using T = typename element_type_traits<IN_ET>::value_type;
std::vector<GatherNDParams> params {
GatherNDParams(
Tensor(IN_ET, {3, 3}, std::vector<T>{10, 11, 12, 13, 14, 15, 16, 17, 18}),
Tensor(element::i32, {2}, std::vector<int32_t>{1, 2}),
0,
Tensor(IN_ET, {}, std::vector<T>{15}),
"gather_nd_8_single_indices"),
GatherNDParams(
Tensor(IN_ET, {2, 2}, std::vector<T>{10, 11, 12, 13}),
Tensor(element::i32, {2, 2}, std::vector<int32_t>{0, 0, 1, 1}),
0,
Tensor(IN_ET, {2}, std::vector<T>{10, 13}),
"gather_nd_8_scalar_from_2d"),
GatherNDParams(
Tensor(IN_ET, {2, 2}, std::vector<T>{10, 11, 12, 13}),
Tensor(element::i32, {2, 1}, std::vector<int32_t>{1, 0}),
0,
Tensor(IN_ET, {2, 2}, std::vector<T>{12, 13, 10, 11}),
"gather_nd_8_1d_from_2d"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {2, 3}, std::vector<int32_t>{0, 0, 1, 1, 0, 1}),
0,
Tensor(IN_ET, {2}, std::vector<T>{11, 21}),
"gather_nd_8_scalar_from_3d"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {2, 2}, std::vector<int32_t>{0, 1, 1, 0}),
0,
Tensor(IN_ET, {2, 2}, std::vector<T>{12, 13, 20, 21}),
"gather_nd_8_1d_from_3d"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {1, 1}, std::vector<int32_t>{1}),
0,
Tensor(IN_ET, {1, 2, 2}, std::vector<T>{20, 21, 22, 23}),
"gather_nd_8_2d_from_3d"),
GatherNDParams(
Tensor(IN_ET, {2, 2}, std::vector<T>{10, 11, 12, 13}),
Tensor(element::i32, {2, 1, 2}, std::vector<int32_t>{0, 0, 0, 1}),
0,
Tensor(IN_ET, {2, 1}, std::vector<T>{10, 11}),
"gather_nd_8_batch_scalar_from_2d"),
GatherNDParams(
Tensor(IN_ET, {2, 2}, std::vector<T>{10, 11, 12, 13}),
Tensor(element::i32, {2, 1, 1}, std::vector<int32_t>{1, 0}),
0,
Tensor(IN_ET, {2, 1, 2}, std::vector<T>{12, 13, 10, 11}),
"gather_nd_8_batch_1d_from_2d"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {2, 2, 3}, std::vector<int32_t>{0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0}),
0,
Tensor(IN_ET, {2, 2}, std::vector<T>{11, 21, 13, 22}),
"gather_nd_8_batch_scalar_from_3d"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {2, 2, 2}, std::vector<int32_t>{0, 1, 1, 0, 0, 0, 1, 1}),
0,
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{12, 13, 20, 21, 10, 11, 22, 23}),
"gather_nd_8_batch_1d_from_3d"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {2, 2, 2}, std::vector<int32_t>{0, -1, -1, 0, 0, 0, 1, 1}),
0,
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{12, 13, 20, 21, 10, 11, 22, 23}),
"gather_nd_8_batch_1d_from_3d_negative"),
GatherNDParams(
Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
Tensor(element::i32, {2, 1, 1}, std::vector<int32_t>{1, 0}),
0,
Tensor(IN_ET, {2, 1, 2, 2}, std::vector<T>{20, 21, 22, 23, 10, 11, 12, 13}),
"gather_nd_8_batch_2d_from_3d"),
GatherNDParams(
Tensor(IN_ET, {2, 3, 4}, std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}),
Tensor(element::i32, {2, 1}, std::vector<int32_t>{1, 0}),
1,
Tensor(IN_ET, {2, 4}, std::vector<T>{5, 6, 7, 8, 13, 14, 15, 16}),
"gather_nd_8_batch_dims1"),
GatherNDParams(
Tensor(IN_ET, {2, 3, 4, 2}, std::vector<T>{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}),
Tensor(element::i32, {2, 3, 3, 2}, std::vector<int32_t>{
1, 0, 3, 1, 2, 1, 0, 1, 1, 1, 2, 0, 3, 0, 3, 1, 2, 1,
2, 0, 1, 1, 3, 1, 1, 1, 2, 0, 2, 0, 0, 0, 3, 1, 3, 1}),
2,
Tensor(IN_ET, {2, 3, 3}, std::vector<T>{
3, 8, 6, 10, 12, 13, 23, 24, 22, 29, 28, 32, 36, 37, 37, 41, 48, 48}),
"gather_8_nd_batch_dims2"),
GatherNDParams(
Tensor(IN_ET, {2, 3, 4}, std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}),
Tensor(element::i32, {2, 3, 1, 1}, std::vector<int32_t>{1, 0, 2, 0, 2, 2}),
2,
Tensor(IN_ET, {2, 3, 1}, std::vector<T>{2, 5, 11, 13, 19, 23}),
"gather_8_nd_batch_dims2_lead_dims"),
GatherNDParams(
Tensor(IN_ET, {2, 3, 4, 5}, std::vector<T>{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111, 112, 113, 114, 115, 116, 117, 118, 119, 120}),
Tensor(element::i32, {2, 3, 2, 1}, std::vector<int32_t>{
1, 0, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0}),
2,
Tensor(IN_ET, {2, 3, 2, 5}, std::vector<T>{
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 31, 32, 33, 34, 35, 21, 22, 23,
24, 25, 51, 52, 53, 54, 55, 41, 42, 43, 44, 45, 66, 67, 68, 69,
70, 61, 62, 63, 64, 65, 91, 92, 93, 94, 95, 81, 82, 83, 84, 85,
111, 112, 113, 114, 115, 101, 102, 103, 104, 105}),
"gather_8_nd_batch_dims2_non_scalar_slices"),
};
return params;
}
std::vector<GatherNDParams> generateCombinedParams_v8() {
const std::vector<std::vector<GatherNDParams>> generatedParams {
generateParams_v8<element::Type_t::i8>(),
generateParams_v8<element::Type_t::i16>(),
generateParams_v8<element::Type_t::i32>(),
generateParams_v8<element::Type_t::i64>(),
generateParams_v8<element::Type_t::u8>(),
generateParams_v8<element::Type_t::u16>(),
generateParams_v8<element::Type_t::u32>(),
generateParams_v8<element::Type_t::u64>(),
generateParams_v8<element::Type_t::bf16>(),
generateParams_v8<element::Type_t::f16>(),
generateParams_v8<element::Type_t::f32>(),
generateParams_v8<element::Type_t::f64>(),
};
std::vector<GatherNDParams> combinedParams;
for (const auto& params : generatedParams) {
combinedParams.insert(combinedParams.end(), params.begin(), params.end());
}
return combinedParams;
}
INSTANTIATE_TEST_SUITE_P(smoke_GatherND_With_Hardcoded_Refs, ReferenceGatherND8Test,
testing::ValuesIn(generateCombinedParams_v8()), ReferenceGatherND8Test::getTestCaseName);
} // namespace

View File

@ -4,7 +4,7 @@
#pragma once
#include "ngraph/op/util/gather_nd_base.hpp"
#include "ngraph/op/op.hpp"
#include "openvino/op/gather_nd.hpp"
namespace ngraph {

View File

@ -25,7 +25,6 @@ public:
GatherND(const Output<Node>& data, const Output<Node>& indices, const size_t batch_dims = 0);
void validate_and_infer_types() override;
bool visit_attributes(AttributeVisitor& visitor) override;
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
};
} // namespace v5
@ -48,7 +47,6 @@ public:
GatherND(const Output<Node>& data, const Output<Node>& indices, const size_t batch_dims = 0);
void validate_and_infer_types() override;
bool visit_attributes(AttributeVisitor& visitor) override;
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
};
} // namespace v8

View File

@ -30,6 +30,8 @@ public:
void validate_inputs_and_infer_shape();
bool visit_attributes(AttributeVisitor& visitor) override;
protected:
size_t m_batch_dims = 0;
};

View File

@ -56,11 +56,6 @@ void gather_nd(const T* const params,
const Shape batch_shape(begin(params_shape), next(begin(params_shape), batch_dims));
const auto batch_size = shape_size(batch_shape);
if (batch_dims && batch_size != out_shape.front()) {
throw std::domain_error{"out_shape should have on first dim multiplication of batch number of first"
"dimensions of shape "};
}
if (!std::equal(begin(params_shape), next(begin(params_shape), batch_dims), begin(indices_shape))) {
throw std::domain_error{"dimensions in params and indices have to be equal on batch dimensions"};
}

View File

@ -55,12 +55,6 @@ void op::v5::GatherND::validate_and_infer_types() {
}
}
bool op::v5::GatherND::visit_attributes(AttributeVisitor& visitor) {
NGRAPH_OP_SCOPE(v5_GatherND_visit_attributes);
visitor.on_attribute("batch_dims", m_batch_dims);
return true;
}
shared_ptr<Node> op::v5::GatherND::clone_with_new_inputs(const OutputVector& new_args) const {
NGRAPH_OP_SCOPE(v5_GatherND_clone_with_new_inputs);
check_new_args_count(this, new_args);
@ -80,12 +74,6 @@ void op::v8::GatherND::validate_and_infer_types() {
validate_inputs_and_infer_shape();
}
bool op::v8::GatherND::visit_attributes(AttributeVisitor& visitor) {
NGRAPH_OP_SCOPE(v8_GatherND_visit_attributes);
visitor.on_attribute("batch_dims", m_batch_dims);
return true;
}
shared_ptr<Node> op::v8::GatherND::clone_with_new_inputs(const OutputVector& new_args) const {
NGRAPH_OP_SCOPE(v8_GatherND_clone_with_new_inputs);
check_new_args_count(this, new_args);

View File

@ -107,3 +107,8 @@ void ov::op::util::GatherNDBase::validate_inputs_and_infer_shape() {
set_output_type(0, data_type, ov::PartialShape::dynamic());
}
}
bool ov::op::util::GatherNDBase::visit_attributes(AttributeVisitor& visitor) {
visitor.on_attribute("batch_dims", m_batch_dims);
return true;
}

View File

@ -2516,17 +2516,42 @@ bool evaluate(const shared_ptr<op::v5::GatherND>& op, const HostTensorVector& ou
runtime::reference::gather_nd<T, int64_t>(inputs[0]->get_data_ptr<T>(),
inputs[1]->get_data_ptr<int64_t>(),
outputs[0]->get_data_ptr<T>(),
op->get_input_shape(0),
op->get_input_shape(1),
op->get_output_shape(0),
inputs[0]->get_shape(),
inputs[1]->get_shape(),
outputs[0]->get_shape(),
op->get_batch_dims());
} else if (op->get_input_element_type(1) == element::i32) {
runtime::reference::gather_nd<T, int32_t>(inputs[0]->get_data_ptr<T>(),
inputs[1]->get_data_ptr<int32_t>(),
outputs[0]->get_data_ptr<T>(),
op->get_input_shape(0),
op->get_input_shape(1),
op->get_output_shape(0),
inputs[0]->get_shape(),
inputs[1]->get_shape(),
outputs[0]->get_shape(),
op->get_batch_dims());
} else {
throw ngraph_error("Unexpected indices type for GatherND operation");
}
return true;
}
template <element::Type_t ET>
bool evaluate(const shared_ptr<op::v8::GatherND>& op, const HostTensorVector& outputs, const HostTensorVector& inputs) {
using T = typename element_type_traits<ET>::value_type;
if (op->get_input_element_type(1) == element::i64) {
runtime::reference::gather_nd<T, int64_t>(inputs[0]->get_data_ptr<T>(),
inputs[1]->get_data_ptr<int64_t>(),
outputs[0]->get_data_ptr<T>(),
inputs[0]->get_shape(),
inputs[1]->get_shape(),
outputs[0]->get_shape(),
op->get_batch_dims());
} else if (op->get_input_element_type(1) == element::i32) {
runtime::reference::gather_nd<T, int32_t>(inputs[0]->get_data_ptr<T>(),
inputs[1]->get_data_ptr<int32_t>(),
outputs[0]->get_data_ptr<T>(),
inputs[0]->get_shape(),
inputs[1]->get_shape(),
outputs[0]->get_shape(),
op->get_batch_dims());
} else {
throw ngraph_error("Unexpected indices type for GatherND operation");

View File

@ -106,6 +106,7 @@ NGRAPH_OP(MulticlassNms, op::v8)
NGRAPH_OP(Slice, op::v8)
NGRAPH_OP(DeformableConvolution, ngraph::op::v8)
NGRAPH_OP(If, ngraph::op::v8)
NGRAPH_OP(GatherND, op::v8)
NGRAPH_OP(Sigmoid, op::v0)
NGRAPH_OP(Tanh, op::v0)