From 6986105079de6c4e333b085d9c049bd9dd0118e5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Fri, 7 Dec 2018 16:46:20 +0900 Subject: [PATCH] Validation check for addOperation (#3911) - 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 --- runtimes/neurun/src/frontend/model.cc | 196 ++++++++++++++++++++-------------- 1 file changed, 113 insertions(+), 83 deletions(-) diff --git a/runtimes/neurun/src/frontend/model.cc b/runtimes/neurun/src/frontend/model.cc index 9e98066..614382e 100644 --- a/runtimes/neurun/src/frontend/model.cc +++ b/runtimes/neurun/src/frontend/model.cc @@ -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(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(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(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(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(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(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(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(node_param)); - - break; - } - case ANEURALNETWORKS_RESHAPE: - { - using GraphNode = neurun::graph::operation::ReshapeNode; + using GraphNode = neurun::graph::operation::FullyConnectedNode; - graph.addOperation(nnfw::cpp14::make_unique(node_param)); - - break; - } - case ANEURALNETWORKS_FULLY_CONNECTED: - { - using GraphNode = neurun::graph::operation::FullyConnectedNode; - - graph.addOperation(nnfw::cpp14::make_unique(node_param)); + graph.addOperation(nnfw::cpp14::make_unique(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(node_param)); + graph.addOperation(nnfw::cpp14::make_unique(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, -- 2.7.4