Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [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/graph/nodes/DepthwiseConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/graph/Error.h" |
| 27 | #include "arm_compute/graph/NodeContext.h" |
| 28 | #include "arm_compute/graph/OperationRegistry.h" |
| 29 | #include "support/ToolchainSupport.h" |
| 30 | |
| 31 | using namespace arm_compute::graph; |
| 32 | |
| 33 | std::unique_ptr<arm_compute::IFunction> DepthwiseConvolutionLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output) |
| 34 | { |
| 35 | ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output); |
| 36 | |
| 37 | arm_compute::ITensor *in = input->tensor(); |
| 38 | arm_compute::ITensor *out = output->tensor(); |
| 39 | _target_hint = ctx.hints().target_hint(); |
| 40 | |
| 41 | if(_weights.tensor() == nullptr) |
| 42 | { |
Georgios Pinitas | 240beb7 | 2018-02-09 11:28:58 +0000 | [diff] [blame] | 43 | TensorShape weights_shape(_conv_width, _conv_height, input->tensor()->info()->tensor_shape().z()); |
| 44 | TensorInfo info = TensorInfo(TensorShape(weights_shape), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()); |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 45 | info.set_quantization_info(_quant_info); |
| 46 | _weights.set_info(std::move(info)); |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 47 | } |
| 48 | if(_biases.has_accessor() && _biases.tensor() == nullptr) |
| 49 | { |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 50 | DataType dt = in->info()->data_type(); |
| 51 | _biases.set_info(TensorInfo(TensorShape(in->info()->dimension(2)), in->info()->num_channels(), is_data_type_quantized_asymmetric(dt) ? DataType::S32 : dt, in->info()->fixed_point_position())); |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | bool weights_is_loaded = _weights.tensor() != nullptr; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 55 | bool biases_is_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true; |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 56 | |
| 57 | _weights.set_target(_target_hint); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 58 | if(_biases.has_accessor()) |
| 59 | { |
| 60 | _biases.set_target(_target_hint); |
| 61 | } |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 62 | |
| 63 | // Create node context |
| 64 | NodeContext node_ctx(OperationType::DepthwiseConvolutionLayer); |
| 65 | node_ctx.set_target(_target_hint); |
| 66 | node_ctx.add_input(in); |
| 67 | node_ctx.add_input(_weights.tensor()); |
| 68 | if(_biases.has_accessor()) |
| 69 | { |
| 70 | node_ctx.add_input(_biases.tensor()); |
| 71 | } |
| 72 | node_ctx.add_output(out); |
| 73 | node_ctx.add_parameter<PadStrideInfo>("ConvolutionInfo", _conv_info); |
| 74 | node_ctx.add_parameter<bool>("Optimized3x3", _opt3x3); |
| 75 | |
Georgios Pinitas | 284c204 | 2017-12-11 17:49:18 +0000 | [diff] [blame] | 76 | // Configure operation |
| 77 | auto func = OperationRegistry::get().find_operation(OperationType::DepthwiseConvolutionLayer, _target_hint)->configure(node_ctx); |
| 78 | |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 79 | // Fill tensors |
| 80 | if(!weights_is_loaded) |
| 81 | { |
| 82 | _weights.allocate_and_fill_if_needed(); |
| 83 | } |
| 84 | if(!biases_is_loaded) |
| 85 | { |
| 86 | _biases.allocate_and_fill_if_needed(); |
| 87 | } |
| 88 | |
| 89 | // Get function |
Georgios Pinitas | 284c204 | 2017-12-11 17:49:18 +0000 | [diff] [blame] | 90 | return func; |
Michalis Spyrou | 7bfe4c5 | 2017-11-24 09:54:20 +0000 | [diff] [blame] | 91 | } |