File size: 21,361 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | // Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 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 "convolutiondepthwise.h"
#include "layer_type.h"
#include "fused_activation.h"
namespace ncnn {
ConvolutionDepthWise::ConvolutionDepthWise()
{
one_blob_only = true;
support_inplace = false;
}
int ConvolutionDepthWise::load_param(const ParamDict& pd)
{
num_output = pd.get(0, 0);
kernel_w = pd.get(1, 0);
kernel_h = pd.get(11, kernel_w);
dilation_w = pd.get(2, 1);
dilation_h = pd.get(12, dilation_w);
stride_w = pd.get(3, 1);
stride_h = pd.get(13, stride_w);
pad_left = pd.get(4, 0);
pad_right = pd.get(15, pad_left);
pad_top = pd.get(14, pad_left);
pad_bottom = pd.get(16, pad_top);
pad_value = pd.get(18, 0.f);
bias_term = pd.get(5, 0);
weight_data_size = pd.get(6, 0);
group = pd.get(7, 1);
int8_scale_term = pd.get(8, 0);
activation_type = pd.get(9, 0);
activation_params = pd.get(10, Mat());
dynamic_weight = pd.get(19, 0);
if (dynamic_weight)
{
one_blob_only = false;
}
if (num_output % group != 0)
{
// reject invalid group
return -100;
}
if (int8_scale_term)
{
#if NCNN_INT8
support_int8_storage = true;
#else
NCNN_LOGE("please build ncnn with NCNN_INT8 enabled for int8 inference");
return -1;
#endif
}
return 0;
}
int ConvolutionDepthWise::load_model(const ModelBin& mb)
{
if (dynamic_weight)
return 0;
weight_data = mb.load(weight_data_size, 0);
if (weight_data.empty())
return -100;
if (bias_term)
{
bias_data = mb.load(num_output, 1);
if (bias_data.empty())
return -100;
}
#if NCNN_INT8
if (int8_scale_term == 1 || int8_scale_term == 101)
{
weight_data_int8_scales = mb.load(group, 1);
bottom_blob_int8_scales = mb.load(1, 1);
float bottom_blob_int8_scale = bottom_blob_int8_scales[0];
bottom_blob_int8_scales = Mat(group);
bottom_blob_int8_scales.fill(bottom_blob_int8_scale);
}
else if (int8_scale_term == 2 || int8_scale_term == 102)
{
weight_data_int8_scales = mb.load(1, 1);
bottom_blob_int8_scales = mb.load(1, 1);
// extend group if only one provided
float weight_data_int8_scale = weight_data_int8_scales[0];
weight_data_int8_scales = Mat(group);
weight_data_int8_scales.fill(weight_data_int8_scale);
float bottom_blob_int8_scale = bottom_blob_int8_scales[0];
bottom_blob_int8_scales = Mat(group);
bottom_blob_int8_scales.fill(bottom_blob_int8_scale);
}
if (int8_scale_term > 100)
{
top_blob_int8_scales = mb.load(1, 1);
float top_blob_int8_scale = top_blob_int8_scales[0];
top_blob_int8_scales = Mat(group);
top_blob_int8_scales.fill(top_blob_int8_scale);
}
#endif // NCNN_INT8
return 0;
}
int ConvolutionDepthWise::create_pipeline(const Option& opt)
{
#if NCNN_INT8
// runtime quantize the weight data
if (opt.use_int8_inference && weight_data.elemsize == (size_t)4u && int8_scale_term)
{
Mat int8_weight_data(weight_data_size, (size_t)1u);
if (int8_weight_data.empty())
return -100;
const int weight_data_size_g = weight_data_size / group;
for (int g = 0; g < group; g++)
{
Option opt_q = opt;
opt_q.blob_allocator = int8_weight_data.allocator;
opt_q.use_packing_layout = false;
const Mat weight_data_g = weight_data.range(weight_data_size_g * g, weight_data_size_g);
Mat int8_weight_data_g = int8_weight_data.range(weight_data_size_g * g, weight_data_size_g);
const Mat weight_data_int8_scales_g = weight_data_int8_scales.range(g, 1);
quantize_to_int8(weight_data_g, int8_weight_data_g, weight_data_int8_scales_g, opt_q);
}
weight_data = int8_weight_data;
}
#else
(void)(opt);
#endif // NCNN_INT8
return 0;
}
static int convolutiondepthwise(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int kernel_h, int stride_w, int stride_h, int dilation_w, int dilation_h, int group, int activation_type, const Mat& activation_params, const Option& opt)
{
const int w = bottom_blob.w;
const int inch = bottom_blob.c;
const int outw = top_blob.w;
const int outh = top_blob.h;
const int outch = top_blob.c;
const int bias_term = bias_data.empty() ? 0 : 1;
const int maxk = kernel_w * kernel_h;
// kernel offsets
std::vector<int> _space_ofs(maxk);
int* space_ofs = &_space_ofs[0];
{
int p1 = 0;
int p2 = 0;
int gap = w * dilation_h - kernel_w * dilation_w;
for (int i = 0; i < kernel_h; i++)
{
for (int j = 0; j < kernel_w; j++)
{
space_ofs[p1] = p2;
p1++;
p2 += dilation_w;
}
p2 += gap;
}
}
// depth-wise
if (inch == group && group == outch)
{
#pragma omp parallel for num_threads(opt.num_threads)
for (int g = 0; g < group; g++)
{
float* outptr = top_blob.channel(g);
const float* kptr = (const float*)weight_data + maxk * g;
const Mat m = bottom_blob.channel(g);
for (int i = 0; i < outh; i++)
{
for (int j = 0; j < outw; j++)
{
float sum = 0.f;
if (bias_term)
sum = bias_data[g];
const float* sptr = m.row(i * stride_h) + j * stride_w;
for (int k = 0; k < maxk; k++)
{
float val = sptr[space_ofs[k]];
float w = kptr[k];
sum += val * w;
}
outptr[j] = activation_ss(sum, activation_type, activation_params);
}
outptr += outw;
}
}
}
else
{
// group convolution
const int inch_g = inch / group;
const int outch_g = outch / group;
#ifdef _WIN32
#pragma omp parallel for num_threads(opt.num_threads)
#else
#pragma omp parallel for collapse(2) num_threads(opt.num_threads)
#endif
for (int g = 0; g < group; g++)
{
for (int p = 0; p < outch_g; p++)
{
float* outptr = top_blob.channel(g * outch_g + p);
const float* weight_data_ptr = (const float*)weight_data + maxk * inch_g * outch_g * g;
// shadowed variable for less openmp task args
const int outw = top_blob.w;
const int outh = top_blob.h;
for (int i = 0; i < outh; i++)
{
for (int j = 0; j < outw; j++)
{
float sum = 0.f;
if (bias_term)
sum = bias_data[outch_g * g + p];
const float* kptr = weight_data_ptr + maxk * inch_g * p;
for (int q = 0; q < inch_g; q++)
{
const Mat m = bottom_blob.channel(inch_g * g + q);
const float* sptr = m.row(i * stride_h) + j * stride_w;
for (int k = 0; k < maxk; k++)
{
float val = sptr[space_ofs[k]];
float w = kptr[k];
sum += val * w;
}
kptr += maxk;
}
outptr[j] = activation_ss(sum, activation_type, activation_params);
}
outptr += outw;
}
}
}
}
return 0;
}
int ConvolutionDepthWise::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
{
// convolv with NxN kernel
// value = value + bias
#if NCNN_INT8
if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
{
return forward_int8(bottom_blob, top_blob, opt);
}
#endif
// NCNN_LOGE("ConvolutionDepthWise input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
Mat bottom_blob_bordered;
make_padding(bottom_blob, bottom_blob_bordered, opt);
if (bottom_blob_bordered.empty())
return -100;
const int w = bottom_blob_bordered.w;
const int h = bottom_blob_bordered.h;
const size_t elemsize = bottom_blob_bordered.elemsize;
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
const int outw = (w - kernel_extent_w) / stride_w + 1;
const int outh = (h - kernel_extent_h) / stride_h + 1;
top_blob.create(outw, outh, num_output, elemsize, opt.blob_allocator);
if (top_blob.empty())
return -100;
int ret = convolutiondepthwise(bottom_blob_bordered, top_blob, weight_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, dilation_w, dilation_h, group, activation_type, activation_params, opt);
if (ret != 0)
return ret;
return 0;
}
int ConvolutionDepthWise::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 _kernel_h = _weight_data.h;
const int _num_output = _weight_data.c;
Mat weight_data_flattened;
flatten(_weight_data, weight_data_flattened, opt);
if (weight_data_flattened.empty())
return -100;
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;
}
Mat bottom_blob_bordered;
make_padding(bottom_blob, bottom_blob_bordered, _kernel_w, _kernel_h, opt);
if (bottom_blob_bordered.empty())
return -100;
const int w = bottom_blob_bordered.w;
const int h = bottom_blob_bordered.h;
const size_t elemsize = bottom_blob_bordered.elemsize;
const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
const int kernel_extent_h = dilation_h * (_kernel_h - 1) + 1;
const int outw = (w - kernel_extent_w) / stride_w + 1;
const int outh = (h - kernel_extent_h) / stride_h + 1;
top_blob.create(outw, outh, _num_output, elemsize, opt.blob_allocator);
if (top_blob.empty())
return -100;
int ret = convolutiondepthwise(bottom_blob_bordered, top_blob, weight_data_flattened, bias_data_flattened, _kernel_w, _kernel_h, stride_w, stride_h, dilation_w, dilation_h, group, activation_type, activation_params, opt);
if (ret != 0)
return ret;
return 0;
}
void ConvolutionDepthWise::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const
{
make_padding(bottom_blob, bottom_blob_bordered, kernel_w, kernel_h, opt);
}
void ConvolutionDepthWise::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, int _kernel_w, int _kernel_h, const Option& opt) const
{
int w = bottom_blob.w;
int h = bottom_blob.h;
const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
const int kernel_extent_h = dilation_h * (_kernel_h - 1) + 1;
bottom_blob_bordered = bottom_blob;
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
{
Option opt_b = opt;
opt_b.blob_allocator = opt.workspace_allocator;
copy_make_border(bottom_blob, bottom_blob_bordered, pad_top, pad_bottom, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b);
}
else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233)
{
// tensorflow padding=SAME or onnx padding=SAME_UPPER
int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
if (wpad > 0 || hpad > 0)
{
Option opt_b = opt;
opt_b.blob_allocator = opt.workspace_allocator;
copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
}
}
else if (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234)
{
// onnx padding=SAME_LOWER
int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
if (wpad > 0 || hpad > 0)
{
Option opt_b = opt;
opt_b.blob_allocator = opt.workspace_allocator;
copy_make_border(bottom_blob, bottom_blob_bordered, hpad - hpad / 2, hpad / 2, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
}
}
}
#if NCNN_INT8
static inline signed char float2int8(float v)
{
int int32 = static_cast<int>(round(v));
if (int32 > 127) return 127;
if (int32 < -127) return -127;
return (signed char)int32;
}
int ConvolutionDepthWise::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
{
// convolv with NxN kernel
// value = value + bias
int w = bottom_blob.w;
int h = bottom_blob.h;
int channels = bottom_blob.c;
size_t elemsize = bottom_blob.elemsize;
if (channels % group != 0 || num_output % group != 0)
{
// reject invalid group
return -100;
}
// NCNN_LOGE("ConvolutionDepthWise input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h);
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
Mat bottom_blob_int8 = bottom_blob;
if (elemsize != 1)
{
const int channels_g = channels / group;
Mat scales(channels);
{
float* ps = scales;
for (int g = 0; g < group; g++)
{
float scale = bottom_blob_int8_scales[g];
for (int q = 0; q < channels_g; q++)
{
*ps++ = scale;
}
}
}
Option opt_q = opt;
opt_q.blob_allocator = opt.workspace_allocator;
quantize_to_int8(bottom_blob, bottom_blob_int8, scales, opt_q);
}
Mat bottom_blob_bordered;
make_padding(bottom_blob_int8, bottom_blob_bordered, opt);
if (bottom_blob_bordered.empty())
return -100;
w = bottom_blob_bordered.w;
h = bottom_blob_bordered.h;
int outw = (w - kernel_extent_w) / stride_w + 1;
int outh = (h - kernel_extent_h) / stride_h + 1;
const int maxk = kernel_w * kernel_h;
// kernel offsets
std::vector<int> _space_ofs(maxk);
int* space_ofs = &_space_ofs[0];
{
int p1 = 0;
int p2 = 0;
int gap = w * dilation_h - kernel_w * dilation_w;
for (int i = 0; i < kernel_h; i++)
{
for (int j = 0; j < kernel_w; j++)
{
space_ofs[p1] = p2;
p1++;
p2 += dilation_w;
}
p2 += gap;
}
}
// int8
bool use_int8_requantize = int8_scale_term > 100;
size_t out_elemsize = use_int8_requantize ? 1u : 4u;
top_blob.create(outw, outh, num_output, out_elemsize, opt.blob_allocator);
if (top_blob.empty())
return -100;
// depth-wise
if (channels == group && group == num_output)
{
#pragma omp parallel for num_threads(opt.num_threads)
for (int g = 0; g < group; g++)
{
signed char* outptr = top_blob.channel(g);
const signed char* kptr = (const signed char*)weight_data + maxk * g;
const Mat m = bottom_blob_bordered.channel(g);
for (int i = 0; i < outh; i++)
{
for (int j = 0; j < outw; j++)
{
int sum = 0;
const signed char* sptr = m.row<signed char>(i * stride_h) + j * stride_w;
for (int k = 0; k < maxk; k++)
{
signed char val = sptr[space_ofs[k]];
signed char w = kptr[k];
sum += val * w;
}
float scale_in;
if (weight_data_int8_scales[g] == 0)
scale_in = 0;
else
scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
float sumfp32 = sum * scale_in;
if (bias_term)
sumfp32 += bias_data[g];
sumfp32 = activation_ss(sumfp32, activation_type, activation_params);
if (use_int8_requantize)
{
// requantize
float scale_out = top_blob_int8_scales[g];
signed char sums8 = float2int8(sumfp32 * scale_out);
outptr[0] = sums8;
outptr += 1;
}
else
{
// dequantize
((float*)outptr)[0] = sumfp32;
outptr += 4;
}
}
}
}
}
else
{
// group convolution
const int channels_g = channels / group;
const int num_output_g = num_output / group;
#ifdef _WIN32
#pragma omp parallel for num_threads(opt.num_threads)
#else // _WIN32
#pragma omp parallel for collapse(2) num_threads(opt.num_threads)
#endif // _WIN32
for (int g = 0; g < group; g++)
{
for (int p = 0; p < num_output_g; p++)
{
signed char* outptr = top_blob.channel(g * num_output_g + p);
const signed char* weight_data_ptr = (const signed char*)weight_data + maxk * channels_g * num_output_g * g;
for (int i = 0; i < outh; i++)
{
for (int j = 0; j < outw; j++)
{
int sum = 0;
const signed char* kptr = weight_data_ptr + maxk * channels_g * p;
// channels_g
for (int q = 0; q < channels_g; q++)
{
const Mat m = bottom_blob_bordered.channel(channels_g * g + q);
const signed char* sptr = m.row<signed char>(i * stride_h) + j * stride_w;
for (int k = 0; k < maxk; k++)
{
signed char val = sptr[space_ofs[k]];
signed char w = kptr[k];
sum += val * w;
}
kptr += maxk;
}
float scale_in;
if (weight_data_int8_scales[g] == 0)
scale_in = 0;
else
scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
float sumfp32 = sum * scale_in;
if (bias_term)
sumfp32 += bias_data[g * num_output_g + p];
sumfp32 = activation_ss(sumfp32, activation_type, activation_params);
if (use_int8_requantize)
{
// requantize
float scale_out = top_blob_int8_scales[g];
signed char sums8 = float2int8(sumfp32 * scale_out);
outptr[0] = sums8;
outptr += 1;
}
else
{
// dequantize
((float*)outptr)[0] = sumfp32;
outptr += 4;
}
}
}
}
}
}
return 0;
}
#endif // NCNN_INT8
} // namespace ncnn
|