From 87f7d17a73bfeaf3c3a0159b50b3d7697d7b9bd8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=91=D0=B0=D1=80?= =?utf8?q?=D0=B0=D0=BD=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2/AI=20Tools=20Lab=20/S?= =?utf8?q?RR/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 26 Jul 2019 13:55:34 +0300 Subject: [PATCH] [nnc] Cleanup after separating importers (#5902) Importers have been extracted into independent components. This is a follow-up cleanup. Signed-off-by: Sergei Barannikov --- .../passes/common_frontend/model_allocation.h | 49 ------------- .../passes/common_frontend/op_creator_helper.h | 52 -------------- .../include/passes/common_frontend/shape_helper.h | 53 -------------- compiler/nnc/include/support/ProtobufHelper.h | 29 -------- compiler/nnc/passes/common_frontend/CMakeLists.txt | 11 +-- .../passes/common_frontend/model_allocation.cpp | 60 ---------------- .../passes/common_frontend/op_creator_helper.cpp | 83 ---------------------- compiler/nnc/support/CMakeLists.txt | 4 +- compiler/nnc/support/ProtobufHelper.cpp | 53 -------------- 9 files changed, 2 insertions(+), 392 deletions(-) delete mode 100644 compiler/nnc/include/passes/common_frontend/model_allocation.h delete mode 100644 compiler/nnc/include/passes/common_frontend/op_creator_helper.h delete mode 100644 compiler/nnc/include/passes/common_frontend/shape_helper.h delete mode 100644 compiler/nnc/include/support/ProtobufHelper.h delete mode 100644 compiler/nnc/passes/common_frontend/model_allocation.cpp delete mode 100644 compiler/nnc/passes/common_frontend/op_creator_helper.cpp delete mode 100644 compiler/nnc/support/ProtobufHelper.cpp diff --git a/compiler/nnc/include/passes/common_frontend/model_allocation.h b/compiler/nnc/include/passes/common_frontend/model_allocation.h deleted file mode 100644 index 2d954ee..0000000 --- a/compiler/nnc/include/passes/common_frontend/model_allocation.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 FRONTEND_COMMON_MODEL_ALLOCATION_H_ -#define FRONTEND_COMMON_MODEL_ALLOCATION_H_ - -#include -#include - -namespace nnc { - -// Class that can be used to memory map a file with NN model -class ModelAllocation { -public: - explicit ModelAllocation(const std::string& filename); - virtual ~ModelAllocation(); - - const void *getDataPnt(); - size_t getNumBytes(); - -private: - enum MmapState { - mapped, - unmapped - }; - - MmapState _mmapState = unmapped; - void* _dataPnt = nullptr; - size_t _numBytes = 0; - - int _fd = -1; -}; - -} // namespace nnc - -#endif // FRONTEND_COMMON_MODEL_ALLOCATION_H_ diff --git a/compiler/nnc/include/passes/common_frontend/op_creator_helper.h b/compiler/nnc/include/passes/common_frontend/op_creator_helper.h deleted file mode 100644 index 80bd64d..0000000 --- a/compiler/nnc/include/passes/common_frontend/op_creator_helper.h +++ /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. - */ - -#ifndef FRONTEND_COMMON_OP_CREATOR_HELPER_H_ -#define FRONTEND_COMMON_OP_CREATOR_HELPER_H_ - -#include -#include -#include - -#include "mir/Shape.h" -#include "mir/TensorVariant.h" - -namespace nnc { - -/** Convert kernel for grouped 2d convolution in kernel for ordinary 2d convolution - * - * Grouped convolution breaks input and kernel channels into selected number of groups and applies convolution in every group of channels independently. - * This technique allows to save kernel size(channels from different groups are not merged, no need to store redundant 0 weights). - * This is not supported by compiler for now, so this function unfolds compact kernel into classic flavored "every input layer affects every output layer", - * by inserting zero coefficients where needed - * - * @param groups number of groups in grouped convolution - * @param foldedKernel original grouped kernel - * @return unfolded kernel, compatible with ordinary conv2D operation - */ -mir::TensorVariant fixGroupedKernel(int groups, const mir::TensorVariant& folded_kernel); - -/** Throws PassException if problems set is not empty - * - * @param problemsSet - set of strings describing incorrect parts of the network - * and parts of the network unsupported by NNC - * @throws PassException with appropriate message - */ -void handleProblemSet(const std::set& problemsSet); - -} // namespace nnc - -#endif // FRONTEND_COMMON_OP_CREATOR_HELPER_H_ diff --git a/compiler/nnc/include/passes/common_frontend/shape_helper.h b/compiler/nnc/include/passes/common_frontend/shape_helper.h deleted file mode 100644 index f203f77..0000000 --- a/compiler/nnc/include/passes/common_frontend/shape_helper.h +++ /dev/null @@ -1,53 +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 NNCC_SHAPE_HELPER_H -#define NNCC_SHAPE_HELPER_H - -#include "mir/Shape.h" - -namespace nnc -{ - -class ShapeHelper -{ -public: - template - static mir::Shape createShape(const Iterable &iter, std::size_t size); -}; - -template -mir::Shape ShapeHelper::createShape(const Iterable &iter, std::size_t size) -{ - //Zero-dim tensor is just a tensor with 1 element - if (size == 0) { - return mir::Shape{1}; - } - - mir::Shape sh(static_cast(size)); - - unsigned int i = 0; - for (auto dim : iter) - { - sh.dim(i++) = dim; - } - - return sh; -} - -} // namespace nnc - -#endif // NNCC_SHAPE_HELPER_H diff --git a/compiler/nnc/include/support/ProtobufHelper.h b/compiler/nnc/include/support/ProtobufHelper.h deleted file mode 100644 index 8fc82ee..0000000 --- a/compiler/nnc/include/support/ProtobufHelper.h +++ /dev/null @@ -1,29 +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 NNCC_PROTOBUF_HELPER_H -#define NNCC_PROTOBUF_HELPER_H - -#include -#include - -namespace nnc { - -void readBinaryProto(const std::string& filename, google::protobuf::Message* message); - -} // namespace nnc - -#endif // NNCC_PROTOBUF_HELPER_H diff --git a/compiler/nnc/passes/common_frontend/CMakeLists.txt b/compiler/nnc/passes/common_frontend/CMakeLists.txt index ec80830..34dcfd4 100644 --- a/compiler/nnc/passes/common_frontend/CMakeLists.txt +++ b/compiler/nnc/passes/common_frontend/CMakeLists.txt @@ -1,15 +1,6 @@ -########################################## -# Common for every importer code library # -########################################## - -set(COMMON_SOURCES model_allocation.cpp op_creator_helper.cpp) - -nnc_add_library(nn_import_common STATIC ${COMMON_SOURCES}) -set_target_properties(nn_import_common PROPERTIES POSITION_INDEPENDENT_CODE ON) -target_link_libraries(nn_import_common PUBLIC mir) - # # This library depends on other frontends to provide uniform interface for those who use frontends +# FIXME Should be STATIC. # set(IMPORTER_SOURCES NNImporter.cpp) nnc_add_library(nn_importer SHARED ${IMPORTER_SOURCES}) diff --git a/compiler/nnc/passes/common_frontend/model_allocation.cpp b/compiler/nnc/passes/common_frontend/model_allocation.cpp deleted file mode 100644 index 81c7f67..0000000 --- a/compiler/nnc/passes/common_frontend/model_allocation.cpp +++ /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 -#include -#include -#include - -#include "passes/common_frontend/model_allocation.h" - -namespace nnc { - -ModelAllocation::ModelAllocation(const std::string& filename) { - using stat = struct stat; - _fd = open(filename.c_str(), O_RDONLY); - - if (_fd == -1) - return; - - stat st; - int flag = fstat(_fd, &st); - if (flag == -1) - return; - - _numBytes = st.st_size; - - _dataPnt = mmap(nullptr, _numBytes, PROT_READ, MAP_SHARED, _fd, 0); - - if (_dataPnt != MAP_FAILED) // NOLINT(cppcoreguidelines-pro-type-cstyle-cast) - _mmapState = mapped; -} - -ModelAllocation::~ModelAllocation() { - if (_mmapState == mapped) { - munmap(_dataPnt, _numBytes); - _mmapState = unmapped; - } - - if (_fd != -1) - close(_fd); -} - -const void *ModelAllocation::getDataPnt() { return _mmapState == mapped ? _dataPnt : nullptr; } - -size_t ModelAllocation::getNumBytes() { return _mmapState == mapped ? _numBytes : 0; } - -} // namespace nnc diff --git a/compiler/nnc/passes/common_frontend/op_creator_helper.cpp b/compiler/nnc/passes/common_frontend/op_creator_helper.cpp deleted file mode 100644 index 411268e..0000000 --- a/compiler/nnc/passes/common_frontend/op_creator_helper.cpp +++ /dev/null @@ -1,83 +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/PassException.h" -#include "passes/common_frontend/op_creator_helper.h" - -#include "mir/Shape.h" -#include "mir/ShapeRange.h" -#include "mir/TensorVariant.h" - -#include - -namespace nnc { - -using namespace mir; - -TensorVariant -fixGroupedKernel(int groups, const TensorVariant& folded_kernel) { - const int kernel_in_chan_num = 2; - const int kernel_out_chan_num = 3; - - const Shape& kernel_shape = folded_kernel.getShape(); - auto kernel_in_channels = kernel_shape.dim(kernel_in_chan_num); - auto kernel_out_channels = kernel_shape.dim(kernel_out_chan_num); - auto in_channels = kernel_in_channels * groups; - - // Original kernel has shape [H, W, inputChannels/groups, outputChannels] - // here creates unfolded kernel with shape [H, W, inputChannels, outputChannels] - Shape unfold_kernel_shape(kernel_shape); - unfold_kernel_shape.dim(kernel_in_chan_num) = in_channels; - size_t data_size = folded_kernel.getElementSize(); - TensorVariant unfold_kernel(folded_kernel.getDataType(), unfold_kernel_shape); - - int in_group_size = kernel_in_channels; - int out_group_size = kernel_out_channels / groups; - assert(kernel_out_channels % groups == 0); - - // Iterate over "unfolded" kernel Shape and insert appropriate values into result kernel - for (const mir::Index& idx: mir::ShapeRange(unfold_kernel_shape)) { - auto in_group_no = idx.at(kernel_in_chan_num) / in_group_size; - auto out_group_no = idx.at(kernel_out_chan_num) / out_group_size; - // check that input channel group fits output channel group - if (in_group_no == out_group_no) { - // compute index in original kernel that corresponds output index - mir::Index folded_idx(idx); - folded_idx.at(kernel_in_chan_num) %= in_group_size; - - std::copy(folded_kernel.at(folded_idx), folded_kernel.at(folded_idx) + data_size, - unfold_kernel.at(idx)); - } else { - // fill element of output kernel with zero element - assert(folded_kernel.getDataType() == DTYPE::FLOAT32 && - "unsupported data type, add appropriate zero element creation"); - auto elem = reinterpret_cast(unfold_kernel.at(idx)); - *elem = 0.0f; - } - } - return unfold_kernel; -} - -void handleProblemSet(const std::set& problemsSet) { - if (!problemsSet.empty()) { - std::string msg("NNC can't load model. Detected problems:"); - for (const auto& problem_str : problemsSet) - msg.append("\n * " + problem_str); - throw PassException(msg); - } -} - -} // namespace nnc \ No newline at end of file diff --git a/compiler/nnc/support/CMakeLists.txt b/compiler/nnc/support/CMakeLists.txt index 8864b0e..542468d 100644 --- a/compiler/nnc/support/CMakeLists.txt +++ b/compiler/nnc/support/CMakeLists.txt @@ -1,9 +1,7 @@ set(SUPPORT_SOURCES CommandLine.cpp - CLOptionChecker.cpp - ProtobufHelper.cpp) + CLOptionChecker.cpp) nnc_add_library(nnc_support STATIC ${SUPPORT_SOURCES}) set_target_properties(nnc_support PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(nnc_support PROPERTIES POSITION_INDEPENDENT_CODE ON) -target_link_libraries(nnc_support PUBLIC libprotobuf) diff --git a/compiler/nnc/support/ProtobufHelper.cpp b/compiler/nnc/support/ProtobufHelper.cpp deleted file mode 100644 index 9c82e4a..0000000 --- a/compiler/nnc/support/ProtobufHelper.cpp +++ /dev/null @@ -1,53 +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 "support/ProtobufHelper.h" -#include -#include -#include -#include -#include -#include -#include - -namespace nnc { - -// Allow files up to 1GB, but warn about files larger than 512MB. -static constexpr int protoTotalBytesLimit = 1024u << 20u; -static constexpr int protoTotalBytesWarningThreshold = 512u << 20u; - -void readBinaryProto(const std::string& filename, google::protobuf::Message* message) { - int file_handle = open(filename.c_str(), O_RDONLY); - - if (file_handle == -1) - throw std::runtime_error("Couldn't open file \"" + filename + "\": " + - std::strerror(errno) + "."); - - google::protobuf::io::FileInputStream file_stream(file_handle); - file_stream.SetCloseOnDelete(true); - - google::protobuf::io::CodedInputStream coded_stream(&file_stream); - coded_stream.SetTotalBytesLimit(protoTotalBytesLimit, protoTotalBytesWarningThreshold); - - if (!message->ParseFromCodedStream(&coded_stream)) - throw std::runtime_error("Couldn't parse file \"" + filename + "\"."); - - // If the file has not been consumed entirely, assume that the file is in the wrong format. - if (!coded_stream.ConsumedEntireMessage()) - throw std::runtime_error("File \"" + filename + "\" has not been consumed entirely."); -} - -} // namespace nnc -- 2.7.4