Kaizen | 8938bd3 | 2017-09-28 14:38:23 +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/ConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h" |
| 27 | #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h" |
| 28 | #include "support/ToolchainSupport.h" |
| 29 | #include "utils/TypePrinter.h" |
| 30 | |
| 31 | using namespace arm_compute::graph; |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | template <typename ConvolutionType, typename TensorType, Hint hint> |
| 36 | std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
| 37 | { |
| 38 | bool weights_are_loaded = weights.tensor() != nullptr; |
| 39 | bool biases_are_loaded = biases.tensor() != nullptr; |
| 40 | |
| 41 | auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>(); |
| 42 | conv->configure( |
| 43 | dynamic_cast<TensorType *>(input), |
| 44 | dynamic_cast<TensorType *>(weights.set_target(hint)), |
| 45 | dynamic_cast<TensorType *>(biases.set_target(hint)), |
| 46 | dynamic_cast<TensorType *>(output), |
| 47 | conv_info, weights_info); |
| 48 | if(!weights_are_loaded) |
| 49 | { |
| 50 | weights.allocate_and_fill_if_needed(); |
| 51 | } |
| 52 | if(!biases_are_loaded) |
| 53 | { |
| 54 | biases.allocate_and_fill_if_needed(); |
| 55 | } |
| 56 | |
| 57 | return std::move(conv); |
| 58 | } |
| 59 | |
| 60 | template <Hint hint> |
| 61 | std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info); |
| 62 | |
| 63 | template <> |
| 64 | std::unique_ptr<arm_compute::IFunction> instantiate<Hint::OPENCL>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
| 65 | { |
| 66 | return instantiate_function<arm_compute::CLConvolutionLayer, arm_compute::CLTensor, Hint::OPENCL>(input, weights, biases, output, conv_info, weights_info); |
| 67 | } |
| 68 | |
| 69 | template <> |
| 70 | std::unique_ptr<arm_compute::IFunction> instantiate<Hint::NEON>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
| 71 | { |
| 72 | return instantiate_function<arm_compute::NEConvolutionLayer, arm_compute::Tensor, Hint::NEON>(input, weights, biases, output, conv_info, weights_info); |
| 73 | } |
| 74 | } // namespace |
| 75 | |
| 76 | std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(Hint hint, ITensor *input, ITensor *output) |
| 77 | { |
| 78 | if(_weights.tensor() == nullptr) |
| 79 | { |
| 80 | _weights.set_info(TensorInfo(TensorShape(_conv_width, _conv_height, input->info()->dimension(2), _ofm), input->info()->num_channels(), input->info()->data_type(), |
| 81 | input->info()->fixed_point_position())); |
| 82 | } |
| 83 | if(_biases.tensor() == nullptr) |
| 84 | { |
| 85 | _biases.set_info(TensorInfo(TensorShape(_ofm), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position())); |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<arm_compute::IFunction> func; |
| 89 | _hint = hint; |
| 90 | _input = input; |
| 91 | _output = output; |
| 92 | |
| 93 | if(_hint == Hint::OPENCL) |
| 94 | { |
| 95 | func = instantiate<Hint::OPENCL>(input, _weights, _biases, output, _conv_info, _weights_info); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | func = instantiate<Hint::NEON>(input, _weights, _biases, output, _conv_info, _weights_info); |
| 100 | } |
| 101 | |
| 102 | return func; |
| 103 | } |
| 104 | |
| 105 | void ConvolutionLayer::print_info() |
| 106 | { |
| 107 | if(_hint == Hint::OPENCL) |
| 108 | { |
| 109 | std::cout << "Instantiating CLConvolutionLayer"; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | std::cout << "Instantiating NEConvolutionLayer"; |
| 114 | } |
| 115 | std::cout << " Type: " << _input->info()->data_type() << " Input Shape: " << _input->info()->tensor_shape() << " Weights shape: " << _weights.info().tensor_shape() << " Biases Shape: " << |
| 116 | _biases.info().tensor_shape() << " Output Shape: " << _output->info()->tensor_shape() << " PadStrideInfo: " << _conv_info << "WeightsInfo: " << _weights_info << std::endl; |
| 117 | } |