// 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 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(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(i); signed char* ptr = top_blob.row(i); float bias = bias_data_size > 1 ? bias_data[i] : bias_data[0]; for (int j=0; j(i); signed char* ptr = top_blob.row(i); for (int j=0; j 1 ? bias_data[q] : bias_data[0]; for (int i=0; i