blob: 0c8769b38f7412d80791bec28e370e0adcfe6b3d [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +00002 * Copyright (c) 2017-2018 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
30using namespace 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
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010037void GCSoftmaxLayer::configure(const IGCTensor *input, IGCTensor *output, float beta)
Anthony Barbier7068f992017-10-26 15:23:08 +010038{
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010039 ARM_COMPUTE_UNUSED(beta);
40
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);
Anthony Barbier7068f992017-10-26 15:23:08 +010043
44 // Create intermediate tensors shapes
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010045 _tmp.allocator()->init(TensorInfo(input->info()->tensor_shape(), input->info()->num_channels(), input->info()->data_type()));
Anthony Barbier7068f992017-10-26 15:23:08 +010046
47 TensorShape shape = input->info()->tensor_shape();
48 shape.set(0, 1);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010049 TensorInfo tensor_info_max_sum(shape, input->info()->num_channels(), input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010050 _max.allocator()->init(tensor_info_max_sum);
51 _sum.allocator()->init(tensor_info_max_sum);
52
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000053 // Manage intermediate buffers
54 _memory_group.manage(&_tmp);
55 _memory_group.manage(&_max);
56 _memory_group.manage(&_sum);
57
Anthony Barbier7068f992017-10-26 15:23:08 +010058 // Configure Kernels
59 _max_kernel.configure(input, &_max);
60 _shift_exp_sum_kernel.configure(input, &_max, &_tmp, &_sum);
61 _norm_kernel.configure(&_tmp, &_sum, output);
62
63 // Allocate intermediate buffers
64 _tmp.allocator()->allocate();
65 _max.allocator()->allocate();
66 _sum.allocator()->allocate();
67}
68
69void GCSoftmaxLayer::run()
70{
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000071 _memory_group.acquire();
72
Joel Liangeb8f71e2017-12-27 13:16:00 +080073 GCScheduler::get().dispatch(_max_kernel, false);
74 GCScheduler::get().memory_barrier();
75 GCScheduler::get().dispatch(_shift_exp_sum_kernel, false);
76 GCScheduler::get().memory_barrier();
77 GCScheduler::get().dispatch(_norm_kernel);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000078
79 _memory_group.release();
Anthony Barbier7068f992017-10-26 15:23:08 +010080}