Introduce neurun::compiler::Compiler class to hide compilation phase from frontend
Introduce src/compiler directory for compile phases
Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
file(GLOB_RECURSE SOURCES_LINEAR "src/linear/*.cc")
file(GLOB_RECURSE SOURCES_CODEGEN "src/codegen/*.cc")
file(GLOB_RECURSE SOURCES_DUMPER "src/dumper/*.cc")
+file(GLOB_RECURSE SOURCES_COMPILER "src/compiler/*.cc")
file(GLOB_RECURSE SOURCES_VERIFIER "src/verifier/*.cc")
file(GLOB_RECURSE SOURCES_VERIFIER "src/util/*.cc")
-set(SOURCES ${SOURCES} ${SOURCES_FRONTEND} ${SOURCES_MIDDLEEND} ${SOURCES_BACKEND} ${SOURCES_INTERNAL} ${SOURCES_GRAPH} ${SOURCES_LINEAR} ${SOURCES_CODEGEN} ${SOURCES_DUMPER} ${SOURCES_VERIFIER})
+set(SOURCES ${SOURCES} ${SOURCES_FRONTEND} ${SOURCES_MIDDLEEND} ${SOURCES_BACKEND} ${SOURCES_INTERNAL} ${SOURCES_GRAPH} ${SOURCES_LINEAR} ${SOURCES_CODEGEN} ${SOURCES_DUMPER} ${SOURCES_COMPILER} ${SOURCES_VERIFIER})
# NOTE For now ARMCompute is necessary
# TODO Remove required package below(should be optional)
--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Compiler.h"
+
+#include "graph/dumper/Dumper.h"
+#include "dumper/dot/DotDumper.h"
+#include "codegen/IPlanBuilder.h"
+#include "codegen/OperationValidator.h"
+#include "codegen/PlanBuilder.h"
+#include "middleend/SubTensorAnalyzer.h"
+#include "codegen/ConstantInitializer.h"
+#include "graph/operation/LowerInfo.h"
+
+#include "linear/Linear.h"
+#include "util/EnvVar.h"
+
+namespace neurun
+{
+
+namespace compiler
+{
+
+void Compiler::compile(void)
+{
+ auto &plan = this->plan();
+ auto &graph = plan.model();
+ const auto &operands = graph.operands();
+
+ // dump graph to .dot
+ // TODO : These can be moved to another place.
+ const auto &dotdump_env = ::nnfw::util::EnvVar{"GRAPH_DOT_DUMP"}.asInt(0);
+ if (dotdump_env)
+ {
+ neurun::dumper::dot::DotDumper dot_dumper(graph);
+ dot_dumper.dump("before_lower", dotdump_env);
+ }
+
+ graph.lower();
+
+ if (dotdump_env)
+ {
+ neurun::dumper::dot::DotDumper dot_dumper(graph);
+ dot_dumper.dump("after_lower", dotdump_env);
+ }
+
+ auto linear = graph.linearize();
+
+ // Dump ops
+ linear->accept(neurun::graph::dumper::Dumper{});
+
+ // SubTensorInfo should be generated after lower, before planner & finalize
+ // lower: decide backend and insert permutation
+ // planner: stage generate (use SubTensorInfo to return stage. prepare to optimization)
+ // finalize: generate tensor using subtensor info, then execute stage
+ // Generated SubTensorInfo is in operand(Object)
+ // for easy pass SubTensorInfo to plan builder and tensor builder
+ linear->accept(neurun::middleend::SubTensorAnalyzer{graph.operands()});
+
+ neurun::codegen::PlanBuilder plan_builder{plan};
+
+ linear->accept(neurun::codegen::OperationValidator{operands});
+
+ // Plan building
+ linear->iterate([&](const neurun::graph::operation::Node *node) {
+ auto backend = node->lower_info()->backend();
+
+ // Generate Stage
+ auto stage_gen = backend->stage_gen();
+ plan_builder.addStage(stage_gen->generate(*node));
+ });
+
+ auto tensor_builders = linear->planTensors();
+
+ // TODO Add optimization passes
+ plan_builder.finalize(tensor_builders);
+
+ neurun::codegen::ConstantInitializer{graph, plan}();
+}
+
+} // namespace compiler
+
+} // namespace neurun
--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file Compiler.h
+ * @brief This file contains Compiler class to define and run compilation phase
+ */
+
+#ifndef __NEURUN_COMPILER_COMPILE_H_
+#define __NEURUN_COMPILER_COMPILE_H_
+
+#include "graph/Graph.h"
+#include "codegen/Plan.h"
+
+namespace neurun
+{
+
+namespace compiler
+{
+
+/**
+ * @brief Class to compile graph model
+ */
+class Compiler
+{
+public:
+ /**
+ * @brief Construct a new Compiler object
+ * @param[in] model Graph model
+ */
+ Compiler(const std::shared_ptr<graph::Graph> &model) : _plan{new codegen::Plan{model}}
+ {
+ // DO NOTHING
+ }
+
+public:
+ /**
+ * @brief Return plan
+ * @return Plan
+ */
+ codegen::Plan &plan(void) { return *_plan; }
+ /**
+ * @brief Run compilation
+ */
+ void compile(void);
+ /**
+ * @brief Pass plan reference
+ * @param[out] plan Plan reference to return
+ */
+ void release(std::shared_ptr<const codegen::Plan> &plan) { plan = _plan; }
+
+private:
+ std::shared_ptr<codegen::Plan> _plan;
+};
+
+} // namespace compiler
+
+} // namespace neurun
+
+#endif // __NEURUN_COMPILER_COMPILE_H_
*/
#include "compilation.h"
-#include "model.h"
-
-#include "graph/dumper/Dumper.h"
-#include "dumper/dot/DotDumper.h"
-#include "codegen/IPlanBuilder.h"
-#include "codegen/OperationValidator.h"
-#include "codegen/PlanBuilder.h"
-#include "middleend/SubTensorAnalyzer.h"
-#include "codegen/ConstantInitializer.h"
-#include "graph/operation/LowerInfo.h"
-
-#include "linear/Linear.h"
-#include "util/EnvVar.h"
int ANeuralNetworksCompilation::finish()
{
- auto &plan = this->plan();
- auto &graph = plan.model();
- const auto &operands = graph.operands();
-
- // dump graph to .dot
- // TODO : These can be moved to another place.
- const auto &dotdump_env = ::nnfw::util::EnvVar{"GRAPH_DOT_DUMP"}.asInt(0);
- if (dotdump_env)
+ try
{
- neurun::dumper::dot::DotDumper dot_dumper(graph);
- dot_dumper.dump("before_lower", dotdump_env);
+ _compiler->compile();
}
-
- graph.lower();
-
- if (dotdump_env)
+ catch (const std::exception &e)
{
- neurun::dumper::dot::DotDumper dot_dumper(graph);
- dot_dumper.dump("after_lower", dotdump_env);
+ return ANEURALNETWORKS_BAD_STATE;
}
- auto linear = graph.linearize();
-
- // Dump ops
- linear->accept(neurun::graph::dumper::Dumper{});
-
- // SubTensorInfo should be generated after lower, before planner & finalize
- // lower: decide backend and insert permutation
- // planner: stage generate (use SubTensorInfo to return stage. prepare to optimization)
- // finalize: generate tensor using subtensor info, then execute stage
- // Generated SubTensorInfo is in operand(Object)
- // for easy pass SubTensorInfo to plan builder and tensor builder
- linear->accept(neurun::middleend::SubTensorAnalyzer{graph.operands()});
-
- neurun::codegen::PlanBuilder plan_builder{plan};
-
- linear->accept(neurun::codegen::OperationValidator{operands});
-
- // Plan building
- linear->iterate([&](const neurun::graph::operation::Node *node) {
- auto backend = node->lower_info()->backend();
-
- // Generate Stage
- auto stage_gen = backend->stage_gen();
- plan_builder.addStage(stage_gen->generate(*node));
- });
-
- auto tensor_builders = linear->planTensors();
-
- // TODO Add optimization passes
- plan_builder.finalize(tensor_builders);
-
- neurun::codegen::ConstantInitializer{graph, plan}();
-
return ANEURALNETWORKS_NO_ERROR;
}
#include "codegen/Plan.h"
#include "graph/Graph.h"
+#include "compiler/Compiler.h"
struct ANeuralNetworksCompilation
{
public:
ANeuralNetworksCompilation(const std::shared_ptr<neurun::graph::Graph> &model)
- : _plan{new neurun::codegen::Plan{model}}
+ : _compiler{new neurun::compiler::Compiler{model}}
{
// DO NOTHING
}
public:
- neurun::codegen::Plan &plan(void) { return *_plan; }
+ neurun::codegen::Plan &plan(void) { return _compiler->plan(); }
public:
- void publish(std::shared_ptr<const neurun::codegen::Plan> &plan) { plan = _plan; }
+ void publish(std::shared_ptr<const neurun::codegen::Plan> &plan) { _compiler->release(plan); }
int finish();
private:
- std::shared_ptr<neurun::codegen::Plan> _plan;
+ std::shared_ptr<neurun::compiler::Compiler> _compiler;
};
#endif