Adnan AlSinan | d9c1d44 | 2023-10-06 19:07:48 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | #ifdef ACL_INTERNAL_TEST_CKW_IN_DF |
| 25 | |
| 26 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentMatMul.h" |
| 27 | |
| 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/dynamic_fusion/sketch/attributes/MatMulAttributes.h" |
| 31 | |
| 32 | #include "src/core/CL/CLValidate.h" |
| 33 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/components/GpuCkwMatMul.h" |
| 34 | #include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace experimental |
| 39 | { |
| 40 | namespace dynamic_fusion |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | using Attributes = MatMulAttributes; |
| 45 | using Settings = GpuMatMulSettings; |
| 46 | |
| 47 | Status validate_matmul_kernel_info(Attributes attributes, Settings settings) |
| 48 | { |
| 49 | const bool adj_lhs = attributes.adj_lhs(); |
| 50 | const bool adj_rhs = attributes.adj_rhs(); |
| 51 | const int m0 = settings.m0(); |
| 52 | const int n0 = settings.n0(); |
| 53 | const int k0 = settings.k0(); |
| 54 | |
| 55 | // Validate M0 |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0"); |
| 57 | |
| 58 | if (adj_lhs) |
| 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((m0 & (m0 - 1)) && (m0 != 3)) || (m0 > 16), |
| 61 | "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed"); |
| 62 | } |
| 63 | |
| 64 | // Validate N0 |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 < 1, "Only positive integers are supported for N0"); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((n0 & (n0 - 1)) && (n0 != 3)) || (n0 > 16), |
| 67 | "Only 1,2,3,4,8,16 are supported for N0"); |
| 68 | |
| 69 | // Validate K0 |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(k0 < 1, "Only positive integers are supported for K0"); |
| 71 | if (!adj_lhs || adj_rhs) |
| 72 | { |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((k0 & (k0 - 1)) && (k0 != 3)) || (k0 > 16), |
| 74 | "Only 1,2,3,4,8,16 are supported for K0"); |
| 75 | } |
| 76 | |
| 77 | return Status{}; |
| 78 | } |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | Status ClComponentMatMul::validate(const Properties &properties, |
| 83 | const ArgumentPack<ITensorInfo> &tensors, |
| 84 | const Attributes &attributes, |
| 85 | const Settings &settings) |
| 86 | { |
| 87 | ARM_COMPUTE_UNUSED(properties); |
| 88 | ARM_COMPUTE_UNUSED(attributes); |
| 89 | |
| 90 | const auto lhs = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 91 | const auto rhs = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 92 | const auto dst = tensors.get_const_tensor(TensorType::ACL_DST_0); |
| 93 | |
| 94 | // Check if Matching data type |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst); |
| 97 | |
| 98 | // Data type |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F16, DataType::F32); |
| 100 | // Data layout |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(lhs, DataLayout::NHWC); |
| 102 | |
| 103 | // All tensor infos are initialized |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(lhs->tensor_shape().total_size() == 0); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON(rhs->tensor_shape().total_size() == 0); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0); |
| 107 | |
| 108 | // Device requirements are met |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(lhs); |
| 110 | |
| 111 | // Check if dst shape is correct |
| 112 | MatMulKernelInfo matmul_kernel_info = |
| 113 | MatMulKernelInfo(attributes.adj_lhs(), attributes.adj_rhs(), settings.m0(), settings.n0(), settings.k0()); |
| 114 | const auto expected_dst_shape = |
| 115 | misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info); |
| 116 | |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), expected_dst_shape); |
| 118 | |
| 119 | // Check if block sizes are supported |
| 120 | ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(attributes, settings)); |
| 121 | |
| 122 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 123 | opencl::kernels::validate_matmul_input_shapes(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)); |
| 124 | |
| 125 | return Status{}; |
| 126 | } |
| 127 | |
| 128 | ClComponentMatMul::ClComponentMatMul(ComponentId id, |
| 129 | const Properties &properties, |
| 130 | const ArgumentPack<ITensorInfo> &tensors, |
| 131 | const Attributes &attributes, |
| 132 | const Settings &settings) |
| 133 | : IGpuKernelComponent{id, properties, tensors}, |
| 134 | _component_writer{std::make_unique<GpuCkwMatMul>(id, tensors, attributes, settings)} |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | ClComponentMatMul::~ClComponentMatMul() |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | const IGpuCkwComponentDriver *ClComponentMatMul::ckw_component_driver() const |
| 143 | { |
| 144 | return _component_writer.get(); |
| 145 | } |
| 146 | |
| 147 | } // namespace dynamic_fusion |
| 148 | } // namespace experimental |
| 149 | } // namespace arm_compute |
| 150 | |
| 151 | #endif // ACL_INTERNAL_TEST_CKW_IN_DF |