 new int8 implement,better accuracy (#749)
* add the armv7a conv3x3s1 implement without overflow,remove old codes
* fix the bug of conv3x3s2 packed int8
* new int8 implement,weight quant by perchanel,better accuracy~
* fix the bug of conv3x3s1 packed int8 neon
* add the naive c fp32 and int8 winograd F(2,3)
* add the neon intrinsic int8 winograd F(2,3)
* optimize the armv7a int8 winograd F(2,3) with neon assembly
* optimize the armv7a int8 winograd F(2,3) input transform with assembly.
* add the requantize layer and int8 relu implement.
* add graph optimize conv1x1s2 -> conv1x1s1,begin optimize int8 aarch64.
* fix int8 bugs
* add the c naive im2col with sgemm
* add aarch64 int8 winograd f23, conv3x3s2 naive implement
* add the int8 sgemm conv7x7s2 on x86/armv7a platform
* optimize the int8 sgemm by neon intrinsic and packed kernel
* optimize the int8 sgemm with packed data
* optimize the int8 sgemm with armv7a neon assembly
* add the int8 sgemm on arm64-v8a platform
* perpare to merge latest codes from master
* add the int8 param files
* In the Class Net,add the fuse_network method
7 years ago  new int8 implement,better accuracy (#749)
* add the armv7a conv3x3s1 implement without overflow,remove old codes
* fix the bug of conv3x3s2 packed int8
* new int8 implement,weight quant by perchanel,better accuracy~
* fix the bug of conv3x3s1 packed int8 neon
* add the naive c fp32 and int8 winograd F(2,3)
* add the neon intrinsic int8 winograd F(2,3)
* optimize the armv7a int8 winograd F(2,3) with neon assembly
* optimize the armv7a int8 winograd F(2,3) input transform with assembly.
* add the requantize layer and int8 relu implement.
* add graph optimize conv1x1s2 -> conv1x1s1,begin optimize int8 aarch64.
* fix int8 bugs
* add the c naive im2col with sgemm
* add aarch64 int8 winograd f23, conv3x3s2 naive implement
* add the int8 sgemm conv7x7s2 on x86/armv7a platform
* optimize the int8 sgemm by neon intrinsic and packed kernel
* optimize the int8 sgemm with packed data
* optimize the int8 sgemm with armv7a neon assembly
* add the int8 sgemm on arm64-v8a platform
* perpare to merge latest codes from master
* add the int8 param files
* In the Class Net,add the fuse_network method
7 years ago  new int8 implement,better accuracy (#749)
* add the armv7a conv3x3s1 implement without overflow,remove old codes
* fix the bug of conv3x3s2 packed int8
* new int8 implement,weight quant by perchanel,better accuracy~
* fix the bug of conv3x3s1 packed int8 neon
* add the naive c fp32 and int8 winograd F(2,3)
* add the neon intrinsic int8 winograd F(2,3)
* optimize the armv7a int8 winograd F(2,3) with neon assembly
* optimize the armv7a int8 winograd F(2,3) input transform with assembly.
* add the requantize layer and int8 relu implement.
* add graph optimize conv1x1s2 -> conv1x1s1,begin optimize int8 aarch64.
* fix int8 bugs
* add the c naive im2col with sgemm
* add aarch64 int8 winograd f23, conv3x3s2 naive implement
* add the int8 sgemm conv7x7s2 on x86/armv7a platform
* optimize the int8 sgemm by neon intrinsic and packed kernel
* optimize the int8 sgemm with packed data
* optimize the int8 sgemm with armv7a neon assembly
* add the int8 sgemm on arm64-v8a platform
* perpare to merge latest codes from master
* add the int8 param files
* In the Class Net,add the fuse_network method
7 years ago  new int8 implement,better accuracy (#749)
* add the armv7a conv3x3s1 implement without overflow,remove old codes
* fix the bug of conv3x3s2 packed int8
* new int8 implement,weight quant by perchanel,better accuracy~
* fix the bug of conv3x3s1 packed int8 neon
* add the naive c fp32 and int8 winograd F(2,3)
* add the neon intrinsic int8 winograd F(2,3)
* optimize the armv7a int8 winograd F(2,3) with neon assembly
* optimize the armv7a int8 winograd F(2,3) input transform with assembly.
* add the requantize layer and int8 relu implement.
* add graph optimize conv1x1s2 -> conv1x1s1,begin optimize int8 aarch64.
* fix int8 bugs
* add the c naive im2col with sgemm
* add aarch64 int8 winograd f23, conv3x3s2 naive implement
* add the int8 sgemm conv7x7s2 on x86/armv7a platform
* optimize the int8 sgemm by neon intrinsic and packed kernel
* optimize the int8 sgemm with packed data
* optimize the int8 sgemm with armv7a neon assembly
* add the int8 sgemm on arm64-v8a platform
* perpare to merge latest codes from master
* add the int8 param files
* In the Class Net,add the fuse_network method
7 years ago |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- // BUG1989 is pleased to support the open source community by supporting ncnn available.
- //
- // Copyright (C) 2019 BUG1989. 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 "requantize.h"
-
- #include <math.h>
-
- namespace ncnn {
-
- DEFINE_LAYER_CREATOR(Requantize)
-
- Requantize::Requantize()
- {
- one_blob_only = true;
- support_inplace = false;
- fusion_relu = false;
- }
-
- 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 Requantize::load_param(const ParamDict& pd)
- {
- scale_in = pd.get(0, 1.f); // bottom_blob_scale * weight_scale
- scale_out = pd.get(1, 1.f); // top_blob_scale
- bias_term = pd.get(2, 0);
- bias_data_size = pd.get(3, 0);
- fusion_relu = pd.get(4, 0);
-
- return 0;
- }
-
- int Requantize::load_model(const ModelBin& mb)
- {
- if (bias_term)
- {
- bias_data = mb.load(bias_data_size, 1);
- if (bias_data.empty())
- return -100;
- }
-
- return 0;
- }
-
- int Requantize::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
- {
- int dims = bottom_blob.dims;
-
- if (dims == 1)
- {
- int w = bottom_blob.w;
-
- const int* intptr = bottom_blob;
- signed char * ptr = top_blob;
-
- if (bias_term)
- {
- if (bias_data_size > 1)
- {
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int i=0; i<w; i++)
- {
- ptr[i] = float2int8(((intptr[i] * scale_in) + bias_data[i]) * scale_out);
- if (fusion_relu && ptr[i] < 0)
- ptr[i] = 0;
- }
- }
- else
- {
- float bias = bias_data[0];
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int i=0; i<w; i++)
- {
- ptr[i] = float2int8(((intptr[i] * scale_in) + bias) * scale_out);
- if (fusion_relu && ptr[i] < 0)
- ptr[i] = 0;
- }
- }
- }
- else
- {
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int i=0; i<w; i++)
- {
- ptr[i] = float2int8(intptr[i] * scale_in * scale_out);
- if (fusion_relu && ptr[i] < 0)
- ptr[i] = 0;
- }
- }
- }
-
- if (dims == 2)
- {
- int w = bottom_blob.w;
- int h = bottom_blob.h;
-
- if (bias_term)
- {
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int i=0; i<h; i++)
- {
- const int* intptr = bottom_blob.row<const int>(i);
- signed char* ptr = top_blob.row<signed char>(i);
-
- float bias = bias_data_size > 1 ? bias_data[i] : bias_data[0];
-
- for (int j=0; j<w; j++)
- {
- ptr[j] = float2int8(((intptr[j] * scale_in) + bias) * scale_out);
- if (fusion_relu && ptr[j] < 0)
- ptr[j] = 0;
- }
- }
- }
- else
- {
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int i=0; i<h; i++)
- {
- const int* intptr = bottom_blob.row<const int>(i);
- signed char* ptr = top_blob.row<signed char>(i);
-
- for (int j=0; j<w; j++)
- {
- ptr[j] = float2int8(intptr[j] * scale_in * scale_out);
- if (fusion_relu && ptr[j] < 0)
- ptr[j] = 0;
- }
- }
- }
- }
-
- if (dims == 3)
- {
- int w = bottom_blob.w;
- int h = bottom_blob.h;
- int channels = bottom_blob.c;
- int size = w * h;
-
- if (bias_term)
- {
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- const int* intptr = bottom_blob.channel(q);
- signed char* ptr = top_blob.channel(q);
-
- float bias = bias_data_size > 1 ? bias_data[q] : bias_data[0];
-
- for (int i=0; i<size; i++)
- {
- ptr[i] = float2int8(((intptr[i] * scale_in) + bias) * scale_out);
- if (fusion_relu && ptr[i] < 0)
- ptr[i] = 0;
- }
- }
- }
- else
- {
- #pragma omp parallel for num_threads(opt.num_threads)
- for (int q=0; q<channels; q++)
- {
- const int* intptr = bottom_blob.channel(q);
- signed char* ptr = top_blob.channel(q);
-
- for (int i=0; i<size; i++)
- {
- ptr[i] = float2int8(intptr[i] * scale_in * scale_out);
- if (fusion_relu && ptr[i] < 0)
- ptr[i] = 0;
- }
- }
- }
- }
-
- return 0;
- }
-
- } // namespace ncnn
|