Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 24 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/functions/CLMeanStdDev.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | |
| 29 | using namespace arm_compute; |
| 30 | |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 31 | CLMeanStdDev::CLMeanStdDev(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 32 | : _memory_group(std::move(memory_manager)), |
| 33 | _data_type(), |
| 34 | _num_pixels(), |
| 35 | _run_stddev(), |
| 36 | _reduction_operation_mean(), |
| 37 | _reduction_operation_stddev(), |
| 38 | _reduction_output_mean(), |
| 39 | _reduction_output_stddev(), |
| 40 | _mean(nullptr), |
| 41 | _stddev(nullptr), |
| 42 | _mean_stddev_kernel(), |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 43 | _fill_border_kernel(), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | _global_sum(), |
| 45 | _global_sum_squared() |
| 46 | { |
| 47 | } |
| 48 | |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 49 | Status CLMeanStdDev::validate(ITensorInfo *input, float *mean, float *stddev) |
| 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(input); |
| 52 | if(is_data_type_float(input->data_type())) |
| 53 | { |
| 54 | ARM_COMPUTE_UNUSED(mean); |
| 55 | ARM_COMPUTE_UNUSED(stddev); |
| 56 | |
| 57 | TensorShape output_shape = TensorShape{ 1, input->dimension(1) }; |
| 58 | TensorInfo output_shape_info = TensorInfo(output_shape, 1, DataType::U8); |
| 59 | return CLReductionOperation::validate(input, &output_shape_info, 0, ReductionOperation::SUM); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | return CLMeanStdDevKernel::validate(input, mean, nullptr, stddev, nullptr); |
| 64 | } |
| 65 | } |
| 66 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 67 | void CLMeanStdDev::configure(ICLImage *input, float *mean, float *stddev) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 69 | configure(CLKernelLibrary::get().get_compile_context(), input, mean, stddev); |
| 70 | } |
| 71 | |
| 72 | void CLMeanStdDev::configure(const CLCompileContext &compile_context, ICLImage *input, float *mean, float *stddev) |
| 73 | { |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 74 | // In the case of F16/F32 we call reduction operation for calculating CLMeanStdDev |
| 75 | _data_type = input->info()->data_type(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 77 | if(is_data_type_float(_data_type)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | { |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 79 | _num_pixels = input->info()->dimension(0) * input->info()->dimension(1); |
| 80 | |
| 81 | _memory_group.manage(&_reduction_output_mean); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 82 | _reduction_operation_mean.configure(compile_context, input, &_reduction_output_mean, 0, ReductionOperation::SUM); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 83 | _reduction_output_mean.allocator()->allocate(); |
| 84 | _mean = mean; |
| 85 | |
| 86 | if(stddev != nullptr) |
| 87 | { |
| 88 | _memory_group.manage(&_reduction_output_stddev); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 89 | _reduction_operation_stddev.configure(compile_context, input, &_reduction_output_stddev, 0, ReductionOperation::SUM_SQUARE); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 90 | _reduction_output_stddev.allocator()->allocate(); |
| 91 | _stddev = stddev; |
| 92 | _run_stddev = true; |
| 93 | } |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | _global_sum = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(cl_ulong)); |
| 98 | |
| 99 | if(stddev != nullptr) |
| 100 | { |
| 101 | _global_sum_squared = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(cl_ulong)); |
| 102 | } |
| 103 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 104 | _mean_stddev_kernel.configure(compile_context, input, mean, &_global_sum, stddev, &_global_sum_squared); |
| 105 | _fill_border_kernel.configure(compile_context, input, _mean_stddev_kernel.border_size(), BorderMode::CONSTANT, PixelValue(static_cast<uint8_t>(0))); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | template <typename T> |
| 110 | void CLMeanStdDev::run_float() |
| 111 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 112 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 113 | |
| 114 | // Perform reduction on x-axis |
| 115 | _reduction_operation_mean.run(); |
| 116 | if(_run_stddev) |
| 117 | { |
| 118 | _reduction_operation_stddev.run(); |
| 119 | _reduction_output_stddev.map(true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 122 | _reduction_output_mean.map(true); |
| 123 | |
| 124 | auto mean = static_cast<T>(0); |
| 125 | |
| 126 | // Calculate final result for mean |
| 127 | for(unsigned int i = 0; i < _reduction_output_mean.info()->dimension(1); ++i) |
| 128 | { |
| 129 | mean += *reinterpret_cast<T *>(_reduction_output_mean.buffer() + _reduction_output_mean.info()->offset_element_in_bytes(Coordinates(0, i))); |
| 130 | } |
| 131 | |
| 132 | mean /= _num_pixels; |
| 133 | *_mean = mean; |
| 134 | |
| 135 | if(_run_stddev) |
| 136 | { |
| 137 | auto stddev = static_cast<T>(0); |
| 138 | // Calculate final result for stddev |
| 139 | for(unsigned int i = 0; i < _reduction_output_stddev.info()->dimension(1); ++i) |
| 140 | { |
| 141 | stddev += *reinterpret_cast<T *>(_reduction_output_stddev.buffer() + _reduction_output_stddev.info()->offset_element_in_bytes(Coordinates(0, i))); |
| 142 | } |
| 143 | *_stddev = std::sqrt((stddev / _num_pixels) - (mean * mean)); |
| 144 | |
| 145 | _reduction_output_stddev.unmap(); |
| 146 | } |
| 147 | _reduction_output_mean.unmap(); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void CLMeanStdDev::run_int() |
| 151 | { |
| 152 | CLScheduler::get().enqueue(_fill_border_kernel); |
| 153 | CLScheduler::get().enqueue(_mean_stddev_kernel); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void CLMeanStdDev::run() |
| 157 | { |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 158 | switch(_data_type) |
| 159 | { |
| 160 | case DataType::F16: |
| 161 | run_float<half>(); |
| 162 | break; |
| 163 | case DataType::F32: |
| 164 | run_float<float>(); |
| 165 | break; |
| 166 | case DataType::U8: |
| 167 | run_int(); |
| 168 | break; |
| 169 | default: |
| 170 | ARM_COMPUTE_ERROR_ON("Not supported"); |
| 171 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | } |