blob: fdb9a42f134b8b4b0a755f741e65fe4015194622 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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/GLES_COMPUTE/functions/GCSoftmaxLayer.h"
25
26#include "arm_compute/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
29
SiCong Lid004a7a2020-05-28 15:26:41 +010030namespace arm_compute
31{
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000032GCSoftmaxLayer::GCSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager)
33 : _memory_group(std::move(memory_manager)), _max_kernel(), _shift_exp_sum_kernel(), _norm_kernel(), _max(), _sum(), _tmp()
Anthony Barbier7068f992017-10-26 15:23:08 +010034{
35}
36
SiCong Li96209c72020-08-21 12:28:30 +010037void GCSoftmaxLayer::configure(const IGCTensor *input, IGCTensor *output, float beta, int32_t axis)
Anthony Barbier7068f992017-10-26 15:23:08 +010038{
SiCong Li96209c72020-08-21 12:28:30 +010039 ARM_COMPUTE_UNUSED(beta, axis);
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010040
Anthony Barbier7068f992017-10-26 15:23:08 +010041 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010042 ARM_COMPUTE_ERROR_ON(beta != 1.0f);
SiCong Li96209c72020-08-21 12:28:30 +010043 ARM_COMPUTE_ERROR_ON_MSG(axis != 0, "axis must be 0 for GLES");
Anthony Barbier7068f992017-10-26 15:23:08 +010044
45 // Create intermediate tensors shapes
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010046 _tmp.allocator()->init(TensorInfo(input->info()->tensor_shape(), input->info()->num_channels(), input->info()->data_type()));
Anthony Barbier7068f992017-10-26 15:23:08 +010047
48 TensorShape shape = input->info()->tensor_shape();
49 shape.set(0, 1);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010050 TensorInfo tensor_info_max_sum(shape, input->info()->num_channels(), input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010051 _max.allocator()->init(tensor_info_max_sum);
52 _sum.allocator()->init(tensor_info_max_sum);
53
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000054 // Manage intermediate buffers
55 _memory_group.manage(&_tmp);
56 _memory_group.manage(&_max);
57 _memory_group.manage(&_sum);
58
Anthony Barbier7068f992017-10-26 15:23:08 +010059 // Configure Kernels
60 _max_kernel.configure(input, &_max);
61 _shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum);
62 _norm_kernel.configure(&_tmp, &_sum, output);
63
64 // Allocate intermediate buffers
65 _tmp.allocator()->allocate();
66 _max.allocator()->allocate();
67 _sum.allocator()->allocate();
68}
69
70void GCSoftmaxLayer::run()
71{
Georgios Pinitasda953f22019-04-02 17:27:03 +010072 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000073
Joel Liangeb8f71e2017-12-27 13:16:00 +080074 GCScheduler::get().dispatch(_max_kernel, false);
75 GCScheduler::get().memory_barrier();
76 GCScheduler::get().dispatch(_shift_exp_sum_kernel, false);
77 GCScheduler::get().memory_barrier();
78 GCScheduler::get().dispatch(_norm_kernel);
Anthony Barbier7068f992017-10-26 15:23:08 +010079}
SiCong Lid004a7a2020-05-28 15:26:41 +010080
81} // namespace arm_compute