blob: 7c9611148932c95e088535ae5c7e4050bafa1ffe [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
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
Anthony Barbier8140e1e2017-12-14 23:48:46 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLKernel.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000028#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
29#include "arm_compute/core/Helpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Kaizen8938bd32017-09-28 14:38:23 +010032#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000033#include "arm_compute/runtime/CL/CLScheduler.h"
34
35using namespace arm_compute;
36
Kaizen8938bd32017-09-28 14:38:23 +010037CLSoftmaxLayer::CLSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000038 : _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 Barbier871448e2017-03-24 14:54:29 +000039{
40}
41
Anthony Barbier8140e1e2017-12-14 23:48:46 +000042void CLSoftmaxLayer::configure(const ICLTensor *input, ICLTensor *output, float beta)
Anthony Barbier871448e2017-03-24 14:54:29 +000043{
Anthony Barbier8140e1e2017-12-14 23:48:46 +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 Barbier871448e2017-03-24 14:54:29 +000047
48 // Create intermediate tensors shapes
Anthony Barbier8140e1e2017-12-14 23:48:46 +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));
52 _tmp.allocator()->init(tensor_info_tmp);
Anthony Barbier871448e2017-03-24 14:54:29 +000053
Anthony Barbier8140e1e2017-12-14 23:48:46 +000054 TensorShape max_sum_shape = input->info()->tensor_shape();
55 max_sum_shape.set(0, 1);
56 _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));
58
59 // Set GPU target to kernels
60 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
Anthony Barbier871448e2017-03-24 14:54:29 +000061
Kaizen8938bd32017-09-28 14:38:23 +010062 // Manage intermediate buffers
63 _memory_group.manage(&_tmp);
64 _memory_group.manage(&_max);
65 _memory_group.manage(&_sum);
66
Anthony Barbier8140e1e2017-12-14 23:48:46 +000067 // Configure kernels
68 _run_legacy_path = is_data_type_quantized_asymmetric(input->info()->data_type());
69 if(_run_legacy_path)
70 {
71 _max_kernel.configure(input, &_max);
72 _shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum, beta);
73 }
74 else
75 {
76 _max_shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum, beta);
77 }
78 _norm_kernel.configure(&_tmp, &_sum, output, beta);
Anthony Barbier871448e2017-03-24 14:54:29 +000079
80 // Allocate intermediate buffers
81 _tmp.allocator()->allocate();
82 _max.allocator()->allocate();
83 _sum.allocator()->allocate();
84}
85
Anthony Barbier8140e1e2017-12-14 23:48:46 +000086Status CLSoftmaxLayer::validate(const ITensorInfo *input, const ITensorInfo *output)
87{
88 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
89
90 // Create intermediate tensor info
91 DataType tmp_data_type = is_data_type_quantized_asymmetric(input->data_type()) ? DataType::S32 : input->data_type();
92 TensorInfo tensor_info_tmp(input->clone()->set_data_type(tmp_data_type));
93
94 TensorShape max_sum_shape = input->tensor_shape();
95 max_sum_shape.set(0, 1);
96 TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape));
97 TensorInfo tensor_info_sum(input->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(QuantizationInfo()));
98
99 bool run_legacy_path = is_data_type_quantized_asymmetric(input->data_type());
100 if(run_legacy_path)
101 {
102 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxKernel::validate(input, &tensor_info_max));
103 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
104 }
105 else
106 {
107 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DMaxShiftExpSumKernel::validate(input, &tensor_info_max, &tensor_info_tmp, &tensor_info_sum));
108 }
109 ARM_COMPUTE_RETURN_ON_ERROR(CLLogits1DNormKernel::validate(&tensor_info_tmp, &tensor_info_sum, output));
110
111 return Status{};
112}
113
Anthony Barbier871448e2017-03-24 14:54:29 +0000114void CLSoftmaxLayer::run()
115{
Kaizen8938bd32017-09-28 14:38:23 +0100116 _memory_group.acquire();
117
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000118 // Force to use the new fused kernel
119 if(_run_legacy_path)
120 {
121 CLScheduler::get().enqueue(_max_kernel, false);
122 CLScheduler::get().enqueue(_shift_exp_sum_kernel, false);
123 }
124 else
125 {
126 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
127 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000128 CLScheduler::get().enqueue(_norm_kernel);
Kaizen8938bd32017-09-28 14:38:23 +0100129
130 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000131}