Elffuss BraidHub

kikocis/ncnn public

master · 4.6 KiBRawPermalink
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_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif
22#endif // __SSE2__
23#include "x86_activation.h"
24#include "x86_usability.h"
25
26namespace ncnn {
27
28#include "convolution1d_packed.h"
29
30Convolution1D_x86::Convolution1D_x86()
31{
32#if __SSE2__
33 support_packing = true;
34#endif // __SSE2__
35}
36
37int Convolution1D_x86::create_pipeline(const Option& opt)
38{
39 if (dynamic_weight)
40 return 0;
41
42 int num_input = weight_data_size / kernel_w / num_output;
43
44 convolution1d_transform_kernel_packed(weight_data, weight_data_tm, num_input, num_output, kernel_w);
45
46 if (opt.lightmode)
47 weight_data.release();
48
49 return 0;
50}
51
52int Convolution1D_x86::destroy_pipeline(const Option& /*opt*/)
53{
54 return 0;
55}
56
57int Convolution1D_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
58{
59 int w = bottom_blob.w;
60 size_t elemsize = bottom_blob.elemsize;
61 int elempack = bottom_blob.elempack;
62
63 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
64
65 Mat bottom_blob_bordered;
66 make_padding(bottom_blob, bottom_blob_bordered, opt);
67 if (bottom_blob_bordered.empty())
68 return -100;
69
70 w = bottom_blob_bordered.w;
71
72 int out_elempack = 1;
73#if __SSE2__
74 if (opt.use_packing_layout)
75 {
76#if __AVX512F__
77 out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
78#elif __AVX__
79 out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
80#else
81 out_elempack = num_output % 4 == 0 ? 4 : 1;
82#endif
83 }
84#endif // __SSE2__
85 size_t out_elemsize = elemsize / elempack * out_elempack;
86
87 const int outw = (w - kernel_extent_w) / stride_w + 1;
88 const int outh = num_output / out_elempack;
89
90 top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
91 if (top_blob.empty())
92 return -100;
93
94 convolution1d_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt);
95
96 return 0;
97}
98
99int Convolution1D_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
100{
101 const Mat& bottom_blob = bottom_blobs[0];
102 const Mat& _weight_data = bottom_blobs[1];
103 Mat& top_blob = top_blobs[0];
104
105 const int _kernel_w = _weight_data.w;
106 const int _num_output = _weight_data.c * _weight_data.elempack;
107
108 Mat weight_data_flattened;
109 flatten(_weight_data, weight_data_flattened, opt);
110 if (weight_data_flattened.empty())
111 return -100;
112
113 // weight_data_flattened as pack1
114 weight_data_flattened.w *= weight_data_flattened.elempack;
115 weight_data_flattened.elemsize /= weight_data_flattened.elempack;
116 weight_data_flattened.elempack = 1;
117
118 Mat bias_data_flattened;
119 if (bias_term)
120 {
121 const Mat& _bias_data = bottom_blobs[2];
122 flatten(_bias_data, bias_data_flattened, opt);
123 if (bias_data_flattened.empty())
124 return -100;
125
126 // bias_data_flattened as pack1
127 bias_data_flattened.w *= bias_data_flattened.elempack;
128 bias_data_flattened.elemsize /= bias_data_flattened.elempack;
129 bias_data_flattened.elempack = 1;
130 }
131
132 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution1D);
133
134 ncnn::ParamDict pd;
135 pd.set(0, _num_output);
136 pd.set(1, _kernel_w);
137 pd.set(2, dilation_w);
138 pd.set(3, stride_w);
139 pd.set(4, pad_left);
140 pd.set(15, pad_right);
141 pd.set(18, pad_value);
142 pd.set(5, bias_term);
143 pd.set(6, weight_data_flattened.w);
144 pd.set(9, activation_type);
145 pd.set(10, activation_params);
146
147 op->load_param(pd);
148
149 ncnn::Mat weights[2];
150 weights[0] = weight_data_flattened;
151 weights[1] = bias_data_flattened;
152
153 op->load_model(ncnn::ModelBinFromMatArray(weights));
154
155 op->create_pipeline(opt);
156
157 op->forward(bottom_blob, top_blob, opt);
158
159 op->destroy_pipeline(opt);
160
161 delete op;
162
163 return 0;
164}
165
166} // namespace ncnn