Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CPU] Add Roll support #5112

Merged
merged 4 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inference-engine/src/mkldnn_plugin/mkldnn_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ static const InferenceEngine::details::caseless_unordered_map<std::string, Type>
{ "ReduceSum", ReduceSum},
{ "ReduceSumSquare", ReduceSumSquare},
{ "Erf", Eltwise },
{ "Roll", Roll },
};

Type TypeFromName(const std::string type) {
Expand Down
5 changes: 4 additions & 1 deletion inference-engine/src/mkldnn_plugin/mkldnn_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ enum Type {
ReduceOr,
ReduceProd,
ReduceSum,
ReduceSumSquare
ReduceSumSquare,
Roll
};

Type TypeFromName(const std::string type);
Expand Down Expand Up @@ -206,6 +207,8 @@ static std::string NameFromType(Type type) {
return "ReduceSum";
case ReduceSumSquare:
return "ReduceSumSquare";
case Roll:
return "Roll";
default:
return "Unknown";
}
Expand Down
214 changes: 214 additions & 0 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_roll_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Copyright (C) 2018-2021 Intel Corporation
dmitry-gorokhov marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: Apache-2.0
//

#include <string>
#include <vector>
#include <cmath>
#include <mkldnn_extension_utils.h>

#include "mkldnn_roll_node.h"
#include "ie_parallel.hpp"
#include "ie_precision.hpp"
#include "mkldnn/ie_mkldnn.h"
#include "utils/general_utils.h"
#include "common/cpu_memcpy.h"

using namespace mkldnn;
using namespace MKLDNNPlugin;
using namespace InferenceEngine;

MKLDNNRollNode::MKLDNNRollNode(const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng, MKLDNNWeightsSharing::Ptr &cache) :
MKLDNNNode(layer, eng, cache) {
const std::string layerErrorPrefix = "Roll layer with name '" + layer->name + "'";
if (layer->insData.size() != numberOfInputs) {
IE_THROW() << layerErrorPrefix << " has incorrect number of input/output edges!";
}

/* Data */
auto data = layer->insData[DATA_INDEX].lock();
if (data == nullptr) {
IE_THROW() << layerErrorPrefix << " has nullable data";
}

const auto &dataTensor = data->getTensorDesc();
const auto &dataShape = dataTensor.getDims();
apertovs marked this conversation as resolved.
Show resolved Hide resolved
const auto &dataPrecision = dataTensor.getPrecision();

if (!MKLDNNPlugin::one_of(dataPrecision, Precision::I8, Precision::U8, Precision::I16, Precision::I32, Precision::FP32, Precision::I64, Precision::BF16)) {
apertovs marked this conversation as resolved.
Show resolved Hide resolved
IE_THROW() << layerErrorPrefix << " has unsupported 'data' input precision: " << dataPrecision.name();
}
if (dataShape.size() < 1) {
IE_THROW() << layerErrorPrefix << " doesn't support 'data' input tensor with rank: " << dataShape.size();
}
numOfDims = dataShape.size();

if (dataShape != layer->outData[0]->getTensorDesc().getDims()) {
IE_THROW() << layerErrorPrefix << " has different 'data' input and output dimensions";
}

/* Axes */
auto axesData = layer->insData[AXES_INDEX].lock();
if (axesData == nullptr) {
IE_THROW() << layerErrorPrefix << " has nullable 'axes' data";
}
const auto& axesTensor = axesData->getTensorDesc();
const auto& axesTensorPrec = axesData->getTensorDesc().getPrecision();
if (axesTensorPrec != Precision::I32 && axesTensorPrec != Precision::I64) {
IE_THROW() << layerErrorPrefix << " has unsupported 'axes' input precision: " << axesTensorPrec.name();
}

const auto axesTensorRank = axesTensor.getDims().size();
if (axesTensorRank > 1) {
IE_THROW() << layerErrorPrefix << " doesn't support 'axes' input tensor with rank: " << axesTensorRank;
}

/* Shift */
auto shiftData = layer->insData[SHIFT_INDEX].lock();
if (shiftData == nullptr) {
IE_THROW() << layerErrorPrefix << " has nullable 'shift' data";
}
const auto& shiftTensor = shiftData->getTensorDesc();
const auto& shiftTensorPrec = shiftData->getTensorDesc().getPrecision();
if (shiftTensorPrec != Precision::I32 && shiftTensorPrec != Precision::I64) {
IE_THROW() << layerErrorPrefix << " has unsupported 'shift' input precision: " << shiftTensorPrec.name();
}

const auto shiftTensorRank = shiftTensor.getDims().size();
if (shiftTensorRank > 1) {
IE_THROW() << layerErrorPrefix << " doesn't support 'shift' input tensor with rank: " << shiftTensorRank;
}

shape = dataShape;
}
void MKLDNNRollNode::getSupportedDescriptors() {}

void MKLDNNRollNode::initSupportedPrimitiveDescriptors() {
if (!supportedPrimitiveDescriptors.empty())
return;

auto inputData = getCnnLayer()->insData[0].lock();

if (inputData == nullptr) {
IE_THROW() << "Roll layer with name '" + getCnnLayer()->name + "'" << " has nullable data";
}

InferenceEngine::Precision precision = inputData->getPrecision();

auto dataType = MKLDNNExtensionUtils::IEPrecisionToDataType(precision);

auto srcDims = getParentEdgeAt(0)->getDims();

auto dataMemoryFormat = MKLDNNMemory::GetPlainFormat(getParentEdgeAt(0)->getDims());
InferenceEngine::LayerConfig config;
config.dynBatchSupport = true;
apertovs marked this conversation as resolved.
Show resolved Hide resolved

auto createDataConfig = [](const MKLDNNDims& dims, memory::data_type dataType) -> InferenceEngine::DataConfig {
InferenceEngine::DataConfig dataConfig;
dataConfig.inPlace = -1;
dataConfig.constant = false;
dataConfig.desc = MKLDNNMemoryDesc(dims, dataType, MKLDNNMemory::GetPlainFormat(dims));
return dataConfig;
};

config.inConfs.push_back(createDataConfig(getParentEdgeAt(0)->getDims(), dataType));
config.inConfs.push_back(createDataConfig(getParentEdgeAt(1)->getDims(), memory::data_type::s32));
config.inConfs.push_back(createDataConfig(getParentEdgeAt(2)->getDims(), memory::data_type::s32));

config.outConfs.push_back(createDataConfig(getChildEdgeAt(0)->getDims(), dataType));

supportedPrimitiveDescriptors.push_back({config, impl_desc_type::ref, dataMemoryFormat});
}


void MKLDNNRollNode::execute(mkldnn::stream strm) {
auto input = getParentEdgeAt(DATA_INDEX)->getBlob();
apertovs marked this conversation as resolved.
Show resolved Hide resolved
auto shifts = getParentEdgeAt(SHIFT_INDEX)->getBlob();
auto axes = getParentEdgeAt(AXES_INDEX)->getBlob();
auto output = getChildEdgeAt(0)->getBlob();
const auto dataPrecision = getInputPrecisions()[0];
apertovs marked this conversation as resolved.
Show resolved Hide resolved
const auto& dataTypeSize = dataPrecision.size();
switch (dataTypeSize) {
case sizeof(PrecisionTrait<Precision::I8>::value_type): {
rollImpl<PrecisionTrait<Precision::I8>::value_type>(input, shifts, axes, output);
break;
}
case sizeof(PrecisionTrait<Precision::I16>::value_type): {
rollImpl<PrecisionTrait<Precision::I16>::value_type>(input, shifts, axes, output);
break;
}
case sizeof(PrecisionTrait<Precision::I32>::value_type): {
rollImpl<PrecisionTrait<Precision::I32>::value_type>(input, shifts, axes, output);
break;
}
case sizeof(PrecisionTrait<Precision::I64>::value_type): {
rollImpl<PrecisionTrait<Precision::I64>::value_type>(input, shifts, axes, output);
apertovs marked this conversation as resolved.
Show resolved Hide resolved
break;
}
default:
IE_THROW() << "Roll has unsupported 'data' input precision: " << dataPrecision.name();
apertovs marked this conversation as resolved.
Show resolved Hide resolved
}
}

size_t MKLDNNRollNode::calculateShiftOffset(size_t dataOffset, size_t dimShift, size_t segmentSize, size_t dimSize) {
size_t pos = dataOffset / segmentSize % dimSize;
size_t shift = (pos + dimShift) % dimSize - pos;
return dataOffset + shift * segmentSize;
}

template <typename DataType>
void MKLDNNRollNode::rollImpl(const Blob::CPtr &inputBlob, const Blob::CPtr &shiftsBlob, const Blob::CPtr &axesBlob, const Blob::Ptr &outputBlob) {
const auto *axes = axesBlob->cbuffer().as<const int32_t*>() + axesBlob->getTensorDesc().getBlockingDesc().getOffsetPadding();
const auto *shifts = shiftsBlob->cbuffer().as<const int32_t *>() + shiftsBlob->getTensorDesc().getBlockingDesc().getOffsetPadding();

const auto *input =
inputBlob->cbuffer().as<const DataType *>() + inputBlob->getTensorDesc().getBlockingDesc().getOffsetPadding();
auto *output = outputBlob->buffer().as<DataType *>() + outputBlob->getTensorDesc().getBlockingDesc().getOffsetPadding();

std::vector<size_t> shiftsVector(numOfDims, 0);
for (size_t dim = 0; dim < axesBlob->size(); ++dim) {
int32_t currentAxis = axes[dim] < 0 ? axes[dim] + numOfDims : axes[dim];
int32_t shiftSum = shiftsVector[currentAxis] + shifts[dim];
int32_t dimSize = shape[currentAxis];
shiftsVector[currentAxis] = (shiftSum % dimSize + dimSize) % dimSize;
}

const size_t blockSize = shape.back();
const size_t totalElements = inputBlob->size();
const size_t leftBlockSize = blockSize - shiftsVector.back();
const size_t rightBlockSize = blockSize - leftBlockSize;
const size_t elementSize = sizeof(DataType);

size_t nIterations = totalElements / blockSize;
parallel_for(nIterations, [&](size_t iter) {
size_t start = iter * blockSize;
size_t leftBlockStartOffset = start;
size_t rightBlockStartOffset = start + leftBlockSize;

size_t segmentSize = 1;
for (int dim = numOfDims - 1; dim >= 0; --dim) {
leftBlockStartOffset = calculateShiftOffset(leftBlockStartOffset, shiftsVector[dim], segmentSize, shape[dim]);
rightBlockStartOffset = calculateShiftOffset(rightBlockStartOffset, shiftsVector[dim], segmentSize, shape[dim]);
segmentSize *= shape[dim];
apertovs marked this conversation as resolved.
Show resolved Hide resolved
}

if (leftBlockSize > 0)
cpu_memcpy(output + leftBlockStartOffset,
input + start,
leftBlockSize * elementSize);


if (rightBlockSize > 0)
cpu_memcpy(output + rightBlockStartOffset,
input + (start + leftBlockSize),
rightBlockSize * elementSize);
});
}

bool MKLDNNRollNode::created() const {
return getType() == Roll;
}

void MKLDNNRollNode::createPrimitive() {}

REG_MKLDNN_PRIM_FOR(MKLDNNRollNode, Roll)
40 changes: 40 additions & 0 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_roll_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <ie_common.h>
#include <mkldnn_node.h>
#include <string>

namespace MKLDNNPlugin {

class MKLDNNRollNode : public MKLDNNNode {
public:
MKLDNNRollNode(const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng, MKLDNNWeightsSharing::Ptr &cache);
~MKLDNNRollNode() override = default;

void getSupportedDescriptors() override;
void initSupportedPrimitiveDescriptors() override;
void createPrimitive() override;
void execute(mkldnn::stream strm) override;
bool created() const override;

private:
size_t calculateShiftOffset(size_t dataOffset, size_t dimShift, size_t segmentSize, size_t dimSize);

template <typename DataType>
void rollImpl(const InferenceEngine::Blob::CPtr &inputBlob, const InferenceEngine::Blob::CPtr &shiftsBlob,
const InferenceEngine::Blob::CPtr &axesBlob, const InferenceEngine::Blob::Ptr &outputBlob);

const size_t DATA_INDEX = 0ul;
const size_t SHIFT_INDEX = 1ul;
const size_t AXES_INDEX = 2ul;
const size_t numberOfInputs = 3ul;

size_t numOfDims;
std::vector<size_t> shape;
};

} // namespace MKLDNNPlugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <vector>

#include "single_layer_tests/roll.hpp"
#include "common_test_utils/test_constants.hpp"

using namespace LayerTestsDefinitions;

namespace {

const std::vector<InferenceEngine::Precision> inputPrecision = {
InferenceEngine::Precision::I8,
InferenceEngine::Precision::U8,
InferenceEngine::Precision::I16,
InferenceEngine::Precision::I32,
InferenceEngine::Precision::FP32,
InferenceEngine::Precision::I64,
InferenceEngine::Precision::BF16
};
apertovs marked this conversation as resolved.
Show resolved Hide resolved

const auto testCase2DZeroShifts = ::testing::Combine(
::testing::Values(std::vector<size_t>{17, 19}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{0, 0}), // Shift
::testing::Values(std::vector<int64_t>{0, 1}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCase1D = ::testing::Combine(
::testing::Values(std::vector<size_t>{16}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{5}), // Shift
::testing::Values(std::vector<int64_t>{0}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCase2D = ::testing::Combine(
::testing::Values(std::vector<size_t>{600, 450}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{300, 250}), // Shift
::testing::Values(std::vector<int64_t>{0, 1}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCase3D = ::testing::Combine(
::testing::Values(std::vector<size_t>{2, 320, 320}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{160, 160}), // Shift
::testing::Values(std::vector<int64_t>{1, 2}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCaseNegativeUnorderedAxes4D = ::testing::Combine(
::testing::Values(std::vector<size_t>{3, 11, 6, 4}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{7, 3}), // Shift
::testing::Values(std::vector<int64_t>{-3, -2}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCaseRepeatingAxes5D = ::testing::Combine(
::testing::Values(std::vector<size_t>{2, 16, 32, 32}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{16, 15, 10, 2, 1, 7, 8, 1, 1}), // Shift
::testing::Values(std::vector<int64_t>{-1, -2, -3, 1, 0, 3, 2, -2, -3}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCaseNegativeShifts6D = ::testing::Combine(
::testing::Values(std::vector<size_t>{4, 16, 3, 6, 5, 2}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{-2, -15, -2, -1, -4, -1}), // Shift
::testing::Values(std::vector<int64_t>{0, 1, 2, 3, 4, 5}), // Axes
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

const auto testCaseUnordNegAxesAndShifts10D = ::testing::Combine(
::testing::Values(std::vector<size_t>{2, 2, 4, 2, 3, 6, 3, 2, 3, 2}), // Input shape
::testing::ValuesIn(inputPrecision), // Precision
::testing::Values(std::vector<int64_t>{-2, -1, 1, 1, 1, -2}), // Shift
::testing::Values(std::vector<int64_t>{-6, -4, -3, 1, -10, -2}), // Axes
apertovs marked this conversation as resolved.
Show resolved Hide resolved
::testing::Values(CommonTestUtils::DEVICE_CPU)
);

INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_2d_zero_shifts, RollLayerTest, testCase2DZeroShifts, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_1d, RollLayerTest, testCase1D, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_2d, RollLayerTest, testCase2D, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_3d, RollLayerTest, testCase3D, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_negative_unordered_axes_4d, RollLayerTest, testCaseNegativeUnorderedAxes4D, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_negative_unordered_axes_5d, RollLayerTest, testCaseRepeatingAxes5D, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_negative_shifts_6d, RollLayerTest, testCaseNegativeShifts6D, RollLayerTest::getTestCaseName);
INSTANTIATE_TEST_CASE_P(smoke_MKLDNN_TestsRoll_unord_neg_shifts_and_axes_10d, RollLayerTest, testCaseUnordNegAxesAndShifts10D, RollLayerTest::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "shared_test_classes/single_layer/roll.hpp"

namespace LayerTestsDefinitions {

TEST_P(RollLayerTest, CompareWithRefs) {
Run();
};

} // namespace LayerTestsDefinitions
Loading