blob: f74b778e9585f92d98616cf87e2ac9427ba3859e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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#include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h"
25
Chunosovd6afedc2017-11-06 22:09:45 +070026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
29#include "arm_compute/core/Helpers.h"
Chunosovd6afedc2017-11-06 22:09:45 +070030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010032#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/runtime/CL/CLScheduler.h"
34
35using namespace arm_compute;
36
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010037CLSoftmaxLayer::CLSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager)
Chunosovd6afedc2017-11-06 22:09:45 +070038 : _memory_group(std::move(memory_manager)), _max_kernel(), _shift_exp_sum_kernel(), _max_shift_exp_sum_kernel(), _norm_kernel(), _max(), _sum(), _tmp(), _run_legacy_path(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
40}
41
Pablo Palmier48a60f92017-10-18 11:03:08 +010042void CLSoftmaxLayer::configure(const ICLTensor *input, ICLTensor *output, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000044 // Perform validation step
45 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
46 ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayer::validate(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48 // Create intermediate tensors shapes
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000049 const TensorInfo input_info = input->info()->clone()->reset_padding().set_is_resizable(true);
50 DataType tmp_data_type = is_data_type_quantized_asymmetric(input->info()->data_type()) ? DataType::S32 : input->info()->data_type();
51 TensorInfo tensor_info_tmp(input_info.clone()->set_data_type(tmp_data_type));
Chunosovf450caa2017-11-08 16:09:35 +070052 _tmp.allocator()->init(tensor_info_tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
Chunosovf450caa2017-11-08 16:09:35 +070054 TensorShape max_sum_shape = input->info()->tensor_shape();
55 max_sum_shape.set(0, 1);
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000056 _max.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape));
57 _sum.allocator()->init(input_info.clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058
Chunosovd6afedc2017-11-06 22:09:45 +070059 // Set GPU target to kernels
60 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
61
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010062 // Manage intermediate buffers
63 _memory_group.manage(&_tmp);
64 _memory_group.manage(&_max);
65 _memory_group.manage(&_sum);
66
Chunosovd6afedc2017-11-06 22:09:45 +070067 // Configure kernels
68 // TODO (COMPMID-661): Remove legacy path once the new one is properly validated
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000069 _run_legacy_path = is_data_type_quantized_asymmetric(input->info()->data_type());
Chunosovd6afedc2017-11-06 22:09:45 +070070 if(_run_legacy_path)
71 {
72 _max_kernel.configure(input, &_max);
73 _shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum, beta);
74 }
75 else
76 {
77 _max_shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum, beta);
78 }
Chunosovf450caa2017-11-08 16:09:35 +070079 _norm_kernel.configure(&_tmp, &_sum, output, beta);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 // Allocate intermediate buffers
82 _tmp.allocator()->allocate();
83 _max.allocator()->allocate();
84 _sum.allocator()->allocate();
85}
86
Georgios Pinitas30902ed2017-11-14 15:32:57 +000087Error CLSoftmaxLayer::validate(const ITensorInfo *input, const ITensorInfo *output)
88{
Georgios Pinitasee8be2d2017-11-22 12:53:45 +000089 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000090
91 // Create intermediate tensor info
92 DataType tmp_data_type = is_data_type_quantized_asymmetric(input->data_type()) ? DataType::S32 : input->data_type();
93 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type));
94
95 TensorShape max_sum_shape = input->tensor_shape();
96 max_sum_shape.set(0, 1);
97 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape));
98 TensorInfo tensor_info_sum(input->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(QuantizationInfo()));
99
100 bool run_legacy_path = is_data_type_quantized_asymmetric(input->data_type());
101 if(run_legacy_path)
102 {
103 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxKernel::validate(input, &tensor_info_max));
104 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
105 }
106 else
107 {
108 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
109 }
110 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output));
111
112 return Error{};
113}
114
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115void CLSoftmaxLayer::run()
116{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100117 _memory_group.acquire();
118
Chunosovd6afedc2017-11-06 22:09:45 +0700119 // Force to use the new fused kernel
120 if(_run_legacy_path)
121 {
122 CLScheduler::get().enqueue(_max_kernel, false);
123 CLScheduler::get().enqueue(_shift_exp_sum_kernel, false);
124 }
125 else
126 {
127 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
128 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 CLScheduler::get().enqueue(_norm_kernel);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100130
131 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132}