Skip to content

Commit

Permalink
Extend Python nGraph API with Einsum-7 and test nGraph reader (#5486)
Browse files Browse the repository at this point in the history
* Extend nGraph Python API and test IE IR reader for Einsum

Signed-off-by: Roman Kazantsev <[email protected]>

* Format description for test auxiliary function

Signed-off-by: Roman Kazantsev <[email protected]>

* Remove print from the python test

Signed-off-by: Roman Kazantsev <[email protected]>
  • Loading branch information
rkazants authored May 4, 2021
1 parent e8a2ca6 commit d716db4
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <string>
#include "ngraph_reader_tests.hpp"
#include "common_test_utils/xml_net_builder/ir_net.hpp"

TEST_F(NGraphReaderTests, ReadEinsumNetwork) {
std::string model = R"V0G0N(
<net name="saved_model" version="10">
<layers>
<layer id="0" name="input_a" type="Parameter" version="opset1">
<data shape="2,3,4" element_type="f32"/>
<output>
<port id="0" precision="FP32">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
</port>
</output>
</layer>
<layer id="1" name="input_b" type="Parameter" version="opset1">
<data shape="5,3,4" element_type="f32"/>
<output>
<port id="0" precision="FP32">
<dim>5</dim>
<dim>3</dim>
<dim>4</dim>
</port>
</output>
</layer>
<layer id="2" name="einsum" type="Einsum" version="opset7">
<data equation="abc,dbc-&gt;ad"/>
<input>
<port id="0">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
</port>
<port id="1">
<dim>5</dim>
<dim>3</dim>
<dim>4</dim>
</port>
</input>
<output>
<port id="2" precision="FP32">
<dim>2</dim>
<dim>5</dim>
</port>
</output>
</layer>
<layer id="3" name="gelu/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>2</dim>
<dim>5</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="2" to-port="0"/>
<edge from-layer="1" from-port="0" to-layer="2" to-port="1"/>
<edge from-layer="2" from-port="2" to-layer="3" to-port="0"/>
</edges>
</net>
)V0G0N";
std::string modelV7 = R"V0G0N(
<net name="saved_model" version="7">
<layers>
<layer id="0" name="input_a" type="Input" version="opset1">
<output>
<port id="0" precision="FP32">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
</port>
</output>
</layer>
<layer id="1" name="input_b" type="Input" version="opset1">
<output>
<port id="0" precision="FP32">
<dim>5</dim>
<dim>3</dim>
<dim>4</dim>
</port>
</output>
</layer>
<layer id="2" name="einsum" type="Einsum" version="opset7">
<data equation="abc,dbc-&gt;ad"/>
<input>
<port id="0">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
</port>
<port id="1">
<dim>5</dim>
<dim>3</dim>
<dim>4</dim>
</port>
</input>
<output>
<port id="2" precision="FP32">
<dim>2</dim>
<dim>5</dim>
</port>
</output>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="2" to-port="0"/>
<edge from-layer="1" from-port="0" to-layer="2" to-port="1"/>
</edges>
</net>
)V0G0N";
compareIRs(model, modelV7);
}

TEST_F(NGraphReaderTests, ReadEinsumNetwork2) {
std::string model = R"V0G0N(
<net name="saved_model" version="10">
<layers>
<layer id="0" name="input_a" type="Parameter" version="opset1">
<data shape="2,3,4,5" element_type="f32"/>
<output>
<port id="0" precision="FP32">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
<dim>5</dim>
</port>
</output>
</layer>
<layer id="1" name="input_b" type="Parameter" version="opset1">
<data shape="4,5,6" element_type="f32"/>
<output>
<port id="0" precision="FP32">
<dim>4</dim>
<dim>5</dim>
<dim>6</dim>
</port>
</output>
</layer>
<layer id="2" name="input_c" type="Parameter" version="opset1">
<data shape="7,4,5" element_type="f32"/>
<output>
<port id="0" precision="FP32">
<dim>7</dim>
<dim>4</dim>
<dim>5</dim>
</port>
</output>
</layer>
<layer id="3" name="einsum" type="Einsum" version="opset7">
<data equation="abcd,cde,fcd-&gt;abe"/>
<input>
<port id="0">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
<dim>5</dim>
</port>
<port id="1">
<dim>4</dim>
<dim>5</dim>
<dim>6</dim>
</port>
<port id="2">
<dim>7</dim>
<dim>4</dim>
<dim>5</dim>
</port>
</input>
<output>
<port id="3" precision="FP32">
<dim>2</dim>
<dim>3</dim>
<dim>6</dim>
</port>
</output>
</layer>
<layer id="4" name="gelu/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>2</dim>
<dim>3</dim>
<dim>6</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="3" to-port="0"/>
<edge from-layer="1" from-port="0" to-layer="3" to-port="1"/>
<edge from-layer="2" from-port="0" to-layer="3" to-port="2"/>
<edge from-layer="3" from-port="3" to-layer="4" to-port="0"/>
</edges>
</net>
)V0G0N";
std::string modelV7 = R"V0G0N(
<net name="saved_model" version="7">
<layers>
<layer id="0" name="input_a" type="Input" version="opset1">
<output>
<port id="0" precision="FP32">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
<dim>5</dim>
</port>
</output>
</layer>
<layer id="1" name="input_b" type="Input" version="opset1">
<output>
<port id="0" precision="FP32">
<dim>4</dim>
<dim>5</dim>
<dim>6</dim>
</port>
</output>
</layer>
<layer id="2" name="input_c" type="Input" version="opset1">
<output>
<port id="0" precision="FP32">
<dim>7</dim>
<dim>4</dim>
<dim>5</dim>
</port>
</output>
</layer>
<layer id="3" name="einsum" type="Einsum" version="opset7">
<data equation="abcd,cde,fcd-&gt;abe"/>
<input>
<port id="0">
<dim>2</dim>
<dim>3</dim>
<dim>4</dim>
<dim>5</dim>
</port>
<port id="1">
<dim>4</dim>
<dim>5</dim>
<dim>6</dim>
</port>
<port id="2">
<dim>7</dim>
<dim>4</dim>
<dim>5</dim>
</port>
</input>
<output>
<port id="3" precision="FP32">
<dim>2</dim>
<dim>3</dim>
<dim>6</dim>
</port>
</output>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="3" to-port="0"/>
<edge from-layer="1" from-port="0" to-layer="3" to-port="1"/>
<edge from-layer="2" from-port="0" to-layer="3" to-port="2"/>
</edges>
</net>
)V0G0N";
compareIRs(model, modelV7);
}

1 change: 1 addition & 0 deletions ngraph/python/src/ngraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from ngraph.opset7 import depth_to_space
from ngraph.opset7 import detection_output
from ngraph.opset7 import divide
from ngraph.opset7 import einsum
from ngraph.opset7 import elu
from ngraph.opset7 import embedding_bag_offsets_sum
from ngraph.opset7 import embedding_bag_packed_sum
Expand Down
1 change: 1 addition & 0 deletions ngraph/python/src/ngraph/opset7/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from ngraph.opset1.ops import depth_to_space
from ngraph.opset1.ops import detection_output
from ngraph.opset1.ops import divide
from ngraph.opset7.ops import einsum
from ngraph.opset1.ops import elu
from ngraph.opset3.ops import embedding_bag_offsets_sum
from ngraph.opset3.ops import embedding_bag_packed_sum
Expand Down
22 changes: 20 additions & 2 deletions ngraph/python/src/ngraph/opset7/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

"""Factory functions for all ngraph ops."""
from functools import partial
from typing import Callable, Iterable, List, Optional, Set, Union

import numpy as np
from functools import partial

from ngraph.impl import Node, Shape
from ngraph.impl.op import Constant, Parameter
from ngraph.opset_utils import _get_node_factory
Expand Down Expand Up @@ -42,9 +41,28 @@

_get_node_factory_opset7 = partial(_get_node_factory, "opset7")


# -------------------------------------------- ops ------------------------------------------------


@nameable_op
def einsum(
inputs: List[Node],
equation: str
) -> Node:
"""Return a node which performs Einsum operation.
@param inputs: The list of input nodes
@param equation: Einsum equation
@return: The new node performing Einsum operation on the inputs
"""
attributes = {
"equation": equation
}

return _get_node_factory_opset7().create("Einsum", as_nodes(*inputs), attributes)


@nameable_op
def gelu(
data: Node,
Expand Down
2 changes: 2 additions & 0 deletions ngraph/python/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,5 @@ def xfail_test(reason="Mark the test as expected to fail", strict=True):
"Not equal to tolerance")

xfail_issue_49391 = xfail_test(reason="Roll is not implemented in CPU plugin.")

xfail_issue_45432 = xfail_test(reason="Einsum is not implemented in CPU plugin.")
Loading

0 comments on commit d716db4

Please sign in to comment.