diff options
Diffstat (limited to 'libs/util')
30 files changed, 213 insertions, 1498 deletions
diff --git a/libs/util/CMakeLists.txt b/libs/util/CMakeLists.txt index 565aaf75e..eaa7ae8cf 100644 --- a/libs/util/CMakeLists.txt +++ b/libs/util/CMakeLists.txt @@ -3,12 +3,18 @@ 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) +list(APPEND NNFW_UTILITY_SRCS src/tensor/Comparator.cpp) +if(BUILD_TFLITE_BENCHMARK_MODEL) + list(APPEND NNFW_UTILITY_SRCS src/profiling/time.cc) +endif() add_library(nnfw_util SHARED ${NNFW_UTILITY_SRCS}) target_include_directories(nnfw_util PUBLIC ${NNFW_INCLUDE_DIR}) +add_library(static_nnfw_util STATIC ${NNFW_UTILITY_SRCS}) +target_include_directories(static_nnfw_util PUBLIC ${NNFW_INCLUDE_DIR}) +set_target_properties(static_nnfw_util PROPERTIES POSITION_INDEPENDENT_CODE ON) + install(TARGETS nnfw_util RUNTIME DESTINATION bin COMPONENT libraries LIBRARY DESTINATION lib COMPONENT libraries) diff --git a/libs/util/examples/tensor_index_iterator.cpp b/libs/util/examples/tensor_index_iterator.cpp index a05d78dc4..284e04aa0 100644 --- a/libs/util/examples/tensor_index_iterator.cpp +++ b/libs/util/examples/tensor_index_iterator.cpp @@ -16,16 +16,52 @@ #include "util/tensor/IndexIterator.h" +#include <array> + #include <iostream> +#include <algorithm> + +#include <cassert> + +void test_iterate(void) +{ + const nnfw::util::tensor::Shape shape{3, 4, 7}; + + std::array<int, 3 * 4 * 7> array; + + array.fill(0); + + using nnfw::util::tensor::iterate; + using nnfw::util::tensor::Index; + + iterate(shape) << [&](const Index &index) { + assert(index.rank() == shape.rank()); + + const size_t rank = index.rank(); + + uint32_t offset = index.at(0); + + for (size_t axis = 1; axis < rank; ++axis) + { + offset *= shape.dim(axis); + offset += index.at(axis); + } + + array[offset] += 1; + }; + + assert(std::all_of(array.begin(), array.end(), [](int num) { return num == 1; })); +} int main(int argc, char **argv) { + test_iterate(); + 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) - { + 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) diff --git a/libs/util/include/util/benchmark.h b/libs/util/include/util/benchmark.h deleted file mode 100644 index c451eddec..000000000 --- a/libs/util/include/util/benchmark.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index fa9dd519d..000000000 --- a/libs/util/include/util/environment.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index e77816669..000000000 --- a/libs/util/include/util/feature/Index.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index dd029f4b6..000000000 --- a/libs/util/include/util/feature/IndexIterator.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index ca217b4a8..000000000 --- a/libs/util/include/util/feature/Object.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 112503d80..000000000 --- a/libs/util/include/util/feature/Reader.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index e05c97f51..000000000 --- a/libs/util/include/util/feature/Shape.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 91b4c9fff..000000000 --- a/libs/util/include/util/feature/TextFormatter.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 604435470..000000000 --- a/libs/util/include/util/fp32.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index ea6b48826..000000000 --- a/libs/util/include/util/kernel/IndexIterator.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index ceed7a0b0..000000000 --- a/libs/util/include/util/kernel/RandomObject.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 9d8f33ad6..000000000 --- a/libs/util/include/util/kernel/Reader.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index bd2332989..000000000 --- a/libs/util/include/util/kernel/Shape.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __NNFW_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 deleted file mode 100644 index e74b09229..000000000 --- a/libs/util/include/util/tensor/Index.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 8014a42b6..000000000 --- a/libs/util/include/util/tensor/IndexFormatter.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 56a8c7dd2..000000000 --- a/libs/util/include/util/tensor/IndexIterator.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __NNFW_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 deleted file mode 100644 index ff013ffa2..000000000 --- a/libs/util/include/util/tensor/NonIncreasingStride.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 839bce236..000000000 --- a/libs/util/include/util/tensor/Object.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 654214880..000000000 --- a/libs/util/include/util/tensor/Reader.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index d4edeaada..000000000 --- a/libs/util/include/util/tensor/Shape.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index fc2d94e57..000000000 --- a/libs/util/include/util/tensor/Zipper.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index 49a58a41e..000000000 --- a/libs/util/include/util/vector.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#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 deleted file mode 100644 index b1bc521da..000000000 --- a/libs/util/include/util/vector/Object.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 deleted file mode 100644 index a3c5cb359..000000000 --- a/libs/util/include/util/vector/Reader.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __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 index dca6c5c55..4b18b409f 100644 --- a/libs/util/src/environment.cpp +++ b/libs/util/src/environment.cpp @@ -25,25 +25,23 @@ namespace nnfw namespace util { -int get_env_int(const char* name) +int get_env_int(const char *name, int defaultValue) { const char *value = std::getenv(name); if (value != nullptr) return std::stoi(value); - return 0; + return defaultValue; } -bool get_env_bool(const char* name) +bool get_env_bool(const char *name, bool defaultValue) { const char *value = std::getenv(name); if (value != nullptr) { - if (std::stoi(value)) - return true; - if (!strcasecmp(value, "true")) - return true; + return std::stoi(value) != 0; } - return false; + + return defaultValue; } } // namespace util @@ -74,6 +72,24 @@ bool IntAccessor::access(int &out) const return true; } +FloatAccessor::FloatAccessor(const std::string &tag) : _tag{tag} +{ + // DO NOTHING +} + +bool FloatAccessor::access(float &out) const +{ + auto value = std::getenv(_tag.c_str()); + + if (value == nullptr) + { + return false; + } + + out = std::stof(value); + return true; +} + } // namespace env } // namespace util } // namespace nnfw diff --git a/libs/util/src/profiling/time.cc b/libs/util/src/profiling/time.cc new file mode 100644 index 000000000..6fe1b54dc --- /dev/null +++ b/libs/util/src/profiling/time.cc @@ -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. + */ + +/* Copyright 2018 The TensorFlow Authors. 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/profiling/time.h" + +#include <sys/time.h> + +namespace tflite +{ +namespace profiling +{ +namespace time +{ +uint64_t NowMicros() +{ + struct timeval tv; + gettimeofday(&tv, nullptr); + return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec; +} +} // namespace time +} // namespace profiling +} // namespace tflite diff --git a/libs/util/src/tensor/Comparator.cpp b/libs/util/src/tensor/Comparator.cpp new file mode 100644 index 000000000..89cd687e9 --- /dev/null +++ b/libs/util/src/tensor/Comparator.cpp @@ -0,0 +1,40 @@ +#include "util/tensor/Comparator.h" +#include "util/tensor/Zipper.h" + +#include "util/fp32.h" + +namespace nnfw +{ +namespace util +{ +namespace tensor +{ + +std::vector<Diff<float>> Comparator::compare(const Shape &shape, const Reader<float> &expected, + const Reader<float> &obtained, + Observer *observer) const +{ + std::vector<Diff<float>> res; + + zip(shape, expected, obtained) << + [&](const Index &index, float expected_value, float obtained_value) { + const auto relative_diff = nnfw::util::fp32::relative_diff(expected_value, obtained_value); + + if (!_compare_fn(expected_value, obtained_value)) + { + res.emplace_back(index, expected_value, obtained_value); + } + + // Update max_diff_index, if necessary + if (observer != nullptr) + { + observer->notify(index, expected_value, obtained_value); + } + }; + + return res; +} + +} // namespace tensor +} // namespace util +} // namespace nnfw diff --git a/libs/util/src/tensor/Shape.cpp b/libs/util/src/tensor/Shape.cpp index d177d1382..f1de26fdc 100644 --- a/libs/util/src/tensor/Shape.cpp +++ b/libs/util/src/tensor/Shape.cpp @@ -16,6 +16,8 @@ #include "util/tensor/Shape.h" +#include <cassert> + namespace nnfw { namespace util @@ -32,7 +34,7 @@ bool operator==(const Shape &lhs, const Shape &rhs) for (size_t axis = 0; axis < lhs.rank(); ++axis) { - if(lhs.dim(axis) != rhs.dim(axis)) + if (lhs.dim(axis) != rhs.dim(axis)) { return false; } @@ -41,6 +43,57 @@ bool operator==(const Shape &lhs, const Shape &rhs) return true; } +Shape Shape::from(const std::string &str) +{ + Shape shape(0); + + bool pending = false; + int value = 0; + + for (const char *cur = str.c_str(); true; ++cur) + { + if (*cur == ',' || *cur == '\0') + { + if (pending) + { + shape.append(value); + } + + if (*cur == '\0') + { + break; + } + + pending = false; + value = 0; + continue; + } + + assert(*cur >= '0' && *cur <= '9'); + + pending = true; + value *= 10; + value += *cur - '0'; + } + + return shape; +} + +std::ostream &operator<<(std::ostream &os, const Shape &shape) +{ + if (shape.rank() > 0) + { + os << shape.dim(0); + + for (uint32_t axis = 1; axis < shape.rank(); ++axis) + { + os << "," << shape.dim(axis); + } + } + + return os; +} + } // namespace tensor } // namespace util } // namespace nnfw |