From 767f93c684d77fed1006e9be63e3a152574272c6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=ED=95=9C=EC=A2=85/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Fri, 10 Aug 2018 10:52:57 +0900 Subject: [PATCH] [neurun] Apply Graph IR (#2244) This commit replaces `internal::tflite::Model` with `neurun::graph::Graph`. Resolve #2155 Signed-off-by: Hanjoung Lee --- .../src/backend/acl_cl/InitializerGenerator.cc | 2 +- .../src/backend/acl_cl/InitializerGenerator.h | 6 +- .../neurun/src/backend/acl_cl/StageGenerator.cc | 3 +- .../neurun/src/backend/acl_cl/StageGenerator.h | 6 +- .../neurun/src/backend/cpu/InitializerGenerator.cc | 2 +- .../neurun/src/backend/cpu/InitializerGenerator.h | 6 +- runtimes/neurun/src/backend/cpu/StageGenerator.cc | 2 +- runtimes/neurun/src/backend/cpu/StageGenerator.h | 6 +- runtimes/neurun/src/compilation.cc | 28 +++++-- runtimes/neurun/src/compilation.h | 4 +- runtimes/neurun/src/execution.h | 4 +- runtimes/neurun/src/frontend/compilation.cc | 2 +- runtimes/neurun/src/frontend/execution.cc | 14 ++-- runtimes/neurun/src/frontend/model.cc | 96 ++++++++++++---------- runtimes/neurun/src/graph/Graph.h | 1 + runtimes/neurun/src/internal/ITensorBuilder.h | 1 + runtimes/neurun/src/internal/Model.h | 6 ++ runtimes/neurun/src/internal/Plan.h | 10 +-- runtimes/neurun/src/internal/Sink.h | 32 +++++++- runtimes/neurun/src/internal/Source.h | 30 +++++++ runtimes/neurun/src/model.cc | 2 +- runtimes/neurun/src/model.h | 8 +- 22 files changed, 181 insertions(+), 90 deletions(-) diff --git a/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.cc b/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.cc index e27e4f9..ff49209 100644 --- a/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.cc +++ b/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.cc @@ -13,7 +13,7 @@ namespace backend namespace acl_cl { -InitializerGenerator::InitializerGenerator(const ::internal::tflite::operand::Set &ctx) : _ctx(ctx) +InitializerGenerator::InitializerGenerator(const neurun::graph::operand::Set &ctx) : _ctx(ctx) { // DO NOTHING } diff --git a/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.h b/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.h index 6884c90..a93fa29 100644 --- a/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.h +++ b/runtimes/neurun/src/backend/acl_cl/InitializerGenerator.h @@ -3,7 +3,7 @@ #include "internal/IInitializerGenerator.h" -#include "internal/Model.h" +#include "graph/operand/Set.h" namespace neurun { @@ -15,7 +15,7 @@ namespace acl_cl class InitializerGenerator : public ::internal::IInitializerGenerator { public: - InitializerGenerator(const ::internal::tflite::operand::Set &ctx); + InitializerGenerator(const neurun::graph::operand::Set &ctx); Initializer generateWeight(const ::internal::tflite::op::Conv2D::implicit::Node &node) override; Initializer generateWeight(const ::internal::tflite::op::FullyConnected::Node &node) override; @@ -24,7 +24,7 @@ public: Initializer generateBias(const ::internal::tflite::op::FullyConnected::Node &node) override; private: - const ::internal::tflite::operand::Set &_ctx; + const neurun::graph::operand::Set &_ctx; }; } // namespace acl_cl diff --git a/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc b/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc index 73d022a..4e229d4 100644 --- a/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc +++ b/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc @@ -99,8 +99,7 @@ void ActivationBuilder::append(FuseCode code, ::arm_compute::ICLTensor *ifm_allo // StageGenerator // StageGenerator::StageGenerator( - const ::internal::tflite::operand::Set &ctx, - const std::shared_ptr &tensor_builder, + const neurun::graph::operand::Set &ctx, const std::shared_ptr &tensor_builder, const std::shared_ptr<::internal::common::TensorBuilder> &common_tensor_builder) : _ctx(ctx), _tensor_builder(tensor_builder), _common_tensor_builder(common_tensor_builder) { diff --git a/runtimes/neurun/src/backend/acl_cl/StageGenerator.h b/runtimes/neurun/src/backend/acl_cl/StageGenerator.h index 6f1dadf..f1bfaaf 100644 --- a/runtimes/neurun/src/backend/acl_cl/StageGenerator.h +++ b/runtimes/neurun/src/backend/acl_cl/StageGenerator.h @@ -3,7 +3,7 @@ #include "internal/IStageGenerator.h" -#include "internal/Model.h" +#include "graph/operand/Set.h" #include "backend/acl_cl/TensorBuilder.h" #include "internal/common/TensorBuilder.h" @@ -17,7 +17,7 @@ namespace acl_cl class StageGenerator : public ::internal::IStageGenerator { public: - StageGenerator(const ::internal::tflite::operand::Set &ctx, + StageGenerator(const neurun::graph::operand::Set &ctx, const std::shared_ptr &tensor_builder, const std::shared_ptr<::internal::common::TensorBuilder> &common_tensor_builder); @@ -43,7 +43,7 @@ public: generate(const ::internal::tflite::op::TensorConvert::AclToCommon::Node &node) override; private: - const ::internal::tflite::operand::Set &_ctx; + const neurun::graph::operand::Set &_ctx; std::shared_ptr _tensor_builder; std::shared_ptr<::internal::common::TensorBuilder> _common_tensor_builder; }; diff --git a/runtimes/neurun/src/backend/cpu/InitializerGenerator.cc b/runtimes/neurun/src/backend/cpu/InitializerGenerator.cc index 2af84a2..970dd9f 100644 --- a/runtimes/neurun/src/backend/cpu/InitializerGenerator.cc +++ b/runtimes/neurun/src/backend/cpu/InitializerGenerator.cc @@ -13,7 +13,7 @@ namespace backend namespace cpu { -InitializerGenerator::InitializerGenerator(const ::internal::tflite::operand::Set &ctx) : _ctx(ctx) +InitializerGenerator::InitializerGenerator(const neurun::graph::operand::Set &ctx) : _ctx(ctx) { // DO NOTHING } diff --git a/runtimes/neurun/src/backend/cpu/InitializerGenerator.h b/runtimes/neurun/src/backend/cpu/InitializerGenerator.h index 2ce8e4a..dd621ee 100644 --- a/runtimes/neurun/src/backend/cpu/InitializerGenerator.h +++ b/runtimes/neurun/src/backend/cpu/InitializerGenerator.h @@ -3,7 +3,7 @@ #include "internal/IInitializerGenerator.h" -#include "internal/Model.h" +#include "graph/operand/Set.h" namespace neurun { @@ -15,7 +15,7 @@ namespace cpu class InitializerGenerator : public ::internal::IInitializerGenerator { public: - InitializerGenerator(const ::internal::tflite::operand::Set &ctx); + InitializerGenerator(const neurun::graph::operand::Set &ctx); Initializer generateWeight(const ::internal::tflite::op::Conv2D::implicit::Node &node) override; Initializer generateWeight(const ::internal::tflite::op::FullyConnected::Node &node) override; @@ -24,7 +24,7 @@ public: Initializer generateBias(const ::internal::tflite::op::FullyConnected::Node &node) override; private: - const ::internal::tflite::operand::Set &_ctx; + const neurun::graph::operand::Set &_ctx; }; } // namespace cpu diff --git a/runtimes/neurun/src/backend/cpu/StageGenerator.cc b/runtimes/neurun/src/backend/cpu/StageGenerator.cc index 0993bda..66060e8 100644 --- a/runtimes/neurun/src/backend/cpu/StageGenerator.cc +++ b/runtimes/neurun/src/backend/cpu/StageGenerator.cc @@ -27,7 +27,7 @@ namespace cpu { StageGenerator::StageGenerator( - const ::internal::tflite::operand::Set &operand_ctx, + const neurun::graph::operand::Set &operand_ctx, const std::shared_ptr &tensor_builder, const std::shared_ptr<::internal::common::TensorBuilder> &common_tensor_builder) : _ctx(operand_ctx), _tensor_builder(tensor_builder), diff --git a/runtimes/neurun/src/backend/cpu/StageGenerator.h b/runtimes/neurun/src/backend/cpu/StageGenerator.h index 7b33a4a..1426671 100644 --- a/runtimes/neurun/src/backend/cpu/StageGenerator.h +++ b/runtimes/neurun/src/backend/cpu/StageGenerator.h @@ -3,7 +3,7 @@ #include "internal/IStageGenerator.h" -#include "internal/Model.h" +#include "graph/operand/Set.h" #include "internal/cpu.h" #include "TensorBuilder.h" @@ -19,7 +19,7 @@ namespace cpu class StageGenerator : public ::internal::IStageGenerator { public: - StageGenerator(const ::internal::tflite::operand::Set &ctx, + StageGenerator(const neurun::graph::operand::Set &ctx, const std::shared_ptr &tensor_builder, const std::shared_ptr<::internal::common::TensorBuilder> &common_tensor_builder); @@ -45,7 +45,7 @@ public: generate(const ::internal::tflite::op::TensorConvert::AclToCommon::Node &node) override; private: - const ::internal::tflite::operand::Set &_ctx; + const neurun::graph::operand::Set &_ctx; std::shared_ptr _tensor_builder; std::shared_ptr<::internal::common::TensorBuilder> _common_tensor_builder; }; diff --git a/runtimes/neurun/src/compilation.cc b/runtimes/neurun/src/compilation.cc index c9efb42..990bf92 100644 --- a/runtimes/neurun/src/compilation.cc +++ b/runtimes/neurun/src/compilation.cc @@ -1,5 +1,6 @@ #include +#include #include #include @@ -166,7 +167,7 @@ std::shared_ptr<::internal::common::TensorBuilder> BackendResolver::getCommonTen class Planner : public ::internal::tflite::op::NodeVisitor { public: - Planner(const ::internal::tflite::operand::Set &ctx, IPlanBuilder &builder, + Planner(const neurun::graph::operand::Set &ctx, IPlanBuilder &builder, BackendResolver &backend_resolver) : _ctx{ctx}, _builder{builder}, _backend_resolver(backend_resolver) { @@ -186,7 +187,7 @@ public: void visit(const ::internal::tflite::op::TensorConvert::AclToCommon::Node &node) override; private: - const ::internal::tflite::operand::Set &_ctx; + const neurun::graph::operand::Set &_ctx; IPlanBuilder &_builder; BackendResolver &_backend_resolver; }; @@ -609,9 +610,6 @@ void PlanBuilder::finalize(BackendResolver &backend_resolver) auto tensor_builders = backend_resolver.getAllTensorBuilders(); auto common_tensor_builder = backend_resolver.getCommonTensorBuilder(); - // Mark tensors - const auto &operations = _plan.model().operations(); - // Prepare tensors for (auto &tensor_builder : tensor_builders) { @@ -656,7 +654,18 @@ int ANeuralNetworksCompilation::finish() auto &plan = this->plan(); const auto &operands = plan.model().operands(); - auto &operations = plan.model().operations(); + + // Get linearized ops + std::vector operations; + { + plan.model().iteratePostDfs([&](const neurun::graph::operation::Node &node) { + auto op = node.op(); + operations.emplace_back(op); + // dynamic_cast(op) + }); + + std::reverse(std::begin(operations), std::end(operations)); + } ::internal::BackendManager backend_manager{plan}; BackendResolver backend_resolver{backend_manager}; @@ -664,21 +673,24 @@ int ANeuralNetworksCompilation::finish() for (uint32_t n = 0; n < operations.size(); ++n) { - const auto &op = operations.at(n); + const auto &op = *operations.at(n); auto tensor_builder = backend_resolver.getTensorBuilder(typeid(op)); op.accept(TensorMarker{*tensor_builder}); } +#if 0 // Tensor Conversion disabled auto tensor_builders = backend_resolver.getAllTensorBuilders(); for (auto tensor_builder : tensor_builders) { tensor_builder->insertTensorConvertNodes(operations); } +#endif for (uint32_t n = 0; n < operations.size(); ++n) { - operations.at(n).accept(Planner{operands, plan_builder, backend_resolver}); + const auto &op = *operations.at(n); + op.accept(Planner{operands, plan_builder, backend_resolver}); } // TODO Add optimization passes diff --git a/runtimes/neurun/src/compilation.h b/runtimes/neurun/src/compilation.h index dc7e00b..609d8a1 100644 --- a/runtimes/neurun/src/compilation.h +++ b/runtimes/neurun/src/compilation.h @@ -1,13 +1,13 @@ #ifndef __COMPILATION_H__ #define __COMPILATION_H__ -#include "internal/Model.h" #include "internal/Plan.h" +#include "graph/Graph.h" struct ANeuralNetworksCompilation { public: - ANeuralNetworksCompilation(const std::shared_ptr &model) + ANeuralNetworksCompilation(const std::shared_ptr &model) : _plan{new internal::Plan{model}} { // DO NOTHING diff --git a/runtimes/neurun/src/execution.h b/runtimes/neurun/src/execution.h index 45d2011..91b20a2 100644 --- a/runtimes/neurun/src/execution.h +++ b/runtimes/neurun/src/execution.h @@ -10,8 +10,8 @@ struct ANeuralNetworksExecution public: ANeuralNetworksExecution(const std::shared_ptr &plan) : _plan{plan} { - _sources.resize(_plan->model().inputs.size()); - _sinks.resize(_plan->model().outputs.size()); + _sources.resize(_plan->model().inputs().size()); + _sinks.resize(_plan->model().outputs().size()); } public: diff --git a/runtimes/neurun/src/frontend/compilation.cc b/runtimes/neurun/src/frontend/compilation.cc index c0639a3..af14604 100644 --- a/runtimes/neurun/src/frontend/compilation.cc +++ b/runtimes/neurun/src/frontend/compilation.cc @@ -16,7 +16,7 @@ int ANeuralNetworksCompilation_create(ANeuralNetworksModel *model, return ANEURALNETWORKS_UNEXPECTED_NULL; } - std::shared_ptr internal; + std::shared_ptr internal; model->release(internal); diff --git a/runtimes/neurun/src/frontend/execution.cc b/runtimes/neurun/src/frontend/execution.cc index a3d4ced..0a4de81 100644 --- a/runtimes/neurun/src/frontend/execution.cc +++ b/runtimes/neurun/src/frontend/execution.cc @@ -51,7 +51,7 @@ int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution *execution, int32 // NOTE The current implemenation assumes that every input is a feature map. // TODO Remove this assumption - const auto operand_index = execution->plan().model().inputs.at(index); + const auto operand_index = execution->plan().model().inputs().at(index); if (operands.at(operand_index).shape().rank() == 2) { @@ -95,7 +95,7 @@ int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution *execution, int3 // NOTE The current implemenation assumes that every output is a feature map. // TODO Remove this assumption - const auto operand_index = execution->plan().model().outputs.at(index); + const auto operand_index = execution->plan().model().outputs().list().at(index); if (operands.at(operand_index).shape().rank() == 2) { @@ -138,11 +138,12 @@ int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, const auto &model = plan.model(); // Set input(s) - for (uint32_t n = 0; n < model.inputs.size(); ++n) + for (uint32_t n = 0; n < model.inputs().size(); ++n) { auto setter = [&](::arm_compute::ITensor &tensor) { execution->source(n).push(tensor); }; - auto objects = plan.common_operands().at(model.inputs.at(n)); + ::internal::tflite::operand::Index index{model.inputs().at(n).asInt()}; + auto objects = plan.operands().at(index); for (auto object : objects) { @@ -158,11 +159,12 @@ int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, } // Get output(s) - for (uint32_t n = 0; n < model.outputs.size(); ++n) + for (uint32_t n = 0; n < model.outputs().size(); ++n) { auto getter = [&](::arm_compute::ITensor &tensor) { execution->sink(n).pull(tensor); }; - auto objects = plan.common_operands().at(model.outputs.at(n)); + ::internal::tflite::operand::Index index{model.outputs().at(n).asInt()}; + auto objects = plan.operands().at(index); for (auto object : objects) { diff --git a/runtimes/neurun/src/frontend/model.cc b/runtimes/neurun/src/frontend/model.cc index 446b977..3900570 100644 --- a/runtimes/neurun/src/frontend/model.cc +++ b/runtimes/neurun/src/frontend/model.cc @@ -5,8 +5,18 @@ #include #include +#include "nnfw/std/memory.h" + +#include "graph/Graph.h" #include "model.h" #include "memory.h" +#include "graph/operation/AvgPool2D.h" +#include "graph/operation/Concat.h" +#include "graph/operation/Conv2D.h" +#include "graph/operation/FullyConnected.h" +#include "graph/operation/MaxPool2D.h" +#include "graph/operation/Reshape.h" +#include "graph/operation/Softmax.h" int ANeuralNetworksModel_create(ANeuralNetworksModel **model) { @@ -83,7 +93,7 @@ int ANeuralNetworksModel_addOperand(ANeuralNetworksModel *model, shape.set(type->type, type->scale, type->zeroPoint); - model->deref().operands().append(shape); + model->deref().addOperand(shape); // NOTE We do NOT allocate CLTensor here as we do not how to interpret this one. // TensorFlow Lite may interpret a rank-4 tensor either as a feature map (with batch) or @@ -105,16 +115,18 @@ int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel *model, int32_t in return ANEURALNETWORKS_BAD_STATE; } - const internal::tflite::operand::Index ind{index}; - auto &obj = model->deref().operands().at(ind); - - using internal::tflite::operand::CachedData; + const neurun::graph::operand::Index ind(index); + auto &obj = model->deref().operands().at(ind); if (!obj.setAsConstant()) { return ANEURALNETWORKS_BAD_DATA; } - obj.data(reinterpret_cast(buffer), length); + + using internal::tflite::operand::CachedData; + + model->deref().setOperandValue( + ind, nnfw::make_unique(reinterpret_cast(buffer), length)); return ANEURALNETWORKS_NO_ERROR; } @@ -133,16 +145,19 @@ int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel *model, return ANEURALNETWORKS_BAD_STATE; } - const internal::tflite::operand::Index ind{index}; - auto &obj = model->deref().operands().at(ind); - - using internal::tflite::operand::ExternalData; + const neurun::graph::operand::Index ind(index); + auto &obj = model->deref().operands().at(ind); if (!obj.setAsConstant()) { return ANEURALNETWORKS_BAD_DATA; } - obj.data(reinterpret_cast(memory->base() + offset), length); + + using internal::tflite::operand::ExternalData; + + model->deref().setOperandValue( + ind, nnfw::make_unique( + reinterpret_cast(memory->base() + offset), length)); return ANEURALNETWORKS_NO_ERROR; } @@ -183,6 +198,8 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, } } + auto &graph = model->deref(); + switch (type) { case ANEURALNETWORKS_CONV_2D: @@ -197,11 +214,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::Conv2D::implicit::Param; using internal::tflite::op::Conv2D::implicit::Node; + using GraphNode = neurun::graph::operation::Conv2D::Implicit::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); } else { @@ -222,11 +238,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::MaxPool2D::implicit::Param; using internal::tflite::op::MaxPool2D::implicit::Node; + using GraphNode = neurun::graph::operation::MaxPool2D::Implicit::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); } else { @@ -247,11 +262,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::AvgPool2D::implicit::Param; using internal::tflite::op::AvgPool2D::implicit::Node; + using GraphNode = neurun::graph::operation::AvgPool2D::Implicit::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); } else { @@ -264,11 +278,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::Concat::Param; using internal::tflite::op::Concat::Node; + using GraphNode = neurun::graph::operation::Concat::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); break; } @@ -276,11 +289,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::Reshape::Param; using internal::tflite::op::Reshape::Node; + using GraphNode = neurun::graph::operation::Reshape::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); break; } @@ -288,11 +300,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::FullyConnected::Param; using internal::tflite::op::FullyConnected::Node; + using GraphNode = neurun::graph::operation::FullyConnected::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); break; } @@ -300,11 +311,10 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, { using internal::tflite::op::Softmax::Param; using internal::tflite::op::Softmax::Node; + using GraphNode = neurun::graph::operation::Softmax::Node; - // Add 'operations' - auto &operations = model->deref().operations(); - - operations.emplace_back(Param{inputCount, inputs, outputCount, outputs}); + graph.addOperation(nnfw::make_unique( + nnfw::make_unique(Param{inputCount, inputs, outputCount, outputs}))); break; } @@ -381,8 +391,8 @@ int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, u // Below, static_cast(...) is introduced to eliminate compiler warning. for (uint32_t n = 0; n < inputCount; ++n) { - const ::internal::tflite::operand::Index ind{static_cast(inputs[n])}; - model->deref().inputs.emplace_back(ind); + const neurun::graph::operand::Index ind{static_cast(inputs[n])}; + model->deref().addInput(ind); auto &obj = model->deref().operands().at(ind); if (!obj.setAsModelInput()) @@ -393,8 +403,8 @@ int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, u for (uint32_t n = 0; n < outputCount; ++n) { - const ::internal::tflite::operand::Index ind{static_cast(outputs[n])}; - model->deref().outputs.emplace_back(ind); + const neurun::graph::operand::Index ind{static_cast(outputs[n])}; + model->deref().addOutput(ind); auto &obj = model->deref().operands().at(ind); // Model output cannot become model input diff --git a/runtimes/neurun/src/graph/Graph.h b/runtimes/neurun/src/graph/Graph.h index 0c35843..a4b762e 100644 --- a/runtimes/neurun/src/graph/Graph.h +++ b/runtimes/neurun/src/graph/Graph.h @@ -32,6 +32,7 @@ public: const operand::IndexSet &inputs() const { return _inputs; } const operand::IndexSet &outputs() const { return _outputs; } const operand::Set &operands() const { return _operands; } + operand::Set &operands() { return _operands; } // TODO Remove this non-const accessor public: // TODO Introduce Iterator class to support many kinds of interation diff --git a/runtimes/neurun/src/internal/ITensorBuilder.h b/runtimes/neurun/src/internal/ITensorBuilder.h index 90e3f49..13206b0 100644 --- a/runtimes/neurun/src/internal/ITensorBuilder.h +++ b/runtimes/neurun/src/internal/ITensorBuilder.h @@ -5,6 +5,7 @@ #include #include "internal/Model.h" +#include "graph/operand/Index.h" namespace internal { diff --git a/runtimes/neurun/src/internal/Model.h b/runtimes/neurun/src/internal/Model.h index 0df6dbd..b852bac 100644 --- a/runtimes/neurun/src/internal/Model.h +++ b/runtimes/neurun/src/internal/Model.h @@ -1,6 +1,8 @@ #ifndef __INTERNAL_MODEL_H__ #define __INTERNAL_MODEL_H__ +#include "graph/operand/Index.h" + namespace internal { namespace tflite @@ -16,6 +18,10 @@ public: // DO NOTHING } + // NOTE Temporary casting operator for legacy code compatibility +public: + operator neurun::graph::operand::Index() const { return neurun::graph::operand::Index{_value}; } + public: int asInt(void) const { return _value; } diff --git a/runtimes/neurun/src/internal/Plan.h b/runtimes/neurun/src/internal/Plan.h index 547b7e7..a950db5 100644 --- a/runtimes/neurun/src/internal/Plan.h +++ b/runtimes/neurun/src/internal/Plan.h @@ -1,7 +1,7 @@ #ifndef __INTERNAL_PLAN_H__ #define __INTERNAL_PLAN_H__ -#include "internal/Model.h" +#include "graph/Graph.h" #include "internal/IObject.h" #include @@ -77,14 +77,14 @@ namespace internal class Plan { public: - Plan(const std::shared_ptr<::internal::tflite::Model> &model) : _model(model) + Plan(const std::shared_ptr &model) : _model(model) { // DO NOTHING } public: - ::internal::tflite::Model &model(void) { return *_model; } - const ::internal::tflite::Model &model(void) const { return *_model; } + neurun::graph::Graph &model(void) { return *_model; } + const neurun::graph::Graph &model(void) const { return *_model; } public: operand::Context &operands(void) { return _operands; } @@ -99,7 +99,7 @@ public: const op::Sequence &operations(void) const { return _ops; } private: - std::shared_ptr<::internal::tflite::Model> _model; + std::shared_ptr _model; operand::Context _operands; operand::Context _common_operands; op::Sequence _ops; diff --git a/runtimes/neurun/src/internal/Sink.h b/runtimes/neurun/src/internal/Sink.h index cecf1e6..4c6f691 100644 --- a/runtimes/neurun/src/internal/Sink.h +++ b/runtimes/neurun/src/internal/Sink.h @@ -35,8 +35,11 @@ public: public: void pull(::arm_compute::ITensor &tensor) const override { +// NOTE Leave common tensor conversion code before Graph IR supports tensor conversion +#if 0 // Only for common tensor now - assert(typeid(tensor) == typeid(::internal::common::Tensor)); + assert(typeid(tensor) == typeid(::internal::common::Tensor)); +#endif float *base = reinterpret_cast(_base); @@ -69,6 +72,32 @@ public: public: void pull(::arm_compute::ITensor &tensor) const override { + // TODO: This is just workaround codes, It needs to refactor. + if (typeid(tensor) == typeid(::internal::cpu::Tensor)) + { + const ::internal::nnapi::feature::Reader from{_shape, tensor.buffer(), _size}; + ::internal::nnapi::feature::View into{_shape, _base, _size}; + + ::nnfw::util::feature::iterate(_shape) + << [&](uint32_t bat, uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(bat, ch, row, col); + into.at(bat, ch, row, col) = value; + }; + } + else if (typeid(tensor) == typeid(::arm_compute::CLTensor)) + { + const ::internal::arm_compute::feature::View from{&tensor}; + ::internal::nnapi::feature::View into{_shape, _base, _size}; + + ::nnfw::util::feature::iterate(_shape) + << [&](uint32_t bat, uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(bat, ch, row, col); + into.at(bat, ch, row, col) = value; + }; + } + +// NOTE Leave common tensor conversion code before Graph IR supports tensor conversion +#if 0 // Only for common tensor now assert(typeid(tensor) == typeid(::internal::common::Tensor)); @@ -81,6 +110,7 @@ public: const auto value = from.at(batch, ch, row, col); into.at(batch, ch, row, col) = value; }; +#endif } private: diff --git a/runtimes/neurun/src/internal/Source.h b/runtimes/neurun/src/internal/Source.h index 8ca0203..e5270ad 100644 --- a/runtimes/neurun/src/internal/Source.h +++ b/runtimes/neurun/src/internal/Source.h @@ -38,8 +38,11 @@ public: public: void push(::arm_compute::ITensor &tensor) const override { +// NOTE Leave common tensor conversion code before Graph IR supports tensor conversion +#if 0 // Only for common tensor now assert(typeid(tensor) == typeid(::internal::common::Tensor)); +#endif auto base = reinterpret_cast(_base); @@ -72,6 +75,32 @@ public: public: void push(::arm_compute::ITensor &tensor) const override { + // TODO: This is just workaround codes, It needs to refactor. + if (typeid(tensor) == typeid(::internal::cpu::Tensor)) + { + const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; + ::internal::nnapi::feature::View into{_shape, tensor.buffer(), _size}; + + ::nnfw::util::feature::iterate(_shape) + << [&](uint32_t bat, uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(bat, ch, row, col); + into.at(bat, ch, row, col) = value; + }; + } + else if (typeid(tensor) == typeid(::arm_compute::CLTensor)) + { + const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; + ::internal::arm_compute::feature::View into{&tensor}; + + ::nnfw::util::feature::iterate(_shape) + << [&](uint32_t bat, uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(bat, ch, row, col); + into.at(bat, ch, row, col) = value; + }; + } + +// NOTE Leave common tensor conversion code before Graph IR supports tensor conversion +#if 0 // Only for common tensor now assert(typeid(tensor) == typeid(::internal::common::Tensor)); @@ -84,6 +113,7 @@ public: const auto value = from.at(batch, ch, row, col); into.at(batch, ch, row, col) = value; }; +#endif } private: diff --git a/runtimes/neurun/src/model.cc b/runtimes/neurun/src/model.cc index 32c16c0..f3ad0b4 100644 --- a/runtimes/neurun/src/model.cc +++ b/runtimes/neurun/src/model.cc @@ -3,7 +3,7 @@ // // ANeuralNetworksModel // -ANeuralNetworksModel::ANeuralNetworksModel() : _model{new internal::tflite::Model}, _finished(false) +ANeuralNetworksModel::ANeuralNetworksModel() : _model{new neurun::graph::Graph}, _finished(false) { // DO NOTHING } diff --git a/runtimes/neurun/src/model.h b/runtimes/neurun/src/model.h index e9c1009..b94dc36 100644 --- a/runtimes/neurun/src/model.h +++ b/runtimes/neurun/src/model.h @@ -3,7 +3,7 @@ #include -#include "internal/Model.h" +#include "graph/Graph.h" struct ANeuralNetworksModel { @@ -11,15 +11,15 @@ public: ANeuralNetworksModel(); public: - internal::tflite::Model &deref(void) { return *_model; } + neurun::graph::Graph &deref(void) { return *_model; } ResultCode finish(); bool isFinished() { return _finished; } public: - void release(std::shared_ptr &model) { model = _model; } + void release(std::shared_ptr &model) { model = _model; } private: - std::shared_ptr _model; + std::shared_ptr _model; bool _finished; }; -- 2.7.4