Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
| 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/graph/nodes/SoftmaxLayer.h" |
| 25 | |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Logger.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 28 | #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h" |
| 29 | #include "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h" |
| 30 | #include "arm_compute/runtime/Tensor.h" |
| 31 | #include "support/ToolchainSupport.h" |
| 32 | #include "utils/TypePrinter.h" |
| 33 | |
| 34 | using namespace arm_compute::graph; |
| 35 | |
| 36 | namespace |
| 37 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 38 | template <typename SoftmaxType, typename TensorType, TargetHint hint> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 39 | std::unique_ptr<arm_compute::IFunction> instantiate_function(arm_compute::ITensor *input, arm_compute::ITensor *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 40 | { |
| 41 | auto softmax = arm_compute::support::cpp14::make_unique<SoftmaxType>(); |
| 42 | softmax->configure( |
| 43 | dynamic_cast<TensorType *>(input), |
| 44 | dynamic_cast<TensorType *>(output)); |
| 45 | |
| 46 | return std::move(softmax); |
| 47 | } |
| 48 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 49 | template <TargetHint target_hint> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 50 | std::unique_ptr<arm_compute::IFunction> instantiate(arm_compute::ITensor *input, arm_compute::ITensor *output); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 51 | |
| 52 | template <> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 53 | std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(arm_compute::ITensor *input, arm_compute::ITensor *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 54 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 55 | return instantiate_function<arm_compute::CLSoftmaxLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, output); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | template <> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 59 | std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(arm_compute::ITensor *input, arm_compute::ITensor *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 60 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 61 | return instantiate_function<arm_compute::NESoftmaxLayer, arm_compute::ITensor, TargetHint::NEON>(input, output); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 62 | } |
| 63 | } // namespace |
| 64 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 65 | std::unique_ptr<arm_compute::IFunction> SoftmaxLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 66 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 67 | ARM_COMPUTE_ERROR_ON(input == nullptr || input->tensor() == nullptr); |
| 68 | ARM_COMPUTE_ERROR_ON(output == nullptr || output->tensor() == nullptr); |
| 69 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 70 | std::unique_ptr<arm_compute::IFunction> func; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 71 | _target_hint = ctx.hints().target_hint(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 72 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 73 | arm_compute::ITensor *in = input->tensor(); |
| 74 | arm_compute::ITensor *out = output->tensor(); |
| 75 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 76 | if(_target_hint == TargetHint::OPENCL) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 77 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 78 | func = instantiate<TargetHint::OPENCL>(in, out); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 79 | } |
| 80 | else |
| 81 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 82 | func = instantiate<TargetHint::NEON>(in, out); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame^] | 85 | ARM_COMPUTE_LOG(" Data Type: " << in->info()->data_type() |
| 86 | << " Input shape: " << in->info()->tensor_shape() |
| 87 | << " Output shape: " << out->info()->tensor_shape() |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 88 | << std::endl); |
| 89 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 90 | return func; |
| 91 | } |