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 EmbeddingBagOffsetsSum op and tests #100

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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/opset4.hpp"

#include "plaidml/op/op.h"

using namespace plaidml; // NOLINT[build/namespaces]
using namespace InferenceEngine; // NOLINT[build/namespaces]
using namespace plaidml::edsl;

namespace {

template <typename T>
std::vector<T> cast_constant_operand(size_t operand_idx, ngraph::Node* layer) {
auto* ngraph_const = ngraph::as_type<ngraph::op::Constant>(layer->get_input_node_ptr(operand_idx));
if (ngraph_const) {
return ngraph_const->cast_vector<T>();
} else {
THROW_IE_EXCEPTION << "Dynamic slicing not currently supported by PlaidML plugin; all of indices, and offsets"
"must be Constants.";
haoyouab marked this conversation as resolved.
Show resolved Hide resolved
}
}

} // namespace

namespace PlaidMLPlugin {

static OpRegistration reg("EmbeddingBagOffsetsSum", [](const Context& ctx) {
auto* layer = ngraph::as_type<ngraph::opset4::EmbeddingBagOffsetsSum>(ctx.layer);
auto I = ctx.operands.at(0);
auto indices = ctx.operands.at(1);
auto indices_cst = cast_constant_operand<size_t>(1, layer);
auto offsets = cast_constant_operand<int32_t>(2, layer);
auto default_index = cast_constant_operand<int64_t>(3, layer);
haoyouab marked this conversation as resolved.
Show resolved Hide resolved

haoyouab marked this conversation as resolved.
Show resolved Hide resolved
auto num_indices = indices_cst.size();
auto batch = offsets.size();
offsets.push_back(num_indices);

auto I_gathered = gather(I, indices);

auto ndims = I_gathered.rank();
std::vector<TensorDim> I_dims(ndims);
std::vector<TensorIndex> I_idxs(ndims);
std::vector<Tensor> slices, Os;
I_gathered.bind_dims(I_dims);
auto O_dims = I_dims;
auto O_idxs = I_idxs;

O_dims[0] = edsl::TensorDim(1);
for (size_t i = 0; i < num_indices; ++i) {
O_idxs[0] = I_idxs[0] - i;
slices.push_back(edsl::Contraction(O_dims, O_idxs).sum(I_gathered(I_idxs)));
}

for (uint32_t l = 0; l < batch; ++l) {
if (offsets[l + 1] == offsets[l]) {
O_idxs[0] = I_idxs[0] - default_index[0];
Os.push_back(edsl::Contraction(O_dims, O_idxs).sum(I(I_idxs)));
} else {
Tensor t = slices[offsets[l]];
for (uint32_t i = offsets[l] + 1; i < offsets[l + 1]; ++i) {
t = t + slices[i];
}
Os.push_back(t);
}
}

auto O = op::concatenate(Os, 0);
return edsl::make_tuple(O);
});

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

#include <vector>

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

using namespace LayerTestsDefinitions;

namespace {

const std::vector<InferenceEngine::Precision> netPrecisions = {
InferenceEngine::Precision::FP32,
InferenceEngine::Precision::I32,
};

const std::vector<InferenceEngine::Precision> indPrecisions = {
InferenceEngine::Precision::I32
};

const std::vector<std::vector<size_t>> embTableShape = {{5, 6}, {10, 35}, {5, 4, 16}};
const std::vector<std::vector<size_t>> indices =
{{0, 1, 2, 2, 3}, {4, 4, 3, 1, 0}, {1, 2, 1, 2, 1, 2, 1, 2, 1, 2}};
const std::vector<std::vector<size_t>> offsets = {{0, 2}, {0, 0, 2, 2}, {2, 4}};
const std::vector<size_t> defaultIndex = {0, 4};
const std::vector<bool> withWeights = {false};
haoyouab marked this conversation as resolved.
Show resolved Hide resolved
const std::vector<bool> withDefaultIndex = {true};
haoyouab marked this conversation as resolved.
Show resolved Hide resolved

const auto embBagOffsetSumArgSet = ::testing::Combine(
::testing::ValuesIn(embTableShape),
::testing::ValuesIn(indices),
::testing::ValuesIn(offsets),
::testing::ValuesIn(defaultIndex),
::testing::ValuesIn(withWeights),
::testing::ValuesIn(withDefaultIndex)
);

INSTANTIATE_TEST_CASE_P(smoke, EmbeddingBagOffsetsSumLayerTest,
::testing::Combine(
embBagOffsetSumArgSet,
::testing::ValuesIn(netPrecisions),
::testing::ValuesIn(indPrecisions),
::testing::Values(CommonTestUtils::DEVICE_PLAIDML)),
EmbeddingBagOffsetsSumLayerTest::getTestCaseName);
} // namespace