Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 6.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2018 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 "dequantize.h"
16
17namespace ncnn {
18
19Dequantize::Dequantize()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int Dequantize::load_param(const ParamDict& pd)
26{
27 scale_data_size = pd.get(0, 1);
28 bias_data_size = pd.get(1, 0);
29
30 return 0;
31}
32
33int Dequantize::load_model(const ModelBin& mb)
34{
35 scale_data = mb.load(scale_data_size, 1);
36 if (scale_data.empty())
37 return -100;
38
39 if (bias_data_size)
40 {
41 bias_data = mb.load(bias_data_size, 1);
42 if (bias_data.empty())
43 return -100;
44 }
45
46 return 0;
47}
48
49int Dequantize::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
50{
51 int dims = bottom_blob.dims;
52
53 if (dims == 1)
54 {
55 int w = bottom_blob.w;
56
57 top_blob.create(w, (size_t)4u, opt.blob_allocator);
58 if (top_blob.empty())
59 return -100;
60
61 const int* intptr = bottom_blob;
62 float* ptr = top_blob;
63
64 if (scale_data_size == 1)
65 {
66 const float scale = scale_data[0];
67
68 if (bias_data_size == 0)
69 {
70 #pragma omp parallel for num_threads(opt.num_threads)
71 for (int i = 0; i < w; i++)
72 {
73 ptr[i] = intptr[i] * scale;
74 }
75 }
76 else if (bias_data_size == 1)
77 {
78 const float bias = bias_data[0];
79
80 #pragma omp parallel for num_threads(opt.num_threads)
81 for (int i = 0; i < w; i++)
82 {
83 ptr[i] = intptr[i] * scale + bias;
84 }
85 }
86 else
87 {
88 #pragma omp parallel for num_threads(opt.num_threads)
89 for (int i = 0; i < w; i++)
90 {
91 ptr[i] = intptr[i] * scale + bias_data[i];
92 }
93 }
94 }
95 else
96 {
97 if (bias_data_size == 0)
98 {
99 #pragma omp parallel for num_threads(opt.num_threads)
100 for (int i = 0; i < w; i++)
101 {
102 ptr[i] = intptr[i] * scale_data[i];
103 }
104 }
105 else if (bias_data_size == 1)
106 {
107 const float bias = bias_data[0];
108
109 #pragma omp parallel for num_threads(opt.num_threads)
110 for (int i = 0; i < w; i++)
111 {
112 ptr[i] = intptr[i] * scale_data[i] + bias;
113 }
114 }
115 else
116 {
117 #pragma omp parallel for num_threads(opt.num_threads)
118 for (int i = 0; i < w; i++)
119 {
120 ptr[i] = intptr[i] * scale_data[i] + bias_data[i];
121 }
122 }
123 }
124 }
125
126 if (dims == 2)
127 {
128 int w = bottom_blob.w;
129 int h = bottom_blob.h;
130
131 top_blob.create(w, h, (size_t)4u, opt.blob_allocator);
132 if (top_blob.empty())
133 return -100;
134
135 if (bias_data_size == 0)
136 {
137 #pragma omp parallel for num_threads(opt.num_threads)
138 for (int i = 0; i < h; i++)
139 {
140 const int* intptr = bottom_blob.row<const int>(i);
141 float* ptr = top_blob.row(i);
142
143 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
144
145 for (int j = 0; j < w; j++)
146 {
147 ptr[j] = intptr[j] * scale;
148 }
149 }
150 }
151 else
152 {
153 #pragma omp parallel for num_threads(opt.num_threads)
154 for (int i = 0; i < h; i++)
155 {
156 const int* intptr = bottom_blob.row<const int>(i);
157 float* ptr = top_blob.row(i);
158
159 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
160 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[i];
161
162 for (int j = 0; j < w; j++)
163 {
164 ptr[j] = intptr[j] * scale + bias;
165 }
166 }
167 }
168 }
169
170 if (dims == 3)
171 {
172 int w = bottom_blob.w;
173 int h = bottom_blob.h;
174 int channels = bottom_blob.c;
175 int size = w * h;
176
177 top_blob.create(w, h, channels, (size_t)4u, opt.blob_allocator);
178 if (top_blob.empty())
179 return -100;
180
181 if (bias_data_size == 0)
182 {
183 #pragma omp parallel for num_threads(opt.num_threads)
184 for (int q = 0; q < channels; q++)
185 {
186 const int* intptr = bottom_blob.channel(q);
187 float* ptr = top_blob.channel(q);
188
189 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
190
191 for (int i = 0; i < size; i++)
192 {
193 ptr[i] = intptr[i] * scale;
194 }
195 }
196 }
197 else
198 {
199 #pragma omp parallel for num_threads(opt.num_threads)
200 for (int q = 0; q < channels; q++)
201 {
202 const int* intptr = bottom_blob.channel(q);
203 float* ptr = top_blob.channel(q);
204
205 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
206 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[q];
207
208 for (int i = 0; i < size; i++)
209 {
210 ptr[i] = intptr[i] * scale + bias;
211 }
212 }
213 }
214 }
215
216 return 0;
217}
218
219} // namespace ncnn