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