| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 | static void requantize_relu_pack8_msa(const Mat& bottom_blob, Mat& top_blob, const Mat& scale_in_data, const Mat& scale_out_data, const Mat& bias_data, const Option& opt) |
| 16 | { |
| 17 | int w = bottom_blob.w; |
| 18 | int h = bottom_blob.h; |
| 19 | int channels = bottom_blob.c; |
| 20 | int size = w * h; |
| 21 | |
| 22 | int scale_in_data_size = scale_in_data.w; |
| 23 | int scale_out_data_size = scale_out_data.w; |
| 24 | int bias_data_size = bias_data.w; |
| 25 | |
| 26 | // int8(relu(v * scale_in) * scale_out) |
| 27 | // int8_relu(v * (scale_in * scale_out)) |
| 28 | |
| 29 | // int8(relu(v * scale_in + bias) * scale_out) |
| 30 | // int8_relu(v * (scale_in * scale_out) + (bias * scale_out)) |
| 31 | |
| 32 | if (bias_data_size == 0) |
| 33 | { |
| 34 | #pragma omp parallel for num_threads(opt.num_threads) |
| 35 | for (int q = 0; q < channels; q++) |
| 36 | { |
| 37 | const int* intptr = bottom_blob.channel(q); |
| 38 | signed char* ptr = top_blob.channel(q); |
| 39 | |
| 40 | v4f32 _scale_in0 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8, 0); |
| 41 | v4f32 _scale_in1 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8 + 4, 0); |
| 42 | v4f32 _scale_out0 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8, 0); |
| 43 | v4f32 _scale_out1 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8 + 4, 0); |
| 44 | |
| 45 | v4f32 _scale0 = __msa_fmul_w(_scale_in0, _scale_out0); |
| 46 | v4f32 _scale1 = __msa_fmul_w(_scale_in1, _scale_out1); |
| 47 | |
| 48 | int i = 0; |
| 49 | for (; i + 3 < size; i += 4) |
| 50 | { |
| 51 | __builtin_prefetch(intptr + 128); |
| 52 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 53 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0)); |
| 54 | v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0)); |
| 55 | v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0)); |
| 56 | v4f32 _v4 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 16, 0)); |
| 57 | v4f32 _v5 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 20, 0)); |
| 58 | v4f32 _v6 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 24, 0)); |
| 59 | v4f32 _v7 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 28, 0)); |
| 60 | _v0 = __msa_fmul_w(_v0, _scale0); |
| 61 | _v1 = __msa_fmul_w(_v1, _scale1); |
| 62 | _v2 = __msa_fmul_w(_v2, _scale0); |
| 63 | _v3 = __msa_fmul_w(_v3, _scale1); |
| 64 | _v4 = __msa_fmul_w(_v4, _scale0); |
| 65 | _v5 = __msa_fmul_w(_v5, _scale1); |
| 66 | _v6 = __msa_fmul_w(_v6, _scale0); |
| 67 | _v7 = __msa_fmul_w(_v7, _scale1); |
| 68 | *((int64_t*)ptr) = float2int8relu(_v0, _v1); |
| 69 | *((int64_t*)(ptr + 8)) = float2int8relu(_v2, _v3); |
| 70 | *((int64_t*)(ptr + 16)) = float2int8relu(_v4, _v5); |
| 71 | *((int64_t*)(ptr + 24)) = float2int8relu(_v6, _v7); |
| 72 | |
| 73 | intptr += 32; |
| 74 | ptr += 32; |
| 75 | } |
| 76 | for (; i + 1 < size; i += 2) |
| 77 | { |
| 78 | __builtin_prefetch(intptr + 64); |
| 79 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 80 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0)); |
| 81 | v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0)); |
| 82 | v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0)); |
| 83 | _v0 = __msa_fmul_w(_v0, _scale0); |
| 84 | _v1 = __msa_fmul_w(_v1, _scale1); |
| 85 | _v2 = __msa_fmul_w(_v2, _scale0); |
| 86 | _v3 = __msa_fmul_w(_v3, _scale1); |
| 87 | *((int64_t*)ptr) = float2int8relu(_v0, _v1); |
| 88 | *((int64_t*)(ptr + 8)) = float2int8relu(_v2, _v3); |
| 89 | |
| 90 | intptr += 16; |
| 91 | ptr += 16; |
| 92 | } |
| 93 | for (; i < size; i++) |
| 94 | { |
| 95 | __builtin_prefetch(intptr + 32); |
| 96 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 97 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0)); |
| 98 | _v0 = __msa_fmul_w(_v0, _scale0); |
| 99 | _v1 = __msa_fmul_w(_v1, _scale1); |
| 100 | *((int64_t*)ptr) = float2int8relu(_v0, _v1); |
| 101 | |
| 102 | intptr += 8; |
| 103 | ptr += 8; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | #pragma omp parallel for num_threads(opt.num_threads) |
| 110 | for (int q = 0; q < channels; q++) |
| 111 | { |
| 112 | const int* intptr = bottom_blob.channel(q); |
| 113 | signed char* ptr = top_blob.channel(q); |
| 114 | |
| 115 | v4f32 _scale_in0 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8, 0); |
| 116 | v4f32 _scale_in1 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8 + 4, 0); |
| 117 | v4f32 _scale_out0 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8, 0); |
| 118 | v4f32 _scale_out1 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8 + 4, 0); |
| 119 | v4f32 _bias0 = bias_data_size == 1 ? (v4f32)__msa_fill_w_f32(bias_data[0]) : (v4f32)__msa_ld_w((const float*)bias_data + q * 8, 0); |
| 120 | v4f32 _bias1 = bias_data_size == 1 ? (v4f32)__msa_fill_w_f32(bias_data[0]) : (v4f32)__msa_ld_w((const float*)bias_data + q * 8 + 4, 0); |
| 121 | |
| 122 | v4f32 _scale0 = __msa_fmul_w(_scale_in0, _scale_out0); |
| 123 | v4f32 _scale1 = __msa_fmul_w(_scale_in1, _scale_out1); |
| 124 | _bias0 = __msa_fmul_w(_bias0, _scale_out0); |
| 125 | _bias1 = __msa_fmul_w(_bias1, _scale_out1); |
| 126 | |
| 127 | int i = 0; |
| 128 | for (; i + 3 < size; i += 4) |
| 129 | { |
| 130 | __builtin_prefetch(intptr + 128); |
| 131 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 132 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0)); |
| 133 | v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0)); |
| 134 | v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0)); |
| 135 | v4f32 _v4 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 16, 0)); |
| 136 | v4f32 _v5 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 20, 0)); |
| 137 | v4f32 _v6 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 24, 0)); |
| 138 | v4f32 _v7 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 28, 0)); |
| 139 | _v0 = __msa_fmadd_w(_bias0, _v0, _scale0); |
| 140 | _v1 = __msa_fmadd_w(_bias1, _v1, _scale1); |
| 141 | _v2 = __msa_fmadd_w(_bias0, _v2, _scale0); |
| 142 | _v3 = __msa_fmadd_w(_bias1, _v3, _scale1); |
| 143 | _v4 = __msa_fmadd_w(_bias0, _v4, _scale0); |
| 144 | _v5 = __msa_fmadd_w(_bias1, _v5, _scale1); |
| 145 | _v6 = __msa_fmadd_w(_bias0, _v6, _scale0); |
| 146 | _v7 = __msa_fmadd_w(_bias1, _v7, _scale1); |
| 147 | *((int64_t*)ptr) = float2int8relu(_v0, _v1); |
| 148 | *((int64_t*)(ptr + 8)) = float2int8relu(_v2, _v3); |
| 149 | *((int64_t*)(ptr + 16)) = float2int8relu(_v4, _v5); |
| 150 | *((int64_t*)(ptr + 24)) = float2int8relu(_v6, _v7); |
| 151 | |
| 152 | intptr += 32; |
| 153 | ptr += 32; |
| 154 | } |
| 155 | for (; i + 1 < size; i += 2) |
| 156 | { |
| 157 | __builtin_prefetch(intptr + 64); |
| 158 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 159 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0)); |
| 160 | v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0)); |
| 161 | v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0)); |
| 162 | _v0 = __msa_fmadd_w(_bias0, _v0, _scale0); |
| 163 | _v1 = __msa_fmadd_w(_bias1, _v1, _scale1); |
| 164 | _v2 = __msa_fmadd_w(_bias0, _v2, _scale0); |
| 165 | _v3 = __msa_fmadd_w(_bias1, _v3, _scale1); |
| 166 | *((int64_t*)ptr) = float2int8relu(_v0, _v1); |
| 167 | *((int64_t*)(ptr + 8)) = float2int8relu(_v2, _v3); |
| 168 | |
| 169 | intptr += 16; |
| 170 | ptr += 16; |
| 171 | } |
| 172 | for (; i < size; i++) |
| 173 | { |
| 174 | __builtin_prefetch(intptr + 32); |
| 175 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 176 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0)); |
| 177 | _v0 = __msa_fmadd_w(_bias0, _v0, _scale0); |
| 178 | _v1 = __msa_fmadd_w(_bias1, _v1, _scale1); |
| 179 | *((int64_t*)ptr) = float2int8relu(_v0, _v1); |
| 180 | |
| 181 | intptr += 8; |
| 182 | ptr += 8; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |