Validation check for addOperation (#3911)
author오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 7 Dec 2018 07:46:20 +0000 (16:46 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 7 Dec 2018 07:46:20 +0000 (16:46 +0900)
- Check operation type is validate
- Add try-catch exception and return ANEURALNETWORKS_BAD_STATE when exception is raised
- Set return value when addOperationEx is not failed

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

index 9e98066..614382e 100644 (file)
@@ -226,6 +226,13 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  const ANeuralNetworksOperationType FIRST_OPERATION = ANEURALNETWORKS_ADD;
+  const ANeuralNetworksOperationType LAST_OPERATION = ANEURALNETWORKS_TRANSPOSE;
+  if ((type < FIRST_OPERATION) || (type > LAST_OPERATION))
+  {
+    return ANEURALNETWORKS_BAD_DATA;
+  }
+
   if (model->isFinished())
   {
     return ANEURALNETWORKS_BAD_STATE;
@@ -247,106 +254,113 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
   auto node_param =
       neurun::graph::operation::Node::InitParam{inputCount, inputs, outputCount, outputs};
 
-  switch (type)
+  try
   {
-    case ANEURALNETWORKS_CONV_2D:
+    switch (type)
     {
-      // 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);
-
-      if (inputCount == 7)
+      case ANEURALNETWORKS_CONV_2D:
       {
-        using GraphNode = neurun::graph::operation::Conv2DNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        // 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);
+
+        if (inputCount == 7)
+        {
+          using GraphNode = neurun::graph::operation::Conv2DNode;
+
+          graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        }
+        else
+        {
+          throw std::runtime_error{"Explicit padding in Conv2D is not supported, yet"};
+        }
+
+        break;
       }
-      else
+      case ANEURALNETWORKS_MAX_POOL_2D:
       {
-        throw std::runtime_error{"Explicit padding in Conv2D is not supported, yet"};
+        // 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);
+
+        if (inputCount == 7)
+        {
+          using GraphNode = neurun::graph::operation::MaxPool2DNode;
+
+          graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        }
+        else
+        {
+          throw std::runtime_error{"Explicit padding in MaxPool2D is not supported, yet"};
+        }
+
+        break;
       }
-
-      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);
-
-      if (inputCount == 7)
+      case ANEURALNETWORKS_AVERAGE_POOL_2D:
       {
-        using GraphNode = neurun::graph::operation::MaxPool2DNode;
-
-        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        // 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);
+
+        if (inputCount == 7)
+        {
+          using GraphNode = neurun::graph::operation::AvgPool2DNode;
+
+          graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        }
+        else
+        {
+          throw std::runtime_error{"Explicit padding in AvgPool2D is not supported, yet"};
+        }
+
+        break;
       }
-      else
+      case ANEURALNETWORKS_CONCATENATION:
       {
-        throw std::runtime_error{"Explicit padding in MaxPool2D is not supported, yet"};
-      }
+        using GraphNode = neurun::graph::operation::ConcatNode;
 
-      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);
+        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
 
-      if (inputCount == 7)
+        break;
+      }
+      case ANEURALNETWORKS_RESHAPE:
       {
-        using GraphNode = neurun::graph::operation::AvgPool2DNode;
+        using GraphNode = neurun::graph::operation::ReshapeNode;
 
         graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+
+        break;
       }
-      else
+      case ANEURALNETWORKS_FULLY_CONNECTED:
       {
-        throw std::runtime_error{"Explicit padding in AvgPool2D is not supported, yet"};
-      }
-
-      break;
-    }
-    case ANEURALNETWORKS_CONCATENATION:
-    {
-      using GraphNode = neurun::graph::operation::ConcatNode;
-
-      graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-      break;
-    }
-    case ANEURALNETWORKS_RESHAPE:
-    {
-      using GraphNode = neurun::graph::operation::ReshapeNode;
+        using GraphNode = neurun::graph::operation::FullyConnectedNode;
 
-      graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
-
-      break;
-    }
-    case ANEURALNETWORKS_FULLY_CONNECTED:
-    {
-      using GraphNode = neurun::graph::operation::FullyConnectedNode;
-
-      graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
 
-      break;
-    }
-    case ANEURALNETWORKS_SOFTMAX:
-    {
-      using GraphNode = neurun::graph::operation::SoftmaxNode;
+        break;
+      }
+      case ANEURALNETWORKS_SOFTMAX:
+      {
+        using GraphNode = neurun::graph::operation::SoftmaxNode;
 
-      graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
+        graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param));
 
-      break;
-    }
-    default:
-      throw std::runtime_error{"Not supported operation"};
-  };
+        break;
+      }
+      default:
+        throw std::runtime_error{"Not supported operation"};
+    };
+  }
+  catch (const std::exception &e)
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
 
   return ANEURALNETWORKS_NO_ERROR;
 }
@@ -366,6 +380,13 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_BAD_STATE;
   }
 
+  const ANeuralNetworksOperationTypeEx FIRST_OPERATION = ANEURALNETWORKS_GATHER_EX;
+  const ANeuralNetworksOperationTypeEx LAST_OPERATION = ANEURALNETWORKS_PRELU_EX;
+  if ((type < FIRST_OPERATION) || (type > LAST_OPERATION))
+  {
+    return ANEURALNETWORKS_BAD_DATA;
+  }
+
   for (uint32_t i = 0; i < outputCount; i++)
   {
     const ::neurun::graph::operand::Index ind{outputs[i]};
@@ -383,11 +404,20 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_BAD_DATA;
   }
 
-  switch (type)
+  try
   {
-    default:
-      throw std::runtime_error{"Not supported operation"};
+    switch (type)
+    {
+      default:
+        throw std::runtime_error{"Not supported operation"};
+    }
+  }
+  catch (const std::exception &e)
+  {
+    return ANEURALNETWORKS_BAD_STATE;
   }
+
+  return ANEURALNETWORKS_NO_ERROR;
 }
 
 int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, uint32_t inputCount,