1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
21
one_blob_only = false;
22
support_inplace = false;
25
int RNN::load_param(const ParamDict& pd)
27
num_output = pd.get(0, 0);
28
weight_data_size = pd.get(1, 0);
29
direction = pd.get(2, 0);
30
int8_scale_term = pd.get(8, 0);
35
NCNN_LOGE("please build ncnn with NCNN_INT8 enabled for int8 inference");
43
int RNN::load_model(const ModelBin& mb)
45
int num_directions = direction == 2 ? 2 : 1;
47
int size = weight_data_size / num_directions / num_output;
50
weight_xc_data = mb.load(size, num_output, num_directions, 0);
51
if (weight_xc_data.empty())
54
bias_c_data = mb.load(num_output, 1, num_directions, 0);
55
if (bias_c_data.empty())
58
weight_hc_data = mb.load(num_output, num_output, num_directions, 0);
59
if (weight_hc_data.empty())
65
weight_xc_data_int8_scales = mb.load(num_output, num_directions, 1);
66
weight_hc_data_int8_scales = mb.load(num_output, num_directions, 1);
73
static int rnn(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc, const Mat& bias_c, const Mat& weight_hc, Mat& hidden_state, const Option& opt)
75
int size = bottom_blob.w;
76
int T = bottom_blob.h;
78
int num_output = top_blob.w;
81
Mat gates(num_output, 4u, opt.workspace_allocator);
86
for (int t = 0; t < T; t++)
88
int ti = reverse ? T - 1 - t : t;
90
const float* x = bottom_blob.row(ti);
91
#pragma omp parallel for num_threads(opt.num_threads)
92
for (int q = 0; q < num_output; q++)
94
const float* weight_xc_ptr = weight_xc.row(q);
95
const float* weight_hc_ptr = weight_hc.row(q);
99
for (int i = 0; i < size; i++)
101
H += weight_xc_ptr[i] * x[i];
104
for (int i = 0; i < num_output; i++)
106
H += weight_hc_ptr[i] * hidden_state[i];
114
float* output_data = top_blob.row(ti);
115
#pragma omp parallel for num_threads(opt.num_threads)
116
for (int q = 0; q < num_output; q++)
129
static int rnn_int8(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc_int8, const float* weight_xc_int8_scales, const Mat& bias_c, const Mat& weight_hc_int8, const float* weight_hc_int8_scales, Mat& hidden_state, const Option& opt)
131
int size = bottom_blob.w;
132
int T = bottom_blob.h;
134
int num_output = top_blob.w;
137
Mat gates(num_output, 4u, opt.workspace_allocator);
141
// dynamic quantize bottom_blob
142
Mat bottom_blob_int8(size, T, (size_t)1u, 1, opt.workspace_allocator);
143
Mat bottom_blob_int8_scales(T, (size_t)4u, 1, opt.workspace_allocator);
145
for (int t = 0; t < T; t++)
147
const float* x = bottom_blob.row(t);
150
for (int i = 0; i < size; i++)
152
absmax = std::max(absmax, (float)fabs(x[i]));
155
bottom_blob_int8_scales[t] = 127.f / absmax;
158
Option opt_quant = opt;
159
opt_quant.blob_allocator = opt.workspace_allocator;
160
opt_quant.use_packing_layout = false;
161
quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_quant);
164
Mat hidden_state_int8(num_output, (size_t)1u, 1, opt.workspace_allocator);
165
Mat hidden_state_int8_scales(1, (size_t)4u, 1, opt.workspace_allocator);
168
for (int t = 0; t < T; t++)
170
int ti = reverse ? T - 1 - t : t;
172
// dynamic quantize hidden_state
175
for (int i = 0; i < num_output; i++)
177
absmax = std::max(absmax, (float)fabs(hidden_state[i]));
182
hidden_state_int8_scales[0] = 1.f;
183
hidden_state_int8.fill<signed char>(0);
187
hidden_state_int8_scales[0] = 127.f / absmax;
189
Option opt_quant = opt;
190
opt_quant.blob_allocator = opt.workspace_allocator;
191
opt_quant.use_packing_layout = false;
192
quantize_to_int8(hidden_state, hidden_state_int8, hidden_state_int8_scales, opt_quant);
196
const signed char* x = bottom_blob_int8.row<const signed char>(ti);
197
const signed char* hs = hidden_state_int8;
198
const float descale_x = 1.f / bottom_blob_int8_scales[ti];
199
const float descale_h = 1.f / hidden_state_int8_scales[0];
200
#pragma omp parallel for num_threads(opt.num_threads)
201
for (int q = 0; q < num_output; q++)
203
const signed char* weight_xc_int8_ptr = weight_xc_int8.row<const signed char>(q);
204
const signed char* weight_hc_int8_ptr = weight_hc_int8.row<const signed char>(q);
206
const float descale_xc = 1.f / weight_xc_int8_scales[q];
207
const float descale_hc = 1.f / weight_hc_int8_scales[q];
210
for (int i = 0; i < size; i++)
212
Hx += weight_xc_int8_ptr[i] * x[i];
216
for (int i = 0; i < num_output; i++)
218
Hh += weight_hc_int8_ptr[i] * hs[i];
221
float H = bias_c[q] + Hx * (descale_x * descale_xc) + Hh * (descale_h * descale_hc);
228
float* output_data = top_blob.row(ti);
229
#pragma omp parallel for num_threads(opt.num_threads)
230
for (int q = 0; q < num_output; q++)
243
int RNN::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
245
int T = bottom_blob.h;
247
int num_directions = direction == 2 ? 2 : 1;
249
// initial hidden state
250
Mat hidden(num_output, 4u, opt.workspace_allocator);
255
top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
256
if (top_blob.empty())
260
if (direction == 0 || direction == 1)
265
int ret = rnn_int8(bottom_blob, top_blob, direction, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), hidden, opt);
272
int ret = rnn(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
280
Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
281
if (top_blob_forward.empty())
284
Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
285
if (top_blob_reverse.empty())
291
int ret = rnn_int8(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), hidden, opt);
298
int ret = rnn(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
308
int ret = rnn_int8(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), weight_xc_data_int8_scales.row(1), bias_c_data.channel(1), weight_hc_data.channel(1), weight_hc_data_int8_scales.row(1), hidden, opt);
315
int ret = rnn(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), hidden, opt);
321
for (int i = 0; i < T; i++)
323
const float* pf = top_blob_forward.row(i);
324
const float* pr = top_blob_reverse.row(i);
325
float* ptr = top_blob.row(i);
327
memcpy(ptr, pf, num_output * sizeof(float));
328
memcpy(ptr + num_output, pr, num_output * sizeof(float));
335
int RNN::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
337
const Mat& bottom_blob = bottom_blobs[0];
338
int T = bottom_blob.h;
339
int num_directions = direction == 2 ? 2 : 1;
342
Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
343
if (bottom_blobs.size() == 2)
345
hidden = bottom_blobs[1].clone(hidden_allocator);
349
hidden.create(num_output, num_directions, 4u, hidden_allocator);
355
Mat& top_blob = top_blobs[0];
356
top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
357
if (top_blob.empty())
361
if (direction == 0 || direction == 1)
366
int ret = rnn_int8(bottom_blob, top_blob, direction, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), hidden, opt);
373
int ret = rnn(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
381
Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
382
if (top_blob_forward.empty())
385
Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
386
if (top_blob_reverse.empty())
389
Mat hidden0 = hidden.row_range(0, 1);
393
int ret = rnn_int8(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), hidden0, opt);
400
int ret = rnn(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden0, opt);
405
Mat hidden1 = hidden.row_range(1, 1);
409
int ret = rnn_int8(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), weight_xc_data_int8_scales.row(1), bias_c_data.channel(1), weight_hc_data.channel(1), weight_hc_data_int8_scales.row(1), hidden1, opt);
416
int ret = rnn(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), hidden1, opt);
422
for (int i = 0; i < T; i++)
424
const float* pf = top_blob_forward.row(i);
425
const float* pr = top_blob_reverse.row(i);
426
float* ptr = top_blob.row(i);
428
memcpy(ptr, pf, num_output * sizeof(float));
429
memcpy(ptr + num_output, pr, num_output * sizeof(float));
433
if (top_blobs.size() == 2)
435
top_blobs[1] = hidden;