• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "wrapper/fp32/dequant_int8_to_fp32_wrapper.h"
18 #include <stdint.h>
19 #include <string.h>
DequantDataPerChannel(const int8_t * quant_src,const DeQuantArg ** de_quant_args,size_t de_quant_nums,size_t per_batch_size,float * de_quant_dst)20 void DequantDataPerChannel(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums,
21                            size_t per_batch_size, float *de_quant_dst) {
22   if (per_batch_size == 0) {
23     return;
24   }
25   size_t matrix_size = de_quant_nums / per_batch_size;
26   for (int i = 0; i < per_batch_size; i++) {
27     const DeQuantArg *de_quant_arg = de_quant_args[i];
28     float scale = de_quant_arg->scale;
29     int32_t zero_point = de_quant_arg->zeroPoint;
30     for (int j = 0; j < matrix_size; j++) {
31       de_quant_dst[i * matrix_size + j] = (quant_src[i * matrix_size + j] - zero_point) * scale;
32     }
33   }
34 }
35 
DequantData(const int8_t * quant_src,const DeQuantArg ** de_quant_args,size_t de_quant_nums,size_t channels,float * de_quant_dst)36 void DequantData(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, size_t channels,
37                  float *de_quant_dst) {
38   if (channels == 0) {
39     return;
40   }
41   size_t per_channel_size = de_quant_nums / channels;
42   for (size_t i = 0; i < channels; i++) {
43     const DeQuantArg *de_quant_arg = de_quant_args[i];
44     float scale = de_quant_arg->scale;
45     int32_t zero_point = de_quant_arg->zeroPoint;
46     float var_corr = de_quant_arg->var_corr;
47     float mean_corr = de_quant_arg->mean_corr;
48     if (var_corr < 0 || var_corr > 10) {
49       var_corr = 1;
50     }
51     for (size_t j = 0; j < per_channel_size; j++) {
52       float dequant_data = (quant_src[per_channel_size * i + j] - zero_point) * scale;
53       de_quant_dst[per_channel_size * i + j] = dequant_data * var_corr + mean_corr;
54     }
55   }
56 }
57 
DequantDataPerTensor(const int8_t * quant_src,const DeQuantArg ** de_quant_args,size_t de_quant_nums,float * de_quant_dst)58 void DequantDataPerTensor(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums,
59                           float *de_quant_dst) {
60   const DeQuantArg *de_quant_arg = de_quant_args[0];
61   float *quant_clusters = de_quant_arg->clusters;
62   float scale = de_quant_arg->scale;
63   int32_t zero_point = de_quant_arg->zeroPoint;
64   for (int j = 0; j < de_quant_nums; j++) {
65     int8_t quant_data = quant_src[j];
66     if (quant_clusters != NULL) {
67       if (quant_data > INT8_MAX || quant_data < INT8_MIN) {
68         return;
69       }
70       de_quant_dst[j] = quant_clusters[quant_data - INT8_MIN];
71     } else {
72       de_quant_dst[j] = (quant_data - zero_point) * scale;
73     }
74   }
75 }
76