[nnc] Fix build (#2871)
authorАндрей Шедько/AI Tools Lab /SRR/Engineer/삼성전자 <a.shedko@samsung.com>
Wed, 16 Jan 2019 17:44:12 +0000 (20:44 +0300)
committerEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Wed, 16 Jan 2019 17:44:12 +0000 (20:44 +0300)
- Fixed build failure due to signature change.
- Refactored ONNX importer from PR #2705

Signed-off-by: Andrei Shedko <a.shedko@samsung.com>
contrib/nnc/passes/caffe2_frontend/caffe2_importer.cpp
contrib/nnc/passes/caffe2_frontend/caffe2_op_creator.cpp
contrib/nnc/passes/caffe2_frontend/caffe2_op_creator.h
contrib/nnc/passes/onnx_frontend/ONNXOpCreator.cpp

index 87473e3..e1fc5f2 100644 (file)
  * limitations under the License.
  */
 
-#include <vector>
-#include <fstream>
-#include <sstream>
-#include <cassert>
-
 #include "caffe2_importer.h"
 #include "passes/common_frontend/op_creator_helper.h"
 #include "passes/common_frontend/shape_helper.h"
 
 #include "caffe2_proto_helper.h"
 
+#include <vector>
+#include <fstream>
+#include <sstream>
+#include <cassert>
+
 namespace nnc {
 
 using namespace ::caffe2;
@@ -195,7 +195,7 @@ void Caffe2Importer::createMIRNodesFromOp(const OperatorDef& op) {
       outputs = _opCreator->convertRelu(inputs);
       break;
     case SupportedCaffe2OpType::resizeNearest:
-      outputs = _opCreator->convertResize(inputs, op);
+      outputs = _opCreator->convertResizeNearest(inputs, op);
       break;
     case SupportedCaffe2OpType::sigmoid:
       outputs = _opCreator->convertSigmoid(inputs);
index fa2c4fc..53bc3e1 100644 (file)
@@ -389,8 +389,9 @@ Caffe2OpCreator::convertRelu(const std::vector<IODescriptor>& inputs) {
   return {relu->getOutput(0)};
 }
 
-std::vector<IODescriptor> Caffe2OpCreator::convertResize(const std::vector<IODescriptor>& inputs,
-                                                         const ::caffe2::OperatorDef& op) {
+std::vector<IODescriptor>
+Caffe2OpCreator::convertResizeNearest(const std::vector<IODescriptor>& inputs,
+                                      const ::caffe2::OperatorDef& op) {
   // assume NCHW and convert to MIR (NHWC)
   std::vector<float> scales(4);
   assert(inputs[0].op->getOutputShape(0).rank() == 4 && "only 4d tensors is supported");
@@ -399,8 +400,8 @@ std::vector<IODescriptor> Caffe2OpCreator::convertResize(const std::vector<IODes
   scales[1] = getSingleArgument(op, "height_scale", 1.0f);
   scales[2] = getSingleArgument(op, "width_scale", 1.0f);
   scales[3] = 1;
-  auto resize = createOp<ops::ResizeOp>(
-    convertCaffeToMIR(inputs[0]), ops::ResizeOp::ResizeMethod::nearestNeighbor, scales);
+  auto resize = createOp<ops::ResizeOp>("ResizeNearest", convertCaffeToMIR(inputs[0]),
+                                        ops::ResizeOp::ResizeMethod::nearestNeighbor, scales);
   return {convertMIRToCaffe(resize->getOutput(0))};
 }
 
index b52a545..726925c 100644 (file)
@@ -82,8 +82,8 @@ public:
 
   std::vector<mir::IODescriptor> convertRelu(const std::vector<mir::IODescriptor>&);
 
-  std::vector<mir::IODescriptor> convertResize(const std::vector<mir::IODescriptor>&,
-                                               const ::caffe2::OperatorDef&);
+  std::vector<mir::IODescriptor> convertResizeNearest(const std::vector<mir::IODescriptor>&,
+                                                      const ::caffe2::OperatorDef&);
 
   std::vector<mir::IODescriptor> convertSigmoid(const std::vector<mir::IODescriptor>&);
 
index cfc0452..d118454 100644 (file)
@@ -57,8 +57,8 @@ namespace nnc {
 
 using namespace mir;
 
-inline static const onnx::AttributeProto* findAttribute(const onnx::NodeProto& onnx_node,
-                                                        const std::string& name) {
+static const onnx::AttributeProto* findAttribute(const onnx::NodeProto& onnx_node,
+                                                 const std::string& name) {
   for (auto& att : onnx_node.attribute()) {
     if (!att.name().compare(name)) {
       return &att;
@@ -67,8 +67,8 @@ inline static const onnx::AttributeProto* findAttribute(const onnx::NodeProto& o
   return nullptr;
 }
 
-inline static std::pair<bool, int> getIntAttribute(const onnx::NodeProto& onnx_node,
-                                                   const std::string& name = "axis") {
+static std::pair<bool, int> getIntAttribute(const onnx::NodeProto& onnx_node,
+                                            const std::string& name = "axis") {
   auto result = findAttribute(onnx_node, name);
   if (!result)
     return {false, 0};
@@ -76,8 +76,8 @@ inline static std::pair<bool, int> getIntAttribute(const onnx::NodeProto& onnx_n
   return {true, result->i()};
 }
 
-inline static std::pair<bool, std::string> getStringAttribute(const onnx::NodeProto& onnx_node,
-                                                              const std::string& name) {
+static std::pair<bool, std::string> getStringAttribute(const onnx::NodeProto& onnx_node,
+                                                       const std::string& name) {
   auto result = findAttribute(onnx_node, name);
   if (!result)
     return {false, ""};
@@ -85,8 +85,8 @@ inline static std::pair<bool, std::string> getStringAttribute(const onnx::NodePr
   return {true, result->s()};
 }
 
-inline static std::pair<bool, float> getFloatAttribute(const onnx::NodeProto& onnx_node,
-                                                       const std::string& name) {
+static std::pair<bool, float> getFloatAttribute(const onnx::NodeProto& onnx_node,
+                                                const std::string& name) {
   auto result = findAttribute(onnx_node, name);
   if (!result)
     return {false, 0.0};
@@ -138,15 +138,13 @@ static void getKernelStridesPadding(const onnx::NodeProto &onnx_node, KernelStri
   cdata.strides_shape = ShapeHelper::createShape(strides->ints(), strides->ints_size());
 
   if (pads) {
-    assert(pads->ints_size() >= 2);
+    assert(pads->ints_size() == 4);
     cdata.padding_before[0] = pads->ints(0);
     cdata.padding_before[1] = pads->ints(1);
     // TODO: ONNX padding could be for the beginning and ending along each axis that's why we
     // should select the interesting ones.
-    if (pads->ints_size() == 4) {
-      cdata.padding_after[0] = pads->ints(2);
-      cdata.padding_after[1] = pads->ints(3);
-    }
+    cdata.padding_after[0] = pads->ints(2);
+    cdata.padding_after[1] = pads->ints(3);
   }
 };
 
@@ -380,14 +378,15 @@ ONNXOpCreator::convertElementwise(const std::vector<mir::IODescriptor>& inputs,
 
 std::vector<IODescriptor>
 ONNXOpCreator::convertUpsample(const std::vector<mir::IODescriptor>& inputs,
-                                                       const onnx::NodeProto& node) {
+                               const onnx::NodeProto& node) {
   bool success;
   std::string mode;
   std::tie(success, mode) = getStringAttribute(node, "mode");
   if (!success) mode = "nearest";
   assert(mode == "nearest" && "Unsupported upscale mode!");
 
-  assert(inputs.size() > 1); // relies on constants being lifted to initializer list
+  // relies on attributes being lifted to constants (ONNX optimization pass)
+  assert(inputs.size() > 1);
   auto* scales = dynamic_cast<mir::ops::ConstantOp*>(inputs[1].op);
   assert(scales && "Weights could be a constant tensor only");
   auto scales_tensor = Tensor<float>(scales->getValue());