#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"
{
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))
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;
{
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 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;
}
*/
#include "model.h"
+#include "OperationFactory.h"
#include "graph/Graph.h"
+#include "model/operation/Node.Include.h"
#include "util/logging.h"
#include "util/NNAPIConvert.h"
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