[neurun] Avoid using Model in nnapi frontend (#9231)
[platform/core/ml/nnfw.git] / runtime / neurun / frontend / nnapi / wrapper / ANeuralNetworksModel.cc
index 029411f..c937f51 100644 (file)
 //
 // ANeuralNetworksModel
 //
-ANeuralNetworksModel::ANeuralNetworksModel() noexcept
-    : _model{new neurun::model::Model}, _graph{nullptr}, _optional_operands{}, _operand_usages{}
+ANeuralNetworksModel::ANeuralNetworksModel() noexcept : _optional_operands{}, _operand_usages{}
 {
-  // DO NOTHING
+  auto model = nnfw::cpp14::make_unique<neurun::model::Model>();
+  _graph = std::make_shared<neurun::graph::Graph>(std::move(model));
 }
 
 bool ANeuralNetworksModel::addOperand(const ANeuralNetworksOperandType *type) noexcept
@@ -38,7 +38,7 @@ bool ANeuralNetworksModel::addOperand(const ANeuralNetworksOperandType *type) no
   {
     const auto shape = NNAPIConvert::getShape(type);
     const auto typeInfo = NNAPIConvert::getTypeInfo(type);
-    _model->operands.emplace(shape, typeInfo);
+    _graph->addOperand(shape, typeInfo);
     _operand_usages.emplace_back(OperandUsage::NOT_DEFINED);
   }
   catch (const std::exception &e)
@@ -71,12 +71,12 @@ bool ANeuralNetworksModel::setOperandValue(uint32_t index, const void *buffer, s
     using ::neurun::model::ExternalData;
     if (copy)
     {
-      _model->operands.at(ind).data(
+      _graph->operands().at(ind).data(
           nnfw::cpp14::make_unique<CachedData>(reinterpret_cast<const uint8_t *>(buffer), length));
     }
     else
     {
-      _model->operands.at(ind).data(nnfw::cpp14::make_unique<ExternalData>(
+      _graph->operands().at(ind).data(nnfw::cpp14::make_unique<ExternalData>(
           reinterpret_cast<const uint8_t *>(buffer), length));
     }
   }
@@ -104,8 +104,8 @@ bool ANeuralNetworksModel::addOperation(ANeuralNetworksOperationType type, uint3
     auto &factory = OperationFactory::get();
     OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
 
-    auto node = factory.create(type, param, _model->operands);
-    _model->operations.push(std::unique_ptr<neurun::model::Operation>{node});
+    auto node = factory.create(type, param, _graph->operands());
+    _graph->addOperation(std::unique_ptr<neurun::model::Operation>{node});
   }
   catch (const std::exception &e)
   {
@@ -131,8 +131,8 @@ bool ANeuralNetworksModel::addOperationEx(ANeuralNetworksOperationTypeEx type, u
     auto &factory = OperationFactory::get();
     OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
 
-    auto node = factory.create(type, param, _model->operands);
-    _model->operations.push(std::unique_ptr<neurun::model::Operation>{node});
+    auto node = factory.create(type, param, _graph->operands());
+    _graph->addOperation(std::unique_ptr<neurun::model::Operation>{node});
   }
   catch (const std::exception &e)
   {
@@ -148,7 +148,7 @@ bool ANeuralNetworksModel::addModelInput(uint32_t index) noexcept
     _operand_usages[index] = OperandUsage::MODEL_INPUT;
 
     const neurun::model::OperandIndex ind{index};
-    _model->inputs.append(ind);
+    _graph->addInput(ind);
   }
   catch (const std::exception &e)
   {
@@ -166,12 +166,12 @@ bool ANeuralNetworksModel::addModelOutput(uint32_t index) noexcept
     const neurun::model::OperandIndex ind{index};
 
     // Duplicated output is not allowed
-    if (_model->outputs.contains(ind))
+    if (_graph->getOutputs().contains(ind))
     {
       return false;
     }
 
-    _model->outputs.append(ind);
+    _graph->addOutput(ind);
   }
   catch (const std::exception &e)
   {
@@ -189,8 +189,6 @@ bool ANeuralNetworksModel::finish() noexcept
   {
     fillOptionalOperand();
 
-    _graph = std::make_shared<neurun::graph::Graph>(std::move(_model));
-
     _graph->finishBuilding();
 
     _operand_usages.clear();
@@ -205,18 +203,18 @@ bool ANeuralNetworksModel::finish() noexcept
   return true;
 }
 
-bool ANeuralNetworksModel::isFinished() noexcept { return _graph != nullptr; }
+bool ANeuralNetworksModel::isFinished() noexcept { return !_graph->isBuildingPhase(); }
 
 bool ANeuralNetworksModel::isExistOperand(uint32_t index) noexcept
 {
-  return _model->operands.exist(neurun::model::OperandIndex{index});
+  return _graph->operands().exist(neurun::model::OperandIndex{index});
 }
 
 size_t ANeuralNetworksModel::operandSize(uint32_t index) noexcept
 {
   try
   {
-    return _model->operands.at(neurun::model::OperandIndex{index}).operandSize();
+    return _graph->operands().at(neurun::model::OperandIndex{index}).operandSize();
   }
   catch (const std::exception &e)
   {
@@ -243,7 +241,7 @@ void ANeuralNetworksModel::setOptionalOperand(const neurun::model::OperandIndex
 
 void ANeuralNetworksModel::fillOptionalOperand(void)
 {
-  _model->operations.iterate(
+  _graph->operations().iterate(
       [&](const ::neurun::model::OperationIndex &, ::neurun::model::Operation &node) {
         for (auto input : node.getInputs())
         {