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

Add Reverse & tests to PlaidML Plugin #35

Merged
merged 6 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
64 changes: 64 additions & 0 deletions inference-engine/src/plaidml_plugin/ops/reverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "plaidml_ops.hpp"
#include "plaidml_util.hpp"

#include "ngraph/opsets/opset.hpp"
#include "ngraph/opsets/opset1.hpp"

#include "plaidml/op/op.h"

using namespace plaidml; // NOLINT[build/namespaces]
using ngraph::opset1::Reverse;

namespace {

ngraph::AxisSet cast_constant_axis_mask(size_t operand_idx, ngraph::Node* layer) {
auto ngraph_const =
std::dynamic_pointer_cast<ngraph::op::Constant>(layer->input_value(operand_idx).get_node_shared_ptr());
if (!ngraph_const) {
THROW_IE_EXCEPTION << "Dynamic axes not currently supported by PlaidML plugin.";
}
auto bool_mask = ngraph_const->cast_vector<bool>();
ngraph::AxisSet axis_set{};
for (size_t i = 0; i < static_cast<size_t>(bool_mask.size()); ++i) {
if (bool_mask[i])
axis_set.emplace(i);
}
return axis_set;
}

} // namespace

namespace PlaidMLPlugin {

static OpRegistration reg("reverse", [](const Context& ctx) {
auto* layer = ngraph::as_type<Reverse>(ctx.layer);
IE_ASSERT(ctx.operands.size() == 2);
auto I = ctx.operands.at(0);
ngraph::AxisSet axes;
if (layer->get_mode() == Reverse::Mode::INDEX) {
axes = get_axis_set_from_constant_operand(1, ctx.layer);
} else if (layer->get_mode() == Reverse::Mode::MASK) {
axes = cast_constant_axis_mask(1, ctx.layer);
}

std::vector<edsl::TensorDim> dims(I.rank());
I.bind_dims(dims);
std::vector<edsl::TensorIndex> I_idxs(I.rank());
std::vector<edsl::TensorIndex> O_idxs;

for (size_t axis = 0; axis < I.rank(); axis++) {
if (axes.count(axis)) {
O_idxs.push_back(dims[axis] - 1 - I_idxs[axis]);
} else {
O_idxs.push_back(I_idxs[axis]);
}
}

return edsl::make_tuple(plaidml::edsl::Contraction().outShape(dims).outAccess(O_idxs).assign(I(I_idxs)));
tzerrell marked this conversation as resolved.
Show resolved Hide resolved
});

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

#include "single_layer_tests/reverse.hpp"
#include "common_test_utils/test_constants.hpp"
#include <vector>

using LayerTestsDefinitions::ReverseLayerTest;

namespace {
const std::vector<InferenceEngine::Precision> netPrecisions = {
InferenceEngine::Precision::FP32,
// InferenceEngine::Precision::FP16,
// InferenceEngine::Precision::I64
};

std::vector<std::vector<size_t>> shape_index{{4, 6, 5}, {3, 9, 2}, {1, 4, 2, 1, 1, 3}};
std::vector<std::vector<size_t>> axes_index{{0}, {1}, {0, 2}};

std::vector<std::vector<size_t>> shape_mask{{4, 6, 5}, {3, 9, 2}, {1, 4, 2}};
std::vector<std::vector<size_t>> axes_mask{{1, 0, 0}, {0, 1, 0}, {1, 0, 1}};

INSTANTIATE_TEST_CASE_P(ReverseCheckIndex, ReverseLayerTest,
::testing::Combine(::testing::ValuesIn(netPrecisions), //
::testing::ValuesIn(shape_index), //
::testing::ValuesIn(axes_index), //
::testing::Values("index"), //
::testing::Values(CommonTestUtils::DEVICE_PLAIDML), //
::testing::Values(std::map<std::string, std::string>({}))), //
ReverseLayerTest::getTestCaseName);

INSTANTIATE_TEST_CASE_P(ReverseCheckMask, ReverseLayerTest,
::testing::Combine(::testing::ValuesIn(netPrecisions), //
::testing::ValuesIn(shape_mask), //
::testing::ValuesIn(axes_mask), //
::testing::Values("mask"), //
::testing::Values(CommonTestUtils::DEVICE_PLAIDML), //
::testing::Values(std::map<std::string, std::string>({}))), //
ReverseLayerTest::getTestCaseName);
} // namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <tuple>
#include <string>
#include <vector>
#include <memory>
#include "ngraph_functions/builders.hpp"
#include "ngraph_functions/utils/ngraph_helpers.hpp"

#include "functional_test_utils/layer_test_utils.hpp"

namespace LayerTestsDefinitions {

// Note: always give axes in "INDEX" mode, the test builder will convert to MASK mode when needed
typedef std::tuple<
InferenceEngine::Precision, // Network precision
std::vector<size_t>, // Input shapes
std::vector<size_t>, // Axes
std::string, // Mode
std::string, // Device name
std::map<std::string, std::string> // Config
> reverseParams;

class ReverseLayerTest : public testing::WithParamInterface<reverseParams>,
virtual public LayerTestsUtils::LayerTestsCommon {
public:
static std::string getTestCaseName(testing::TestParamInfo<reverseParams> obj);

protected:
void SetUp() override;
};

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

#include <tuple>
#include <string>
#include <vector>
#include <memory>
#include <ie_plugin_config.hpp>
#include <ie_core.hpp>
#include <functional>

#include "functional_test_utils/blob_utils.hpp"
#include "functional_test_utils/layer_test_utils.hpp"
#include "common_test_utils/common_utils.hpp"
#include "single_layer_tests/reverse.hpp"

namespace LayerTestsDefinitions {
std::string ReverseLayerTest::getTestCaseName(testing::TestParamInfo<reverseParams> obj) {
InferenceEngine::Precision netPrecision;
InferenceEngine::SizeVector inputShapes;
std::vector<size_t> axes;
std::string mode, targetDevice;
std::map<std::string, std::string> config;
std::tie(netPrecision, inputShapes, axes, mode, targetDevice, config) = obj.param;
std::ostringstream result;
result << "IS=" << CommonTestUtils::vec2str(inputShapes) << "_";
result << "AX=" << CommonTestUtils::vec2str(axes) << "_";
result << "mode=" << mode << "_";
result << "netPRC=" << netPrecision.name() << "_";
result << "targetDevice=" << targetDevice;
return result.str();
}

void ReverseLayerTest::SetUp() {
InferenceEngine::SizeVector inputShapes;
std::vector<size_t> axes;
std::string mode;
InferenceEngine::Precision netPrecision;
std::tie(netPrecision, inputShapes, axes, mode, targetDevice, configuration) = this->GetParam();
auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
auto paramsIn = ngraph::builder::makeParams(ngPrc, {inputShapes});
auto paramIn = ngraph::helpers::convert2OutputVector(
ngraph::helpers::castOps2Nodes<ngraph::op::Parameter>(paramsIn));
std::shared_ptr<ngraph::opset1::Constant> constNode;
auto axes_dtype = (mode == "index") ? ngraph::element::Type_t::i64 : ngraph::element::Type_t::boolean;
constNode = std::make_shared<ngraph::opset1::Constant>(axes_dtype, ngraph::Shape{axes.size()}, axes);
auto reverse = std::dynamic_pointer_cast<ngraph::opset1::Reverse>(
std::make_shared<ngraph::opset1::Reverse>(paramIn[0], constNode, mode));
ngraph::ResultVector results{std::make_shared<ngraph::opset1::Result>(reverse)};
function = std::make_shared<ngraph::Function>(results, paramsIn, "Reverse");
}

TEST_P(ReverseLayerTest, CompareWithRefsDynamicBath) {
Run();
}
} // namespace LayerTestsDefinitions