diff options
Diffstat (limited to 'runtime/neurun/core/src/compiler/Compiler.cc')
-rw-r--r-- | runtime/neurun/core/src/compiler/Compiler.cc | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/runtime/neurun/core/src/compiler/Compiler.cc b/runtime/neurun/core/src/compiler/Compiler.cc new file mode 100644 index 000000000..d5ee39d85 --- /dev/null +++ b/runtime/neurun/core/src/compiler/Compiler.cc @@ -0,0 +1,143 @@ +/* + * 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/Compiler.h" + +#include "BackendResolver.h" +#include "ParamChecker.h" +#include "ExecutorFactory.h" +#include "OperationValidator.h" + +#include "compiler/IScheduler.h" +#include "compiler/ManualScheduler.h" +#include "compiler/HEScheduler.h" +#include "backend/ExecTime.h" +#include "ir/operation/LowerInfo.h" +#include "dumper/dot/DotDumper.h" +#include "compiler/Linear.h" +#include "exec/interp/ExecManager.h" +#include "backend/ExecTime.h" +#include "util/ConfigSource.h" + +namespace neurun +{ + +namespace compiler +{ + +static void checkProfilerConditions() +{ + if (!util::getConfigBool(util::config::USE_SCHEDULER)) + throw std::runtime_error("Heterogeneous scheduler must be enabled during profiling."); + + if (util::getConfigString(util::config::EXECUTOR) != "Dataflow") + throw std::runtime_error("Profiling mode works only with 'Dataflow' executor"); +} + +void Compiler::compile(void) +{ + _state = State::STARTED; + + /*************************************************** + * Prepare compilation phase + ***************************************************/ + + // Operation validation check + OperationValidator{*_graph}(); + + // Compilable check + if (!checkCompilable()) + { + _executor = std::make_shared<exec::interp::ExecManager>(*_graph); + return; + } + + // Mode check + if (util::getConfigBool(util::config::PROFILING_MODE)) + checkProfilerConditions(); + + /*************************************************** + * Backend independent analysis & optimization phase + ***************************************************/ + // Schedule + std::unique_ptr<BackendResolver> br; + std::shared_ptr<ir::OperationIndexMap<int64_t>> indexed_ranks; + if (util::getConfigBool(util::config::USE_SCHEDULER)) + { + auto scheduler = compiler::HEScheduler( + _graph->operands(), backend::BackendManager::get().getAll(), _graph->getKernelBuilder()); + br = scheduler.schedule(*_graph); + indexed_ranks = scheduler.getIndexedRanks(); + } + else + { + auto scheduler = compiler::ManualScheduler(); + br = scheduler.schedule(*_graph); + } + _graph->setBackendResolver(std::move(br)); + /************************************************************* + * Backend independent analysis & optimization phase finished + *************************************************************/ + + // dump graph to .dot + auto dump_level = + static_cast<dumper::dot::DotDumper::Level>(util::getConfigInt(util::config::GRAPH_DOT_DUMP)); + neurun::dumper::dot::DotDumper dot_dumper(*_graph, dump_level); + dot_dumper.dump("before_lower"); + + // Lower: decide backend + _graph->lower(); + _state = State::LOWERED; + + dot_dumper.dump("after_lower"); + + const std::string executor_str = util::getConfigString(util::config::EXECUTOR); + + _executor = + std::shared_ptr<exec::IExecutor>{ExecutorFactory::get().create(executor_str, *_graph)}; + _executor->setIndexedRanks(indexed_ranks); + /******************************** + * Code generation phase finished + ********************************/ + _state = State::COMPILED; +} + +bool Compiler::checkCompilable() +{ + // Disable compile phase + // When ready to use interpreter backend, remove this config and use backend setting + const auto env_disable_compile = util::getConfigBool(util::config::DISABLE_COMPILE); + if (env_disable_compile) + { + return false; + } + + // TODO check unspecified operand shape + + // Check compilable parameter + ParamChecker paramChecker{_graph}; + paramChecker(); + if (paramChecker.haveNoneConstParam()) + { + return false; + } + + return true; +} + +} // namespace compiler + +} // namespace neurun |