| 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 "convolution1d_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "arm_activation.h" |
| 22 | #include "arm_usability.h" |
| 23 | |
| 24 | #include "cpu.h" |
| 25 | |
| 26 | namespace ncnn { |
| 27 | |
| 28 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 29 | #include "convolution1d_packed_fp16s.h" |
| 30 | |
| 31 | int Convolution1D_arm::create_pipeline_fp16s(const Option& opt) |
| 32 | { |
| 33 | const int num_input = weight_data_size / kernel_w / num_output; |
| 34 | |
| 35 | convolution1d_transform_kernel_packed_fp16s(weight_data, weight_data_tm, num_input, num_output, kernel_w); |
| 36 | |
| 37 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 38 | |
| 39 | if (opt.lightmode) |
| 40 | weight_data.release(); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int Convolution1D_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 46 | { |
| 47 | int w = bottom_blob.w; |
| 48 | size_t elemsize = bottom_blob.elemsize; |
| 49 | int elempack = bottom_blob.elempack; |
| 50 | |
| 51 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 52 | |
| 53 | Mat bottom_blob_bordered; |
| 54 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 55 | if (bottom_blob_bordered.empty()) |
| 56 | return -100; |
| 57 | |
| 58 | w = bottom_blob_bordered.w; |
| 59 | |
| 60 | int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1; |
| 61 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 62 | |
| 63 | const int outw = (w - kernel_extent_w) / stride_w + 1; |
| 64 | const int outh = num_output / out_elempack; |
| 65 | |
| 66 | top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator); |
| 67 | if (top_blob.empty()) |
| 68 | return -100; |
| 69 | |
| 70 | convolution1d_packed_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int Convolution1D_arm::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 76 | { |
| 77 | int w = bottom_blob.w; |
| 78 | size_t elemsize = bottom_blob.elemsize; |
| 79 | int elempack = bottom_blob.elempack; |
| 80 | |
| 81 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 82 | |
| 83 | Mat bottom_blob_bordered; |
| 84 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 85 | if (bottom_blob_bordered.empty()) |
| 86 | return -100; |
| 87 | |
| 88 | w = bottom_blob_bordered.w; |
| 89 | |
| 90 | int out_elempack = 1; |
| 91 | if (opt.use_packing_layout) |
| 92 | { |
| 93 | out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 94 | } |
| 95 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 96 | |
| 97 | const int outw = (w - kernel_extent_w) / stride_w + 1; |
| 98 | const int outh = num_output / out_elempack; |
| 99 | |
| 100 | top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator); |
| 101 | if (top_blob.empty()) |
| 102 | return -100; |
| 103 | |
| 104 | convolution1d_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 109 | |
| 110 | } // namespace ncnn |