Revise frontend operation add (#4440)
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 19 Feb 2019 01:41:50 +0000 (10:41 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 19 Feb 2019 01:41:50 +0000 (10:41 +0900)
- Introduce wrapper method to add operation
- Add log message for fail

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/neurun/src/frontend/model.cc
runtimes/neurun/src/frontend/wrapper/model.cc
runtimes/neurun/src/frontend/wrapper/model.h

index f57baae..fb0e3f4 100644 (file)
@@ -26,8 +26,6 @@
 #include "graph/Graph.h"
 #include "frontend/wrapper/model.h"
 #include "frontend/wrapper/memory.h"
-#include "frontend/wrapper/OperationFactory.h"
-#include "model/operation/Node.Include.h"
 #include "util/NNAPIConvert.h"
 #include "util/logging.h"
 
@@ -249,9 +247,16 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
 {
   if ((model == nullptr) || (inputs == nullptr) || (outputs == nullptr))
   {
+    VERBOSE(NNAPI::Model) << "addOperation: Incorrect null pointer parameter(s)" << std::endl;
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    VERBOSE(NNAPI::Model) << "addOperation: Already finished" << std::endl;
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   const ANeuralNetworksOperationType FIRST_OPERATION = ANEURALNETWORKS_ADD;
   const ANeuralNetworksOperationType LAST_OPERATION = ANEURALNETWORKS_TRANSPOSE;
   if ((type < FIRST_OPERATION) || (type > LAST_OPERATION))
@@ -259,131 +264,19 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_BAD_DATA;
   }
 
-  if (model->isFinished())
-  {
-    return ANEURALNETWORKS_BAD_STATE;
-  }
-
   for (uint32_t i = 0; i < outputCount; i++)
   {
-    const ::neurun::model::operand::Index ind{outputs[i]};
-    auto &obj = model->deref().operands().at(ind);
-
-    if (!obj.usage(neurun::model::operand::Usage::OPERATION_OUTPUT))
+    if (model->isUsageSet(outputs[i]))
     {
+      VERBOSE(NNAPI::Model) << "addOperation: Already set output operand" << std::endl;
       return ANEURALNETWORKS_BAD_DATA;
     }
   }
 
-  auto &graph = model->deref();
-
-  auto &factory = OperationFactory::instance();
-  auto node_param = // TODO Will be removed once all creation is done via factory
-      neurun::model::operation::Node::InitParam{inputCount, inputs, outputCount, outputs};
-  OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
-
-  try
+  if (!model->addOperation(type, inputCount, inputs, outputCount, outputs))
   {
-    switch (type)
-    {
-      case ANEURALNETWORKS_ADD:
-      {
-        assert(inputCount == 3);
-        assert(outputCount == 1);
-
-        using GraphNode = neurun::model::operation::AddNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_CONV_2D:
-      {
-        auto node = factory.create(type, param);
-        graph.addOperation(std::unique_ptr<neurun::model::operation::Node>{node});
-
-        break;
-      }
-      case ANEURALNETWORKS_DEPTHWISE_CONV_2D:
-      {
-        // inputCount is either 8 or 11 acccording to NN API specification.
-        //  - Padding is implicit when inputCount is 8
-        //  - Padding is explicit when inputCount is 11
-        assert(inputCount == 8 || inputCount == 11);
-        assert(outputCount == 1);
-
-        using GraphNode = neurun::model::operation::DepthwiseConv2DNode;
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_MAX_POOL_2D:
-      {
-        // inputCount is either 7 or 10 acccording to NN API specification.
-        //  - Padding is implicit when inputCount is 7
-        //  - Padding is explicit when inputCount is 10
-        assert(inputCount == 7 || inputCount == 10);
-        assert(outputCount == 1);
-
-        using GraphNode = neurun::model::operation::MaxPool2DNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_AVERAGE_POOL_2D:
-      {
-        // inputCount is either 7 or 10 acccording to NN API specification.
-        //  - Padding is implicit when inputCount is 7
-        //  - Padding is explicit when inputCount is 10
-        assert(inputCount == 7 || inputCount == 10);
-        assert(outputCount == 1);
-
-        using GraphNode = neurun::model::operation::AvgPool2DNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_CONCATENATION:
-      {
-        using GraphNode = neurun::model::operation::ConcatNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_RESHAPE:
-      {
-        using GraphNode = neurun::model::operation::ReshapeNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_FULLY_CONNECTED:
-      {
-        using GraphNode = neurun::model::operation::FullyConnectedNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      case ANEURALNETWORKS_SOFTMAX:
-      {
-        using GraphNode = neurun::model::operation::SoftmaxNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-        break;
-      }
-      default:
-        throw std::runtime_error{"Not supported operation"};
-    };
-  }
-  catch (const std::exception &e)
-  {
-    return ANEURALNETWORKS_BAD_STATE;
+    VERBOSE(NNAPI::Model) << "addOperation: Fail to add operation" << std::endl;
+    return ANEURALNETWORKS_BAD_DATA;
   }
 
   return ANEURALNETWORKS_NO_ERROR;
@@ -396,11 +289,13 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
 {
   if ((model == nullptr) || (inputs == nullptr) || (outputs == nullptr))
   {
+    VERBOSE(NNAPI::Model) << "addOperation: Incorrect null pointer parameter(s)" << std::endl;
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
   if (model->isFinished())
   {
+    VERBOSE(NNAPI::Model) << "addOperation: Already finished" << std::endl;
     return ANEURALNETWORKS_BAD_STATE;
   }
 
@@ -408,39 +303,25 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
   const ANeuralNetworksOperationTypeEx LAST_OPERATION = ANEURALNETWORKS_PRELU_EX;
   if ((type < FIRST_OPERATION) || (type > LAST_OPERATION))
   {
+    VERBOSE(NNAPI::Model) << "addOperation: Invalid operation type" << std::endl;
     return ANEURALNETWORKS_BAD_DATA;
   }
 
   for (uint32_t i = 0; i < outputCount; i++)
   {
-    const ::neurun::model::operand::Index ind{outputs[i]};
-    auto &obj = model->deref().operands().at(ind);
-
-    if (!obj.usage(neurun::model::operand::Usage::OPERATION_OUTPUT))
+    if (model->isUsageSet(outputs[i]))
     {
+      VERBOSE(NNAPI::Model) << "addOperation: Already set output operand" << std::endl;
       return ANEURALNETWORKS_BAD_DATA;
     }
   }
 
-  // Workaround: to avoid compile error by unused-parameter, use inputCount
-  if (inputCount == 0)
+  if (!model->addOperationEx(type, inputCount, inputs, outputCount, outputs))
   {
+    VERBOSE(NNAPI::Model) << "addOperation: Fail to add operation" << std::endl;
     return ANEURALNETWORKS_BAD_DATA;
   }
 
-  try
-  {
-    switch (type)
-    {
-      default:
-        throw std::runtime_error{"Not supported operation"};
-    }
-  }
-  catch (const std::exception &e)
-  {
-    return ANEURALNETWORKS_BAD_STATE;
-  }
-
   return ANEURALNETWORKS_NO_ERROR;
 }
 
index d38e670..6be9748 100644 (file)
  */
 
 #include "model.h"
+#include "OperationFactory.h"
 
 #include "graph/Graph.h"
+#include "model/operation/Node.Include.h"
 #include "util/logging.h"
 #include "util/NNAPIConvert.h"
 
@@ -93,6 +95,154 @@ bool ANeuralNetworksModel::setOperandValue(uint32_t index, const void *buffer, s
   return true;
 }
 
+bool ANeuralNetworksModel::addOperation(ANeuralNetworksOperationType type, uint32_t inputCount,
+                                        const uint32_t *inputs, uint32_t outputCount,
+                                        const uint32_t *outputs) noexcept
+{
+  try
+  {
+    for (uint32_t i = 0; i < outputCount; i++)
+    {
+      const neurun::model::operand::Index ind{outputs[i]};
+      _model->operands().at(ind).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
+    }
+
+    auto &factory = OperationFactory::instance();
+    auto node_param = // TODO Will be removed once all creation is done via factory
+        neurun::model::operation::Node::InitParam{inputCount, inputs, outputCount, outputs};
+    OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
+
+    switch (type)
+    {
+      case ANEURALNETWORKS_ADD:
+      {
+        assert(inputCount == 3);
+        assert(outputCount == 1);
+
+        using GraphNode = neurun::model::operation::AddNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_CONV_2D:
+      {
+        auto node = factory.create(type, param);
+        _model->addOperation(std::unique_ptr<neurun::model::operation::Node>{node});
+
+        break;
+      }
+      case ANEURALNETWORKS_DEPTHWISE_CONV_2D:
+      {
+        // inputCount is either 8 or 11 acccording to NN API specification.
+        //  - Padding is implicit when inputCount is 8
+        //  - Padding is explicit when inputCount is 11
+        assert(inputCount == 8 || inputCount == 11);
+        assert(outputCount == 1);
+
+        using GraphNode = neurun::model::operation::DepthwiseConv2DNode;
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_MAX_POOL_2D:
+      {
+        // inputCount is either 7 or 10 acccording to NN API specification.
+        //  - Padding is implicit when inputCount is 7
+        //  - Padding is explicit when inputCount is 10
+        assert(inputCount == 7 || inputCount == 10);
+        assert(outputCount == 1);
+
+        using GraphNode = neurun::model::operation::MaxPool2DNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_AVERAGE_POOL_2D:
+      {
+        // inputCount is either 7 or 10 acccording to NN API specification.
+        //  - Padding is implicit when inputCount is 7
+        //  - Padding is explicit when inputCount is 10
+        assert(inputCount == 7 || inputCount == 10);
+        assert(outputCount == 1);
+
+        using GraphNode = neurun::model::operation::AvgPool2DNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_CONCATENATION:
+      {
+        using GraphNode = neurun::model::operation::ConcatNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_RESHAPE:
+      {
+        using GraphNode = neurun::model::operation::ReshapeNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_FULLY_CONNECTED:
+      {
+        using GraphNode = neurun::model::operation::FullyConnectedNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      case ANEURALNETWORKS_SOFTMAX:
+      {
+        using GraphNode = neurun::model::operation::SoftmaxNode;
+
+        _model->addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
+      }
+      default:
+        throw std::runtime_error{"Not supported operation"};
+    };
+  }
+  catch (const std::exception &e)
+  {
+    VERBOSE(EXCEPTION) << e.what() << std::endl;
+
+    return false;
+  }
+
+  return true;
+}
+
+bool ANeuralNetworksModel::addOperationEx(ANeuralNetworksOperationTypeEx type, uint32_t,
+                                          const uint32_t *, uint32_t outputCount,
+                                          const uint32_t *outputs) noexcept
+{
+  try
+  {
+    for (uint32_t i = 0; i < outputCount; i++)
+    {
+      const neurun::model::operand::Index ind{outputs[i]};
+      _model->operands().at(ind).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
+    }
+    switch (type)
+    {
+      default:
+        throw std::runtime_error{"Not supported operation"};
+    }
+  }
+  catch (const std::exception &e)
+  {
+    return false;
+  }
+  return true;
+}
+
 bool ANeuralNetworksModel::finish() noexcept
 {
   try
index 77ed5aa..af81082 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <unordered_set>
 #include <NeuralNetworks.h>
+#include <NeuralNetworksEx.h>
 
 #include "graph/Graph.h"
 
@@ -31,6 +32,11 @@ public:
   bool addOperand(const ANeuralNetworksOperandType *type) noexcept;
   bool setOperandValue(uint32_t index, const void *buffer, size_t length, bool optional = false,
                        bool copy = false) noexcept;
+  bool addOperation(ANeuralNetworksOperationType type, uint32_t inputCount, const uint32_t *inputs,
+                    uint32_t outputCount, const uint32_t *outputs) noexcept;
+  bool addOperationEx(ANeuralNetworksOperationTypeEx type, uint32_t inputCount,
+                      const uint32_t *inputs, uint32_t outputCount,
+                      const uint32_t *outputs) noexcept;
   bool finish() noexcept;
 
   neurun::graph::Graph &deref(void) { return *_model; }