Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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/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 | |
| 30 | using namespace arm_compute; |
| 31 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 32 | GCSoftmaxLayer::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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 37 | void GCSoftmaxLayer::configure(const IGCTensor *input, IGCTensor *output, float beta, size_t axis) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 38 | { |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 39 | ARM_COMPUTE_UNUSED(beta, axis); |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 40 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 42 | ARM_COMPUTE_ERROR_ON(beta != 1.0f); |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 43 | ARM_COMPUTE_ERROR_ON_MSG(axis != 1, "Axis must be 1 for GLES"); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 44 | |
| 45 | // Create intermediate tensors shapes |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 46 | _tmp.allocator()->init(TensorInfo(input->info()->tensor_shape(), input->info()->num_channels(), input->info()->data_type())); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 47 | |
| 48 | TensorShape shape = input->info()->tensor_shape(); |
| 49 | shape.set(0, 1); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 50 | TensorInfo tensor_info_max_sum(shape, input->info()->num_channels(), input->info()->data_type()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 51 | _max.allocator()->init(tensor_info_max_sum); |
| 52 | _sum.allocator()->init(tensor_info_max_sum); |
| 53 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 54 | // Manage intermediate buffers |
| 55 | _memory_group.manage(&_tmp); |
| 56 | _memory_group.manage(&_max); |
| 57 | _memory_group.manage(&_sum); |
| 58 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 59 | // 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 | |
| 70 | void GCSoftmaxLayer::run() |
| 71 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 72 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 73 | |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 74 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 79 | } |