Added conversion of execution graph to old representation (#1626)
authorIlya Lavrenov <ilya.lavrenov@intel.com>
Tue, 4 Aug 2020 17:30:35 +0000 (20:30 +0300)
committerGitHub <noreply@github.com>
Tue, 4 Aug 2020 17:30:35 +0000 (20:30 +0300)
* Added conversion of execution graph to old representation

* Fixed compilation on Windows

inference-engine/src/legacy_api/src/convert_function_to_cnn_network.cpp
inference-engine/src/legacy_api/src/ie_cnn_layer_builder_ngraph.cpp
inference-engine/tests/functional/plugin/shared/src/execution_graph_tests/num_inputs_fusing_bin_conv.cpp

index 164cbd4..c00d8f3 100644 (file)
@@ -644,6 +644,7 @@ void convertFunctionToICNNNetwork(const std::shared_ptr<const ::ngraph::Function
                 std::make_shared<Builder::NodeConverter<::ngraph::op::v1::ReduceLogicalAnd>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::v1::ReduceLogicalOr>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::ShuffleChannels>>(),
+                std::make_shared<Builder::NodeConverter<::ExecGraphInfoSerialization::ExecutionNode>>(),
         };
         CNNLayerPtr result;
 
index 073d00a..e81e166 100644 (file)
@@ -45,6 +45,7 @@
 #include "ngraph_ops/rnn_cell_ie.hpp"
 #include "ngraph_ops/hard_sigmoid_ie.hpp"
 #include "generic_ie.hpp"
+#include "exec_graph_info.hpp"
 
 #include "graph_transformer.h"
 
@@ -1724,6 +1725,40 @@ CNNLayer::Ptr NodeConverter<ngraph::op::MatMul>::createLayer(const std::shared_p
 }
 
 template <>
+CNNLayer::Ptr NodeConverter<ExecGraphInfoSerialization::ExecutionNode>::createLayer(const std::shared_ptr<ngraph::Node>& layer) const {
+    auto castedLayer = ngraph::as_type_ptr<ExecGraphInfoSerialization::ExecutionNode>(layer);
+    if (castedLayer == nullptr)
+        THROW_IE_EXCEPTION << "Cannot convert " << layer->get_friendly_name() << " layer ";
+
+    auto & rtInfo = castedLayer->get_rt_info();
+    if (rtInfo.count(ExecGraphInfoSerialization::LAYER_TYPE) == 0) {
+        THROW_IE_EXCEPTION << "No " << ExecGraphInfoSerialization::LAYER_TYPE
+            << " attribute is set in " << layer->get_friendly_name() << " node";
+    }
+
+    auto getStringValue = [] (const std::shared_ptr<ngraph::Variant> & variant) {
+        auto castedVariant = std::dynamic_pointer_cast<ngraph::VariantImpl<std::string>>(variant);
+        IE_ASSERT(castedVariant != nullptr);
+        return castedVariant->get();
+    };
+
+    LayerParams params = { layer->get_friendly_name(),
+                           getStringValue(rtInfo[ExecGraphInfoSerialization::LAYER_TYPE]),
+                           details::convertPrecision(layer->get_output_element_type(0)) };
+    rtInfo.erase(ExecGraphInfoSerialization::LAYER_TYPE);
+
+    auto res = std::make_shared<InferenceEngine::CNNLayer>(params);
+    for (const auto & kvp : rtInfo) {
+        auto castedVariant = std::dynamic_pointer_cast<ngraph::VariantImpl<std::string>>(kvp.second);
+        // skip RT info which holds fusedNames, etc
+        if (castedVariant)
+            res->params[kvp.first] = getStringValue(castedVariant);
+    }
+
+    return res;
+}
+
+template <>
 CNNLayer::Ptr NodeConverter<ngraph::op::RegionYolo>::createLayer(const std::shared_ptr<ngraph::Node>& layer) const {
     LayerParams params = {layer->get_friendly_name(), "RegionYolo",
                           details::convertPrecision(layer->get_output_element_type(0))};
index 506ca90..df42546 100644 (file)
@@ -5,12 +5,14 @@
 #include <vector>
 
 #include <ie_core.hpp>
+#include <exec_graph_info.hpp>
 
 #include <ngraph/function.hpp>
 #include <ngraph/variant.hpp>
 
 #include "functional_test_utils/plugin_cache.hpp"
 #include "functional_test_utils/layer_test_utils.hpp"
+#include "common_test_utils/common_utils.hpp"
 
 #include "execution_graph_tests/num_inputs_fusing_bin_conv.hpp"
 
@@ -55,9 +57,15 @@ TEST_P(ExecGraphInputsFusingBinConv, CheckNumInputsInBinConvFusingWithConv) {
     InferenceEngine::CNNNetwork execGraphInfo = execNet.GetExecGraphInfo();
 
     if (auto function = execGraphInfo.getFunction()) {
+        // try to convert to old representation and check that conversion passed well
+        std::shared_ptr<InferenceEngine::details::CNNNetworkImpl> convertedExecGraph;
+        ASSERT_NO_THROW(convertedExecGraph = std::make_shared<InferenceEngine::details::CNNNetworkImpl>(execGraphInfo));
+        // serialization for IR-v7 like execution graph is still supported for compatibility
+        ASSERT_NO_THROW(convertedExecGraph->serialize("exeNetwork.xml", "exeNetwork.bin", nullptr));
+        ASSERT_EQ(0, std::remove("exeNetwork.xml"));
+
         for (const auto & op : function->get_ops()) {
             const auto & rtInfo = op->get_rt_info();
-
             auto getExecValue = [&rtInfo](const std::string & paramName) -> std::string {
                 auto it = rtInfo.find(paramName);
                 IE_ASSERT(rtInfo.end() != it);
@@ -74,6 +82,24 @@ TEST_P(ExecGraphInputsFusingBinConv, CheckNumInputsInBinConvFusingWithConv) {
                 ASSERT_TRUE(originalLayersNames.find("Add") != std::string::npos);
                 ASSERT_EQ(op->get_input_size(), 1);
             }
+
+            // IR v7 does not have output nodes
+            if (ngraph::op::is_output(op))
+                continue;
+
+            IE_SUPPRESS_DEPRECATED_START
+            InferenceEngine::CNNLayerPtr cnnLayer;
+            ASSERT_NO_THROW(cnnLayer = CommonTestUtils::getLayerByName(convertedExecGraph.get(), op->get_friendly_name()));
+            ASSERT_EQ(cnnLayer->name, op->get_friendly_name());
+            auto variantType = std::dynamic_pointer_cast<ngraph::VariantImpl<std::string>>(
+                op->get_rt_info()[ExecGraphInfoSerialization::LAYER_TYPE]);;
+            ASSERT_EQ(cnnLayer->type, variantType->get());
+
+            for (const auto & kvp : cnnLayer->params) {
+                auto variant = std::dynamic_pointer_cast<ngraph::VariantImpl<std::string>>(op->get_rt_info()[kvp.first]);
+                ASSERT_EQ(variant->get(), kvp.second);
+            }
+            IE_SUPPRESS_DEPRECATED_END
         }
     } else {
         IE_SUPPRESS_DEPRECATED_START