diff options
Diffstat (limited to 'runtime/libs/misc/src')
-rw-r--r-- | runtime/libs/misc/src/RandomGenerator.cpp | 83 | ||||
-rw-r--r-- | runtime/libs/misc/src/tensor/Comparator.cpp | 54 | ||||
-rw-r--r-- | runtime/libs/misc/src/tensor/IndexFormatter.cpp | 49 | ||||
-rw-r--r-- | runtime/libs/misc/src/tensor/NonIncreasingStride.cpp | 46 | ||||
-rw-r--r-- | runtime/libs/misc/src/tensor/Shape.cpp | 107 |
5 files changed, 339 insertions, 0 deletions
diff --git a/runtime/libs/misc/src/RandomGenerator.cpp b/runtime/libs/misc/src/RandomGenerator.cpp new file mode 100644 index 000000000..e7fbc10ca --- /dev/null +++ b/runtime/libs/misc/src/RandomGenerator.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020 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 "misc/RandomGenerator.h" + +namespace nnfw +{ +namespace misc +{ + +template <> uint8_t RandomGenerator::generate<uint8_t>(void) +{ + // The value of type_range is 255. + float type_range = static_cast<float>(std::numeric_limits<uint8_t>::max()) - + static_cast<float>(std::numeric_limits<uint8_t>::min()); + // Most _dist values range from -5.0 to 5.0. + float min_range = -5.0f; + float max_range = 5.0f; + // NOTE shifted_relative_val has Gaussian distribution that origin mean was 0 and standard + // deviation was 2. And then its values are distributed and shift to that mean is 127.5 and range + // is about [0, 255]. + float shifted_relative_val = (_dist(_rand) - min_range) * type_range / (max_range - min_range); + + // shifted_relative_val is adjusted to be mapped to end points of the range, if it is out of range + // values. + if (shifted_relative_val < 0.0f) + { + return 0; + } + else if (shifted_relative_val > type_range) + { + return 255; + } + + // Convert shifted_relative_val from float to uint8 + return static_cast<uint8_t>(shifted_relative_val); +} + +template <> bool RandomGenerator::generate<bool>(void) +{ + std::uniform_int_distribution<> dist(0, 1); // [0, 1] + return dist(_rand); +} + +template <> int32_t RandomGenerator::generate<int32_t>(void) +{ + // Instead of INT_MAX, 99 is chosen because int32_t input does not mean + // that the model can have any value in int32_t can hold. + // For example, one_hot operation gets indices as int32_t tensor. + // However, we usually expect it would hold a value in [0..depth). + // In our given model, depth was 10137. + const int int32_random_max = 99; + std::uniform_int_distribution<> dist(0, int32_random_max); + return dist(_rand); +} + +template <> int64_t RandomGenerator::generate<int64_t>(void) +{ + // Instead of INT_MAX, 99 is chosen because int64_t input does not mean + // that the model can have any value in int64_t can hold. + // For example, one_hot operation gets indices as int64_t tensor. + // However, we usually expect it would hold a value in [0..depth). + // In our given model, depth was 10137. + const int64_t int64_random_max = 99; + std::uniform_int_distribution<> dist(0, int64_random_max); + return dist(_rand); +} + +} // namespace misc +} // namespace nnfw diff --git a/runtime/libs/misc/src/tensor/Comparator.cpp b/runtime/libs/misc/src/tensor/Comparator.cpp new file mode 100644 index 000000000..80a18c11a --- /dev/null +++ b/runtime/libs/misc/src/tensor/Comparator.cpp @@ -0,0 +1,54 @@ +/* + * 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 "misc/tensor/Comparator.h" +#include "misc/tensor/Zipper.h" + +#include "misc/fp32.h" + +namespace nnfw +{ +namespace misc +{ +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) { + 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 misc +} // namespace nnfw diff --git a/runtime/libs/misc/src/tensor/IndexFormatter.cpp b/runtime/libs/misc/src/tensor/IndexFormatter.cpp new file mode 100644 index 000000000..c949db7a8 --- /dev/null +++ b/runtime/libs/misc/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 "misc/tensor/IndexFormatter.h" + +#include <cassert> + +namespace nnfw +{ +namespace misc +{ +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 misc +} // namespace nnfw diff --git a/runtime/libs/misc/src/tensor/NonIncreasingStride.cpp b/runtime/libs/misc/src/tensor/NonIncreasingStride.cpp new file mode 100644 index 000000000..c51ad0324 --- /dev/null +++ b/runtime/libs/misc/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 "misc/tensor/NonIncreasingStride.h" + +#include <cassert> + +namespace nnfw +{ +namespace misc +{ +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 misc +} // namespace nnfw diff --git a/runtime/libs/misc/src/tensor/Shape.cpp b/runtime/libs/misc/src/tensor/Shape.cpp new file mode 100644 index 000000000..70d3bdfdb --- /dev/null +++ b/runtime/libs/misc/src/tensor/Shape.cpp @@ -0,0 +1,107 @@ +/* + * 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 "misc/tensor/Shape.h" + +#include <cassert> +#include <functional> +#include <numeric> + +namespace nnfw +{ +namespace misc +{ +namespace tensor +{ + +bool operator==(const Shape &lhs, const Shape &rhs) +{ + if (lhs.rank() != rhs.rank()) + { + return false; + } + + for (uint32_t axis = 0; axis < lhs.rank(); ++axis) + { + if (lhs.dim(axis) != rhs.dim(axis)) + { + return false; + } + } + + 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; +} + +uint64_t Shape::num_elements() const +{ + return std::accumulate(_dimensions.cbegin(), _dimensions.cend(), UINT64_C(1), + std::multiplies<uint64_t>()); +} + +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 misc +} // namespace nnfw |