| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 "convolution1d_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "arm_usability.h" |
| 22 | #include "arm_activation.h" |
| 23 | |
| 24 | #include "cpu.h" |
| 25 | #include "layer_type.h" |
| 26 | |
| 27 | namespace ncnn { |
| 28 | |
| 29 | #include "convolution1d_packed.h" |
| 30 | #if NCNN_BF16 |
| 31 | #include "convolution1d_packed_bf16s.h" |
| 32 | #endif // NCNN_BF16 |
| 33 | |
| 34 | Convolution1D_arm::Convolution1D_arm() |
| 35 | { |
| 36 | #if __ARM_NEON |
| 37 | support_packing = true; |
| 38 | #if NCNN_ARM82 |
| 39 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 40 | #endif |
| 41 | #endif // __ARM_NEON |
| 42 | |
| 43 | #if NCNN_BF16 |
| 44 | support_bf16_storage = true; |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | int Convolution1D_arm::create_pipeline(const Option& opt) |
| 49 | { |
| 50 | if (dynamic_weight) |
| 51 | return 0; |
| 52 | |
| 53 | #if NCNN_ARM82 |
| 54 | if (support_fp16_storage && opt.use_fp16_storage) |
| 55 | { |
| 56 | return create_pipeline_fp16s(opt); |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | #if NCNN_BF16 |
| 61 | if (opt.use_bf16_storage) |
| 62 | { |
| 63 | return create_pipeline_bf16s(opt); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | const int num_input = weight_data_size / kernel_w / num_output; |
| 68 | |
| 69 | convolution1d_transform_kernel_packed(weight_data, weight_data_tm, num_input, num_output, kernel_w); |
| 70 | |
| 71 | if (opt.lightmode) |
| 72 | weight_data.release(); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int Convolution1D_arm::destroy_pipeline(const Option& /*opt*/) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int Convolution1D_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 83 | { |
| 84 | int elembits = bottom_blob.elembits(); |
| 85 | |
| 86 | #if NCNN_ARM82 |
| 87 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 88 | { |
| 89 | if (opt.use_fp16_arithmetic) |
| 90 | return forward_fp16sa(bottom_blob, top_blob, opt); |
| 91 | else |
| 92 | return forward_fp16s(bottom_blob, top_blob, opt); |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #if NCNN_BF16 |
| 97 | if (opt.use_bf16_storage && elembits == 16) |
| 98 | return forward_bf16s(bottom_blob, top_blob, opt); |
| 99 | #endif |
| 100 | |
| 101 | int w = bottom_blob.w; |
| 102 | size_t elemsize = bottom_blob.elemsize; |
| 103 | int elempack = bottom_blob.elempack; |
| 104 | |
| 105 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 106 | |
| 107 | Mat bottom_blob_bordered; |
| 108 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 109 | if (bottom_blob_bordered.empty()) |
| 110 | return -100; |
| 111 | |
| 112 | w = bottom_blob_bordered.w; |
| 113 | |
| 114 | int out_elempack = 1; |
| 115 | #if __ARM_NEON |
| 116 | if (opt.use_packing_layout) |
| 117 | { |
| 118 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 119 | } |
| 120 | #endif |
| 121 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 122 | |
| 123 | const int outw = (w - kernel_extent_w) / stride_w + 1; |
| 124 | const int outh = num_output / out_elempack; |
| 125 | |
| 126 | top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator); |
| 127 | if (top_blob.empty()) |
| 128 | return -100; |
| 129 | |
| 130 | convolution1d_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int Convolution1D_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 136 | { |
| 137 | const Mat& bottom_blob = bottom_blobs[0]; |
| 138 | const Mat& _weight_data = bottom_blobs[1]; |
| 139 | Mat& top_blob = top_blobs[0]; |
| 140 | |
| 141 | const int _kernel_w = _weight_data.w; |
| 142 | const int _num_output = _weight_data.c * _weight_data.elempack; |
| 143 | |
| 144 | Mat weight_data_flattened; |
| 145 | flatten(_weight_data, weight_data_flattened, opt); |
| 146 | if (weight_data_flattened.empty()) |
| 147 | return -100; |
| 148 | |
| 149 | #if NCNN_ARM82 |
| 150 | if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16) |
| 151 | { |
| 152 | Mat weight_data_flattened_fp32; |
| 153 | cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt); |
| 154 | weight_data_flattened = weight_data_flattened_fp32; |
| 155 | } |
| 156 | #endif // NCNN_ARM82 |
| 157 | #if NCNN_BF16 |
| 158 | if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16) |
| 159 | { |
| 160 | Mat weight_data_flattened_fp32; |
| 161 | cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt); |
| 162 | weight_data_flattened = weight_data_flattened_fp32; |
| 163 | } |
| 164 | #endif // NCNN_BF16 |
| 165 | |
| 166 | // weight_data_flattened as pack1 |
| 167 | weight_data_flattened.w *= weight_data_flattened.elempack; |
| 168 | weight_data_flattened.elemsize /= weight_data_flattened.elempack; |
| 169 | weight_data_flattened.elempack = 1; |
| 170 | |
| 171 | Mat bias_data_flattened; |
| 172 | if (bias_term) |
| 173 | { |
| 174 | const Mat& _bias_data = bottom_blobs[2]; |
| 175 | flatten(_bias_data, bias_data_flattened, opt); |
| 176 | if (bias_data_flattened.empty()) |
| 177 | return -100; |
| 178 | |
| 179 | #if NCNN_ARM82 |
| 180 | if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16) |
| 181 | { |
| 182 | Mat bias_data_flattened_fp32; |
| 183 | cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt); |
| 184 | bias_data_flattened = bias_data_flattened_fp32; |
| 185 | } |
| 186 | #endif // NCNN_ARM82 |
| 187 | #if NCNN_BF16 |
| 188 | if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16) |
| 189 | { |
| 190 | Mat bias_data_flattened_fp32; |
| 191 | cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt); |
| 192 | bias_data_flattened = bias_data_flattened_fp32; |
| 193 | } |
| 194 | #endif // NCNN_BF16 |
| 195 | |
| 196 | // bias_data_flattened as pack1 |
| 197 | bias_data_flattened.w *= bias_data_flattened.elempack; |
| 198 | bias_data_flattened.elemsize /= bias_data_flattened.elempack; |
| 199 | bias_data_flattened.elempack = 1; |
| 200 | } |
| 201 | |
| 202 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution1D); |
| 203 | |
| 204 | ncnn::ParamDict pd; |
| 205 | pd.set(0, _num_output); |
| 206 | pd.set(1, _kernel_w); |
| 207 | pd.set(2, dilation_w); |
| 208 | pd.set(3, stride_w); |
| 209 | pd.set(4, pad_left); |
| 210 | pd.set(15, pad_right); |
| 211 | pd.set(18, pad_value); |
| 212 | pd.set(5, bias_term); |
| 213 | pd.set(6, weight_data_flattened.w); |
| 214 | pd.set(9, activation_type); |
| 215 | pd.set(10, activation_params); |
| 216 | |
| 217 | op->load_param(pd); |
| 218 | |
| 219 | ncnn::Mat weights[2]; |
| 220 | weights[0] = weight_data_flattened; |
| 221 | weights[1] = bias_data_flattened; |
| 222 | |
| 223 | op->load_model(ncnn::ModelBinFromMatArray(weights)); |
| 224 | |
| 225 | op->create_pipeline(opt); |
| 226 | |
| 227 | op->forward(bottom_blob, top_blob, opt); |
| 228 | |
| 229 | op->destroy_pipeline(opt); |
| 230 | |
| 231 | delete op; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | #if NCNN_BF16 |
| 237 | int Convolution1D_arm::create_pipeline_bf16s(const Option& opt) |
| 238 | { |
| 239 | const int num_input = weight_data_size / kernel_w / num_output; |
| 240 | |
| 241 | convolution1d_transform_kernel_packed_bf16s(weight_data, weight_data_tm, num_input, num_output, kernel_w); |
| 242 | |
| 243 | if (opt.lightmode) |
| 244 | weight_data.release(); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int Convolution1D_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 250 | { |
| 251 | int w = bottom_blob.w; |
| 252 | size_t elemsize = bottom_blob.elemsize; |
| 253 | int elempack = bottom_blob.elempack; |
| 254 | |
| 255 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 256 | |
| 257 | Mat bottom_blob_bordered; |
| 258 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 259 | if (bottom_blob_bordered.empty()) |
| 260 | return -100; |
| 261 | |
| 262 | w = bottom_blob_bordered.w; |
| 263 | |
| 264 | int out_elempack = 1; |
| 265 | #if __ARM_NEON |
| 266 | if (opt.use_packing_layout) |
| 267 | { |
| 268 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 269 | } |
| 270 | #endif |
| 271 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 272 | |
| 273 | const int outw = (w - kernel_extent_w) / stride_w + 1; |
| 274 | const int outh = num_output / out_elempack; |
| 275 | |
| 276 | top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator); |
| 277 | if (top_blob.empty()) |
| 278 | return -100; |
| 279 | |
| 280 | convolution1d_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | #endif // NCNN_BF16 |
| 285 | |
| 286 | } // namespace ncnn |