diff options
Diffstat (limited to 'libs/util')
30 files changed, 1760 insertions, 0 deletions
diff --git a/libs/util/CMakeLists.txt b/libs/util/CMakeLists.txt new file mode 100644 index 000000000..565aaf75e --- /dev/null +++ b/libs/util/CMakeLists.txt @@ -0,0 +1,17 @@ +# Library `nnfw_util` +set(NNFW_UTILITY_SRCS src/environment.cpp) +list(APPEND NNFW_UTILITY_SRCS src/tensor/Shape.cpp) +list(APPEND NNFW_UTILITY_SRCS src/tensor/NonIncreasingStride.cpp) +list(APPEND NNFW_UTILITY_SRCS src/tensor/IndexFormatter.cpp) + +set(NNFW_INCLUDE_DIR include) + +add_library(nnfw_util SHARED ${NNFW_UTILITY_SRCS}) +target_include_directories(nnfw_util PUBLIC ${NNFW_INCLUDE_DIR}) + +install(TARGETS nnfw_util + RUNTIME DESTINATION bin COMPONENT libraries + LIBRARY DESTINATION lib COMPONENT libraries) + +add_executable(nnfw_util_tensor_index_iterator "examples/tensor_index_iterator.cpp") +target_link_libraries(nnfw_util_tensor_index_iterator nnfw_util) diff --git a/libs/util/examples/tensor_index_iterator.cpp b/libs/util/examples/tensor_index_iterator.cpp new file mode 100644 index 000000000..a05d78dc4 --- /dev/null +++ b/libs/util/examples/tensor_index_iterator.cpp @@ -0,0 +1,38 @@ +/* + * 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/tensor/IndexIterator.h" + +#include <iostream> + +int main(int argc, char **argv) +{ + nnfw::util::tensor::Shape shape{3, 4, 3, 4}; + + std::cout << "Iterate over tensor{3, 4, 3, 4}" << std::endl; + + nnfw::util::tensor::iterate(shape) << [] (const nnfw::util::tensor::Index &index) + { + std::cout << "rank: " << index.rank() << std::endl; + + for (size_t d = 0; d < index.rank(); ++d) + { + std::cout << " offset(" << d << ") = " << index.at(d) << std::endl; + } + }; + + return 0; +} diff --git a/libs/util/include/util/benchmark.h b/libs/util/include/util/benchmark.h new file mode 100644 index 000000000..c451eddec --- /dev/null +++ b/libs/util/include/util/benchmark.h @@ -0,0 +1,66 @@ +/* + * 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_UTIL_BENCHMARK_H__ +#define __NNFW_UTIL_BENCHMARK_H__ + +#include <chrono> + +namespace nnfw +{ +namespace util +{ +// Benckmark support +namespace benchmark +{ + +template <typename T> class Accumulator +{ +public: + Accumulator(T &ref) : _ref(ref) + { + // DO NOTHING + } + +public: + T &operator()(void) { return _ref; } + +private: + T &_ref; +}; + +template <typename T, typename Callable> +Accumulator<T> &operator<<(Accumulator<T> &&acc, Callable cb) +{ + auto begin = std::chrono::steady_clock::now(); + cb(); + auto end = std::chrono::steady_clock::now(); + + acc() += std::chrono::duration_cast<T>(end - begin); + + return acc; +} + +template <typename T> Accumulator<T> measure(T &out) +{ + return Accumulator<T>(out); +} + +} // namespace benchmark +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_BENCHMARK_H__ diff --git a/libs/util/include/util/environment.h b/libs/util/include/util/environment.h new file mode 100644 index 000000000..fa9dd519d --- /dev/null +++ b/libs/util/include/util/environment.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 __UTIL_ENVIRONMENT_H__ +#define __UTIL_ENVIRONMENT_H__ + +namespace nnfw +{ +namespace util +{ + +int get_env_int(const char* name); +bool get_env_bool(const char* name); + +} +} + +#include <string> + +namespace nnfw +{ +namespace util +{ +namespace env +{ + +template <typename T> struct Accessor +{ + virtual ~Accessor() = default; + + virtual bool access(T &out) const = 0; +}; + +class IntAccessor : public Accessor<int> +{ +public: + IntAccessor(const std::string &tag); + +public: + bool access(int &out) const override; + +private: + std::string _tag; +}; + +} // namespace env +} // namespace util +} // namespace nnfw + +#endif // __UTIL_ENVIRONMENT_H__ diff --git a/libs/util/include/util/feature/Index.h b/libs/util/include/util/feature/Index.h new file mode 100644 index 000000000..e77816669 --- /dev/null +++ b/libs/util/include/util/feature/Index.h @@ -0,0 +1,60 @@ +/* + * 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_UTIL_FEATURE_INDEX_H__ +#define __NNFW_UTIL_FEATURE_INDEX_H__ + +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace feature +{ + +class Index +{ +public: + Index() = default; + +public: + Index(int32_t ch, int32_t row, int32_t col) : _ch{ch}, _row{row}, _col{col} + { + // DO NOTHING + } + +public: + int32_t ch(void) const { return _ch; } + int32_t row(void) const { return _row; } + int32_t col(void) const { return _col; } + +public: + int32_t &ch(void) { return _ch; } + int32_t &row(void) { return _row; } + int32_t &col(void) { return _col; } + +private: + int32_t _ch; + int32_t _row; + int32_t _col; +}; + +} // namespace feature +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_INDEX_H__ diff --git a/libs/util/include/util/feature/IndexIterator.h b/libs/util/include/util/feature/IndexIterator.h new file mode 100644 index 000000000..dd029f4b6 --- /dev/null +++ b/libs/util/include/util/feature/IndexIterator.h @@ -0,0 +1,69 @@ +/* + * 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_UTIL_FEATURE_INDEX_ITERATOR_H__ +#define __NNFW_UTIL_FEATURE_INDEX_ITERATOR_H__ + +#include "util/feature/Shape.h" + +namespace nnfw +{ +namespace util +{ +namespace feature +{ + +class IndexIterator +{ +public: + IndexIterator(const Shape &shape) : _shape{shape} + { + // DO NOTHING + } + +public: + template <typename Callable> IndexIterator &iter(Callable cb) + { + for (uint32_t ch = 0; ch < _shape.C; ++ch) + { + for (uint32_t row = 0; row < _shape.H; ++row) + { + for (uint32_t col = 0; col < _shape.W; ++col) + { + cb(ch, row, col); + } + } + } + + return (*this); + } + +private: + const Shape _shape; +}; + +IndexIterator iterate(const Shape &shape) { return IndexIterator{shape}; } + +template <typename Callable> IndexIterator &operator<<(IndexIterator &&it, Callable cb) +{ + return it.iter(cb); +} + +} // namespace feature +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_INDEX_ITERATOR_H__ diff --git a/libs/util/include/util/feature/Object.h b/libs/util/include/util/feature/Object.h new file mode 100644 index 000000000..ca217b4a8 --- /dev/null +++ b/libs/util/include/util/feature/Object.h @@ -0,0 +1,79 @@ +/* + * 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_UTIL_FEATURE_OBJECT_H__ +#define __NNFW_UTIL_FEATURE_OBJECT_H__ + +#include "util/feature/Shape.h" +#include "util/feature/Index.h" +#include "util/feature/Reader.h" + +#include <vector> + +namespace nnfw +{ +namespace util +{ +namespace feature +{ + +template<typename T> class Object final : public Reader<T> +{ +public: + using Generator = std::function<T (const Shape &shape, const Index &index)>; + +public: + Object(const Shape &shape, const Generator &fn) : _shape{shape} + { + _value.resize(_shape.C * _shape.H * _shape.W); + + for (int32_t ch = 0; ch < _shape.C; ++ch) + { + for (int32_t row = 0; row < _shape.H; ++row) + { + for (int32_t col = 0; col < _shape.W; ++col) + { + _value.at(offsetOf(ch, row, col)) = fn(_shape, Index{ch, row, col}); + } + } + } + } + +public: + const Shape &shape(void) const { return _shape; } + +public: + T at(uint32_t ch, uint32_t row, uint32_t col) const override + { + return _value.at(offsetOf(ch, row, col)); + } + +private: + uint32_t offsetOf(uint32_t ch, uint32_t row, uint32_t col) const + { + return ch * _shape.H * _shape.W + row * _shape.W + col; + } + +private: + Shape _shape; + std::vector<T> _value; +}; + +} // namespace feature +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_OBJECT_H__ diff --git a/libs/util/include/util/feature/Reader.h b/libs/util/include/util/feature/Reader.h new file mode 100644 index 000000000..112503d80 --- /dev/null +++ b/libs/util/include/util/feature/Reader.h @@ -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 __NNFW_UTIL_FEATURE_READER_H__ +#define __NNFW_UTIL_FEATURE_READER_H__ + +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace feature +{ + +template <typename T> struct Reader +{ + virtual ~Reader() = default; + + virtual T at(uint32_t ch, uint32_t row, uint32_t col) const = 0; +}; + +} // namespace feature +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_READER_H__ diff --git a/libs/util/include/util/feature/Shape.h b/libs/util/include/util/feature/Shape.h new file mode 100644 index 000000000..e05c97f51 --- /dev/null +++ b/libs/util/include/util/feature/Shape.h @@ -0,0 +1,47 @@ +/* + * 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_UTIL_FEATURE_SHAPE_H__ +#define __NNFW_UTIL_FEATURE_SHAPE_H__ + +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace feature +{ + +struct Shape +{ + int32_t C; // Depth + int32_t H; // Height + int32_t W; // Width + + Shape() = default; + Shape(int32_t depth, int32_t height, int32_t width) : C{depth}, H{height}, W{width} + { + // DO NOTHING + } + +}; + +} // namespace feature +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_H__ diff --git a/libs/util/include/util/feature/TextFormatter.h b/libs/util/include/util/feature/TextFormatter.h new file mode 100644 index 000000000..91b4c9fff --- /dev/null +++ b/libs/util/include/util/feature/TextFormatter.h @@ -0,0 +1,84 @@ +/* + * 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_UTIL_FEATURE_TEXT_FORMATTER_H__ +#define __NNFW_UTIL_FEATURE_TEXT_FORMATTER_H__ + +#include "util/feature/Shape.h" +#include "util/feature/Reader.h" + +#include <ostream> +#include <iomanip> +#include <limits> + +namespace nnfw +{ +namespace util +{ +namespace feature +{ + +template <typename T> class TextFormatter +{ +public: + TextFormatter(const Shape &shape, const Reader<T> &data) + : _shape(shape), _data(data) + { + // DO NOTHING + } + +public: + const Shape &shape(void) const { return _shape; } + const Reader<T> &data(void) const { return _data; } + +private: + const Shape &_shape; + const Reader<T> &_data; +}; + +template <typename T> +std::ostream &operator<<(std::ostream &os, const TextFormatter<T> &fmt) +{ + const auto &shape = fmt.shape(); + + for (uint32_t ch = 0; ch < shape.C; ++ch) + { + os << " Channel " << ch << ":" << std::endl; + for (uint32_t row = 0; row < shape.H; ++row) + { + os << " "; + for (uint32_t col = 0; col < shape.W; ++col) + { + const auto value = fmt.data().at(ch, row, col); + os << std::right; + os << std::fixed; + os << std::setw(std::numeric_limits<T>::digits10 + 2); + os << std::setprecision(5); + os << value; + os << " "; + } + os << std::endl; + } + } + + return os; +} + +} // namespace feature +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_TEXT_FORMATTER_H__ diff --git a/libs/util/include/util/fp32.h b/libs/util/include/util/fp32.h new file mode 100644 index 000000000..604435470 --- /dev/null +++ b/libs/util/include/util/fp32.h @@ -0,0 +1,71 @@ +/* + * 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_UTIL_FP32_H__ +#define __NNFW_UTIL_FP32_H__ + +#include <cmath> +#include <cfloat> +#include <algorithm> +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace fp32 +{ + +inline float relative_diff(float lhs, float rhs) +{ + const auto diff = std::fabs(lhs - rhs); + const auto base = std::max(std::fabs(lhs), std::fabs(rhs)); + + return diff / base; +} + +inline bool epsilon_equal(float expected, float obtained, uint32_t tolerance = 1) +{ + if (std::isnan(expected) && std::isnan(obtained)) + { + return true; + } + + // Let's use relative epsilon comparision + const auto diff = std::fabs(expected - obtained); + const auto max = std::max(std::fabs(expected), std::fabs(obtained)); + + return diff <= (max * FLT_EPSILON * tolerance); +} + +inline bool absolute_epsilon_equal(float expected, float obtained, float tolerance = 0.001) +{ + if (std::isnan(expected) && std::isnan(obtained)) + { + return true; + } + + // Let's use absolute epsilon comparision + const auto diff = std::fabs(expected - obtained); + + return diff <= tolerance; +} + +} // namespace fp32 +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FP32_H__ diff --git a/libs/util/include/util/kernel/IndexIterator.h b/libs/util/include/util/kernel/IndexIterator.h new file mode 100644 index 000000000..ea6b48826 --- /dev/null +++ b/libs/util/include/util/kernel/IndexIterator.h @@ -0,0 +1,72 @@ +/* + * 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_UTIL_KERNEL_INDEX_ITERATOR_H__ +#define __NNFW_UTIL_KERNEL_INDEX_ITERATOR_H__ + +#include "util/kernel/Shape.h" + +namespace nnfw +{ +namespace util +{ +namespace kernel +{ + +class IndexIterator +{ +public: + IndexIterator(const Shape &shape) : _shape{shape} + { + // DO NOTHING + } + +public: + template <typename Callable> IndexIterator &iter(Callable cb) + { + for (uint32_t nth = 0; nth < _shape.N; ++nth) + { + for (uint32_t ch = 0; ch < _shape.C; ++ch) + { + for (uint32_t row = 0; row < _shape.H; ++row) + { + for (uint32_t col = 0; col < _shape.W; ++col) + { + cb(nth, ch, row, col); + } + } + } + } + + return (*this); + } + +private: + const Shape _shape; +}; + +IndexIterator iterate(const Shape &shape) { return IndexIterator{shape}; } + +template <typename Callable> IndexIterator &operator<<(IndexIterator &&it, Callable cb) +{ + return it.iter(cb); +} + +} // namespace kernel +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_INDEX_ITERATOR_H__ diff --git a/libs/util/include/util/kernel/RandomObject.h b/libs/util/include/util/kernel/RandomObject.h new file mode 100644 index 000000000..ceed7a0b0 --- /dev/null +++ b/libs/util/include/util/kernel/RandomObject.h @@ -0,0 +1,71 @@ +/* + * 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_UTIL_KERNEL_RANDOM_OBJECT_H__ +#define __NNFW_UTIL_KERNEL_RANDOM_OBJECT_H__ + +#include "util/kernel/Shape.h" +#include "util/kernel/Reader.h" + +#include <vector> + +namespace nnfw +{ +namespace util +{ +namespace kernel +{ + +template<typename T> class RandomObject final : public Reader<T> +{ +public: + RandomObject(const Shape &shape) : _shape{shape} + { + const uint32_t size = _shape.N * _shape.C * _shape.H * _shape.W; + + // TODO Use random number + for (uint32_t off = 0; off < size; ++off) + { + _value.emplace_back(static_cast<float>(off)); + } + } + +public: + const Shape &shape(void) const { return _shape; } + +public: + T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override + { + uint32_t index = 0; + + index += nth * _shape.C * _shape.H * _shape.W; + index += ch * _shape.H * _shape.W; + index += row * _shape.W; + index += col; + + return _value.at(index); + } + +private: + const Shape _shape; + std::vector<T> _value; +}; + +} // namespace kernel +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_KERNEL_RANDOM_OBJECT_H__ diff --git a/libs/util/include/util/kernel/Reader.h b/libs/util/include/util/kernel/Reader.h new file mode 100644 index 000000000..9d8f33ad6 --- /dev/null +++ b/libs/util/include/util/kernel/Reader.h @@ -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 __NNFW_UTIL_KERNEL_READER_H__ +#define __NNFW_UTIL_KERNEL_READER_H__ + +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace kernel +{ + +template <typename T> struct Reader +{ + virtual ~Reader() = default; + + virtual T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const = 0; +}; + +} // namespace kernel +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_KERNEL_READER_H__ diff --git a/libs/util/include/util/kernel/Shape.h b/libs/util/include/util/kernel/Shape.h new file mode 100644 index 000000000..bd2332989 --- /dev/null +++ b/libs/util/include/util/kernel/Shape.h @@ -0,0 +1,48 @@ +/* + * 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_UTIL_KERNEL_SHAPE_H__ +#define __NNFW_UTIL_KERNEL_SHAPE_H__ + +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace kernel +{ + +struct Shape +{ + int32_t N; + int32_t C; + int32_t H; + int32_t W; + + Shape() = default; + Shape(int32_t count, int32_t depth, int32_t height, int32_t width) + : N{count}, C{depth}, H{height}, W{width} + { + // DO NOTHING + } +}; + +} // namespace kernel +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_KERNEL_SHAPE_H__ diff --git a/libs/util/include/util/tensor/Index.h b/libs/util/include/util/tensor/Index.h new file mode 100644 index 000000000..e74b09229 --- /dev/null +++ b/libs/util/include/util/tensor/Index.h @@ -0,0 +1,62 @@ +/* + * 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_UTIL_TENSOR_INDEX_H__ +#define __NNFW_UTIL_TENSOR_INDEX_H__ + +#include <cstdint> +#include <cstddef> + +#include <vector> +#include <initializer_list> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +struct Index +{ +public: + Index(size_t rank) + { + _offsets.resize(rank); + } + +public: + Index(std::initializer_list<int32_t> offsets) : _offsets{offsets} + { + // DO NOTHING + } + +public: + size_t rank(void) const { return _offsets.size(); } + +public: + int32_t at(size_t n) const { return _offsets.at(n); } + int32_t &at(size_t n) { return _offsets.at(n); } + +private: + std::vector<int32_t> _offsets; +}; + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_INDEX_H__ diff --git a/libs/util/include/util/tensor/IndexFormatter.h b/libs/util/include/util/tensor/IndexFormatter.h new file mode 100644 index 000000000..8014a42b6 --- /dev/null +++ b/libs/util/include/util/tensor/IndexFormatter.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. + */ + +#ifndef __NNFW_UTIL_TENSOR_INDEX_FORMATTER_H__ +#define __NNFW_UTIL_TENSOR_INDEX_FORMATTER_H__ + +#include "util/tensor/Index.h" + +#include <ostream> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +class IndexFormatter +{ +public: + IndexFormatter(const nnfw::util::tensor::Index &index) : _index(index) + { + // DO NOTHING + } + +public: + const nnfw::util::tensor::Index &index(void) const { return _index; } + +private: + const nnfw::util::tensor::Index &_index; +}; + +std::ostream &operator<<(std::ostream &os, const IndexFormatter &fmt); + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_INDEX_FORMATTER_H__ diff --git a/libs/util/include/util/tensor/IndexIterator.h b/libs/util/include/util/tensor/IndexIterator.h new file mode 100644 index 000000000..56a8c7dd2 --- /dev/null +++ b/libs/util/include/util/tensor/IndexIterator.h @@ -0,0 +1,104 @@ +/* + * 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_UTIL_TENSOR_INDEX_ITERATOR_H__ +#define __NNFW_UTIL_TENSOR_INDEX_ITERATOR_H__ + +#include "util/tensor/Shape.h" +#include "util/tensor/Index.h" + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +class IndexIterator +{ +public: + IndexIterator(const Shape &shape) : _shape(shape) + { + // DO NOTHING + } + +public: + // Allow move, but disallow copy + IndexIterator(IndexIterator &&) = default; + IndexIterator(const IndexIterator &) = delete; + +public: + template <typename Callable> IndexIterator &iter(Callable fn) + { + Index index(_shape.rank()); + + for (size_t d = 0; d < _shape.rank(); ++d) + { + index.at(d) = 0; + } + + size_t cursor = 0; + + while (cursor < _shape.rank()) + { + fn(index); + + if (index.at(cursor) + 1 < _shape.dim(cursor)) + { + index.at(cursor) += 1; + } + else + { + while ((cursor < _shape.rank()) && (index.at(cursor) + 1 == _shape.dim(cursor))) + { + ++cursor; + } + + if (cursor == _shape.rank()) + { + break; + } + + index.at(cursor) += 1; + + for (size_t d = 0; d < cursor; ++d) + { + index.at(d) = 0; + } + + cursor = 0; + } + } + + return (*this); + } + +private: + const Shape &_shape; +}; + +inline IndexIterator iterate(const Shape &shape) { return IndexIterator{shape}; } + +template <typename Callable> IndexIterator &operator<<(IndexIterator &&it, Callable cb) +{ + return it.iter(cb); +} + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_INDEX_ITERATOR_H__ diff --git a/libs/util/include/util/tensor/NonIncreasingStride.h b/libs/util/include/util/tensor/NonIncreasingStride.h new file mode 100644 index 000000000..ff013ffa2 --- /dev/null +++ b/libs/util/include/util/tensor/NonIncreasingStride.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 __NNFW_UTIL_TENSOR_NON_INCREASING_STRIDE_H__ +#define __NNFW_UTIL_TENSOR_NON_INCREASING_STRIDE_H__ + +#include "util/tensor/Shape.h" +#include "util/tensor/Index.h" + +#include <vector> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +// As its name suggests, stride[N-1] >= stride[N] holds for all N < rank in NonIncreasingStride. +class NonIncreasingStride +{ +public: + void init(const Shape &shape) + { + _stride.resize(shape.rank()); + _stride.at(shape.rank() - 1) = 1; + + for (uint32_t axis = shape.rank() - 1; axis > 0; --axis) + { + _stride.at(axis - 1) = _stride.at(axis) * shape.dim(axis); + } + } + +public: + uint32_t at(uint32_t axis) const { return _stride.at(axis); } + +public: + uint32_t offset(const Index &index) const; + +private: + std::vector<uint32_t> _stride; +}; + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_NON_INCREASING_STRIDE_H__ diff --git a/libs/util/include/util/tensor/Object.h b/libs/util/include/util/tensor/Object.h new file mode 100644 index 000000000..839bce236 --- /dev/null +++ b/libs/util/include/util/tensor/Object.h @@ -0,0 +1,77 @@ +/* + * 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_UTIL_TENSOR_OBJECT_H__ +#define __NNFW_UTIL_TENSOR_OBJECT_H__ + +#include "util/tensor/Shape.h" +#include "util/tensor/Index.h" +#include "util/tensor/IndexIterator.h" +#include "util/tensor/NonIncreasingStride.h" +#include "util/tensor/Reader.h" + +#include <vector> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +template<typename T> class Object final : public Reader<T> +{ +public: + using Generator = std::function<T (const Shape &shape, const Index &index)>; + +public: + Object(const Shape &shape, const Generator &fn) : _shape{shape} + { + // Set 'stride' + _stride.init(shape); + + // Pre-allocate buffer + _values.resize(_shape.dim(0) * _stride.at(0)); + + // Set 'value' + iterate(_shape) << [this, &fn] (const Index &index) + { + _values.at(_stride.offset(index)) = fn(_shape, index); + }; + } + +public: + const Shape &shape(void) const { return _shape; } + +public: + T at(const Index &index) const override + { + return _values.at(_stride.offset(index)); + } + +private: + Shape _shape; + NonIncreasingStride _stride; + +private: + std::vector<T> _values; +}; + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_FEATURE_OBJECT_H__ diff --git a/libs/util/include/util/tensor/Reader.h b/libs/util/include/util/tensor/Reader.h new file mode 100644 index 000000000..654214880 --- /dev/null +++ b/libs/util/include/util/tensor/Reader.h @@ -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 __NNFW_UTIL_TENSOR_READER_H__ +#define __NNFW_UTIL_TENSOR_READER_H__ + +#include "util/tensor/Index.h" + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +template <typename T> struct Reader +{ + virtual ~Reader() = default; + + virtual T at(const Index &index) const = 0; +}; + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_READER_H__ diff --git a/libs/util/include/util/tensor/Shape.h b/libs/util/include/util/tensor/Shape.h new file mode 100644 index 000000000..d4edeaada --- /dev/null +++ b/libs/util/include/util/tensor/Shape.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 __NNFW_UTIL_TENSOR_SHAPE_H__ +#define __NNFW_UTIL_TENSOR_SHAPE_H__ + +#include <cstdint> +#include <cstddef> +#include <vector> +#include <initializer_list> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +class Shape +{ +public: + Shape(size_t rank) + { + _dimensions.resize(rank); + } + +public: + Shape(const std::initializer_list<int32_t> &dimensions) : _dimensions{dimensions} + { + // DO NOTHING + } + +public: + size_t rank(void) const { return _dimensions.size(); } + +public: + int32_t dim(size_t n) const { return _dimensions.at(n); } + int32_t &dim(size_t n) { return _dimensions.at(n); } + +private: + std::vector<int32_t> _dimensions; +}; + +bool operator==(const Shape &, const Shape &); + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_SHAPE_H__ diff --git a/libs/util/include/util/tensor/Zipper.h b/libs/util/include/util/tensor/Zipper.h new file mode 100644 index 000000000..fc2d94e57 --- /dev/null +++ b/libs/util/include/util/tensor/Zipper.h @@ -0,0 +1,72 @@ +/* + * 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_UTIL_TENSOR_ZIPPER_H__ +#define __NNFW_UTIL_TENSOR_ZIPPER_H__ + +#include "util/tensor/Index.h" +#include "util/tensor/IndexIterator.h" +#include "util/tensor/Reader.h" + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +template <typename T> class Zipper +{ +public: + Zipper(const Shape &shape, const Reader<T> &lhs, const Reader<T> &rhs) + : _shape{shape}, _lhs{lhs}, _rhs{rhs} + { + // DO NOTHING + } + +public: + template <typename Callable> void zip(Callable cb) const + { + iterate(_shape) << [this, &cb] (const Index &index) + { + cb(index, _lhs.at(index), _rhs.at(index)); + }; + } + +private: + const Shape &_shape; + const Reader<T> &_lhs; + const Reader<T> &_rhs; +}; + +template<typename T, typename Callable> +const Zipper<T> &operator<<(const Zipper<T> &zipper, Callable cb) +{ + zipper.zip(cb); + return zipper; +} + +template<typename T> +Zipper<T> zip(const Shape &shape, const Reader<T> &lhs, const Reader<T> &rhs) +{ + return Zipper<T>{shape, lhs, rhs}; +} + +} // namespace tensor +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_TENSOR_ZIPPER_H__ diff --git a/libs/util/include/util/vector.h b/libs/util/include/util/vector.h new file mode 100644 index 000000000..49a58a41e --- /dev/null +++ b/libs/util/include/util/vector.h @@ -0,0 +1,41 @@ +/* + * 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_UTIL_VECTOR_H__ +#define __NNFW_UTIL_VECTOR_H__ + +#include <vector> + +template <typename T> +bool operator==(const std::vector<T> &lhs, const std::vector<T> &rhs) +{ + if (lhs.size() != rhs.size()) + { + return false; + } + + for (size_t ind = 0; ind < lhs.size(); ++ind) + { + if (lhs.at(ind) != rhs.at(ind)) + { + return false; + } + } + + return true; +} + +#endif // __NNFW_UTIL_VECTOR_H__ diff --git a/libs/util/include/util/vector/Object.h b/libs/util/include/util/vector/Object.h new file mode 100644 index 000000000..b1bc521da --- /dev/null +++ b/libs/util/include/util/vector/Object.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 __NNFW_UTIL_VECTOR_OBJECT_H__ +#define __NNFW_UTIL_VECTOR_OBJECT_H__ + +#include "util/vector/Reader.h" + +#include <vector> +#include <functional> + +namespace nnfw +{ +namespace util +{ +namespace vector +{ + +template<typename T> class Object final : public Reader<T> +{ +public: + using Generator = std::function<T (int32_t size, int32_t offset)>; + +public: + Object(int32_t size, const Generator &gen) : _size{size} + { + _value.resize(_size); + + for (int32_t offset = 0; offset < size; ++offset) + { + _value.at(offset) = gen(size, offset); + } + } + +public: + int32_t size(void) const { return _size; } + +public: + T at(uint32_t nth) const override { return _value.at(nth); } + +private: + const int32_t _size; + std::vector<T> _value; +}; + +} // namespace vector +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_VECTOR_OBJECT_H__ diff --git a/libs/util/include/util/vector/Reader.h b/libs/util/include/util/vector/Reader.h new file mode 100644 index 000000000..a3c5cb359 --- /dev/null +++ b/libs/util/include/util/vector/Reader.h @@ -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 __NNFW_UTIL_VECTOR_READER_H__ +#define __NNFW_UTIL_VECTOR_READER_H__ + +#include <cstdint> + +namespace nnfw +{ +namespace util +{ +namespace vector +{ + +template <typename T> struct Reader +{ + virtual ~Reader() = default; + + virtual T at(uint32_t nth) const = 0; +}; + +} // namespace vector +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_VECTOR_READER_H__ diff --git a/libs/util/src/environment.cpp b/libs/util/src/environment.cpp new file mode 100644 index 000000000..dca6c5c55 --- /dev/null +++ b/libs/util/src/environment.cpp @@ -0,0 +1,79 @@ +/* + * 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 <string.h> +#include <cstdlib> +#include <string> + +#include "util/environment.h" + +namespace nnfw +{ +namespace util +{ + +int get_env_int(const char* name) +{ + const char *value = std::getenv(name); + if (value != nullptr) + return std::stoi(value); + return 0; +} + +bool get_env_bool(const char* name) +{ + const char *value = std::getenv(name); + if (value != nullptr) + { + if (std::stoi(value)) + return true; + if (!strcasecmp(value, "true")) + return true; + } + return false; +} + +} // namespace util +} // namespace nnfw + +namespace nnfw +{ +namespace util +{ +namespace env +{ + +IntAccessor::IntAccessor(const std::string &tag) : _tag{tag} +{ + // DO NOTHING +} + +bool IntAccessor::access(int &out) const +{ + auto value = std::getenv(_tag.c_str()); + + if (value == nullptr) + { + return false; + } + + out = std::stoi(value); + return true; +} + +} // namespace env +} // namespace util +} // namespace nnfw diff --git a/libs/util/src/tensor/IndexFormatter.cpp b/libs/util/src/tensor/IndexFormatter.cpp new file mode 100644 index 000000000..66ff80771 --- /dev/null +++ b/libs/util/src/tensor/IndexFormatter.cpp @@ -0,0 +1,49 @@ +/* + * 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/tensor/IndexFormatter.h" + +#include <cassert> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +std::ostream &operator<<(std::ostream &os, const IndexFormatter &fmt) +{ + const auto rank = fmt.index().rank(); + + assert(rank > 0); + + os << fmt.index().at(0); + + if (rank > 1) + { + for (uint32_t axis = 1; axis < rank; ++axis) + { + os << ", " << fmt.index().at(axis); + } + } + + return os; +} + +} // namespace tensor +} // namespace util +} // namespace nnfw diff --git a/libs/util/src/tensor/NonIncreasingStride.cpp b/libs/util/src/tensor/NonIncreasingStride.cpp new file mode 100644 index 000000000..3774ded83 --- /dev/null +++ b/libs/util/src/tensor/NonIncreasingStride.cpp @@ -0,0 +1,46 @@ +/* + * 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/tensor/NonIncreasingStride.h" + +#include <cassert> + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +uint32_t NonIncreasingStride::offset(const Index &index) const +{ + const size_t rank = _stride.size(); + + assert(index.rank() == rank); + + uint32_t offset = 0; + + for (size_t axis = 0; axis < rank; ++axis) + { + offset += _stride.at(axis) * index.at(axis); + } + + return offset; +} + +} // namespace tensor +} // namespace util +} // namespace nnfw diff --git a/libs/util/src/tensor/Shape.cpp b/libs/util/src/tensor/Shape.cpp new file mode 100644 index 000000000..d177d1382 --- /dev/null +++ b/libs/util/src/tensor/Shape.cpp @@ -0,0 +1,46 @@ +/* + * 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/tensor/Shape.h" + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +bool operator==(const Shape &lhs, const Shape &rhs) +{ + if (lhs.rank() != rhs.rank()) + { + return false; + } + + for (size_t axis = 0; axis < lhs.rank(); ++axis) + { + if(lhs.dim(axis) != rhs.dim(axis)) + { + return false; + } + } + + return true; +} + +} // namespace tensor +} // namespace util +} // namespace nnfw |