Importers have been extracted into independent components. This is a follow-up cleanup.
Signed-off-by: Sergei Barannikov <s.barannikov@samsung.com>
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef FRONTEND_COMMON_MODEL_ALLOCATION_H_
-#define FRONTEND_COMMON_MODEL_ALLOCATION_H_
-
-#include <string>
-#include <cstdint>
-
-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_
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef FRONTEND_COMMON_OP_CREATOR_HELPER_H_
-#define FRONTEND_COMMON_OP_CREATOR_HELPER_H_
-
-#include <cstdint>
-#include <memory>
-#include <set>
-
-#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<std::string>& problemsSet);
-
-} // namespace nnc
-
-#endif // FRONTEND_COMMON_OP_CREATOR_HELPER_H_
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef NNCC_SHAPE_HELPER_H
-#define NNCC_SHAPE_HELPER_H
-
-#include "mir/Shape.h"
-
-namespace nnc
-{
-
-class ShapeHelper
-{
-public:
- template<typename Iterable>
- static mir::Shape createShape(const Iterable &iter, std::size_t size);
-};
-
-template<typename Iterable>
-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<int32_t>(size));
-
- unsigned int i = 0;
- for (auto dim : iter)
- {
- sh.dim(i++) = dim;
- }
-
- return sh;
-}
-
-} // namespace nnc
-
-#endif // NNCC_SHAPE_HELPER_H
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef NNCC_PROTOBUF_HELPER_H
-#define NNCC_PROTOBUF_HELPER_H
-
-#include <google/protobuf/message.h>
-#include <string>
-
-namespace nnc {
-
-void readBinaryProto(const std::string& filename, google::protobuf::Message* message);
-
-} // namespace nnc
-
-#endif // NNCC_PROTOBUF_HELPER_H
-##########################################
-# 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})
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-
-#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
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "pass/PassException.h"
-#include "passes/common_frontend/op_creator_helper.h"
-
-#include "mir/Shape.h"
-#include "mir/ShapeRange.h"
-#include "mir/TensorVariant.h"
-
-#include <set>
-
-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<float*>(unfold_kernel.at(idx));
- *elem = 0.0f;
- }
- }
- return unfold_kernel;
-}
-
-void handleProblemSet(const std::set<std::string>& 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
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)
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "support/ProtobufHelper.h"
-#include <google/protobuf/io/zero_copy_stream_impl.h>
-#include <google/protobuf/io/coded_stream.h>
-#include <cerrno>
-#include <cstring>
-#include <stdexcept>
-#include <fcntl.h>
-#include <unistd.h>
-
-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