diff options
Diffstat (limited to 'src/core/utils/helpers')
-rw-r--r-- | src/core/utils/helpers/bit_ops.h | 52 | ||||
-rw-r--r-- | src/core/utils/helpers/fft.cpp | 23 | ||||
-rw-r--r-- | src/core/utils/helpers/fft.h | 55 | ||||
-rw-r--r-- | src/core/utils/helpers/float_ops.h | 119 | ||||
-rw-r--r-- | src/core/utils/helpers/tensor_info.h | 59 | ||||
-rw-r--r-- | src/core/utils/helpers/tensor_transform.cpp | 72 |
6 files changed, 342 insertions, 38 deletions
diff --git a/src/core/utils/helpers/bit_ops.h b/src/core/utils/helpers/bit_ops.h new file mode 100644 index 0000000000..fbd0382509 --- /dev/null +++ b/src/core/utils/helpers/bit_ops.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018-2021, 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_UTILS_HELPERS_BIT_OPS_H +#define ARM_COMPUTE_UTILS_HELPERS_BIT_OPS_H + +#include "support/AclRequires.h" + +#include <type_traits> + +namespace arm_compute +{ +namespace helpers +{ +namespace bit_ops +{ +/** Checks if the idx-th bit is set in an integral type + * + * @param[in] v Integral input + * @param[in] idx Index of the bit to check + * + * @return True if the idx-th bit is set else false + */ +template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)> +bool is_bit_set(T v, unsigned int idx) +{ + return (v & 1 << idx) != 0; +} +} // namespace bit_ops +} // namespace helpers +} // namespace arm_compute +#endif /* ARM_COMPUTE_UTILS_HELPERS_BIT_OPS_H */ diff --git a/src/core/utils/helpers/fft.cpp b/src/core/utils/helpers/fft.cpp index 7ff2fdf62b..edc8d0eacc 100644 --- a/src/core/utils/helpers/fft.cpp +++ b/src/core/utils/helpers/fft.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 ARM Limited. + * Copyright (c) 2019-2020 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "arm_compute/core/utils/helpers/fft.h" +#include "src/core/utils/helpers/fft.h" #include <numeric> @@ -37,7 +37,7 @@ std::vector<unsigned int> decompose_stages(unsigned int N, const std::set<unsign unsigned int res = N; // Early exit if no supported factors are provided - if(supported_factors.empty()) + if (supported_factors.empty()) { return stages; } @@ -46,10 +46,10 @@ std::vector<unsigned int> decompose_stages(unsigned int N, const std::set<unsign auto rfactor_it = supported_factors.rbegin(); // Decomposition step - while(res != 0) + while (res != 0) { const unsigned int factor = *rfactor_it; - if(0 == (res % factor) && res >= factor) + if (0 == (res % factor) && res >= factor) { stages.push_back(factor); res /= factor; @@ -57,9 +57,9 @@ std::vector<unsigned int> decompose_stages(unsigned int N, const std::set<unsign else { ++rfactor_it; - if(rfactor_it == supported_factors.rend()) + if (rfactor_it == supported_factors.rend()) { - if(res > 1) + if (res > 1) { // Couldn't decompose with given factors stages.clear(); @@ -81,8 +81,9 @@ std::vector<unsigned int> digit_reverse_indices(unsigned int N, const std::vecto std::vector<unsigned int> idx_digit_reverse; // Early exit in case N and fft stages do not match - const float stages_prod = std::accumulate(std::begin(fft_stages), std::end(fft_stages), 1, std::multiplies<unsigned int>()); - if(stages_prod != N) + const float stages_prod = + std::accumulate(std::begin(fft_stages), std::end(fft_stages), 1, std::multiplies<unsigned int>()); + if (stages_prod != N) { return idx_digit_reverse; } @@ -94,13 +95,13 @@ std::vector<unsigned int> digit_reverse_indices(unsigned int N, const std::vecto unsigned int n_stages = fft_stages.size(); // Scan elements - for(unsigned int n = 0; n < N; ++n) + for (unsigned int n = 0; n < N; ++n) { unsigned int k = n; unsigned int Nx = fft_stages[0]; // Scan stages - for(unsigned int s = 1; s < n_stages; ++s) + for (unsigned int s = 1; s < n_stages; ++s) { // radix of stage i-th unsigned int Ny = fft_stages[s]; diff --git a/src/core/utils/helpers/fft.h b/src/core/utils/helpers/fft.h new file mode 100644 index 0000000000..f7b99dd7b8 --- /dev/null +++ b/src/core/utils/helpers/fft.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019-2020 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_UTILS_HELPERS_FFT_H +#define ARM_COMPUTE_UTILS_HELPERS_FFT_H + +#include <set> +#include <vector> + +namespace arm_compute +{ +namespace helpers +{ +namespace fft +{ +/** Decompose a given 1D input size using the provided supported factors. + * + * @param[in] N Input size to be decomposed. + * @param[in] supported_factors Supported factors that can be used for decomposition. + * + * @return A vector with the stages of the decomposition. Will be empty if decomposition failed. + */ +std::vector<unsigned int> decompose_stages(unsigned int N, const std::set<unsigned int> &supported_factors); +/** Calculate digit reverse index vector given fft size and the decomposed stages + * + * @param N Input size to calculate digit reverse for + * @param fft_stages A vector with the FFT decomposed stages + * + * @return A vector with the digit reverse indices. Will be empty if it failed. + */ +std::vector<unsigned int> digit_reverse_indices(unsigned int N, const std::vector<unsigned int> &fft_stages); +} // namespace fft +} // namespace helpers +} // namespace arm_compute +#endif /* ARM_COMPUTE_UTILS_HELPERS_FFT_H */ diff --git a/src/core/utils/helpers/float_ops.h b/src/core/utils/helpers/float_ops.h new file mode 100644 index 0000000000..487496915a --- /dev/null +++ b/src/core/utils/helpers/float_ops.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2019-2020, 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H +#define ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H + +#include <cmath> +#include <cstdint> +#include <cstdlib> + +namespace arm_compute +{ +namespace helpers +{ +namespace float_ops +{ +union RawFloat +{ + /** Constructor + * + * @param[in] val Floating-point value + */ + explicit RawFloat(float val) : f32(val) + { + } + /** Extract sign of floating point number + * + * @return Sign of floating point number + */ + int32_t sign() const + { + return i32 >> 31; + } + /** Extract exponent of floating point number + * + * @return Exponent of floating point number + */ + int32_t exponent() const + { + return (i32 >> 23) & 0xFF; + } + /** Extract mantissa of floating point number + * + * @return Mantissa of floating point number + */ + int32_t mantissa() const + { + return i32 & 0x007FFFFF; + } + + int32_t i32; + float f32; +}; + +/** Checks if two floating point numbers are equal given an allowed number of ULPs + * + * @param[in] a First number to compare + * @param[in] b Second number to compare + * @param[in] max_allowed_ulps (Optional) Number of allowed ULPs + * + * @return True if number is close else false + */ +inline bool is_equal_ulps(float a, float b, int max_allowed_ulps = 0) +{ + RawFloat ra(a); + RawFloat rb(b); + + // Check ULP distance + const int ulps = std::abs(ra.i32 - rb.i32); + return ulps <= max_allowed_ulps; +} + +/** Checks if the input floating point number is 1.0f checking if the difference is within a range defined with epsilon + * + * @param[in] a Input floating point number + * @param[in] epsilon (Optional) Epsilon used to define the error bounds + * + * @return True if number is close to 1.0f + */ +inline bool is_one(float a, float epsilon = 0.00001f) +{ + return std::abs(1.0f - a) <= epsilon; +} + +/** Checks if the input floating point number is 0.0f checking if the difference is within a range defined with epsilon + * + * @param[in] a Input floating point number + * @param[in] epsilon (Optional) Epsilon used to define the error bounds + * + * @return True if number is close to 0.0f + */ +inline bool is_zero(float a, float epsilon = 0.00001f) +{ + return std::abs(0.0f - a) <= epsilon; +} +} // namespace float_ops +} // namespace helpers +} // namespace arm_compute +#endif // ACL_SRC_CORE_UTILS_HELPERS_FLOAT_OPS_H diff --git a/src/core/utils/helpers/tensor_info.h b/src/core/utils/helpers/tensor_info.h new file mode 100644 index 0000000000..fd4745a453 --- /dev/null +++ b/src/core/utils/helpers/tensor_info.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019-2020 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_UTILS_HELPERS_TENSOR_INFO_H +#define ARM_COMPUTE_UTILS_HELPERS_TENSOR_INFO_H + +#include "arm_compute/core/ITensorInfo.h" + +namespace arm_compute +{ +namespace helpers +{ +namespace tensor_info +{ +/** Checks if the quantization info of given tensors are different + * + * @param tensor_info_1 Tensor info of the first tensor + * @param tensor_info_2 Tensor info of the second tensor + * @param tensor_infos Tensor infos of the rest tensors + * + * @return True if tensors have mismatching quantization info else false. + */ +template <typename... Ts> +inline bool tensors_have_different_quantization_info(const ITensorInfo *tensor_info_1, + const ITensorInfo *tensor_info_2, + Ts... tensor_infos) +{ + const QuantizationInfo first_quantization_info = tensor_info_1->quantization_info(); + + const std::array<const ITensorInfo *, 1 + sizeof...(Ts)> tensor_infos_array{ + {tensor_info_2, std::forward<Ts>(tensor_infos)...}}; + return std::any_of(tensor_infos_array.begin(), tensor_infos_array.end(), + [&](const ITensorInfo *tensor_info) + { return tensor_info->quantization_info() != first_quantization_info; }); +} +} // namespace tensor_info +} // namespace helpers +} // namespace arm_compute +#endif /* ARM_COMPUTE_UTILS_HELPERS_TENSOR_INFO_H */ diff --git a/src/core/utils/helpers/tensor_transform.cpp b/src/core/utils/helpers/tensor_transform.cpp index cd874b24b3..212cfdabaa 100644 --- a/src/core/utils/helpers/tensor_transform.cpp +++ b/src/core/utils/helpers/tensor_transform.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 ARM Limited. + * Copyright (c) 2018-2020, 2024 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -23,7 +23,7 @@ */ #include "arm_compute/core/utils/helpers/tensor_transform.h" -#include "arm_compute/core/utils/helpers/bit_ops.h" +#include "bit_ops.h" namespace arm_compute { @@ -36,10 +36,11 @@ int calculate_stride_on_index(int index, Coordinates strides) return index >= static_cast<int>(strides.num_dimensions()) ? 1 : strides[index]; } -int calculate_start_on_index(TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask) +int calculate_start_on_index( + TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask) { // Early exit - if(index >= static_cast<int>(starts.num_dimensions())) + if (index >= static_cast<int>(starts.num_dimensions())) { return 0; } @@ -51,14 +52,14 @@ int calculate_start_on_index(TensorShape input_shape, int index, Coordinates sta int start = starts[index]; // Reset in case of begin mask present - if(arm_compute::helpers::bit_ops::is_bit_set(begin_mask, index)) + if (arm_compute::helpers::bit_ops::is_bit_set(begin_mask, index)) { start = stride > 0 ? std::numeric_limits<int>::lowest() : std::numeric_limits<int>::max(); } // Account negative start points const int dim_size = input_shape[index]; - if(start < 0) + if (start < 0) { start += dim_size; } @@ -69,12 +70,16 @@ int calculate_start_on_index(TensorShape input_shape, int index, Coordinates sta return start; } -int calculate_end_on_index(TensorShape input_shape, int index, int start_on_index, - Coordinates ends, Coordinates strides, - int32_t end_mask, int32_t shrink_axis_mask) +int calculate_end_on_index(TensorShape input_shape, + int index, + int start_on_index, + Coordinates ends, + Coordinates strides, + int32_t end_mask, + int32_t shrink_axis_mask) { // Early exit - if(index >= static_cast<int>(ends.num_dimensions())) + if (index >= static_cast<int>(ends.num_dimensions())) { return input_shape[index]; } @@ -86,9 +91,9 @@ int calculate_end_on_index(TensorShape input_shape, int index, int start_on_inde int stop = ends[index]; // Shrink dimension - if(shrink_axis) + if (shrink_axis) { - if(start_on_index == std::numeric_limits<int>::max()) + if (start_on_index == std::numeric_limits<int>::max()) { stop = start_on_index; } @@ -99,33 +104,40 @@ int calculate_end_on_index(TensorShape input_shape, int index, int start_on_inde } // Reset in case of begin mask present - if(arm_compute::helpers::bit_ops::is_bit_set(end_mask, index) && !shrink_axis) + if (arm_compute::helpers::bit_ops::is_bit_set(end_mask, index) && !shrink_axis) { stop = (stride > 0) ? std::numeric_limits<int>::max() : std::numeric_limits<int>::lowest(); } // Account negative end points const int dim_size = input_shape[index]; - if(stop < 0) + if (stop < 0) { stop += dim_size; } // Final clamp - stop = (stride > 0) ? utility::clamp(stop, 0, dim_size) : utility::clamp(stop, -1, dim_size - 1); + if (stride > 0) + stop = utility::clamp(stop, 0, dim_size); + else + stop = utility::clamp(stop, -1, dim_size - 1); return stop; } std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords(TensorShape input_shape, - Coordinates starts, Coordinates ends, Coordinates strides, - int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) + Coordinates starts, + Coordinates ends, + Coordinates strides, + int32_t begin_mask, + int32_t end_mask, + int32_t shrink_axis_mask) { Coordinates starts_abs{}; Coordinates ends_abs{}; Coordinates final_strides{}; - for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) + for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i) { const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask); starts_abs.set(i, start_i); @@ -136,13 +148,19 @@ std::tuple<Coordinates, Coordinates, Coordinates> calculate_strided_slice_coords return std::make_tuple(starts_abs, ends_abs, final_strides); } -TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides, - int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask, bool return_unshrinked) +TensorShape compute_strided_slice_output_shape(TensorShape input_shape, + Coordinates starts, + Coordinates ends, + Coordinates strides, + int32_t begin_mask, + int32_t end_mask, + int32_t shrink_axis_mask, + bool return_unshrinked) { unsigned int index = 0; TensorShape output_shape; - for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i) + for (unsigned int i = 0; i < input_shape.num_dimensions(); ++i) { const int stride = calculate_stride_on_index(index, strides); const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask); @@ -150,11 +168,11 @@ TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordina const int range = end - start; const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i); - if(return_unshrinked || !is_shrink) + if (return_unshrinked || !is_shrink) { - if((range == 0) || // Zero range - (range < 0 && stride >= 0) || // Negative range with positive stride - (range > 0 && stride <= 0)) // Positive range with negative stride + if ((range == 0) || // Zero range + (range < 0 && stride >= 0) || // Negative range with positive stride + (range > 0 && stride <= 0)) // Positive range with negative stride { output_shape.set(index, 0); return output_shape; @@ -173,9 +191,9 @@ int32_t construct_slice_end_mask(Coordinates ends) { // Create end mask int32_t end_mask = 0; - for(unsigned int i = 0; i < ends.num_dimensions(); ++i) + for (unsigned int i = 0; i < ends.num_dimensions(); ++i) { - if(ends[i] < 0) + if (ends[i] < 0) { end_mask |= 1 << i; } |