Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 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 TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_CLAMPFIXTURE |
| 25 | #define TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_CLAMPFIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h" |
| 31 | #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h" |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 32 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h" |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 33 | |
| 34 | #include "tests/framework/Fixture.h" |
| 35 | #include "tests/validation/reference/ActivationLayer.h" |
| 36 | |
| 37 | using namespace arm_compute::experimental::dynamic_fusion; |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 46 | class DynamicFusionClampValidationFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 49 | void setup(TensorShape shape, ClampAttributes attributes, bool fuse, DataType data_type) |
| 50 | { |
| 51 | // CLAMP is implemented as LU_BOUNDED_RELU with the alpha and beta variables swapped. |
| 52 | ActivationLayerInfo act_info{ ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, attributes.max_val(), attributes.min_val() }; |
| 53 | |
| 54 | _fuse = fuse; |
| 55 | _attributes = attributes; |
| 56 | _data_type = data_type; |
| 57 | _target = compute_target(shape, attributes); |
| 58 | _reference = compute_reference(shape, act_info); |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | std::vector<T> get_boundary_values(T min, T max) |
| 63 | { |
| 64 | // This function will return a vector filled with the following values that can |
| 65 | // represent two partitions derived from equivalent partitioning. |
| 66 | // * Lower partition: min, min + delta, lower quarter (nominal), center - delta |
| 67 | // * Upper partition: center, center + delta, upper quarter (nominal), max - delta, max |
| 68 | const auto delta = is_data_type_float(_data_type) ? T(0.1f) : T(1); |
| 69 | const auto center_value = (min + max) / 2; |
| 70 | const auto lower_quarter = (min + center_value) / 2; |
| 71 | const auto upper_quarter = (center_value + max) / 2; |
| 72 | |
| 73 | std::vector<T> boundary_values{}; |
| 74 | |
| 75 | // To ensure all the inserted values are within the given range after subtracing/adding delta |
| 76 | auto insert_values = [&boundary_values, &min, &max](const std::initializer_list<T> &new_values) |
| 77 | { |
| 78 | for(auto &v : new_values) |
| 79 | { |
| 80 | if(v >= min && v <= max) |
| 81 | { |
| 82 | boundary_values.emplace_back(v); |
| 83 | } |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | insert_values({ min, static_cast<T>(min + delta), static_cast<T>(lower_quarter), static_cast<T>(center_value - delta) }); // lower partition |
| 88 | insert_values({ static_cast<T>(center_value), static_cast<T>(center_value + delta), static_cast<T>(upper_quarter), static_cast<T>(max - delta), max }); // upper partition |
| 89 | |
| 90 | return boundary_values; |
| 91 | } |
| 92 | |
| 93 | template <typename U> |
| 94 | void fill(U &&tensor) |
| 95 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 96 | float min_bound = 0; |
| 97 | float max_bound = 0; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 98 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, _data_type); |
| 99 | library->fill_static_values(tensor, get_boundary_values(static_cast<T>(min_bound), static_cast<T>(max_bound))); |
| 100 | } |
| 101 | |
| 102 | TensorType compute_target(const TensorShape &shape, ClampAttributes attributes) |
| 103 | { |
| 104 | // Create a new workload sketch |
| 105 | CLCompileContext cl_compile_ctx = CLKernelLibrary::get().get_compile_context(); |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 106 | GpuWorkloadContext context{ &cl_compile_ctx }; |
| 107 | GpuWorkloadSketch sketch{ &context }; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 108 | |
| 109 | // Create sketch tensors |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 110 | TensorInfo src_info = context.create_tensor_info(TensorInfo(shape, 1, _data_type)); |
| 111 | TensorInfo dst_info = context.create_tensor_info(TensorInfo(shape, 1, _data_type)); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 112 | |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 113 | ITensorInfo *ans_0_info = FunctionType::create_op(sketch, &src_info, attributes); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 114 | if(_fuse) |
| 115 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 116 | ITensorInfo *ans_1_info = FunctionType::create_op(sketch, ans_0_info, attributes); |
| 117 | GpuOutput::create_op(sketch, ans_1_info, &dst_info); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 121 | GpuOutput::create_op(sketch, ans_0_info, &dst_info); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Configure runtime |
| 125 | ClWorkloadRuntime runtime; |
| 126 | runtime.configure(sketch); |
| 127 | |
| 128 | // Construct user tensors |
| 129 | TensorType t_src{}; |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 130 | TensorType t_dst{}; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 131 | |
| 132 | // Initialize user tensors |
| 133 | t_src.allocator()->init(src_info); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 134 | t_dst.allocator()->init(dst_info); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 135 | |
| 136 | // Allocate and fill user tensors |
| 137 | t_src.allocator()->allocate(); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 138 | t_dst.allocator()->allocate(); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 139 | |
| 140 | fill(AccessorType(t_src)); |
| 141 | |
| 142 | // Run runtime |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 143 | runtime.run({ &t_src, &t_dst }); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 144 | |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 145 | return t_dst; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | SimpleTensor<T> compute_reference(const TensorShape &shape, ActivationLayerInfo act_info) |
| 149 | { |
| 150 | // Create reference |
| 151 | SimpleTensor<T> src{ shape, _data_type, 1, _quantization_info }; |
| 152 | |
| 153 | // Fill reference |
| 154 | fill(src); |
| 155 | |
| 156 | auto dst = reference::activation_layer<T>(src, act_info, _quantization_info); |
| 157 | return dst; |
| 158 | } |
| 159 | |
| 160 | protected: |
| 161 | QuantizationInfo _quantization_info{}; |
| 162 | ClampAttributes _attributes{}; |
| 163 | bool _fuse{ false }; |
| 164 | DataType _data_type{}; |
| 165 | TensorType _target{}; |
| 166 | SimpleTensor<T> _reference{}; |
| 167 | }; |
| 168 | } // namespace validation |
| 169 | } // namespace test |
| 170 | } // namespace arm_compute |
| 171 | #endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_CLAMPFIXTURE */ |