Migrate ngraph backend test/convolution backprop (#7997)

* Remove fp16 of Convert layer test from skip_tests.config.cpp as it works now

* update repo

* add draft reference of binary convolution

* build test

* fix build error

* add binary_convolution reference test and ngraph visitor api test

* remove ngraph backend test of binary_convolution

* add reference test and remove ngraph backend test of convolution_backprop

* add f64 case for convolution_backprop
This commit is contained in:
Wilson Seok 2021-10-15 01:39:04 -07:00 committed by GitHub
parent a9688bac54
commit 34fce43ab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 2150 additions and 1867 deletions

View File

@ -0,0 +1,316 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <ngraph_functions/builders.hpp>
#include "openvino/op/binary_convolution.hpp"
#include "base_reference_test.hpp"
#include "openvino/opsets/opset8.hpp"
using namespace reference_tests;
using namespace ov;
namespace {
struct BinaryConvolutionParams {
template <class T>
BinaryConvolutionParams(const PartialShape& inputShape,
const Shape& filterShape,
const PartialShape& outputShape,
const element::Type& iType,
const std::vector<T>& iValues,
const std::vector<uint8_t>& filterValues,
const std::vector<T>& oValues,
const Strides& strides,
const CoordinateDiff& padBegin,
const CoordinateDiff& padEnd,
const Strides& dialations,
const float padValue = 0,
const std::string& test_name = "")
: inputShape(inputShape),
filterShape(filterShape),
outputShape(outputShape),
inType(iType),
outType(iType),
inputData(CreateTensor(iType, iValues)),
filterData(filterValues),
refData(CreateTensor(iType, oValues)),
strides(strides),
padBegin(padBegin),
padEnd(padEnd),
dialations(dialations),
padValue(padValue) {}
PartialShape inputShape;
Shape filterShape;
PartialShape outputShape;
ov::element::Type inType;
ov::element::Type outType;
ov::runtime::Tensor inputData;
std::vector<uint8_t> filterData;
ov::runtime::Tensor refData;
ov::Strides strides;
ov::CoordinateDiff padBegin;
ov::CoordinateDiff padEnd;
ov::Strides dialations;
ov::op::v1::BinaryConvolution::BinaryConvolutionMode mode = ov::op::v1::BinaryConvolution::BinaryConvolutionMode::XNOR_POPCOUNT;
float padValue;
std::string testcaseName;
};
class ReferenceBinaryConvolutionLayerTest : public testing::TestWithParam<BinaryConvolutionParams>, public CommonReferenceTest {
public:
void SetUp() override {
auto params = GetParam();
function = CreateFunction(params, params.filterData);
inputData = {params.inputData};
refOutData = {params.refData};
}
static std::string getTestCaseName(const testing::TestParamInfo<BinaryConvolutionParams>& obj) {
auto param = obj.param;
std::ostringstream result;
result << "inputShape=" << param.inputShape << "_";
result << "filterShape=" << param.filterShape << "_";
result << "outputShape=" << param.outputShape << "_";
result << "iType=" << param.inType << "_";
result << "oType=" << param.outType << "_";
result << "strides=" << param.strides << "_";
result << "padBegin=" << param.padBegin << "_";
result << "padEnd=" << param.padEnd << "_";
result << "dialations=" << param.dialations << "_";
if (param.testcaseName != "") {
result << "padValue=" << param.padValue << "_";
result << param.testcaseName;
} else {
result << "padValue=" << param.padValue;
}
return result.str();
}
private:
static std::shared_ptr<Function> CreateFunction(const BinaryConvolutionParams& params, const std::vector<uint8_t>& filterData) {
const op::PadType auto_pad{op::PadType::EXPLICIT};
const auto in = std::make_shared<op::v0::Parameter>(params.inType, params.inputShape);
auto filter = std::make_shared<opset8::Constant>(ov::element::u1, params.filterShape, &filterData[0]);
const auto BinaryConvolution = std::make_shared<op::v1::BinaryConvolution>(in,
filter,
params.strides,
params.padBegin,
params.padEnd,
params.dialations,
params.mode,
params.padValue,
auto_pad);
return std::make_shared<ov::Function>(NodeVector {BinaryConvolution}, ParameterVector {in});
}
};
TEST_P(ReferenceBinaryConvolutionLayerTest, CompareWithRefs) {
Exec();
}
template <element::Type_t IN_ET>
std::vector<BinaryConvolutionParams> generateBinaryConvolutionParams() {
using T = typename element_type_traits<IN_ET>::value_type;
std::vector<BinaryConvolutionParams> binaryConvolutionParams {
// --------------------- 2D BinaryConvolution ------------------------------------------
BinaryConvolutionParams(PartialShape {1, 1, 4, 4},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 2, 2},
IN_ET,
std::vector<T>{1, 0, 0, 1,
1, 1, 0, 0,
0, 0, 0, 1,
1, 0, 1, 1},
std::vector<uint8_t>{0xAA, 0x80}, // 10101010 10000000
std::vector<T>{1, 1,
3, -1},
{1, 1},
{0, 0},
{0, 0},
{1, 1}),
BinaryConvolutionParams(PartialShape {1, 1, 4, 4},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 4, 4},
IN_ET,
std::vector<T>{1, 0, 0, 1,
1, 1, 0, 0,
0, 0, 0, 1,
1, 0, 1, 1},
std::vector<uint8_t>{0xAA, 0x80}, // 10101010 10000000
std::vector<T>{1, -3, -1, 1,
-3, 1, 1, -5,
-3, 3, -1, 1,
1, -5, 1, -3},
{1, 1},
{1, 1},
{1, 1},
{1, 1}),
BinaryConvolutionParams(PartialShape {1, 1, 4, 4},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 4, 4},
IN_ET,
std::vector<T>{1, 0, 0, 1,
1, 1, 0, 0,
0, 0, 0, 1,
1, 0, 1, 1},
std::vector<uint8_t>{0xAA, 0x80}, // 10101010 10000000
std::vector<T>{3, -1, 1, 3,
-1, 1, 1, -3,
-1, 3, -1, 3,
3, -3, 3, -1},
{1, 1},
{1, 1},
{1, 1},
{1, 1},
1.0f),
BinaryConvolutionParams(PartialShape {1, 1, 5, 5},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 2, 2},
IN_ET,
std::vector<T>{0, 1, 1, 0, 1,
1, 1, 0, 1, 0,
0, 0, 1, 0, 1,
1, 1, 0, 1, 0,
0, 0, 1, 1, 1},
std::vector<uint8_t>{0x2E, 0x00}, // 10101010 10000000
std::vector<T>{-1, 3,
1, 1},
{2, 2},
{0, 0},
{0, 0},
{1, 1}),
BinaryConvolutionParams(PartialShape {1, 1, 7, 7},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 3, 3},
IN_ET,
std::vector<T>{1, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0,
1, 1, 1, 1, 0, 1, 1,
0, 0, 0, 1, 1, 1, 0,
0, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0,
1, 1, 1, 0, 1, 0, 0},
std::vector<uint8_t>{0x6B, 0x00}, // 10101010 10000000
std::vector<T>{-5, -3, -5,
5, 1, 3,
-1, -1, 3},
{1, 1},
{0, 0},
{0, 0},
{2, 2}),
BinaryConvolutionParams(PartialShape {1, 1, 7, 7},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 4, 4},
IN_ET,
std::vector<T>{1, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0,
1, 1, 1, 1, 0, 1, 1,
0, 0, 0, 1, 1, 1, 0,
0, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0,
1, 1, 1, 0, 1, 0, 0},
std::vector<uint8_t>{0x6B, 0x00}, // 10101010 10000000
std::vector<T>{1, 1, -1, 1,
1, -5, -5, 5,
3, -1, 3, 3,
-1, -1, 3, -3},
{2, 2},
{2, 2},
{2, 2},
{2, 2}),
BinaryConvolutionParams(PartialShape {1, 1, 7, 7},
Shape {1, 1, 3, 3},
PartialShape {1, 1, 4, 4},
IN_ET,
std::vector<T>{1, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0,
1, 1, 1, 1, 0, 1, 1,
0, 0, 0, 1, 1, 1, 0,
0, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0,
1, 1, 1, 0, 1, 0, 0},
std::vector<uint8_t>{0x6B, 0x00}, // 10101010 10000000
std::vector<T>{3, 3, 1, -1,
-1, -5, -5, 3,
1, -1, 3, 1,
-3, 1, 5, -1},
{2, 2},
{2, 2},
{2, 2},
{2, 2},
1.0f),
BinaryConvolutionParams(PartialShape {1, 2, 4, 4},
Shape {1, 2, 3, 3},
PartialShape {1, 1, 2, 2},
IN_ET,
std::vector<T>{
// channel 1
1, 0, 0, 1,
1, 1, 0, 0,
0, 0, 0, 1,
1, 0, 1, 1,
// channel 2
0, 1, 1, 0,
0, 0, 1, 1,
1, 1, 1, 0,
0, 1, 0, 0},
std::vector<uint8_t>{0xAA, 0xAA, 0x80}, // 10101010 10000000
std::vector<T>{2, 2,
6, -2},
{1, 1},
{0, 0},
{0, 0},
{1, 1}),
BinaryConvolutionParams(PartialShape {2, 1, 4, 4},
Shape {1, 1, 3, 3},
PartialShape {2, 1, 2, 2},
IN_ET,
std::vector<T>{
// batch 1
1, 0, 0, 1,
1, 1, 0, 0,
0, 0, 0, 1,
1, 0, 1, 1,
// batch 2
0, 0, 0, 0,
1, 1, 1, 0,
1, 1, 0, 1,
1, 0, 1, 0},
std::vector<uint8_t>{0xAA, 0x80}, // 10101010 10000000
std::vector<T>{
// batch 1
1, 1,
3, -1,
// batch 2
-3, 3,
5, -7},
{1, 1},
{0, 0},
{0, 0},
{1, 1})
};
return binaryConvolutionParams;
}
std::vector<BinaryConvolutionParams> generateBinaryConvolutionCombinedParams() {
const std::vector<std::vector<BinaryConvolutionParams>> binaryConvolutionTypeParams {
generateBinaryConvolutionParams<element::Type_t::f32>(),
generateBinaryConvolutionParams<element::Type_t::f16>(),
generateBinaryConvolutionParams<element::Type_t::i64>(),
generateBinaryConvolutionParams<element::Type_t::i32>()
};
std::vector<BinaryConvolutionParams> combinedParams;
for (const auto& params : binaryConvolutionTypeParams) {
combinedParams.insert(combinedParams.end(), params.begin(), params.end());
}
return combinedParams;
}
INSTANTIATE_TEST_SUITE_P(smoke_BinaryConvolution_With_Hardcoded_Refs, ReferenceBinaryConvolutionLayerTest,
testing::ValuesIn(generateBinaryConvolutionCombinedParams()), ReferenceBinaryConvolutionLayerTest::getTestCaseName);
} // namespace

File diff suppressed because it is too large Load Diff

View File

@ -268,6 +268,7 @@ set(SRC
visitors/op/atanh.cpp
visitors/op/batch_norm.cpp
visitors/op/batch_to_space.cpp
visitors/op/binary_convolution.cpp
visitors/op/broadcast.cpp
visitors/op/bucketize.cpp
visitors/op/ceiling.cpp
@ -464,8 +465,6 @@ set(MULTI_TEST_SRC
backend/ceiling.in.cpp
backend/concat.in.cpp
backend/constant.in.cpp
backend/convolution_backprop.in.cpp
backend/binary_convolution.in.cpp
backend/ctc_greedy_decoder.in.cpp
backend/ctc_greedy_decoder_seq_len.in.cpp
backend/deformable_psroi_pooling.in.cpp

View File

@ -1,646 +0,0 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "engines_util/execute_tools.hpp"
#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "ngraph/runtime/tensor.hpp"
#include "runtime/backend.hpp"
#include "util/all_close.hpp"
#include "util/all_close_f.hpp"
#include "util/test_control.hpp"
using namespace std;
using namespace ngraph;
static string s_manifest = "${MANIFEST}";
template <typename T_IN, typename T_KERN>
static void BinaryConvolutionTest(const std::vector<T_IN>& inputs,
const Shape inputs_shape,
const std::vector<T_KERN>& filters,
const Shape filters_shape,
const std::vector<T_IN>& outputs,
const Shape outputs_shape,
const Strides& strides,
const CoordinateDiff& padding,
const Strides& dilations,
const float pad_value = 0.0f) {
const CoordinateDiff pads_begin{padding};
const CoordinateDiff pads_end{padding};
const op::PadType auto_pad{op::PadType::EXPLICIT};
auto inputs_param = make_shared<op::Parameter>(element::from<T_IN>(), inputs_shape);
auto filters_const = make_shared<op::Constant>(element::u1, filters_shape, &filters[0]);
auto bin_conv =
make_shared<op::v1::BinaryConvolution>(inputs_param,
filters_const,
strides,
pads_begin,
pads_end,
dilations,
op::v1::BinaryConvolution::BinaryConvolutionMode::XNOR_POPCOUNT,
pad_value,
auto_pad);
auto f = make_shared<Function>(bin_conv, ParameterVector{inputs_param});
auto backend = runtime::Backend::create("${BACKEND_NAME}");
auto input_tensor = backend->create_tensor(element::from<T_IN>(), inputs_shape);
copy_data(input_tensor, inputs);
auto result = backend->create_tensor(element::from<T_IN>(), outputs_shape);
auto handle = backend->compile(f);
handle->call_with_validate({result}, {input_tensor});
EXPECT_TRUE(test::all_close_f((outputs), read_vector<T_IN>(result), MIN_FLOAT_TOLERANCE_BITS));
}
template <typename T_IN>
static void ConvolutionTest(const std::vector<T_IN>& inputs,
const Shape inputs_shape,
const std::vector<T_IN>& filters,
const Shape filters_shape,
const std::vector<T_IN>& outputs,
const Shape outputs_shape,
const Strides& strides,
const CoordinateDiff& padding,
const Strides& dilations) {
const CoordinateDiff pads_begin{padding};
const CoordinateDiff pads_end{padding};
const op::PadType auto_pad{op::PadType::EXPLICIT};
auto inputs_param = make_shared<op::Parameter>(element::from<T_IN>(), inputs_shape);
auto filters_param = make_shared<op::Parameter>(element::from<T_IN>(), filters_shape);
auto conv = make_shared<op::v1::Convolution>(inputs_param,
filters_param,
strides,
pads_begin,
pads_end,
dilations,
auto_pad);
auto f = make_shared<Function>(conv, ParameterVector{inputs_param, filters_param});
auto backend = runtime::Backend::create("${BACKEND_NAME}");
auto input_tensor = backend->create_tensor(element::from<T_IN>(), inputs_shape);
copy_data(input_tensor, inputs);
auto filters_tensor = backend->create_tensor(element::from<T_IN>(), filters_shape);
copy_data(filters_tensor, filters);
auto result = backend->create_tensor(element::from<T_IN>(), outputs_shape);
auto handle = backend->compile(f);
handle->call_with_validate({result}, {input_tensor, filters_tensor});
EXPECT_TRUE(test::all_close_f((outputs), read_vector<T_IN>(result), MIN_FLOAT_TOLERANCE_BITS));
}
// clang-format off
// --------------------- 2D convolution ------------------------------------------
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_1batch_1channel)
{
const Strides strides{1, 1};
const CoordinateDiff padding{0, 0};
const Strides dilations{1, 1};
const Shape inputs_shape{1, 1, 4, 4};
const std::vector<float> inputs_conv{1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f};
const std::vector<float> inputs_bin_conv{1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f};
const std::vector<uint8_t> filters_bin_conv{0xAA, 0x80}; // 10101010 10000000
const Shape outputs_shape{1, 1, 2, 2};
const std::vector<float> outputs{1.0f, 1.0f,
3.0f, -1.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
ConvolutionTest(
inputs_conv,
inputs_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_1batch_1channel_padding_pad_val_0)
{
const Strides strides{1, 1};
const Strides dilations{1, 1};
const CoordinateDiff padding_conv{0, 0};
const Shape inputs_conv_shape{1, 1, 6, 6};
const std::vector<float> inputs_conv{-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f};
const CoordinateDiff padding_bin_conv{1, 1};
const Shape inputs_bin_conv_shape{1, 1, 4, 4};
const std::vector<float> inputs_bin_conv{1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f};
const std::vector<uint8_t> filters_bin_conv{0xAA, 0x80}; // 10101010 10000000
const Shape outputs_shape{1, 1, 4, 4};
const std::vector<float> outputs{1.0f, -3.0f, -1.0f, 1.0f,
-3.0f, 1.0f, 1.0f, -5.0f,
-3.0f, 3.0f, -1.0f, 1.0f,
1.0f, -5.0f, 1.0f, -3.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_bin_conv_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_bin_conv,
dilations);
ConvolutionTest(
inputs_conv,
inputs_conv_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_conv,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_1batch_1channel_padding_pad_val_1)
{
const Strides strides{1, 1};
const Strides dilations{1, 1};
const float pad_value = 1.0f;
const CoordinateDiff padding_conv{0, 0};
const Shape inputs_conv_shape{1, 1, 6, 6};
const std::vector<float> inputs_conv{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
const CoordinateDiff padding_bin_conv{1, 1};
const Shape inputs_bin_conv_shape{1, 1, 4, 4};
const std::vector<float> inputs_bin_conv{1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f};
const std::vector<uint8_t> filters_bin_conv{0xAA, 0x80}; // 10101010 10000000
const Shape outputs_shape{1, 1, 4, 4};
const std::vector<float> outputs{3.0f, -1.0f, 1.0f, 3.0f,
-1.0f, 1.0f, 1.0f, -3.0f,
-1.0f, 3.0f, -1.0f, 3.0f,
3.0f, -3.0f, 3.0f, -1.0f,};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_bin_conv_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_bin_conv,
dilations,
pad_value);
ConvolutionTest(
inputs_conv,
inputs_conv_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_conv,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_1batch_1channel_stride)
{
const Strides strides{2, 2};
const CoordinateDiff padding{0, 0};
const Strides dilations{1, 1};
const Shape inputs_shape{1, 1, 5, 5};
const std::vector<float> inputs_conv{-1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f};
const std::vector<float> inputs_bin_conv{0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f};
const std::vector<uint8_t> filters_bin_conv{0x2E, 0x00}; // 00101110 00000000
const Shape outputs_shape{1, 1, 2, 2};
const std::vector<float> outputs{-1.0f, 3.0f,
1.0f, 1.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
ConvolutionTest(
inputs_conv,
inputs_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_1batch_1channel_dilation)
{
const Strides strides{1, 1};
const CoordinateDiff padding{0, 0};
const Strides dilations{2, 2};
const Shape inputs_shape{1, 1, 7, 7};
const std::vector<float> inputs_conv{1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f};
const std::vector<float> inputs_bin_conv{1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f};
const std::vector<uint8_t> filters_bin_conv{0x6B, 0x00}; // 01101011 00000000
const Shape outputs_shape{1, 1, 3, 3};
const std::vector<float> outputs{-5.0f, -3.0f, -5.0f,
5.0f, 1.0f, 3.0f,
-1.0f, -1.0f, 3.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
ConvolutionTest(
inputs_conv,
inputs_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME},
bin_convolution_2D_1batch_1channel_strides_dilation_padding_pad_val_0)
{
const Strides strides{2, 2};
const Strides dilations{2, 2};
const CoordinateDiff padding_conv{0, 0};
const Shape inputs_conv_shape{1, 1, 11, 11};
const std::vector<float> inputs_conv{
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,-1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f};
const CoordinateDiff padding_bin_conv{2, 2};
const Shape inputs_bin_conv_shape{1, 1, 7, 7};
const std::vector<float> inputs_bin_conv{1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f};
const std::vector<uint8_t> filters_bin_conv{0x6B, 0x00}; // 01101011 00000000
const Shape outputs_shape{1, 1, 4, 4};
const std::vector<float> outputs{1.0f, 1.0f, -1.0f, 1.0f,
1.0f, -5.0f, -5.0f, 5.0f,
3.0f, -1.0f, 3.0f, 3.0f,
-1.0f, -1.0f, 3.0f, -3.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_bin_conv_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_bin_conv,
dilations);
ConvolutionTest(
inputs_conv,
inputs_conv_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_conv,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME},
bin_convolution_2D_1batch_1channel_strides_dilation_padding_pad_val_1)
{
const Strides strides{2, 2};
const Strides dilations{2, 2};
const float pad_value = 1.0f;
const CoordinateDiff padding_conv{0, 0};
const Shape inputs_conv_shape{1, 1, 11, 11};
const std::vector<float> inputs_conv{
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
const CoordinateDiff padding_bin_conv{2, 2};
const Shape inputs_bin_conv_shape{1, 1, 7, 7};
const std::vector<float> inputs_bin_conv{1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f};
const std::vector<uint8_t> filters_bin_conv{0x6B, 0x00}; // 01101011 00000000
const Shape outputs_shape{1, 1, 4, 4};
const std::vector<float> outputs{3.0f, 3.0f, 1.0f, -1.0f,
-1.0f, -5.0f, -5.0f, 3.0f,
1.0f, -1.0f, 3.0f, 1.0f,
-3.0f, 1.0f, 5.0f, -1.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_bin_conv_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_bin_conv,
dilations,
pad_value);
ConvolutionTest(
inputs_conv,
inputs_conv_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding_conv,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_1batch_2channel)
{
const Strides strides{1, 1};
const CoordinateDiff padding{0, 0};
const Strides dilations{1, 1};
const Shape inputs_shape{1, 2, 4, 4};
const std::vector<float> inputs_conv{
// channel 1
1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
// channel 2
-1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, -1.0f};
const std::vector<float> inputs_bin_conv{
// channel 1
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
// channel 2
0.0f, 1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f};
const Shape filters_shape{1, 2, 3, 3};
const std::vector<float> filters_conv{
// channel 1
1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
// channel 2
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f};
// 10101010 10101010 10000000
const std::vector<uint8_t> filters_bin_conv{0xAA, 0xAA, 0x80};
const Shape outputs_shape{1, 1, 2, 2};
const std::vector<float> outputs{2.0f, 2.0f,
6.0f, -2.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
ConvolutionTest(
inputs_conv,
inputs_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
}
NGRAPH_TEST(${BACKEND_NAME}, bin_convolution_2D_2batch_1channel)
{
const Strides strides{1, 1};
const CoordinateDiff padding{0, 0};
const Strides dilations{1, 1};
const Shape inputs_shape{2, 1, 4, 4};
const std::vector<float> inputs_conv{
// batch 1
1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
// batch 2
-1.0f, -1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f, -1.0f};
const std::vector<float> inputs_bin_conv{
// batch 1
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
// batch 2
0.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f};
const Shape filters_shape{1, 1, 3, 3};
const std::vector<float> filters_conv{1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f};
const std::vector<uint8_t> filters_bin_conv{0xAA, 0x80}; // 10101010 10000000
const Shape outputs_shape{2, 1, 2, 2};
const std::vector<float> outputs{
// batch 1
1.0f, 1.0f,
3.0f, -1.0f,
// batch 2
-3.0f, 3.0f,
5.0f, -7.0f};
BinaryConvolutionTest(
inputs_bin_conv,
inputs_shape,
filters_bin_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
ConvolutionTest(
inputs_conv,
inputs_shape,
filters_conv,
filters_shape,
outputs,
outputs_shape,
strides,
padding,
dilations);
}
// clang-format on

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,57 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "ngraph/op/util/attr_types.hpp"
#include "ngraph/opsets/opset1.hpp"
#include "ngraph/opsets/opset3.hpp"
#include "ngraph/opsets/opset4.hpp"
#include "ngraph/opsets/opset5.hpp"
#include "util/visitor.hpp"
using namespace std;
using namespace ngraph;
using ngraph::test::NodeBuilder;
using ngraph::test::ValueMap;
TEST(attributes, bin_convolution) {
NodeBuilder::get_ops().register_factory<op::v1::Convolution>();
const PartialShape data_batch_shape{1, 1, 5, 5};
const PartialShape filters_shape{1, 1, 3, 3};
Strides strides{1, 1};
CoordinateDiff pads_begin{0, 0};
CoordinateDiff pads_end{0, 0};
Strides dilations{1, 1};
const auto mode = op::v1::BinaryConvolution::BinaryConvolutionMode::XNOR_POPCOUNT;
const float pad_value = 1.0f;
const auto auto_pad = op::PadType::SAME_LOWER;
auto data_batch = make_shared<op::Parameter>(element::f32, data_batch_shape);
auto filters = make_shared<op::Parameter>(element::u1, filters_shape);
auto conv = make_shared<op::v1::BinaryConvolution>(data_batch,
filters,
strides,
pads_begin,
pads_end,
dilations,
mode,
pad_value,
auto_pad);
NodeBuilder builder(conv);
auto g_convolution = ov::as_type_ptr<op::v1::BinaryConvolution>(builder.create());
// attribute count
const auto expected_attr_count = 7;
EXPECT_EQ(builder.get_value_map_size(), expected_attr_count);
EXPECT_EQ(g_convolution->get_strides(), conv->get_strides());
EXPECT_EQ(g_convolution->get_pads_begin(), conv->get_pads_begin());
EXPECT_EQ(g_convolution->get_pads_end(), conv->get_pads_end());
EXPECT_EQ(g_convolution->get_dilations(), conv->get_dilations());
EXPECT_EQ(g_convolution->get_auto_pad(), conv->get_auto_pad());
EXPECT_EQ(g_convolution->get_mode(), conv->get_mode());
EXPECT_EQ(g_convolution->get_pad_value(), conv->get_pad_value());
}