forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ONNX op "com.microsoft.EmbedLayerNormalization" (open…
- Loading branch information
1 parent
201778c
commit 4604fed
Showing
11 changed files
with
1,187 additions
and
4 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
ngraph/frontend/onnx/frontend/src/op/com.microsoft/embed_layer_normalization.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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"); | ||
|
||
const auto& input_ids = nodes[0]; | ||
const auto& segment_ids = nodes[1]; | ||
const auto& word_embeddings = nodes[2]; | ||
const auto& position_embeddings = nodes[3]; | ||
const auto& segment_embeddings = nodes[4]; | ||
const auto& gamma = nodes[5]; | ||
const auto& beta = nodes[6]; | ||
|
||
auto zero = default_opset::Constant::create(element::i32, Shape{1}, {0}); | ||
std::shared_ptr<ngraph::Node> input = std::make_shared<default_opset::Gather>(word_embeddings, input_ids, zero, 0); | ||
input = std::make_shared<default_opset::Add>(input, position_embeddings); | ||
|
||
// add segment embeddings if available | ||
if (!ngraph::op::is_null(segment_ids)) { | ||
NGRAPH_CHECK(!ngraph::op::is_null(segment_embeddings), | ||
"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 gathered_segment_embeddings = | ||
std::make_shared<default_opset::Gather>(segment_embeddings, segment_ids, zero, 0); | ||
input = std::make_shared<default_opset::Add>(input, gathered_segment_embeddings); | ||
} | ||
|
||
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); | ||
|
||
// result = gamma * result + beta | ||
result = std::make_shared<default_opset::Multiply>(result, gamma); | ||
result = std::make_shared<default_opset::Add>(result, beta); | ||
|
||
// compute mask_index output | ||
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 |
17 changes: 17 additions & 0 deletions
17
ngraph/frontend/onnx/frontend/src/op/com.microsoft/embed_layer_normalization.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
ngraph/test/models/onnx/dynamic_shapes/embed_layer_normalization_dynamic_shapes.prototxt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.