diff options
Diffstat (limited to 'runtimes/neurun/src')
198 files changed, 0 insertions, 16186 deletions
diff --git a/runtimes/neurun/src/backend/BackendManager.cc b/runtimes/neurun/src/backend/BackendManager.cc deleted file mode 100644 index 5d19d4015..000000000 --- a/runtimes/neurun/src/backend/BackendManager.cc +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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 <dlfcn.h> -#include "BackendManager.h" - -#include "backend/interface/IConfig.h" -#include "backend/interface/ITensorBuilder.h" -#include "backend/interface/IStageGenerator.h" -#include "util/logging.h" -#include "util/config/ConfigManager.h" - -namespace neurun -{ -namespace backend -{ - -Backend::Backend(const std::shared_ptr<neurun::backend::IConfig> &backend_config, - const std::shared_ptr<neurun::backend::IStageGenerator> &stage_gen) - : _config(backend_config), _stage_gen(stage_gen) -{ - backend_config->initialize(); -} - -const std::shared_ptr<neurun::backend::IConfig> Backend::config() const { return _config; } - -const std::shared_ptr<neurun::backend::IStageGenerator> Backend::stage_gen() const -{ - return _stage_gen; -} - -const std::shared_ptr<neurun::backend::ITensorBuilder> Backend::tensor_builder() const -{ - return _stage_gen->tensor_builder(); -} - -template <typename T, class... Types> -void BackendManager::loadObjectFromPlugin(std::shared_ptr<T> &object_of_plugin_class, - const std::string obj_creator_func_name, void *handle, - Types &&... args) -{ - T *(*allocate_obj)(Types && ... Args); - // load object creator function - allocate_obj = (T * (*)(Types && ... Args))dlsym(handle, obj_creator_func_name.c_str()); - if (allocate_obj == nullptr) - { - fprintf(stderr, "BackendManager: unable to open function %s: %s\n", - obj_creator_func_name.c_str(), dlerror()); - abort(); - } - - object_of_plugin_class.reset(allocate_obj(args...)); -} - -void BackendManager::loadBackend(const std::string &backend, - const neurun::model::operand::Set &operands) -{ - const std::string backend_plugin = "libbackend_" + backend + ".so"; - void *handle = dlopen(backend_plugin.c_str(), RTLD_LAZY | RTLD_LOCAL); - if (handle == nullptr) - { - fprintf(stderr, "BackendManager::loadBackend failed to load plugin of %s backend: %s\n", - backend.c_str(), dlerror()); - abort(); - } - VERBOSE(BackendManager::loadBackend) << "loaded " << backend_plugin << " as a plugin of " - << backend << " backend\n"; - - // load Config - std::shared_ptr<neurun::backend::IConfig> config; - loadObjectFromPlugin(config, std::string("allocate_Config"), handle); - - // load TensorBuilder - std::shared_ptr<neurun::backend::ITensorBuilder> tensor_builder; - loadObjectFromPlugin(tensor_builder, std::string("allocate_TensorBuilder"), handle); - - // load StageGenerator - std::shared_ptr<neurun::backend::IStageGenerator> stage_gen; - loadObjectFromPlugin(stage_gen, std::string("allocate_StageGenerator"), handle, operands, - tensor_builder); - _gen_map[config->id()] = {config, stage_gen}; -} - -BackendManager::BackendManager(const neurun::model::operand::Set &operands) -{ - const auto backends = config::ConfigManager::instance().get<std::string>("BACKENDS"); - size_t prev_pos = 0; - auto pos = backends.find(";"); - while (pos != std::string::npos) - { - loadBackend(backends.substr(prev_pos, pos - prev_pos), operands); - prev_pos = pos + 1; - pos = backends.find(";", prev_pos); - } - // if backends doesn't terminate with ";" - if (prev_pos < backends.size()) - { - loadBackend(backends.substr(prev_pos), operands); - } -} - -Backend *BackendManager::get(const std::string &key) { return &_gen_map.at(key); } - -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/BackendManager.h b/runtimes/neurun/src/backend/BackendManager.h deleted file mode 100644 index 428542b1e..000000000 --- a/runtimes/neurun/src/backend/BackendManager.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_BACKEND_MANAGER_H__ -#define __NEURUN_BACKEND_BACKEND_MANAGER_H__ - -#include <memory> -#include <map> - -#include "model/operand/Set.h" - -namespace neurun -{ -namespace backend -{ - -struct IConfig; -struct IStageGenerator; -struct ITensorBuilder; - -class Backend -{ -public: - Backend(const std::shared_ptr<neurun::backend::IConfig> &backend_config, - const std::shared_ptr<neurun::backend::IStageGenerator> &stage_gen); - - Backend(void) : _config(nullptr), _stage_gen(nullptr) - { - // DO NOTHING - } - -public: - const std::shared_ptr<neurun::backend::IConfig> config() const; - const std::shared_ptr<neurun::backend::IStageGenerator> stage_gen() const; - const std::shared_ptr<neurun::backend::ITensorBuilder> tensor_builder() const; - -private: - std::shared_ptr<neurun::backend::IConfig> _config; - std::shared_ptr<neurun::backend::IStageGenerator> _stage_gen; -}; - -class BackendManager -{ -public: - BackendManager(const neurun::model::operand::Set &operands); - - Backend *get(const std::string &key); - -private: - std::map<std::string, Backend> _gen_map; - /** - * @brief Allocate an object of a class of a plugin by loading a plugin function, that does - * allocation, and calling it - * - * @param object_of_plugin_class target object - * @param obj_creator_func_name name of the plugin function, that allocates an object - * @param handle handle of the plugin - * @param args arguments to pass to constructor of the plugin class - * - * @return - */ - template <typename T, class... Types> - void loadObjectFromPlugin(std::shared_ptr<T> &object_of_plugin_class, - const std::string obj_creator_func_name, void *handle, - Types &&... args); - - /** - * @brief load backend plugin - * - * @param backend backend to be loaded - * @param operands operands to construct StageGenerator - * - * @return - */ - void loadBackend(const std::string &backend, const neurun::model::operand::Set &operands); -}; - -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_BACKEND_MANAGER_H__ diff --git a/runtimes/neurun/src/backend/CMakeLists.txt b/runtimes/neurun/src/backend/CMakeLists.txt deleted file mode 100644 index a39823102..000000000 --- a/runtimes/neurun/src/backend/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_subdirectory(cpu) -add_subdirectory(acl_cl) diff --git a/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt b/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt deleted file mode 100644 index f1ea22bc5..000000000 --- a/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -file(GLOB_RECURSE SOURCES "*.cc") - -add_library(${LIB_NEURUN_BACKEND_ACL_CL} SHARED ${SOURCES}) - -target_include_directories(${LIB_NEURUN_BACKEND_ACL_CL} PUBLIC ${NNFW_INCLUDE_DIR}) -target_include_directories(${LIB_NEURUN_BACKEND_ACL_CL} PUBLIC ${NEURUN_INCLUDE_DIR}) - -target_link_libraries(${LIB_NEURUN_BACKEND_ACL_CL} arm_compute) -target_link_libraries(${LIB_NEURUN_BACKEND_ACL_CL} ${LIB_NEURUN_KERNEL_ACL_CL}) -target_link_libraries(${LIB_NEURUN_BACKEND_ACL_CL} ${LIB_NEURUN}) - -target_compile_options(${LIB_NEURUN_BACKEND_ACL_CL} PRIVATE -Wall -Wextra -Werror -Wno-unused-parameter) - -set_target_properties(${LIB_NEURUN_BACKEND_ACL_CL} PROPERTIES OUTPUT_NAME backend_acl_cl) -install(TARGETS ${LIB_NEURUN_BACKEND_ACL_CL} DESTINATION lib/neurun) diff --git a/runtimes/neurun/src/backend/acl_cl/Config.cc b/runtimes/neurun/src/backend/acl_cl/Config.cc deleted file mode 100644 index cad9b8988..000000000 --- a/runtimes/neurun/src/backend/acl_cl/Config.cc +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 <arm_compute/runtime/CL/CLScheduler.h> - -#include "backend/acl_cl/Config.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -void Config::initialize() { arm_compute::CLScheduler::get().default_init(); } - -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/Config.h b/runtimes/neurun/src/backend/acl_cl/Config.h deleted file mode 100644 index cb43bfbe0..000000000 --- a/runtimes/neurun/src/backend/acl_cl/Config.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_CONFIG_H__ -#define __NEURUN_BACKEND_ACL_CL_CONFIG_H__ - -#include "backend/interface/IConfig.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -class Config : public IConfig -{ -public: - Config() - { - // DO NOTHING - } - - virtual std::string id() override { return "acl_cl"; } - virtual void initialize() override; - virtual graph::operand::Layout getOperandLayout() { return graph::operand::Layout::NCHW; } - virtual bool SupportSubTensorAlloc() override { return true; } -}; - -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_CONFIG_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/Convert.cc b/runtimes/neurun/src/backend/acl_cl/Convert.cc deleted file mode 100644 index ed0a089c4..000000000 --- a/runtimes/neurun/src/backend/acl_cl/Convert.cc +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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 "Convert.h" - -#include "Swizzle.h" -#include "model/operand/DataType.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -::arm_compute::TensorShape asTensorShape(const ::neurun::model::operand::Shape &shape, - bool apply_dim_correction) -{ - const uint32_t rank = shape.rank(); - - ::arm_compute::TensorShape res{}; - - res.set_num_dimensions(rank); - - for (uint32_t axis = 0; axis < rank; ++axis) - { - // NOTE In some cases, in incorrect dimensions is required. - // For example, intput_size is 1 in LSTM. The input-to-input weights([num_units, input_size]) of - // LSTM is used as the weight of the FullyConnected. - // The FullyConnected's weight must be greater or equal than 2-dimensions. - // However, if the dimension correction is applied to input_to_input_weights with input_size - // equal to 1, it will be changed to 1-D. - // So input_to_input_weights is not used by the weight of FullyConnected. - res.set(ToARMComputeAxis(rank, axis).value(), shape.dim(axis), apply_dim_correction); - } - - return res; -} - -::arm_compute::DataType asDataType(const ::neurun::model::operand::DataType &type) -{ - switch (type) - { - case ::neurun::model::operand::DataType::SCALAR_FLOAT32: - case ::neurun::model::operand::DataType::TENSOR_FLOAT32: - return ::arm_compute::DataType::F32; - case ::neurun::model::operand::DataType::SCALAR_INT32: - case ::neurun::model::operand::DataType::TENSOR_INT32: - return ::arm_compute::DataType::S32; - case ::neurun::model::operand::DataType::SCALAR_UINT32: - return ::arm_compute::DataType::U32; - case ::neurun::model::operand::DataType::TENSOR_QUANT8_ASYMM: - return ::arm_compute::DataType::QASYMM8; - default: - throw std::runtime_error("Not supported, yet"); - break; - } -} - -::arm_compute::QuantizationInfo asQuantizationInfo(const float scale, const int32_t offset) -{ - return ::arm_compute::QuantizationInfo(scale, offset); -} - -::arm_compute::TensorInfo asTensorInfo(const ::neurun::model::operand::Shape &shape, - const ::neurun::model::operand::TypeInfo &typeInfo) -{ - return ::arm_compute::TensorInfo(asTensorShape(shape), 1, asDataType(typeInfo.type()), - asQuantizationInfo(typeInfo.scale(), typeInfo.offset())); -} - -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/Convert.h b/runtimes/neurun/src/backend/acl_cl/Convert.h deleted file mode 100644 index 1a233fb87..000000000 --- a/runtimes/neurun/src/backend/acl_cl/Convert.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_CONVERT_H__ -#define __NEURUN_BACKEND_ACL_CL_CONVERT_H__ - -#include <arm_compute/core/TensorInfo.h> -#include <arm_compute/core/SubTensorInfo.h> -#include <arm_compute/core/TensorShape.h> - -#include "model/operand/Object.h" -#include "model/operand/Shape.h" -#include "model/operand/TypeInfo.h" -#include "misc/feature/Shape.h" -#include "misc/kernel/Shape.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -::arm_compute::TensorShape asTensorShape(const ::neurun::model::operand::Shape &shape, - bool apply_dim_correction = true); -::arm_compute::DataType asDataType(const ::neurun::model::operand::DataType &type); -::arm_compute::TensorInfo asTensorInfo(const ::neurun::model::operand::Shape &shape, - const ::neurun::model::operand::TypeInfo &typeInfo); - -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_CONVERT_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/PluginClassesAllocator.cc b/runtimes/neurun/src/backend/acl_cl/PluginClassesAllocator.cc deleted file mode 100644 index f33e71d33..000000000 --- a/runtimes/neurun/src/backend/acl_cl/PluginClassesAllocator.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 <memory> -#include "TensorBuilder.h" -#include "StageGenerator.h" -#include "Config.h" -#include "util/logging.h" - -extern "C" { -neurun::backend::acl_cl::TensorBuilder *allocate_TensorBuilder() -{ - VERBOSE(allocate_TensorBuilder) << "loaded from acl_cl\n"; - return new neurun::backend::acl_cl::TensorBuilder; -} - -neurun::backend::acl_cl::StageGenerator *allocate_StageGenerator( - const neurun::model::operand::Set &operand_ctx, - const std::shared_ptr<neurun::backend::acl_cl::TensorBuilder> &tensor_builder) -{ - VERBOSE(allocate_StageGenerator) << "loaded from acl_cl\n"; - return new neurun::backend::acl_cl::StageGenerator(operand_ctx, tensor_builder); -} - -neurun::backend::acl_cl::Config *allocate_Config() -{ - VERBOSE(allocate_Config) << "loaded from acl_cl\n"; - return new neurun::backend::acl_cl::Config; -} -} diff --git a/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc b/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc deleted file mode 100644 index 89bbd7bd2..000000000 --- a/runtimes/neurun/src/backend/acl_cl/StageGenerator.cc +++ /dev/null @@ -1,593 +0,0 @@ -/* - * 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 "backend/acl_cl/StageGenerator.h" - -#include "kernel/acl_cl/CLFunction.h" - -#include <arm_compute/runtime/CL/functions/CLConvolutionLayer.h> -#include <arm_compute/runtime/CL/functions/CLPoolingLayer.h> -#include <arm_compute/runtime/CL/functions/CLActivationLayer.h> -#include <arm_compute/runtime/CL/functions/CLReshapeLayer.h> -#include <arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h> -#include <arm_compute/runtime/CL/functions/CLSoftmaxLayer.h> - -#include "kernel/acl_cl/ConcatLayer.h" - -#include "util/Padding.h" - -#include "model/operand/Index.h" - -#include "util/logging.h" - -#include "NeuralNetworks.h" - -#include "util/Utils.h" - -template <typename T> std::unique_ptr<T> make_layer(void) { return std::unique_ptr<T>{new T}; } - -std::unique_ptr<::neurun::kernel::acl_cl::CLFunction> -make_cl_function(std::unique_ptr<::arm_compute::IFunction> &&layer) -{ - return std::unique_ptr<::neurun::kernel::acl_cl::CLFunction>( - new ::neurun::kernel::acl_cl::CLFunction(std::move(layer))); -} - -::arm_compute::PadStrideInfo asPadStringInfo(const neurun::util::Padding &padding, - const neurun::util::Stride &stride) -{ - return ::arm_compute::PadStrideInfo{stride.horizontal, - stride.vertical, - padding.left, - padding.right, - padding.top, - padding.bottom, - ::arm_compute::DimensionRoundingType::FLOOR}; -} - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -// -// ActivationBuilder -// -class ActivationBuilder -{ -public: - ActivationBuilder(IExecutionBuilder &builder) : _builder(builder) - { - // DO NOTHING - } - -private: - void appendReLU(::arm_compute::ICLTensor *tensor); - -public: - void append(FuseCode code, ::arm_compute::ICLTensor *tensor); - -private: - IExecutionBuilder &_builder; -}; - -void ActivationBuilder::appendReLU(::arm_compute::ICLTensor *ifm_alloc) -{ - const ::arm_compute::ActivationLayerInfo act_info{ - ::arm_compute::ActivationLayerInfo::ActivationFunction::RELU}; - - auto fn = make_layer<::arm_compute::CLActivationLayer>(); - - fn->configure(ifm_alloc, nullptr, act_info); - - auto acl_fn = make_cl_function(std::move(fn)); - - _builder.append(std::move(acl_fn)); -} - -void ActivationBuilder::append(FuseCode code, ::arm_compute::ICLTensor *ifm_alloc) -{ - switch (code) - { - case ANEURALNETWORKS_FUSED_NONE: - { - // DO NOTHING - break; - } - case ANEURALNETWORKS_FUSED_RELU: - { - appendReLU(ifm_alloc); - break; - } - default: - { - throw std::runtime_error("Not supported, yet"); - } - } -} - -// -// StageGenerator -// -StageGenerator::StageGenerator(const neurun::model::operand::Set &ctx, - const std::shared_ptr<TensorBuilder> &tensor_builder) - : _ctx(ctx), _tensor_builder(tensor_builder) -{ - // DO NOTHING -} - -void StageGenerator::visit(const model::operation::Conv2DNode &node) -{ - using model::operation::Conv2DNode; - - const auto ofm_index{node.getOutputs().at(0)}; - const auto ifm_index{node.getInputs().at(Conv2DNode::Input::INPUT)}; - const auto ker_index{node.getInputs().at(Conv2DNode::Input::KERNEL)}; - const auto bias_index{node.getInputs().at(Conv2DNode::Input::BIAS)}; - - const auto vstride_index{node.param().vstride_index}; - const auto hstride_index{node.param().hstride_index}; - - const auto padding_index{node.param().padding_index}; - const auto activation_index{node.param().activation_index}; - - const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(); - const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(); - const auto ker_shape = _ctx.at(ker_index).shape().asKernel(); - - const PaddingCode padding_type = - static_cast<PaddingCode>(_ctx.at(padding_index).asScalar<int32_t>()); - - assert((ANEURALNETWORKS_PADDING_SAME == padding_type) || - (ANEURALNETWORKS_PADDING_VALID == padding_type)); - - neurun::util::Stride stride; - - stride.vertical = _ctx.at(vstride_index).asScalar<int32_t>(); - stride.horizontal = _ctx.at(hstride_index).asScalar<int32_t>(); - - // Construct operation parameters - struct Param - { - model::operand::Index ofm_index; - model::operand::Index ifm_index; - model::operand::Index ker_index; - model::operand::Index bias_index; - - neurun::util::Padding padding; - neurun::util::Stride stride; - - FuseCode activation; - }; - - Param param; - - param.ofm_index = ofm_index; - param.ifm_index = ifm_index; - param.ker_index = ker_index; - param.bias_index = bias_index; - - param.stride = stride; - param.padding = - (padding_type == ANEURALNETWORKS_PADDING_SAME) - ? neurun::util::same_padding(ifm_shape, ofm_shape, stride, ker_shape.W, ker_shape.H) - : neurun::util::valid_padding(); - - param.activation = static_cast<FuseCode>(_ctx.at(activation_index).asScalar<int32_t>()); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto ofm_alloc = tensors->at(param.ofm_index).get(); - auto ifm_alloc = tensors->at(param.ifm_index).get(); - auto ker_alloc = tensors->at(param.ker_index).get(); - auto bias_alloc = tensors->at(param.bias_index).get(); - - const auto conv_info = asPadStringInfo(param.padding, param.stride); - - std::unique_ptr<::arm_compute::CLConvolutionLayer> fn{new ::arm_compute::CLConvolutionLayer}; - - fn->configure(ifm_alloc->handle(), ker_alloc->handle(), bias_alloc->handle(), - ofm_alloc->handle(), conv_info); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append(std::move(acl_fn)); - - ActivationBuilder{builder}.append(param.activation, ofm_alloc->handle()); - }); -} - -void StageGenerator::visit(const model::operation::MaxPool2DNode &node) -{ - const auto ofm_index{node.getOutputs().at(0)}; - const auto ifm_index{node.getInputs().at(model::operation::MaxPool2DNode::Input::INPUT)}; - - const auto kh_index{node.param().kh_index}; - const auto kw_index{node.param().kw_index}; - - const auto vstride_index{node.param().vstride_index}; - const auto hstride_index{node.param().hstride_index}; - - const auto padding_index{node.param().padding_index}; - - const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(); - const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(); - - const int32_t kh = _ctx.at(kh_index).asScalar<int32_t>(); - const int32_t kw = _ctx.at(kw_index).asScalar<int32_t>(); - - const int32_t vstride = _ctx.at(vstride_index).asScalar<int32_t>(); - const int32_t hstride = _ctx.at(hstride_index).asScalar<int32_t>(); - - const PaddingCode padding_type = - static_cast<PaddingCode>(_ctx.at(padding_index).asScalar<int32_t>()); - - // Construct operation parameters - struct Param - { - model::operand::Index ofm_index; - model::operand::Index ifm_index; - - uint32_t kw; - uint32_t kh; - - neurun::util::Padding padding; - neurun::util::Stride stride; - - // TODO Add 'activation' field - }; - - Param param; - - param.ofm_index = ofm_index; - param.ifm_index = ifm_index; - - param.kh = kh; - param.kw = kw; - - param.stride.vertical = vstride; - param.stride.horizontal = hstride; - - param.padding = (padding_type == ANEURALNETWORKS_PADDING_SAME) - ? neurun::util::same_padding(ifm_shape, ofm_shape, param.stride, kw, kh) - : neurun::util::valid_padding(); - - VERBOSE(MaxPool2D) << "IFM_H: " << ifm_shape.H << std::endl; - VERBOSE(MaxPool2D) << "IFM_W: " << ifm_shape.W << std::endl; - VERBOSE(MaxPool2D) << "OFM_H: " << ofm_shape.H << std::endl; - VERBOSE(MaxPool2D) << "OFM_W: " << ofm_shape.W << std::endl; - VERBOSE(MaxPool2D) << "KER_H: " << kh << std::endl; - VERBOSE(MaxPool2D) << "KER_W: " << kw << std::endl; - VERBOSE(MaxPool2D) << "STRIDE_H: " << vstride << std::endl; - VERBOSE(MaxPool2D) << "STRIDE_W: " << hstride << std::endl; - VERBOSE(MaxPool2D) << "PAD(T): " << param.padding.top << std::endl; - VERBOSE(MaxPool2D) << "PAD(B): " << param.padding.bottom << std::endl; - VERBOSE(MaxPool2D) << "PAD(L): " << param.padding.left << std::endl; - VERBOSE(MaxPool2D) << "PAD(R): " << param.padding.right << std::endl; - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto ofm_alloc = tensors->at(param.ofm_index).get(); - auto ifm_alloc = tensors->at(param.ifm_index).get(); - - ::arm_compute::PoolingLayerInfo info{::arm_compute::PoolingType::MAX, - ::arm_compute::Size2D{param.kw, param.kh}, - asPadStringInfo(param.padding, param.stride)}; - - std::unique_ptr<::arm_compute::CLPoolingLayer> fn{new ::arm_compute::CLPoolingLayer}; - - fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), info); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append((std::move(acl_fn))); - }); -} - -void StageGenerator::visit(const model::operation::AvgPool2DNode &node) -{ - const auto ofm_index{node.getOutputs().at(0)}; - const auto ifm_index{node.getInputs().at(model::operation::AvgPool2DNode::Input::INPUT)}; - - const auto kh_index{node.param().kh_index}; - const auto kw_index{node.param().kw_index}; - - const auto vstride_index{node.param().vstride_index}; - const auto hstride_index{node.param().hstride_index}; - - const auto padding_index{node.param().padding_index}; - - const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(); - const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(); - - const int32_t kh = _ctx.at(kh_index).asScalar<int32_t>(); - const int32_t kw = _ctx.at(kw_index).asScalar<int32_t>(); - - const int32_t vstride = _ctx.at(vstride_index).asScalar<int32_t>(); - const int32_t hstride = _ctx.at(hstride_index).asScalar<int32_t>(); - - const PaddingCode padding_type = - static_cast<PaddingCode>(_ctx.at(padding_index).asScalar<int32_t>()); - - assert((ANEURALNETWORKS_PADDING_SAME == padding_type) || - (ANEURALNETWORKS_PADDING_VALID == padding_type)); - - // Construct operation parameters - struct Param - { - model::operand::Index ofm_index; - model::operand::Index ifm_index; - - uint32_t kw; - uint32_t kh; - - neurun::util::Padding padding; - neurun::util::Stride stride; - - // TODO Add 'activation' field - }; - - Param param; - - param.ofm_index = ofm_index; - param.ifm_index = ifm_index; - - param.kh = kh; - param.kw = kw; - - param.stride.vertical = vstride; - param.stride.horizontal = hstride; - - param.padding = (padding_type == ANEURALNETWORKS_PADDING_SAME) - ? neurun::util::same_padding(ifm_shape, ofm_shape, param.stride, kw, kh) - : neurun::util::valid_padding(); - - VERBOSE(AvgPool2D) << "IFM_H: " << ifm_shape.H << std::endl; - VERBOSE(AvgPool2D) << "IFM_W: " << ifm_shape.W << std::endl; - VERBOSE(AvgPool2D) << "OFM_H: " << ofm_shape.H << std::endl; - VERBOSE(AvgPool2D) << "OFM_W: " << ofm_shape.W << std::endl; - VERBOSE(AvgPool2D) << "KER_H: " << kh << std::endl; - VERBOSE(AvgPool2D) << "KER_W: " << kw << std::endl; - VERBOSE(AvgPool2D) << "STRIDE_H: " << vstride << std::endl; - VERBOSE(AvgPool2D) << "STRIDE_W: " << hstride << std::endl; - VERBOSE(AvgPool2D) << "PAD: " << neurun::util::to_string(padding_type) << std::endl; - VERBOSE(AvgPool2D) << "PAD(T): " << param.padding.top << std::endl; - VERBOSE(AvgPool2D) << "PAD(B): " << param.padding.bottom << std::endl; - VERBOSE(AvgPool2D) << "PAD(L): " << param.padding.left << std::endl; - VERBOSE(AvgPool2D) << "PAD(R): " << param.padding.right << std::endl; - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto ofm_alloc = tensors->at(param.ofm_index).get(); - auto ifm_alloc = tensors->at(param.ifm_index).get(); - - ::arm_compute::PoolingLayerInfo info{ - ::arm_compute::PoolingType::AVG, ::arm_compute::Size2D{param.kw, param.kh}, - asPadStringInfo(param.padding, param.stride), true /* exclude_padding */}; - - std::unique_ptr<::arm_compute::CLPoolingLayer> fn{new ::arm_compute::CLPoolingLayer}; - - fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), info); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append((std::move(acl_fn))); - }); -} - -void StageGenerator::visit(const model::operation::ConcatNode &node) -{ - const auto ofm_index{node.getOutputs().at(0)}; - const auto axis_index{node.param().axis_index}; - - struct Param - { - model::operand::Index output_index; - std::vector<model::operand::Index> input_indexes; - - int32_t axis; - }; - - Param param; - - param.output_index = ofm_index; - for (const auto &e : node.getInputs()) - { - param.input_indexes.emplace_back(e); - } - param.axis = _ctx.at(axis_index).asScalar<int32_t>(); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - // If tensor allocator allocate as subtensor - bool canEliminate = true; - for (auto ifm_ind : param.input_indexes) - { - if (!tensors->isSubTensorOf(param.output_index, ifm_ind)) - { - canEliminate = false; - break; - } - } - if (canEliminate) - { - // If concat eliminated, return with nothing to do - return; - } - - auto output_alloc = tensors->at(param.output_index).get(); - - std::vector<::neurun::backend::acl_cl::operand::ICLTensor *> input_allocs; - for (auto ifm_ind : param.input_indexes) - { - input_allocs.emplace_back( - dynamic_cast<::neurun::backend::acl_cl::operand::CLTensor *>(tensors->at(ifm_ind).get())); - } - - std::unique_ptr<::neurun::kernel::acl_cl::ConcatLayer> fn{ - new ::neurun::kernel::acl_cl::ConcatLayer}; - - fn->configure(input_allocs, param.axis, - dynamic_cast<::neurun::backend::acl_cl::operand::CLTensor *>(output_alloc)); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append(std::move(acl_fn)); - }); -} - -void StageGenerator::visit(const model::operation::FullyConnectedNode &node) -{ - using model::operation::FullyConnectedNode; - - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(FullyConnectedNode::Input::INPUT)}; - const auto weight_index{node.getInputs().at(FullyConnectedNode::Input::WEIGHT)}; - const auto bias_index{node.getInputs().at(FullyConnectedNode::Input::BIAS)}; - const auto activation_index{node.param().activation_index}; - - // Construct operation parameters - struct Param - { - model::operand::Index output_index; - - model::operand::Index input_index; - model::operand::Index weight_index; - model::operand::Index bias_index; - - FuseCode activation; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - param.weight_index = weight_index; - param.bias_index = bias_index; - - param.activation = static_cast<FuseCode>(_ctx.at(activation_index).asScalar<int32_t>()); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - auto input_alloc = tensors->at(param.input_index).get(); - auto weight_alloc = tensors->at(param.weight_index).get(); - auto bias_alloc = tensors->at(param.bias_index).get(); - - auto fn = make_layer<::arm_compute::CLFullyConnectedLayer>(); - - fn->configure(input_alloc->handle(), weight_alloc->handle(), bias_alloc->handle(), - output_alloc->handle()); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append(std::move(acl_fn)); - - ActivationBuilder{builder}.append(param.activation, output_alloc->handle()); - }); -} - -void StageGenerator::visit(const model::operation::ReshapeNode &node) -{ - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(model::operation::ReshapeNode::Input::INPUT)}; - - struct Param - { - model::operand::Index output_index; - model::operand::Index input_index; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - auto input_alloc = tensors->at(param.input_index).get(); - - auto fn = make_layer<::arm_compute::CLReshapeLayer>(); - - fn->configure(input_alloc->handle(), output_alloc->handle()); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append(std::move(acl_fn)); - }); -} - -void StageGenerator::visit(const model::operation::SoftmaxNode &node) -{ - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(model::operation::SoftmaxNode::Input::INPUT)}; - const auto scale_index{node.param().scale_index}; - - assert(_ctx.at(scale_index).shape().rank() == 0); - - struct Param - { - model::operand::Index output_index; - model::operand::Index input_index; - float scale; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - param.scale = _ctx.at(scale_index).asScalar<float>(); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - auto input_alloc = tensors->at(param.input_index).get(); - - auto fn = make_layer<::arm_compute::CLSoftmaxLayer>(); - - fn->configure(input_alloc->handle(), output_alloc->handle(), param.scale); - - auto acl_fn = make_cl_function(std::move(fn)); - - builder.append(std::move(acl_fn)); - }); -} - -void StageGenerator::visit(const model::operation::PermuteNode & /* node */) -{ - throw "Unsupported"; -} - -void StageGenerator::visit(const model::operation::AddNode &) -{ - VERBOSE(Add) << "generate CPU Add" << std::endl; - - throw std::runtime_error("NYI"); -} - -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/StageGenerator.h b/runtimes/neurun/src/backend/acl_cl/StageGenerator.h deleted file mode 100644 index 1dac2592b..000000000 --- a/runtimes/neurun/src/backend/acl_cl/StageGenerator.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_STAGE_GENERATOR_H__ -#define __NEURUN_BACKEND_ACL_CL_STAGE_GENERATOR_H__ - -#include "backend/interface/IStageGenerator.h" - -#include "model/operand/Set.h" -#include "backend/acl_cl/TensorBuilder.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -class StageGenerator : public IStageGenerator -{ -public: - StageGenerator(const neurun::model::operand::Set &ctx, - const std::shared_ptr<TensorBuilder> &tensor_builder); - - virtual std::shared_ptr<ITensorBuilder> tensor_builder() override { return _tensor_builder; } - -#define OP(InternalName, IsNnApi, NnApiName) \ - virtual void visit(const model::operation::InternalName &) override; -#include "model/operation/Op.lst" -#undef OP - -private: - const neurun::model::operand::Set &_ctx; - std::shared_ptr<TensorBuilder> _tensor_builder; -}; - -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_STAGE_GENERATOR_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/Swizzle.h b/runtimes/neurun/src/backend/acl_cl/Swizzle.h deleted file mode 100644 index 838e57162..000000000 --- a/runtimes/neurun/src/backend/acl_cl/Swizzle.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_SWIZZLE_H__ -#define __NEURUN_BACKEND_ACL_CL_SWIZZLE_H__ - -#include <cassert> - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -class ARMComputeAxis -{ -public: - ARMComputeAxis() = default; - -public: - explicit ARMComputeAxis(uint32_t value) : _value{value} - { - // DO NOTHING - } - -public: - uint32_t value(void) const { return _value; } - -private: - uint32_t _value; -}; - -// Convert T/F Lite / NNAPI axis (based on ...NHWC) to ARMCompute axis (WHCN...) -inline ARMComputeAxis ToARMComputeAxis(uint32_t rank, uint32_t axis) -{ - assert(rank > axis); - const ARMComputeAxis reversed{(rank - axis) - 1}; - - if (rank < 4) - { - return reversed; - } - - // DEPTH - if (0 == reversed.value()) - { - return ARMComputeAxis{2}; - } - // WIDTH - if (1 == reversed.value()) - { - return ARMComputeAxis{0}; - } - // HEIGHT - if (2 == reversed.value()) - { - return ARMComputeAxis{1}; - } - - // ELSE - return reversed; -} - -template <typename T> inline T ReorderBits(T in, size_t numOfBits) -{ - assert(numOfBits > 0); - T out = 0; - for (int32_t i = numOfBits - 1; i >= 0; --i) - { - const uint32_t toShift = numOfBits - ToARMComputeAxis(numOfBits, i).value() - 1; - out += ((in & 1) << toShift); - in >>= 1; - } - return out; -} - -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_SWIZZLE_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/TensorBuilder.cc b/runtimes/neurun/src/backend/acl_cl/TensorBuilder.cc deleted file mode 100644 index b5c038200..000000000 --- a/runtimes/neurun/src/backend/acl_cl/TensorBuilder.cc +++ /dev/null @@ -1,246 +0,0 @@ -/* - * 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 "backend/acl_cl/TensorBuilder.h" - -#include <cassert> -#include <stack> - -#include "operand/Object.h" -#include "Convert.h" - -#include "util/logging.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -TensorBuilder::TensorBuilder() -{ - // DO NOTHING -} - -void TensorBuilder::registerTensorInfo(const model::operand::Index &ind, - const compiler::TensorInfo &info) -{ - assert(_tensors.size() == 0); - - _tensor_info_map.insert({ind, info}); -} - -void TensorBuilder::registerSubTensorInfo(const model::operand::Index &ind, - const compiler::SubTensorInfo &info) -{ - assert(_tensors.size() == 0); - - _subtensor_info_map.insert({ind, info}); -} - -void TensorBuilder::notifyFirstUse(const model::operand::Index &) -{ - // DO NOTHING -} - -void TensorBuilder::notifyLastUse(const model::operand::Index &) -{ - // DO NOTHING -} - -void TensorBuilder::prepare(void) -{ - assert(_tensors.size() == 0); - - // TODO Handle SubTensor(subsumption) - // Currently this TensorBuilder does not have subsumption info yet - // Allocated subtensor will be mapped to _subtensors instead of _tensors - assert(_subtensors.size() == 0); - - for (auto &entry : _tensor_info_map) - { - auto ind = entry.first; - const auto &info = entry.second; - auto tensor = std::make_shared<::neurun::backend::acl_cl::operand::CLTensor>(info); - _tensors[ind] = tensor; - } - - // To make subtensor, parent tensor must be made first - // For this condition, use stack - // 1) Push one subtensor index to stack (iterate subtensors) - // 2) If tensor at stack top is already made, pop and go to 4) - // 3) If tensor pushed at 1) is not made, check parent tensor - // 3-1) If parent tensor is already made, we can make child tensor - // Make child tensor and pop, go to 4) - // 3-2) If parent tensor is not made, we can't make child tensor yet - // Push parent tensor index to stack and return to 4) - // 4) If stack is empty, return to 1), else return to 2) - for (auto &entry : _subtensor_info_map) - { - model::operand::Index ind = entry.first; - - std::stack<model::operand::Index> stack; - stack.push(ind); - - while (!stack.empty()) - { - const auto current = stack.top(); - const auto &info = _subtensor_info_map.at(current); - - // Already generated CLSubTensor - if (_subtensors.find(current) != _subtensors.end()) - { - stack.pop(); - continue; - } - - auto parent = info.parent(); - std::shared_ptr<::neurun::backend::acl_cl::operand::ICLTensor> parent_tensor; - - if (_tensors.find(parent) != _tensors.end()) - { - // Parent is allocated as tensor - parent_tensor = _tensors[parent]; - } - else if (_subtensors.find(parent) != _subtensors.end()) - { - // Parent is allocated as subtensor - parent_tensor = _subtensors[parent]; - } - else - { - // Cannot find allocated parent tensor: allocate parent first - assert(_subtensor_info_map.find(parent) != _subtensor_info_map.end()); - stack.push(parent); - continue; - } - assert(parent_tensor != nullptr); - - // Child's type should be same with parent - assert(info.type().offset() == parent_tensor->info()->quantization_info().offset); - assert(info.type().scale() == parent_tensor->info()->quantization_info().scale); - assert(asDataType(info.type().type()) == parent_tensor->info()->data_type()); - auto shape = asTensorShape(info.shape()); - - // Only support axis: 3 (channel) - ::arm_compute::Coordinates coordinates; - coordinates.set_num_dimensions(4); - assert(info.offset().h() == 0); - assert(info.offset().n() == 0); - assert(info.offset().w() == 0); - coordinates[2] = info.offset().c(); - auto tensor = std::make_shared<::neurun::backend::acl_cl::operand::CLSubTensor>( - parent_tensor.get(), shape, coordinates, true); - _subtensors[current] = tensor; - stack.pop(); - } - } -} - -void TensorBuilder::allocate(void) -{ - assert(_tensor_info_map.size() == _tensors.size()); - - for (const auto &tensor_entry : _tensors) - { - auto tensor = tensor_entry.second; - tensor->allocator()->allocate(); - } -} - -std::shared_ptr<::neurun::backend::operand::ITensor> -TensorBuilder::tensorAt(const model::operand::Index &ind) -{ - if (_tensors.find(ind) != _tensors.end()) - { - return _tensors.at(ind); - } - else - { - return _subtensors.at(ind); - } -} - -std::shared_ptr<backend::operand::IObject> -TensorBuilder::wrapTensor(const model::operand::Index &ind) -{ - if (_objects.find(ind) != _objects.end()) - { - return _objects.at(ind); - } - else - { - if (_tensors.find(ind) != _tensors.end()) - { - return _objects[ind] = std::make_shared<operand::Object>(_tensors.at(ind)); - } - else - { - return _objects[ind] = std::make_shared<operand::Object>(_subtensors.at(ind)); - } - } -} - -void TensorBuilder::iterate(const IterateFunction &fn) -{ - for (auto it : _tensors) - { - fn(it.first); - } - for (auto it : _subtensors) - { - fn(it.first); - } -} - -std::shared_ptr<::neurun::backend::acl_cl::operand::ICLTensor> -TensorBuilder::at(const ::neurun::model::operand::Index &ind) -{ - if (_tensors.find(ind) != _tensors.end()) - { - return _tensors.at(ind); - } - else - { - return _subtensors.at(ind); - } -} - -bool TensorBuilder::isSubTensorOf(const model::operand::Index &parent, - const model::operand::Index &child) -{ - if (_subtensor_info_map.find(child) == _subtensor_info_map.end()) - { - return false; - } - - if (_subtensors.find(child) == _subtensors.end()) - { - return false; - } - - if (_subtensor_info_map.at(child).parent() != parent) - { - return false; - } - - return true; -} - -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/TensorBuilder.h b/runtimes/neurun/src/backend/acl_cl/TensorBuilder.h deleted file mode 100644 index 64d81721a..000000000 --- a/runtimes/neurun/src/backend/acl_cl/TensorBuilder.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_TENSOR_BUILDER_H__ -#define __NEURUN_BACKEND_ACL_CL_TENSOR_BUILDER_H__ - -#include "backend/interface/ITensorBuilder.h" -#include "backend/acl_cl/operand/CLTensor.h" -#include "backend/acl_cl/operand/CLSubTensor.h" -#include "backend/acl_cl/operand/Object.h" - -#include <unordered_map> - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ - -class TensorBuilder : public ITensorBuilder -{ -public: - TensorBuilder(); - - /** - * @brief Register tensor information to allocate on ACL-CL backend - * @param[in] ind Operand index - * @param[in] info Tensor information - */ - virtual void registerTensorInfo(const model::operand::Index &ind, - const compiler::TensorInfo &info) override; - /** - * @brief Register subtensor information to allocate on ACL-CL backend - * @param[in] ind Operand index - * @param[in] info Tensor information - */ - virtual void registerSubTensorInfo(const model::operand::Index &ind, - const compiler::SubTensorInfo &info) override; - - virtual void notifyFirstUse(const model::operand::Index &) override; - virtual void notifyLastUse(const model::operand::Index &) override; - - virtual void prepare(void) override; - virtual void allocate(void) override; - - virtual std::shared_ptr<::neurun::backend::operand::ITensor> - tensorAt(const model::operand::Index &ind) override; - virtual std::shared_ptr<backend::operand::IObject> - wrapTensor(const model::operand::Index &ind) override; - virtual void iterate(const IterateFunction &fn) override; - - std::shared_ptr<::neurun::backend::acl_cl::operand::ICLTensor> - at(const ::neurun::model::operand::Index &ind); - /** - * @brief Check child tensor is allocated as subtensor of parent tensor - * @param[in] parent Index of parent - * @param[in] child Index of child - * @return @c true if child is allocated as subtensor of parent, otherwise @c false - */ - bool isSubTensorOf(const model::operand::Index &parent, const model::operand::Index &child); - -private: - std::unordered_map<model::operand::Index, compiler::TensorInfo> _tensor_info_map; - std::unordered_map<model::operand::Index, compiler::SubTensorInfo> _subtensor_info_map; - std::unordered_map<model::operand::Index, - std::shared_ptr<::neurun::backend::acl_cl::operand::CLTensor>> - _tensors; - std::unordered_map<model::operand::Index, - std::shared_ptr<::neurun::backend::acl_cl::operand::CLSubTensor>> - _subtensors; - std::unordered_map<model::operand::Index, - std::shared_ptr<::neurun::backend::acl_cl::operand::Object>> - _objects; -}; - -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_TENSOR_BUILDER_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/operand/CLSubTensor.cc b/runtimes/neurun/src/backend/acl_cl/operand/CLSubTensor.cc deleted file mode 100644 index f64b521dd..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/CLSubTensor.cc +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 "CLSubTensor.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -CLSubTensor::CLSubTensor(ICLTensor *parent, const arm_compute::TensorShape &tensor_shape, - const arm_compute::Coordinates &coords, bool extend_parent) - : _cl_sub_tensor(std::make_shared<arm_compute::CLSubTensor>(parent->handle(), tensor_shape, - coords, extend_parent)) -{ - // DO NOTHING -} - -arm_compute::CLSubTensor *CLSubTensor::handle() const { return _cl_sub_tensor.get(); } - -arm_compute::CLSubTensor *CLSubTensor::handle() { return _cl_sub_tensor.get(); } - -void CLSubTensor::map(bool blocking) { _cl_sub_tensor->map(blocking); } - -void CLSubTensor::unmap() { _cl_sub_tensor->unmap(); } - -uint8_t *CLSubTensor::doMap(cl::CommandQueue &q, bool blocking) -{ - assert(cl_buffer().get() == nullptr); - return static_cast<uint8_t *>(q.enqueueMapBuffer(cl_buffer(), blocking ? CL_TRUE : CL_FALSE, - CL_MAP_READ | CL_MAP_WRITE, 0, - info()->total_size())); -} - -void CLSubTensor::doUnmap(cl::CommandQueue &q) -{ - assert(cl_buffer().get() == nullptr); - q.enqueueUnmapMemObject(cl_buffer(), buffer()); -} - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/operand/CLSubTensor.h b/runtimes/neurun/src/backend/acl_cl/operand/CLSubTensor.h deleted file mode 100644 index cef78c196..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/CLSubTensor.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_OPERAND_CL_SUB_TENSOR_H__ -#define __NEURUN_BACKEND_ACL_CL_OPERAND_CL_SUB_TENSOR_H__ - -#include <arm_compute/runtime/CL/CLSubTensor.h> -#include "ICLTensor.h" -#include "compiler/SubTensorInfo.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -class CLSubTensor : public ICLTensor -{ -public: - CLSubTensor() = delete; - -public: - CLSubTensor(ICLTensor *parent, const arm_compute::TensorShape &tensor_shape, - const arm_compute::Coordinates &coords, bool extend_parent = false); - -public: - arm_compute::CLSubTensor *handle() const override; - arm_compute::CLSubTensor *handle() override; - -public: - void map(bool blocking = true); - void unmap(); - -protected: - uint8_t *doMap(cl::CommandQueue &q, bool blocking) override; - virtual void doUnmap(cl::CommandQueue &q) override; - -private: - std::shared_ptr<arm_compute::CLSubTensor> _cl_sub_tensor; -}; - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_OPERAND_CL_SUB_TENSOR_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/operand/CLTensor.cc b/runtimes/neurun/src/backend/acl_cl/operand/CLTensor.cc deleted file mode 100644 index e7b718df3..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/CLTensor.cc +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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 <arm_compute/runtime/CL/CLScheduler.h> -#include <arm_compute/runtime/CL/CLMemory.h> -#include <arm_compute/runtime/CL/CLMemoryRegion.h> -#include "CLTensor.h" - -#include "backend/acl_cl/Convert.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -CLTensor::CLTensor(const compiler::TensorInfo &info) - : _cl_tensor(std::make_shared<arm_compute::CLTensor>()) -{ - auto acl_cl_info = asTensorInfo(info.shape(), info.typeInfo()); - allocator()->init(acl_cl_info); -} - -arm_compute::CLTensor *CLTensor::handle() const { return _cl_tensor.get(); } - -arm_compute::CLTensor *CLTensor::handle() { return _cl_tensor.get(); } - -arm_compute::CLTensorAllocator *CLTensor::allocator() { return _cl_tensor->allocator(); } - -void CLTensor::map(bool blocking) { _cl_tensor->map(blocking); } - -void CLTensor::unmap() { _cl_tensor->unmap(); } - -uint8_t *CLTensor::doMap(cl::CommandQueue &q, bool blocking) -{ - return allocator()->map(q, blocking); -} - -void CLTensor::doUnmap(cl::CommandQueue &q) { allocator()->unmap(q, buffer()); } - -// handle() is Deprecated on acl v18.11 -// TODO Update this -#if 0 -void CLTensor::setBuffer(void *host_ptr) -{ - // create empty MemoryRegion: just context. Since flag isn't used here, no matter which flag to - // pass - auto memory = arm_compute::CLMemory(std::make_shared<arm_compute::CLBufferMemoryRegion>( - arm_compute::CLScheduler::get().context(), CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, 0)); - - // set buffer - auto mem = reinterpret_cast<cl::Buffer *>(memory.region()->handle()); - *mem = cl::Buffer(arm_compute::CLScheduler::get().context(), - CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, info()->total_size(), host_ptr); - // set correct buffer size - memory.region()->set_size(info()->total_size()); - // import memory - allocator()->import_memory(memory); -} -#endif - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/operand/CLTensor.h b/runtimes/neurun/src/backend/acl_cl/operand/CLTensor.h deleted file mode 100644 index 31c96e201..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/CLTensor.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_OPERAND_CL_TENSOR_H__ -#define __NEURUN_BACKEND_ACL_CL_OPERAND_CL_TENSOR_H__ - -#include <arm_compute/core/TensorInfo.h> -#include <arm_compute/runtime/CL/CLTensor.h> -#include <arm_compute/runtime/CL/CLScheduler.h> -#include "arm_compute/runtime/CL/CLTensorAllocator.h" -#include "ICLTensor.h" -#include "compiler/TensorInfo.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -class CLTensor : public ICLTensor -{ -public: - CLTensor() = delete; - -public: - CLTensor(const compiler::TensorInfo &info); - -public: - arm_compute::CLTensor *handle() const override; - arm_compute::CLTensor *handle() override; - -public: - arm_compute::CLTensorAllocator *allocator(); - void map(bool blocking = true); - void unmap(); - void setBuffer(void *host_ptr); - -protected: - uint8_t *doMap(cl::CommandQueue &q, bool blocking) override; - void doUnmap(cl::CommandQueue &q) override; - -private: - std::shared_ptr<arm_compute::CLTensor> _cl_tensor; -}; - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_OPERAND_CL_TENSOR_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.cc b/runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.cc deleted file mode 100644 index 23d723de4..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.cc +++ /dev/null @@ -1,48 +0,0 @@ -#include "ICLTensor.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -size_t ICLTensor::total_size() const { return info()->total_size(); } - -size_t ICLTensor::dimension(size_t index) const { return info()->dimension(index); } - -size_t ICLTensor::num_dimensions() const { return info()->num_dimensions(); } - -size_t ICLTensor::calcOffset(const neurun::util::feature::Coordinate4D &coords) -{ - int32_t N = coords.n(); - int32_t C = coords.c(); - int32_t H = coords.h(); - int32_t W = coords.w(); - - ::arm_compute::Coordinates coordinates{W, H, C, N}; - return info()->offset_element_in_bytes(coordinates); -} - -arm_compute::DataType ICLTensor::data_type() const { return info()->data_type(); } - -uint8_t *ICLTensor::buffer() const { return handle()->buffer(); } - -const cl::Buffer &ICLTensor::cl_buffer() const { return handle()->cl_buffer(); } - -arm_compute::ITensorInfo *ICLTensor::info() const { return handle()->info(); } - -arm_compute::ITensorInfo *ICLTensor::info() { return handle()->info(); } - -void ICLTensor::map(cl::CommandQueue &q, bool blocking) { return handle()->map(q, blocking); } - -void ICLTensor::unmap(cl::CommandQueue &q) { return handle()->unmap(q); } - -void ICLTensor::clear(cl::CommandQueue &q) { return handle()->clear(q); } - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.h b/runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.h deleted file mode 100644 index 226fbf814..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/ICLTensor.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_OPERAND_I_CL_TENSOR_H__ -#define __NEURUN_BACKEND_ACL_CL_OPERAND_I_CL_TENSOR_H__ - -#include <arm_compute/core/ITensorInfo.h> -#include <arm_compute/core/CL/ICLTensor.h> -#include "backend/interface/operand/ITensor.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -class ICLTensor : public ::neurun::backend::operand::ITensor -{ -public: - ICLTensor() = default; - ICLTensor(const ICLTensor &) = delete; - ICLTensor &operator=(const ICLTensor &) = delete; - ICLTensor(ICLTensor &&) = default; - ICLTensor &operator=(ICLTensor &&) = default; - virtual ~ICLTensor() = default; - -public: - virtual arm_compute::ICLTensor *handle() = 0; - virtual arm_compute::ICLTensor *handle() const = 0; - -public: - uint8_t *buffer() const override; - size_t total_size() const override; - size_t dimension(size_t index) const override; - size_t num_dimensions() const override; - size_t calcOffset(const neurun::util::feature::Coordinate4D &coords) override; - -public: - arm_compute::DataType data_type() const; - const cl::Buffer &cl_buffer() const; - arm_compute::ITensorInfo *info() const; - arm_compute::ITensorInfo *info(); - void map(cl::CommandQueue &q, bool blocking = true); - void unmap(cl::CommandQueue &q); - void clear(cl::CommandQueue &q); - -protected: - virtual uint8_t *doMap(cl::CommandQueue &q, bool blocking) = 0; - virtual void doUnmap(cl::CommandQueue &q) = 0; -}; - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_OPERAND_I_CL_TENSOR_H__ diff --git a/runtimes/neurun/src/backend/acl_cl/operand/Object.cc b/runtimes/neurun/src/backend/acl_cl/operand/Object.cc deleted file mode 100644 index a84fa2366..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/Object.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 "Object.h" - -#include <arm_compute/runtime/CL/CLScheduler.h> - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -void Object::access( - const std::function<void(::neurun::backend::operand::ITensor &tensor)> &fn) const -{ - auto &queue = ::arm_compute::CLScheduler::get().queue(); - - _tensor->map(queue); - fn(*_tensor); - _tensor->unmap(queue); -} - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/acl_cl/operand/Object.h b/runtimes/neurun/src/backend/acl_cl/operand/Object.h deleted file mode 100644 index 4ba22b269..000000000 --- a/runtimes/neurun/src/backend/acl_cl/operand/Object.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ACL_CL_OPERAND_OBJECT_H__ -#define __NEURUN_BACKEND_ACL_CL_OPERAND_OBJECT_H__ - -#include <memory> - -#include "backend/interface/operand/IObject.h" -#include "backend/acl_cl/operand/ICLTensor.h" - -namespace neurun -{ -namespace backend -{ -namespace acl_cl -{ -namespace operand -{ - -class Object : public backend::operand::IObject -{ -public: - Object() = default; - -public: - Object(const std::shared_ptr<::neurun::backend::acl_cl::operand::ICLTensor> &tensor) - : _tensor{tensor} - { - // DO NOTHING - } - -public: - ::neurun::backend::acl_cl::operand::ICLTensor *ptr(void) const override { return _tensor.get(); } - -private: - std::shared_ptr<::neurun::backend::acl_cl::operand::ICLTensor> _tensor; - -public: - void - access(const std::function<void(::neurun::backend::operand::ITensor &tensor)> &fn) const override; -}; - -} // namespace operand -} // namespace acl_cl -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ACL_CL_OPERAND_OBJECT_H__ diff --git a/runtimes/neurun/src/backend/cpu/CMakeLists.txt b/runtimes/neurun/src/backend/cpu/CMakeLists.txt deleted file mode 100644 index dc4406a65..000000000 --- a/runtimes/neurun/src/backend/cpu/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -file(GLOB_RECURSE SOURCES "*.cc") - -add_library(${LIB_NEURUN_BACKEND_CPU} SHARED ${SOURCES}) - -target_include_directories(${LIB_NEURUN_BACKEND_CPU} PUBLIC ${NNFW_INCLUDE_DIR}) -target_include_directories(${LIB_NEURUN_BACKEND_CPU} PUBLIC ${NEURUN_INCLUDE_DIR}) -target_include_directories(${LIB_NEURUN_BACKEND_CPU} PUBLIC ${CMAKE_SOURCE_DIR}/externals/tensorflow) - -target_link_libraries(${LIB_NEURUN_BACKEND_CPU} tensorflow-lite) -target_link_libraries(${LIB_NEURUN_BACKEND_CPU} nnfw_lib_misc) -target_link_libraries(${LIB_NEURUN_BACKEND_CPU} nnfw_lib_cpp14) -target_link_libraries(${LIB_NEURUN_BACKEND_CPU} ${LIB_NEURUN_KERNEL_CPU}) -target_link_libraries(${LIB_NEURUN_BACKEND_CPU} ${LIB_NEURUN}) - -target_compile_options(${LIB_NEURUN_BACKEND_CPU} PRIVATE -Wall -Wextra -Werror) - -set_target_properties(${LIB_NEURUN_BACKEND_CPU} PROPERTIES OUTPUT_NAME backend_cpu) -install(TARGETS ${LIB_NEURUN_BACKEND_CPU} DESTINATION lib/neurun) diff --git a/runtimes/neurun/src/backend/cpu/Config.cc b/runtimes/neurun/src/backend/cpu/Config.cc deleted file mode 100644 index 001ba9d02..000000000 --- a/runtimes/neurun/src/backend/cpu/Config.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 "backend/cpu/Config.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -void Config::initialize() -{ - // DO NOTHING -} - -} // namespace cpu -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/cpu/Config.h b/runtimes/neurun/src/backend/cpu/Config.h deleted file mode 100644 index ad9ca0ee8..000000000 --- a/runtimes/neurun/src/backend/cpu/Config.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_CPU_CONFIG_H__ -#define __NEURUN_BACKEND_CPU_CONFIG_H__ - -#include "backend/interface/IConfig.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -class Config : public IConfig -{ -public: - Config() - { - // DO NOTHING - } - - virtual std::string id() override { return "cpu"; } - virtual void initialize() override; - virtual graph::operand::Layout getOperandLayout() { return graph::operand::Layout::NHWC; } - virtual bool SupportSubTensorAlloc() override - { - // NOTE CPU allocator cannot support subtensor allocation yet - return false; - } -}; - -} // namespace cpu -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_CPU_CONFIG_H__ diff --git a/runtimes/neurun/src/backend/cpu/MemoryPlanner.cc b/runtimes/neurun/src/backend/cpu/MemoryPlanner.cc deleted file mode 100644 index 2d0995b8a..000000000 --- a/runtimes/neurun/src/backend/cpu/MemoryPlanner.cc +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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 "MemoryPlanner.h" -#include "util/logging.h" -#include <cassert> - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -Allocator::Allocator(uint32_t capacity) -{ - assert(!_base && capacity != 0); - - _base = new uint8_t[capacity]; - - VERBOSE(ALLOC) << "allocation capacity: " << capacity << std::endl; - VERBOSE(ALLOC) << "base pointer: " << static_cast<void *>(_base) << std::endl; -} - -Allocator::~Allocator() { delete[] _base; } - -void BumpPlanner::claim(const model::operand::Index &ind, size_t size) -{ - assert(size != 0); - - Block blk{_capacity, size}; - _mem_plans[ind] = blk; - _capacity += size; - - VERBOSE(BP_PLANNER) << "CLAIM(#" << ind.value() << "): " << blk.offset << ", " << blk.size - << std::endl; -} - -void BumpPlanner::release(const model::operand::Index &ind) -{ - VERBOSE(BP_PLANNER) << "RELEASE(#" << ind.value() << "): " - << "NOTHING does" << std::endl; -} - -// There are some assumptions for claiming memory(== making a reservation for memory). -// 1. About _claim_table(std::map). -// - The table's data structure is std::map so that it always sorts -// value(model::operand::Index) by key(base_offset). -// - This claim() inserts key/value into _claim_table and the release() removes the key/value from -// _claim_table. -// - _claim_table shows the memory status at a certain point in time. Therefore, -// - If _claim_table has an offset and a certain size at a certain point in time, -// it means the place at the offset has been already claimed(== can't claim now. need to find -// someplace new). -// - If _claim_table doesn't have any element for an offset and a certain size at a certain -// point in time, it means the place at the offset can be claimed. -// 2. In the loop for _claim_table, we can assume the current claim_base_offset value is bigger than -// the previous claim_base_offset. -void FirstFitPlanner::claim(const model::operand::Index &ind, size_t size) -{ - assert(size != 0); - - // Find the right position for claiming - uint32_t next_offset = 0; - for (auto &mem_claim : _claim_table) - { - auto claimed_base_offset = mem_claim.first; - auto claimed_size = _mem_plans[mem_claim.second].size; - if (next_offset + size <= claimed_base_offset) - { - break; - } - else - { - next_offset = claimed_base_offset + claimed_size; - } - } - - // Now next_offset is set to the proper offset - _claim_table[next_offset] = ind; - _mem_plans[ind] = {next_offset, size}; - - VERBOSE(FF_PLANNER) << "claim(#" << ind.value() << "): [+" << next_offset << ", " << size << "sz]" - << std::endl; - - if (_capacity < next_offset + size) - { - _capacity = next_offset + size; - } -} - -void FirstFitPlanner::release(const model::operand::Index &ind) -{ - for (auto it = _claim_table.cbegin(); it != _claim_table.cend(); ++it) - { - if (it->second == ind) - { - uint32_t offset = it->first; - uint32_t index = ind.value(); - uint32_t size = _mem_plans[ind].size; - - _claim_table.erase(it); - - VERBOSE(FF_PLANNER) << "release(#" << index << "): [+" << offset << ", " << size << "sz]" - << std::endl; - return; - } - } - assert(!"Cannot release for given index. It has been not claimed or released already."); -} - -} // namespace cpu -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/cpu/MemoryPlanner.h b/runtimes/neurun/src/backend/cpu/MemoryPlanner.h deleted file mode 100644 index 4b2661223..000000000 --- a/runtimes/neurun/src/backend/cpu/MemoryPlanner.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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       MemoryPlanner.h - * @brief      This file contains Memory Planning related classes - */ - -#ifndef __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ -#define __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ - -#include <map> -#include <unordered_map> - -#include "model/operand/Index.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -/** - * @brief Structure to have memory offset and size - */ -struct Block -{ - uint32_t offset; - uint32_t size; -}; - -/** - * @brief Class to allocate memory - */ -class Allocator -{ -public: - Allocator(uint32_t capacity); - ~Allocator(); - /** - * @brief Get memory base pointer - * @return base pointer - */ - uint8_t *base() const { return _base; } - -private: - uint8_t *_base = nullptr; -}; - -/** - * @brief Interface to plan memory - */ -struct IMemoryPlanner -{ - using MemoryPlans = std::unordered_map<model::operand::Index, Block>; - - /** - * @brief Claim memory for operand - * @param[in] index The operand index - * @param[in] size The size of the memory - */ - virtual void claim(const model::operand::Index &, size_t) = 0; - /** - * @brief Release memory for operand - * @param[in] index The operand index - */ - virtual void release(const model::operand::Index &) = 0; - /** - * @brief Get capacity for memory planning - * @return The value of capacity - */ - virtual uint32_t capacity() = 0; - /** - * @brief Get MemoryPlans - * @return MemoryPlans - */ - virtual MemoryPlans &memory_plans() = 0; -}; - -/** - * @brief Class to plan memory by bump way - */ -class BumpPlanner : public IMemoryPlanner -{ -public: - /** - * @brief Claim memory for operand by bump way - * @param[in] index The operand index - * @param[in] size The size of the memory - */ - virtual void claim(const model::operand::Index &, size_t) override; - /** - * @brief Release memory for operand by bump way - * @param[in] index The operand index - */ - virtual void release(const model::operand::Index &) override; - /** - * @brief Get capacity for memory planning - * @return The value of capacity - */ - virtual uint32_t capacity() override { return _capacity; } - /** - * @brief Get MemoryPlans - * @return MemoryPlans - */ - virtual MemoryPlans &memory_plans() override { return _mem_plans; } - -private: - uint32_t _capacity = 0; - MemoryPlans _mem_plans; -}; - -/** - * @brief Class to plan memory by firstfit way - */ -class FirstFitPlanner : public IMemoryPlanner -{ -public: - /** - * @brief Claim memory for operand by firstfit way - * @param[in] index The operand index - * @param[in] size The size of the memory - */ - virtual void claim(const model::operand::Index &, size_t) override; - /** - * @brief Release memory for operand by firstfit way - * @param[in] index The operand index - */ - virtual void release(const model::operand::Index &) override; - /** - * @brief Get capacity for memory planning - * @return The value of capacity - */ - virtual uint32_t capacity() override { return _capacity; } - /** - * @brief Get MemoryPlans - * @return MemoryPlans - */ - virtual MemoryPlans &memory_plans() override { return _mem_plans; } - -private: - uint32_t _capacity = 0; - MemoryPlans _mem_plans; - // Use std::map because claim() assumes that _claim_table is sorted by uint32_t(base_offset) - std::map<uint32_t, model::operand::Index> _claim_table; -}; - -} // namespace cpu -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ diff --git a/runtimes/neurun/src/backend/cpu/PluginClassesAllocator.cc b/runtimes/neurun/src/backend/cpu/PluginClassesAllocator.cc deleted file mode 100644 index 26d4d8858..000000000 --- a/runtimes/neurun/src/backend/cpu/PluginClassesAllocator.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 <memory> -#include "TensorBuilder.h" -#include "StageGenerator.h" -#include "Config.h" -#include "util/logging.h" - -extern "C" { -neurun::backend::cpu::TensorBuilder *allocate_TensorBuilder() -{ - VERBOSE(allocate_TensorBuilder) << "loaded from CPU\n"; - return new neurun::backend::cpu::TensorBuilder; -} - -neurun::backend::cpu::StageGenerator * -allocate_StageGenerator(const neurun::model::operand::Set &operand_ctx, - const std::shared_ptr<neurun::backend::cpu::TensorBuilder> &tensor_builder) -{ - VERBOSE(allocate_StageGenerator) << "loaded from CPU\n"; - return new neurun::backend::cpu::StageGenerator(operand_ctx, tensor_builder); -} - -neurun::backend::cpu::Config *allocate_Config() -{ - VERBOSE(allocate_Config) << "loaded from CPU\n"; - return new neurun::backend::cpu::Config; -} -} diff --git a/runtimes/neurun/src/backend/cpu/StageGenerator.cc b/runtimes/neurun/src/backend/cpu/StageGenerator.cc deleted file mode 100644 index c53b320a4..000000000 --- a/runtimes/neurun/src/backend/cpu/StageGenerator.cc +++ /dev/null @@ -1,547 +0,0 @@ -/* - * 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 "StageGenerator.h" - -#include <stdexcept> - -#include "cpp14/memory.h" -#include "util/Padding.h" -#include "kernel/cpu/OperationUtils.h" -#include "kernel/cpu/ConvolutionLayer.h" -#include "kernel/cpu/AvgPoolLayer.h" -#include "kernel/cpu/MaxPoolLayer.h" -#include "kernel/cpu/ConcatLayer.h" -#include "kernel/cpu/FullyConnectedLayer.h" -#include "kernel/cpu/ReshapeLayer.h" -#include "kernel/cpu/SoftMaxLayer.h" -#include "kernel/cpu/PermuteLayer.h" -#include "backend/BackendManager.h" -#include "backend/interface/IConfig.h" - -#include "util/logging.h" - -#include "util/Utils.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -StageGenerator::StageGenerator(const neurun::model::operand::Set &operand_ctx, - const std::shared_ptr<TensorBuilder> &tensor_builder) - : _ctx(operand_ctx), _tensor_builder(tensor_builder) -{ - // DO NOTHING -} - -void StageGenerator::visit(const model::operation::Conv2DNode &node) -{ - using model::operation::Conv2DNode; - - const auto ofm_index{node.getOutputs().at(0)}; - const auto ifm_index{node.getInputs().at(Conv2DNode::Input::INPUT)}; - const auto ker_index{node.getInputs().at(Conv2DNode::Input::KERNEL)}; - const auto bias_index{node.getInputs().at(Conv2DNode::Input::BIAS)}; - - const auto vstride_index{node.param().vstride_index}; - const auto hstride_index{node.param().hstride_index}; - - const auto padding_index{node.param().padding_index}; - const auto activation_index{node.param().activation_index}; - - const PaddingCode padding_type = - static_cast<PaddingCode>(_ctx.at(padding_index).asScalar<int32_t>()); - - assert((ANEURALNETWORKS_PADDING_SAME == padding_type) || - (ANEURALNETWORKS_PADDING_VALID == padding_type)); - - util::Stride stride; - - stride.vertical = _ctx.at(vstride_index).asScalar<int32_t>(); - stride.horizontal = _ctx.at(hstride_index).asScalar<int32_t>(); - - // Construct operation parameters - struct Param - { - model::operand::Index ofm_index; - model::operand::Index ifm_index; - model::operand::Index ker_index; - model::operand::Index bias_index; - - ::neurun::kernel::cpu::Shape ofm_shape; - ::neurun::kernel::cpu::Shape ifm_shape; - ::neurun::kernel::cpu::Shape ker_shape; - ::neurun::kernel::cpu::Shape bias_shape; - - util::Padding padding; - util::Stride stride; - - FuseCode activation; - }; - - Param param; - - param.ofm_index = ofm_index; - param.ifm_index = ifm_index; - param.ker_index = ker_index; - param.bias_index = bias_index; - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ofm_index)); - param.ifm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ifm_index)); - param.ker_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ker_index)); - param.bias_shape = ::neurun::kernel::cpu::getShape(_ctx.at(bias_index)); - - param.stride = stride; - param.padding = (padding_type == ANEURALNETWORKS_PADDING_SAME) - ? util::same_padding(_ctx.at(ifm_index).shape().asFeature(), - _ctx.at(ofm_index).shape().asFeature(), stride, - _ctx.at(ker_index).shape().asKernel().W, - _ctx.at(ker_index).shape().asKernel().H) - : util::valid_padding(); - - param.activation = static_cast<FuseCode>(_ctx.at(activation_index).asScalar<int32_t>()); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto ofm_alloc = tensors->at(param.ofm_index); - auto ifm_alloc = tensors->at(param.ifm_index); - auto ker_alloc = tensors->at(param.ker_index); - auto bias_alloc = tensors->at(param.bias_index); - - std::unique_ptr<::neurun::kernel::cpu::ConvolutionLayer> fn{ - new ::neurun::kernel::cpu::ConvolutionLayer}; - - fn->configure(ifm_alloc->buffer(), param.ifm_shape, ker_alloc->buffer(), param.ker_shape, - bias_alloc->buffer(), param.bias_shape, param.padding.left, param.padding.right, - param.padding.top, param.padding.bottom, param.stride.horizontal, - param.stride.vertical, param.activation, ofm_alloc->buffer(), param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::MaxPool2DNode &node) -{ - const auto ofm_index{node.getOutputs().at(0)}; - const auto ifm_index{node.getInputs().at(model::operation::MaxPool2DNode::Input::INPUT)}; - - const auto kh_index{node.param().kh_index}; - const auto kw_index{node.param().kw_index}; - - const auto vstride_index{node.param().vstride_index}; - const auto hstride_index{node.param().hstride_index}; - - const auto padding_index{node.param().padding_index}; - const auto activation_index{node.param().activation_index}; - - const int32_t kh = _ctx.at(kh_index).asScalar<int32_t>(); - const int32_t kw = _ctx.at(kw_index).asScalar<int32_t>(); - - const int32_t vstride = _ctx.at(vstride_index).asScalar<int32_t>(); - const int32_t hstride = _ctx.at(hstride_index).asScalar<int32_t>(); - - const PaddingCode padding_type = - static_cast<PaddingCode>(_ctx.at(padding_index).asScalar<int32_t>()); - - // Construct operation parameters - struct Param - { - model::operand::Index ofm_index; - model::operand::Index ifm_index; - - uint32_t kw; - uint32_t kh; - - ::neurun::kernel::cpu::Shape ofm_shape; - ::neurun::kernel::cpu::Shape ifm_shape; - - util::Padding padding; - util::Stride stride; - - FuseCode activation; - }; - - Param param; - - param.ofm_index = ofm_index; - param.ifm_index = ifm_index; - - param.kh = kh; - param.kw = kw; - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ofm_index)); - param.ifm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ifm_index)); - - param.stride.vertical = vstride; - param.stride.horizontal = hstride; - - param.padding = - (padding_type == ANEURALNETWORKS_PADDING_SAME) - ? util::same_padding(_ctx.at(ifm_index).shape().asFeature(), - _ctx.at(ofm_index).shape().asFeature(), param.stride, kw, kh) - : util::valid_padding(); - - param.activation = static_cast<FuseCode>(_ctx.at(activation_index).asScalar<int32_t>()); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto ofm_alloc = tensors->at(param.ofm_index).get(); - auto ifm_alloc = tensors->at(param.ifm_index).get(); - - std::unique_ptr<::neurun::kernel::cpu::MaxPoolLayer> fn{ - new ::neurun::kernel::cpu::MaxPoolLayer}; - - fn->configure(ifm_alloc->buffer(), param.ifm_shape, param.padding.left, param.padding.right, - param.padding.top, param.padding.bottom, param.stride.horizontal, - param.stride.vertical, param.kw, param.kh, param.activation, ofm_alloc->buffer(), - param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::AvgPool2DNode &node) -{ - const auto ofm_index{node.getOutputs().at(0)}; - const auto ifm_index{node.getInputs().at(model::operation::AvgPool2DNode::Input::INPUT)}; - - const auto kh_index{node.param().kh_index}; - const auto kw_index{node.param().kw_index}; - - const auto vstride_index{node.param().vstride_index}; - const auto hstride_index{node.param().hstride_index}; - - const auto padding_index{node.param().padding_index}; - const auto activation_index{node.param().activation_index}; - - const int32_t kh = _ctx.at(kh_index).asScalar<int32_t>(); - const int32_t kw = _ctx.at(kw_index).asScalar<int32_t>(); - - const int32_t vstride = _ctx.at(vstride_index).asScalar<int32_t>(); - const int32_t hstride = _ctx.at(hstride_index).asScalar<int32_t>(); - - const PaddingCode padding_type = - static_cast<PaddingCode>(_ctx.at(padding_index).asScalar<int32_t>()); - - assert((ANEURALNETWORKS_PADDING_SAME == padding_type) || - (ANEURALNETWORKS_PADDING_VALID == padding_type)); - - // Construct operation parameters - struct Param - { - model::operand::Index ofm_index; - model::operand::Index ifm_index; - - uint32_t kw; - uint32_t kh; - - ::neurun::kernel::cpu::Shape ofm_shape; - ::neurun::kernel::cpu::Shape ifm_shape; - - util::Padding padding; - util::Stride stride; - - FuseCode activation; - }; - - Param param; - - param.ofm_index = ofm_index; - param.ifm_index = ifm_index; - - param.kh = kh; - param.kw = kw; - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ofm_index)); - param.ifm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ifm_index)); - - param.stride.vertical = vstride; - param.stride.horizontal = hstride; - - param.padding = - (padding_type == ANEURALNETWORKS_PADDING_SAME) - ? util::same_padding(_ctx.at(ifm_index).shape().asFeature(), - _ctx.at(ofm_index).shape().asFeature(), param.stride, kw, kh) - : util::valid_padding(); - - param.activation = static_cast<FuseCode>(_ctx.at(activation_index).asScalar<int32_t>()); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto ofm_alloc = tensors->at(param.ofm_index).get(); - auto ifm_alloc = tensors->at(param.ifm_index).get(); - - std::unique_ptr<::neurun::kernel::cpu::AvgPoolLayer> fn{ - new ::neurun::kernel::cpu::AvgPoolLayer}; - - fn->configure(ifm_alloc->buffer(), param.ifm_shape, param.padding.left, param.padding.right, - param.padding.top, param.padding.bottom, param.stride.horizontal, - param.stride.vertical, param.kw, param.kh, param.activation, ofm_alloc->buffer(), - param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::ConcatNode &node) -{ - const auto ofm_index{node.getOutputs().at(0)}; - const auto axis_index{node.param().axis_index}; - - struct Param - { - model::operand::Index output_index; - std::vector<model::operand::Index> input_indexes; - - int32_t axis; - - ::neurun::kernel::cpu::Shape ofm_shape; - std::vector<::neurun::kernel::cpu::Shape> ifm_shapes; - }; - - Param param; - - param.output_index = ofm_index; - for (const auto &e : node.getInputs()) - { - param.input_indexes.emplace_back(e); - } - param.axis = _ctx.at(axis_index).asScalar<int32_t>(); - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(ofm_index)); - - for (auto e : node.getInputs()) - { - param.ifm_shapes.emplace_back(::neurun::kernel::cpu::getShape(_ctx.at(e))); - } - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - - std::vector<const uint8_t *> input_buffers; - for (auto ifm_ind : param.input_indexes) - { - input_buffers.emplace_back(tensors->at(ifm_ind).get()->buffer()); - } - - std::unique_ptr<::neurun::kernel::cpu::ConcatLayer> fn{new ::neurun::kernel::cpu::ConcatLayer}; - - fn->configure(input_buffers, param.ifm_shapes, param.axis, output_alloc->buffer(), - param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::FullyConnectedNode &node) -{ - using model::operation::FullyConnectedNode; - - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(FullyConnectedNode::Input::INPUT)}; - const auto weight_index{node.getInputs().at(FullyConnectedNode::Input::WEIGHT)}; - const auto bias_index{node.getInputs().at(FullyConnectedNode::Input::BIAS)}; - const auto activation_index{node.param().activation_index}; - - // Construct operation parameters - struct Param - { - model::operand::Index output_index; - model::operand::Index input_index; - model::operand::Index weight_index; - model::operand::Index bias_index; - - ::neurun::kernel::cpu::Shape ofm_shape; - ::neurun::kernel::cpu::Shape ifm_shape; - ::neurun::kernel::cpu::Shape weight_shape; - ::neurun::kernel::cpu::Shape bias_shape; - - FuseCode activation; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - param.weight_index = weight_index; - param.bias_index = bias_index; - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(output_index)); - param.ifm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(input_index)); - param.weight_shape = ::neurun::kernel::cpu::getShape(_ctx.at(weight_index)); - param.bias_shape = ::neurun::kernel::cpu::getShape(_ctx.at(bias_index)); - - param.activation = static_cast<FuseCode>(_ctx.at(activation_index).asScalar<int32_t>()); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - auto input_alloc = tensors->at(param.input_index).get(); - auto weight_alloc = tensors->at(param.weight_index).get(); - auto bias_alloc = tensors->at(param.bias_index).get(); - - std::unique_ptr<::neurun::kernel::cpu::FullyConnectedLayer> fn{ - new ::neurun::kernel::cpu::FullyConnectedLayer}; - - fn->configure(input_alloc->buffer(), param.ifm_shape, weight_alloc->buffer(), - param.weight_shape, bias_alloc->buffer(), param.bias_shape, param.activation, - output_alloc->buffer(), param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::ReshapeNode &node) -{ - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(model::operation::ReshapeNode::Input::INPUT)}; - - struct Param - { - model::operand::Index output_index; - model::operand::Index input_index; - - ::neurun::kernel::cpu::Shape ofm_shape; - ::neurun::kernel::cpu::Shape ifm_shape; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(output_index)); - param.ifm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(input_index)); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - auto input_alloc = tensors->at(param.input_index).get(); - - std::unique_ptr<::neurun::kernel::cpu::ReshapeLayer> fn{ - new ::neurun::kernel::cpu::ReshapeLayer}; - - fn->configure(input_alloc->buffer(), param.ifm_shape, output_alloc->buffer(), param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::SoftmaxNode &node) -{ - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(model::operation::SoftmaxNode::Input::INPUT)}; - const auto scale_index{node.param().scale_index}; - - struct Param - { - model::operand::Index output_index; - model::operand::Index input_index; - - ::neurun::kernel::cpu::Shape ofm_shape; - ::neurun::kernel::cpu::Shape ifm_shape; - - float scale; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - - param.ofm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(output_index)); - param.ifm_shape = ::neurun::kernel::cpu::getShape(_ctx.at(input_index)); - - param.scale = _ctx.at(scale_index).asScalar<float>(); - - auto tensors = _tensor_builder; - - returnStage([tensors, param](IExecutionBuilder &builder) { - auto output_alloc = tensors->at(param.output_index).get(); - auto input_alloc = tensors->at(param.input_index).get(); - - std::unique_ptr<::neurun::kernel::cpu::SoftMaxLayer> fn{ - new ::neurun::kernel::cpu::SoftMaxLayer}; - - fn->configure(input_alloc->buffer(), param.ifm_shape, param.scale, output_alloc->buffer(), - param.ofm_shape); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::PermuteNode &node) -{ - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(0)}; - - using PermuteType = model::operation::PermuteNode::Type; - - struct Param - { - model::operand::Index output_index; - model::operand::Index input_index; - - model::operand::Shape shape; - - PermuteType type{PermuteType::COPY}; - }; - - Param param; - - param.output_index = output_index; - param.input_index = input_index; - - param.shape = _ctx.at(output_index).shape(); - param.type = node.param().type; - - // assert(param.shape == _ctx.at(input_index)); - - const auto &input_li = _ctx.at(input_index).lower_info(); - const auto &output_li = _ctx.at(output_index).lower_info(); - const auto input_backend = input_li->def_backends().getOnlyElement(); - const auto output_backend = output_li->def_backends().getOnlyElement(); - - const auto input_tensors = input_backend->tensor_builder(); - const auto output_tensors = output_backend->tensor_builder(); - - returnStage([input_tensors, output_tensors, param](IExecutionBuilder &builder) { - auto output_object = output_tensors->wrapTensor(param.output_index); - auto input_object = input_tensors->wrapTensor(param.input_index); - - auto fn = nnfw::cpp14::make_unique<::neurun::kernel::cpu::PermuteLayer>(); - - fn->configure(input_object, output_object, param.shape, param.type); - - builder.append(std::move(fn)); - }); -} - -void StageGenerator::visit(const model::operation::AddNode &) { throw std::runtime_error("NYI"); } - -} // namespace neurun -} // namespace backend -} // namespace cpu diff --git a/runtimes/neurun/src/backend/cpu/StageGenerator.h b/runtimes/neurun/src/backend/cpu/StageGenerator.h deleted file mode 100644 index 6a0e387da..000000000 --- a/runtimes/neurun/src/backend/cpu/StageGenerator.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_CPU_STAGE_GENERATOR_H__ -#define __NEURUN_BACKEND_CPU_STAGE_GENERATOR_H__ - -#include "backend/interface/IStageGenerator.h" - -#include "model/operand/Set.h" -#include "backend/cpu/operand/Tensor.h" -#include "TensorBuilder.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -class StageGenerator : public IStageGenerator -{ -public: - StageGenerator(const neurun::model::operand::Set &ctx, - const std::shared_ptr<TensorBuilder> &tensor_builder); - - virtual std::shared_ptr<ITensorBuilder> tensor_builder() override { return _tensor_builder; } - -#define OP(InternalName, IsNnApi, NnApiName) \ - virtual void visit(const model::operation::InternalName &) override; -#include "model/operation/Op.lst" -#undef OP - -private: - const neurun::model::operand::Set &_ctx; - std::shared_ptr<TensorBuilder> _tensor_builder; -}; - -} // namespace cpu -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_CPU_STAGE_GENERATOR_H__ diff --git a/runtimes/neurun/src/backend/cpu/TensorBuilder.cc b/runtimes/neurun/src/backend/cpu/TensorBuilder.cc deleted file mode 100644 index 9c39b9c00..000000000 --- a/runtimes/neurun/src/backend/cpu/TensorBuilder.cc +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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 "TensorBuilder.h" - -#include <cassert> - -#include "operand/Object.h" -#include "util/logging.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -TensorBuilder::TensorBuilder() : _mem_planner(std::make_shared<FirstFitPlanner>()) -{ - // DO NOTHING -} - -void TensorBuilder::registerTensorInfo(const model::operand::Index &ind, - const compiler::TensorInfo &info) -{ - _tensor_info_map.insert({ind, info}); -} - -void TensorBuilder::registerSubTensorInfo(const model::operand::Index &, - const compiler::SubTensorInfo &) -{ - // Not supported yet - assert(false); -} - -void TensorBuilder::notifyFirstUse(const model::operand::Index &ind) -{ - assert(_tensor_info_map.find(ind) != _tensor_info_map.end()); - const auto &info = _tensor_info_map.at(ind); - - const auto size = info.total_size(); - _mem_planner->claim(ind, size); -} - -void TensorBuilder::notifyLastUse(const model::operand::Index &ind) { _mem_planner->release(ind); } - -void TensorBuilder::prepare(void) -{ - assert(_tensors.size() == 0); - - _mem_alloc = std::make_shared<Allocator>(_mem_planner->capacity()); - assert(_mem_alloc->base()); - - for (auto &mem_plan : _mem_planner->memory_plans()) - { - auto ind = mem_plan.first; - auto mem_blk = mem_plan.second; - const auto &info = _tensor_info_map[ind]; - - uint8_t *buffer = _mem_alloc->base() + mem_blk.offset; - auto tensor = std::make_shared<operand::Tensor>(info); - tensor->setBuffer(buffer); - _tensors[ind] = tensor; - - VERBOSE(CPU_TENSORBUILDER) << "TENSOR(#" << ind.value() << "): " << static_cast<void *>(buffer) - << std::endl; - - // If we do not make tensor here currently, stages would cause segment fault - } -} - -void TensorBuilder::allocate(void) -{ - // NOTE For now nothing to do. Allocation is done in prepare stage, which is wrong -} - -std::shared_ptr<::neurun::backend::operand::ITensor> -TensorBuilder::tensorAt(const model::operand::Index &ind) -{ - return _tensors.at(ind); -} - -std::shared_ptr<backend::operand::IObject> -TensorBuilder::wrapTensor(const model::operand::Index &ind) -{ - if (_objects.find(ind) != _objects.end()) - { - return _objects.at(ind); - } - else - { - return _objects[ind] = std::make_shared<operand::Object>(_tensors.at(ind)); - } -} - -void TensorBuilder::iterate(const IterateFunction &fn) -{ - for (auto it : _tensors) - { - fn(it.first); - } -} - -std::shared_ptr<operand::Tensor> TensorBuilder::at(const ::neurun::model::operand::Index &ind) -{ - return _tensors.at(ind); -} - -} // namespace cpu -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/cpu/TensorBuilder.h b/runtimes/neurun/src/backend/cpu/TensorBuilder.h deleted file mode 100644 index 2715d57f0..000000000 --- a/runtimes/neurun/src/backend/cpu/TensorBuilder.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_CPU_TENSOR_BUILDER_H__ -#define __NEURUN_BACKEND_CPU_TENSOR_BUILDER_H__ - -#include <unordered_map> - -#include "backend/interface/ITensorBuilder.h" -#include "backend/cpu/operand/Tensor.h" -#include "backend/cpu/operand/Object.h" -#include "model/operand/Index.h" -#include "MemoryPlanner.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ - -class TensorBuilder : public ITensorBuilder -{ -public: - TensorBuilder(); - - /** - * @brief Register tensor information to allocate on CPU backend - * @param[in] ind Operand index - * @param[in] info Tensor information - */ - virtual void registerTensorInfo(const model::operand::Index &ind, - const compiler::TensorInfo &info) override; - /** - * @brief Register subtensor information to allocate on CPU backend - * @param[in] ind Operand index - * @param[in] info Tensor information - */ - virtual void registerSubTensorInfo(const model::operand::Index &ind, - const compiler::SubTensorInfo &info) override; - - virtual void notifyFirstUse(const model::operand::Index &) override; - virtual void notifyLastUse(const model::operand::Index &) override; - - virtual void prepare(void) override; - virtual void allocate(void) override; - - virtual std::shared_ptr<::neurun::backend::operand::ITensor> - tensorAt(const model::operand::Index &ind) override; - virtual std::shared_ptr<backend::operand::IObject> - wrapTensor(const model::operand::Index &ind) override; - virtual void iterate(const IterateFunction &fn) override; - - std::shared_ptr<operand::Tensor> at(const ::neurun::model::operand::Index &ind); - -private: - std::unordered_map<model::operand::Index, compiler::TensorInfo> _tensor_info_map; - std::unordered_map<model::operand::Index, std::shared_ptr<operand::Tensor>> _tensors; - std::unordered_map<model::operand::Index, std::shared_ptr<operand::Object>> _objects; - std::unordered_map<model::operand::Index, Block> _tensor_mem_map; - std::shared_ptr<IMemoryPlanner> _mem_planner; - std::shared_ptr<Allocator> _mem_alloc; -}; - -} // namespace cpu -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_CPU_TENSOR_BUILDER_H__ diff --git a/runtimes/neurun/src/backend/cpu/operand/Object.cc b/runtimes/neurun/src/backend/cpu/operand/Object.cc deleted file mode 100644 index 011747a8c..000000000 --- a/runtimes/neurun/src/backend/cpu/operand/Object.cc +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 "Object.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ -namespace operand -{ - -void Object::access( - const std::function<void(::neurun::backend::operand::ITensor &tensor)> &fn) const -{ - fn(*_tensor); -} - -} // namespace operand -} // namespace cpu -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/cpu/operand/Object.h b/runtimes/neurun/src/backend/cpu/operand/Object.h deleted file mode 100644 index 5ef7c4fbf..000000000 --- a/runtimes/neurun/src/backend/cpu/operand/Object.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_CPU_OPERAND_OBJECT_H__ -#define __NEURUN_BACKEND_CPU_OPERAND_OBJECT_H__ - -#include <memory> -#include "backend/interface/operand/ITensor.h" - -#include "backend/interface/operand/IObject.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ -namespace operand -{ - -class Object : public backend::operand::IObject -{ -public: - Object() = default; - -public: - Object(const std::shared_ptr<::neurun::backend::operand::ITensor> &tensor) : _tensor{tensor} - { - // DO NOTHING - } - -public: - ::neurun::backend::operand::ITensor *ptr(void) const override { return _tensor.get(); } - -private: - std::shared_ptr<::neurun::backend::operand::ITensor> _tensor; - -public: - void - access(const std::function<void(::neurun::backend::operand::ITensor &tensor)> &fn) const override; -}; - -} // namespace operand -} // namespace cpu -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_CPU_OPERAND_OBJECT_H__ diff --git a/runtimes/neurun/src/backend/cpu/operand/Tensor.cc b/runtimes/neurun/src/backend/cpu/operand/Tensor.cc deleted file mode 100644 index a5251292e..000000000 --- a/runtimes/neurun/src/backend/cpu/operand/Tensor.cc +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 "Tensor.h" - -#define NO_USE(a) (void)(a) - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ -namespace operand -{ - -size_t Tensor::calcOffset(const neurun::util::feature::Coordinate4D &coords) -{ - NO_USE(coords); - throw std::runtime_error("offset_element_in_bytes is not supported for cpu::Tensor now."); -} - -} // namespace operand -} // namespace cpu -} // namespace backend -} // namespace neurun diff --git a/runtimes/neurun/src/backend/cpu/operand/Tensor.h b/runtimes/neurun/src/backend/cpu/operand/Tensor.h deleted file mode 100644 index 7500f890f..000000000 --- a/runtimes/neurun/src/backend/cpu/operand/Tensor.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_CPU_OPERAND_TENSOR_H__ -#define __NEURUN_BACKEND_CPU_OPERAND_TENSOR_H__ - -#include "backend/interface/operand/ITensor.h" -#include "compiler/TensorInfo.h" - -namespace neurun -{ -namespace backend -{ -namespace cpu -{ -namespace operand -{ - -class Tensor : public ::neurun::backend::operand::ITensor -{ -public: - Tensor() = delete; - -public: - Tensor(const compiler::TensorInfo &info) : _info(info) - { - // DO NOTHING - } - -public: - void setBuffer(uint8_t *buffer) { _buffer = buffer; } - ::neurun::model::operand::DataType data_type() const { return _info.typeInfo().type(); } - -public: - uint8_t *buffer() const override { return _buffer; } - /** - * @brief Get dimension by index - * - * @param index Index to get diemension - * @return size_t Dimension at index - * @note N : dimension(0) - * H : dimension(1) - * W : dimension(2) - * C : dimension(3) - */ - size_t dimension(size_t index) const override { return _info.shape().dim(index); } - size_t num_dimensions() const override { return _info.shape().dims().size(); } - size_t total_size() const override { return _info.total_size(); } - size_t calcOffset(const neurun::util::feature::Coordinate4D &coords) override; - -private: - compiler::TensorInfo _info; - uint8_t *_buffer = nullptr; -}; - -} // namespace operand -} // namespace cpu -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_CPU_OPERAND_TENSOR_H__ diff --git a/runtimes/neurun/src/backend/interface/IConfig.h b/runtimes/neurun/src/backend/interface/IConfig.h deleted file mode 100644 index 82789d0ff..000000000 --- a/runtimes/neurun/src/backend/interface/IConfig.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ICONFIG_H__ -#define __NEURUN_BACKEND_ICONFIG_H__ - -#include <string> - -#include "graph/operand/Layout.h" - -namespace neurun -{ -namespace backend -{ - -struct IConfig -{ - virtual ~IConfig() = default; - - virtual std::string id() = 0; - virtual void initialize() = 0; - // NOTE Assume backend has only one type of operand layout - virtual graph::operand::Layout getOperandLayout() = 0; - // Support subtensor allocation - virtual bool SupportSubTensorAlloc() = 0; -}; - -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ICONFIG_H__ diff --git a/runtimes/neurun/src/backend/interface/IStageGenerator.h b/runtimes/neurun/src/backend/interface/IStageGenerator.h deleted file mode 100644 index 878a50e3f..000000000 --- a/runtimes/neurun/src/backend/interface/IStageGenerator.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ISTAGE_GENERATOR_H__ -#define __NEURUN_BACKEND_ISTAGE_GENERATOR_H__ - -#include <memory> -#include <functional> - -#include "exec/interface/IFunction.h" - -#include "backend/interface/ITensorBuilder.h" -#include "model/operation/NodeVisitor.h" - -struct IExecutionBuilder -{ - virtual ~IExecutionBuilder() = default; - - virtual void append(std::unique_ptr<::neurun::exec::IFunction> &&f) = 0; -}; - -using Stage = std::function<void(IExecutionBuilder &)>; - -namespace neurun -{ -namespace backend -{ - -class IStageGenerator : model::operation::NodeVisitor -{ -public: - virtual ~IStageGenerator() = default; - - virtual std::shared_ptr<ITensorBuilder> tensor_builder() = 0; - -protected: -#define OP(InternalName, IsNnApi, NnApiName) \ - virtual void visit(const model::operation::InternalName &) override {} -#include "model/operation/Op.lst" -#undef OP - -protected: - void returnStage(const Stage &stage) { _return = stage; } - -public: - Stage generate(const model::operation::Node &node) - { - node.accept(std::move(*this)); - return _return; - } - -private: - Stage _return = nullptr; -}; - -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ISTAGE_GENERATOR_H__ diff --git a/runtimes/neurun/src/backend/interface/ITensorBuilder.h b/runtimes/neurun/src/backend/interface/ITensorBuilder.h deleted file mode 100644 index 354a270e6..000000000 --- a/runtimes/neurun/src/backend/interface/ITensorBuilder.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_ITENSOR_BUILDER_H__ -#define __NEURUN_BACKEND_ITENSOR_BUILDER_H__ - -#include <map> - -#include "model/operand/Index.h" -#include "operand/IObject.h" -#include "compiler/SubTensorInfo.h" -#include "compiler/TensorInfo.h" -#include "backend/interface/operand/ITensor.h" - -namespace neurun -{ -namespace backend -{ - -struct ITensorBuilder -{ - using IterateFunction = std::function<void(const model::operand::Index &)>; - - virtual ~ITensorBuilder(void) = default; - - // TODO Merge registerTensorInfo and registerSubTensorInfo using abstraction by internal class - /** - * @brief Register tensor information to allocate on backend - */ - virtual void registerTensorInfo(const model::operand::Index &, const compiler::TensorInfo &) = 0; - /** - * @brief Register subtensor information to allocate on backend - */ - virtual void registerSubTensorInfo(const model::operand::Index &, - const compiler::SubTensorInfo &) = 0; - - virtual void notifyFirstUse(const model::operand::Index &) = 0; - virtual void notifyLastUse(const model::operand::Index &) = 0; - - virtual void prepare(void) = 0; - virtual void allocate(void) = 0; - - virtual std::shared_ptr<::neurun::backend::operand::ITensor> - tensorAt(const model::operand::Index &ind) = 0; - virtual std::shared_ptr<backend::operand::IObject> - wrapTensor(const model::operand::Index &ind) = 0; - virtual void iterate(const IterateFunction &fn) = 0; -}; - -} // namespace backend -} // namespace neurun - -#include <set> -#include <memory> - -namespace neurun -{ -namespace backend -{ - -using TensorBuilderSet = std::set<std::shared_ptr<backend::ITensorBuilder>>; - -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_ITENSOR_BUILDER_H__ diff --git a/runtimes/neurun/src/backend/interface/operand/IObject.h b/runtimes/neurun/src/backend/interface/operand/IObject.h deleted file mode 100644 index 44b33b080..000000000 --- a/runtimes/neurun/src/backend/interface/operand/IObject.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_OPERAND_I_OBJECT_H__ -#define __NEURUN_BACKEND_OPERAND_I_OBJECT_H__ - -#include <functional> - -#include "ITensor.h" - -namespace neurun -{ -namespace backend -{ -namespace operand -{ - -struct IObject -{ - virtual ~IObject() = default; - virtual ::neurun::backend::operand::ITensor *ptr(void) const = 0; - virtual void - access(const std::function<void(::neurun::backend::operand::ITensor &tensor)> &fn) const = 0; -}; - -} // namespace operand -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_OPERAND_I_OBJECT_H__ diff --git a/runtimes/neurun/src/backend/interface/operand/ITensor.h b/runtimes/neurun/src/backend/interface/operand/ITensor.h deleted file mode 100644 index 8bc3ff465..000000000 --- a/runtimes/neurun/src/backend/interface/operand/ITensor.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_BACKEND_OPERAND_I_TENSOR_H__ -#define __NEURUN_BACKEND_OPERAND_I_TENSOR_H__ - -#include <cstring> -#include <cstdint> - -#include "util/feature/Coordinate4D.h" - -namespace neurun -{ -namespace backend -{ -namespace operand -{ - -class ITensor -{ -public: - virtual ~ITensor() = default; - -public: - virtual uint8_t *buffer() const = 0; - virtual size_t total_size() const = 0; - virtual size_t dimension(size_t index) const = 0; - virtual size_t num_dimensions() const = 0; - virtual size_t calcOffset(const neurun::util::feature::Coordinate4D &coords) = 0; -}; - -} // namespace operand -} // namespace backend -} // namespace neurun - -#endif // __NEURUN_BACKEND_OPERAND_I_TENSOR_H__ diff --git a/runtimes/neurun/src/compiler/BackendResolver.cc b/runtimes/neurun/src/compiler/BackendResolver.cc deleted file mode 100644 index 6c1f32603..000000000 --- a/runtimes/neurun/src/compiler/BackendResolver.cc +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 "BackendResolver.h" - -namespace neurun -{ -namespace compiler -{ - -// NOT IMPLEMENTED - -} // namespace compiler -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/BackendResolver.h b/runtimes/neurun/src/compiler/BackendResolver.h deleted file mode 100644 index 4742b2d94..000000000 --- a/runtimes/neurun/src/compiler/BackendResolver.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_BACKEND_RESOLVER_H__ -#define __NEURUN_COMPILER_BACKEND_RESOLVER_H__ - -#include <set> -#include <unordered_map> -#include <typeindex> - -#include "util/logging.h" -#include "util/config/ConfigManager.h" -#include "backend/BackendManager.h" -#include "backend/interface/IStageGenerator.h" - -namespace neurun -{ -namespace compiler -{ - -class BackendResolver -{ -public: - BackendResolver(const neurun::model::operand::Set &operands) - { - _backend_manager = std::make_shared<backend::BackendManager>(operands); - - const auto backend_all_str = - config::ConfigManager::instance().get<std::string>("OP_BACKEND_ALLOPS"); - if (backend_all_str.compare("none") != 0) - { - VERBOSE(BackendResolver) << "Use backend for all ops: " << backend_all_str << std::endl; -#define OP(InternalName, IsNnApi, NnApiName) \ - if (IsNnApi) \ - { \ - auto backend = _backend_manager->get(backend_all_str); \ - _gen_map[typeid(model::operation::InternalName)] = backend; \ - } -#include "model/operation/Op.lst" -#undef OP - } - else - { -#define OP(InternalName, IsNnApi, NnApiName) \ - if (IsNnApi) \ - { \ - const auto &backend_str = \ - config::ConfigManager::instance().get<std::string>("OP_BACKEND_" #NnApiName); \ - auto backend = _backend_manager->get(backend_str); \ - VERBOSE(BackendResolver) << "backend for " << #NnApiName << ": " << backend_str << std::endl; \ - _gen_map[typeid(model::operation::InternalName)] = backend; \ - } - -#include "model/operation/Op.lst" -#undef OP - } - } - -public: - const backend::Backend *getBackend(const std::type_index &type) { return _gen_map[type]; } - const backend::Backend *getDefaultBackend() const - { - backend::Backend *default_backend = _backend_manager->get("cpu"); - return default_backend; - } - -private: - std::unordered_map<std::type_index, backend::Backend *> _gen_map; - std::shared_ptr<backend::BackendManager> _backend_manager; -}; - -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_BACKEND_RESOLVER_H__ diff --git a/runtimes/neurun/src/compiler/Compiler.cc b/runtimes/neurun/src/compiler/Compiler.cc deleted file mode 100644 index 92ec69afb..000000000 --- a/runtimes/neurun/src/compiler/Compiler.cc +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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 "OperationValidator.h" -#include "SubTensorAnalyzer.h" -#include "PlanBuilder.h" -#include "ConstantInitializer.h" - -#include "graph/dumper/Dumper.h" -#include "graph/operation/LowerInfo.h" -#include "dumper/dot/DotDumper.h" -#include "linear/Linear.h" - -namespace neurun -{ - -namespace compiler -{ - -void Compiler::compile(void) -{ - auto &plan = this->plan(); - auto &graph = plan.model(); - const auto &operands = graph.operands(); - - // Disable compile phase - // When ready to use interpreter backend, remove this config and use backend setting - const auto env_disable_compile = config::ConfigManager::instance().get<bool>("DISABLE_COMPILE"); - if (env_disable_compile) - { - plan.state(State::NOT_COMPILED); - return; - } - - /*************************************************** - * Backend independent analysis & optimization phase - ***************************************************/ - - /************************************************************* - * Backend independent analysis & optimization phase finished - *************************************************************/ - - // dump graph to .dot - neurun::dumper::dot::DotDumper dot_dumper(graph); - dot_dumper.dumpIfNeeded("before_lower"); - - // Lower: decide backend - graph.lower(); - plan.state(State::LOWERED); - - dot_dumper.dumpIfNeeded("after_lower"); - - auto linear = graph.linearize(); - plan.state(State::LINEARIZED); - - // Dump ops - linear->accept(neurun::graph::dumper::Dumper{}); - - linear->accept(OperationValidator{operands}); - - /************************************************* - * Backend dependent analysis & optimization phase - *************************************************/ - - // SubTensorInfo should be generated after lower, before stage generation and finalize - // because SubTensorAnalyzer assume that insert permutation is already finished - // lower: decide backend and insert permutation - // stage generation: prepare codegen 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(SubTensorAnalyzer{graph.operands()}); - - /********************************************************** - * Backend dependent analysis & optimization phase finished - **********************************************************/ - - /*********************** - * Code generation phase - ***********************/ - - PlanBuilder plan_builder{plan}; - - // Plan building - linear->iterate([&](const linear::Element &element) { - auto backend = element.lower_info->backend(); - - // Generate Stage - auto stage_gen = backend->stage_gen(); - plan_builder.addStage(stage_gen->generate(*element.node)); - }); - - auto tensor_builders = linear->planTensors(); - - // TODO Add optimization passes - plan_builder.finalize(tensor_builders); - - ConstantInitializer{graph, plan}(); - - /******************************** - * Code generation phase finished - ********************************/ - - plan.state(State::COMPILED); -} - -} // namespace compiler - -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/Compiler.h b/runtimes/neurun/src/compiler/Compiler.h deleted file mode 100644 index d8f620a10..000000000 --- a/runtimes/neurun/src/compiler/Compiler.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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 "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 Plan{model}} - { - // DO NOTHING - } - -public: - /** - * @brief Return plan - * @return Plan - */ - 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 Plan> &plan) { plan = _plan; } - -private: - std::shared_ptr<Plan> _plan; -}; - -} // namespace compiler - -} // namespace neurun - -#endif // __NEURUN_COMPILER_COMPILE_H_ diff --git a/runtimes/neurun/src/compiler/ConstantInitializer.cc b/runtimes/neurun/src/compiler/ConstantInitializer.cc deleted file mode 100644 index d6d58e273..000000000 --- a/runtimes/neurun/src/compiler/ConstantInitializer.cc +++ /dev/null @@ -1,188 +0,0 @@ -/* - * 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 "ConstantInitializer.h" - -#include "backend/interface/operand/IObject.h" -#include "backend/interface/IConfig.h" -#include "backend/BackendManager.h" -#include "model/operation/FullyConnectedNode.h" -#include "util/feature/nhwc/Reader.h" -#include "util/feature/nhwc/View.h" -#include "util/feature/nchw/View.h" -#include "misc/feature/IndexIterator.h" -#include "util/logging.h" - -namespace neurun -{ -namespace compiler -{ - -ConstantInitializer::ConstantInitializer(const graph::Graph &graph, Plan &plan) - : _graph{graph}, _plan{plan} -{ -} - -void ConstantInitializer::operator()() -{ - // Fill operand data - _plan.operands().iterate([&](int ind, neurun::backend::operand::IObject &obj) { - neurun::model::operand::Index index(ind); - const auto &model_obj = _graph.operands().at(index); - - // For only CONSTANTS - if (model_obj.getUsage() != neurun::model::operand::OperandUsage::CONSTANT) - return; - - // Only float32 is supported - auto type = model_obj.typeInfo().type(); - if (type != ::neurun::model::operand::DataType::TENSOR_FLOAT32) - throw std::runtime_error{"Unsupported data type. Only TENSOR_FLOAT32 is supported."}; - - VERBOSE(FillOperandData) << "Fill data for operand " << ind << std::endl; - - auto layout = - model_obj.lower_info()->def_backends().getOnlyElement()->config()->getOperandLayout(); - const auto shape = model_obj.shape(); - auto base = reinterpret_cast<const float *>(model_obj.data().base()); - auto size = model_obj.data().size(); - - obj.access([&](::neurun::backend::operand::ITensor &tensor) { - switch (shape.rank()) - { - case 1: - { - auto vec_size = shape.asVector(); - for (int32_t n = 0; n < vec_size; ++n) - { - const float *from = reinterpret_cast<const float *>(base) + n; - const auto value = *from; - - float *into = reinterpret_cast<float *>(tensor.buffer()) + n; - - *into = value; - } - break; - } - case 2: - { - // NOTE This is a WORKAROUND which supports FullyConnected weight only - // For FullyConnected, we must know the IFM shape to deduce 2D weight shape from 4D - // IFM. - // This is because of NHWC/NCHW layout, the order of mapping will be different. - // TODO Support general case - explicitly insert Reshape op for IFM as 2D - - // Find corresponding FullyConnected IFM - auto operation_index = _graph.operands().at(index).getUses().list().front(); - auto operation = &_graph.operations().at(operation_index); - auto fc_operation = - dynamic_cast<const neurun::model::operation::FullyConnectedNode *>(operation); - - if (fc_operation == nullptr) - break; - - auto ifm_index = fc_operation->getInputs().at( - neurun::model::operation::FullyConnectedNode::Input::INPUT); - const auto &ifm = _graph.operands().at(ifm_index); - const auto ifm_shape = ifm.shape().asFeature(); - const auto num_output = shape.dim(0); - - const ::nnfw::misc::feature::Shape ker_shape{num_output, ifm_shape.C, ifm_shape.H, - ifm_shape.W}; - const util::feature::nhwc::Reader<float> from{ker_shape, base, size}; - - if (layout == neurun::graph::operand::Layout::NHWC) - { - ::nnfw::misc::feature::iterate(ker_shape) - << [&](uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(nth, ch, row, col); - - uint32_t offset = 0; - - // NNAPI uses NHWC ordering - offset += nth * ifm_shape.H * ifm_shape.W * ifm_shape.C; - offset += row * ifm_shape.W * ifm_shape.C; - offset += col * ifm_shape.C; - offset += ch; - - float *into = reinterpret_cast<float *>(tensor.buffer()) + offset; - - *into = value; - }; - } - else - { - assert(layout == neurun::graph::operand::Layout::NCHW); - - ::nnfw::misc::feature::iterate(ker_shape) - << [&](uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(nth, ch, row, col); - - uint32_t offset = 0; - - // 'NCHW' ordering - offset += nth * ifm_shape.C * ifm_shape.H * ifm_shape.W; - offset += ch * ifm_shape.H * ifm_shape.W; - offset += row * ifm_shape.W; - offset += col; - - float *into = reinterpret_cast<float *>(tensor.buffer()) + offset; - - *into = value; - }; - } - - break; - } - case 4: - { - auto ker_shape = shape.asFeature(); - auto from = util::feature::nhwc::Reader<float>{ker_shape, base, size}; - - if (layout == neurun::graph::operand::Layout::NHWC) - { - auto into = util::feature::nhwc::View<float>{ - ker_shape, reinterpret_cast<float *>(tensor.buffer()), size}; - - ::nnfw::misc::feature::iterate(ker_shape) - << [&](uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(nth, ch, row, col); - into.at(nth, ch, row, col) = value; - }; - } - else - { - assert(layout == neurun::graph::operand::Layout::NCHW); - - auto into = util::feature::nchw::View<float>{&tensor}; - - ::nnfw::misc::feature::iterate(ker_shape) - << [&](uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(nth, ch, row, col); - into.at(nth, ch, row, col) = value; - }; - } - break; - } - default: - throw std::runtime_error{"Not yet supported"}; - } - }); - }); -} - -} // namespace codegen -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/ConstantInitializer.h b/runtimes/neurun/src/compiler/ConstantInitializer.h deleted file mode 100644 index 7d9231908..000000000 --- a/runtimes/neurun/src/compiler/ConstantInitializer.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_CONSTANT_INITIALIZER_H__ -#define __NEURUN_COMPILER_CONSTANT_INITIALIZER_H__ - -#include "graph/Graph.h" -#include "Plan.h" - -namespace neurun -{ -namespace compiler -{ - -class ConstantInitializer -{ -public: - ConstantInitializer(const graph::Graph &graph, Plan &plan); - - void operator()(); - -private: - const graph::Graph &_graph; - Plan &_plan; -}; - -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_CONSTANT_INITIALIZER_H__ diff --git a/runtimes/neurun/src/compiler/OperationValidator.cc b/runtimes/neurun/src/compiler/OperationValidator.cc deleted file mode 100644 index 0110eccb8..000000000 --- a/runtimes/neurun/src/compiler/OperationValidator.cc +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 "OperationValidator.h" - -#include <typeinfo> - -#include "model/operand/Set.h" -#include "graph/operation/LowerInfo.h" - -#include "util/logging.h" - -namespace neurun -{ -namespace compiler -{ - -void OperationValidator::visit(const model::operation::Conv2DNode &) -{ - // DO NOTHING -} - -void OperationValidator::visit(const model::operation::MaxPool2DNode &) -{ - // DO NOTHING -} - -void OperationValidator::visit(const model::operation::AvgPool2DNode &) -{ - // DO NOTHING -} - -void OperationValidator::visit(const model::operation::ConcatNode &node) -{ - (void)node; // NOTE To prevent from unused variable warning - - // NOTE This implementation assumes concat over feature depth - // TODO Remove this assumption - assert(_ctx.at(::neurun::model::operand::Index{node.param().axis_index}).asScalar<int32_t>() == - 3); -} - -void OperationValidator::visit(const model::operation::FullyConnectedNode &) -{ - // DO NOTHING -} - -void OperationValidator::visit(const model::operation::ReshapeNode &node) -{ - (void)node; // NOTE To prevent from unused variable warning - - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(0)}; - - // NOTE The content of a tensor specified by shape_index should be aligned with - // output tensor shape - // TODO Check consistency of ouput shape - - // 'Feature Map' to 'Vector' reshape - assert(_ctx.at(input_index).shape().rank() == 4); - assert(_ctx.at(output_index).shape().rank() == 2); - assert(_ctx.at(output_index).shape().dim(0) == 1); - - // NOTE Vector element ordering issue arises when H or W is not 1 - assert(_ctx.at(input_index).shape().dim(1) == 1); // H - assert(_ctx.at(input_index).shape().dim(2) == 1); // W - // input(4D)'s C * H * W == output(2D)'s W - assert((_ctx.at(input_index).shape().dim(3) * _ctx.at(input_index).shape().dim(1) * - _ctx.at(input_index).shape().dim(2)) == _ctx.at(output_index).shape().dim(1)); -} - -void OperationValidator::visit(const model::operation::SoftmaxNode &node) -{ - (void)node; // NOTE To prevent from unused variable warning - - VERBOSE(Softmax) << "Configure SOFTMAX operation" << std::endl; - - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(0)}; - - assert(_ctx.at(output_index).shape().rank() == _ctx.at(input_index).shape().rank()); - - // TODO Support 'feature map' input - assert(_ctx.at(input_index).shape().rank() == 2); - assert(_ctx.at(input_index).shape().dim(0) == 1); - assert(_ctx.at(input_index).shape().dim(0) == _ctx.at(output_index).shape().dim(0)); - assert(_ctx.at(input_index).shape().dim(1) == _ctx.at(output_index).shape().dim(1)); -} - -void OperationValidator::visit(const model::operation::PermuteNode &node) -{ - (void)node; // NOTE To prevent from unused variable warning - - VERBOSE(Permute) << "Configure Permute operation" << std::endl; - - const auto output_index{node.getOutputs().at(0)}; - const auto input_index{node.getInputs().at(0)}; - - assert(_ctx.at(output_index).shape().rank() == _ctx.at(input_index).shape().rank()); -} - -void OperationValidator::visit(const model::operation::AddNode &) -{ - // DO NOTHING -} - -} // namespace compiler -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/OperationValidator.h b/runtimes/neurun/src/compiler/OperationValidator.h deleted file mode 100644 index f4ed533ed..000000000 --- a/runtimes/neurun/src/compiler/OperationValidator.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_OPERATION_VALIDATOR_H__ -#define __NEURUN_COMPILER_OPERATION_VALIDATOR_H__ - -#include "model/operation/NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ -class Set; -} // namespace operand -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace compiler -{ - -class OperationValidator : public model::operation::NodeVisitor -{ -public: - OperationValidator(const neurun::model::operand::Set &ctx) : _ctx{ctx} {} - -public: -#define OP(InternalName, IsNnApi, NnApiName) \ - virtual void visit(const model::operation::InternalName &) override; -#include "model/operation/Op.lst" -#undef OP - -private: - const neurun::model::operand::Set &_ctx; -}; - -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_OPERATION_VALIDATOR_H__ diff --git a/runtimes/neurun/src/compiler/Plan.cc b/runtimes/neurun/src/compiler/Plan.cc deleted file mode 100644 index b7637b189..000000000 --- a/runtimes/neurun/src/compiler/Plan.cc +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 "Plan.h" - -namespace neurun -{ -namespace codegen -{ - -// NO IMPLEMENTATION YET - -} // namespace codegen -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/Plan.h b/runtimes/neurun/src/compiler/Plan.h deleted file mode 100644 index f2a526e0e..000000000 --- a/runtimes/neurun/src/compiler/Plan.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_CODEGEN_PLAN_H__ -#define __NEURUN_CODEGEN_PLAN_H__ - -#include "graph/Graph.h" -#include "compiler/operand/Context.h" -#include "compiler/operation/Sequence.h" - -namespace neurun -{ -namespace compiler -{ - -enum class State -{ - NONE, // Initial state - LOWERED, // Backend is decided - LINEARIZED, // Everything is moved to Linear object so this Graph object is no longer effective - COMPILED, // Success compilation - NOT_COMPILED // Not compiled by environment or graph status -}; - -class Plan -{ -public: - Plan(const std::shared_ptr<neurun::graph::Graph> &model) : _model(model), _state(State::NONE) - { - // DO NOTHING - } - -public: - neurun::graph::Graph &model(void) { return *_model; } - const neurun::graph::Graph &model(void) const { return *_model; } - - void state(State state) { _state = state; } - State state(void) const { return _state; } - -public: - operand::Context &operands(void) { return _operands; } - const operand::Context &operands(void) const { return _operands; } - -public: - operation::Sequence &operations(void) { return _ops; } - const operation::Sequence &operations(void) const { return _ops; } - -private: - std::shared_ptr<neurun::graph::Graph> _model; - operand::Context _operands; - operation::Sequence _ops; - State _state; -}; - -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_CODEGEN_PLAN_H__ diff --git a/runtimes/neurun/src/compiler/PlanBuilder.cc b/runtimes/neurun/src/compiler/PlanBuilder.cc deleted file mode 100644 index 8ef3fedbf..000000000 --- a/runtimes/neurun/src/compiler/PlanBuilder.cc +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 "PlanBuilder.h" - -#include "backend/interface/operand/IObject.h" - -namespace neurun -{ -namespace compiler -{ - -void PlanBuilder::addStage(const Stage &stage) { _stages.emplace_back(stage); } - -void PlanBuilder::finalize(const backend::TensorBuilderSet &tensor_builders) -{ - auto &operands = _plan.operands(); - - // Prepare tensors - for (auto &tensor_builder : tensor_builders) - { - tensor_builder->prepare(); - - // Wrap tensors as Object and store them to plan - tensor_builder->iterate([&](const model::operand::Index &index) { - auto object = tensor_builder->wrapTensor(index); - operands.set(index, object); - }); - } - - // Process Stage - ExecutionBuilder execution_builder{_plan}; - - for (const auto &stage : _stages) - { - stage(execution_builder); - } - - // Allocate Tensor Memory for cl_tensors - for (auto &tensor_builder : tensor_builders) - { - tensor_builder->allocate(); - } -} - -} // namepsace compiler -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/PlanBuilder.h b/runtimes/neurun/src/compiler/PlanBuilder.h deleted file mode 100644 index 3231906d2..000000000 --- a/runtimes/neurun/src/compiler/PlanBuilder.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_PLAN_BUILDER_H__ -#define __NEURUN_COMPILER_PLAN_BUILDER_H__ - -#include "Plan.h" -#include "backend/interface/IStageGenerator.h" -#include "backend/interface/ITensorBuilder.h" - -namespace neurun -{ -namespace compiler -{ - -class ExecutionBuilder final : public IExecutionBuilder -{ -public: - ExecutionBuilder(Plan &plan) : _plan{plan} - { - // DO NOTHING - } - -public: - void append(std::unique_ptr<::neurun::exec::IFunction> &&f) override - { - _plan.operations().append(std::move(f)); - } - -private: - Plan &_plan; -}; - -class PlanBuilder -{ -public: - PlanBuilder(Plan &plan) : _plan{plan} - { - // DO NOTHING - } - -public: - void addStage(const Stage &stage); - -public: - // TODO Remove the argument `tensor_builders` - void finalize(const backend::TensorBuilderSet &tensor_builders); - -private: - Plan &_plan; - -private: - std::vector<Stage> _stages; -}; - -} // namepsace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_PLAN_BUILDER_H__ diff --git a/runtimes/neurun/src/compiler/SubTensorAnalyzer.cc b/runtimes/neurun/src/compiler/SubTensorAnalyzer.cc deleted file mode 100644 index 0851b7991..000000000 --- a/runtimes/neurun/src/compiler/SubTensorAnalyzer.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 "SubTensorAnalyzer.h" - -#include <typeinfo> - -#include "cpp14/memory.h" -#include "model/operand/Set.h" -#include "graph/operation/LowerInfo.h" -#include "util/logging.h" - -namespace neurun -{ -namespace compiler -{ - -void SubTensorAnalyzer::visit(const model::operation::ConcatNode &node) -{ - // If operator is concat (or other operators related with subsumption), fill subsumption info - // TODO: if one tensor is subset of many parents or model input - // Solution 1. Handle 1st parent only, ignore others (need to invert for other childrun) - // Solution 2. Insert copy operation for other parents - auto axis_index = node.param().axis_index; - - // To prepare concat elimination, axis should be constant - if (_ctx.at(axis_index).getUsage() != model::operand::OperandUsage::CONSTANT) - { - VERBOSE(SUBTENSOR) << "Cannot handle non-constant axis" << std::endl; - return; - } - - // NOTE This implementation assumes concat over feature depth - // TODO Remove this assumption - int32_t axis = _ctx.at(axis_index).asScalar<int32_t>(); - if (axis != 3) - { - VERBOSE(SUBTENSOR) << "Cannot handle axis is not channel" << std::endl; - return; - } - - auto &output_index = node.getOutputs().at(0); - auto &inputs = node.getInputs(); - - int32_t axis_point = 0; - for (auto &input_index : inputs) - { - auto input_shape_4D = _ctx.at(input_index).lower_info()->shape(); - std::vector<int32_t> offset = {0, 0, 0, 0}; - offset[axis] = axis_point; - neurun::util::feature::Coordinate4D coordinate_info(offset[0], offset[1], offset[2], offset[3]); - std::unique_ptr<graph::operand::ParentInfo> parentInfo = - nnfw::cpp14::make_unique<graph::operand::ParentInfo>(output_index, coordinate_info); - - // NOTD Not support multiple parent tensor yet - assert(_ctx.at(input_index).parent_info() == nullptr); - _ctx.at(input_index).parent_info(std::move(parentInfo)); - - // NOTE Only support when axis is 3(channel) - axis_point += input_shape_4D.c(); - } -} - -} // namespace compiler -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/SubTensorAnalyzer.h b/runtimes/neurun/src/compiler/SubTensorAnalyzer.h deleted file mode 100644 index ddfd10263..000000000 --- a/runtimes/neurun/src/compiler/SubTensorAnalyzer.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 SubTensorAnalyzer.h - * @brief This file contains SubTensorAnalyzer to analyze tensor subsumption - * using operation visitor - */ - -#ifndef __NEURUN_COMPILER_SUBTENSOR_ANALYZER_H__ -#define __NEURUN_COMPILER_SUBTENSOR_ANALYZER_H__ - -#include "model/operation/NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ -class Set; -} // namespace operation -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace compiler -{ - -/** - * @brief Class to analyze tensor subsumption - */ -class SubTensorAnalyzer : public model::operation::NodeVisitor -{ -public: - /** - * @brief Construct a new SubTensorAnalyzer object - * @param[in] ctx Graph operand set - */ - SubTensorAnalyzer(neurun::model::operand::Set &ctx) : _ctx{ctx} {} - -public: - virtual void visit(const model::operation::ConcatNode &) override; - -private: - neurun::model::operand::Set &_ctx; -}; - -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_SUBTENSOR_ANALYZER_H__ diff --git a/runtimes/neurun/src/compiler/SubTensorInfo.h b/runtimes/neurun/src/compiler/SubTensorInfo.h deleted file mode 100644 index c0fb857d0..000000000 --- a/runtimes/neurun/src/compiler/SubTensorInfo.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 SubTensorInfo.h - * @brief This file contains SubTensorInfo to represent subsumption between tensors - * for backend tensor allocation - */ -#ifndef __NEURUN_COMPILER_SUBTENSOR_INFO_H__ -#define __NEURUN_COMPILER_SUBTENSOR_INFO_H__ - -#include "model/operand/Object.h" -#include "util/feature/Coordinate4D.h" - -namespace neurun -{ -namespace compiler -{ - -/** - * @brief Class to represent information of subtensor - */ -class SubTensorInfo -{ -public: - SubTensorInfo() = delete; - - /** - * @brief Construct a new SubTensorInfo object - * @param[in] obj SubTensor object - */ - SubTensorInfo(const model::operand::Object &obj) - : _parent{obj.parent_info()->parent()}, _shape{obj.shape()}, _type{obj.typeInfo()}, - _offset{obj.parent_info()->offset()} - { - // DO NOTHING - } - -public: - /** - * @brief Return parent tensor index - * @return Parent tensor index - */ - const model::operand::Index parent(void) const { return _parent; } - /** - * @brief Return tensor shape - * @return Tensor shape - */ - const model::operand::Shape shape(void) const { return _shape; } - /** - * @brief Return tensor type - * @return Tensor type - */ - const model::operand::TypeInfo type(void) const { return _type; } - /** - * @brief Return tensor's offset in parent tensor - * @return Tensor offset - */ - const neurun::util::feature::Coordinate4D offset(void) const { return _offset; } - -private: - const model::operand::Index _parent; - const model::operand::Shape _shape; - const model::operand::TypeInfo _type; - const neurun::util::feature::Coordinate4D _offset; -}; - -} // compiler -} // neurun - -#endif // __NEURUN_COMPILER_SUBTENSOR_INFO_H__ diff --git a/runtimes/neurun/src/compiler/TensorInfo.h b/runtimes/neurun/src/compiler/TensorInfo.h deleted file mode 100644 index 787c433e5..000000000 --- a/runtimes/neurun/src/compiler/TensorInfo.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_TENSOR_INFO_H__ -#define __NEURUN_COMPILER_TENSOR_INFO_H__ - -#include "model/operand/Shape.h" -#include "model/operand/TypeInfo.h" - -#include <numeric> - -namespace neurun -{ -namespace compiler -{ - -class TensorInfo -{ -public: - TensorInfo() = default; - -public: - TensorInfo(const ::neurun::model::operand::Shape &shape, - const ::neurun::model::operand::TypeInfo &typeInfo) - : _shape(shape), _typeInfo(typeInfo) - { - // DO NOTHING - } - -public: - const ::neurun::model::operand::Shape &shape() const { return _shape; } - const ::neurun::model::operand::TypeInfo &typeInfo() const { return _typeInfo; } - size_t total_size() const - { - const auto &dims = _shape.dims(); - return std::accumulate(dims.begin(), dims.end(), 4, std::multiplies<size_t>()); - } - -private: - ::neurun::model::operand::Shape _shape; - ::neurun::model::operand::TypeInfo _typeInfo; -}; - -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_TENSOR_INFO_H__ diff --git a/runtimes/neurun/src/compiler/operand/Context.cc b/runtimes/neurun/src/compiler/operand/Context.cc deleted file mode 100644 index 3fa529995..000000000 --- a/runtimes/neurun/src/compiler/operand/Context.cc +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 "Context.h" - -#include <cassert> - -namespace neurun -{ -namespace compiler -{ -namespace operand -{ - -Context &Context::set(const model::operand::Index &id, - const std::shared_ptr<backend::operand::IObject> &object) -{ - // Only one object for an id - assert(_objects.find(id.value()) == _objects.end()); - _objects[id.value()] = object; - return (*this); -} - -void Context::iterate(const std::function<void(int, backend::operand::IObject &)> &fn) -{ - for (auto &e : _objects) - { - fn(e.first, *e.second); - } -} - -} // namespace operand -} // namespace compiler -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/operand/Context.h b/runtimes/neurun/src/compiler/operand/Context.h deleted file mode 100644 index bc558404e..000000000 --- a/runtimes/neurun/src/compiler/operand/Context.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_OPERAND_CONTEXT_H__ -#define __NEURUN_COMPILER_OPERAND_CONTEXT_H__ - -#include "backend/interface/operand/IObject.h" -#include "model/operand/Index.h" - -#include <map> -#include <memory> - -namespace neurun -{ -namespace compiler -{ -namespace operand -{ - -class Context -{ -public: - Context &set(const model::operand::Index &ind, - const std::shared_ptr<backend::operand::IObject> &object); - -public: - bool exist(const ::neurun::model::operand::Index &ind) const - { - return _objects.find(ind.asInt()) != _objects.end(); - } - -public: - std::shared_ptr<backend::operand::IObject> at(const model::operand::Index &ind) const - { - return _objects.at(ind.asInt()); - } - - std::shared_ptr<backend::operand::IObject> &at(const model::operand::Index &ind) - { - return _objects.at(ind.asInt()); - } - - void iterate(const std::function<void(int, backend::operand::IObject &)> &fn); - -private: - std::map<int, std::shared_ptr<backend::operand::IObject>> _objects; -}; - -} // namespace operand -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_OPERAND_CONTEXT_H__ diff --git a/runtimes/neurun/src/compiler/operation/Sequence.cc b/runtimes/neurun/src/compiler/operation/Sequence.cc deleted file mode 100644 index 3160e04b6..000000000 --- a/runtimes/neurun/src/compiler/operation/Sequence.cc +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 "Sequence.h" - -namespace neurun -{ -namespace compiler -{ -namespace operation -{ - -// NO IMPLEMENTATION YET - -} // namespace operation -} // namespace compiler -} // namespace neurun diff --git a/runtimes/neurun/src/compiler/operation/Sequence.h b/runtimes/neurun/src/compiler/operation/Sequence.h deleted file mode 100644 index d69cfcfe3..000000000 --- a/runtimes/neurun/src/compiler/operation/Sequence.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_COMPILER_OPERATION_SEQUENCE_H__ -#define __NEURUN_COMPILER_OPERATION_SEQUENCE_H__ -#include <stdint.h> -#include "exec/interface/IFunction.h" -#include <memory> -#include <vector> - -namespace neurun -{ -namespace compiler -{ -namespace operation -{ - -class Sequence -{ -public: - uint32_t size(void) const { return _functions.size(); } - -public: - Sequence &append(std::unique_ptr<::neurun::exec::IFunction> &&func) - { - _functions.emplace_back(std::move(func)); - return (*this); - } - -public: - ::neurun::exec::IFunction &at(uint32_t n) const { return *(_functions.at(n)); } - -private: - std::vector<std::unique_ptr<::neurun::exec::IFunction>> _functions; -}; - -} // namespace operation -} // namespace compiler -} // namespace neurun - -#endif // __NEURUN_COMPILER_OPERATION_SEQUENCE_H__ diff --git a/runtimes/neurun/src/dumper/dot/DotBuilder.cc b/runtimes/neurun/src/dumper/dot/DotBuilder.cc deleted file mode 100644 index d694323b4..000000000 --- a/runtimes/neurun/src/dumper/dot/DotBuilder.cc +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 "DotBuilder.h" - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -// NodeAttr -NodeAttr &NodeAttr::addAttr(const std::string &name, const std::string &attr) -{ - _attrs.emplace_back(name, attr); - - return *this; -} - -void NodeAttr::finish() -{ - _attr_stream << "["; - for (auto attr : _attrs) - { - _attr_stream << attr.first << "=" - << "\"" << attr.second << "\" "; - } - _attr_stream << "];\n"; -} - -// DotDumper -DotBuilder::DotBuilder() {} - -void DotBuilder::update(const IDotInfo &node_info) -{ - addNode(node_info); - for (auto child : node_info.children()) - { - addEdge(node_info, *child); - } -} - -void DotBuilder::writeDot(std::ostream &os) -{ - os << "digraph D {\n" - << _dot.str() << "\n" - << "}\n"; -} - -void DotBuilder::addNode(const IDotInfo &dotinfo) -{ - NodeAttr attr; - attr.addAttr("shape", dotinfo.dot_shape()) - .addAttr("label", dotinfo.label()) - .addAttr("style", "filled") - .addAttr("colorscheme", dotinfo.bg_color_scheme()) - .addAttr("fillcolor", dotinfo.bg_color()); - - attr.finish(); - - _dot << dotinfo.index_str() << attr.attr_stream(); -} - -void DotBuilder::addEdge(const IDotInfo &dotinfo1, const IDotInfo &dotinfo2) -{ - _dot << dotinfo1.index_str() << " -> " << dotinfo2.index_str() << ";\n"; -} - -} // namespace dot -} // namespace dumper -} // namespace neurun diff --git a/runtimes/neurun/src/dumper/dot/DotBuilder.h b/runtimes/neurun/src/dumper/dot/DotBuilder.h deleted file mode 100644 index 783e92b80..000000000 --- a/runtimes/neurun/src/dumper/dot/DotBuilder.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_DUMPER_DOT_DOT_BUILDER_H__ -#define __NEURUN_DUMPER_DOT_DOT_BUILDER_H__ - -#include <sstream> - -#include "model/operation/Index.h" -#include "model/operand/Index.h" - -#include "model/operation/Node.h" -#include "model/operand/Object.h" - -#include "DotNodeInfo.h" -#include "DotOperandInfo.h" - -using Node = neurun::model::operation::Node; -using Object = neurun::model::operand::Object; - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -class NodeAttr -{ -public: - NodeAttr() = default; - -public: - void finish(); - NodeAttr &addAttr(const std::string &name, const std::string &attr); - -public: - std::stringbuf *attr_stream() { return _attr_stream.rdbuf(); } - -private: - std::vector<std::pair<std::string, std::string>> _attrs; - std::stringstream _attr_stream; -}; - -class DotBuilder -{ -public: - DotBuilder(); - -public: - void update(const IDotInfo &dotinfo); - - void writeDot(std::ostream &os); - -private: - void addNode(const IDotInfo &dotinfo); - void addEdge(const IDotInfo &dotinfo1, const IDotInfo &dotinfo2); - - std::stringstream _dot; -}; - -} // namespace dot -} // namespace dumper -} // namespace neurun - -#endif // __NEURUN_DUMPER_DOT_DOT_BUILDER_H__ diff --git a/runtimes/neurun/src/dumper/dot/DotDumper.cc b/runtimes/neurun/src/dumper/dot/DotDumper.cc deleted file mode 100644 index 1e53ece19..000000000 --- a/runtimes/neurun/src/dumper/dot/DotDumper.cc +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 <fstream> - -#include "DotDumper.h" -#include "DotBuilder.h" - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -using namespace neurun::graph; - -void DotDumper::dumpIfNeeded(const std::string &tag) -{ - if (_option == OPTIONS::DUMP_OFF) - { - return; - } - neurun::dumper::dot::DotBuilder dot_builder; - - auto &operations = _graph.operations(); - auto &operands = _graph.operands(); - - operations.iterate([&](const model::operation::Index &index, const model::operation::Node &node) { - neurun::dumper::dot::DotNodeInfo node_info(_graph, index, node); - - for (auto output : node.getOutputs()) - { - using neurun::dumper::dot::DotOperandInfo; - auto child = std::make_shared<DotOperandInfo>(output, operands.at(output), - DotOperandInfo::Type::MODEL_OUTPUT); - node_info.appendChild(child); - } - - dot_builder.update(node_info); - }); - - operands.iterate([&](const model::operand::Index &index, const model::operand::Object &object) { - bool showing_cond = false; - auto usage = object.getUsage(); - if (_option == OPTIONS::SHOW_CONSTANTS) - { - showing_cond = object.usageIsDefined(); - } - else - { - showing_cond = (usage == model::operand::OperandUsage::MODEL_INPUT) || - (usage == model::operand::OperandUsage::OPERATION_OUTPUT); - } - if (usage != model::operand::OperandUsage::OPERATION_OUTPUT) - { - showing_cond = showing_cond && (object.getUses().size() > 0); - } - if (showing_cond) - { - auto type = [&]() { - using neurun::dumper::dot::DotOperandInfo; - if (_graph.getInputs().contains(index)) - return DotOperandInfo::Type::MODEL_INPUT; - if (_graph.getOutputs().contains(index)) - return DotOperandInfo::Type::MODEL_OUTPUT; - return DotOperandInfo::Type::INTERNAL; - }(); - - neurun::dumper::dot::DotOperandInfo operand_info(index, object, type); - - for (auto operation_index : object.getUses().list()) - { - auto &node = operations.at(operation_index); - auto child = - std::make_shared<neurun::dumper::dot::DotNodeInfo>(_graph, operation_index, node); - operand_info.appendChild(child); - } - - dot_builder.update(operand_info); - } - }); - - // Dump to file - { - std::string file_name; - file_name += tag; - file_name += ".dot"; - std::filebuf fb; - - fb.open(file_name, std::ios::out); - std::ostream os(&fb); - - dot_builder.writeDot(os); - - fb.close(); - } -} - -} // namespace dot -} // namespace dumper -} // namespace neurun diff --git a/runtimes/neurun/src/dumper/dot/DotDumper.h b/runtimes/neurun/src/dumper/dot/DotDumper.h deleted file mode 100644 index 0c0a9b8df..000000000 --- a/runtimes/neurun/src/dumper/dot/DotDumper.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 "graph/Graph.h" -#include "util/config/ConfigManager.h" - -#ifndef __NEURUN_DUMPER_DOT_DOT_DUMPER_H__ -#define __NEURUN_DUMPER_DOT_DOT_DUMPER_H__ - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -enum OPTIONS -{ - DUMP_OFF = 0, // Don't dump - DEFAULT = 1, // Show default dot graph - SHOW_CONSTANTS // Show dot graph with input constants -}; - -class DotDumper -{ -public: - DotDumper(const neurun::graph::Graph &graph) : _graph(graph) - { - _option = config::ConfigManager::instance().get<int>("GRAPH_DOT_DUMP"); - } - -public: - /** - * @brief Dump to dot file as tag name if "GRAPH_DOT_DUMP" is set - * - * @param[in] tag The name of dot file that would be created - * @return N/A - */ - void dumpIfNeeded(const std::string &tag); - -private: - const neurun::graph::Graph &_graph; - uint32_t _option; -}; - -} // namespace dot -} // namespace dumper -} // namespace neurun - -#endif // __NEURUN_DUMPER_DOT_DOT_DUMPER_H__ diff --git a/runtimes/neurun/src/dumper/dot/DotNodeInfo.cc b/runtimes/neurun/src/dumper/dot/DotNodeInfo.cc deleted file mode 100644 index aefe12e2a..000000000 --- a/runtimes/neurun/src/dumper/dot/DotNodeInfo.cc +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 <sstream> - -#include "DotNodeInfo.h" -#include "graph/Graph.h" -#include "graph/operation/LowerInfo.h" -#include "backend/interface/IConfig.h" - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -const std::string DotNodeInfo::NODE_SHAPE = "rect"; -const std::string DotNodeInfo::BG_COLOR_SCHEME = "pastel18"; -// RED BLUE ORANGE YELLOW GREEN PUPLE CYAN PINK -const std::string DotNodeInfo::BG_COLORS[8] = {"1", "2", "5", "6", "3", "4", "7", "8"}; - -DotNodeInfo::DotNodeInfo(const neurun::graph::Graph &graph, - const neurun::model::operation::Index &index, - const neurun::model::operation::Node &node) - : _index(index), _node(node), _lower_info(graph.getLowerInfo(index)) -{ - addBackendLabel(); -} - -std::string DotNodeInfo::index_str() const -{ - std::stringstream ss; - ss << "node" << _index.value(); - - return ss.str(); -} - -std::string DotNodeInfo::label() const -{ - std::stringstream ss; - ss << _index.value() << " : " << _node.getName() << std::endl; - for (auto label : _labels) - { - ss << label << std::endl; - } - - return ss.str(); -} - -std::string DotNodeInfo::dot_shape() const { return NODE_SHAPE; } - -std::string DotNodeInfo::bg_color_scheme() const { return BG_COLOR_SCHEME; } - -std::string DotNodeInfo::bg_color() const -{ - if (!_lower_info) - return DEFAULT_BG_COLOR; - assert(_lower_info != nullptr); - const auto &backend = _lower_info->backend(); - assert(backend != nullptr); - - std::string backend_id = backend->config()->id(); - // TODO : This is just workaround it can be made more efficient. - if (backend_id == "acl_cl") - { - return BG_COLORS[RED]; - } - else if (backend_id == "cpu") - { - return BG_COLORS[BLUE]; - } - else - { - return DEFAULT_BG_COLOR; - } -} - -void DotNodeInfo::addBackendLabel() -{ - if (!_lower_info) - return; - - std::string label; - const auto &backend = _lower_info->backend(); - assert(backend != nullptr); - - label += "[Backend] : "; - label += backend->config()->id(); - _labels.emplace_back(label); -} - -} // namespace dot -} // namespace dumper -} // namespace neurun diff --git a/runtimes/neurun/src/dumper/dot/DotNodeInfo.h b/runtimes/neurun/src/dumper/dot/DotNodeInfo.h deleted file mode 100644 index 656a05af6..000000000 --- a/runtimes/neurun/src/dumper/dot/DotNodeInfo.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_DUMPER_DOT_DOT_NODE_INFO_H__ -#define __NEURUN_DUMPER_DOT_DOT_NODE_INFO_H__ - -#include "IDotInfo.h" -#include "model/operation/Node.h" -#include "model/operation/Index.h" - -namespace neurun -{ -namespace graph -{ -class Graph; -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -class DotNodeInfo : public IDotInfo -{ -public: - static const std::string NODE_SHAPE; - static const std::string BG_COLOR_SCHEME; - static const std::string BG_COLORS[8]; - -public: - DotNodeInfo(const neurun::graph::Graph &graph, const neurun::model::operation::Index &index, - const neurun::model::operation::Node &node); - -public: - virtual std::string index_str() const override; - virtual std::string label() const override; - virtual std::string dot_shape() const override; - virtual std::string bg_color_scheme() const override; - virtual std::string bg_color() const override; - -private: - void addBackendLabel(); - -private: - neurun::model::operation::Index _index; - const neurun::model::operation::Node &_node; - const neurun::graph::operation::LowerInfo *_lower_info; - std::vector<std::string> _labels; -}; - -} // namespace dot -} // namespace dumper -} // namespace neurun - -#endif // __NEURUN_DUMPER_DOT_DOT_NODE_INFO_H__ diff --git a/runtimes/neurun/src/dumper/dot/DotOperandInfo.cc b/runtimes/neurun/src/dumper/dot/DotOperandInfo.cc deleted file mode 100644 index 8f5905020..000000000 --- a/runtimes/neurun/src/dumper/dot/DotOperandInfo.cc +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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 <sstream> - -#include "DotOperandInfo.h" -#include "graph/operand/LowerInfo.h" -#include "backend/interface/IConfig.h" -#include "backend/BackendManager.h" - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -const std::string DotOperandInfo::INPUT_SHAPE = "doublecircle"; -const std::string DotOperandInfo::OUTPUT_SHAPE = "doublecircle"; -const std::string DotOperandInfo::OPERAND_SHAPE = "ellipse"; -const std::string DotOperandInfo::BG_COLOR_SCHEME = "set38"; -// RED BLUE ORANGE YELLOW GREEN PUPLE CYAN PINK -const std::string DotOperandInfo::BG_COLORS[8] = {"4", "5", "6", "2", "7", "3", "1", "8"}; - -DotOperandInfo::DotOperandInfo(const neurun::model::operand::Index &index, - const neurun::model::operand::Object &object, Type type) - : _index(index), _object(object), _type(type) -{ - const auto &lower_info = object.lower_info(); - if (lower_info) - { - addBackendLabel(); - } -} - -std::string DotOperandInfo::index_str() const -{ - std::stringstream ss; - ss << "obj" << _index.value(); - - return ss.str(); -} - -std::string DotOperandInfo::label() const -{ - std::stringstream ss; - ss << _index.value() << std::endl; - for (auto label : _labels) - { - ss << label << std::endl; - } - - return ss.str(); -} - -std::string DotOperandInfo::dot_shape() const -{ - switch (_type) - { - case Type::MODEL_INPUT: - return INPUT_SHAPE; - - case Type::MODEL_OUTPUT: - return OUTPUT_SHAPE; - - case Type::UNDEFINED: - case Type::INTERNAL: - default: - return OPERAND_SHAPE; - } -} - -std::string DotOperandInfo::bg_color_scheme() const { return BG_COLOR_SCHEME; } - -std::string DotOperandInfo::bg_color() const -{ - const auto &lower_info = _object.lower_info(); - if (!lower_info) - return DEFAULT_BG_COLOR; - assert(lower_info != nullptr); - const auto &def_backends = lower_info->def_backends(); - assert(def_backends.size() == 1); - - std::string backend_id = def_backends.getOnlyElement()->config()->id(); - // TODO : This is just workaround it can be made more efficient. - if (backend_id == "acl_cl") - { - return BG_COLORS[RED]; - } - else if (backend_id == "cpu") - { - return BG_COLORS[BLUE]; - } - else - { - return DEFAULT_BG_COLOR; - } -} - -void DotOperandInfo::addBackendLabel() -{ - std::string label; - const auto &lower_info = _object.lower_info(); - assert(lower_info != nullptr); - const auto &def_backends = lower_info->def_backends(); - assert(def_backends.size() == 1); - - label += "["; - label += def_backends.getOnlyElement()->config()->id(); - label += "]"; - _labels.emplace_back(label); -} - -} // namespace dot -} // namespace dumper -} // namespace neurun diff --git a/runtimes/neurun/src/dumper/dot/DotOperandInfo.h b/runtimes/neurun/src/dumper/dot/DotOperandInfo.h deleted file mode 100644 index c54da444d..000000000 --- a/runtimes/neurun/src/dumper/dot/DotOperandInfo.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_DUMPER_DOT_DOT_OPERAND_INFO_H__ -#define __NEURUN_DUMPER_DOT_DOT_OPERAND_INFO_H__ - -#include <vector> - -#include "IDotInfo.h" -#include "model/operand/Object.h" -#include "model/operand/Index.h" - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -class DotOperandInfo : public IDotInfo -{ -public: - enum class Type - { - UNDEFINED, - MODEL_INPUT, - MODEL_OUTPUT, - INTERNAL - }; - -public: - static const std::string INPUT_SHAPE; - static const std::string OUTPUT_SHAPE; - static const std::string OPERAND_SHAPE; - static const std::string BG_COLOR_SCHEME; - static const std::string BG_COLORS[8]; - -public: - DotOperandInfo(const neurun::model::operand::Index &index, - const neurun::model::operand::Object &object, Type type); - -public: - virtual std::string index_str() const override; - virtual std::string label() const override; - virtual std::string dot_shape() const override; - virtual std::string bg_color_scheme() const override; - virtual std::string bg_color() const override; - -private: - void addBackendLabel(); - -private: - const neurun::model::operand::Index &_index; - const neurun::model::operand::Object &_object; - Type _type; - - std::vector<std::string> _labels; -}; - -} // namespace dot -} // namespace dumper -} // namespace neurun - -#endif // __NEURUN_DUMPER_DOT_DOT_OPERAND_INFO_H__ diff --git a/runtimes/neurun/src/dumper/dot/IDotInfo.h b/runtimes/neurun/src/dumper/dot/IDotInfo.h deleted file mode 100644 index d507e724a..000000000 --- a/runtimes/neurun/src/dumper/dot/IDotInfo.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_DUMPER_DOT_IDOTINFO_H__ -#define __NEURUN_DUMPER_DOT_IDOTINFO_H__ - -#include <string> -#include <memory> -#include <vector> - -namespace neurun -{ -namespace dumper -{ -namespace dot -{ - -#define DEFAULT_BG_COLOR_SCHEME "x11" -#define DEFAULT_BG_COLOR "white" - -enum BGCOLORS : int -{ - RED, - BLUE, - ORANGE, - YELLOW, - GREEN, - PUPLE, - CYAN, - PINK -}; - -struct IDotInfo -{ - virtual ~IDotInfo() = default; - - virtual std::string index_str() const = 0; - virtual std::string label() const = 0; - virtual std::string dot_shape() const = 0; - virtual std::string bg_color_scheme() const { return DEFAULT_BG_COLOR_SCHEME; } - virtual std::string bg_color() const { return DEFAULT_BG_COLOR; } - - void appendChild(std::shared_ptr<IDotInfo> dotinfo) { _children.emplace_back(dotinfo); } - const std::vector<std::shared_ptr<IDotInfo>> &children() const { return _children; } - -private: - std::vector<std::shared_ptr<IDotInfo>> _children; -}; - -} // namespace dot -} // namespace dumper -} // namespace neurun - -#endif // __NEURUN_DUMPER_DOT_IDOTINFO_H__ diff --git a/runtimes/neurun/src/exec/Sink.h b/runtimes/neurun/src/exec/Sink.h deleted file mode 100644 index fe23e8ac3..000000000 --- a/runtimes/neurun/src/exec/Sink.h +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_EXEC_SINK_H__ -#define __NEURUN_EXEC_SINK_H__ - -#include <cassert> - -#include "cpp14/memory.h" -#include "util/feature/nhwc/View.h" -#include "util/feature/nchw/View.h" -#include <misc/feature/IndexIterator.h> - -namespace neurun -{ -namespace exec -{ - -struct ISink -{ - virtual ~ISink() = default; - - virtual void pull(::neurun::backend::operand::ITensor &tensor) const = 0; -}; - -template <typename T> class Sink final : public ISink -{ -public: - Sink(T *base, const size_t size) : _base{base}, _size{size} {} - -public: - void pull(::neurun::backend::operand::ITensor &tensor) const override - { - memcpy(_base, tensor.buffer(), _size); - } - -private: - T *const _base; - const size_t _size; -}; - -class PermutateSink final : public ISink -{ -public: - PermutateSink(void *output_buffer, const size_t &output_size, const model::operand::Shape &shape) - : _output_buffer{(uint8_t *)output_buffer}, _output_size{output_size}, _shape{shape} - { - } - -public: - void pull(neurun::backend::operand::ITensor &tensor) const override - { - // do NCHW_TO_NHWC permutation - auto input_buffer = tensor.buffer(); - auto rank = _shape.rank(); - - switch (rank) - { - case 0: - case 1: - { - memcpy(_output_buffer, input_buffer, _output_size); - break; - } - case 2: - { - auto matrix_shape = _shape.asMatrix(); - - for (auto h = 0; h < matrix_shape.H; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, 0}; - memcpy(_output_buffer + h * matrix_shape.W, input_buffer + tensor.calcOffset(coord), - matrix_shape.W * sizeof(float)); - } - break; - } - case 3: - { - const int32_t depth = _shape.dim(0); - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); - - for (auto c = 0; c < depth; ++c) - { - for (auto h = 0; h < height; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, c}; - memcpy(_output_buffer + c * height * width + h * width, - input_buffer + tensor.calcOffset(coord), width * sizeof(float)); - } - } - break; - } - case 4: - { - auto feature = _shape.asFeature(); - - const util::feature::nchw::View<float> from{&tensor}; - util::feature::nhwc::View<float> into{feature, reinterpret_cast<float *>(_output_buffer), - _output_size}; - - ::nnfw::misc::feature::iterate(feature) - << [&](uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(batch, ch, row, col); - into.at(batch, ch, row, col) = value; - }; - break; - } - default: - throw "NYI"; - break; - } - } - -private: - uint8_t *_output_buffer; - const size_t _output_size; - const model::operand::Shape _shape; -}; - -} // namespace exec -} // namespace neurun - -#endif // __NEURUN_EXEC_SINK_H__ diff --git a/runtimes/neurun/src/exec/Source.h b/runtimes/neurun/src/exec/Source.h deleted file mode 100644 index 169f8b386..000000000 --- a/runtimes/neurun/src/exec/Source.h +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_EXEC_SOURCE_H__ -#define __NEURUN_EXEC_SOURCE_H__ - -#include <cassert> - -#include "cpp14/memory.h" -#include "util/feature/nchw/View.h" -#include "util/feature/nhwc/Reader.h" -#include "util/feature/Coordinate4D.h" -#include <misc/feature/IndexIterator.h> - -namespace neurun -{ -namespace exec -{ - -struct ISource -{ - virtual ~ISource() = default; - - virtual void push(::neurun::backend::operand::ITensor &tensor) const = 0; -}; - -template <typename T> class Source final : public ISource -{ -public: - Source(const T *base, const size_t size) : _base{base}, _size{size} {} - -public: - void push(::neurun::backend::operand::ITensor &tensor) const override - { - memcpy(tensor.buffer(), _base, _size); - } - -private: - const T *const _base; - const size_t _size; -}; - -class PermutateSource final : public ISource -{ -public: - PermutateSource(const void *input_buffer, const size_t &input_size, - const model::operand::Shape &shape) - : _input_buffer{(uint8_t *)input_buffer}, _input_size{input_size}, _shape{shape} - { - } - -public: - void push(neurun::backend::operand::ITensor &tensor) const override - { - // do NHWC_TO_NCHW permutation - auto output_buffer = tensor.buffer(); - auto rank = _shape.rank(); - - switch (rank) - { - case 0: - case 1: - { - memcpy(output_buffer, _input_buffer, _input_size); - break; - } - case 2: - { - auto matrix_shape = _shape.asMatrix(); - - for (auto h = 0; h < matrix_shape.H; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, 0}; - memcpy(output_buffer + tensor.calcOffset(coord), _input_buffer + h * matrix_shape.W, - matrix_shape.W * sizeof(float)); - } - break; - } - case 3: - { - const int32_t depth = _shape.dim(0); - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); - - for (auto c = 0; c < depth; ++c) - { - for (auto h = 0; h < height; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, c}; - memcpy(output_buffer + tensor.calcOffset(coord), - _input_buffer + c * height * width + h * width, width * sizeof(float)); - } - } - break; - } - case 4: - { - auto feature = _shape.asFeature(); - - const util::feature::nhwc::Reader<float> from{ - feature, reinterpret_cast<const float *>(_input_buffer), _input_size}; - util::feature::nchw::View<float> into{&tensor}; - - ::nnfw::misc::feature::iterate(feature) - << [&](uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(batch, ch, row, col); - into.at(batch, ch, row, col) = value; - }; - break; - } - default: - throw "NYI"; - break; - } - } - -private: - const uint8_t *_input_buffer; - const size_t _input_size; - const model::operand::Shape _shape; -}; - -} // namespace exec -} // namespace neurun - -#endif // __NEURUN_EXEC_SOURCE_H__ diff --git a/runtimes/neurun/src/exec/interface/IFunction.h b/runtimes/neurun/src/exec/interface/IFunction.h deleted file mode 100644 index b7a721d1d..000000000 --- a/runtimes/neurun/src/exec/interface/IFunction.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_EXEC_I_FUNCTION_H__ -#define __NEURUN_EXEC_I_FUNCTION_H__ - -namespace neurun -{ -namespace exec -{ - -class IFunction -{ -public: - virtual ~IFunction() = default; - virtual void run() = 0; - virtual void prepare() {} -}; - -} // namespace exec -} // namespace neurun - -#endif // __NEURUN_EXEC_I_FUNCTION_H__ diff --git a/runtimes/neurun/src/frontend/compilation.cc b/runtimes/neurun/src/frontend/compilation.cc deleted file mode 100644 index 9b0719f46..000000000 --- a/runtimes/neurun/src/frontend/compilation.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 <NeuralNetworks.h> - -#include <new> - -#include "frontend/wrapper/model.h" -#include "frontend/wrapper/compilation.h" - -// -// NNAPI Implementation -// -int ANeuralNetworksCompilation_create(ANeuralNetworksModel *model, - ANeuralNetworksCompilation **compilation) -{ - if ((model == nullptr) || (compilation == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - if (!model->isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - std::shared_ptr<neurun::graph::Graph> internal; - - model->release(internal); - - *compilation = new (std::nothrow) ANeuralNetworksCompilation(internal); - if (*compilation == nullptr) - { - return ANEURALNETWORKS_OUT_OF_MEMORY; - } - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation *compilation) -{ - if (compilation == nullptr) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - return compilation->finish(); -} - -void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation *compilation) -{ - delete compilation; -} - -int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation *compilation, - int32_t /* preference */) -{ - if (compilation == nullptr) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // NYi - return ANEURALNETWORKS_NO_ERROR; -} diff --git a/runtimes/neurun/src/frontend/event.cc b/runtimes/neurun/src/frontend/event.cc deleted file mode 100644 index cd47cc691..000000000 --- a/runtimes/neurun/src/frontend/event.cc +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 <NeuralNetworks.h> - -#include "frontend/wrapper/event.h" - -int ANeuralNetworksEvent_wait(ANeuralNetworksEvent *event) -{ - if (event == nullptr) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - return ANEURALNETWORKS_NO_ERROR; -} - -void ANeuralNetworksEvent_free(ANeuralNetworksEvent *event) { delete event; } diff --git a/runtimes/neurun/src/frontend/execution.cc b/runtimes/neurun/src/frontend/execution.cc deleted file mode 100644 index 5f1729b30..000000000 --- a/runtimes/neurun/src/frontend/execution.cc +++ /dev/null @@ -1,328 +0,0 @@ -/* - * 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 <NeuralNetworks.h> - -#include <new> - -#include "frontend/wrapper/compilation.h" -#include "frontend/wrapper/execution.h" -#include "frontend/wrapper/event.h" - -#include "model/operand/DataType.h" -#include "model/operand/Index.h" -#include "graph/operand/Layout.h" -#include "backend/BackendManager.h" -#include "backend/interface/IConfig.h" -#include "compiler/BackendResolver.h" -#include "compiler/TensorInfo.h" -#include "backend/interface/operand/ITensor.h" - -inline void source(ANeuralNetworksExecution *execution, - const ::neurun::model::operand::DataType &type, int32_t index, - const void *buffer, size_t length) -{ - const auto &operands = execution->plan().model().operands(); - neurun::model::operand::IO::Index input_index{index}; - - const auto operand_index = execution->plan().model().getInputs().at(input_index); - auto operand = &operands.at(operand_index); - auto operand_li = operand->lower_info(); - const auto output_backend = operand_li->def_backends().getOnlyElement(); - const auto output_layout = output_backend->config()->getOperandLayout(); - auto input_layout = execution->plan() - .model() - .backend_resolver() - ->getDefaultBackend() - ->config() - ->getOperandLayout(); - if (input_layout == neurun::graph::operand::Layout::NHWC && - output_layout == neurun::graph::operand::Layout::NCHW) - { - const auto tensor_info = neurun::compiler::TensorInfo(operand->shape(), operand->typeInfo()); - - execution->source<::neurun::exec::PermutateSource>(index, buffer, tensor_info.total_size(), - operand->shape()); - return; - } - using ::neurun::model::operand::DataType; - switch (type) - { - case DataType::SCALAR_FLOAT32: - case DataType::TENSOR_FLOAT32: - execution->source<::neurun::exec::Source<float>>( - index, reinterpret_cast<const float *>(buffer), length); - break; - case DataType::SCALAR_INT32: - case DataType::TENSOR_INT32: - execution->source<::neurun::exec::Source<int32_t>>( - index, reinterpret_cast<const int32_t *>(buffer), length); - break; - case DataType::SCALAR_UINT32: - execution->source<::neurun::exec::Source<uint32_t>>( - index, reinterpret_cast<const uint32_t *>(buffer), length); - break; - case DataType::TENSOR_QUANT8_ASYMM: - execution->source<::neurun::exec::Source<uint8_t>>( - index, reinterpret_cast<const uint8_t *>(buffer), length); - break; - default: - throw std::runtime_error("Not supported, yet"); - break; - } -} - -inline void sink(ANeuralNetworksExecution *execution, - const ::neurun::model::operand::DataType &type, int32_t index, void *buffer, - size_t length) -{ - const auto &operands = execution->plan().model().operands(); - neurun::model::operand::IO::Index input_index{index}; - - const auto operand_index = execution->plan().model().getOutputs().at(input_index); - auto operand = &operands.at(operand_index); - auto operand_li = operand->lower_info(); - const auto input_backend = operand_li->def_backends().getOnlyElement(); - const auto input_layout = input_backend->config()->getOperandLayout(); - auto output_layout = execution->plan() - .model() - .backend_resolver() - ->getDefaultBackend() - ->config() - ->getOperandLayout(); - if (input_layout == neurun::graph::operand::Layout::NCHW && - output_layout == neurun::graph::operand::Layout::NHWC) - { - const auto tensor_info = neurun::compiler::TensorInfo(operand->shape(), operand->typeInfo()); - - execution->sink<::neurun::exec::PermutateSink>(index, buffer, tensor_info.total_size(), - operand->shape()); - return; - } - using ::neurun::model::operand::DataType; - switch (type) - { - case DataType::SCALAR_FLOAT32: - case DataType::TENSOR_FLOAT32: - execution->sink<::neurun::exec::Sink<float>>(index, reinterpret_cast<float *>(buffer), - length); - break; - case DataType::SCALAR_INT32: - case DataType::TENSOR_INT32: - execution->sink<::neurun::exec::Sink<int32_t>>(index, reinterpret_cast<int32_t *>(buffer), - length); - break; - case DataType::SCALAR_UINT32: - execution->sink<::neurun::exec::Sink<uint32_t>>(index, reinterpret_cast<uint32_t *>(buffer), - length); - break; - case DataType::TENSOR_QUANT8_ASYMM: - execution->sink<::neurun::exec::Sink<uint8_t>>(index, reinterpret_cast<uint8_t *>(buffer), - length); - break; - default: - throw std::runtime_error("Not supported, yet"); - break; - } -} - -// -// NNAPI Implementation -// -int ANeuralNetworksExecution_create(ANeuralNetworksCompilation *compilation, - ANeuralNetworksExecution **execution) -{ - if ((compilation == nullptr) || (execution == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // Can handle compiled state only - if (compilation->plan().state() != neurun::compiler::State::COMPILED) - { - return ANEURALNETWORKS_BAD_STATE; - } - - std::shared_ptr<const neurun::compiler::Plan> plan; - - compilation->publish(plan); - - *execution = new (std::nothrow) ANeuralNetworksExecution{plan}; - if (*execution == nullptr) - { - return ANEURALNETWORKS_OUT_OF_MEMORY; - } - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution *execution, int32_t index, - const ANeuralNetworksOperandType * /* type */, - const void *buffer, size_t length) -{ - // Don't check type - // Comment about ANeuralNetworksOperandType in NeuralNetworks.h: - // If the input or output is optional and omitted then it need not have a fully specified tensor - // operand type - if ((execution == nullptr) || ((buffer == nullptr) && (length != 0))) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // TODO Handle optional input - if (buffer == nullptr) - { - throw std::runtime_error("Not supported optional input, yet"); - } - - const auto &operands = execution->plan().model().operands(); - - // TODO Check type conflicts - - neurun::model::operand::IO::Index input_index{index}; - - const auto operand_index = execution->plan().model().getInputs().at(input_index); - const auto data_type = operands.at(operand_index).typeInfo().type(); - const auto operand_shape = operands.at(operand_index).shape(); - - source(execution, data_type, index, buffer, length); - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution *execution, int32_t index, - const ANeuralNetworksOperandType * /* type */, void *buffer, - size_t length) -{ - // Don't check type - // Comment about ANeuralNetworksOperandType in NeuralNetworks.h: - // If the input or output is optional and omitted then it need not have a fully specified tensor - // operand type - if ((execution == nullptr) || ((buffer == nullptr) && (length != 0))) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // Handle optional output - if (buffer == nullptr) - { - return ANEURALNETWORKS_NO_ERROR; - } - - const auto &operands = execution->plan().model().operands(); - - // TODO Check type conflicts - - neurun::model::operand::IO::Index output_index{index}; - - const auto operand_index = execution->plan().model().getOutputs().at(output_index); - const auto data_type = operands.at(operand_index).typeInfo().type(); - const auto operand_shape = operands.at(operand_index).shape(); - - sink(execution, data_type, index, buffer, length); - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, - ANeuralNetworksEvent **event) -{ - if ((execution == nullptr) || (event == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // TODO: Handle event - *event = new (std::nothrow) ANeuralNetworksEvent{}; - if (*event == nullptr) - { - return ANEURALNETWORKS_OUT_OF_MEMORY; - } - - const auto &plan = execution->plan(); - const auto &model = plan.model(); - - // Set input(s) - for (uint32_t n = 0; n < model.getInputs().size(); ++n) - { - auto setter = [&](::neurun::backend::operand::ITensor &tensor) { - execution->source(n).push(tensor); - }; - - neurun::model::operand::IO::Index input_index{n}; - - ::neurun::model::operand::Index index{model.getInputs().at(input_index)}; - auto object = plan.operands().at(index); - - object->access(setter); - } - - const auto &operations = execution->plan().operations(); - - for (uint32_t n = 0; n < operations.size(); ++n) - { - operations.at(n).run(); - } - - // Get output(s) - for (uint32_t n = 0; n < model.getOutputs().size(); ++n) - { - auto getter = [&](::neurun::backend::operand::ITensor &tensor) { - execution->sink(n).pull(tensor); - }; - - neurun::model::operand::IO::Index output_index{n}; - - ::neurun::model::operand::Index index{model.getOutputs().at(output_index)}; - auto object = plan.operands().at(index); - - object->access(getter); - } - - return ANEURALNETWORKS_NO_ERROR; -} - -void ANeuralNetworksExecution_free(ANeuralNetworksExecution * /* execution */) {} - -int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution *execution, - int32_t /* index */, - const ANeuralNetworksOperandType * /* type */, - const ANeuralNetworksMemory *memory, - size_t /* offset */, size_t /* length */) -{ - if ((execution == nullptr) || (memory == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // NYI - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution *execution, - int32_t /* index */, - const ANeuralNetworksOperandType * /* type */, - const ANeuralNetworksMemory *memory, - size_t /* offset */, size_t /* length */) -{ - if ((execution == nullptr) || (memory == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // NYI - return ANEURALNETWORKS_NO_ERROR; -} diff --git a/runtimes/neurun/src/frontend/memory.cc b/runtimes/neurun/src/frontend/memory.cc deleted file mode 100644 index b2f6ab2d0..000000000 --- a/runtimes/neurun/src/frontend/memory.cc +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 <NeuralNetworks.h> -#include <sys/mman.h> -#include <new> -#include <memory> - -#include "cpp14/memory.h" -#include "frontend/wrapper/memory.h" - -int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t offset, - ANeuralNetworksMemory **memory) -{ - if (memory == nullptr) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - // Use unique pointer to avoid memory leak - std::unique_ptr<ANeuralNetworksMemory> memory_ptr = - nnfw::cpp14::make_unique<ANeuralNetworksMemory>(size, protect, fd, offset); - if (memory_ptr == nullptr) - { - return ANEURALNETWORKS_OUT_OF_MEMORY; - } - *memory = memory_ptr.release(); - - return ANEURALNETWORKS_NO_ERROR; -} - -void ANeuralNetworksMemory_free(ANeuralNetworksMemory *memory) { delete memory; } diff --git a/runtimes/neurun/src/frontend/model.cc b/runtimes/neurun/src/frontend/model.cc deleted file mode 100644 index 3aa2aa2ff..000000000 --- a/runtimes/neurun/src/frontend/model.cc +++ /dev/null @@ -1,480 +0,0 @@ -/* - * 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 <NeuralNetworks.h> -#include <NeuralNetworksEx.h> - -#include <cassert> -#include <stdexcept> -#include <new> - -#include "cpp14/memory.h" - -#include "graph/Graph.h" -#include "frontend/wrapper/model.h" -#include "frontend/wrapper/memory.h" -#include "model/operation/Node.Include.h" - -int ANeuralNetworksModel_create(ANeuralNetworksModel **model) -{ - if (model == nullptr) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - *model = new (std::nothrow) ANeuralNetworksModel{}; - if (*model == nullptr) - { - return ANEURALNETWORKS_OUT_OF_MEMORY; - } - - return ANEURALNETWORKS_NO_ERROR; -} - -void ANeuralNetworksModel_free(ANeuralNetworksModel *model) { delete model; } - -int ANeuralNetworksModel_addOperand(ANeuralNetworksModel *model, - const ANeuralNetworksOperandType *type) -{ - if ((model == nullptr) || (type == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - if (model->isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - // scale and zeroPoint should be zero for scalars and non-fixed point tensors - // Quantized: - // scale: a 32 bit floating point value greater than zero - // zeroPoint: a 32 bit integer, in range [0, 255] - if (type->type == ANEURALNETWORKS_TENSOR_QUANT8_ASYMM) - { - if (!(type->scale > 0.0f)) - { - return ANEURALNETWORKS_BAD_DATA; - } - - if ((type->zeroPoint < 0) || (type->zeroPoint > 255)) - { - return ANEURALNETWORKS_BAD_DATA; - } - } - else if ((type->scale != 0.0f) || (type->zeroPoint != 0)) - { - return ANEURALNETWORKS_BAD_DATA; - } - - // dimensionCount should be zero for scalars - if ((type->dimensionCount != 0) && - ((type->type == ANEURALNETWORKS_FLOAT32) || (type->type == ANEURALNETWORKS_INT32) || - (type->type == ANEURALNETWORKS_UINT32))) - { - return ANEURALNETWORKS_BAD_DATA; - } - - ::neurun::model::operand::Shape shape(type->dimensionCount); - ::neurun::model::operand::TypeInfo typeInfo((OperandCode)(type->type), type->scale, - type->zeroPoint); - - for (uint32_t axis = 0; axis < type->dimensionCount; ++axis) - { - shape.dim(axis) = type->dimensions[axis]; - } - - model->deref().addOperand(shape, typeInfo); - - // 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 - // a convolution kernel. - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel *model, int32_t index, - const void *buffer, size_t length) -{ - const bool isOptional = ((buffer == nullptr) && (length == 0)); - - if ((model == nullptr) || ((buffer == nullptr) && (length != 0))) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - if (model->isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - // Negative index value is not allowed - if (index < 0) - { - return ANEURALNETWORKS_BAD_DATA; - } - const neurun::model::operand::Index ind{static_cast<uint32_t>(index)}; - - if (!model->deref().operands().exist(ind)) - { - return ANEURALNETWORKS_BAD_DATA; - } - - auto &obj = model->deref().operands().at(ind); - if ((obj.operandSize() != length) && !isOptional) - { - return ANEURALNETWORKS_BAD_DATA; - } - if (!obj.setAsConstant()) - { - return ANEURALNETWORKS_BAD_DATA; - } - - using ::neurun::model::operand::CachedData; - using ::neurun::model::operand::ExternalData; - - // Remain operands.at(ind).data()->base() as nullptr for optional operand - // This will be filled when model finished - if (isOptional) - { - model->setOptionalOperand(ind); - } - - // NNAPI spec in NeuralNetworks.h - // For values of length greater than ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES, - // the application is responsible for not changing the content of this region - // until all executions using this model have completed - if (length <= ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES) - { - model->deref().setOperandValue(ind, nnfw::cpp14::make_unique<CachedData>( - reinterpret_cast<const uint8_t *>(buffer), length)); - } - else - { - model->deref().setOperandValue(ind, nnfw::cpp14::make_unique<ExternalData>( - reinterpret_cast<const uint8_t *>(buffer), length)); - } - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel *model, int32_t index, - const ANeuralNetworksMemory *memory, - size_t offset, size_t length) -{ - if ((model == nullptr) || (memory == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - if (model->isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - // Negative index value is not allowed - if (index < 0) - { - return ANEURALNETWORKS_BAD_DATA; - } - const neurun::model::operand::Index ind{static_cast<uint32_t>(index)}; - - if (!model->deref().operands().exist(ind)) - { - return ANEURALNETWORKS_BAD_DATA; - } - - auto &obj = model->deref().operands().at(ind); - if ((obj.operandSize() != length) || (memory->size() < (offset + length))) - { - return ANEURALNETWORKS_BAD_DATA; - } - if (!obj.setAsConstant()) - { - return ANEURALNETWORKS_BAD_DATA; - } - - using ::neurun::model::operand::ExternalData; - - model->deref().setOperandValue( - ind, nnfw::cpp14::make_unique<ExternalData>( - reinterpret_cast<const uint8_t *>(memory->base() + offset), length)); - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model, - ANeuralNetworksOperationType type, uint32_t inputCount, - const uint32_t *inputs, uint32_t outputCount, - const uint32_t *outputs) -{ - if ((model == nullptr) || (inputs == nullptr) || (outputs == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - 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.setAsOperationOutput()) - { - return ANEURALNETWORKS_BAD_DATA; - } - } - - auto &graph = model->deref(); - - auto node_param = - neurun::model::operation::Node::InitParam{inputCount, inputs, outputCount, outputs}; - - try - { - switch (type) - { - case ANEURALNETWORKS_CONV_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); - - if (inputCount == 7) - { - using GraphNode = neurun::model::operation::Conv2DNode; - - graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param)); - } - else - { - throw std::runtime_error{"Explicit padding in Conv2D is not supported, yet"}; - } - - 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); - - if (inputCount == 7) - { - using GraphNode = neurun::model::operation::MaxPool2DNode; - - graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param)); - } - else - { - throw std::runtime_error{"Explicit padding in MaxPool2D is not supported, yet"}; - } - - 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); - - if (inputCount == 7) - { - using GraphNode = neurun::model::operation::AvgPool2DNode; - - graph.addOperation(nnfw::cpp14::make_unique<GraphNode>(node_param)); - } - else - { - throw std::runtime_error{"Explicit padding in AvgPool2D is not supported, yet"}; - } - - 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; - } - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model, - ANeuralNetworksOperationTypeEx type, uint32_t inputCount, - const uint32_t *inputs, uint32_t outputCount, - const uint32_t *outputs) -{ - if ((model == nullptr) || (inputs == nullptr) || (outputs == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - if (model->isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - const ANeuralNetworksOperationTypeEx FIRST_OPERATION = ANEURALNETWORKS_GATHER_EX; - const ANeuralNetworksOperationTypeEx LAST_OPERATION = ANEURALNETWORKS_PRELU_EX; - if ((type < FIRST_OPERATION) || (type > LAST_OPERATION)) - { - 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.setAsOperationOutput()) - { - return ANEURALNETWORKS_BAD_DATA; - } - } - - // Workaround: to avoid compile error by unused-parameter, use inputCount - if (inputCount == 0) - { - 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; -} - -int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, uint32_t inputCount, - const uint32_t *inputs, uint32_t outputCount, - const uint32_t *outputs) -{ - if ((model == nullptr) || (inputs == nullptr) || (outputs == nullptr)) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - if (model->isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - // NOTE ::neurun::model::operand::Index uses int as its underlying type as various NNAPI - // functions such as ANeuralNetworksModel_setOperandValue use int to represent operand index - // - // ANeuralNetworksModel_identifyInputsAndOutputs, however, uses uint32_t to represent operand - // index. - // - // Below, static_cast<int>(...) is introduced to eliminate compiler warning. - for (uint32_t n = 0; n < inputCount; ++n) - { - const neurun::model::operand::Index ind{static_cast<uint32_t>(inputs[n])}; - model->deref().addInput(ind); - - auto &obj = model->deref().operands().at(ind); - if (!obj.setAsModelInput()) - { - return ANEURALNETWORKS_BAD_DATA; - } - } - - for (uint32_t n = 0; n < outputCount; ++n) - { - const neurun::model::operand::Index ind{static_cast<uint32_t>(outputs[n])}; - model->deref().addOutput(ind); - - auto &obj = model->deref().operands().at(ind); - // Model output cannot become model input - if (obj.isModelInput()) - { - return ANEURALNETWORKS_BAD_DATA; - } - } - - return ANEURALNETWORKS_NO_ERROR; -} - -int ANeuralNetworksModel_finish(ANeuralNetworksModel *model) -{ - if (model == nullptr) - { - return ANEURALNETWORKS_UNEXPECTED_NULL; - } - - return model->finish(); -} diff --git a/runtimes/neurun/src/frontend/wrapper/compilation.cc b/runtimes/neurun/src/frontend/wrapper/compilation.cc deleted file mode 100644 index e4aa99f7a..000000000 --- a/runtimes/neurun/src/frontend/wrapper/compilation.cc +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 "compilation.h" - -int ANeuralNetworksCompilation::finish() -{ - try - { - _compiler->compile(); - } - catch (const std::exception &e) - { - return ANEURALNETWORKS_BAD_STATE; - } - - return ANEURALNETWORKS_NO_ERROR; -} diff --git a/runtimes/neurun/src/frontend/wrapper/compilation.h b/runtimes/neurun/src/frontend/wrapper/compilation.h deleted file mode 100644 index d4ba32ea5..000000000 --- a/runtimes/neurun/src/frontend/wrapper/compilation.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#ifndef __COMPILATION_H__ -#define __COMPILATION_H__ - -#include "compiler/Compiler.h" -#include "graph/Graph.h" - -struct ANeuralNetworksCompilation -{ -public: - ANeuralNetworksCompilation(const std::shared_ptr<neurun::graph::Graph> &model) - : _compiler{new neurun::compiler::Compiler{model}} - { - // DO NOTHING - } - -public: - neurun::compiler::Plan &plan(void) { return _compiler->plan(); } - -public: - void publish(std::shared_ptr<const neurun::compiler::Plan> &plan) { _compiler->release(plan); } - int finish(); - -private: - std::shared_ptr<neurun::compiler::Compiler> _compiler; -}; - -#endif diff --git a/runtimes/neurun/src/frontend/wrapper/event.h b/runtimes/neurun/src/frontend/wrapper/event.h deleted file mode 100644 index d144b7c07..000000000 --- a/runtimes/neurun/src/frontend/wrapper/event.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -#ifndef __EVENT_H__ -#define __EVENT_H__ - -struct ANeuralNetworksEvent -{ -}; - -#endif diff --git a/runtimes/neurun/src/frontend/wrapper/execution.h b/runtimes/neurun/src/frontend/wrapper/execution.h deleted file mode 100644 index b68a7b967..000000000 --- a/runtimes/neurun/src/frontend/wrapper/execution.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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. - */ - -#ifndef __EXECUTION_H__ -#define __EXECUTION_H__ - -#include "compiler/Plan.h" -#include "exec/Source.h" -#include "exec/Sink.h" - -struct ANeuralNetworksExecution -{ -public: - ANeuralNetworksExecution(const std::shared_ptr<const neurun::compiler::Plan> &plan) : _plan{plan} - { - _sources.resize(_plan->model().getInputs().size()); - _sinks.resize(_plan->model().getOutputs().size()); - } - -public: - const neurun::compiler::Plan &plan(void) const { return *_plan; } - -private: - std::shared_ptr<const neurun::compiler::Plan> _plan; - -public: - // TODO Use InputIndex instead of int - void source(int n, std::unique_ptr<neurun::exec::ISource> &&source) - { - _sources.at(n) = std::move(source); - } - template <typename T, typename... Args> void source(int n, Args &&... args) - { - source(n, std::unique_ptr<T>{new T{std::forward<Args>(args)...}}); - } - -public: - const neurun::exec::ISource &source(int n) const { return *(_sources.at(n)); } - -public: - // TODO Use OutputIndex instead of int - void sink(int n, std::unique_ptr<neurun::exec::ISink> &&sink) { _sinks.at(n) = std::move(sink); } - template <typename T, typename... Args> void sink(int n, Args &&... args) - { - sink(n, std::unique_ptr<T>{new T{std::forward<Args>(args)...}}); - } - -public: - const neurun::exec::ISink &sink(int n) const { return *(_sinks.at(n)); } - -private: - std::vector<std::unique_ptr<neurun::exec::ISource>> _sources; - std::vector<std::unique_ptr<neurun::exec::ISink>> _sinks; -}; - -#endif diff --git a/runtimes/neurun/src/frontend/wrapper/memory.cc b/runtimes/neurun/src/frontend/wrapper/memory.cc deleted file mode 100644 index 456015123..000000000 --- a/runtimes/neurun/src/frontend/wrapper/memory.cc +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 <NeuralNetworks.h> -#include <sys/mman.h> - -#include "memory.h" - -// -// ANeuralNetworksMemory -// -ANeuralNetworksMemory::ANeuralNetworksMemory(size_t size, int protect, int fd, size_t offset) -{ - _base = reinterpret_cast<uint8_t *>(mmap(nullptr, size, protect, MAP_PRIVATE, fd, offset)); - _size = size; -} - -ANeuralNetworksMemory::~ANeuralNetworksMemory() { munmap(reinterpret_cast<void *>(_base), _size); } diff --git a/runtimes/neurun/src/frontend/wrapper/memory.h b/runtimes/neurun/src/frontend/wrapper/memory.h deleted file mode 100644 index a430bcf49..000000000 --- a/runtimes/neurun/src/frontend/wrapper/memory.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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. - */ - -#ifndef __MEMORY_H__ -#define __MEMORY_H__ - -#include <cstdint> - -struct ANeuralNetworksMemory -{ -public: - ANeuralNetworksMemory(size_t size, int protect, int fd, size_t offset); - ~ANeuralNetworksMemory(); - -public: - size_t size(void) const { return _size; } - uint8_t *base(void) { return _base; } - const uint8_t *base(void) const { return _base; } - -private: - size_t _size; - uint8_t *_base; -}; - -#endif // __MEMORY_H__ diff --git a/runtimes/neurun/src/frontend/wrapper/model.cc b/runtimes/neurun/src/frontend/wrapper/model.cc deleted file mode 100644 index a7a9275fc..000000000 --- a/runtimes/neurun/src/frontend/wrapper/model.cc +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 "model.h" - -#include "graph/Graph.h" - -// -// ANeuralNetworksModel -// -ANeuralNetworksModel::ANeuralNetworksModel() - : _model{new neurun::graph::Graph}, _optional_operands{} -{ - // DO NOTHING -} - -ResultCode ANeuralNetworksModel::finish() -{ - // This function must only be called once for a given model - if (isFinished()) - { - return ANEURALNETWORKS_BAD_STATE; - } - - fillOptionalOperand(); - - _model->finishBuilding(); - - return ANEURALNETWORKS_NO_ERROR; -} - -void ANeuralNetworksModel::fillOptionalOperand(void) -{ - _model->operations().iterate( - [&](const ::neurun::model::operation::Index &, ::neurun::model::operation::Node &node) { - for (auto input : node.getInputs()) - { - // TODO fill default value for optional operands - if (_optional_operands.find(input) != _optional_operands.end()) - { - throw std::runtime_error{"Optional operand is not supported yet"}; - } - } - }); -} diff --git a/runtimes/neurun/src/frontend/wrapper/model.h b/runtimes/neurun/src/frontend/wrapper/model.h deleted file mode 100644 index 2386a648d..000000000 --- a/runtimes/neurun/src/frontend/wrapper/model.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - */ - -#ifndef __MODEL_H__ -#define __MODEL_H__ - -#include <NeuralNetworks.h> - -#include "graph/Graph.h" - -struct ANeuralNetworksModel -{ -public: - ANeuralNetworksModel(); - -public: - neurun::graph::Graph &deref(void) { return *_model; } - ResultCode finish(); - bool isFinished() { return !_model->isBuildingPhase(); } - void release(std::shared_ptr<neurun::graph::Graph> &model) { model = _model; } - void setOptionalOperand(const neurun::model::operand::Index idx) - { - _optional_operands.insert(idx); - } - -private: - void fillOptionalOperand(void); - -private: - std::shared_ptr<neurun::graph::Graph> _model; - std::unordered_set<neurun::model::operand::Index> _optional_operands; -}; - -#endif // __MODEL_H__ diff --git a/runtimes/neurun/src/graph/Graph.cc b/runtimes/neurun/src/graph/Graph.cc deleted file mode 100644 index 832e2b887..000000000 --- a/runtimes/neurun/src/graph/Graph.cc +++ /dev/null @@ -1,334 +0,0 @@ -/* - * 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 "Graph.h" - -#include <algorithm> -#include <bitset> - -#include "util/logging.h" -#include "verifier/Verifier.h" -#include "cpp14/memory.h" -#include "linear/Linear.h" -#include "operation/LowerInfo.h" -#include "operand/LowerInfo.h" -#include "operand/Shape4DConvert.h" -#include "compiler/BackendResolver.h" -#include "backend/interface/IConfig.h" -#include "pass/PermutationInsertionPass.h" -#include "pass/PermutationEliminationPass.h" - -namespace neurun -{ -namespace graph -{ - -Graph::Graph(void) = default; - -Graph::~Graph(void) = default; - -model::operand::Index Graph::addOperand(const model::operand::Shape &shape, - const model::operand::TypeInfo &type) -{ - return _model->operands.append(shape, type); -} - -model::operation::Index Graph::addOperation(std::unique_ptr<model::operation::Node> &&node) -{ - assert(isBuildingPhase()); - return _model->operations.append(std::move(node)); -} - -void Graph::setOperandValue(const model::operand::Index &ind, - std::unique_ptr<model::operand::Data> &&data) -{ - assert(isBuildingPhase()); - assert(_model->operands.exist(ind)); - _model->operands.at(ind).data(std::move(data)); -} - -void Graph::addInput(const model::operand::Index &ind) -{ - assert(isBuildingPhase()); - _model->inputs.append(ind); -} - -void Graph::addOutput(const model::operand::Index &ind) -{ - assert(isBuildingPhase()); - _model->outputs.append(ind); -} - -void Graph::finishBuilding(void) -{ - assert(isBuildingPhase()); - _phase = Phase::MODEL; - - // Initialize operand use-def - initializeUseDef(); - - // Call graph verifications for the MODEL phase - { - assert(verifier::DAGChecker().verify(*this)); - assert(verifier::EdgeConsistencyChecker().verify(*this)); - } -} - -void Graph::lower(void) -{ - assert(_phase == Phase::MODEL); - - // Lower - { - // operand::LowerInfo holder - std::unordered_map<model::operand::Index, std::unique_ptr<operand::LowerInfo>> - operands_lower_info; - - _model->operands.iterate([&](const model::operand::Index &index, - const model::operand::Object &object) { - operands_lower_info[index] = - nnfw::cpp14::make_unique<operand::LowerInfo>(graph::operand::asShape4D(object.shape())); - }); - - _backend_resolver = nnfw::cpp14::make_unique<compiler::BackendResolver>(_model->operands); - - _model->operations.iterate( - [&](const model::operation::Index &index, model::operation::Node &node) { - auto backend = _backend_resolver->getBackend(typeid(node)); - - // Operation LowerInfo - setLowerInfo(index, nnfw::cpp14::make_unique<graph::operation::LowerInfo>(backend)); - - // LowerInfo for in/output operands - for (auto operand : node.getInputs()) - { - auto &&lower_info = operands_lower_info.at(operand); - lower_info->addUseBackend(backend); - } - for (auto operand : node.getOutputs()) - { - auto &&lower_info = operands_lower_info.at(operand); - lower_info->addDefBackend(backend); - } - }); - - // Add def backend to model input/output operand as default backend - for (auto index : getInputs()) - { - auto &&lower_info = operands_lower_info.at(index); - lower_info->addDefBackend(_backend_resolver->getDefaultBackend()); - } - - for (auto index : getOutputs()) - { - auto &&lower_info = operands_lower_info.at(index); - lower_info->addUseBackend(_backend_resolver->getDefaultBackend()); - } - - // Add DefBackend constants same as UseBackend - // NOTE This assumes a constant operand is used by only one operation - _model->operations.iterate([&](const model::operation::Index &, model::operation::Node &node) { - // LowerInfo for input operands - for (auto operand : node.getInputs()) - { - auto &&lower_info = operands_lower_info.at(operand); - if (lower_info->def_backends().empty()) - { - lower_info->addDefBackend(lower_info->use_backends().getOnlyElement()); - } - } - }); - - // Set LowerInfo for each operand from the operand::LowerInfo holder - _model->operands.iterate([&](const model::operand::Index &index, - model::operand::Object &object) { - object.lower_info(std::move(operands_lower_info[index])); - - // Dump operand LowerInfo - // TODO Extract this dumping procedure to be reusable - if (!object.lower_info()->def_backends().empty() || - !object.lower_info()->use_backends().empty()) - { - auto backends_to_string = [](const operand::BackendSet &backends) { - std::string str; - for (auto backend : backends) - { - str += backend->config()->id(); - str += " "; - } - return "{ " + str + "}"; - }; - - auto operation_index_to_string = [](const model::operation::IndexList &operations) { - std::string str; - for (auto op : operations.list()) - { - str += std::to_string(op.value()); - str += " "; - } - return "{ " + str + "}"; - }; - - const auto &lower_info = object.lower_info(); - const auto &shape = object.shape(); - const auto &lower_shape = lower_info->shape(); - std::string def_ops = operation_index_to_string(object.getDef()); - std::string use_ops = operation_index_to_string(object.getUses()); - std::string def_layouts = backends_to_string(lower_info->def_backends()); - std::string use_layouts = backends_to_string(lower_info->use_backends()); - VERBOSE(Lower) << "* Operand #" << index.value() << " LowerInfo" << std::endl; - VERBOSE(Lower) << " - Shape : { " << shape.dim(0) << " " - << (shape.rank() > 1 ? shape.dim(1) : 0) << " " - << (shape.rank() > 2 ? shape.dim(2) : 0) << " " - << (shape.rank() > 3 ? shape.dim(3) : 0) << " " - << "}" << std::endl; - VERBOSE(Lower) << " - Def Operations : " << def_ops << std::endl; - VERBOSE(Lower) << " - Use Operations : " << use_ops << std::endl; - VERBOSE(Lower) << " - Lower Info" << std::endl; - VERBOSE(Lower) << " - 4D Shape (NHWC) : { " << lower_shape.n() << " " << lower_shape.h() - << " " << lower_shape.w() << " " << lower_shape.c() << " " - << "}" << std::endl; - VERBOSE(Lower) << " - Def Backends : " << def_layouts << std::endl; - VERBOSE(Lower) << " - Use Backends : " << use_layouts << std::endl; - } - }); - } - - // Run PermutationInsertionPass - { - pass::PermutationInsertionPass pi_pass(*this); - pi_pass.run(); - pass::PermutationEliminationPass pe_pass(*this); - pe_pass.run(); - } - - // Graph verifications for the LOWERED phase - { - assert(verifier::DAGChecker().verify(*this)); - assert(verifier::EdgeConsistencyChecker().verify(*this)); - } -} - -std::unique_ptr<linear::Linear> Graph::linearize(void) -{ - assert(_phase == Phase::MODEL); - - auto linear = nnfw::cpp14::make_unique<linear::Linear>(*this); - - // TODO Move the operations and operands to linear object - return std::move(linear); -} - -void Graph::initializeUseDef() -{ - operations().iterate( - [&](const model::operation::Index &index, const model::operation::Node &node) -> void { - auto outputs = node.getOutputs(); - for (auto output : outputs) - { - operands().at(output).appendDef(index); - } - - auto inputs = node.getInputs(); - for (auto input : inputs) - { - operands().at(input).appendUse(index); - } - }); -} - -const operation::LowerInfo *Graph::getLowerInfo(const model::operation::Index &index) const -{ - auto itr = _operation_lower_info.find(index); - if (itr == _operation_lower_info.end()) - return nullptr; - return itr->second.get(); -} - -void Graph::setLowerInfo(const model::operation::Index &index, - std::unique_ptr<operation::LowerInfo> &&lower_info) -{ - _operation_lower_info.insert(std::make_pair(index, std::move(lower_info))); -} - -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace graph -{ - -// Explicit instantiations to have implementation in the source file. - -template class Graph::DefaultIterator<true>; -template class Graph::DefaultIterator<false>; - -template class Graph::PostDfsIterator<true>; -template class Graph::PostDfsIterator<false>; - -// -// Graph::DefaultIterator -// - -template <bool is_const> -void Graph::DefaultIterator<is_const>::iterate(GraphRef graph, const IterFn &fn) const -{ - graph.operations().iterate( - [&](const model::operation::Index &index, NodeRef node) -> void { fn(index, node); }); -} - -// -// Graph::PostDfsIterator -// - -template <bool is_const> -void Graph::PostDfsIterator<is_const>::iterate(GraphRef graph, const IterFn &fn) const -{ - assert(!graph.isBuildingPhase()); // Restrict iteration condition - - std::unordered_map<model::operation::Index, bool> visited; - graph.operations().iterate( - [&](const model::operation::Index &index, NodeRef) { visited[index] = false; }); - - std::function<void(const model::operation::Index &, NodeRef)> dfs_recursive = - [&](const model::operation::Index &index, NodeRef node) -> void { - if (visited[index]) - return; - visited[index] = true; - - for (auto output : node.getOutputs()) - { - const auto &operand = graph.operands().at(output); - for (const auto &use : operand.getUses().list()) - { - dfs_recursive(use, graph.operations().at(use)); - } - } - - fn(index, node); - }; - - graph.operations().iterate(dfs_recursive); - - // All of the operations(nodes) must have been visited. - assert(std::all_of( - visited.begin(), visited.end(), - [](const std::pair<const model::operation::Index, bool> &v) { return v.second; })); -} - -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/Graph.h b/runtimes/neurun/src/graph/Graph.h deleted file mode 100644 index afcfdce12..000000000 --- a/runtimes/neurun/src/graph/Graph.h +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_GRAPH_H__ -#define __NEURUN_GRAPH_GRAPH_H__ - -#include <functional> - -#include "model/operation/Node.h" -#include "graph/Model.h" - -namespace neurun -{ -namespace linear -{ -class Linear; -} // namespace linear -} // namespace neurun - -namespace neurun -{ -namespace compiler -{ -class BackendResolver; -} // namespace compiler -} // namespace neurun - -namespace neurun -{ -namespace graph -{ - -class Graph -{ -private: - enum class Phase - { - BUILDING, - MODEL - }; - -public: - template <bool is_const> class Iterator - { - public: - using GraphRef = typename std::conditional<is_const, const Graph &, Graph &>::type; - using IndexRef = const model::operation::Index &; - using NodeRef = typename std::conditional<is_const, const model::operation::Node &, - model::operation::Node &>::type; - using IterFn = std::function<void(IndexRef, NodeRef)>; - - public: - virtual ~Iterator() = default; - virtual void iterate(GraphRef graph, const IterFn &fn) const = 0; - }; - - template <bool is_const = false> class DefaultIterator final : public Iterator<is_const> - { - public: - using GraphRef = typename Iterator<is_const>::GraphRef; - using IndexRef = typename Iterator<is_const>::IndexRef; - using NodeRef = typename Iterator<is_const>::NodeRef; - using IterFn = typename Iterator<is_const>::IterFn; - - public: - void iterate(GraphRef graph, const IterFn &fn) const; - }; - using DefaultConstIterator = DefaultIterator<true>; - - template <bool is_const = false> class PostDfsIterator final : public Iterator<is_const> - { - public: - using GraphRef = typename Iterator<is_const>::GraphRef; - using IndexRef = typename Iterator<is_const>::IndexRef; - using NodeRef = typename Iterator<is_const>::NodeRef; - using IterFn = typename Iterator<is_const>::IterFn; - - public: - void iterate(GraphRef graph, const IterFn &fn) const; - }; - using PostDfsConstIterator = PostDfsIterator<true>; - -public: - Graph(void); - ~Graph(void); - - // Graph Building -public: - model::operand::Index addOperand(const model::operand::Shape &shape, - const model::operand::TypeInfo &type); - model::operation::Index addOperation(std::unique_ptr<model::operation::Node> &&node); - void setOperandValue(const model::operand::Index &ind, - std::unique_ptr<model::operand::Data> &&data); - void addInput(const model::operand::Index &ind); - void addOutput(const model::operand::Index &ind); - void finishBuilding(void); - void lower(void); - void removeOperand(const model::operand::Index &ind) { _model->operands.remove(ind); } - std::unique_ptr<linear::Linear> linearize(void); - bool isBuildingPhase(void) const { return _phase == Phase::BUILDING; } - -private: - void initializeUseDef(); - - // Accessors -public: - const model::operand::IndexSet &getInputs() const { return _model->inputs; } - model::operand::IndexSet &getInputs() { return _model->inputs; } - const model::operand::IndexSet &getOutputs() const { return _model->outputs; } - model::operand::IndexSet &getOutputs() { return _model->outputs; } - const model::operand::Set &operands() const { return _model->operands; } - model::operand::Set &operands() - { - return _model->operands; - } // TODO Remove this non-const accessor - const model::operation::Set &operations() const { return _model->operations; } - model::operation::Set &operations() { return _model->operations; } - const compiler::BackendResolver *backend_resolver() const { return _backend_resolver.get(); } - -private: - Phase _phase{Phase::BUILDING}; - std::unique_ptr<Model> _model{new Model}; - - // For LOWERED phase -public: - const operation::LowerInfo *getLowerInfo(const model::operation::Index &index) const; - void setLowerInfo(const model::operation::Index &index, - std::unique_ptr<operation::LowerInfo> &&lower_info); - -private: - std::unique_ptr<compiler::BackendResolver> _backend_resolver; - std::unordered_map<model::operation::Index, std::unique_ptr<operation::LowerInfo>> - _operation_lower_info; -}; - -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_GRAPH_H__ diff --git a/runtimes/neurun/src/graph/Index.h b/runtimes/neurun/src/graph/Index.h deleted file mode 100644 index 3263d12ad..000000000 --- a/runtimes/neurun/src/graph/Index.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_INDEX_H__ -#define __NEURUN_GRAPH_INDEX_H__ - -#include <functional> -#include <limits> -#include <stdint.h> - -namespace neurun -{ -namespace graph -{ - -template <typename T, typename DummyTag> class Index -{ -private: - static const T UNDEFINED = std::numeric_limits<T>::max(); - -public: - explicit Index(void) : _index{UNDEFINED} {} - explicit Index(T o) : _index{o} {} - explicit Index(int32_t o) : _index{static_cast<T>(o)} {} // For legacy code compatibility - Index(const Index &o) : _index{o._index} {} - - Index &operator=(T o) - { - _index = o; - return *this; - } - - Index &operator=(const T &o) - { - _index = o._index; - return *this; - } - - bool operator==(T o) const { return _index == o; } - bool operator==(const Index &o) const { return _index == o._index; } - bool operator!=(T o) const { return !(*this == o); } - bool operator!=(const Index &o) const { return !(*this == o); } - - T value() const { return _index; } - int32_t asInt() const { return static_cast<int32_t>(_index); } // For legacy code compatibility - -private: - T _index; -}; - -} // namespace graph -} // namespace neurun - -namespace std -{ - -template <typename T, typename Tag> struct hash<::neurun::graph::Index<T, Tag>> -{ - size_t operator()(const ::neurun::graph::Index<T, Tag> &index) const noexcept - { - return hash<T>()(index.value()); - } -}; - -} // namespace std - -#endif // __NEURUN_GRAPH_INDEX_H__ diff --git a/runtimes/neurun/src/graph/Model.h b/runtimes/neurun/src/graph/Model.h deleted file mode 100644 index 20bb713af..000000000 --- a/runtimes/neurun/src/graph/Model.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_MODEL_H__ -#define __NEURUN_GRAPH_MODEL_H__ - -#include "model/operation/Set.h" -#include "model/operand/IndexSet.h" -#include "model/operand/Set.h" - -namespace neurun -{ -namespace graph -{ - -struct Model -{ - model::operation::Set operations; - model::operand::Set operands; - model::operand::IndexSet inputs; - model::operand::IndexSet outputs; -}; - -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_MODEL_H__ diff --git a/runtimes/neurun/src/graph/dumper/Dumper.cc b/runtimes/neurun/src/graph/dumper/Dumper.cc deleted file mode 100644 index efffc5849..000000000 --- a/runtimes/neurun/src/graph/dumper/Dumper.cc +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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 "Dumper.h" - -#include <string> - -#include "util/logging.h" - -namespace neurun -{ -namespace graph -{ -namespace dumper -{ - -using namespace neurun::model::operation; - -void Dumper::visit(const Conv2DNode &node) -{ - VERBOSE(LIR) << "* Conv2D(Implicit)" << std::endl; - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ") Kernel(" - << node.getInputs().at(1).value() << ") Bias(" << node.getInputs().at(2).value() - << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const MaxPool2DNode &node) -{ - VERBOSE(LIR) << "* MaxPool2D(Implicit)" << std::endl; - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const AvgPool2DNode &node) -{ - VERBOSE(LIR) << "* AvgPool2D(Implicit)" << std::endl; - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const ConcatNode &node) -{ - VERBOSE(LIR) << "* Concat" << std::endl; - std::string inputs; - for (auto i : node.getInputs()) - { - inputs += std::to_string(i.value()) + ","; - } - VERBOSE(LIR) << " - Inputs : IFM(" << inputs << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const FullyConnectedNode &node) -{ - VERBOSE(LIR) << "* FullyConnected" << std::endl; - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ") Weight(" - << node.getInputs().at(1).value() << ") Bias(" << node.getInputs().at(2).value() - << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const ReshapeNode &node) -{ - VERBOSE(LIR) << "* Reshape" << std::endl; - // TODO The shape index should be "node.getInputs().at(1).value()" but not valid for now - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ") Shape(" - << "?" - << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const SoftmaxNode &node) -{ - VERBOSE(LIR) << "* Softmax" << std::endl; - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const PermuteNode &node) -{ - VERBOSE(LIR) << "* Permute" << std::endl; - VERBOSE(LIR) << " - Inputs : IFM(" << node.getInputs().at(0).value() << ")" << std::endl; - VERBOSE(LIR) << " - Output : OFM(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -void Dumper::visit(const AddNode &node) -{ - VERBOSE(LIR) << "* Add" << std::endl; - VERBOSE(LIR) << " - Inputs : Input(" << node.getInputs().at(0).value() << ", " - << node.getInputs().at(1).value() << ")" << std::endl; - VERBOSE(LIR) << " - Output : Output(" << node.getOutputs().at(0).value() << ")" << std::endl; -} - -} // namespace dumper -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/dumper/Dumper.h b/runtimes/neurun/src/graph/dumper/Dumper.h deleted file mode 100644 index 8c079a11d..000000000 --- a/runtimes/neurun/src/graph/dumper/Dumper.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_DUMPER_H__ -#define __NEURUN_GRAPH_DUMPER_H__ - -#include "model/operation/NodeVisitor.h" - -namespace neurun -{ -namespace graph -{ -namespace dumper -{ - -class Dumper : public model::operation::NodeVisitor -{ -public: - Dumper() = default; - -public: - void visit(const model::operation::Conv2DNode &node) override; - void visit(const model::operation::MaxPool2DNode &node) override; - void visit(const model::operation::AvgPool2DNode &node) override; - void visit(const model::operation::ConcatNode &node) override; - void visit(const model::operation::FullyConnectedNode &node) override; - void visit(const model::operation::ReshapeNode &node) override; - void visit(const model::operation::SoftmaxNode &node) override; - void visit(const model::operation::PermuteNode &node) override; - void visit(const model::operation::AddNode &node) override; -}; - -} // namespace dumper -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_DUMPER_H__ diff --git a/runtimes/neurun/src/graph/operand/BackendSet.cc b/runtimes/neurun/src/graph/operand/BackendSet.cc deleted file mode 100644 index 9a284d722..000000000 --- a/runtimes/neurun/src/graph/operand/BackendSet.cc +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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 "BackendSet.h" - -#include <cassert> - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -BackendSet::BackendSet(std::initializer_list<const backend::Backend *> backends) -{ - for (auto backend : backends) - { - _set.insert(backend); - } -} - -const backend::Backend *BackendSet::getOnlyElement() const -{ - assert(_set.size() == 1u); - return *_set.begin(); -} - -BackendSet BackendSet::operator|(const BackendSet &other) const -{ - auto ret = *this; - for (auto backend : other) - { - ret.add(backend); - } - return ret; -} - -BackendSet BackendSet::operator&(const BackendSet &other) const -{ - BackendSet ret; - for (auto backend : other) - { - if (contains(backend)) - { - ret.add(backend); - } - } - return ret; -} - -BackendSet BackendSet::operator-(const BackendSet &other) const -{ - auto ret = *this; - for (auto backend : other) - { - ret.remove(backend); - } - return ret; -} - -} // namespace operand -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/operand/BackendSet.h b/runtimes/neurun/src/graph/operand/BackendSet.h deleted file mode 100644 index 8b457a084..000000000 --- a/runtimes/neurun/src/graph/operand/BackendSet.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_OPERAND_BACKEND_SET_H__ -#define __NEURUN_GRAPH_OPERAND_BACKEND_SET_H__ - -#include <initializer_list> -#include <unordered_set> - -namespace neurun -{ -namespace backend -{ -class Backend; -} // namespace backend -} // namespace neurun - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -class BackendSet -{ -public: - BackendSet() = default; - BackendSet(std::initializer_list<const backend::Backend *> backends); - -public: - void add(const backend::Backend *backend) { _set.insert(backend); } - void remove(const backend::Backend *backend) { _set.erase(backend); } - uint32_t size() const { return static_cast<uint32_t>(_set.size()); } - bool empty() const { return _set.empty(); } - bool contains(const backend::Backend *backend) const { return _set.find(backend) != _set.end(); } - const backend::Backend *getOnlyElement() const; - -public: - BackendSet operator|(const BackendSet &other) const; // Union - BackendSet operator&(const BackendSet &other) const; // Intersect - BackendSet operator-(const BackendSet &other) const; // Minus - -public: - std::unordered_set<const backend::Backend *>::const_iterator begin() const - { - return _set.begin(); - } - std::unordered_set<const backend::Backend *>::const_iterator end() const { return _set.end(); } - -private: - std::unordered_set<const backend::Backend *> _set; -}; - -} // namespace operand -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_OPERAND_BACKEND_SET_H__ diff --git a/runtimes/neurun/src/graph/operand/Layout.h b/runtimes/neurun/src/graph/operand/Layout.h deleted file mode 100644 index 023ecbdad..000000000 --- a/runtimes/neurun/src/graph/operand/Layout.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_OPERAND_LAYOUT_H__ -#define __NEURUN_GRAPH_OPERAND_LAYOUT_H__ - -#include <functional> - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -enum class Layout -{ - UNKNOWN = 0, - NHWC, - NCHW -}; - -} // namespace operand -} // namespace graph -} // namespace neurun - -namespace std -{ - -template <> struct hash<::neurun::graph::operand::Layout> -{ - size_t operator()(const ::neurun::graph::operand::Layout &value) const noexcept - { - using type = typename std::underlying_type<::neurun::graph::operand::Layout>::type; - return hash<type>()(static_cast<type>(value)); - } -}; - -} // namespace std - -#endif // __NEURUN_GRAPH_OPERAND_LAYOUT_H__ diff --git a/runtimes/neurun/src/graph/operand/LayoutSet.cc b/runtimes/neurun/src/graph/operand/LayoutSet.cc deleted file mode 100644 index 47bb5900a..000000000 --- a/runtimes/neurun/src/graph/operand/LayoutSet.cc +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 "LayoutSet.h" - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -LayoutSet::LayoutSet(std::initializer_list<Layout> layouts) -{ - for (auto layout : layouts) - { - _set.insert(layout); - } -} - -LayoutSet LayoutSet::operator|(const LayoutSet &other) const -{ - auto ret = *this; - for (auto layout : other) - { - ret.add(layout); - } - return ret; -} - -LayoutSet LayoutSet::operator&(const LayoutSet &other) const -{ - LayoutSet ret; - for (auto layout : other) - { - if (contains(layout)) - { - ret.add(layout); - } - } - return ret; -} - -LayoutSet LayoutSet::operator-(const LayoutSet &other) const -{ - auto ret = *this; - for (auto layout : other) - { - ret.remove(layout); - } - return ret; -} - -} // namespace operand -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/operand/LayoutSet.h b/runtimes/neurun/src/graph/operand/LayoutSet.h deleted file mode 100644 index 928259c87..000000000 --- a/runtimes/neurun/src/graph/operand/LayoutSet.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_OPERAND_LAYOUT_SET_H__ -#define __NEURUN_GRAPH_OPERAND_LAYOUT_SET_H__ - -#include <initializer_list> -#include <unordered_set> - -#include "Layout.h" - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -class LayoutSet -{ -public: - LayoutSet() = default; - LayoutSet(std::initializer_list<Layout> layouts); - -public: - void add(const Layout &layout) { _set.insert(layout); } - void remove(const Layout &layout) { _set.erase(layout); } - uint32_t size() const { return static_cast<uint32_t>(_set.size()); } - bool contains(const Layout &layout) const { return _set.find(layout) != _set.end(); } - -public: - LayoutSet operator|(const LayoutSet &other) const; // Union - LayoutSet operator&(const LayoutSet &other) const; // Intersect - LayoutSet operator-(const LayoutSet &other) const; // Minus - -public: - std::unordered_set<Layout>::const_iterator begin() const { return _set.begin(); } - std::unordered_set<Layout>::const_iterator end() const { return _set.end(); } - -private: - std::unordered_set<Layout> _set; -}; - -} // namespace operand -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_OPERAND_LAYOUT_SET_H__ diff --git a/runtimes/neurun/src/graph/operand/LowerInfo.cc b/runtimes/neurun/src/graph/operand/LowerInfo.cc deleted file mode 100644 index c26965911..000000000 --- a/runtimes/neurun/src/graph/operand/LowerInfo.cc +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 "LowerInfo.h" - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -// NO IMPLEMENTATION YET - -} // namespace operand -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/operand/LowerInfo.h b/runtimes/neurun/src/graph/operand/LowerInfo.h deleted file mode 100644 index 7900e54d9..000000000 --- a/runtimes/neurun/src/graph/operand/LowerInfo.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_OPERAND_LOWER_INFO_H__ -#define __NEURUN_GRAPH_OPERAND_LOWER_INFO_H__ - -#include <stdint.h> - -#include "BackendSet.h" - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -class LowerInfo -{ -public: - class Shape4D - { - public: - Shape4D(uint32_t n, uint32_t h, uint32_t w, uint32_t c) : _n{n}, _h{h}, _w{w}, _c{c} - { - // DO NOTHING - } - - public: - uint32_t n(void) const { return _n; } - uint32_t h(void) const { return _h; } - uint32_t w(void) const { return _w; } - uint32_t c(void) const { return _c; } - - private: - uint32_t _n; - uint32_t _h; - uint32_t _w; - uint32_t _c; - }; - -public: - LowerInfo(const Shape4D &shape) : _shape{shape} - { - // DO NOTHING - } - -public: - const Shape4D &shape(void) const { return _shape; } - const BackendSet &def_backends(void) const { return _def_backends; } - const BackendSet &use_backends(void) const { return _use_backends; } - -public: - void addDefBackend(const backend::Backend *backend) { _def_backends.add(backend); } - void addUseBackend(const backend::Backend *backend) { _use_backends.add(backend); } - void removeDefBackend(const backend::Backend *backend) { _def_backends.remove(backend); } - void removeUseBackend(const backend::Backend *backend) { _use_backends.remove(backend); } - -private: - Shape4D _shape; - BackendSet _def_backends; - BackendSet _use_backends; -}; - -} // namespace operand -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_OPERAND_LOWED_INFO_H__ diff --git a/runtimes/neurun/src/graph/operand/ParentInfo.h b/runtimes/neurun/src/graph/operand/ParentInfo.h deleted file mode 100644 index 5e6f56237..000000000 --- a/runtimes/neurun/src/graph/operand/ParentInfo.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 ParentInfo.h - * @brief This file contains ParentInfo class and internal Coordinate4D class - * to represent subsumption between operand - */ - -#ifndef __NEURUN_GRAPH_OPERAND_PARENT_INFO_H__ -#define __NEURUN_GRAPH_OPERAND_PARENT_INFO_H__ - -#include <stdint.h> - -#include "model/operand/Index.h" -#include "util/feature/Coordinate4D.h" - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -using neurun::util::feature::Coordinate4D; - -/** - * @brief Class to represent parent operand in child operand - */ -class ParentInfo -{ -public: - /** - * @brief Construct a new ParentInfo object - * @param[in] parent Index of parent operand - * @param[in] coordinate Offset of child operand in parent operand - * @return - */ - ParentInfo(const model::operand::Index parent, const Coordinate4D &coordinate) - : _parent{parent}, _coordinate{coordinate} - { - // DO NOTHING - } - -public: - /** - * @brief Return parent index - * @return Parent index - */ - model::operand::Index parent(void) const { return _parent; } - /** - * @brief Retern offset in parent - * @return Offset - */ - Coordinate4D offset(void) const { return _coordinate; } - -private: - model::operand::Index _parent; - Coordinate4D _coordinate; -}; - -} // namespace operand -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_OPERAND_PARENT_INFO_H__ diff --git a/runtimes/neurun/src/graph/operand/Shape4DConvert.h b/runtimes/neurun/src/graph/operand/Shape4DConvert.h deleted file mode 100644 index 73cf0903a..000000000 --- a/runtimes/neurun/src/graph/operand/Shape4DConvert.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_OPERAND_SHAPE4D_CONVERT_H__ -#define __NEURUN_GRAPH_OPERAND_SHAPE4D_CONVERT_H__ - -#include "LowerInfo.h" - -namespace neurun -{ -namespace graph -{ -namespace operand -{ - -inline LowerInfo::Shape4D asShape4D(const model::operand::Shape &shape) -{ - switch (shape.rank()) - { - case 0u: - return LowerInfo::Shape4D(1, 1, 1, 1); - - case 1u: - return LowerInfo::Shape4D(shape.dim(0), 1, 1, 1); - - case 2u: - return LowerInfo::Shape4D(shape.dim(0), shape.dim(1), 1, 1); - - case 3u: - return LowerInfo::Shape4D(shape.dim(0), shape.dim(1), shape.dim(2), 1); - - case 4u: - return LowerInfo::Shape4D(shape.dim(0), shape.dim(1), shape.dim(2), shape.dim(3)); - - default: - throw "Unsupported rank > 4"; - } -} - -} // namespace operand -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_OPERAND_SHAPE4D_CONVERT_H__ diff --git a/runtimes/neurun/src/graph/operation/LowerInfo.cc b/runtimes/neurun/src/graph/operation/LowerInfo.cc deleted file mode 100644 index 7862fd0c9..000000000 --- a/runtimes/neurun/src/graph/operation/LowerInfo.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 "LowerInfo.h" - -namespace neurun -{ -namespace graph -{ -namespace operation -{ - -LowerInfo::LowerInfo(const backend::Backend *backend) : _backend(backend) -{ - // DO NOTHING -} - -} // namespace operation -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/operation/LowerInfo.h b/runtimes/neurun/src/graph/operation/LowerInfo.h deleted file mode 100644 index e920b0eb9..000000000 --- a/runtimes/neurun/src/graph/operation/LowerInfo.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_OPERATION_LOWER_INFO_H__ -#define __NEURUN_GRAPH_OPERATION_LOWER_INFO_H__ - -#include <string> - -#include "backend/BackendManager.h" - -namespace neurun -{ -namespace graph -{ -namespace operation -{ - -class LowerInfo -{ -public: - LowerInfo(const backend::Backend *backend); - const backend::Backend *backend() const { return _backend; } - -private: - const backend::Backend *_backend; -}; - -} // namespace operation -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_OPERATION_LOWER_INFO_H__ diff --git a/runtimes/neurun/src/graph/pass/OperandPass.cc b/runtimes/neurun/src/graph/pass/OperandPass.cc deleted file mode 100644 index 3c24d3830..000000000 --- a/runtimes/neurun/src/graph/pass/OperandPass.cc +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 "OperandPass.h" - -#include "graph/Graph.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -void OperandPass::run() -{ - _graph.operands().iterate([&](const model::operand::Index &index, - model::operand::Object &object) { callback(index, object); }); -} - -} // namespace pass -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/pass/OperandPass.h b/runtimes/neurun/src/graph/pass/OperandPass.h deleted file mode 100644 index b84391082..000000000 --- a/runtimes/neurun/src/graph/pass/OperandPass.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_PASS_OPERAND_PASS_H__ -#define __NEURUN_GRAPH_PASS_OPERAND_PASS_H__ - -#include "Pass.h" -#include "model/operand/Index.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ -class Object; -} // namespace operand -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -class OperandPass : public Pass -{ -public: - using Pass::Pass; - -public: - virtual std::string id() = 0; - virtual void run() override final; - virtual void callback(const model::operand::Index &i, model::operand::Object &o) = 0; -}; - -} // namespace pass -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_PASS_OPERAND_PASS_H__ diff --git a/runtimes/neurun/src/graph/pass/OperationPass.cc b/runtimes/neurun/src/graph/pass/OperationPass.cc deleted file mode 100644 index e71f79188..000000000 --- a/runtimes/neurun/src/graph/pass/OperationPass.cc +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 "OperationPass.h" - -#include "graph/Graph.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -void OperationPass::run() -{ - _graph.operations().iterate([&](const model::operation::Index &index, - model::operation::Node &node) { callback(index, node); }); -} - -} // namespace pass -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/pass/OperationPass.h b/runtimes/neurun/src/graph/pass/OperationPass.h deleted file mode 100644 index e86f1aa57..000000000 --- a/runtimes/neurun/src/graph/pass/OperationPass.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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 OperationPass.h - * @brief This file contains OperationPass class - */ - -#ifndef __NEURUN_GRAPH_PASS_OPERATION_PASS_H__ -#define __NEURUN_GRAPH_PASS_OPERATION_PASS_H__ - -#include "Pass.h" - -#include "model/operation/Index.h" -#include "model/operation/Node.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -/** - * @brief Class to iterate over operations and calls callback() method - */ -class OperationPass : public Pass -{ -public: - using Pass::Pass; - -public: - /** - * @brief Returns string id for this pass. Same with class name. - * - * @return string id - */ - virtual std::string id() = 0; - - /** - * @brief Run the pass - */ - virtual void run() override final; - - /** - * @brief The function that will be executed for each operations - * - * @param i[in] Index of the operation node - * @param n[in] The operation node - */ - virtual void callback(const model::operation::Index &i, model::operation::Node &n) = 0; -}; - -} // namespace pass -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_PASS_OPERATION_PASS_H__ diff --git a/runtimes/neurun/src/graph/pass/Pass.cc b/runtimes/neurun/src/graph/pass/Pass.cc deleted file mode 100644 index 4c3436961..000000000 --- a/runtimes/neurun/src/graph/pass/Pass.cc +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 "Pass.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -} // namespace pass -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/pass/Pass.h b/runtimes/neurun/src/graph/pass/Pass.h deleted file mode 100644 index 4200936d1..000000000 --- a/runtimes/neurun/src/graph/pass/Pass.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_PASS_PASS_H__ -#define __NEURUN_GRAPH_PASS_PASS_H__ - -#include <string> - -namespace neurun -{ -namespace graph -{ -class Graph; -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -class Pass -{ -public: - Pass(Graph &graph) : _graph{graph} {} - virtual ~Pass() = default; - -public: - virtual std::string id() = 0; - virtual void run() = 0; - -protected: - Graph &_graph; -}; - -} // namespace pass -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_PASS_PASS_H__ diff --git a/runtimes/neurun/src/graph/pass/PermutationEliminationPass.cc b/runtimes/neurun/src/graph/pass/PermutationEliminationPass.cc deleted file mode 100644 index 848f6b574..000000000 --- a/runtimes/neurun/src/graph/pass/PermutationEliminationPass.cc +++ /dev/null @@ -1,192 +0,0 @@ -/* - * 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 "PermutationEliminationPass.h" - -#include "model/operand/Object.h" -#include "graph/Graph.h" -#include "backend/interface/IConfig.h" -#include "util/logging.h" -#include "compiler/BackendResolver.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ -void PermutationEliminationPass::callback(const model::operand::Index &inp_index, - model::operand::Object &object) -{ - if (_graph.getInputs().contains(inp_index)) - { - eliminateInput(inp_index, object); - } - else if (_graph.getOutputs().contains(inp_index)) - { - eliminateOutput(inp_index, object); - } -} - -void PermutationEliminationPass::eliminateInput(const model::operand::Index &inp_index, - model::operand::Object &object) -{ - auto &model_inputs = _graph.getInputs(); - - // get uses of the model's given input - auto uses = object.getUses(); - - // input must be used just by permutation - if (uses.size() != 1) - { - return; - } - - for (auto input_use : uses.list()) - { - auto &perm_operation = _graph.operations().at(input_use); - auto perm_inputs = perm_operation.getInputs(); - - auto perm_outputs = perm_operation.getOutputs(); - - if (!isPermuteLayerToEliminate(perm_inputs, perm_outputs, true)) - { - return; - } - - assert(perm_inputs.at(0) == inp_index); - - VERBOSE(PermutationEliminationPass::EliminateInput) << "remove NHWC_TO_NCHW permutation\n"; - - // set model's new input, which was output of permutation - model_inputs.replace(inp_index, perm_outputs.at(0)); - - // remove model's input, which is also input of permutation - _graph.removeOperand(inp_index); - - // remove permutation operation - _graph.operations().remove(input_use); - - VERBOSE(PermutationEliminationPass::EliminateInput) - << inp_index.value() << " is model's input and is removed. New input is " - << perm_outputs.at(0).value() << "\n" - << input_use.value() << " is removed permutation operation\n"; - } -} - -void PermutationEliminationPass::eliminateOutput(const model::operand::Index &out_index, - model::operand::Object &object) -{ - auto &model_outputs = _graph.getOutputs(); - - // get defs of the model's given output - auto defs = object.getDef(); - - // output must use just permutation - if (defs.size() != 1) - { - return; - } - - for (auto output_def : defs.list()) - { - auto &perm_operation = _graph.operations().at(output_def); - auto perm_outputs = perm_operation.getOutputs(); - - auto perm_inputs = perm_operation.getInputs(); - if (!isPermuteLayerToEliminate(perm_inputs, perm_outputs, false)) - { - return; - } - - assert(perm_outputs.at(0) == out_index); - - VERBOSE(PermutationEliminationPass::EliminateOutput) << "remove NCHW_TO_NHWC permutation\n"; - - // Update operations' output that is used by permute operand - for (auto perm_input_index : perm_inputs) - { - auto &perm_input_operand = _graph.operands().at(perm_input_index); - perm_input_operand.removeUse(output_def); - } - - // set model's new output, which was input of permutation - model_outputs.replace(out_index, perm_inputs.at(0)); - - // remove model's output, which is also output of permutation - _graph.removeOperand(out_index); - - // remove permutation operation - _graph.operations().remove(output_def); - - VERBOSE(PermutationEliminationPass::EliminateOutput) - << out_index.value() << " is model's output and is removed. New output is " - << perm_inputs.at(0).value() << "\n" - << output_def.value() << " is removed permutation operation\n"; - } -} - -bool PermutationEliminationPass::isPermuteLayerToEliminate( - const model::operand::IndexSet &inp_indexes, const model::operand::IndexSet &out_indexes, - bool is_for_model_input) -{ - auto input_def_backends = _graph.operands().at(inp_indexes.at(0)).lower_info()->def_backends(); - auto output_def_backends = _graph.operands().at(out_indexes.at(0)).lower_info()->def_backends(); - - auto input_layout = input_def_backends.getOnlyElement()->config()->getOperandLayout(); - auto output_layout = output_def_backends.getOnlyElement()->config()->getOperandLayout(); - - if (input_def_backends.size() != 1 || output_def_backends.size() != 1) - { - return false; - } - - // all operands' backend must be the same - for (auto index : inp_indexes) - { - auto op_backend_set = _graph.operands().at(index).lower_info()->def_backends(); - if (op_backend_set.size() != 1 || - input_layout != op_backend_set.getOnlyElement()->config()->getOperandLayout()) - { - return false; - } - } - // all operands' backend must be the same - for (auto index : out_indexes) - { - auto op_backend_set = _graph.operands().at(index).lower_info()->def_backends(); - if (op_backend_set.size() != 1 || - output_layout != op_backend_set.getOnlyElement()->config()->getOperandLayout()) - { - return false; - } - } - - if (is_for_model_input) - { - // check if this is NHWC_TO_NCHW permutation: must have single input, which is model's input - return (inp_indexes.size() == 1 && input_layout == graph::operand::Layout::NHWC && - output_layout == graph::operand::Layout::NCHW); - } - - // check if this is NCHW_TO_NHWC permutation: must have single output, which is model's output - return (out_indexes.size() == 1 && input_layout == graph::operand::Layout::NCHW && - output_layout == graph::operand::Layout::NHWC); -} - -} // namespace pass -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/pass/PermutationEliminationPass.h b/runtimes/neurun/src/graph/pass/PermutationEliminationPass.h deleted file mode 100644 index 2b528c479..000000000 --- a/runtimes/neurun/src/graph/pass/PermutationEliminationPass.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_PASS_PERMUTATION_ELIMINATION_PASS_H__ -#define __NEURUN_GRAPH_PASS_PERMUTATION_ELIMINATION_PASS_H__ - -#include "OperandPass.h" -#include "model/operand/Object.h" -#include "model/operand/IndexSet.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -class PermutationEliminationPass : public OperandPass -{ -public: - using OperandPass::OperandPass; - -public: - virtual std::string id() override { return "PermutationEliminationPass"; } - - virtual void callback(const model::operand::Index &index, model::operand::Object &object); - -private: - /** - * @brief Remove Permute operation that permutates input - * - * Note: This function aslo removes model's input and - * sets output of permutation as model's new input - * - * @param inp_index is the target operand index for the elimination - * @param object is the target operand object for the elimination - * - * @return - */ - void eliminateInput(const model::operand::Index &inp_index, model::operand::Object &object); - - /** - * @brief Remove Permute operation that permutates output of a model - * - * Note: This function aslo removes model's output and - * sets input of permutation as model's new output - * - * @param out_index is the target operand index for the elimination - * @param object is the target operand object for the elimination - * - * @return - */ - void eliminateOutput(const model::operand::Index &out_index, model::operand::Object &object); - - /** - * @brief Determine if passed operands are permute layer's input and output, that must be - * eliminated - * - * @param inp_index indexes of the input operand to operation - * @param out_index indexes of the output operand to operation - * @param is_for_model_input checking for model's input or output - * - * @return if it is permutation layer - */ - bool isPermuteLayerToEliminate(const model::operand::IndexSet &inp_indexes, - const model::operand::IndexSet &out_indexes, - bool is_for_model_input); -}; - -} // namespace pass -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_PASS_PERMUTATION_ELIMINATION_PASS_H__ diff --git a/runtimes/neurun/src/graph/pass/PermutationInsertionPass.cc b/runtimes/neurun/src/graph/pass/PermutationInsertionPass.cc deleted file mode 100644 index 9b833b8c5..000000000 --- a/runtimes/neurun/src/graph/pass/PermutationInsertionPass.cc +++ /dev/null @@ -1,191 +0,0 @@ -/* - * 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 "PermutationInsertionPass.h" - -#include <cassert> -#include <utility> -#include <unordered_map> - -#include "model/operand/Object.h" -#include "graph/operation/LowerInfo.h" -#include "graph/Graph.h" -#include "backend/interface/IConfig.h" -#include "util/logging.h" -#include "cpp14/memory.h" -#include "model/operation/PermuteNode.h" -#include "graph/operand/Shape4DConvert.h" -#include "compiler/BackendResolver.h" - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -void PermutationInsertionPass::callback(const model::operand::Index &index, - model::operand::Object &object) -{ - auto &&operand_li = object.lower_info(); - assert(operand_li); - - // NOTE Later, constants also will have Def - // Ignore constants - if (operand_li->def_backends().size() == 0) - { - return; - } - - std::list<model::operation::Index> permute_indexes; - - // Build a map for all necessary type of operands - std::unordered_map<const backend::Backend *, model::operand::Index> backend_to_index; - { - assert(operand_li->def_backends().size() == 1); - for (auto backend : operand_li->def_backends()) - { - backend_to_index.insert({backend, index}); - } - - auto insert_set = operand_li->use_backends() - operand_li->def_backends(); - for (auto backend : insert_set) - { - const auto permute_operation_index = insertPermute(index, backend); - permute_indexes.push_back(permute_operation_index); - VERBOSE(PermutationInsertionPass) << "Insert 'Permute' operation for operand " - << index.value() << std::endl; - const auto &permute_operation = _graph.operations().at(permute_operation_index); - const auto permuted_operand_index = permute_operation.getOutputs().at(0); - backend_to_index.insert({backend, permuted_operand_index}); - } - } - - // Update operations' input that uses this operand - { - std::list<model::operation::Index> remove_list; - - auto uses = object.getUses(); - for (auto use : uses.list()) - { - // If permute operation, ignore it - if (std::find(permute_indexes.begin(), permute_indexes.end(), use) != permute_indexes.end()) - continue; - - auto &operation = _graph.operations().at(use); - auto operation_li = _graph.getLowerInfo(use); - assert(operation_li); - auto backend = operation_li->backend(); - - auto use_node_inputs = operation.getInputs(); - assert(use_node_inputs.contains(index)); - - auto new_index = backend_to_index.at(backend); - if (index != new_index) - { - // Update from operation - operation.replaceInput(index, new_index); - - // Update from operand - remove_list.push_back( - use); // Removal should be done in another loop since we are in the loop - _graph.operands().at(new_index).appendUse(use); - } - } - - for (auto &operation : remove_list) - { - object.removeUse(operation); - } - } -} - -model::operation::Index -PermutationInsertionPass::insertPermute(const model::operand::Index &operand_index, - const backend::Backend *backend) -{ - assert(!_graph.isBuildingPhase()); - - auto &operand = _graph.operands().at(operand_index); - - // Generate output operand and permute operation - auto out_operand_index = _graph.addOperand(operand.shape(), operand.typeInfo()); - auto &out_operand = _graph.operands().at(out_operand_index); - out_operand.setAsOperationOutput(); - // change model output if operand_index is model output index - auto &model_outputs = _graph.getOutputs(); - if (model_outputs.contains(operand_index)) - { - model_outputs.replace(operand_index, out_operand_index); - } - out_operand.setAsOperationOutput(); - auto out_operand_li = - nnfw::cpp14::make_unique<operand::LowerInfo>(operand::asShape4D(operand.shape())); - out_operand_li->addDefBackend(backend); - out_operand_li->addUseBackend(backend); - out_operand.lower_info(std::move(out_operand_li)); - - // Update LowerInfo of input operand - operand.lower_info()->removeUseBackend(backend); - operand.lower_info()->addUseBackend(operand.lower_info()->def_backends().getOnlyElement()); - - using PermuteNode = model::operation::PermuteNode; - - // Find Permutation Type - auto type = [&]() { - auto input_layout = - operand.lower_info()->def_backends().getOnlyElement()->config()->getOperandLayout(); - auto output_layout = - out_operand.lower_info()->def_backends().getOnlyElement()->config()->getOperandLayout(); - - if (input_layout == graph::operand::Layout::NHWC && - output_layout == graph::operand::Layout::NCHW) - { - return PermuteNode::Type::NHWC_TO_NCHW; - } - else if (input_layout == graph::operand::Layout::NCHW && - output_layout == graph::operand::Layout::NHWC) - { - return PermuteNode::Type::NCHW_TO_NHWC; - } - else - { - return PermuteNode::Type::COPY; - } - }(); - - // Insert permute operation to the graph - auto insert_node = nnfw::cpp14::make_unique<PermuteNode>(operand_index, out_operand_index, type); - - auto node_index = _graph.operations().append(std::move(insert_node)); - const auto &node = _graph.operations().at(node_index); - - _graph.setLowerInfo(node_index, nnfw::cpp14::make_unique<graph::operation::LowerInfo>( - _graph.backend_resolver()->getDefaultBackend())); - - // Update Use/Def info - { - _graph.operands().at(operand_index).appendUse(node_index); - - auto node_out_indexes = node.getOutputs(); - auto node_out_index = node_out_indexes.at(model::operand::IO::Index{0}); - _graph.operands().at(node_out_index).appendDef(node_index); - } - return node_index; -} -} // namespace pass -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/pass/PermutationInsertionPass.h b/runtimes/neurun/src/graph/pass/PermutationInsertionPass.h deleted file mode 100644 index b2d417e82..000000000 --- a/runtimes/neurun/src/graph/pass/PermutationInsertionPass.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_PASS_PERMUTATION_INSERTION_PASS_H__ -#define __NEURUN_GRAPH_PASS_PERMUTATION_INSERTION_PASS_H__ - -#include "OperandPass.h" -#include "model/operand/Object.h" //for model::operation::Index - -namespace neurun -{ -namespace graph -{ -namespace pass -{ - -class PermutationInsertionPass : public OperandPass -{ -public: - using OperandPass::OperandPass; - -public: - virtual std::string id() override { return "PermutationInsertionPass"; } - virtual void callback(const model::operand::Index &index, model::operand::Object &object); - - /** - * @brief Insert Permute operation that has given operand as input - * - * @param operand_index is the target operand index for the insertion - * @param backend is the output operand's backend type - * - * @return model::operation::Index - */ - model::operation::Index insertPermute(const model::operand::Index &operand_index, - const backend::Backend *backend); - -private: -}; - -} // namespace pass -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_PASS_PERMUTATION_INSERTION_PASS_H__ diff --git a/runtimes/neurun/src/graph/verifier/Verifier.cc b/runtimes/neurun/src/graph/verifier/Verifier.cc deleted file mode 100644 index a5b53af85..000000000 --- a/runtimes/neurun/src/graph/verifier/Verifier.cc +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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 "Verifier.h" - -#include "graph/Graph.h" - -#include "util/logging.h" - -namespace neurun -{ -namespace graph -{ -namespace verifier -{ - -// -// DAGChecker -// - -bool DAGChecker::verify(const Graph &graph) const -{ - auto &operations = graph.operations(); - bool cyclic = false; - - std::unordered_map<model::operation::Index, bool> visited; - operations.iterate([&](const model::operation::Index &index, const model::operation::Node &) { - visited[index] = false; - }); - std::unordered_map<model::operation::Index, bool> on_stack = visited; // Copy from visited - - std::function<void(const model::operation::Index &index, const model::operation::Node &)> - dfs_recursive = - [&](const model::operation::Index &index, const model::operation::Node &node) -> void { - if (on_stack[index]) - cyclic = true; - if (visited[index]) - return; - visited[index] = true; - on_stack[index] = true; - - for (auto output : node.getOutputs()) - { - const auto &operand = graph.operands().at(output); - for (const auto &use : operand.getUses().list()) - { - dfs_recursive(use, graph.operations().at(use)); - } - } - - on_stack[index] = false; - }; - - operations.iterate(dfs_recursive); - - return !cyclic; -} - -// -// EdgeConsistencyVerifier -// - -bool EdgeConsistencyChecker::verify(const Graph &graph) const -{ - auto &operations = graph.operations(); - uint32_t mismatches = 0; - operations.iterate([&](const model::operation::Index &index, const model::operation::Node &node) { - for (auto operand_index : node.getInputs()) - { - auto &operand = graph.operands().at(operand_index); - mismatches += (operand.getUses().contains(index) ? 0 : 1); - } - for (auto operand_index : node.getOutputs()) - { - auto &operand = graph.operands().at(operand_index); - mismatches += (operand.getDef().contains(index) ? 0 : 1); - } - }); - return mismatches == 0; -} - -} // namespace verifier -} // namespace graph -} // namespace neurun diff --git a/runtimes/neurun/src/graph/verifier/Verifier.h b/runtimes/neurun/src/graph/verifier/Verifier.h deleted file mode 100644 index 5f1f79ee6..000000000 --- a/runtimes/neurun/src/graph/verifier/Verifier.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_GRAPH_VERIFIER_VERIFIER_H__ -#define __NEURUN_GRAPH_VERIFIER_VERIFIER_H__ - -namespace neurun -{ -namespace graph -{ -class Graph; -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace graph -{ -namespace verifier -{ - -struct IVerifier -{ - virtual ~IVerifier() = default; - virtual bool verify(const Graph &graph) const = 0; -}; - -} // namespace verifier -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace graph -{ -namespace verifier -{ - -class DAGChecker : public IVerifier -{ -public: - virtual bool verify(const Graph &graph) const override; -}; - -class EdgeConsistencyChecker : public IVerifier -{ -public: - virtual bool verify(const Graph &graph) const override; -}; - -} // namespace verifier -} // namespace graph -} // namespace neurun - -#endif // __NEURUN_GRAPH_VERIFIER_VERIFIER_H__ diff --git a/runtimes/neurun/src/kernel/CMakeLists.txt b/runtimes/neurun/src/kernel/CMakeLists.txt deleted file mode 100644 index a39823102..000000000 --- a/runtimes/neurun/src/kernel/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_subdirectory(cpu) -add_subdirectory(acl_cl) diff --git a/runtimes/neurun/src/kernel/acl_cl/CLFunction.h b/runtimes/neurun/src/kernel/acl_cl/CLFunction.h deleted file mode 100644 index f34210c8a..000000000 --- a/runtimes/neurun/src/kernel/acl_cl/CLFunction.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_ACL_CL_CL_FUNCTION_H__ -#define __NEURUN_KERNEL_ACL_CL_CL_FUNCTION_H__ - -#include "exec/interface/IFunction.h" -#include <arm_compute/runtime/IFunction.h> -#include <memory> - -namespace neurun -{ -namespace kernel -{ -namespace acl_cl -{ - -class CLFunction : public ::neurun::exec::IFunction -{ -public: - CLFunction() = delete; - -public: - CLFunction(std::unique_ptr<::arm_compute::IFunction> &&func) - : _func(std::forward<std::unique_ptr<::arm_compute::IFunction>>(func)) - { - // DO NOTHING - } - -public: - void run() override { _func->run(); } - void prepare() override { _func->prepare(); } - -private: - std::unique_ptr<::arm_compute::IFunction> _func; -}; - -} // namespace acl_cl -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_ACL_CL_CL_FUNCTION_H__ diff --git a/runtimes/neurun/src/kernel/acl_cl/CMakeLists.txt b/runtimes/neurun/src/kernel/acl_cl/CMakeLists.txt deleted file mode 100644 index 0658effea..000000000 --- a/runtimes/neurun/src/kernel/acl_cl/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -file(GLOB SOURCES "*.cc") - -add_library(${LIB_NEURUN_KERNEL_ACL_CL} STATIC ${SOURCES}) - -target_include_directories(${LIB_NEURUN_KERNEL_ACL_CL} PUBLIC ${NNFW_INCLUDE_DIR}) -target_include_directories(${LIB_NEURUN_KERNEL_ACL_CL} PUBLIC ${NEURUN_INCLUDE_DIR}) - -target_link_libraries(${LIB_NEURUN_KERNEL_ACL_CL} arm_compute) -target_link_libraries(${LIB_NEURUN_KERNEL_ACL_CL} nnfw_lib_misc) - -set_target_properties(${LIB_NEURUN_KERNEL_ACL_CL} PROPERTIES POSITION_INDEPENDENT_CODE ON) -set_target_properties(${LIB_NEURUN_KERNEL_ACL_CL} PROPERTIES OUTPUT_NAME kernel_acl_cl) -install(TARGETS ${LIB_NEURUN_KERNEL_ACL_CL} DESTINATION lib/neurun) diff --git a/runtimes/neurun/src/kernel/acl_cl/ConcatLayer.cc b/runtimes/neurun/src/kernel/acl_cl/ConcatLayer.cc deleted file mode 100644 index 3844317ab..000000000 --- a/runtimes/neurun/src/kernel/acl_cl/ConcatLayer.cc +++ /dev/null @@ -1,159 +0,0 @@ -/* - * 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 "ConcatLayer.h" - -#include <arm_compute/runtime/CL/CLScheduler.h> - -#include "util/feature/nchw/View.h" -#include "util/logging.h" - -namespace -{ - -bool matchSizeExceptAxis(const ::neurun::backend::acl_cl::operand::ICLTensor *t1, - const ::neurun::backend::acl_cl::operand::ICLTensor *t2, uint32_t axis) -{ - assert(t1->num_dimensions() <= 4); - assert(t2->num_dimensions() <= 4); - - for (uint32_t i = 0; i < 4; i++) - { - if (axis == i) - continue; - if (t1->dimension(i) != t2->dimension(i)) - return false; - } - return true; -} - -} // namespace {anonymous} - -namespace neurun -{ -namespace kernel -{ -namespace acl_cl -{ - -ConcatLayer::ConcatLayer() - : _input_allocs(), _output_alloc(nullptr), _axis(0), _input_type(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -bool ConcatLayer::concatenationFloat32() -{ - // Input and output size check - { - // NOTE Support only tensor with dimension 4 or less - - uint32_t axis_sum = 0; - - for (auto input : _input_allocs) - { - assert(matchSizeExceptAxis(_output_alloc, input, _axis)); - axis_sum += input->dimension(_axis); - } - - assert(_output_alloc->dimension(_axis) == axis_sum); - } - - VERBOSE(Concat_RUN) << "START Concat" << std::endl; - - // Perform operation - { - uint32_t axis_offset = 0; - - auto &queue = ::arm_compute::CLScheduler::get().queue(); - - _output_alloc->map(queue); - util::feature::nchw::View<float> output_view{_output_alloc}; - - for (auto input : _input_allocs) - { - input->map(queue); - const util::feature::nchw::View<float> input_reader{input}; - - for (uint32_t n = 0; n < input_reader.shape().N; n++) - { - for (uint32_t c = 0; c < input_reader.shape().C; c++) - { - for (uint32_t h = 0; h < input_reader.shape().H; h++) - { - for (uint32_t w = 0; w < input_reader.shape().W; w++) - { - uint32_t no = (_axis == 3) ? axis_offset : 0; - uint32_t co = (_axis == 2) ? axis_offset : 0; - uint32_t ho = (_axis == 1) ? axis_offset : 0; - uint32_t wo = (_axis == 0) ? axis_offset : 0; - output_view.at(n + no, c + co, h + ho, w + wo) = input_reader.at(n, c, h, w); - } - } - } - } - if (_axis == 3) - axis_offset += input_reader.shape().N; - if (_axis == 2) - axis_offset += input_reader.shape().C; - if (_axis == 1) - axis_offset += input_reader.shape().H; - if (_axis == 0) - axis_offset += input_reader.shape().W; - - input->unmap(queue); - } - _output_alloc->unmap(queue); - } - - VERBOSE(Concat_RUN) << "End Concat" << std::endl; - - return true; -} - -void ConcatLayer::configure( - const std::vector<::neurun::backend::acl_cl::operand::ICLTensor *> &input_allocs, int32_t axis, - ::neurun::backend::acl_cl::operand::ICLTensor *output_alloc) -{ - _input_allocs = input_allocs; - _output_alloc = output_alloc; - - assert(axis < 4); - - // This map converts NHWC to NCHW(reversed) - // NHWC -> WHCN - static const uint32_t axis_map[] = {3, 1, 0, 2}; - _axis = axis_map[axis]; - - // TODO Support Quant8 - _input_type = OperandType::TENSOR_FLOAT32; -} - -void ConcatLayer::run() -{ - if (_input_type == OperandType::TENSOR_FLOAT32) - { - concatenationFloat32(); - } - else if (_input_type == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error("NYI - concatenationQuant8()"); - } -} - -} // namespace acl_cl -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/acl_cl/ConcatLayer.h b/runtimes/neurun/src/kernel/acl_cl/ConcatLayer.h deleted file mode 100644 index d468a6dfb..000000000 --- a/runtimes/neurun/src/kernel/acl_cl/ConcatLayer.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_ACL_CL_CONCAT_LAYER_H__ -#define __NEURUN_KERNEL_ACL_CL_CONCAT_LAYER_H__ - -#include <NeuralNetworks.h> - -#include <arm_compute/runtime/IFunction.h> - -#include "model/operand/DataType.h" -#include "backend/acl_cl/operand/ICLTensor.h" - -using OperandType = neurun::model::operand::DataType; - -namespace neurun -{ -namespace kernel -{ -namespace acl_cl -{ - -// -// neurun::kernel::acl_cl::ConcatLayer -// A naive implementation of ConcatLayer for ACL -// - -class ConcatLayer : public ::arm_compute::IFunction -{ -public: - ConcatLayer(); - -public: - void configure(const std::vector<::neurun::backend::acl_cl::operand::ICLTensor *> &input_allocs, - int32_t axis /* NNAPI tensor axis from NHWC order */, - ::neurun::backend::acl_cl::operand::ICLTensor *output_alloc); - - void run(); - -private: - bool concatenationFloat32(); - -private: - std::vector<::neurun::backend::acl_cl::operand::ICLTensor *> _input_allocs; - ::neurun::backend::acl_cl::operand::ICLTensor *_output_alloc; - int32_t _axis; - OperandType _input_type; -}; - -} // namespace acl_cl -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_ACL_CL_CONCAT_LAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/AvgPoolLayer.cc b/runtimes/neurun/src/kernel/cpu/AvgPoolLayer.cc deleted file mode 100644 index f434a6dec..000000000 --- a/runtimes/neurun/src/kernel/cpu/AvgPoolLayer.cc +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * Copyright (C) 2017 The Android Open Source Project - * - * 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 "AvgPoolLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -#define AVGPOOLING_PARAMETERS \ - tflite::PoolParams op_params; \ - op_params.stride_height = _strideHeight; \ - op_params.stride_width = _strideWidth; \ - op_params.filter_height = _kernelHeight; \ - op_params.filter_width = _kernelWidth; \ - op_params.padding_values.height = (int8_t)_paddingTop; \ - op_params.padding_values.width = (int8_t)_paddingLeft; - -AvgPoolLayer::AvgPoolLayer() - : _inputData(nullptr), _outputData(nullptr), _inputShape(), _outputShape(), _paddingLeft(0), - _paddingTop(0), _paddingRight(0), _paddingBottom(0), _strideWidth(0), _strideHeight(0), - _kernelWidth(0), _kernelHeight(0), _activation(ANEURALNETWORKS_FUSED_NONE), - _inputType(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -bool AvgPoolLayer::averagePoolFloat32() -{ - AVGPOOLING_PARAMETERS - float output_activation_min, output_activation_max; - CalculateActivationRangeFloat(_activation, &output_activation_min, &output_activation_max); - op_params.float_activation_min = output_activation_min; - op_params.float_activation_max = output_activation_max; - - ::tflite::optimized_ops::AveragePool(op_params, convertShapeToTFLiteShape(_inputShape), - reinterpret_cast<const float *>(_inputData), - convertShapeToTFLiteShape(_outputShape), - reinterpret_cast<float *>(_outputData)); - return true; -} -bool AvgPoolLayer::averagePoolQuant8() -{ - AVGPOOLING_PARAMETERS - int32_t output_activation_min = 0; - int32_t output_activation_max = 0; - CalculateActivationRangeUint8(_activation, _outputShape, &output_activation_min, - &output_activation_max); - op_params.quantized_activation_min = output_activation_min; - op_params.quantized_activation_max = output_activation_max; - - ::tflite::optimized_ops::AveragePool(op_params, convertShapeToTFLiteShape(_inputShape), - _inputData, convertShapeToTFLiteShape(_outputShape), - _outputData); - return true; -} - -void AvgPoolLayer::configure(uint8_t *inputData, const Shape inputShape, const uint32_t paddingLeft, - const uint32_t paddingRight, const uint32_t paddingTop, - const uint32_t paddingBottom, const uint32_t strideWidth, - const uint32_t strideHeight, const uint32_t kernelWidth, - const uint32_t kernelHeight, const FuseCode activation, - uint8_t *outputData, const Shape outputShape) -{ - _inputData = inputData; - _inputShape = inputShape; - _inputType = inputShape.type; - _paddingLeft = paddingLeft; - _paddingRight = paddingRight; - _paddingTop = paddingTop; - _paddingBottom = paddingBottom; - _strideWidth = strideWidth; - _strideHeight = strideHeight; - _kernelWidth = kernelWidth; - _kernelHeight = kernelHeight; - _activation = activation; - _outputData = outputData; - _outputShape = outputShape; -} - -void AvgPoolLayer::run() -{ - if (_inputType == OperandType::TENSOR_FLOAT32) - { - averagePoolFloat32(); - } - else if (_inputType == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error{"AvgPoolLayer : Not tested for TENSOR_QUANT8_ASYMM"}; - // averagePoolQuant8(); - } -} - -#undef AVGPOOLING_PARAMETERS - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/AvgPoolLayer.h b/runtimes/neurun/src/kernel/cpu/AvgPoolLayer.h deleted file mode 100644 index 280f7ae5f..000000000 --- a/runtimes/neurun/src/kernel/cpu/AvgPoolLayer.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_AVGPOOLLAYER_H__ -#define __NEURUN_KERNEL_CPU_AVGPOOLLAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class AvgPoolLayer : public ::neurun::exec::IFunction -{ -public: - AvgPoolLayer(); - -public: - bool averagePoolFloat32(); - - bool averagePoolQuant8(); - - void configure(uint8_t *inputData, const Shape inputShape, const uint32_t paddingLeft, - const uint32_t paddingRight, const uint32_t paddingTop, - const uint32_t paddingBottom, const uint32_t strideWidth, - const uint32_t strideHeight, const uint32_t kernelWidth, - const uint32_t kernelHeight, const FuseCode activation, uint8_t *outputData, - const Shape outputShape); - - void run(); - -private: - uint8_t *_inputData; - uint8_t *_outputData; - - Shape _inputShape; - Shape _outputShape; - - uint32_t _paddingLeft; - uint32_t _paddingTop; - uint32_t _paddingRight; - uint32_t _paddingBottom; - - uint32_t _strideWidth; - uint32_t _strideHeight; - uint32_t _kernelWidth; - uint32_t _kernelHeight; - - FuseCode _activation; - - OperandType _inputType; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_AVGPOOLLAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/CMakeLists.txt b/runtimes/neurun/src/kernel/cpu/CMakeLists.txt deleted file mode 100644 index 436cb898c..000000000 --- a/runtimes/neurun/src/kernel/cpu/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -file(GLOB SOURCES "*.cc") - -add_library(${LIB_NEURUN_KERNEL_CPU} STATIC ${SOURCES}) - -target_include_directories(${LIB_NEURUN_KERNEL_CPU} PUBLIC ${NNFW_INCLUDE_DIR}) -target_include_directories(${LIB_NEURUN_KERNEL_CPU} PUBLIC ${NEURUN_INCLUDE_DIR}) -target_include_directories(${LIB_NEURUN_KERNEL_CPU} PUBLIC ${CMAKE_SOURCE_DIR}/externals/tensorflow) - -target_link_libraries(${LIB_NEURUN_KERNEL_CPU} tensorflow-lite) -target_link_libraries(${LIB_NEURUN_KERNEL_CPU} nnfw_lib_misc) - -set_target_properties(${LIB_NEURUN_KERNEL_CPU} PROPERTIES POSITION_INDEPENDENT_CODE ON) -set_target_properties(${LIB_NEURUN_KERNEL_CPU} PROPERTIES OUTPUT_NAME kernel_cpu) -install(TARGETS ${LIB_NEURUN_KERNEL_CPU} DESTINATION lib/neurun) diff --git a/runtimes/neurun/src/kernel/cpu/ConcatLayer.cc b/runtimes/neurun/src/kernel/cpu/ConcatLayer.cc deleted file mode 100644 index be093b437..000000000 --- a/runtimes/neurun/src/kernel/cpu/ConcatLayer.cc +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * Copyright (C) 2017 The Android Open Source Project - * - * 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 "ConcatLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ - -namespace cpu -{ - -ConcatLayer::ConcatLayer() - : _inputDataPtrs(), _outputData(nullptr), _axis(0), _inputShapes(), _outputShape(), - _inputType(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -bool ConcatLayer::concatenationFloat32() -{ - uint32_t num_inputs = _inputShapes.size(); - - tflite::ConcatenationParams op_params; - op_params.axis = _axis; - op_params.inputs_count = num_inputs; - - std::vector<::tflite::RuntimeShape *> inputDimsPtr; - std::vector<::tflite::RuntimeShape> inputDims; - inputDimsPtr.reserve(num_inputs); - inputDims.reserve(num_inputs); - - for (uint32_t i = 0; i < num_inputs; i++) - { - inputDims.push_back(convertShapeToTFLiteShape(_inputShapes[i])); - inputDimsPtr.push_back(&inputDims[i]); - } - - std::vector<const float *> inputFloatPtrs; - - for (auto ptr : _inputDataPtrs) - { - inputFloatPtrs.emplace_back(reinterpret_cast<const float *>(ptr)); - } - - ::tflite::optimized_ops::Concatenation<float>( - op_params, inputDimsPtr.data(), inputFloatPtrs.data(), - convertShapeToTFLiteShape(_outputShape), reinterpret_cast<float *>(_outputData)); - return true; -} -bool ConcatLayer::concatenationQuant8() -{ - int num_inputs = _inputShapes.size(); - - std::vector<int32_t> input_zeropoints(num_inputs); - std::vector<float> input_scales(num_inputs); - for (uint32_t i = 0; i < num_inputs; i++) - { - input_zeropoints[i] = _inputShapes[i].offset; - input_scales[i] = _inputShapes[i].scale; - } - - tflite::ConcatenationParams op_params; - op_params.axis = _axis; - op_params.inputs_count = num_inputs; - op_params.input_zeropoint = input_zeropoints.data(); - op_params.input_scale = input_scales.data(); - op_params.output_zeropoint = _outputShape.offset; - op_params.output_scale = _outputShape.scale; - - std::vector<::tflite::RuntimeShape *> inputDimsPtr; - std::vector<::tflite::RuntimeShape> inputDims; - inputDimsPtr.reserve(num_inputs); - inputDims.reserve(num_inputs); - for (uint32_t i = 0; i < num_inputs; i++) - { - inputDims.push_back(convertShapeToTFLiteShape(_inputShapes[i])); - inputDimsPtr.push_back(&inputDims[i]); - } - - ::tflite::optimized_ops::Concatenation<uint8_t>( - op_params, inputDimsPtr.data(), _inputDataPtrs.data(), - convertShapeToTFLiteShape(_outputShape), _outputData); - return true; -} - -void ConcatLayer::configure(const std::vector<const uint8_t *> &inputDataPtrs, - const std::vector<Shape> &inputShapes, int32_t axis, - uint8_t *outputData, const Shape outputShape) -{ - _inputDataPtrs = inputDataPtrs; - - for (auto shape : inputShapes) - { - _inputShapes.emplace_back(shape); - _inputType = shape.type; - } - - _axis = axis; - - _outputData = outputData; - _outputShape = outputShape; -} - -void ConcatLayer::run() -{ - if (_inputType == OperandType::TENSOR_FLOAT32) - { - concatenationFloat32(); - } - else if (_inputType == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error{"ConcatLayer : Not tested for TENSOR_QUANT8_ASYMM"}; - // concatenationQuant8(); - } -} - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/ConcatLayer.h b/runtimes/neurun/src/kernel/cpu/ConcatLayer.h deleted file mode 100644 index 64f813508..000000000 --- a/runtimes/neurun/src/kernel/cpu/ConcatLayer.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * Copyright (C) 2017 The Android Open Source Project - * - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_CONCATLAYER_H__ -#define __NEURUN_KERNEL_CPU_CONCATLAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class ConcatLayer : public ::neurun::exec::IFunction -{ -public: - ConcatLayer(); - -public: - bool concatenationFloat32(); - - bool concatenationQuant8(); - - void configure(const std::vector<const uint8_t *> &inputDataPtrs, - const std::vector<Shape> &inputShapes, int32_t axis, uint8_t *outputData, - const Shape outputShape); - - void run(); - -private: - std::vector<const uint8_t *> _inputDataPtrs; - uint8_t *_outputData; - - int32_t _axis; - - std::vector<Shape> _inputShapes; - Shape _outputShape; - - OperandType _inputType; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_CONCATLAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/ConvolutionLayer.cc b/runtimes/neurun/src/kernel/cpu/ConvolutionLayer.cc deleted file mode 100644 index c694fa75f..000000000 --- a/runtimes/neurun/src/kernel/cpu/ConvolutionLayer.cc +++ /dev/null @@ -1,228 +0,0 @@ -/* - * 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 "ConvolutionLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "kernel/cpu/OperationUtils.h" - -#include <mutex> - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -// If possible we will use this static buffer for the tensor. -static constexpr int kStaticBufferSize = 1605632; -static char static_scratch_buffer[kStaticBufferSize]; -static std::mutex executionMutex; - -#define ANDROID_NN_CONV_PARAMETERS(Type) \ - uint32_t height = getSizeOfDimension(_inputShape, 1); \ - uint32_t width = getSizeOfDimension(_inputShape, 2); \ - uint32_t kernelHeight = getSizeOfDimension(_kernelShape, 1); \ - uint32_t kernelWidth = getSizeOfDimension(_kernelShape, 2); \ - uint32_t outHeight = getSizeOfDimension(_outputShape, 1); \ - uint32_t outWidth = getSizeOfDimension(_outputShape, 2); \ - uint32_t inDepth = getSizeOfDimension(_inputShape, 3); \ - \ - uint32_t paddingHeight = (uint32_t)_paddingTop; \ - uint32_t paddingWidth = (uint32_t)_paddingLeft; \ - \ - Shape im2colShape; \ - im2colShape.dimensions.resize(4); \ - im2colShape.dimensions[0] = getSizeOfDimension(_outputShape, 0); \ - im2colShape.dimensions[1] = getSizeOfDimension(_outputShape, 1); \ - im2colShape.dimensions[2] = getSizeOfDimension(_outputShape, 2); \ - im2colShape.dimensions[3] = inDepth * kernelHeight * kernelWidth; \ - \ - Type *im2colData = nullptr; \ - uint64_t im2colByteSize = sizeof(Type); \ - std::unique_ptr<Type[]> im2colGuard; \ - for (int i = 0; i < 4; i++) \ - { \ - im2colByteSize *= im2colShape.dimensions[i]; \ - } \ - /* http://b/77982879, tflite::optimized_ops::Conv uses int for offsets */ \ - if (im2colByteSize >= 0x7fffffff) \ - { \ - std::cout << "Conv size is too large, not enough memory" << std::endl; \ - return false; \ - } \ - if (im2colByteSize <= kStaticBufferSize) \ - { \ - im2colData = reinterpret_cast<Type *>(static_scratch_buffer); \ - } \ - else \ - { \ - im2colData = new (std::nothrow) Type[im2colByteSize / sizeof(Type)]; \ - if (im2colData == nullptr) \ - { \ - std::cout << "Conv size is too large, not enough memory" << std::endl; \ - return false; \ - } \ - im2colGuard.reset(im2colData); \ - } - -ConvolutionLayer::ConvolutionLayer() - : _inputData(nullptr), _kernelData(nullptr), _outputData(nullptr), _biasData(nullptr), - _inputShape(), _kernelShape(), _outputShape(), _biasShape(), _paddingLeft(0), _paddingTop(0), - _paddingRight(0), _paddingBottom(0), _strideWidth(0), _strideHeight(0), - _activation(ANEURALNETWORKS_FUSED_NONE), _inputType(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -bool ConvolutionLayer::convFloat32() -{ - ANDROID_NN_CONV_PARAMETERS(float) - - const ::tflite::Dims<4> &kernel_dim = convertShapeToDims(_kernelShape); - const int kernel_width = ArraySize(kernel_dim, 1); - const int kernel_height = ArraySize(kernel_dim, 2); - const bool need_im2col = - _strideWidth != 1 || _strideHeight != 1 || kernel_width != 1 || kernel_height != 1; - - float *im2colDataToPass = nullptr; - if (need_im2col) - { - im2colDataToPass = im2colData; - } - - float output_activation_min, output_activation_max; - CalculateActivationRangeFloat(_activation, &output_activation_min, &output_activation_max); - int32_t dilationWidthFactor = 1, dilationHeightFactor = 1; - - ::tflite::ConvParams op_params; - op_params.padding_type = ::tflite::PaddingType::kSame; - op_params.padding_values.width = paddingWidth; - op_params.padding_values.height = paddingHeight; - op_params.stride_width = _strideWidth; - op_params.stride_height = _strideHeight; - op_params.dilation_width_factor = dilationWidthFactor; - op_params.dilation_height_factor = dilationHeightFactor; - op_params.float_activation_min = output_activation_min; - op_params.float_activation_max = output_activation_max; - - ::tflite::optimized_ops::Conv( - op_params, convertShapeToTFLiteShape(_inputShape), - reinterpret_cast<const float *>(_inputData), convertShapeToTFLiteShape(_kernelShape), - reinterpret_cast<const float *>(_kernelData), convertShapeToTFLiteShape(_biasShape), - reinterpret_cast<const float *>(_biasData), convertShapeToTFLiteShape(_outputShape), - reinterpret_cast<float *>(_outputData), convertShapeToTFLiteShape(im2colShape), - im2colDataToPass); - return true; -} - -bool ConvolutionLayer::convQuant8() -{ - ANDROID_NN_CONV_PARAMETERS(uint8_t) - - int32_t inputOffset = -_inputShape.offset; - int32_t kernelOffset = -_kernelShape.offset; - int32_t outputOffset = _outputShape.offset; - float real_multiplier = 0.0; - int32_t output_multiplier = 0; - int32_t output_shift = 0; - int32_t output_activation_min = 0; - int32_t output_activation_max = 0; - if (!GetQuantizedConvolutionMultipler(_inputShape, _kernelShape, _biasShape, _outputShape, - &real_multiplier) || - !QuantizeMultiplierSmallerThanOne(real_multiplier, &output_multiplier, &output_shift)) - { - return false; - } - CalculateActivationRangeUint8(_activation, _outputShape, &output_activation_min, - &output_activation_max); - int32_t dilationWidthFactor = 1, dilationHeightFactor = 1; - - ::tflite::ConvParams op_params; - op_params.padding_type = ::tflite::PaddingType::kSame; - op_params.padding_values.width = paddingWidth; - op_params.padding_values.height = paddingHeight; - op_params.stride_width = _strideWidth; - op_params.stride_height = _strideHeight; - op_params.dilation_width_factor = dilationWidthFactor; - op_params.dilation_height_factor = dilationHeightFactor; - op_params.input_offset = inputOffset; - op_params.weights_offset = kernelOffset; - op_params.output_offset = outputOffset; - op_params.output_multiplier = output_multiplier; - op_params.output_shift = output_shift; - op_params.quantized_activation_min = output_activation_min; - op_params.quantized_activation_max = output_activation_max; - - static gemmlowp::GemmContext gemm_context; - // Prevent concurrent executions that may access the scratch buffer and - // gemm_context. - std::unique_lock<std::mutex> lock(executionMutex); - // Alow gemmlowp automatically decide how many threads to use. - gemm_context.set_max_num_threads(0); - ::tflite::optimized_ops::Conv( - op_params, convertShapeToTFLiteShape(_inputShape), _inputData, - convertShapeToTFLiteShape(_kernelShape), _kernelData, convertShapeToTFLiteShape(_biasShape), - reinterpret_cast<const int32_t *>(_biasData), convertShapeToTFLiteShape(_outputShape), - _outputData, convertShapeToTFLiteShape(im2colShape), im2colData, &gemm_context); - return true; -} - -void ConvolutionLayer::configure(uint8_t *inputData, const Shape inputShape, uint8_t *kernelData, - const Shape kernelShape, uint8_t *biasData, const Shape biasShape, - const uint32_t paddingLeft, const uint32_t paddingRight, - const uint32_t paddingTop, const uint32_t paddingBottom, - const uint32_t strideWidth, const uint32_t strideHeight, - const FuseCode activation, uint8_t *outputData, - const Shape outputShape) -{ - _inputData = inputData; - _inputShape = inputShape; - _inputType = inputShape.type; - _kernelData = kernelData; - _kernelShape = kernelShape; - _biasData = biasData; - _biasShape = biasShape; - _paddingLeft = paddingLeft; - _paddingRight = paddingRight; - _paddingTop = paddingTop; - _paddingBottom = paddingBottom; - _strideWidth = strideWidth; - _strideHeight = strideHeight; - _activation = activation; - _outputData = outputData; - _outputShape = outputShape; -} - -void ConvolutionLayer::run() -{ - if (_inputType == OperandType::TENSOR_FLOAT32) - { - convFloat32(); - } - else if (_inputType == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error{"ConvolutionLayer : Not tested for TENSOR_QUANT8_ASYMM"}; - // convQuant8(); - } -} - -#undef ANDROID_NN_CONV_PARAMETERS - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/ConvolutionLayer.h b/runtimes/neurun/src/kernel/cpu/ConvolutionLayer.h deleted file mode 100644 index 9b7f55ff1..000000000 --- a/runtimes/neurun/src/kernel/cpu/ConvolutionLayer.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_CONVOLUTIONLAYER_H__ -#define __NEURUN_KERNEL_CPU_CONVOLUTIONLAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class ConvolutionLayer : public ::neurun::exec::IFunction -{ -public: - ConvolutionLayer(); - -public: - bool convFloat32(); - - bool convQuant8(); - - void configure(uint8_t *inputData, const Shape inputShape, uint8_t *kernelData, - const Shape kernelShape, uint8_t *biasData, const Shape biasShape, - const uint32_t paddingLeft, const uint32_t paddingRight, const uint32_t paddingTop, - const uint32_t paddingBottom, const uint32_t strideW, const uint32_t strideH, - const FuseCode activation, uint8_t *outputData, const Shape outputShape); - - void run(); - -private: - uint8_t *_inputData; - uint8_t *_kernelData; - uint8_t *_outputData; - uint8_t *_biasData; - - Shape _inputShape; - Shape _kernelShape; - Shape _outputShape; - Shape _biasShape; - - uint32_t _paddingLeft; - uint32_t _paddingTop; - uint32_t _paddingRight; - uint32_t _paddingBottom; - - uint32_t _strideWidth; - uint32_t _strideHeight; - - FuseCode _activation; - - OperandType _inputType; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_CONVOLUTIONLAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/FullyConnectedLayer.cc b/runtimes/neurun/src/kernel/cpu/FullyConnectedLayer.cc deleted file mode 100644 index abe82db5e..000000000 --- a/runtimes/neurun/src/kernel/cpu/FullyConnectedLayer.cc +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * Copyright (C) 2017 The Android Open Source Project - * - * 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 "FullyConnectedLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h" -#include "kernel/cpu/OperationUtils.h" - -#include <mutex> - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -FullyConnectedLayer::FullyConnectedLayer() - : _inputData(nullptr), _weightsData(nullptr), _biasData(nullptr), _outputData(nullptr), - _inputShape(), _weightsShape(), _biasShape(), _outputShape(), - _activation(ANEURALNETWORKS_FUSED_NONE), _inputType(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -// executionMutex is used to protect concurrent access of non-threadsafe resources -// like gemmlowp::GemmContext. -// std::mutex is safe for pthreads on Android. -static std::mutex executionMutex; -bool FullyConnectedLayer::fullyConnectedFloat32() -{ - int total_input_size = 1; - for (int i = 0; i < _inputShape.dimensions.size(); i++) - { - total_input_size *= _inputShape.dimensions[i]; - } - - int input_size = _weightsShape.dimensions[1]; - const int batch_size = total_input_size / input_size; - const int num_units = _weightsShape.dimensions[0]; - - TfLiteFusedActivation act = convertFusedActivation(_activation); - - ::tflite::tensor_utils::VectorBatchVectorAssign(reinterpret_cast<const float *>(_biasData), - num_units, batch_size, - reinterpret_cast<float *>(_outputData)); - - // Compute output += weight * input - ::tflite::tensor_utils::MatrixBatchVectorMultiplyAccumulate( - reinterpret_cast<const float *>(_weightsData), num_units, input_size, - reinterpret_cast<const float *>(_inputData), batch_size, - reinterpret_cast<float *>(_outputData), /*result_stride=*/1); - - // Apply activation function - ::tflite::tensor_utils::ApplyActivationToVector(reinterpret_cast<float *>(_outputData), - batch_size * num_units, act, - reinterpret_cast<float *>(_outputData)); - - return true; -} - -bool FullyConnectedLayer::fullyConnectedQuant8() -{ - throw std::runtime_error{"FullyConnectedLayer : Not tested for TENSOR_QUANT8_ASYMM"}; -} - -void FullyConnectedLayer::configure(uint8_t *inputData, const Shape inputShape, - uint8_t *weightsData, const Shape weightsShape, - uint8_t *biasData, const Shape biasShape, FuseCode activation, - uint8_t *outputData, const Shape outputShape) -{ - _inputData = inputData; - _inputShape = inputShape; - _inputType = inputShape.type; - _weightsData = weightsData; - _weightsShape = weightsShape; - _biasData = biasData; - _biasShape = biasShape; - _activation = activation; - _outputData = outputData; - _outputShape = outputShape; -} - -void FullyConnectedLayer::run() -{ - if (_inputType == OperandType::TENSOR_FLOAT32) - { - fullyConnectedFloat32(); - } - else if (_inputType == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error{"FullyConnectedLayer : Not tested for TENSOR_QUANT8_ASYMM"}; - // fullyConnectedQuant8(); - } -} - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/FullyConnectedLayer.h b/runtimes/neurun/src/kernel/cpu/FullyConnectedLayer.h deleted file mode 100644 index 20a388349..000000000 --- a/runtimes/neurun/src/kernel/cpu/FullyConnectedLayer.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_FULLYCONNECTEDLAYER_H__ -#define __NEURUN_KERNEL_CPU_FULLYCONNECTEDLAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class FullyConnectedLayer : public ::neurun::exec::IFunction -{ -public: - FullyConnectedLayer(); - -public: - bool fullyConnectedFloat32(); - - bool fullyConnectedQuant8(); - - void configure(uint8_t *inputData, const Shape inputShape, uint8_t *weightsData, - const Shape weightsShape, uint8_t *biasData, const Shape biasShape, - FuseCode activation, uint8_t *outputData, const Shape outputShape); - - void run(); - -private: - uint8_t *_inputData; - uint8_t *_weightsData; - uint8_t *_biasData; - uint8_t *_outputData; - - Shape _inputShape; - Shape _weightsShape; - Shape _biasShape; - Shape _outputShape; - - FuseCode _activation; - - OperandType _inputType; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_FULLYCONNECTEDLAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/MaxPoolLayer.cc b/runtimes/neurun/src/kernel/cpu/MaxPoolLayer.cc deleted file mode 100644 index c4a288b07..000000000 --- a/runtimes/neurun/src/kernel/cpu/MaxPoolLayer.cc +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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 "MaxPoolLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -#define MAXPOOLING_PARAMETERS \ - tflite::PoolParams op_params; \ - op_params.stride_height = _strideHeight; \ - op_params.stride_width = _strideWidth; \ - op_params.filter_height = _kernelHeight; \ - op_params.filter_width = _kernelWidth; \ - op_params.padding_values.height = (int8_t)_paddingTop; \ - op_params.padding_values.width = (int8_t)_paddingLeft; - -MaxPoolLayer::MaxPoolLayer() - : _inputData(nullptr), _outputData(nullptr), _inputShape(), _outputShape(), _paddingLeft(0), - _paddingTop(0), _paddingRight(0), _paddingBottom(0), _strideWidth(0), _strideHeight(0), - _kernelWidth(0), _kernelHeight(0), _activation(ANEURALNETWORKS_FUSED_NONE), - _inputType(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -bool MaxPoolLayer::maxPoolFloat32() -{ - MAXPOOLING_PARAMETERS - float output_activation_min, output_activation_max; - CalculateActivationRangeFloat(_activation, &output_activation_min, &output_activation_max); - op_params.float_activation_min = output_activation_min; - op_params.float_activation_max = output_activation_max; - - ::tflite::optimized_ops::MaxPool(op_params, convertShapeToTFLiteShape(_inputShape), - reinterpret_cast<const float *>(_inputData), - convertShapeToTFLiteShape(_outputShape), - reinterpret_cast<float *>(_outputData)); - return true; -} -bool MaxPoolLayer::maxPoolQuant8() -{ - MAXPOOLING_PARAMETERS - int32_t output_activation_min = 0; - int32_t output_activation_max = 0; - CalculateActivationRangeUint8(_activation, _outputShape, &output_activation_min, - &output_activation_max); - op_params.quantized_activation_min = output_activation_min; - op_params.quantized_activation_max = output_activation_max; - - ::tflite::optimized_ops::MaxPool(op_params, convertShapeToTFLiteShape(_inputShape), _inputData, - convertShapeToTFLiteShape(_outputShape), _outputData); - return true; -} - -void MaxPoolLayer::configure(uint8_t *inputData, const Shape inputShape, const uint32_t paddingLeft, - const uint32_t paddingRight, const uint32_t paddingTop, - const uint32_t paddingBottom, const uint32_t strideWidth, - const uint32_t strideHeight, const uint32_t kernelWidth, - const uint32_t kernelHeight, const FuseCode activation, - uint8_t *outputData, const Shape outputShape) -{ - _inputData = inputData; - - _inputShape = inputShape; - _inputType = inputShape.type; - _paddingLeft = paddingLeft; - _paddingRight = paddingRight; - _paddingTop = paddingTop; - _paddingBottom = paddingBottom; - _strideWidth = strideWidth; - _strideHeight = strideHeight; - _kernelWidth = kernelWidth; - _kernelHeight = kernelHeight; - _activation = activation; - _outputData = outputData; - _outputShape = outputShape; -} - -void MaxPoolLayer::run() -{ - if (_inputType == OperandType::TENSOR_FLOAT32) - { - maxPoolFloat32(); - } - else if (_inputType == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error{"MaxPoolLayer : Not tested for TENSOR_QUANT8_ASYMM"}; - // maxPoolQuant8(); - } -} - -#undef MAXPOOLING_PARAMETERS - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/MaxPoolLayer.h b/runtimes/neurun/src/kernel/cpu/MaxPoolLayer.h deleted file mode 100644 index 2b185550b..000000000 --- a/runtimes/neurun/src/kernel/cpu/MaxPoolLayer.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_MAXPOOLLAYER_H__ -#define __NEURUN_KERNEL_CPU_MAXPOOLLAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class MaxPoolLayer : public ::neurun::exec::IFunction -{ -public: - MaxPoolLayer(); - -public: - bool maxPoolFloat32(); - - bool maxPoolQuant8(); - - void configure(uint8_t *inputData, const Shape inputShape, const uint32_t paddingLeft, - const uint32_t paddingRight, const uint32_t paddingTop, - const uint32_t paddingBottom, const uint32_t strideWidth, - const uint32_t strideHeight, const uint32_t kernelWidth, - const uint32_t kernelHeight, const FuseCode activation, uint8_t *outputData, - const Shape outputShape); - - void run(); - -private: - uint8_t *_inputData; - uint8_t *_outputData; - - Shape _inputShape; - Shape _outputShape; - - uint32_t _paddingLeft; - uint32_t _paddingTop; - uint32_t _paddingRight; - uint32_t _paddingBottom; - - uint32_t _strideWidth; - uint32_t _strideHeight; - uint32_t _kernelWidth; - uint32_t _kernelHeight; - - FuseCode _activation; - - OperandType _inputType; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_MAXPOOLLAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/OperationUtils.cc b/runtimes/neurun/src/kernel/cpu/OperationUtils.cc deleted file mode 100644 index b28508c27..000000000 --- a/runtimes/neurun/src/kernel/cpu/OperationUtils.cc +++ /dev/null @@ -1,230 +0,0 @@ -/* - * 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 "kernel/cpu/OperationUtils.h" - -#include <cmath> -#include <algorithm> -#include <cassert> - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -uint32_t getNumberOfDimensions(const Shape &shape) { return shape.dimensions.size(); } - -uint32_t getNumberOfElements(const Shape &shape) -{ - uint32_t count = 1; - for (size_t i = 0; i < shape.dimensions.size(); i++) - { - count *= shape.dimensions[i]; - } - return count; -} - -uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx) -{ - if (dimensionIdx >= shape.dimensions.size()) - { - // TODO, log the error - return 0; - } - return shape.dimensions[dimensionIdx]; -} - -bool QuantizeMultiplierSmallerThanOne(double double_multiplier, int32_t *quantized_multiplier, - int32_t *right_shift) -{ - assert(double_multiplier >= 0.); - assert(double_multiplier < 1.); - if (double_multiplier == 0.) - { - *quantized_multiplier = 0; - *right_shift = 0; - return true; - } - assert(double_multiplier > 0.); - const double q = std::frexp(double_multiplier, right_shift); - *right_shift *= -1; - int64_t q_fixed = static_cast<int64_t>(std::round(q * (1ll << 31))); - assert(q_fixed <= (1ll << 31)); - if (q_fixed == (1ll << 31)) - { - q_fixed /= 2; - --*right_shift; - } - assert(*right_shift >= 0); - assert(q_fixed <= std::numeric_limits<int32_t>::max()); - *quantized_multiplier = static_cast<int32_t>(q_fixed); - return true; -} - -bool GetQuantizedConvolutionMultipler(const Shape &inputShape, const Shape &filterShape, - const Shape &biasShape, const Shape &outputShape, - float *multiplier) -{ - const float input_product_scale = inputShape.scale * filterShape.scale; - const float bias_scale = biasShape.scale; - const float output_scale = outputShape.scale; - // The following conditions must be guaranteed by the training pipeline. - assert(std::abs(input_product_scale - bias_scale) <= - 1e-6 * std::min(input_product_scale, bias_scale)); - assert(input_product_scale >= 0); - assert(input_product_scale < output_scale); - *multiplier = input_product_scale / output_scale; - return true; -} - -bool QuantizeMultiplierGreaterThanOne(double double_multiplier, int32_t *quantized_multiplier, - int *left_shift) -{ - assert(double_multiplier > 1.); - const double q = std::frexp(double_multiplier, left_shift); - int64_t q_fixed = static_cast<int64_t>(std::round(q * (1ll << 31))); - assert(q_fixed <= (1ll << 31)); - if (q_fixed == (1ll << 31)) - { - q_fixed /= 2; - ++*left_shift; - } - assert(*left_shift >= 0); - assert(q_fixed <= std::numeric_limits<int32_t>::max()); - *quantized_multiplier = static_cast<int32_t>(q_fixed); - return true; -} - -void CalculateActivationRangeFloat(int32_t activation, float *activation_min, float *activation_max) -{ - if (activation == ANEURALNETWORKS_FUSED_RELU) - { - *activation_min = 0.f; - *activation_max = std::numeric_limits<float>::max(); - } - else if (activation == ANEURALNETWORKS_FUSED_RELU6) - { - *activation_min = 0.f; - *activation_max = 6.f; - } - else if (activation == ANEURALNETWORKS_FUSED_RELU1) - { - *activation_min = -1.f; - *activation_max = 1.f; - } - else if (activation == ANEURALNETWORKS_FUSED_NONE) - { - *activation_min = std::numeric_limits<float>::lowest(); - *activation_max = std::numeric_limits<float>::max(); - } - else - { - std::cout << "Unsupported fused activation function." << std::endl; - } -} - -void CalculateActivationRangeUint8(int32_t activation, const Shape &outputShape, int32_t *act_min, - int32_t *act_max) -{ - const int32_t qmin = std::numeric_limits<uint8_t>::min(); - const int32_t qmax = std::numeric_limits<uint8_t>::max(); - const auto scale = outputShape.scale; - const auto zero_point = outputShape.offset; - auto quantize = [scale, zero_point](float f) { - return zero_point + static_cast<int32_t>(std::round(f / scale)); - }; - if (activation == ANEURALNETWORKS_FUSED_RELU) - { - *act_min = std::max(qmin, quantize(0.0)); - *act_max = qmax; - } - else if (activation == ANEURALNETWORKS_FUSED_RELU6) - { - *act_min = std::max(qmin, quantize(0.0)); - *act_max = std::min(qmax, quantize(6.0)); - } - else if (activation == ANEURALNETWORKS_FUSED_RELU1) - { - *act_min = std::max(qmin, quantize(-1.0)); - *act_max = std::min(qmax, quantize(1.0)); - } - else if (activation == ANEURALNETWORKS_FUSED_NONE) - { - *act_min = qmin; - *act_max = qmax; - } - else - { - std::cout << "Unsupported fused activation function." << std::endl; - } -} - -int32_t CalculateInputRadius(int input_integer_bits, int input_left_shift) -{ - const double max_input_rescaled = 1.0 * ((1 << input_integer_bits) - 1) * - (1ll << (31 - input_integer_bits)) / (1ll << input_left_shift); - // Tighten bound using floor. Suppose that we could use the exact value. - // After scaling the difference, the result would be at the maximum. Thus we - // must ensure that our value has lower magnitude. - return static_cast<int32_t>(std::floor(max_input_rescaled)); -} - -Shape getShape(const ::neurun::model::operand::Object &o) -{ - Shape shape; - - shape.type = static_cast<OperandType>(static_cast<int32_t>(o.typeInfo().type())); - shape.dimensions = std::vector<uint32_t>(o.shape().dims().begin(), o.shape().dims().end()); - shape.scale = o.typeInfo().scale(); - // shape.offset = _offset; - - return shape; -} - -size_t sizeOfData(OperandType type, const std::vector<uint32_t> &dimensions) -{ - size_t size = 4; - - switch (type) - { - case OperandType::SCALAR_FLOAT32: - case OperandType::SCALAR_INT32: - case OperandType::SCALAR_UINT32: - case OperandType::TENSOR_FLOAT32: - case OperandType::TENSOR_INT32: - size = 4; - break; - case OperandType::TENSOR_QUANT8_ASYMM: - size = 1; - break; - default: - throw std::runtime_error("Not supported operand type."); - break; - } - - for (auto d : dimensions) - { - size *= d; - } - - return size; -} - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/OperationUtils.h b/runtimes/neurun/src/kernel/cpu/OperationUtils.h deleted file mode 100644 index 3610990a5..000000000 --- a/runtimes/neurun/src/kernel/cpu/OperationUtils.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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. - */ - -#ifndef __NNFW_SUPPORT_NNAPI_OPERATION_UTILS_H__ -#define __NNFW_SUPPORT_NNAPI_OPERATION_UTILS_H__ - -#include <NeuralNetworks.h> - -#include <iostream> -#include <limits> -#include <vector> - -#include "tensorflow/contrib/lite/c/builtin_op_data.h" -#include "tensorflow/contrib/lite/kernels/internal/types.h" -#include "tensorflow/contrib/lite/kernels/internal/tensor.h" -#include "model/operand/Object.h" -#include "model/operand/DataType.h" - -using OperandType = neurun::model::operand::DataType; - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -struct Shape -{ - OperandType type; - std::vector<uint32_t> dimensions; - float scale; - int32_t offset; -}; - -uint32_t getNumberOfDimensions(const Shape &shape); - -uint32_t getNumberOfElements(const Shape &shape); - -uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx); - -inline ::tflite::Dims<4> convertShapeToDims(const Shape &shape) -{ - // nnAssert(shape.dimensions.size() <= 4); - ::tflite::Dims<4> dims; - // The dimensions are reversed in Dims<4>. - for (int i = 0; i < 4; ++i) - { - int src = static_cast<int>(shape.dimensions.size()) - i - 1; - if (src >= 0) - { - dims.sizes[i] = static_cast<int>(getSizeOfDimension(shape, src)); - } - else - { - dims.sizes[i] = 1; - } - } - dims.strides[0] = 1; - for (int i = 1; i < 4; i++) - { - dims.strides[i] = dims.strides[i - 1] * dims.sizes[i - 1]; - } - return dims; -} - -inline ::tflite::RuntimeShape convertShapeToTFLiteShape(const Shape &shape) -{ - std::vector<int32_t> raw_shape; - raw_shape.resize(4); - - for (uint32_t i = 0; i < 4; ++i) - { - if (i >= shape.dimensions.size()) - { - raw_shape[i] = 1; - } - else - { - raw_shape[i] = shape.dimensions[i]; - } - } - - return ::tflite::GetTensorShape(raw_shape); -} - -inline TfLiteFusedActivation convertFusedActivation(FuseCode act) -{ - if (act == ANEURALNETWORKS_FUSED_NONE) - { - return kTfLiteActNone; - } - - if (act == ANEURALNETWORKS_FUSED_RELU) - { - return kTfLiteActRelu; - } - - if (act == ANEURALNETWORKS_FUSED_RELU1) - { - return kTfLiteActRelu1; - } - - if (act == ANEURALNETWORKS_FUSED_RELU6) - { - return kTfLiteActRelu6; - } - - return kTfLiteActNone; -} - -__wur bool QuantizeMultiplierSmallerThanOne(double double_multiplier, int32_t *quantized_multiplier, - int32_t *right_shift); - -__wur bool GetQuantizedConvolutionMultipler(const Shape &inputShape, const Shape &filterShape, - const Shape &biasShape, const Shape &outputShape, - float *multiplier); -__wur bool QuantizeMultiplierGreaterThanOne(double double_multiplier, int32_t *quantized_multiplier, - int *left_shift); - -void CalculateActivationRangeFloat(int32_t activation, float *activation_min, - float *activation_max); - -void CalculateActivationRangeUint8(int32_t activation, const Shape &outputShape, int32_t *act_min, - int32_t *act_max); - -int32_t CalculateInputRadius(int input_integer_bits, int input_left_shift); - -Shape getShape(const ::neurun::model::operand::Object &o); - -uint32_t sizeOfData(OperandType type, const std::vector<uint32_t> &dimensions); - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NNFW_SUPPORT_NNAPI_OPERATION_UTILS_H__ diff --git a/runtimes/neurun/src/kernel/cpu/PermuteLayer.cc b/runtimes/neurun/src/kernel/cpu/PermuteLayer.cc deleted file mode 100644 index ba8c5ab92..000000000 --- a/runtimes/neurun/src/kernel/cpu/PermuteLayer.cc +++ /dev/null @@ -1,201 +0,0 @@ -/* - * 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 "PermuteLayer.h" - -#include "util/feature/nhwc/Reader.h" -#include "util/feature/nhwc/View.h" -#include "util/feature/nchw/View.h" -#include "util/feature/Coordinate4D.h" - -#include <misc/feature/IndexIterator.h> - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -using Type = model::operation::PermuteNode::Type; - -void PermuteLayer::configure(std::shared_ptr<::neurun::backend::operand::IObject> input, - std::shared_ptr<::neurun::backend::operand::IObject> output, - const model::operand::Shape &shape, Type type) -{ - _input = input; - _output = output; - _shape = shape; - _type = type; -} - -void PermuteLayer::run() -{ - auto rank = _shape.rank(); - - switch (_type) - { - case Type::NHWC_TO_NCHW: - { - auto fn = [&](::neurun::backend::operand::ITensor &tensor) { - auto input_tensor = _input->ptr(); - - auto input_buffer = input_tensor->buffer(); - auto input_size = input_tensor->total_size(); - - auto output_buffer = tensor.buffer(); - auto output_size = tensor.total_size(); - switch (rank) - { - case 0: - case 1: - { - memcpy(output_buffer, input_buffer, input_size); - break; - } - case 2: - { - auto matrix_shape = _shape.asMatrix(); - - for (auto h = 0; h < matrix_shape.H; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, 0}; - memcpy(output_buffer + tensor.calcOffset(coord), input_buffer + h * matrix_shape.W, - matrix_shape.W * sizeof(float)); - } - break; - } - case 3: - { - const int32_t depth = _shape.dim(0); - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); - - for (auto c = 0; c < depth; ++c) - { - for (auto h = 0; h < height; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, c}; - memcpy(output_buffer + tensor.calcOffset(coord), - input_buffer + c * height * width + h * width, width * sizeof(float)); - } - } - break; - } - case 4: - { - auto feature = _shape.asFeature(); - - const util::feature::nhwc::Reader<float> from{ - feature, reinterpret_cast<const float *>(input_buffer), input_size}; - util::feature::nchw::View<float> into{&tensor}; - - ::nnfw::misc::feature::iterate(feature) - << [&](uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(batch, ch, row, col); - into.at(batch, ch, row, col) = value; - }; - break; - } - default: - throw "NYI"; - break; - } - }; - _output->access(fn); - break; - } - case Type::NCHW_TO_NHWC: - { - auto fn = [&](::neurun::backend::operand::ITensor &tensor) { - auto input_buffer = tensor.buffer(); - auto input_size = tensor.total_size(); - - auto output_tensor = _output->ptr(); - - auto output_buffer = output_tensor->buffer(); - auto output_size = output_tensor->total_size(); - - switch (rank) - { - case 0: - case 1: - { - memcpy(output_buffer, input_buffer, output_size); - break; - } - case 2: - { - auto matrix_shape = _shape.asMatrix(); - - for (auto h = 0; h < matrix_shape.H; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, 0}; - memcpy(output_buffer + h * matrix_shape.W, input_buffer + tensor.calcOffset(coord), - matrix_shape.W * sizeof(float)); - } - break; - } - case 3: - { - const int32_t depth = _shape.dim(0); - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); - - for (auto c = 0; c < depth; ++c) - { - for (auto h = 0; h < height; ++h) - { - neurun::util::feature::Coordinate4D coord{0, h, 0, c}; - memcpy(output_buffer + c * height * width + h * width, - input_buffer + tensor.calcOffset(coord), width * sizeof(float)); - } - } - break; - } - case 4: - { - auto feature = _shape.asFeature(); - - const util::feature::nchw::View<float> from{&tensor}; - util::feature::nhwc::View<float> into{feature, reinterpret_cast<float *>(output_buffer), - output_size}; - - ::nnfw::misc::feature::iterate(feature) - << [&](uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(batch, ch, row, col); - into.at(batch, ch, row, col) = value; - }; - break; - } - default: - throw "NYI"; - break; - } - }; - _input->access(fn); - break; - } - case Type::COPY: - // If two different backends using same tensor layout, we need this. - throw "NYI"; - break; - } -} - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/PermuteLayer.h b/runtimes/neurun/src/kernel/cpu/PermuteLayer.h deleted file mode 100644 index d9e1709bc..000000000 --- a/runtimes/neurun/src/kernel/cpu/PermuteLayer.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_PERMUTE_LAYER_H__ -#define __NEURUN_KERNEL_CPU_PERMUTE_LAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "util/feature/nhwc/View.h" -#include "OperationUtils.h" -#include "backend/interface/operand/IObject.h" -#include "model/operation/PermuteNode.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class PermuteLayer : public ::neurun::exec::IFunction -{ -public: - PermuteLayer() = default; - -public: - void configure(std::shared_ptr<::neurun::backend::operand::IObject> input, - std::shared_ptr<::neurun::backend::operand::IObject> output, - const model::operand::Shape &shape, model::operation::PermuteNode::Type type); - void run(); - -private: - std::shared_ptr<::neurun::backend::operand::IObject> _input; - std::shared_ptr<::neurun::backend::operand::IObject> _output; - model::operand::Shape _shape; - model::operation::PermuteNode::Type _type; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_PERMUTE_LAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/ReshapeLayer.cc b/runtimes/neurun/src/kernel/cpu/ReshapeLayer.cc deleted file mode 100644 index 377f783e0..000000000 --- a/runtimes/neurun/src/kernel/cpu/ReshapeLayer.cc +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * Copyright (C) 2017 The Android Open Source Project - * - * 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 "ReshapeLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h" -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -ReshapeLayer::ReshapeLayer() - : _inputData(nullptr), _outputData(nullptr), _inputShape(), _outputShape() -{ - // DO NOTHING -} - -bool ReshapeLayer::reshapeGeneric() -{ - size_t count = sizeOfData(_inputShape.type, _inputShape.dimensions); - memcpy(reinterpret_cast<void *>(_outputData), reinterpret_cast<const void *>(_inputData), count); - return true; -} - -void ReshapeLayer::configure(uint8_t *inputData, const Shape &inputShape, uint8_t *outputData, - const Shape &outputShape) -{ - _inputData = inputData; - _inputShape = inputShape; - _outputData = outputData; - _outputShape = outputShape; -} - -void ReshapeLayer::run() { reshapeGeneric(); } - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/ReshapeLayer.h b/runtimes/neurun/src/kernel/cpu/ReshapeLayer.h deleted file mode 100644 index 51d0bacee..000000000 --- a/runtimes/neurun/src/kernel/cpu/ReshapeLayer.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_RESHAPELAYER_H__ -#define __NEURUN_KERNEL_CPU_RESHAPELAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class ReshapeLayer : public ::neurun::exec::IFunction -{ -public: - ReshapeLayer(); - -public: - bool reshapeGeneric(); - - void configure(uint8_t *inputData, const Shape &inputShape, uint8_t *outputData, - const Shape &outputShape); - - void run(); - -private: - uint8_t *_inputData; - uint8_t *_outputData; - - Shape _inputShape; - Shape _outputShape; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_RESHAPELAYER_H__ diff --git a/runtimes/neurun/src/kernel/cpu/SoftMaxLayer.cc b/runtimes/neurun/src/kernel/cpu/SoftMaxLayer.cc deleted file mode 100644 index c998c65f6..000000000 --- a/runtimes/neurun/src/kernel/cpu/SoftMaxLayer.cc +++ /dev/null @@ -1,174 +0,0 @@ -/* - * 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 "SoftMaxLayer.h" - -#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h" -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -SoftMaxLayer::SoftMaxLayer() - : _inputData(nullptr), _outputData(nullptr), _beta(0.0), _inputShape(), _outputShape(), - _inputType(OperandType::SCALAR_FLOAT32) -{ - // DO NOTHING -} - -// Performs softmax along the input of size (input_size * batch_size). -void Softmax(const float *in, const int input_size, const int batch_size, const float beta, - float *out) -{ - TF_LITE_ASSERT(input_size > 0); - - // For each batch - for (int b = 0; b < batch_size; b++) - { - // Find the max coeff. - float max_coeff = in[0]; - for (int i = 1; i < input_size; i++) - { - if (in[i] > max_coeff) - max_coeff = in[i]; - } - - // Compute the normalized sum of exps. - float exp_sum = 0.0; - for (int i = 0; i < input_size; i++) - { - out[i] = std::exp((in[i] - max_coeff) * beta); - exp_sum += out[i]; - } - - // Divide by the sum of exps. - float reciprocal_sum_exp = 1.f / exp_sum; - for (int i = 0; i < input_size; i++) - { - out[i] *= reciprocal_sum_exp; - } - - // Advance in and out pointers for the next batch. - in += input_size; - out += input_size; - } -} - -bool SoftMaxLayer::softmaxFloat32() -{ - Shape shapeIn4D; - - if (getNumberOfDimensions(_inputShape) == 2) - { - uint32_t batch_size = getSizeOfDimension(_inputShape, 0); - uint32_t input_size = getNumberOfElements(_inputShape) / batch_size; - Softmax(reinterpret_cast<const float *>(_inputData), input_size, batch_size, _beta, - reinterpret_cast<float *>(_outputData)); - } - else if (getNumberOfDimensions(_inputShape) == 4) - { - ::tflite::SoftmaxParams op_params; - op_params.beta = _beta; - ::tflite::optimized_ops::Softmax(op_params, convertShapeToTFLiteShape(_inputShape), - reinterpret_cast<const float *>(_inputData), - convertShapeToTFLiteShape(_outputShape), - reinterpret_cast<float *>(_outputData)); - } - else - { - std::cout << "only 2D and 4D tensors supported" << std::endl; - return false; - } - - return true; -} - -bool SoftMaxLayer::softmaxQuant8() -{ - Shape shapeIn4D = _inputShape; - - if (getNumberOfDimensions(_inputShape) == 2) - { - uint32_t batch_size = getSizeOfDimension(_inputShape, 0); - uint32_t input_size = getNumberOfElements(_inputShape) / batch_size; - shapeIn4D.dimensions = {batch_size, 1, 1, input_size}; - } - else if (getNumberOfDimensions(_inputShape) == 4) - { - shapeIn4D = _inputShape; - } - else - { - std::cout << "only 2D and 4D tensors supported" << std::endl; - return false; - } - if (_outputShape.offset != 0 || _outputShape.scale != 1.f / 256) - { - std::cout << "incorrect scale / offset for output" << std::endl; - return false; - } - static const int32_t kScaledDiffIntegerBits = 5; - const double input_beta_real_multiplier = std::min( - 1.0 * _beta * _inputShape.scale * (1 << (31 - kScaledDiffIntegerBits)), (1ll << 31) - 1.0); - int32_t input_multiplier = 0; - int32_t input_left_shift = 0; - if (!QuantizeMultiplierGreaterThanOne(input_beta_real_multiplier, &input_multiplier, - &input_left_shift)) - { - return false; - } - float diff_min = -1.0f * CalculateInputRadius(kScaledDiffIntegerBits, input_left_shift); - - ::tflite::SoftmaxParams op_params; - op_params.input_multiplier = input_multiplier; - op_params.input_left_shift = input_left_shift; - op_params.diff_min = diff_min; - ::tflite::optimized_ops::Softmax(op_params, convertShapeToTFLiteShape(shapeIn4D), _inputData, - convertShapeToTFLiteShape(shapeIn4D), _outputData); - return true; -} - -void SoftMaxLayer::configure(uint8_t *inputData, const Shape &inputShape, const float beta, - uint8_t *outputData, const Shape &outputShape) -{ - _inputData = inputData; - _inputShape = inputShape; - _inputType = inputShape.type; - _outputData = outputData; - _outputShape = outputShape; - _beta = beta; -} - -void SoftMaxLayer::run() -{ - if (_inputType == OperandType::TENSOR_FLOAT32) - { - softmaxFloat32(); - } - else if (_inputType == OperandType::TENSOR_QUANT8_ASYMM) - { - throw std::runtime_error{"SoftMaxLayer : Not tested for TENSOR_QUANT8_ASYMM"}; - // softmaxQuant8(); - } -} - -} // namespace cpu -} // namespace kernel -} // namespace neurun diff --git a/runtimes/neurun/src/kernel/cpu/SoftMaxLayer.h b/runtimes/neurun/src/kernel/cpu/SoftMaxLayer.h deleted file mode 100644 index df1aa4044..000000000 --- a/runtimes/neurun/src/kernel/cpu/SoftMaxLayer.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_KERNEL_CPU_SOFTMAXLAYER_H__ -#define __NEURUN_KERNEL_CPU_SOFTMAXLAYER_H__ - -#include <NeuralNetworks.h> - -#include "exec/interface/IFunction.h" - -#include "kernel/cpu/OperationUtils.h" - -namespace neurun -{ -namespace kernel -{ -namespace cpu -{ - -class SoftMaxLayer : public ::neurun::exec::IFunction -{ -public: - SoftMaxLayer(); - -public: - bool softmaxFloat32(); - - bool softmaxQuant8(); - - void configure(uint8_t *inputData, const Shape &inputShape, const float beta, uint8_t *outputData, - const Shape &outputShape); - - void run(); - -private: - uint8_t *_inputData; - uint8_t *_outputData; - - float _beta; - - Shape _inputShape; - Shape _outputShape; - - OperandType _inputType; -}; - -} // namespace cpu -} // namespace kernel -} // namespace neurun - -#endif // __NEURUN_KERNEL_CPU_SOFTMAXLAYER_H__ diff --git a/runtimes/neurun/src/library_info.cc b/runtimes/neurun/src/library_info.cc deleted file mode 100644 index 4adf70465..000000000 --- a/runtimes/neurun/src/library_info.cc +++ /dev/null @@ -1,17 +0,0 @@ -/* - * 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. - */ - -volatile static const char info[] = "library information : runtime=neurun"; diff --git a/runtimes/neurun/src/linear/Linear.cc b/runtimes/neurun/src/linear/Linear.cc deleted file mode 100644 index 6452bbd49..000000000 --- a/runtimes/neurun/src/linear/Linear.cc +++ /dev/null @@ -1,199 +0,0 @@ -/* - * 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 <algorithm> - -#include "Linear.h" - -#include "graph/Graph.h" - -#include "graph/operation/LowerInfo.h" -#include "backend/interface/IStageGenerator.h" -#include "backend/interface/IConfig.h" -#include "compiler/SubTensorInfo.h" -#include "compiler/TensorInfo.h" - -#include "util/logging.h" - -namespace neurun -{ -namespace linear -{ - -Linear::Linear(const graph::Graph &graph) : _graph(graph) -{ - // Linearize with topological sort - // - // Topological sort algorithm - // 1. Iterate with DFS - // 2. Append the node to vector when DFS for the node finishes(post order) - // 3. Reverse the order of nodes - - graph::Graph::PostDfsConstIterator().iterate( - graph, [&](const model::operation::Index &index, const model::operation::Node &node) { - const auto lower_info = graph.getLowerInfo(index); - _operations.emplace_back(&node, lower_info); - }); - - std::reverse(std::begin(_operations), std::end(_operations)); -} - -void Linear::accept(model::operation::NodeVisitor &&visitor) const -{ - for (const auto op : _operations) - { - op.node->accept(std::move(visitor)); - } -} - -backend::TensorBuilderSet Linear::planTensors() -{ - using ITensorBuilderPtr = std::shared_ptr<backend::ITensorBuilder>; - using FnOnTensorBuilder = - std::function<void(const model::operand::Index &ind, ITensorBuilderPtr)>; - - const auto &operands = _graph.operands(); - auto iterTensorBuilders = [&operands](const model::operand::Index &ind, FnOnTensorBuilder fn) { - const auto &obj = operands.at(ind); - for (auto backend : obj.lower_info()->def_backends()) - { - auto tensor_builder = backend->tensor_builder(); - fn(ind, tensor_builder); - } - }; - - backend::TensorBuilderSet tensor_builders; - - std::unordered_map<model::operand::Index, uint32_t> uses_map; - std::vector<model::operand::Index> constants; - - _graph.operands().iterate( - [&](const model::operand::Index &ind, const model::operand::Object &obj) { - uses_map[ind] = obj.getUses().size(); - - // If a tensor is a constant, increase the use of the tensor. - // It makes the tensor not be dealloced. - if (obj.getUsage() == model::operand::OperandUsage::CONSTANT) - { - constants.push_back(ind); - uses_map[ind]++; - } - - for (auto backend : obj.lower_info()->def_backends()) - { - bool isSubTensor = false; - auto tensor_builder = backend->tensor_builder(); - - if (backend->config()->SupportSubTensorAlloc()) - { - const auto parentInfo = obj.parent_info(); - if (parentInfo != nullptr) - { - isSubTensor = true; - } - } - - if (isSubTensor) - { - const compiler::SubTensorInfo info(obj); - tensor_builder->registerSubTensorInfo(ind, info); - } - else - { - const auto info = compiler::TensorInfo(obj.shape(), obj.typeInfo()); - tensor_builder->registerTensorInfo(ind, info); - } - - // Prepare tensor builders to be returned - tensor_builders.insert(tensor_builder); - } - }); - - // If a tensor is model output, increase the use of the tensor. - // This aim is same to above one. - for (const auto &ind : _graph.getOutputs()) - { - uses_map[ind]++; - } - - // Allocate constant operands first - VERBOSE(LINEAR) << "TENSORS as CONSTANT" << std::endl; - for (const auto &ind : constants) - { - iterTensorBuilders(ind, [](const model::operand::Index &ind, ITensorBuilderPtr tensor_builder) { - tensor_builder->notifyFirstUse(ind); - }); - } - - // Allocate Model's inputs - VERBOSE(LINEAR) << "TENSORS as MODEL INPUT" << std::endl; - for (const auto &ind : _graph.getInputs()) - { - iterTensorBuilders(ind, [](const model::operand::Index &ind, ITensorBuilderPtr tensor_builder) { - tensor_builder->notifyFirstUse(ind); - }); - } - - // At each operation, - // 1. Scan USE of inputs. Decrease the USE and deallocate if the USE is 0 - // 2. Scan DEF of outputs. If the DEF, allocate it - VERBOSE(LINEAR) << "TENSORS" << std::endl; - for (const auto op : _operations) - { - for (const auto &ind : op.node->getOutputs()) - { - const auto &obj = operands.at(ind); - if (obj.getDef().size()) - { - iterTensorBuilders(ind, - [](const model::operand::Index &ind, ITensorBuilderPtr tensor_builder) { - tensor_builder->notifyFirstUse(ind); - }); - } - } - - for (const auto &ind : op.node->getInputs()) - { - uses_map[ind]--; - if (uses_map[ind] == 0) - { - iterTensorBuilders(ind, - [](const model::operand::Index &ind, ITensorBuilderPtr tensor_builder) { - tensor_builder->notifyLastUse(ind); - }); - } - } - } - - // Now, model outputs should be not deallocated - assert(std::all_of(_graph.getOutputs().begin(), _graph.getOutputs().end(), - [&uses_map](const model::operand::Index &ind) { return uses_map[ind] > 0; })); - - // Set subtensor information - // Todo: move this phase outside as optimization phase - return tensor_builders; -} - -void Linear::iterate(const std::function<void(const Element &element)> &fn) const -{ - for (const auto op : _operations) - { - fn(op); - } -} - -} // namespace linear -} // namespace neurun diff --git a/runtimes/neurun/src/linear/Linear.h b/runtimes/neurun/src/linear/Linear.h deleted file mode 100644 index fb3f539d4..000000000 --- a/runtimes/neurun/src/linear/Linear.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_LINEAR_LINEAR_H__ -#define __NEURUN_LINEAR_LINEAR_H__ - -#include <vector> - -#include "model/operation/Node.h" -#include "backend/interface/ITensorBuilder.h" - -namespace neurun -{ -namespace graph -{ -namespace operation -{ -struct NodeVisitor; -} // namespace operation -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace graph -{ -class Graph; -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace linear -{ - -struct Element -{ - const model::operation::Node *node; - const graph::operation::LowerInfo *lower_info; - - Element(const model::operation::Node *node, const graph::operation::LowerInfo *lower_info) - : node{node}, lower_info{lower_info} - { - } -}; - -class Linear -{ -public: - Linear(const graph::Graph &graph); - -public: - Linear(const Linear &linear) = delete; - -public: - void accept(model::operation::NodeVisitor &&visitor) const; - - // TODO Should not return TensorBuilderSet - backend::TensorBuilderSet planTensors(); - - void iterate(const std::function<void(const Element &element)> &fn) const; - -private: - const graph::Graph &_graph; - std::vector<Element> _operations; -}; - -} // namespace linear -} // namespace neurun - -#endif // __NEURUN_LINEAR_LINEAR_H__ diff --git a/runtimes/neurun/src/model/operand/Data.h b/runtimes/neurun/src/model/operand/Data.h deleted file mode 100644 index 506cb185a..000000000 --- a/runtimes/neurun/src/model/operand/Data.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_DATA_H__ -#define __NEURUN_MODEL_OPERAND_DATA_H__ - -#include <algorithm> - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -struct Data -{ - virtual ~Data() = default; - - virtual size_t size(void) const = 0; - virtual const uint8_t *base(void) const = 0; -}; - -class CachedData final : public Data -{ -public: - CachedData(const uint8_t *base, size_t size) : _base{new uint8_t[size]}, _size{size} - { - std::copy(base, base + size, _base); - } - -public: - ~CachedData() { delete[] _base; } - -public: - size_t size(void) const override { return _size; } - const uint8_t *base(void) const override { return _base; } - -private: - uint8_t *_base; - size_t _size; -}; - -class ExternalData final : public Data -{ -public: - ExternalData(const uint8_t *base, size_t size) : _base{base}, _size{size} - { - // DO NOTHING - } - -public: - size_t size(void) const override { return _size; } - const uint8_t *base(void) const override { return _base; } - -private: - const uint8_t *_base; - const size_t _size; -}; - -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_DATA_H__ diff --git a/runtimes/neurun/src/model/operand/DataType.h b/runtimes/neurun/src/model/operand/DataType.h deleted file mode 100644 index d75a0dbf1..000000000 --- a/runtimes/neurun/src/model/operand/DataType.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_DATATYPE_H__ -#define __NEURUN_MODEL_OPERAND_DATATYPE_H__ - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -enum class DataType -{ - SCALAR_FLOAT32 = 0, - SCALAR_INT32 = 1, - SCALAR_UINT32 = 2, - - TENSOR_FLOAT32 = 3, - TENSOR_INT32 = 4, - - TENSOR_QUANT8_ASYMM = 5, -}; - -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_DATATYPE_H__ diff --git a/runtimes/neurun/src/model/operand/Index.h b/runtimes/neurun/src/model/operand/Index.h deleted file mode 100644 index 1c84ba451..000000000 --- a/runtimes/neurun/src/model/operand/Index.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_INDEX_H__ -#define __NEURUN_MODEL_OPERAND_INDEX_H__ - -#include "graph/Index.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -using Index = ::neurun::graph::Index<uint32_t, struct IndexTag>; - -} // namespace operand -} // namespace model -} // namespace neurun - -namespace neurun -{ -namespace model -{ -namespace operand -{ -namespace IO -{ - -using Index = ::neurun::graph::Index<uint32_t, struct IndexTag>; - -} // namespace IO -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_INDEX_H__ diff --git a/runtimes/neurun/src/model/operand/IndexSet.cc b/runtimes/neurun/src/model/operand/IndexSet.cc deleted file mode 100644 index b83d314e4..000000000 --- a/runtimes/neurun/src/model/operand/IndexSet.cc +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 "IndexSet.h" - -#include <algorithm> - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -IndexSet::IndexSet(std::initializer_list<Index> list) : _set(list) -{ - // DO NOTHING -} - -IndexSet::IndexSet(std::initializer_list<int32_t> list) -{ - for (auto val : list) - { - _set.emplace_back(static_cast<uint32_t>(val)); - } -} - -IndexSet::IndexSet(std::initializer_list<uint32_t> list) -{ - for (auto val : list) - { - _set.emplace_back(val); - } -} - -bool IndexSet::contains(const Index &index) const -{ - return std::find(_set.begin(), _set.end(), index) != _set.end(); -} - -void IndexSet::replace(const Index &from, const Index &to) -{ - std::replace(_set.begin(), _set.end(), from, to); -} - -} // namespace operand -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/IndexSet.h b/runtimes/neurun/src/model/operand/IndexSet.h deleted file mode 100644 index e8827de9c..000000000 --- a/runtimes/neurun/src/model/operand/IndexSet.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_INDEX_SET_H__ -#define __NEURUN_MODEL_OPERAND_INDEX_SET_H__ - -#include <initializer_list> -#include <vector> - -#include "Index.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -class IndexSet -{ -public: - IndexSet(void) = default; - IndexSet(std::initializer_list<Index> list); - IndexSet(std::initializer_list<int32_t> list); - IndexSet(std::initializer_list<uint32_t> list); - -public: - void append(const Index &index) { _set.emplace_back(index); } - -public: - uint32_t size() const { return static_cast<uint32_t>(_set.size()); } - const Index &at(IO::Index set_index) const { return _set.at(set_index.asInt()); } - const Index &at(uint32_t index) const { return _set.at(index); } - bool contains(const Index &index) const; - void replace(const Index &from, const Index &to); - -public: - std::vector<Index>::const_iterator begin(void) const { return _set.begin(); } - std::vector<Index>::const_iterator end(void) const { return _set.end(); } - -private: - std::vector<Index> _set; -}; - -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_INDEX_SET_H__ diff --git a/runtimes/neurun/src/model/operand/Object.cc b/runtimes/neurun/src/model/operand/Object.cc deleted file mode 100644 index 63cf29bd3..000000000 --- a/runtimes/neurun/src/model/operand/Object.cc +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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 "Object.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -size_t Object::operandSize(void) const -{ - const uint32_t ranks = _shape.rank(); - int32_t elements = 1; - - for (uint32_t rank = 0; rank < ranks; rank++) - { - elements *= _shape.dim(rank); - } - - DataType type = _type.type(); - size_t element_size = 0; - - // Value of type is matched with OperandCode enum in NeuralNetworks.h - switch (type) - { - case DataType::SCALAR_FLOAT32: - case DataType::TENSOR_FLOAT32: - element_size = sizeof(float); - break; - case DataType::SCALAR_INT32: - case DataType::TENSOR_INT32: - element_size = sizeof(int32_t); - break; - case DataType::SCALAR_UINT32: - element_size = sizeof(uint32_t); - break; - case DataType::TENSOR_QUANT8_ASYMM: - element_size = sizeof(uint8_t); - break; - default: - throw std::runtime_error{"Unsuppported type size"}; - } - - return element_size * elements; -} - -bool Object::setUsage(const OperandUsage usage) -{ - if (usageIsDefined() && (_usage != usage)) - { - // Already set as different type - return false; - } - - _usage = usage; - - return true; -} - -void Object::appendUse(const ::neurun::model::operation::Index &idx) -{ - assert(_usage != OperandUsage::NOT_DEFINED); - assert(!_uses.contains(idx)); - - _uses.append(idx); -} - -void Object::removeUse(const ::neurun::model::operation::Index &idx) -{ - assert(_usage != OperandUsage::NOT_DEFINED); - assert(_uses.contains(idx)); - - _uses.remove(idx); -} - -void Object::appendDef(const ::neurun::model::operation::Index &idx) -{ - assert(_usage != OperandUsage::NOT_DEFINED && _usage != OperandUsage::CONSTANT); - assert(_def.size() == 0); - - _def.append(idx); -} - -void Object::removeDef(const ::neurun::model::operation::Index &idx) -{ - assert(_usage != OperandUsage::NOT_DEFINED); - assert(_def.contains(idx)); - - _def.remove(idx); -} - -void Object::lower_info(std::unique_ptr<graph::operand::LowerInfo> &&lower_info) -{ - _lower_info = std::move(lower_info); -} - -const graph::operand::LowerInfo *Object::lower_info() const { return _lower_info.get(); } - -graph::operand::LowerInfo *Object::lower_info() { return _lower_info.get(); } - -void Object::parent_info(std::unique_ptr<graph::operand::ParentInfo> &&parent_info) -{ - _parent_info = std::move(parent_info); -} - -const graph::operand::ParentInfo *Object::parent_info() const { return _parent_info.get(); } - -graph::operand::ParentInfo *Object::parent_info() { return _parent_info.get(); } - -} // namespace operand -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/Object.h b/runtimes/neurun/src/model/operand/Object.h deleted file mode 100644 index eb5f6275e..000000000 --- a/runtimes/neurun/src/model/operand/Object.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_OBJECT_H__ -#define __NEURUN_MODEL_OPERAND_OBJECT_H__ - -#include <cassert> -#include <cstdint> -#include <memory> -#include <algorithm> - -#include "Shape.h" -#include "Data.h" -#include "TypeInfo.h" -#include "graph/operand/LowerInfo.h" // TODO Remove this dependency -#include "graph/operand/ParentInfo.h" // TODO Remove this dependency -#include "model/operation/IndexList.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -// Operand usage should be exact one of these -enum class OperandUsage -{ - NOT_DEFINED, - MODEL_INPUT, - CONSTANT, - OPERATION_OUTPUT, -}; - -class Object -{ -public: - explicit Object(const Shape &shape, const TypeInfo &type) - : _shape{shape}, _type{type}, _usage{OperandUsage::NOT_DEFINED} - { - // DO NOTHING - } - -public: - const Shape &shape(void) const { return _shape; } - const TypeInfo &typeInfo(void) const { return _type; } - size_t operandSize(void) const; - bool setAsConstant() { return setUsage(OperandUsage::CONSTANT); } - bool setAsModelInput() { return setUsage(OperandUsage::MODEL_INPUT); } - bool setAsOperationOutput() { return setUsage(OperandUsage::OPERATION_OUTPUT); } - bool usageIsDefined(void) const { return _usage != OperandUsage::NOT_DEFINED; } - bool isModelInput(void) const { return _usage == OperandUsage::MODEL_INPUT; } - OperandUsage getUsage() const { return _usage; } - - const operation::IndexList &getUses() const { return _uses; } - const operation::IndexList &getDef() const { return _def; } - void appendUse(const operation::Index &idx); - void removeUse(const operation::Index &idx); - void appendDef(const operation::Index &idx); - void removeDef(const operation::Index &idx); - -private: - bool setUsage(OperandUsage usage); - -public: - void data(std::unique_ptr<Data> &&data) { _data = std::move(data); } - const Data &data(void) const { return *_data; } - -public: - template <typename T, typename... Args> void data(Args &&... args) - { - data(std::unique_ptr<T>(new T{std::forward<Args>(args)...})); - } - -public: - template <typename T> T asScalar(void) const - { - assert((_shape.rank() == 0) || ((_shape.rank() == 1) && (_shape.dim(0) == 1))); - assert(_data != nullptr); - assert((_data->base() != nullptr) && (_data->size() == sizeof(T))); - - return *(reinterpret_cast<const T *>(_data->base())); - } - -public: - void lower_info(std::unique_ptr<graph::operand::LowerInfo> &&lower_info); - const graph::operand::LowerInfo *lower_info() const; - graph::operand::LowerInfo *lower_info(); - /** - * @brief Set parent information - * @param[in] parent_info Parent information - */ - void parent_info(std::unique_ptr<graph::operand::ParentInfo> &&parent_info); - /** - * @brief Return parent information pointer as constant - * @return Parent information pointer - */ - const graph::operand::ParentInfo *parent_info() const; - /** - * @brief Return parent information pointer - * @return Perent information pointer - */ - graph::operand::ParentInfo *parent_info(); - -private: - const Shape _shape; - const TypeInfo _type; - std::unique_ptr<Data> _data; - OperandUsage _usage; - - operation::IndexList _uses; - operation::IndexList _def; // size is 0 (constant) or 1 (from def operation) - - std::unique_ptr<graph::operand::LowerInfo> _lower_info; - std::unique_ptr<graph::operand::ParentInfo> _parent_info; -}; - -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_OBJECT_H__ diff --git a/runtimes/neurun/src/model/operand/Set.cc b/runtimes/neurun/src/model/operand/Set.cc deleted file mode 100644 index d93c21514..000000000 --- a/runtimes/neurun/src/model/operand/Set.cc +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 "Set.h" - -#include "cpp14/memory.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -const Index Set::generateIndex() -{ - assert((_index_count) <= 0x7fffffff); - - return Index{_index_count++}; -} - -Index Set::append(const Shape &shape, const TypeInfo &type) -{ - auto index = generateIndex(); - - _objects[index] = nnfw::cpp14::make_unique<Object>(shape, type); - - return index; -} - -const Object &Set::at(const Index &index) const { return *(_objects.at(index)); } - -Object &Set::at(const Index &index) { return *(_objects.at(index)); } - -bool Set::exist(const Index &index) const { return index.value() < _objects.size(); } - -void Set::iterate(const std::function<void(const Index &, const Object &)> &fn) const -{ - for (const auto &e : _objects) - { - fn(e.first, *e.second); - } -} - -void Set::iterate(const std::function<void(const Index &, Object &)> &fn) -{ - // TODO Remove this workaround - // This implementation is a workaround in case of adding operands while iteration - // - // // Original Implementation (We probably should be back to this) - // for (auto &e : _objects) - // { - // fn(e.first, *e.second); - // } - - std::list<Index> l; - - for (auto &e : _objects) - { - l.push_back(e.first); - } - - for (auto index : l) - { - fn(index, *_objects[index]); - } -} - -} // namespace operand -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/Set.h b/runtimes/neurun/src/model/operand/Set.h deleted file mode 100644 index 9dff7ec3c..000000000 --- a/runtimes/neurun/src/model/operand/Set.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_SET_H__ -#define __NEURUN_MODEL_OPERAND_SET_H__ - -#include <memory> -#include <unordered_map> - -#include "Object.h" -#include "Index.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -class Set -{ -public: - Set() : _index_count(0) {} - -public: - Index append(const Shape &, const TypeInfo &); - void remove(const Index &index) { _objects.erase(index); }; - -public: - const Object &at(const Index &) const; - Object &at(const Index &); - bool exist(const Index &) const; - void iterate(const std::function<void(const Index &, const Object &)> &fn) const; - void iterate(const std::function<void(const Index &, Object &)> &fn); - -private: - const Index generateIndex(); - -private: - std::unordered_map<Index, std::unique_ptr<Object>> _objects; - uint32_t _index_count; -}; - -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_SET_H__ diff --git a/runtimes/neurun/src/model/operand/Shape.cc b/runtimes/neurun/src/model/operand/Shape.cc deleted file mode 100644 index f74c48d88..000000000 --- a/runtimes/neurun/src/model/operand/Shape.cc +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 <cassert> - -#include "Shape.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -Shape::Shape(uint32_t rank) { _dims.resize(rank); } - -int32_t Shape::asVector(void) const -{ - assert(rank() == 1); - - return dim(0); -} - -nnfw::misc::matrix::Shape Shape::asMatrix(void) const -{ - assert(rank() == 2); - - const auto height = dim(0); - const auto width = dim(1); - - return nnfw::misc::matrix::Shape(height, width); -} - -nnfw::misc::feature::Shape Shape::asFeature(void) const -{ - assert(rank() == 4); - - // Feature Map in NNAPI - // - Dimension(0) -> Batch - // - Dimension(1) -> Height - // - Dimension(2) -> Width - // - Dimension(3) -> Depth - const auto batch = dim(0); - const auto depth = dim(3); - const auto height = dim(1); - const auto width = dim(2); - - return nnfw::misc::feature::Shape(batch, depth, height, width); -} - -nnfw::misc::kernel::Shape Shape::asKernel(void) const -{ - assert(rank() == 4); - - // Convolution Kernel in NNAPI - // - Dimension(0) -> Count - // - Dimension(1) -> Height - // - Dimension(2) -> Width - // - Dimension(3) -> Depth - const auto count = dim(0); - const auto depth = dim(3); - const auto height = dim(1); - const auto width = dim(2); - - return nnfw::misc::kernel::Shape(count, depth, height, width); -} - -nnfw::misc::tensor::Shape Shape::asTensor(void) const -{ - nnfw::misc::tensor::Shape shape{}; - for (uint32_t i = 0; i < rank(); ++i) - { - shape.append(dim(i)); - } - - return shape; // this shape represents shape of NNAPI -} - -} // namespace operand -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/Shape.h b/runtimes/neurun/src/model/operand/Shape.h deleted file mode 100644 index b80f647d5..000000000 --- a/runtimes/neurun/src/model/operand/Shape.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_SHAPE_H__ -#define __NEURUN_MODEL_OPERAND_SHAPE_H__ - -#include <vector> -#include <cstdint> - -#include "misc/feature/Shape.h" -#include "misc/kernel/Shape.h" -#include "misc/matrix/Shape.h" -#include "misc/tensor/Shape.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -struct Shape -{ -public: - Shape(uint32_t rank = 0); - -public: - uint32_t rank(void) const { return _dims.size(); } - -public: - int32_t dim(uint32_t n) const { return _dims.at(n); } - int32_t &dim(uint32_t n) { return _dims.at(n); } - const std::vector<int32_t> &dims() const { return _dims; } - -public: - int32_t asVector(void) const; - nnfw::misc::matrix::Shape asMatrix(void) const; - nnfw::misc::feature::Shape asFeature(void) const; - nnfw::misc::kernel::Shape asKernel(void) const; - nnfw::misc::tensor::Shape asTensor(void) const; - -private: - std::vector<int32_t> _dims; -}; - -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_SHAPE_H__ diff --git a/runtimes/neurun/src/model/operand/TypeInfo.cc b/runtimes/neurun/src/model/operand/TypeInfo.cc deleted file mode 100644 index 0b9f63c93..000000000 --- a/runtimes/neurun/src/model/operand/TypeInfo.cc +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 "TypeInfo.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -DataType TypeInfo::typeFromOperandCode(OperandCode type) -{ - // Now neurun::model::operand::DataType share same enum value with OperandCode - // in NeuralNetworks.h. - return static_cast<DataType>(static_cast<uint32_t>(type)); -} - -} // namespace operand -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operand/TypeInfo.h b/runtimes/neurun/src/model/operand/TypeInfo.h deleted file mode 100644 index d16172a09..000000000 --- a/runtimes/neurun/src/model/operand/TypeInfo.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERAND_TYPEINFO_H__ -#define __NEURUN_MODEL_OPERAND_TYPEINFO_H__ - -#include <cstdint> - -#include <NeuralNetworks.h> - -#include "DataType.h" - -namespace neurun -{ -namespace model -{ -namespace operand -{ - -class TypeInfo -{ -public: - TypeInfo() = default; - - TypeInfo(OperandCode type, float scale, int32_t offset) - : _type(typeFromOperandCode(type)), _scale(scale), _offset(offset) - { - // DO NOTHING - } - -public: - DataType type() const { return _type; } - float scale() const { return _scale; } - int32_t offset() const { return _offset; } - -private: - // Now neurun::model::operand::DataType share same enum value with OperandCode - // in NeuralNetworks.h. - // If we don't share same value, we must fix this mapping function. - DataType typeFromOperandCode(OperandCode type); - -private: - DataType _type; - float _scale; - int32_t _offset; -}; -} // namespace operand -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERAND_TYPEINFO_H__ diff --git a/runtimes/neurun/src/model/operation/AddNode.cc b/runtimes/neurun/src/model/operation/AddNode.cc deleted file mode 100644 index 0c9d4e09b..000000000 --- a/runtimes/neurun/src/model/operation/AddNode.cc +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 "AddNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void AddNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -AddNode::AddNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(2u)} -{ - assert(init_param.input_count == 2); - assert(init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // 0 -> Lefthand side operand - // 1 -> Righthand side operand - - setInputs({init_param.inputs[0], init_param.inputs[1]}); - setOutputs({init_param.outputs[0]}); -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/AddNode.h b/runtimes/neurun/src/model/operation/AddNode.h deleted file mode 100644 index 533fb0ab3..000000000 --- a/runtimes/neurun/src/model/operation/AddNode.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_ADD_NODE_H__ -#define __NEURUN_MODEL_OPERATION_ADD_NODE_H__ - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class AddNode : public model::operation::Node -{ -public: - AddNode(const model::operation::Node::InitParam &init_param); - - enum Input - { - LHS = 0, - RHS - }; - - struct Param - { - operand::Index activation_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "Add"; } -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_ADD_H__ diff --git a/runtimes/neurun/src/model/operation/AvgPool2DNode.cc b/runtimes/neurun/src/model/operation/AvgPool2DNode.cc deleted file mode 100644 index 8c688e60a..000000000 --- a/runtimes/neurun/src/model/operation/AvgPool2DNode.cc +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 "AvgPool2DNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void AvgPool2DNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -AvgPool2DNode::AvgPool2DNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(1u)} -{ - assert(init_param.input_count == 7); - assert(init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // 0 -> IFM Tensor Index - // 1 -> Padding Code (ANEURALNETWORKS_PADDING_SAME or ANEURALNETWORKS_PADDING_VALID) Index - // 2 -> Horizontal (over width) Stride Index - // 3 -> Vertial (over height) Stride Index - // 4 -> Filter Width Index - // 5 -> Filter Height Index - // 6 -> FuseCode (activation) Index - - setInputs({init_param.inputs[0]}); - setOutputs({init_param.outputs[0]}); - - _param.padding_index = operand::Index{init_param.inputs[1]}; - _param.hstride_index = operand::Index{init_param.inputs[2]}; - _param.vstride_index = operand::Index{init_param.inputs[3]}; - - _param.kw_index = operand::Index{init_param.inputs[4]}; - _param.kh_index = operand::Index{init_param.inputs[5]}; - _param.activation_index = operand::Index{init_param.inputs[6]}; -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/AvgPool2DNode.h b/runtimes/neurun/src/model/operation/AvgPool2DNode.h deleted file mode 100644 index e66e6146e..000000000 --- a/runtimes/neurun/src/model/operation/AvgPool2DNode.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_AVGPOOL2D_NODE_H__ -#define __NEURUN_MODEL_OPERATION_AVGPOOL2D_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class AvgPool2DNode : public model::operation::Node -{ -public: - AvgPool2DNode(const model::operation::Node::InitParam &init_param); - - enum Input - { - INPUT = 0 - }; - - struct Param - { - operand::Index kw_index; - operand::Index kh_index; - - operand::Index hstride_index; - operand::Index vstride_index; - - operand::Index padding_index; - operand::Index activation_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "AvgPool2D"; } - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_AVGPOOL2D_H__ diff --git a/runtimes/neurun/src/model/operation/ConcatNode.cc b/runtimes/neurun/src/model/operation/ConcatNode.cc deleted file mode 100644 index 23cfef294..000000000 --- a/runtimes/neurun/src/model/operation/ConcatNode.cc +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 "ConcatNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void ConcatNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -ConcatNode::ConcatNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createAtLeast(2u)} -{ - assert(init_param.input_count >= 2); // At least one one input tensor and axis - assert(init_param.output_count == 1); - - // When there are N + 1 inputs, each input should be interpreted as follows: - // - // [0, N) -> Input tensors - // N -> Axis - // - - { - operand::IndexSet inds; - for (uint32_t n = 0; n < init_param.input_count - 1; ++n) - { - inds.append(operand::Index{init_param.inputs[n]}); - } - setInputs(inds); - } - setOutputs({init_param.outputs[0]}); - - _param.axis_index = operand::Index{init_param.inputs[init_param.input_count - 1]}; -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/ConcatNode.h b/runtimes/neurun/src/model/operation/ConcatNode.h deleted file mode 100644 index b69ee2f23..000000000 --- a/runtimes/neurun/src/model/operation/ConcatNode.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_CONCAT_NODE_H__ -#define __NEURUN_MODEL_OPERATION_CONCAT_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class ConcatNode : public model::operation::Node -{ -public: - ConcatNode(const model::operation::Node::InitParam &init_param); - - struct Param - { - operand::Index axis_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "Concat"; } - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_CONCAT_H__ diff --git a/runtimes/neurun/src/model/operation/Conv2DNode.cc b/runtimes/neurun/src/model/operation/Conv2DNode.cc deleted file mode 100644 index 7eb2b183d..000000000 --- a/runtimes/neurun/src/model/operation/Conv2DNode.cc +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 "Conv2DNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void Conv2DNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -Conv2DNode::Conv2DNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(3u)} -{ - assert(init_param.input_count == 7 && init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // - // 0 -> IFM Tensor Index - // 1 -> Kernel Tensor Index - // 2 -> Bias Tensor Index - // 3 -> Padding Code (ANEURALNETWORKS_PADDING_SAME or ANEURALNETWORKS_PADDING_VALID) Index - // 4 -> Stride (width) Index - // 5 -> Stride (height) INdex - // 6 -> Activation Index - - setInputs({init_param.inputs[0], init_param.inputs[1], init_param.inputs[2]}); - setOutputs({init_param.outputs[0]}); - - _param.padding_index = operand::Index{init_param.inputs[3]}; - _param.hstride_index = operand::Index{init_param.inputs[4]}; - _param.vstride_index = operand::Index{init_param.inputs[5]}; - _param.activation_index = operand::Index{init_param.inputs[6]}; -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/Conv2DNode.h b/runtimes/neurun/src/model/operation/Conv2DNode.h deleted file mode 100644 index 34a95f0d9..000000000 --- a/runtimes/neurun/src/model/operation/Conv2DNode.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_CONV2D_NODE_H__ -#define __NEURUN_MODEL_OPERATION_CONV2D_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class Conv2DNode : public model::operation::Node -{ -public: - Conv2DNode(const model::operation::Node::InitParam &); - - enum Input - { - INPUT = 0, - KERNEL, - BIAS - }; - - struct Param - { - operand::Index hstride_index; - operand::Index vstride_index; - - operand::Index padding_index; - operand::Index activation_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "Conv2D"; } - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_CONV2D_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/FullyConnectedNode.cc b/runtimes/neurun/src/model/operation/FullyConnectedNode.cc deleted file mode 100644 index 0fde5182d..000000000 --- a/runtimes/neurun/src/model/operation/FullyConnectedNode.cc +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 "FullyConnectedNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void FullyConnectedNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -FullyConnectedNode::FullyConnectedNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(3u)} -{ - assert(init_param.input_count == 4 && init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // 0 -> A tensor, specifying the input. - // 1 -> A 2-D tensor, specifying the weights - // 2 -> A 1-D tensor, specifying the bias - // 3 -> An INT32 value, and has to be one of the FuseCode values - - setInputs({init_param.inputs[0], init_param.inputs[1], init_param.inputs[2]}); - setOutputs({init_param.outputs[0]}); - - _param.activation_index = operand::Index{init_param.inputs[3]}; -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/FullyConnectedNode.h b/runtimes/neurun/src/model/operation/FullyConnectedNode.h deleted file mode 100644 index 9820ddc8c..000000000 --- a/runtimes/neurun/src/model/operation/FullyConnectedNode.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_FULLYCONNECTED_NODE_H__ -#define __NEURUN_MODEL_OPERATION_FULLYCONNECTED_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class FullyConnectedNode : public model::operation::Node -{ -public: - FullyConnectedNode(const model::operation::Node::InitParam &init_param); - - enum Input - { - INPUT = 0, - WEIGHT, - BIAS - }; - - struct Param - { - operand::Index activation_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "FullyConnected"; } - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_FULLYCONNECTED_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/Index.h b/runtimes/neurun/src/model/operation/Index.h deleted file mode 100644 index e03dd74d6..000000000 --- a/runtimes/neurun/src/model/operation/Index.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_INDEX_H__ -#define __NEURUN_MODEL_OPERATION_INDEX_H__ - -#include "graph/Index.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -using Index = ::neurun::graph::Index<uint32_t, struct IndexTag>; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_INDEX_H__ diff --git a/runtimes/neurun/src/model/operation/IndexList.cc b/runtimes/neurun/src/model/operation/IndexList.cc deleted file mode 100644 index e46987036..000000000 --- a/runtimes/neurun/src/model/operation/IndexList.cc +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 "IndexList.h" - -#include <algorithm> - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -IndexList::IndexList(std::initializer_list<Index> list) : _list(list) -{ - // DO NOTHING -} - -bool IndexList::contains(const ::neurun::model::operation::Index &index) const -{ - return std::find(_list.begin(), _list.end(), index) != _list.end(); -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/IndexList.h b/runtimes/neurun/src/model/operation/IndexList.h deleted file mode 100644 index c0af29829..000000000 --- a/runtimes/neurun/src/model/operation/IndexList.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_INDEX_LIST_H__ -#define __NEURUN_MODEL_OPERATION_INDEX_LIST_H__ - -#include <initializer_list> -#include <list> - -#include "Index.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class IndexList -{ -public: - IndexList(void) = default; - IndexList(std::initializer_list<Index> list); - -public: - void append(const Index &index) { _list.push_back(index); } - void remove(const Index &index) { _list.remove(index); } - -public: - uint32_t size() const { return static_cast<uint32_t>(_list.size()); } - const std::list<Index> &list() const { return _list; } - bool contains(const Index &index) const; - -private: - std::list<Index> _list; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_INDEX_LIST_H__ diff --git a/runtimes/neurun/src/model/operation/MaxPool2DNode.cc b/runtimes/neurun/src/model/operation/MaxPool2DNode.cc deleted file mode 100644 index 3d3686b0e..000000000 --- a/runtimes/neurun/src/model/operation/MaxPool2DNode.cc +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 "MaxPool2DNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void MaxPool2DNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -MaxPool2DNode::MaxPool2DNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(1u)} -{ - assert(init_param.input_count == 7); - assert(init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // 0 -> IFM Tensor Index - // 1 -> Padding Code (ANEURALNETWORKS_PADDING_SAME or ANEURALNETWORKS_PADDING_VALID) Index - // 2 -> Horizontal (over width) Stride Index - // 3 -> Vertial (over height) Stride Index - // 4 -> Filter Width Index - // 5 -> Filter Height Index - // 6 -> FuseCode (activation) Index - - setInputs({init_param.inputs[0]}); - setOutputs({init_param.outputs[0]}); - - _param.padding_index = operand::Index{init_param.inputs[1]}; - _param.hstride_index = operand::Index{init_param.inputs[2]}; - _param.vstride_index = operand::Index{init_param.inputs[3]}; - - _param.kw_index = operand::Index{init_param.inputs[4]}; - _param.kh_index = operand::Index{init_param.inputs[5]}; - _param.activation_index = operand::Index{init_param.inputs[6]}; -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/MaxPool2DNode.h b/runtimes/neurun/src/model/operation/MaxPool2DNode.h deleted file mode 100644 index 96d1210a7..000000000 --- a/runtimes/neurun/src/model/operation/MaxPool2DNode.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_MAXPOOL2D_NODE_H__ -#define __NEURUN_MODEL_OPERATION_MAXPOOL2D_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class MaxPool2DNode : public model::operation::Node -{ -public: - MaxPool2DNode(const model::operation::Node::InitParam &init_param); - - enum Input - { - INPUT = 0 - }; - - struct Param - { - operand::Index kw_index; - operand::Index kh_index; - - operand::Index hstride_index; - operand::Index vstride_index; - - operand::Index padding_index; - operand::Index activation_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "MaxPool2D"; } - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_MAXPOOL2D_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/Node.Include.h b/runtimes/neurun/src/model/operation/Node.Include.h deleted file mode 100644 index 95e78c7b5..000000000 --- a/runtimes/neurun/src/model/operation/Node.Include.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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. - */ - -// This file has no ifdef guard intentionally - -#include "Conv2DNode.h" -#include "MaxPool2DNode.h" -#include "AvgPool2DNode.h" -#include "ConcatNode.h" -#include "ReshapeNode.h" -#include "FullyConnectedNode.h" -#include "SoftmaxNode.h" -#include "PermuteNode.h" -#include "AddNode.h" diff --git a/runtimes/neurun/src/model/operation/Node.cc b/runtimes/neurun/src/model/operation/Node.cc deleted file mode 100644 index 76397afde..000000000 --- a/runtimes/neurun/src/model/operation/Node.cc +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 "Node.h" - -#include <cassert> - -#include "graph/operation/LowerInfo.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -Node::Node(OperandConstraint input_constr) : _input_constr{input_constr} {} - -Node::~Node() = default; - -void Node::setInputs(const operand::IndexSet &indexes) -{ - assert(_input_constr.check(indexes.size())); - _inputs = indexes; -} - -void Node::setOutputs(const operand::IndexSet &indexes) { _outputs = indexes; } - -void Node::replaceInput(const operand::Index &from, const operand::Index &to) -{ - _inputs.replace(from, to); -} - -void Node::replaceOutput(const operand::Index &from, const operand::Index &to) -{ - _outputs.replace(from, to); -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/Node.h b/runtimes/neurun/src/model/operation/Node.h deleted file mode 100644 index 76f0d2d00..000000000 --- a/runtimes/neurun/src/model/operation/Node.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_NODE_H__ -#define __NEURUN_MODEL_OPERATION_NODE_H__ - -#include <memory> - -#include "model/operand/Object.h" -#include "model/operand/IndexSet.h" -#include "OperandConstraint.h" - -namespace neurun -{ -namespace graph -{ -namespace operation -{ -class LowerInfo; -} // namespace operation -} // namespace graph -} // namespace neurun - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -struct NodeVisitor; - -class Node -{ -public: - struct InitParam - { - uint32_t input_count; - const uint32_t *inputs; - uint32_t output_count; - const uint32_t *outputs; - }; - -public: - Node(OperandConstraint input_constr); - virtual ~Node(); - -public: - virtual void accept(NodeVisitor &&) const = 0; - virtual std::string getName() const = 0; - -public: - void replaceInput(const operand::Index &from, const operand::Index &to); - void replaceOutput(const operand::Index &from, const operand::Index &to); - const operand::IndexSet &getInputs() const { return _inputs; } - const operand::IndexSet &getOutputs() const { return _outputs; } - // It's for only input/output tensors but const data. - void setInputs(const operand::IndexSet &indexes); - void setOutputs(const operand::IndexSet &indexes); - -private: - operand::IndexSet _inputs; - operand::IndexSet _outputs; - OperandConstraint _input_constr; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/NodeVisitor.h b/runtimes/neurun/src/model/operation/NodeVisitor.h deleted file mode 100644 index 8420de998..000000000 --- a/runtimes/neurun/src/model/operation/NodeVisitor.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_NODE_VISITOR_H__ -#define __NEURUN_MODEL_OPERATION_NODE_VISITOR_H__ - -#include "Node.Include.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -struct NodeVisitor -{ - virtual ~NodeVisitor() = default; - -#define OP(InternalName, IsNnApi, NnApiName) \ - virtual void visit(const InternalName &) {} -#include "model/operation/Op.lst" -#undef OP -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_NODE_VISITOR_H__ diff --git a/runtimes/neurun/src/model/operation/Op.lst b/runtimes/neurun/src/model/operation/Op.lst deleted file mode 100644 index 23f4b5118..000000000 --- a/runtimes/neurun/src/model/operation/Op.lst +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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. - */ - -#ifndef OP -#error Define OP before including this file -#endif - -// NOTE The relation between "Internal Name" and "NN API Name" is "1 : N". - -// Internal Name | NN API? | NN API Name -OP(AddNode , true , ADD) -OP(Conv2DNode , true , CONV_2D) -OP(AvgPool2DNode , true , AVERAGE_POOL_2D) -OP(MaxPool2DNode , true , MAX_POOL_2D) -OP(ConcatNode , true , CONCATENATION) -OP(FullyConnectedNode , true , FULLY_CONNECTED) -OP(ReshapeNode , true , RESHAPE) -OP(SoftmaxNode , true , SOFTMAX) -OP(PermuteNode , false , NOT_AVAILABLE) diff --git a/runtimes/neurun/src/model/operation/OperandConstraint.cc b/runtimes/neurun/src/model/operation/OperandConstraint.cc deleted file mode 100644 index 5c69de928..000000000 --- a/runtimes/neurun/src/model/operation/OperandConstraint.cc +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 "OperandConstraint.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/OperandConstraint.h b/runtimes/neurun/src/model/operation/OperandConstraint.h deleted file mode 100644 index d1cd8aa2c..000000000 --- a/runtimes/neurun/src/model/operation/OperandConstraint.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_OPERAND_CONSTRAINT_H__ -#define __NEURUN_MODEL_OPERATION_OPERAND_CONSTRAINT_H__ - -#include <stdint.h> -#include <limits> -#include <set> - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class OperandConstraint -{ -private: - static const uint32_t INF = std::numeric_limits<uint32_t>::max(); - -public: - static OperandConstraint createAny() { return OperandConstraint{0u, INF}; } - static OperandConstraint createExact(uint32_t exact) { return OperandConstraint{exact, exact}; } - static OperandConstraint createAtMost(uint32_t end) { return OperandConstraint{0u, end}; } - static OperandConstraint createAtLeast(uint32_t begin) { return OperandConstraint{begin, INF}; } - static OperandConstraint createInRange(uint32_t begin, uint32_t end) - { - return OperandConstraint{begin, end}; - } - -private: - OperandConstraint(uint32_t begin, uint32_t end) : _begin{begin}, _end{end} {} - -public: - bool check(uint32_t ind) const { return _begin <= ind && ind <= _end; } - -private: - uint32_t _begin; - uint32_t _end; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_OPERAND_CONSTRAINT_H__ diff --git a/runtimes/neurun/src/model/operation/PermuteNode.cc b/runtimes/neurun/src/model/operation/PermuteNode.cc deleted file mode 100644 index 174d2a86b..000000000 --- a/runtimes/neurun/src/model/operation/PermuteNode.cc +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 "PermuteNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void PermuteNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -PermuteNode::PermuteNode(const operand::Index &input, const operand::Index &output, Type type) - : model::operation::Node{OperandConstraint::createExact(1u)}, _param{type} -{ - setInputs({input}); - setOutputs({output}); -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/PermuteNode.h b/runtimes/neurun/src/model/operation/PermuteNode.h deleted file mode 100644 index b589975be..000000000 --- a/runtimes/neurun/src/model/operation/PermuteNode.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_PERMUTE_NODE_H__ -#define __NEURUN_MODEL_OPERATION_PERMUTE_NODE_H__ - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class PermuteNode : public model::operation::Node -{ -public: - enum class Type - { - NHWC_TO_NCHW, - NCHW_TO_NHWC, - COPY - }; - - struct Param - { - Type type; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "Permute"; } - -public: - PermuteNode(const operand::Index &input, const operand::Index &output, Type type); - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_PERMUTE_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/ReshapeNode.cc b/runtimes/neurun/src/model/operation/ReshapeNode.cc deleted file mode 100644 index 616b8cd65..000000000 --- a/runtimes/neurun/src/model/operation/ReshapeNode.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 "ReshapeNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void ReshapeNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -ReshapeNode::ReshapeNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(1u)} -{ - assert(init_param.input_count == 2 && init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // 0 -> A tensor, specifying the tensor to be reshaped. - // 1 -> A 1-D tensor of type ANEURALNETWORKS_TENSOR_INT32, defining the shape of the output - // tensor - - // TODO Second input should be shape tensor (init_param.inputs[1]) - setInputs({init_param.inputs[0] /* , init_param.inputs[1] */}); - setOutputs({init_param.outputs[0]}); -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/ReshapeNode.h b/runtimes/neurun/src/model/operation/ReshapeNode.h deleted file mode 100644 index 1758e9ec8..000000000 --- a/runtimes/neurun/src/model/operation/ReshapeNode.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_RESHAPE_NODE_H__ -#define __NEURUN_MODEL_OPERATION_RESHAPE_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class ReshapeNode : public model::operation::Node -{ -public: - ReshapeNode(const model::operation::Node::InitParam &init_param); - - enum Input - { - INPUT = 0 - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "Reshape"; } -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_RESHAPE_NODE_H__ diff --git a/runtimes/neurun/src/model/operation/Set.cc b/runtimes/neurun/src/model/operation/Set.cc deleted file mode 100644 index 14bd4f584..000000000 --- a/runtimes/neurun/src/model/operation/Set.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 "Set.h" - -#include <cassert> - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -const Index Set::generateIndex() -{ - assert((_index_count) <= 0x7fffffff); - - return Index{_index_count++}; -} - -Index Set::append(std::unique_ptr<Node> &&node) -{ - auto index = generateIndex(); - - _nodes[index] = std::move(node); - return index; -} - -const Node &Set::at(const Index &index) const { return *(_nodes.at(index)); } - -Node &Set::at(const Index &index) { return *(_nodes.at(index)); } - -bool Set::exist(const Index &index) const { return _nodes.find(index) != _nodes.end(); } - -void Set::iterate(const std::function<void(const Index &, const Node &)> &fn) const -{ - for (auto it = _nodes.begin(); it != _nodes.end(); ++it) - { - fn(it->first, *it->second); - } -} - -void Set::iterate(const std::function<void(const Index &, Node &)> &fn) -{ - for (auto it = _nodes.begin(); it != _nodes.end(); ++it) - { - fn(it->first, *it->second); - } -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/Set.h b/runtimes/neurun/src/model/operation/Set.h deleted file mode 100644 index eebf91e65..000000000 --- a/runtimes/neurun/src/model/operation/Set.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_SET_H__ -#define __NEURUN_MODEL_OPERATION_SET_H__ - -#include <memory> - -#include "model/operation/Index.h" -#include "Node.h" - -#include <unordered_map> - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class Set -{ -public: - Set() : _index_count(0) {} - -public: - Index append(std::unique_ptr<Node> &&node); - void remove(const Index &index) { _nodes.erase(index); }; - -public: - const Node &at(const Index &) const; - Node &at(const Index &); - bool exist(const Index &) const; - uint32_t size() const { return _nodes.size(); } - void iterate(const std::function<void(const Index &, const Node &)> &fn) const; - void iterate(const std::function<void(const Index &, Node &)> &fn); - -private: - const Index generateIndex(); - -private: - std::unordered_map<Index, std::unique_ptr<Node>> _nodes; - uint32_t _index_count; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_SET_H__ diff --git a/runtimes/neurun/src/model/operation/SoftmaxNode.cc b/runtimes/neurun/src/model/operation/SoftmaxNode.cc deleted file mode 100644 index d157aa4a7..000000000 --- a/runtimes/neurun/src/model/operation/SoftmaxNode.cc +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 "SoftmaxNode.h" - -#include <cassert> - -#include "NodeVisitor.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -void SoftmaxNode::accept(NodeVisitor &&v) const { v.visit(*this); } - -SoftmaxNode::SoftmaxNode(const model::operation::Node::InitParam &init_param) - : model::operation::Node{OperandConstraint::createExact(1u)} -{ - assert(init_param.input_count == 2 && init_param.output_count == 1); - - // Each input should be interpreted as follows: - // - // 0 -> A 2-D or 4-D tensor, specifying the tensor to be reshaped. - // 1 -> FLOAT32 value, specifying the positive scaling factor for the exponent, beta. - - setInputs({init_param.inputs[0]}); - setOutputs({init_param.outputs[0]}); - - _param.scale_index = operand::Index{init_param.inputs[1]}; -} - -} // namespace operation -} // namespace model -} // namespace neurun diff --git a/runtimes/neurun/src/model/operation/SoftmaxNode.h b/runtimes/neurun/src/model/operation/SoftmaxNode.h deleted file mode 100644 index 4a5a72e5a..000000000 --- a/runtimes/neurun/src/model/operation/SoftmaxNode.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_MODEL_OPERATION_SOFTMAX_NODE_H__ -#define __NEURUN_MODEL_OPERATION_SOFTMAX_NODE_H__ - -#include <memory> - -#include "model/operation/Node.h" - -namespace neurun -{ -namespace model -{ -namespace operation -{ - -class SoftmaxNode : public model::operation::Node -{ -public: - SoftmaxNode(const model::operation::Node::InitParam &init_param); - enum Input - { - INPUT = 0 - }; - - struct Param - { - operand::Index scale_index; - }; - -public: - virtual void accept(NodeVisitor &&) const override; - virtual std::string getName() const override { return "SoftMax"; } - -public: - const Param ¶m() const { return _param; } - -private: - Param _param; -}; - -} // namespace operation -} // namespace model -} // namespace neurun - -#endif // __NEURUN_MODEL_OPERATION_SOFTMAX_NODE_H__ diff --git a/runtimes/neurun/src/util/Padding.cc b/runtimes/neurun/src/util/Padding.cc deleted file mode 100644 index a24c9ddf7..000000000 --- a/runtimes/neurun/src/util/Padding.cc +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 "util/Padding.h" - -#include <algorithm> - -namespace neurun -{ -namespace util -{ - -Padding valid_padding(void) -{ - // - // ANEURALNETWORKS_PADDING_VALID - // - // VALID padding. No padding. - // - // When the input size is not evenly divisible by the filter size, - // the input at the end that could not fill the whole filter tile - // will simply be ignored. - // - Padding padding; - - padding.top = 0; - padding.bottom = 0; - padding.left = 0; - padding.right = 0; - - return padding; -} - -Padding same_padding(const nnfw::misc::feature::Shape &ifm_shape, - const nnfw::misc::feature::Shape &ofm_shape, const Stride &stride, uint32_t kw, - uint32_t kh) -{ - Padding padding; - - // ANEURALNETWORKS_PADDING_SAME (from NNAPI spec) - // - // SAME padding. Padding on both ends are the "same": - // - // padding_to_beginning = total_padding / 2 - // padding_to_end = (total_padding + 1)/2. - // - const int32_t vertical_needed_input = (ofm_shape.H - 1) * stride.vertical + kh; - const int32_t vertical_total_padding = std::max(0, vertical_needed_input - ifm_shape.H); - - const int32_t horizontal_needed_input = (ofm_shape.W - 1) * stride.horizontal + kw; - const int32_t horizontal_total_padding = std::max(0, horizontal_needed_input - ifm_shape.W); - - padding.top = vertical_total_padding / 2; - padding.bottom = (vertical_total_padding + 1) / 2; - padding.left = horizontal_total_padding / 2; - padding.right = (horizontal_total_padding + 1) / 2; - - return padding; -} - -} // namespace util -} // namespace neurun diff --git a/runtimes/neurun/src/util/Padding.h b/runtimes/neurun/src/util/Padding.h deleted file mode 100644 index 05a14eb31..000000000 --- a/runtimes/neurun/src/util/Padding.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_PADDING_H__ -#define __NEURUN_UTIL_PADDING_H__ - -#include <stdint.h> - -#include <misc/feature/Shape.h> - -namespace neurun -{ -namespace util -{ - -struct Padding -{ - uint32_t top; - uint32_t bottom; - uint32_t left; - uint32_t right; -}; - -struct Stride -{ - uint32_t vertical; - uint32_t horizontal; -}; - -Padding valid_padding(void); -Padding same_padding(const nnfw::misc::feature::Shape &ifm_shape, - const nnfw::misc::feature::Shape &ofm_shape, const Stride &stride, uint32_t kw, - uint32_t kh); - -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_PADDING_H__ diff --git a/runtimes/neurun/src/util/Utils.cc b/runtimes/neurun/src/util/Utils.cc deleted file mode 100644 index def02db69..000000000 --- a/runtimes/neurun/src/util/Utils.cc +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 "Utils.h" - -#include <cassert> - -namespace neurun -{ -namespace util -{ - -const char *to_string(const PaddingCode &code) -{ - assert((ANEURALNETWORKS_PADDING_SAME == code) || (ANEURALNETWORKS_PADDING_VALID == code)); - - switch (code) - { - case ANEURALNETWORKS_PADDING_SAME: - return "ANEURALNETWORKS_PADDING_SAME"; - case ANEURALNETWORKS_PADDING_VALID: - return "ANEURALNETWORKS_PADDING_VALID"; - } - - return nullptr; -} - -} // namespace util -} // namespace neurun diff --git a/runtimes/neurun/src/util/Utils.h b/runtimes/neurun/src/util/Utils.h deleted file mode 100644 index a1e5bf0ba..000000000 --- a/runtimes/neurun/src/util/Utils.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 Utils.h - * @brief This file contains utility functions - * @ingroup COM_AI_RUNTIME - */ - -#ifndef __NEURUN_UTIL_UTILS_H__ -#define __NEURUN_UTIL_UTILS_H__ - -#include "NeuralNetworks.h" - -namespace neurun -{ -namespace util -{ - -/** - * @brief Converts a PaddingCode to const char* - * @param[in] code The PaddingCode to be converted - * @return A string holding the converted value - */ -const char *to_string(const PaddingCode &code); - -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_UTILS_H__ diff --git a/runtimes/neurun/src/util/config/Config.lst b/runtimes/neurun/src/util/config/Config.lst deleted file mode 100644 index e029ebe37..000000000 --- a/runtimes/neurun/src/util/config/Config.lst +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ - -#ifndef CONFIG -#error Define CONFIG before including this file -#endif - -// Name | Type | Default -CONFIG(GRAPH_DOT_DUMP , int , "0") -CONFIG(BACKENDS , std::string , "cpu;acl_cl") -CONFIG(OP_BACKEND_ALLOPS , std::string , "none") -CONFIG(DISABLE_COMPILE , bool , "0") - - -// Auto-generate all operations - -#define OP(InternalName, IsNnApi, NnApiName) \ - CONFIG(OP_BACKEND_ ## NnApiName, std::string, "acl_cl") -#include "model/operation/Op.lst" -#undef OP - diff --git a/runtimes/neurun/src/util/config/ConfigManager.cc b/runtimes/neurun/src/util/config/ConfigManager.cc deleted file mode 100644 index 46b80311c..000000000 --- a/runtimes/neurun/src/util/config/ConfigManager.cc +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 "ConfigManager.h" - -#include <cstdlib> - -namespace neurun -{ -namespace config -{ - -ConfigManager &ConfigManager::instance() -{ - static ConfigManager manager; - return manager; -} - -ConfigManager::ConfigManager() -{ - auto fetch_from_env_var = [&](const std::string &key) { - const char *value = std::getenv(key.c_str()); - if (value != nullptr) - { - _map[key] = value; - } - }; - -#define CONFIG(Name, Type, Default) \ - _map.insert({std::string{#Name}, std::string{Default}}); \ - fetch_from_env_var(#Name); - -#include "Config.lst" - -#undef CONFIG -} - -template <> bool ConfigManager::get<bool>(const std::string &key) const -{ - auto raw = _map.at(key); - - static const std::array<std::string, 5> false_list{"0", "OFF", "FALSE", "N", "NO"}; - auto false_found = std::find(false_list.begin(), false_list.end(), raw); - - return (false_found == false_list.end()); -} - -template <> int ConfigManager::get<int>(const std::string &key) const -{ - auto raw = _map.at(key); - return std::stoi(raw); -} - -template <> std::string ConfigManager::get<std::string>(const std::string &key) const -{ - auto raw = _map.at(key); - return raw; -} - -} // namespace config -} // namespace neurun diff --git a/runtimes/neurun/src/util/config/ConfigManager.h b/runtimes/neurun/src/util/config/ConfigManager.h deleted file mode 100644 index 78db03dc1..000000000 --- a/runtimes/neurun/src/util/config/ConfigManager.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_CONFIG_CONFIG_MANAGER_H__ -#define __NEURUN_CONFIG_CONFIG_MANAGER_H__ - -#include <algorithm> -#include <string> -#include <unordered_map> - -/** - * @file ConfigManager.h - * @brief This file contains neurun::config::ConfigManager class - */ - -namespace neurun -{ -namespace config -{ - -/** - * @brief Class that manages configurations - */ - -class ConfigManager -{ -public: - static ConfigManager &instance(); - -private: - /** - * @brief Construct a new ConfigManager object. Fetch variables from Environment Variables. - */ - ConfigManager(); - -public: - /** - * @brief Return the configuration value of given key - * - * @tparam T Type of the config - * @param key String key value - * - * @return The configuration value of given key value - */ - template <typename T> T get(const std::string &key) const; - -private: - std::unordered_map<std::string, std::string> _map; -}; - -template <> bool ConfigManager::get<bool>(const std::string &key) const; -template <> int ConfigManager::get<int>(const std::string &key) const; -template <> std::string ConfigManager::get<std::string>(const std::string &key) const; - -} // namespace config -} // namespace neurun - -#endif // __NEURUN_CONFIG_CONFIG_MANAGER_H__ diff --git a/runtimes/neurun/src/util/feature/Coordinate4D.h b/runtimes/neurun/src/util/feature/Coordinate4D.h deleted file mode 100644 index 27d6f7b9e..000000000 --- a/runtimes/neurun/src/util/feature/Coordinate4D.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_FEATURE_COORDINATE_4D_H__ -#define __NEURUN_UTIL_FEATURE_COORDINATE_4D_H__ - -#include <stdint.h> - -namespace neurun -{ -namespace util -{ -namespace feature -{ - -/** - * @brief Class to represent position(offset) of subtensor.\n - * Assume that parent and child are already lowered (can get Shape4D). - */ -class Coordinate4D -{ -public: - /** - * @brief Construct a new Coordinate4D object - */ - Coordinate4D(void) : _n{0}, _h{0}, _w{0}, _c{0} - { - // DO NOTHING - } - /** - * @brief Construct a new Coordinate4D object - * @param[in] n Batch offset - * @param[in] h Height offset - * @param[in] w Width offset - * @param[in] c Channel offset - * @return - */ - Coordinate4D(int32_t n, int32_t h, int32_t w, int32_t c) : _n{n}, _h{h}, _w{w}, _c{c} - { - // DO NOTHING - } - -public: - /** - * @brief Return batch offset - * @return Batch offset - */ - int32_t n(void) const { return _n; } - /** - * @brief Return height offset - * @return Height offset - */ - int32_t h(void) const { return _h; } - /** - * @brief Return width offset - * @return Width offset - */ - int32_t w(void) const { return _w; } - /** - * @brief Return channel offset - * @return Channel offset - */ - int32_t c(void) const { return _c; } - -private: - int32_t _n; - int32_t _h; - int32_t _w; - int32_t _c; -}; - -} // namespace feature -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_FEATURE_COORDINATE_4D_H__ diff --git a/runtimes/neurun/src/util/feature/nchw/View.h b/runtimes/neurun/src/util/feature/nchw/View.h deleted file mode 100644 index 048fdecd8..000000000 --- a/runtimes/neurun/src/util/feature/nchw/View.h +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_FEATURE_NCHW_VIEW_H__ -#define __NEURUN_UTIL_FEATURE_NCHW_VIEW_H__ - -#include "misc/feature/Reader.h" -#include "misc/feature/Shape.h" - -#include "backend/interface/operand/ITensor.h" -#include "util/feature/Coordinate4D.h" - -#include <cassert> - -namespace neurun -{ -namespace util -{ -namespace feature -{ -namespace nchw -{ - -template <typename T> class View final : public nnfw::misc::feature::Reader<T> -{ -public: - View(::neurun::backend::operand::ITensor *tensor) : _tensor{tensor} - { - // TODO Validate whether tensor is a feature map, or not - _shape.N = tensor->dimension(3); - _shape.C = tensor->dimension(2); - _shape.H = tensor->dimension(1); - _shape.W = tensor->dimension(0); - } - -public: - const ::nnfw::misc::feature::Shape &shape(void) const { return _shape; } - -public: - T at(uint32_t ch, uint32_t row, uint32_t col) const override - { - const auto offset = feature_index_to_byte_offset(0, ch, row, col); - - T *ptr = reinterpret_cast<T *>(_tensor->buffer() + offset); - - return *ptr; - } - T at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const override - { - const auto offset = feature_index_to_byte_offset(batch, ch, row, col); - - T *ptr = reinterpret_cast<T *>(_tensor->buffer() + offset); - - return *ptr; - } - -public: - T &at(uint32_t ch, uint32_t row, uint32_t col) - { - const auto offset = feature_index_to_byte_offset(0, ch, row, col); - - T *ptr = reinterpret_cast<T *>(_tensor->buffer() + offset); - - return *ptr; - } - T &at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) - { - const auto offset = feature_index_to_byte_offset(batch, ch, row, col); - - T *ptr = reinterpret_cast<T *>(_tensor->buffer() + offset); - - return *ptr; - } - -private: - size_t feature_index_to_byte_offset(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const - { - return _tensor->calcOffset( - neurun::util::feature::Coordinate4D{static_cast<int32_t>(batch), static_cast<int32_t>(row), - static_cast<int32_t>(col), static_cast<int32_t>(ch)}); - } - -private: - ::nnfw::misc::feature::Shape _shape; - ::neurun::backend::operand::ITensor *_tensor; -}; - -} // namespace nchw -} // namespace feature -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_FEATURE_NCHW_VIEW_H__ diff --git a/runtimes/neurun/src/util/feature/nhwc/Reader.h b/runtimes/neurun/src/util/feature/nhwc/Reader.h deleted file mode 100644 index 85b8cab74..000000000 --- a/runtimes/neurun/src/util/feature/nhwc/Reader.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_FEATURE_NHWC_READER_H__ -#define __NEURUN_UTIL_FEATURE_NHWC_READER_H__ - -#include "Utils.h" - -#include "misc/feature/Reader.h" - -namespace neurun -{ -namespace util -{ -namespace feature -{ -namespace nhwc -{ - -template <typename T> class Reader final : public nnfw::misc::feature::Reader<T> -{ -public: - Reader(const ::nnfw::misc::feature::Shape &shape, const T *ptr, size_t len) - : _shape{shape}, _ptr{ptr} - { - (void)len; // Workaround for unused variable in release mode - assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); - } - -public: - const nnfw::misc::feature::Shape &shape(void) const { return _shape; } - -public: - T at(uint32_t ch, uint32_t row, uint32_t col) const override - { - uint32_t index = index_of(_shape, ch, row, col); - - return _ptr[index]; - } - T at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const override - { - uint32_t index = index_of(_shape, batch, ch, row, col); - - return _ptr[index]; - } - -private: - nnfw::misc::feature::Shape _shape; - -private: - const T *_ptr; -}; - -} // namespace nhwc -} // namespace feature -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_FEATURE_NHWC_READER_H__ diff --git a/runtimes/neurun/src/util/feature/nhwc/Utils.h b/runtimes/neurun/src/util/feature/nhwc/Utils.h deleted file mode 100644 index 3dab4261c..000000000 --- a/runtimes/neurun/src/util/feature/nhwc/Utils.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_FEATURE_NHWC_UTILS_H__ -#define __NEURUN_UTIL_FEATURE_NHWC_UTILS_H__ - -#include "misc/feature/Shape.h" - -namespace neurun -{ -namespace util -{ -namespace feature -{ -namespace nhwc -{ - -inline uint32_t index_of(const ::nnfw::misc::feature::Shape &shape, uint32_t ch, uint32_t row, - uint32_t col) -{ - uint32_t res = 0; - - // NNAPI uses NHWC ordering - res += row * shape.W * shape.C; - res += col * shape.C; - res += ch; - - return res; -} - -inline uint32_t index_of(const ::nnfw::misc::feature::Shape &shape, uint32_t batch, uint32_t ch, - uint32_t row, uint32_t col) -{ - uint32_t res = 0; - - // NNAPI uses NHWC ordering - res += batch * shape.H * shape.W * shape.C; - res += row * shape.W * shape.C; - res += col * shape.C; - res += ch; - - return res; -} - -} // namespace nhwc -} // namespace feature -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_FEATURE_NHWC_UTILS_H__ diff --git a/runtimes/neurun/src/util/feature/nhwc/View.h b/runtimes/neurun/src/util/feature/nhwc/View.h deleted file mode 100644 index 1b9be9e1d..000000000 --- a/runtimes/neurun/src/util/feature/nhwc/View.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_FEATURE_NHWC_VIEW_H__ -#define __NEURUN_UTIL_FEATURE_NHWC_VIEW_H__ - -#include <cassert> - -#include "Utils.h" - -#include "misc/feature/Reader.h" - -namespace neurun -{ -namespace util -{ -namespace feature -{ -namespace nhwc -{ - -template <typename T> class View final : public nnfw::misc::feature::Reader<T> -{ -public: - View(const ::nnfw::misc::feature::Shape &shape, T *ptr, size_t len) : _shape{shape}, _ptr{ptr} - { - (void)len; // Workaround for unused variable in release mode - assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); - } - -public: - const nnfw::misc::feature::Shape &shape(void) const { return _shape; } - -public: - T at(uint32_t ch, uint32_t row, uint32_t col) const override - { - uint32_t index = index_of(_shape, ch, row, col); - - return _ptr[index]; - } - - T at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const override - { - uint32_t index = index_of(_shape, batch, ch, row, col); - - return _ptr[index]; - } - - T &at(uint32_t ch, uint32_t row, uint32_t col) - { - uint32_t index = index_of(_shape, ch, row, col); - - return _ptr[index]; - } - - T &at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) - { - uint32_t index = index_of(_shape, batch, ch, row, col); - - return _ptr[index]; - } - -private: - nnfw::misc::feature::Shape _shape; - -private: - T *_ptr; -}; - -} // namespace nhwc -} // namespace feature -} // namespace util -} // namespace neurun - -#endif // __NEURUN_UTIL_FEATURE_NHWC_VIEW_H__ diff --git a/runtimes/neurun/src/util/logging.h b/runtimes/neurun/src/util/logging.h deleted file mode 100644 index 62d563967..000000000 --- a/runtimes/neurun/src/util/logging.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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. - */ - -#ifndef __NEURUN_UTIL_LOGGING_H__ -#define __NEURUN_UTIL_LOGGING_H__ - -#include <iostream> - -namespace neurun -{ -namespace util -{ -namespace logging -{ - -class Context -{ -public: - Context() : _enabled{false} - { - auto env = std::getenv("NEURUN_LOG_ENABLE"); - - if (env && std::atoi(env) > 0) - { - _enabled = true; - } - } - -public: - bool enabled(void) const { return _enabled; } - -private: - bool _enabled; -}; - -static Context ctx; - -} // namespace logging -} // namespace util -} // namespace neurun - -#define VERBOSE(name) \ - if (::neurun::util::logging::ctx.enabled()) \ - std::cout << "[" << #name << "] " - -#endif // __NEURUN_UTIL_LOGGING_H__ |