Add TopK tests (#2165)

This commit is contained in:
Liubov Batanina 2020-09-21 11:37:22 +03:00 committed by GitHub
parent babff1fd15
commit 6839ef7699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 143 additions and 1 deletions

View File

@ -0,0 +1,52 @@
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <vector>
#include "single_layer_tests/topk.hpp"
using namespace LayerTestsDefinitions;
namespace {
const std::vector<InferenceEngine::Precision> netPrecisions = {
InferenceEngine::Precision::FP32,
InferenceEngine::Precision::FP16
};
const std::vector<int64_t> axes = {
0,
1,
2,
};
const std::vector<int64_t> k = {
1,
5,
10,
};
const std::vector<ngraph::opset4::TopK::Mode> modes = {
ngraph::opset4::TopK::Mode::MIN,
ngraph::opset4::TopK::Mode::MAX
};
const std::vector<ngraph::opset4::TopK::SortType> sortTypes = {
ngraph::opset4::TopK::SortType::NONE,
ngraph::opset4::TopK::SortType::SORT_INDICES,
ngraph::opset4::TopK::SortType::SORT_VALUES,
};
INSTANTIATE_TEST_CASE_P(TopK, TopKLayerTest,
::testing::Combine(
::testing::ValuesIn(k),
::testing::ValuesIn(axes),
::testing::ValuesIn(modes),
::testing::ValuesIn(sortTypes),
::testing::ValuesIn(netPrecisions),
::testing::Values(std::vector<size_t>({10, 10, 10})),
::testing::Values(CommonTestUtils::DEVICE_CPU)),
TopKLayerTest::getTestCaseName);
} // namespace

View File

@ -50,6 +50,9 @@ std::vector<std::string> disabledTestPatterns() {
// TODO: Issue: 32032
R"(.*ActivationParamLayerTest.*)",
// TODO: Issue: 37862
R"(.*ReverseSequenceLayerTest.*netPRC=(I8|U8).*)"
R"(.*ReverseSequenceLayerTest.*netPRC=(I8|U8).*)",
// TODO: Issue: 38841
R"(.*TopKLayerTest.*k=10.*mode=min.*sort=index.*)",
R"(.*TopKLayerTest.*k=5.*sort=(none|index).*)"
};
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <tuple>
#include <string>
#include "functional_test_utils/layer_test_utils.hpp"
#include "ngraph_functions/builders.hpp"
namespace LayerTestsDefinitions {
typedef std::tuple<
int64_t, // keepK
int64_t, // axis
ngraph::opset4::TopK::Mode, // mode
ngraph::opset4::TopK::SortType, // sort
InferenceEngine::Precision, // Net precision
InferenceEngine::SizeVector, // inputShape
std::string // Target device name
> TopKParams;
class TopKLayerTest : public testing::WithParamInterface<TopKParams>,
virtual public LayerTestsUtils::LayerTestsCommon {
public:
static std::string getTestCaseName(testing::TestParamInfo<TopKParams> obj);
protected:
void SetUp() override;
};
} // namespace LayerTestsDefinitions

View File

@ -0,0 +1,54 @@
// Copyright (C) 2019 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "single_layer_tests/topk.hpp"
namespace LayerTestsDefinitions {
std::string TopKLayerTest::getTestCaseName(testing::TestParamInfo<TopKParams> obj) {
InferenceEngine::Precision netPrecision;
InferenceEngine::SizeVector inputShape;
std::string targetDevice;
int64_t keepK, axis;
ngraph::opset4::TopK::Mode mode;
ngraph::opset4::TopK::SortType sort;
std::tie(keepK, axis, mode, sort, netPrecision, inputShape, targetDevice) = obj.param;
std::ostringstream result;
result << "IS=" << CommonTestUtils::vec2str(inputShape) << "_";
result << "k=" << keepK << "_";
result << "axis=" << axis << "_";
result << "mode=" << mode << "_";
result << "sort=" << sort << "_";
result << "netPRC=" << netPrecision.name() << "_";
result << "targetDevice=" << targetDevice;
return result.str();
}
void TopKLayerTest::SetUp() {
InferenceEngine::SizeVector inputShape;
InferenceEngine::Precision netPrecision;
int64_t keepK, axis;
ngraph::opset4::TopK::Mode mode;
ngraph::opset4::TopK::SortType sort;
std::tie(keepK, axis, mode, sort, netPrecision, inputShape, targetDevice) = this->GetParam();
auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
auto params = ngraph::builder::makeParams(ngPrc, {inputShape});
auto paramIn = ngraph::helpers::convert2OutputVector(
ngraph::helpers::castOps2Nodes<ngraph::op::Parameter>(params));
auto k = std::make_shared<ngraph::opset3::Constant>(ngraph::element::Type_t::i64, ngraph::Shape{}, &keepK);
auto topk = std::dynamic_pointer_cast<ngraph::opset4::TopK>(
std::make_shared<ngraph::opset4::TopK>(paramIn[0], k, axis, mode, sort));
ngraph::ResultVector results;
for (int i = 0; i < topk->get_output_size(); i++) {
results.push_back(std::make_shared<ngraph::opset4::Result>(topk->output(i)));
}
function = std::make_shared<ngraph::Function>(results, params, "TopK");
}
TEST_P(TopKLayerTest, CompareWithRefsDynamicBath) {
Run();
}
} // namespace LayerTestsDefinitions