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