Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ |
| 25 | #define __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ |
| 26 | |
| 27 | #include "Types.h" |
| 28 | |
| 29 | #include <type_traits> |
| 30 | #include <utility> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | /** Helper function to get the testing range for each activation layer. |
| 39 | * |
| 40 | * @param[in] activation Activation function to test. |
| 41 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1. |
| 42 | * |
| 43 | * @return A pair containing the lower upper testing bounds for a given function. |
| 44 | */ |
| 45 | template <typename T> |
| 46 | std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1) |
| 47 | { |
| 48 | bool is_float = std::is_floating_point<T>::value; |
| 49 | std::pair<T, T> bounds; |
| 50 | |
| 51 | // Set initial values |
| 52 | if(is_float) |
| 53 | { |
| 54 | bounds = std::make_pair(-255.f, 255.f); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | bounds = std::make_pair(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()); |
| 59 | } |
| 60 | |
| 61 | // Reduce testing ranges |
| 62 | switch(activation) |
| 63 | { |
| 64 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 65 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 66 | // Reduce range as exponent overflows |
| 67 | if(is_float) |
| 68 | { |
| 69 | bounds.first = -40.f; |
| 70 | bounds.second = 40.f; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | bounds.first = -(1 << (fixed_point_position)); |
| 75 | bounds.second = 1 << (fixed_point_position); |
| 76 | } |
| 77 | break; |
| 78 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 79 | // Reduce range as exponent overflows |
| 80 | if(!is_float) |
| 81 | { |
| 82 | bounds.first = -(1 << (fixed_point_position)); |
| 83 | bounds.second = 1 << (fixed_point_position); |
| 84 | } |
| 85 | break; |
| 86 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 87 | // Reduce range as sqrt should take a non-negative number |
| 88 | bounds.first = (is_float) ? 0 : 1 << (fixed_point_position); |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | return bounds; |
| 94 | } |
| 95 | |
| 96 | /** Helper function to get the testing range for batch normalization layer. |
| 97 | * |
| 98 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1. |
| 99 | * |
| 100 | * @return A pair containing the lower upper testing bounds. |
| 101 | */ |
| 102 | template <typename T> |
| 103 | std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1) |
| 104 | { |
| 105 | bool is_float = std::is_floating_point<T>::value; |
| 106 | std::pair<T, T> bounds; |
| 107 | |
| 108 | // Set initial values |
| 109 | if(is_float) |
| 110 | { |
| 111 | bounds = std::make_pair(-1.f, 1.f); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | bounds = std::make_pair(1, 1 << (fixed_point_position)); |
| 116 | } |
| 117 | |
| 118 | return bounds; |
| 119 | } |
| 120 | } // namespace validation |
| 121 | } // namespace test |
| 122 | } // namespace arm_compute |
| 123 | #endif //__ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ |