diff options
Diffstat (limited to 'runtimes/neurun/core/include/util')
18 files changed, 1475 insertions, 0 deletions
diff --git a/runtimes/neurun/core/include/util/Config.lst b/runtimes/neurun/core/include/util/Config.lst new file mode 100644 index 000000000..c17ac147e --- /dev/null +++ b/runtimes/neurun/core/include/util/Config.lst @@ -0,0 +1,40 @@ +/* + * 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;acl_neon;srcn") +CONFIG(OP_BACKEND_ALLOPS , std::string , "acl_cl") +CONFIG(OP_BACKEND_MAP , std::string , "") +CONFIG(DISABLE_COMPILE , bool , "0") +CONFIG(NEURUN_LOG_ENABLE , bool , "0") +CONFIG(CPU_MEMORY_PLANNER , std::string , "FirstFit") +CONFIG(EXECUTOR , std::string , "Linear") +CONFIG(ACL_LAYOUT , std::string , "none") +CONFIG(PROFILING_MODE , bool , "0") +CONFIG(USE_SCHEDULER , bool , "0") + +// Auto-generate all operations + +#define OP(InternalName, IsNnApi) \ + CONFIG(OP_BACKEND_ ## InternalName, std::string, "") +#include "model/Operations.lst" +#undef OP + diff --git a/runtimes/neurun/core/include/util/ConfigSource.h b/runtimes/neurun/core/include/util/ConfigSource.h new file mode 100644 index 000000000..b1fa9a87d --- /dev/null +++ b/runtimes/neurun/core/include/util/ConfigSource.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019 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_CONFIG_SOURCE_H__ +#define __NEURUN_UTIL_CONFIG_SOURCE_H__ + +#include <memory> + +#include "IConfigSource.h" + +namespace neurun +{ +namespace util +{ + +void config_source(std::unique_ptr<IConfigSource> &&source); + +bool getConfigBool(const std::string &key); +int getConfigInt(const std::string &key); +std::string getConfigString(const std::string &key); + +} // namespace util +} // namespace neurun + +namespace neurun +{ +namespace util +{ +namespace config +{ + +#define CONFIG(Name, Type, Default) extern const char *Name; + +#include "Config.lst" + +#undef CONFIG + +} // namespace config +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_CONFIG_SOURCE_H__ diff --git a/runtimes/neurun/core/include/util/Coordinates.h b/runtimes/neurun/core/include/util/Coordinates.h new file mode 100644 index 000000000..67947138f --- /dev/null +++ b/runtimes/neurun/core/include/util/Coordinates.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2019 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_COORDINATES_H__ +#define __NEURUN_UTIL_COORDINATES_H__ + +#include <cassert> +#include <stdint.h> +#include <vector> + +namespace neurun +{ +namespace util +{ + +/** + * @brief Class to represent position(offset) of tensor.\n + * Assume that the front is higher dimensional. + * i.g. N: 0, C: 1, H: 2, W: 3 for NCHW layout + */ +class Coordinates final +{ +public: + static constexpr size_t num_max_dimensions = 4; + +public: + /** + * @brief Construct a new Coordinates object + * @param[in] init The initialzer_list with coordinates + * @return + */ + Coordinates(std::initializer_list<int32_t> init) : _coordinates{init} + { + assert(init.size() <= num_max_dimensions); + } + +public: + /** + * @brief Set the coordinate of one of the coordinates. + * + * @param[in] dimension Dimension for which the coordinate is set. + * @param[in] Coordinate Coordinate to be set for the dimension. + */ + void set(size_t dimension, int32_t coordinate) + { + assert(dimension < num_max_dimensions); + if (dimension >= _coordinates.size()) + { + _coordinates.resize(dimension + 1, 0); + } + _coordinates[dimension] = coordinate; + } + +public: + /** + * @brief Return size of coordinates + * + * @return size of coordinates + */ + size_t size() const { return _coordinates.size(); } + +public: + int32_t operator[](size_t dimension) const + { + assert(dimension < _coordinates.size()); + return _coordinates[dimension]; + } + +public: + /** + * @brief begin() of const_iterator for this class + * + * @return The first iterator of the coordinates + */ + std::vector<int32_t>::const_iterator begin() const { return _coordinates.begin(); } + /** + * @brief end() of const_iterator for this class + * + * @return The last iterator of the coordinates + */ + std::vector<int32_t>::const_iterator end() const { return _coordinates.end(); } + +private: + std::vector<int32_t> _coordinates; +}; + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_COORDINATES_H__ diff --git a/runtimes/neurun/core/include/util/GeneralConfigSource.h b/runtimes/neurun/core/include/util/GeneralConfigSource.h new file mode 100644 index 000000000..04e3332b3 --- /dev/null +++ b/runtimes/neurun/core/include/util/GeneralConfigSource.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 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_GLOBAL_CONFIG_SOURCE_H__ +#define __NEURUN_UTIL_GLOBAL_CONFIG_SOURCE_H__ + +#include <unordered_map> + +#include "util/IConfigSource.h" + +namespace neurun +{ +namespace util +{ + +class GeneralConfigSource : public IConfigSource +{ +public: + GeneralConfigSource() = default; + + std::string get(const std::string &key) const override; + void set(const std::string &key, const std::string &val); + +private: + std::unordered_map<std::string, std::string> _map; +}; + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_GLOBAL_CONFIG_SOURCE_H__ diff --git a/runtimes/neurun/core/include/util/IConfigSource.h b/runtimes/neurun/core/include/util/IConfigSource.h new file mode 100644 index 000000000..a52d87097 --- /dev/null +++ b/runtimes/neurun/core/include/util/IConfigSource.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 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_I_CONFIG_SOURCE_H__ +#define __NEURUN_UTIL_I_CONFIG_SOURCE_H__ + +#include <string> + +namespace neurun +{ +namespace util +{ + +struct IConfigSource +{ + /** + * @brief Destroy the IConfigSource object + */ + virtual ~IConfigSource() = default; + + /** + * @brief get the value for the matching key + * + * @param key string key to search + * @return string value associated with the key + */ + virtual std::string get(const std::string &key) const = 0; +}; + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_I_CONFIG_SOURCE_H__ diff --git a/runtimes/neurun/core/include/util/ITimer.h b/runtimes/neurun/core/include/util/ITimer.h new file mode 100644 index 000000000..79ecdd0ca --- /dev/null +++ b/runtimes/neurun/core/include/util/ITimer.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 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_ITIMER_H__ +#define __NEURUN_UTIL_ITIMER_H__ + +#include <chrono> + +namespace neurun +{ +namespace util +{ + +class ITimer +{ +public: + virtual void handleBegin() = 0; + virtual void handleEnd() = 0; + int getTime() { return _timer_res; }; + + virtual ~ITimer() = default; + +protected: + int _timer_res{0}; +}; + +class CPUTimer : public ITimer +{ +public: + void handleBegin() override { _start_time = std::chrono::steady_clock::now(); }; + + void handleEnd() override + { + const auto end_time = std::chrono::steady_clock::now(); + _timer_res = + std::chrono::duration_cast<std::chrono::microseconds>(end_time - _start_time).count(); + }; + +private: + std::chrono::steady_clock::time_point _start_time; // in microseconds +}; + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_ITIMER_H__ diff --git a/runtimes/neurun/core/include/util/Index.h b/runtimes/neurun/core/include/util/Index.h new file mode 100644 index 000000000..d1fdc237c --- /dev/null +++ b/runtimes/neurun/core/include/util/Index.h @@ -0,0 +1,158 @@ +/* + * 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_INDEX_H__ +#define __NEURUN_UTIL_INDEX_H__ + +#include <functional> +#include <limits> +#include <stdint.h> + +namespace neurun +{ +namespace util +{ + +/** + * @brief A wrapper class for unsigned integral Index + * NOTE : Max value of the underlying type is used as the invalid value + * + * @tparam T Underlying type. Must be unsigned integral type otherwise its behavior is undefined. + * @tparam DummyTag Dummy type to distinguish types with a same underlying type. Using an opaque + * type is recommended. + */ +template <typename T, typename DummyTag> class Index +{ +private: + static const T UNDEFINED = std::numeric_limits<T>::max(); + +public: + /** + * @brief Construct a new Index object + */ + explicit Index(void) : _index{UNDEFINED} {} + /** + * @brief Construct a new Index object with a value in the underlying type + * + * @param o Value in the underlying type + */ + explicit Index(T o) : _index{o} {} + /** + * @brief Copy Constructor + * + * @param o Object to be copied + */ + Index(const Index &o) : _index{o._index} {} + + /** + * @brief Assign a value in the underlying time + * + * @param o Value in the underlying type + * @return Index& Reference of this pointer + */ + Index &operator=(T o) + { + _index = o; + return *this; + } + + /** + * @brief Copy assignment operator + * + * @param o Object to be copied + * @return Index& Reference of this pointer + */ + Index &operator=(const T &o) + { + _index = o._index; + return *this; + } + + /** + * @brief Equality operator + * + * @param o The other value in the underlying type to compare + * @return true if underlying value is the same, false otherwise + */ + bool operator==(T o) const { return _index == o; } + /** + * @brief Equality operator + * + * @param o The other object to compare + * @return true if underlying value is the same, false otherwise + */ + bool operator==(const Index &o) const { return _index == o._index; } + /** + * @brief Inquality operator + * + * @param o The other value in the underlying type to compare + * @return true if underlying value is the same, false otherwise + */ + bool operator!=(T o) const { return !(*this == o); } + /** + * @brief Inquality operator + * + * @param o The other object to compare + * @return true if underlying value is the same, false otherwise + */ + bool operator!=(const Index &o) const { return !(*this == o); } + + /** + * @brief Post increment operator + * + * @return Index Index before increment + */ + Index operator++(int) + { + Index temp = *this; + _index++; + return temp; + } + + /** + * @brief Check whether the value is valid or not + * + * @return true if valid, false otherwise + */ + bool valid() const { return _index != UNDEFINED; } + /** + * @brief Return underlying value + * + * @return T Underlying value + */ + T value() const { return _index; } + +private: + T _index; +}; + +} // namespace util +} // namespace neurun + +namespace std +{ + +template <typename T, typename Tag> struct hash<::neurun::util::Index<T, Tag>> +{ + size_t operator()(const ::neurun::util::Index<T, Tag> &index) const noexcept + { + return hash<T>()(index.value()); + } +}; + +} // namespace std + +#endif // __NEURUN_UTIL_INDEX_H__ diff --git a/runtimes/neurun/core/include/util/ObjectManager.h b/runtimes/neurun/core/include/util/ObjectManager.h new file mode 100644 index 000000000..fd2c3f295 --- /dev/null +++ b/runtimes/neurun/core/include/util/ObjectManager.h @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2019 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_OBJECT_MANAGER_H__ +#define __NEURUN_UTIL_OBJECT_MANAGER_H__ + +#include <unordered_map> +#include <memory> + +namespace neurun +{ +namespace util +{ + +/** + * @brief Class that owns objects and maps them with indices as a handle for them + * + */ +template <typename Index, typename Object> class ObjectManager +{ +public: + ObjectManager() : _index_count{0u} {} + +public: + /** + * @brief Create an object with args and put it in the container with a new Index for that + * + * @param[in] args Arguments for creating Operand object + * @return Created index that is associated to the object + */ + template <class... Args> Index emplace(Args &&... args) + { + auto index = generateIndex(); + _objects.emplace(index, nnfw::cpp14::make_unique<Object>(std::forward<Args>(args)...)); + return index; + } + + /** + * @brief Put object in the container with a new Index for that + * + * @param[in] object Object to be pushed + * @return Created index that is associated to the object + */ + Index push(std::unique_ptr<Object> &&object) + { + auto index = generateIndex(); + _objects.emplace(index, std::move(object)); + return index; + } + + /** + * @brief Remove the object that is associated with the given index + * + * @param[in] index Index of the object to be removed + * @return N/A + */ + void remove(const Index &index) { _objects.erase(index); }; + + /** + * @brief Get the object that is associated with the given index + * + * @param[in] index Index of the object to be returned + * @return Object + */ + const Object &at(const Index &index) const { return *(_objects.at(index)); } + /** + * @brief Get the object that is associated with the given index + * + * @param[in] index Index of the object to be returned + * @return Object + */ + Object &at(const Index &index) { return *(_objects.at(index)); } + /** + * @brief Get the object that is associated with the given index + * + * @param[in] index Index of the object to be returned + * @return true if such entry exists otherwise false + */ + bool exist(const Index &index) const + { + auto it = _objects.find(index); + return it != _objects.end(); + } + /** + * @brief Iterate over the container with given function + * + * @param[in] fn Function to be run for every container entry + * @return N/A + */ + void iterate(const std::function<void(const Index &, const Object &)> &fn) const + { + for (const auto &e : _objects) + { + fn(e.first, *e.second); + } + } + /** + * @brief Iterate over the container with given function + * + * @param[in] fn Function to be run for every container entry + * @return N/A + */ + void 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 + std::list<Index> l; + + for (auto &e : _objects) + { + l.push_back(e.first); + } + + for (auto index : l) + { + fn(index, *_objects[index]); + } + } + +private: + Index generateIndex() { return Index{_index_count++}; } + +private: + std::unordered_map<Index, std::unique_ptr<Object>> _objects; + uint32_t _index_count; +}; + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_OBJECT_MANAGER_H__ diff --git a/runtimes/neurun/core/include/util/Padding.h b/runtimes/neurun/core/include/util/Padding.h new file mode 100644 index 000000000..230013238 --- /dev/null +++ b/runtimes/neurun/core/include/util/Padding.h @@ -0,0 +1,42 @@ +/* + * 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 "model/Shape.h" +#include "model/InternalType.h" + +namespace neurun +{ +namespace util +{ + +model::ExplicitPadding validPadding(void); +model::ExplicitPadding samePadding(const model::FeatureShape &ifm_shape, + const model::FeatureShape &ofm_shape, + const model::Stride &stride, uint32_t kw, uint32_t kh); +model::ExplicitPadding calculatePadding(const model::Padding &padding, + const model::FeatureShape &ifm_shape, + const model::FeatureShape &ofm_shape, + const model::Stride &stride, uint32_t kw, uint32_t kh); + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_PADDING_H__ diff --git a/runtimes/neurun/core/include/util/Set.h b/runtimes/neurun/core/include/util/Set.h new file mode 100644 index 000000000..13213511d --- /dev/null +++ b/runtimes/neurun/core/include/util/Set.h @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2019 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 Set.h + * @brief This file contains neurun::util::Set class + * @ingroup COM_AI_RUNTIME + */ + +#ifndef __NEURUN_UTIL_SET_H__ +#define __NEURUN_UTIL_SET_H__ + +#include <cassert> +#include <unordered_set> + +namespace neurun +{ +namespace util +{ + +/** + * @brief Class for set of custom element + & @tparam Element Key type of Set + */ +template <typename Element> class Set +{ +public: + /** + * @brief Construct default Set object. + */ + Set() = default; + /** + * @brief Construct Set object by copy semantics. + */ + Set(const Set<Element> &) = default; + /** + * @brief Construct move Set object by move semantics. + */ + Set(Set<Element> &&) = default; + +public: + /** + * @brief Add a given element to the set + * + * @param e Element added + */ + void add(const Element &e) { _set.insert(e); } + /** + * @brief remove a given element from the set + * + * @param e Element removed + */ + void remove(const Element &e) { _set.erase(e); } + /** + * @brief Get size of the set + * + * @return The size of the set + */ + uint32_t size() const { return static_cast<uint32_t>(_set.size()); } + /** + * @brief Get whether the set is empty + * + * @return Whether the set is empty + */ + bool empty() const { return _set.empty(); } + /** + * @brief Get whether a given element exists in the set + * + * @param e A given element + * + * @return Whether a given element exists in the set + */ + bool contains(const Element &e) const { return _set.find(e) != _set.end(); } + /** + * @brief Get first element of the set + * + * @return first element of the set + */ + const Element &getOnlyElement() const + { + assert(_set.size() == 1u); + return *_set.begin(); + } + +public: + /** + * @brief operator overloading function for `|` + * + * @return A set with two sets combined + */ + Set<Element> operator|(const Set<Element> &other) const // Union + { + auto ret = *this; + for (auto e : other) + { + ret.add(e); + } + return ret; + } + /** + * @brief operator overloading function for `&` + * + * @return A set of elements that overlap in two sets + */ + Set<Element> operator&(const Set<Element> &other) const // Intersect + { + Set<Element> ret; + for (auto e : other) + { + if (contains(e)) + { + ret.add(e); + } + } + return ret; + } + /** + * @brief operator overloading function for `-` + * + * @return A set of subtracted from another set + */ + Set<Element> operator-(const Set<Element> &other) const // Minus + { + auto ret = *this; + for (auto e : other) + { + ret.remove(e); + } + return ret; + } + +public: + /** + * @brief begin() of const_iterator for this class + * + * @return The first iterator of the set + */ + typename std::unordered_set<Element>::const_iterator begin() const { return _set.begin(); } + /** + * @brief end() of const_iterator for this class + * + * @return The last iterator of the set + */ + typename std::unordered_set<Element>::const_iterator end() const { return _set.end(); } + +private: + std::unordered_set<Element> _set; +}; + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_SET_H__ diff --git a/runtimes/neurun/core/include/util/ShapeInference.h b/runtimes/neurun/core/include/util/ShapeInference.h new file mode 100644 index 000000000..54076199b --- /dev/null +++ b/runtimes/neurun/core/include/util/ShapeInference.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019 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_SHAPE_INFERENCE_H__ +#define __NEURUN_GRAPH_SHAPE_INFERENCE_H__ + +#include "model/operation/AvgPool2DNode.h" +#include "model/operation/ConcatNode.h" +#include "model/operation/MaxPool2DNode.h" +#include "model/operation/Conv2DNode.h" +#include "model/operation/DepthwiseConv2DNode.h" +#include "model/Operands.h" +#include "model/Index.h" +#include "model/Layout.h" + +namespace neurun +{ +namespace shape_inference +{ + +using Shapes = std::vector<model::Shape>; + +Shapes inferEltwiseShape(const model::Shape &lhs_shape, const model::Shape &rhs_shape); + +Shapes inferAvgPoolShape(const model::Shape &in_shape, + const model::operation::AvgPool2DNode::Param ¶m, + model::Layout layout = model::Layout::NHWC); + +Shapes inferConcatShape(const Shapes &in_shapes, const model::operation::ConcatNode::Param ¶m); + +Shapes inferMaxPoolShape(const model::Shape &in_shape, + const model::operation::MaxPool2DNode::Param ¶m, + model::Layout layout = model::Layout::NHWC); + +Shapes inferConv2DShape(const model::Shape &in_shape, const model::Shape &ker_shape, + const model::operation::Conv2DNode::Param ¶m, + model::Layout layout = model::Layout::NHWC); + +Shapes inferDepthwiseConv2DShape(const model::Shape &in_shape, const model::Shape &ker_shape, + const model::operation::DepthwiseConv2DNode::Param ¶m, + model::Layout layout = model::Layout::NHWC); + +Shapes inferFullyConnectedShape(const model::Shape &in_shape, const model::Shape &ker_shape); + +} // namespace shape_inference +} // namespace neurun + +#endif // __NEURUN_GRAPH_SHAPE_INFERENCE_H__ diff --git a/runtimes/neurun/core/include/util/Utils.h b/runtimes/neurun/core/include/util/Utils.h new file mode 100644 index 000000000..c472dd7c8 --- /dev/null +++ b/runtimes/neurun/core/include/util/Utils.h @@ -0,0 +1,52 @@ +/* + * 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 "model/InternalType.h" +#include "model/Layout.h" +#include "model/Operand.h" +#include "util/Coordinates.h" +#include "backend/operand/IObject.h" + +#define UNUSED_RELEASE(a) (void)(a) + +namespace neurun +{ +namespace util +{ + +/** + * @brief Converts a internal padding type to const char* + * @param[in] code Padding type to be converted + * @return A string holding the converted value + */ +const char *to_string(const model::PaddingType &type); + +Coordinates convertCoordinates(const Coordinates &from_coordinates, model::Layout from_layout, + model::Layout to_layout); + +} // namespace util +} // namespace neurun + +#endif // __NEURUN_UTIL_UTILS_H__ diff --git a/runtimes/neurun/core/include/util/feature/Coordinate4D.h b/runtimes/neurun/core/include/util/feature/Coordinate4D.h new file mode 100644 index 000000000..b020ed239 --- /dev/null +++ b/runtimes/neurun/core/include/util/feature/Coordinate4D.h @@ -0,0 +1,111 @@ +/* + * 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 Set batch offset + * @param[in] n Batch offset + */ + void n(int32_t n) { _n = n; } + /** + * @brief Set height offset + * @param[in] h Height offset + */ + void h(int32_t h) { _h = h; } + /** + * @brief Set width offset + * @param[in] w Width offset + */ + void w(int32_t w) { _w = w; } + /** + * @brief Set channel offset + * @param[in] c Channel offset + */ + void c(int32_t c) { _c = c; } + +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/core/include/util/feature/nchw/View.h b/runtimes/neurun/core/include/util/feature/nchw/View.h new file mode 100644 index 000000000..37ee8e398 --- /dev/null +++ b/runtimes/neurun/core/include/util/feature/nchw/View.h @@ -0,0 +1,106 @@ +/* + * 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/operand/ITensor.h" +#include "util/Coordinates.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} + { + assert(tensor->num_dimensions() == 4 && tensor->layout() == model::Layout::NCHW); + _shape.N = tensor->dimension(0); + _shape.C = tensor->dimension(1); + _shape.H = tensor->dimension(2); + _shape.W = tensor->dimension(3); + } + +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::Coordinates{static_cast<int32_t>(batch), static_cast<int32_t>(ch), + static_cast<int32_t>(row), static_cast<int32_t>(col)}); + } + +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/core/include/util/feature/nhwc/Reader.h b/runtimes/neurun/core/include/util/feature/nhwc/Reader.h new file mode 100644 index 000000000..471f62a4b --- /dev/null +++ b/runtimes/neurun/core/include/util/feature/nhwc/Reader.h @@ -0,0 +1,73 @@ +/* + * 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 "util/Utils.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} + { + UNUSED_RELEASE(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/core/include/util/feature/nhwc/Utils.h b/runtimes/neurun/core/include/util/feature/nhwc/Utils.h new file mode 100644 index 000000000..3dab4261c --- /dev/null +++ b/runtimes/neurun/core/include/util/feature/nhwc/Utils.h @@ -0,0 +1,63 @@ +/* + * 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/core/include/util/feature/nhwc/View.h b/runtimes/neurun/core/include/util/feature/nhwc/View.h new file mode 100644 index 000000000..cfaab8ea4 --- /dev/null +++ b/runtimes/neurun/core/include/util/feature/nhwc/View.h @@ -0,0 +1,91 @@ +/* + * 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 <cstddef> + +#include "Utils.h" +#include "util/Utils.h" + +#include "misc/feature/Reader.h" + +namespace neurun +{ +namespace util +{ +namespace feature +{ +namespace nhwc +{ + +// This class is for cpu buffer only, and do not support padding. +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} + { + UNUSED_RELEASE(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/core/include/util/logging.h b/runtimes/neurun/core/include/util/logging.h new file mode 100644 index 000000000..a2fdbdd59 --- /dev/null +++ b/runtimes/neurun/core/include/util/logging.h @@ -0,0 +1,61 @@ +/* + * 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> + +#include "util/ConfigSource.h" + +namespace neurun +{ +namespace util +{ +namespace logging +{ + +class Context +{ +public: + Context() : _enabled{false} + { + const auto env = util::getConfigBool(util::config::NEURUN_LOG_ENABLE); + + if (env) + { + _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__ |