SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2022 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 "ClComponentDirectConv2d.h" |
| 25 | |
| 26 | #include "arm_compute/core/Validate.h" |
| 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 28 | #include "arm_compute/dynamic_fusion/sketch/OperatorAttributes.h" |
| 29 | #include "src/core/CL/CLValidate.h" |
| 30 | #include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateDirectConv2d.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace experimental |
| 35 | { |
| 36 | namespace dynamic_fusion |
| 37 | { |
| 38 | using Settings = ClComponentDirectConv2dSettings; |
| 39 | |
| 40 | Settings &Settings::export_to_cl_image(bool cl_image) |
| 41 | { |
| 42 | _export_to_cl_image = cl_image; |
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | bool Settings::export_to_cl_image() const |
| 47 | { |
| 48 | return _export_to_cl_image; |
| 49 | } |
| 50 | |
| 51 | Settings &Settings::fast_relaxed_math(bool fast_relaxed_math) |
| 52 | { |
| 53 | _fast_relaxed_math = fast_relaxed_math; |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | bool Settings::fast_relaxed_math() const |
| 58 | { |
| 59 | return _fast_relaxed_math; |
| 60 | } |
| 61 | |
| 62 | Status ClComponentDirectConv2d::validate( |
| 63 | const Properties &properties, |
| 64 | const ArgumentPack<ITensorInfo> &tensors, |
| 65 | const Attributes &attributes, |
| 66 | const Settings &settings) |
| 67 | { |
| 68 | ARM_COMPUTE_UNUSED(properties, settings); |
| 69 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 70 | const auto wei = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 71 | const auto bia = tensors.get_const_tensor(TensorType::ACL_SRC_2); |
| 72 | const auto dst = tensors.get_const_tensor(TensorType::ACL_DST_0); |
| 73 | |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei, dst); |
| 75 | |
| 76 | // 1. Check validity |
| 77 | // Matching data type |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, wei); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 80 | if(bia != nullptr) |
| 81 | { |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, bia); |
| 83 | } |
| 84 | |
| 85 | // Matching data layout |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, wei); |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst); |
| 88 | if(bia != nullptr) |
| 89 | { |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, bia); |
| 91 | } |
| 92 | |
| 93 | // All tensor infos are initialized |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(src->tensor_shape().total_size() == 0); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(wei->tensor_shape().total_size() == 0); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0); |
| 97 | if(bia != nullptr) |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON(bia->tensor_shape().total_size() == 0); |
| 100 | } |
| 101 | // Device requirements are met |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
| 103 | // wei shape is correct |
| 104 | const DataLayout data_layout = src->data_layout(); |
| 105 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(wei->dimension(channel_idx) != src->dimension(channel_idx), "Weights feature map dimension should match the respective src's one"); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(wei->num_dimensions() > 4, "Weights can be at most 4 dimensional"); |
| 108 | |
| 109 | // dst shape is correct |
| 110 | PadStrideInfo legacy_pad_stride(attributes.stride().x(), attributes.stride().y(), attributes.pad().left, attributes.pad().right, attributes.pad().top, |
| 111 | attributes.pad().bottom, DimensionRoundingType{}); |
| 112 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), |
| 113 | misc::shape_calculator::compute_deep_convolution_shape(*src, *wei, legacy_pad_stride)); |
| 114 | |
| 115 | // bia shape is correct |
| 116 | if(bia != nullptr) |
| 117 | { |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(bia->dimension(0) != wei->dimension(3), |
| 119 | "Biases size and number of dst feature maps should match"); |
| 120 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(bia->num_dimensions() > 1, |
| 121 | "Biases should be one dimensional"); |
| 122 | } |
| 123 | |
| 124 | // 2. Check support level |
| 125 | // Data type |
| 126 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
| 127 | // Data layout |
| 128 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC); |
| 129 | |
| 130 | return Status{}; |
| 131 | } |
| 132 | |
| 133 | ClComponentDirectConv2d::ClComponentDirectConv2d( |
| 134 | ComponentId id, |
| 135 | const Properties &properties, |
| 136 | const ArgumentPack<ITensorInfo> &tensors, |
| 137 | const Attributes &attributes, |
| 138 | const Settings &settings) |
| 139 | : IGpuKernelComponent{ id, properties, tensors }, |
| 140 | _component_writer{ std::make_unique<ClTemplateDirectConv2d>(id, tensors, attributes, settings) } |
| 141 | { |
| 142 | } |
| 143 | ClComponentDirectConv2d::~ClComponentDirectConv2d() |
| 144 | { |
| 145 | } |
| 146 | const IGpuTemplateComponentWriter *ClComponentDirectConv2d::template_writer() const |
| 147 | { |
| 148 | return _component_writer.get(); |
| 149 | } |
| 150 | } // namespace dynamic_fusion |
| 151 | } // namespace experimental |
| 152 | } // namespace arm_compute |