blob: 7268d8eab595e3f9abb8e7018004ce4ea0cc90c6 [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 Pinitas09796752017-07-10 16:05:21 +010044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
46 // Create intermediate tensors shapes
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010047 _tmp.allocator()->init(TensorInfo(input->info()->tensor_shape(), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048
49 TensorShape shape = input->info()->tensor_shape();
50 shape.set(0, 1);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010051 TensorInfo tensor_info_max_sum(shape, input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 _max.allocator()->init(tensor_info_max_sum);
53 _sum.allocator()->init(tensor_info_max_sum);
54
Chunosovd6afedc2017-11-06 22:09:45 +070055 // Set GPU target to kernels
56 _max_shift_exp_sum_kernel.set_target(CLScheduler::get().target());
57
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010058 // Manage intermediate buffers
59 _memory_group.manage(&_tmp);
60 _memory_group.manage(&_max);
61 _memory_group.manage(&_sum);
62
Chunosovd6afedc2017-11-06 22:09:45 +070063 // Configure kernels
64 // TODO (COMPMID-661): Remove legacy path once the new one is properly validated
65 _run_legacy_path = is_data_type_quantized_assymetric(input->info()->data_type());
66 if(_run_legacy_path)
67 {
68 _max_kernel.configure(input, &_max);
69 _shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum, beta);
70 }
71 else
72 {
73 _max_shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum, beta);
74 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 _norm_kernel.configure(&_tmp, &_sum, output);
76
77 // Allocate intermediate buffers
78 _tmp.allocator()->allocate();
79 _max.allocator()->allocate();
80 _sum.allocator()->allocate();
81}
82
83void CLSoftmaxLayer::run()
84{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010085 _memory_group.acquire();
86
Chunosovd6afedc2017-11-06 22:09:45 +070087 // Force to use the new fused kernel
88 if(_run_legacy_path)
89 {
90 CLScheduler::get().enqueue(_max_kernel, false);
91 CLScheduler::get().enqueue(_shift_exp_sum_kernel, false);
92 }
93 else
94 {
95 CLScheduler::get().enqueue(_max_shift_exp_sum_kernel, false);
96 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 CLScheduler::get().enqueue(_norm_kernel);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010098
99 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100}