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 support for ONNX op "com.microsoft.EmbedLayerNormalization" #7837

Merged
merged 4 commits into from
Oct 7, 2021
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,58 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "op/com.microsoft/embed_layer_normalization.hpp"

#include "default_opset.hpp"
#include "onnx_import/core/null_node.hpp"

namespace ngraph {
namespace onnx_import {
namespace op {
namespace set_1 {
OutputVector embed_layer_normalization(const Node& node) {
auto nodes = node.get_ng_inputs();
auto num_nodes = nodes.size();
NGRAPH_CHECK(num_nodes >= 7 && num_nodes <= 8,
"EmbedLayerNormalization takes 7 or 8 inputs. Provided " + std::to_string(num_nodes));
NGRAPH_CHECK(nodes[0].get_element_type() == element::i32, "input_ids must have int32 type");
auto zero = default_opset::Constant::create(element::i32, Shape{1}, {0});
auto word_embedding = std::make_shared<default_opset::Gather>(nodes[2], nodes[0], zero, 0);
auto input = std::make_shared<default_opset::Add>(word_embedding, nodes[3]);
if (!ngraph::op::is_null(nodes[1])) {
NGRAPH_CHECK(!ngraph::op::is_null(nodes[4]), "segment_ids provided, but segment_embedding input is missing");
NGRAPH_CHECK(nodes[1].get_element_type() == element::i32, "segment_ids must have int32 type");
auto segment_embedding = std::make_shared<default_opset::Gather>(nodes[4], nodes[1], zero, 0);
input = std::make_shared<default_opset::Add>(input, segment_embedding);
}
float eps = node.get_attribute_value<float>("epsilon");
// reduce over hidden_size
// hidden_size dimension is 2 here, because the shape after Gather(word_embedding, input_ids)
// is (batch_size, seq_len, hidden_size)
int hidden_size_dim = 2;
const auto reduction_axes = default_opset::Constant::create(element::i32, Shape{1}, {hidden_size_dim});
std::shared_ptr<ngraph::Node> result =
std::make_shared<default_opset::MVN>(input, reduction_axes, true, eps, ngraph::op::MVNEpsMode::INSIDE_SQRT);
// multiply by gamma
result = std::make_shared<default_opset::Multiply>(result, nodes[5]);
// add beta
result = std::make_shared<default_opset::Add>(result, nodes[6]);
std::shared_ptr<ngraph::Node> mask_index;
if (num_nodes > 7 && !ngraph::op::is_null(nodes[7])) {
NGRAPH_CHECK(nodes[7].get_element_type() == element::i32, "mask must have int32 type");
auto axis = default_opset::Constant::create(element::i32, Shape{}, {1});
mask_index = std::make_shared<default_opset::ReduceSum>(nodes[7], axis, false);
} else {
auto batch_size = std::make_shared<default_opset::Gather>(std::make_shared<default_opset::ShapeOf>(nodes[0]),
zero, // indices
zero // axis
);
mask_index = std::make_shared<default_opset::Broadcast>(zero, batch_size);
}
return {result, mask_index};
}
} // namespace set_1
} // namespace op
} // namespace onnx_import
} // namespace ngraph
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "onnx_import/core/node.hpp"

namespace ngraph {
namespace onnx_import {
namespace op {
namespace set_1 {
OutputVector embed_layer_normalization(const Node& node);
} // namespace set_1
} // namespace op
} // namespace onnx_import
} // namespace ngraph
2 changes: 2 additions & 0 deletions ngraph/frontend/onnx/frontend/src/ops_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "op/ceil.hpp"
#include "op/clip.hpp"
#include "op/com.microsoft/bias_gelu.hpp"
#include "op/com.microsoft/embed_layer_normalization.hpp"
#include "op/com.microsoft/skip_layer_normalization.hpp"
#include "op/compress.hpp"
#include "op/concat.hpp"
Expand Down Expand Up @@ -480,6 +481,7 @@ OperatorsBridge::OperatorsBridge() {
REGISTER_OPERATOR_WITH_DOMAIN(OPENVINO_ONNX_DOMAIN, "Swish", 1, swish);

REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "BiasGelu", 1, bias_gelu);
REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "EmbedLayerNormalization", 1, embed_layer_normalization);
REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "SkipLayerNormalization", 1, skip_layer_normalization);
}

Expand Down
3 changes: 3 additions & 0 deletions ngraph/test/engines_util/ie_engines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ testing::AssertionResult test::IE_Engine::compare_results_with_tolerance_as_fp(c
comparison_result = test::compare_with_tolerance(test_results.first, test_results.second, tolerance);
break;
}
case InferenceEngine::Precision::I32:
comparison_result = compare_blobs<int32_t>(computed_output_blob, expected_output_blob, 0);
break;
default:
comparison_result = testing::AssertionFailure() << "Unsupported data type encountered in "
"'compare_results_with_tolerance_as_fp' method";
Expand Down
3 changes: 3 additions & 0 deletions ngraph/test/engines_util/interpreter_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ testing::AssertionResult test::INTERPRETER_Engine::compare_results_with_toleranc
case element::Type_t::f32:
comparison_result = compare_with_fp_tolerance(expected_result_constant, result_tensor, tolerance);
break;
case element::Type_t::i32:
comparison_result = compare_values<int32_t>(expected_result_constant, result_tensor, 0);
break;
default:
comparison_result = testing::AssertionFailure() << "Unsupported data type encountered in "
"'compare_results_with_tolerance_as_fp' method";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
ir_version: 6
producer_name: "nGraph"
graph {
node {
input: "input_ids"
input: "segment_ids"
input: "word_embeddings"
input: "position_embeddings"
input: "segment_embeddings"
input: "gamma"
input: "beta"
input: "mask"
output: "output"
output: "mask_index"
name: "EmbedLayerNormalization_1"
op_type: "EmbedLayerNormalization"
attribute {
name: "epsilon"
f: 9.999999960041972e-13
type: FLOAT
}
domain: "com.microsoft"
}
name: "graph"
input {
name: "input_ids"
type {
tensor_type {
elem_type: 6
shape {
dim {
dim_param: "batch_size"
}
dim {
dim_param: "seq_len"
}
}
}
}
}
input {
name: "segment_ids"
type {
tensor_type {
elem_type: 6
shape {
dim {
dim_param: "batch_size"
}
dim {
dim_param: "seq_len"
}
}
}
}
}
input {
name: "word_embeddings"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "word_embed_len"
}
dim {
dim_value: 5
}
}
}
}
}
input {
name: "position_embeddings"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "pos_embed_len"
}
dim {
dim_value: 5
}
}
}
}
}
input {
name: "segment_embeddings"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "segment_embed_len"
}
dim {
dim_value: 5
}
}
}
}
}
input {
name: "gamma"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 5
}
}
}
}
}
input {
name: "beta"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 5
}
}
}
}
}
input {
name: "mask"
type {
tensor_type {
elem_type: 6
shape {
dim {
dim_param: "batch_size"
}
dim {
dim_param: "seq_len"
}
}
}
}
}
output {
name: "output"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "batch_size"
}
dim {
dim_param: "seq_len"
}
dim {
dim_value: 5
}
}
}
}
}
output {
name: "mask_index"
type {
tensor_type {
elem_type: 6
shape {
dim {
dim_param: "batch_size"
}
}
}
}
}
}
opset_import {
version: 11
}
opset_import {
domain: "com.microsoft"
version: 1
}
Loading