| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "matmul_arm.h" |
| 16 | |
| 17 | #include "layer_type.h" |
| 18 | |
| 19 | #include "cpu.h" |
| 20 | |
| 21 | namespace ncnn { |
| 22 | |
| 23 | MatMul_arm::MatMul_arm() |
| 24 | { |
| 25 | #if __ARM_NEON |
| 26 | #if NCNN_ARM82 |
| 27 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 28 | #endif |
| 29 | #endif // __ARM_NEON |
| 30 | |
| 31 | #if NCNN_BF16 |
| 32 | support_bf16_storage = true; |
| 33 | #endif |
| 34 | |
| 35 | gemm = 0; |
| 36 | } |
| 37 | |
| 38 | int MatMul_arm::create_pipeline(const Option& opt) |
| 39 | { |
| 40 | gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm); |
| 41 | |
| 42 | ncnn::ParamDict pd; |
| 43 | pd.set(2, 0); // transA |
| 44 | pd.set(3, transB); // transB |
| 45 | pd.set(4, 0); // constantA |
| 46 | pd.set(5, 0); // constantB |
| 47 | pd.set(6, 1); // constantC |
| 48 | pd.set(7, 0); // M = outch |
| 49 | pd.set(8, 0); // N = size |
| 50 | pd.set(9, 0); // K = maxk*inch |
| 51 | pd.set(10, -1); // constant_broadcast_type_C = null |
| 52 | pd.set(11, 0); // output_N1M |
| 53 | pd.set(12, 1); // output_elempack |
| 54 | |
| 55 | gemm->load_param(pd); |
| 56 | |
| 57 | gemm->load_model(ModelBinFromMatArray(0)); |
| 58 | |
| 59 | gemm->create_pipeline(opt); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int MatMul_arm::destroy_pipeline(const Option& opt) |
| 65 | { |
| 66 | if (gemm) |
| 67 | { |
| 68 | gemm->destroy_pipeline(opt); |
| 69 | delete gemm; |
| 70 | gemm = 0; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int MatMul_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 77 | { |
| 78 | const Mat& A = bottom_blobs[0]; |
| 79 | const Mat& B = bottom_blobs[1]; |
| 80 | Mat& top_blob = top_blobs[0]; |
| 81 | |
| 82 | const int Adims = A.dims; |
| 83 | const int Bdims = B.dims; |
| 84 | const int max_ABdims = std::max(Adims, Bdims); |
| 85 | const size_t elemsize = A.elemsize; |
| 86 | |
| 87 | if (Adims == 1 && Bdims == 1) |
| 88 | { |
| 89 | // dot product |
| 90 | std::vector<Mat> _bottom_blobs(2); |
| 91 | _bottom_blobs[0] = A.reshape(A.w, 1); |
| 92 | _bottom_blobs[1] = transB ? B.reshape(B.w, 1) : B.reshape(1, B.w); |
| 93 | gemm->forward(_bottom_blobs, top_blobs, opt); |
| 94 | |
| 95 | top_blob = top_blob.reshape(1, opt.blob_allocator); |
| 96 | } |
| 97 | else if (Adims == 2 && Bdims == 2) |
| 98 | { |
| 99 | // matrix multiply |
| 100 | gemm->forward(bottom_blobs, top_blobs, opt); |
| 101 | } |
| 102 | else if (Adims == 1 && Bdims == 2) |
| 103 | { |
| 104 | // matrix multiply |
| 105 | std::vector<Mat> _bottom_blobs(2); |
| 106 | _bottom_blobs[0] = A.reshape(A.w, 1); |
| 107 | _bottom_blobs[1] = B; |
| 108 | gemm->forward(_bottom_blobs, top_blobs, opt); |
| 109 | |
| 110 | top_blob = top_blob.reshape(top_blob.w, opt.blob_allocator); |
| 111 | } |
| 112 | else if (Adims == 2 && Bdims == 1) |
| 113 | { |
| 114 | // matrix multiply |
| 115 | std::vector<Mat> _bottom_blobs(2); |
| 116 | _bottom_blobs[0] = A; |
| 117 | _bottom_blobs[1] = transB ? B.reshape(B.w, 1) : B.reshape(1, B.w); |
| 118 | gemm->forward(_bottom_blobs, top_blobs, opt); |
| 119 | |
| 120 | top_blob = top_blob.reshape(top_blob.h, opt.blob_allocator); |
| 121 | } |
| 122 | else if (Adims == 1 && Bdims > 2) |
| 123 | { |
| 124 | // batched matrix multiply |
| 125 | const int N = transB == 0 ? B.w : B.h; |
| 126 | const int batch_size = B.d * B.c; |
| 127 | |
| 128 | Mat top_blob1(N, 1, batch_size, elemsize, opt.blob_allocator); |
| 129 | if (top_blob1.empty()) |
| 130 | return -100; |
| 131 | |
| 132 | Mat A1 = A.reshape(A.w, 1); |
| 133 | Mat B1 = B.reshape(B.w, B.h, batch_size); |
| 134 | |
| 135 | for (int p = 0; p < batch_size; p++) |
| 136 | { |
| 137 | std::vector<Mat> _bottom_blobs(2); |
| 138 | _bottom_blobs[0] = A1; |
| 139 | _bottom_blobs[1] = B1.channel(p); |
| 140 | std::vector<Mat> _top_blobs(1); |
| 141 | _top_blobs[0] = top_blob1.channel(p); |
| 142 | gemm->forward(_bottom_blobs, _top_blobs, opt); |
| 143 | } |
| 144 | |
| 145 | if (Bdims == 3) |
| 146 | top_blob = top_blob1.reshape(N, B.d * B.c, opt.blob_allocator); |
| 147 | else |
| 148 | top_blob = top_blob1.reshape(N, B.d, B.c, opt.blob_allocator); |
| 149 | } |
| 150 | else if (Adims > 2 && Bdims == 1) |
| 151 | { |
| 152 | // batched matrix multiply |
| 153 | const int M = A.h; |
| 154 | const int batch_size = A.d * A.c; |
| 155 | |
| 156 | Mat top_blob1(1, M, batch_size, elemsize, opt.blob_allocator); |
| 157 | if (top_blob1.empty()) |
| 158 | return -100; |
| 159 | |
| 160 | Mat A1 = A.reshape(A.w, A.h, batch_size); |
| 161 | Mat BT = transB ? B.reshape(B.w, 1) : B.reshape(1, B.w); |
| 162 | |
| 163 | for (int p = 0; p < batch_size; p++) |
| 164 | { |
| 165 | std::vector<Mat> _bottom_blobs(2); |
| 166 | _bottom_blobs[0] = A1.channel(p); |
| 167 | _bottom_blobs[1] = BT; |
| 168 | std::vector<Mat> _top_blobs(1); |
| 169 | _top_blobs[0] = top_blob1.channel(p); |
| 170 | gemm->forward(_bottom_blobs, _top_blobs, opt); |
| 171 | } |
| 172 | |
| 173 | if (Adims == 3) |
| 174 | top_blob = top_blob1.reshape(M, A.d * A.c, opt.blob_allocator); |
| 175 | else |
| 176 | top_blob = top_blob1.reshape(M, A.d, A.c, opt.blob_allocator); |
| 177 | } |
| 178 | else if (max_ABdims == 3) |
| 179 | { |
| 180 | Mat A1 = Adims == 2 ? A.reshape(A.w, A.h, 1) : A; |
| 181 | Mat B1 = Bdims == 2 ? B.reshape(B.w, B.h, 1) : B; |
| 182 | |
| 183 | const int M = A1.h; |
| 184 | const int N = transB == 0 ? B1.w : B1.h; |
| 185 | const int batch_size = std::max(A1.c, B1.c); |
| 186 | |
| 187 | top_blob.create(N, M, batch_size, elemsize, opt.blob_allocator); |
| 188 | if (top_blob.empty()) |
| 189 | return -100; |
| 190 | |
| 191 | for (int p = 0; p < batch_size; p++) |
| 192 | { |
| 193 | int Ap = A1.c == 1 ? 0 : p; |
| 194 | int Bp = B1.c == 1 ? 0 : p; |
| 195 | |
| 196 | std::vector<Mat> _bottom_blobs(2); |
| 197 | _bottom_blobs[0] = A1.channel(Ap); |
| 198 | _bottom_blobs[1] = B1.channel(Bp); |
| 199 | std::vector<Mat> _top_blobs(1); |
| 200 | _top_blobs[0] = top_blob.channel(p); |
| 201 | gemm->forward(_bottom_blobs, _top_blobs, opt); |
| 202 | } |
| 203 | } |
| 204 | else if (max_ABdims == 4) |
| 205 | { |
| 206 | Mat A1 = Adims == 3 ? A.reshape(A.w, A.h, A.c, 1) : A; |
| 207 | Mat B1 = Bdims == 3 ? B.reshape(B.w, B.h, B.c, 1) : B; |
| 208 | |
| 209 | const int M = A1.h; |
| 210 | const int N = transB == 0 ? B1.w : B1.h; |
| 211 | const int batch_size_d = std::max(A1.d, B1.d); |
| 212 | const int batch_size_c = std::max(A1.c, B1.c); |
| 213 | |
| 214 | top_blob.create(N, M, batch_size_d, batch_size_c, elemsize, opt.blob_allocator); |
| 215 | if (top_blob.empty()) |
| 216 | return -100; |
| 217 | |
| 218 | for (int p = 0; p < batch_size_c; p++) |
| 219 | { |
| 220 | int Ap = A1.c == 1 ? 0 : p; |
| 221 | int Bp = B1.c == 1 ? 0 : p; |
| 222 | |
| 223 | for (int q = 0; q < batch_size_d; q++) |
| 224 | { |
| 225 | int Ad = A1.d == 1 ? 0 : q; |
| 226 | int Bd = B1.d == 1 ? 0 : q; |
| 227 | |
| 228 | std::vector<Mat> _bottom_blobs(2); |
| 229 | _bottom_blobs[0] = A1.channel(Ap).depth(Ad); |
| 230 | _bottom_blobs[1] = B1.channel(Bp).depth(Bd); |
| 231 | std::vector<Mat> _top_blobs(1); |
| 232 | _top_blobs[0] = top_blob.channel(p).depth(q); |
| 233 | gemm->forward(_bottom_blobs, _top_blobs, opt); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | NCNN_LOGE("impossible matmul %d %d", Adims, Bdims); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | } // namespace ncnn |