Skip to content

Commit

Permalink
[CPU] [TESTS] added int8 deconvolution tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonvor committed May 12, 2021
1 parent 4a6567a commit a2b0c40
Show file tree
Hide file tree
Showing 10 changed files with 595 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ const std::vector<fusingSpecificParams> fusingParamsSetBF16{
fusingSum
};

const std::vector<fusingSpecificParams> fusingParamsSetI8{
emptyFusingSpec,
// activations
fusingRelu,
fusingElu,
fusingSigmoid,
fusingClamp,
fusingPRelu,
// todo: [antonvor] not supported yet
// fusingSwish,
fusingHSwish,
fusingMish,
// other patterns
fusingReluScaleShift,
fusingFakeQuantizePerTensorRelu,
fusingFakeQuantizePerChannelRelu,
// todo: [antonvor] not supported yet
// fusingSumEluFQ,
// fusingSum
};

const std::map<std::string, std::string> cpuEmptyPluginConfig;
const std::map<std::string, std::string> cpuBF16PluginConfig = { { PluginConfigParams::KEY_ENFORCE_BF16, PluginConfigParams::YES } };

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "shared_test_classes/base/layer_test_utils.hpp"
#include <exec_graph_info.hpp>
#include "ie_system_conf.h"
#include "ngraph_ops/type_relaxed.hpp"

namespace CPUTestUtils {
typedef enum {
Expand Down Expand Up @@ -146,6 +147,36 @@ class CPUTestsBase {
std::string selectedType;
};

template <typename NodeType>
class ConvertPrecision : public ngraph::pass::GraphRewrite {
class ConvertLayerOutPrecision : public ngraph::pass::MatcherPass {
public:
ConvertLayerOutPrecision() {
auto node = ngraph::pattern::wrap_type<NodeType>();

ngraph::matcher_pass_callback callback = [](ngraph::pattern::Matcher &m) {
auto node = std::dynamic_pointer_cast<NodeType>(m.get_match_root());
if (node) {
auto newNode = std::make_shared<ngraph::op::TypeRelaxed<NodeType>>(
*ngraph::as_type_ptr<ngraph::op::TypeRelaxed<NodeType>>(node), ngraph::element::f32);
newNode->set_friendly_name(node->get_friendly_name());
ngraph::replace_node(node, newNode);
return true;
}
return false;
};

auto m = std::make_shared<ngraph::pattern::Matcher>(node, "ConvertLayerOutPrecision");
register_matcher(m, callback);
}
};

public:
ConvertPrecision() {
add_matcher<ConvertLayerOutPrecision>();
}
};

const auto emptyCPUSpec = CPUSpecificParams{{}, {}, {}, {}};

const auto conv_ref_2D = CPUSpecificParams{{nchw}, {nchw}, {"ref_any"}, "ref_any"};
Expand All @@ -168,6 +199,9 @@ const auto conv_avx512_2D = CPUSpecificParams{{nChw16c}, {nChw16c}, {"jit_avx512
const auto conv_avx512_3D = CPUSpecificParams{{nCdhw16c}, {nCdhw16c}, {"jit_avx512"}, "jit_avx512"};

const auto conv_avx512_2D_I8 = CPUSpecificParams{{nhwc}, {nhwc}, {"jit_avx512"}, "jit_avx512"};
const auto conv_avx512_3D_I8 = CPUSpecificParams{{ndhwc}, {ndhwc}, {"jit_avx512"}, "jit_avx512"};
const auto conv_avx512_2D_1x1_I8 = CPUSpecificParams{{nhwc}, {nhwc}, {"jit_avx512_1x1"}, "jit_avx512_1x1"};
const auto conv_avx512_3D_1x1_I8 = CPUSpecificParams{{ndhwc}, {ndhwc}, {"jit_avx512_1x1"}, "jit_avx512_1x1"};

const auto conv_avx512_dw_2D = CPUSpecificParams{{nChw16c}, {nChw16c}, {"jit_avx512_dw"}, "jit_avx512_dw"};
const auto conv_avx512_dw_3D = CPUSpecificParams{{nCdhw16c}, {nCdhw16c}, {"jit_avx512_dw"}, "jit_avx512_dw"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class LayerTestsCommon : public CommonTestUtils::TestsCommon {

template<class T>
static void Compare(const T *expected, const T *actual, std::size_t size, T threshold) {
// for (std::size_t i = 0; i < size; ++i) {
// const auto &ref = expected[i];
// const auto &res = actual[i];
// std::cout << i << ". ref = " << ref << ", res = " << res << std::endl;
// }
for (std::size_t i = 0; i < size; ++i) {
const auto &ref = expected[i];
const auto &res = actual[i];
Expand Down Expand Up @@ -143,6 +148,8 @@ class LayerTestsCommon : public CommonTestUtils::TestsCommon {

InferenceEngine::InferRequest inferRequest;

std::vector<std::shared_ptr<ngraph::pass::GraphRewrite>> additionalPasses;

private:
RefMode refMode = RefMode::INTERPRETER;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ std::vector<std::vector<std::uint8_t>> LayerTestsCommon::CalculateRefs() {
ngraph::pass::ConvertPrecision<ngraph::element::Type_t::f16, ngraph::element::Type_t::f32>().run_on_function(function);
ngraph::pass::ConvertPrecision<ngraph::element::Type_t::bf16, ngraph::element::Type_t::f32>().run_on_function(function);

for (const auto &pass : additionalPasses)
pass->run_on_function(function);

function->validate_nodes_and_infer_types();

auto referenceInputs = std::vector<std::vector<std::uint8_t>>(inputs.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ addIeTarget(
INCLUDES
PUBLIC
${PUBLIC_HEADERS_DIR}
${IE_MAIN_SOURCE_DIR}/src/transformations/include
ADDITIONAL_SOURCE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/src
LINK_LIBRARIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <ngraph/opsets/opset7.hpp>

#include "ngraph_functions/utils/data_utils.hpp"
#include "ngraph_ops/type_relaxed.hpp"

namespace ngraph {
namespace builder {
Expand Down Expand Up @@ -90,6 +91,19 @@ std::shared_ptr<ngraph::Node> makeConvolution(const ngraph::Output<Node> &in,
const std::vector<float> &filterWeights = {},
const std::vector<float> &biasesWeights = {});

std::shared_ptr<ngraph::Node> makeConvolutionRelaxed(const ngraph::Output<Node> &in,
const element::Type &type,
const std::vector<size_t> &filterSize,
const std::vector<size_t> &strides,
const std::vector<ptrdiff_t> &padsBegin,
const std::vector<ptrdiff_t> &padsEnd,
const std::vector<size_t> &dilations,
const op::PadType &autoPad,
size_t numOutChannels,
bool addBiases = false,
const std::vector<float> &filterWeights = {},
const std::vector<float> &biasesWeights = {});

std::shared_ptr<ngraph::Node> makeGroupConvolution(const ngraph::Output<Node> &in,
const element::Type &type,
const std::vector<size_t> &filterSize,
Expand Down Expand Up @@ -139,6 +153,20 @@ std::shared_ptr<ngraph::Node> makeConvolutionBackpropData(const ngraph::Output<N
bool addBiases = false,
const std::vector<float> &biasesWeights = {});

std::shared_ptr<ngraph::Node> makeConvolutionBackpropDataRelaxed(const ngraph::Output<Node> &in,
const element::Type &weiType,
const element::Type &outType,
const std::vector<size_t> &filterSize,
const std::vector<size_t> &strides,
const std::vector<ptrdiff_t> &padsBegin,
const std::vector<ptrdiff_t> &padsEnd,
const std::vector<size_t> &dilations,
const op::PadType &autoPad,
size_t numOutChannels,
bool addBiases = false,
const std::vector<float> &filterWeights = {},
const std::vector<float> &biasesWeights = {});

std::shared_ptr<ngraph::Node> makeCTCGreedyDecoder(
const ngraph::Output<Node>& inputData,
const bool mergeRepeated);
Expand Down Expand Up @@ -193,6 +221,21 @@ std::shared_ptr<ngraph::Node> makeGroupConvolutionBackpropData(const ngraph::Out
bool addBiases = false,
const std::vector<float> &biasesWeights = {});

std::shared_ptr<Node> makeGroupConvolutionBackpropDataRelaxed(const ngraph::Output<Node> &in,
const element::Type &weiType,
const element::Type &outType,
const std::vector<size_t> &filterSize,
const std::vector<size_t> &strides,
const std::vector<ptrdiff_t> &padsBegin,
const std::vector<ptrdiff_t> &padsEnd,
const std::vector<size_t> &dilations,
const op::PadType &autoPad,
size_t numOutChannels,
size_t numGroups,
bool addBiases = false,
const std::vector<float> &filterWeights = {},
const std::vector<float> &biasesWeights = {});

std::shared_ptr<ngraph::Node> makeBinaryConvolution(const ngraph::Output<Node> &in,
const std::vector<size_t> &filterSize,
const std::vector<size_t> &strides,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,45 @@ std::shared_ptr<Node> makeConvolutionBackpropData(const ngraph::Output<Node> &in
}
}

std::shared_ptr<Node> makeConvolutionBackpropDataRelaxed(const ngraph::Output<Node> &in,
const element::Type &weiType,
const element::Type &outType,
const std::vector<size_t> &filterSize,
const std::vector<size_t> &strides,
const std::vector<ptrdiff_t> &padsBegin,
const std::vector<ptrdiff_t> &padsEnd,
const std::vector<size_t> &dilations,
const op::PadType &autoPad,
size_t numOutChannels,
bool addBiases,
const std::vector<float> &filterWeights,
const std::vector<float> &biasesWeights) {
auto inputParamsFP32 = ngraph::builder::makeParams(ngraph::element::f32, { in.get_shape() });
auto paramOutsFP32 = ngraph::helpers::convert2OutputVector(ngraph::helpers::castOps2Nodes<ngraph::op::Parameter>(inputParamsFP32));

auto deconvolutionNodeRelaxed = std::make_shared<op::TypeRelaxed<opset1::ConvolutionBackpropData>>(
*as_type_ptr<opset1::ConvolutionBackpropData>(ngraph::builder::makeConvolutionBackpropData(
paramOutsFP32.front(), ngraph::element::f32, filterSize, strides, padsBegin, padsEnd, dilations, autoPad, numOutChannels)),
outType);

bool randomFilterWeights = filterWeights.empty();
auto shape = in.get_shape();
std::vector<size_t> filterWeightsShape = {shape[1], numOutChannels};
filterWeightsShape.insert(filterWeightsShape.end(), filterSize.begin(), filterSize.end());
auto filterWeightsNode = makeConstant(weiType, filterWeightsShape, filterWeights, randomFilterWeights);

auto newDeconvolution = deconvolutionNodeRelaxed->copy_with_new_inputs(
{in, filterWeightsNode});

if (addBiases) {
bool randomBiases = biasesWeights.empty();
auto biasesWeightsNode = makeConstant(outType, {}, biasesWeights, randomBiases);
auto add = std::make_shared<ngraph::opset1::Add>(newDeconvolution, biasesWeightsNode);
return add;
} else {
return newDeconvolution;
}
}

} // namespace builder
} // namespace ngraph
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,49 @@ std::shared_ptr<Node> makeGroupConvolutionBackpropData(const ngraph::Output<Node
}
}

std::shared_ptr<Node> makeGroupConvolutionBackpropDataRelaxed(const ngraph::Output<Node> &in,
const element::Type &weiType,
const element::Type &outType,
const std::vector<size_t> &filterSize,
const std::vector<size_t> &strides,
const std::vector<ptrdiff_t> &padsBegin,
const std::vector<ptrdiff_t> &padsEnd,
const std::vector<size_t> &dilations,
const op::PadType &autoPad,
size_t numOutChannels,
size_t numGroups,
bool addBiases,
const std::vector<float> &filterWeights,
const std::vector<float> &biasesWeights) {
auto inputParamsFP32 = ngraph::builder::makeParams(ngraph::element::f32, { in.get_shape() });
auto paramOutsFP32 = ngraph::helpers::convert2OutputVector(ngraph::helpers::castOps2Nodes<ngraph::op::Parameter>(inputParamsFP32));

auto groupDeconvolutionNodeRelaxed = std::make_shared<op::TypeRelaxed<opset1::GroupConvolutionBackpropData>>(
*as_type_ptr<opset1::GroupConvolutionBackpropData>(ngraph::builder::makeGroupConvolutionBackpropData(
paramOutsFP32.front(), ngraph::element::f32, filterSize, strides, padsBegin, padsEnd, dilations, autoPad, numOutChannels, numGroups)),
outType);

bool randomFilterWeights = filterWeights.empty();
auto shape = in.get_shape();
std::vector<size_t> filterWeightsShape = {shape[1], numOutChannels};
filterWeightsShape[0] /= numGroups;
filterWeightsShape[1] /= numGroups;
filterWeightsShape.insert(filterWeightsShape.begin(), numGroups);
filterWeightsShape.insert(filterWeightsShape.end(), filterSize.begin(), filterSize.end());
auto filterWeightsNode = makeConstant(weiType, filterWeightsShape, filterWeights, randomFilterWeights);

auto newGroupDeconvolution = groupDeconvolutionNodeRelaxed->copy_with_new_inputs(
{in, filterWeightsNode});

if (addBiases) {
bool randomBiases = biasesWeights.empty();
auto biasesWeightsNode = makeConstant(outType, {}, biasesWeights, randomBiases);
auto add = std::make_shared<ngraph::opset1::Add>(newGroupDeconvolution, biasesWeightsNode);
return add;
} else {
return newGroupDeconvolution;
}
}

} // namespace builder
} // namespace ngraph

0 comments on commit a2b0c40

Please sign in to comment.