|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import tvm |
| 19 | +import numpy as np |
| 20 | +from tvm import relay |
| 21 | +from tvm.contrib import graph_runtime |
| 22 | +import topi.testing |
| 23 | + |
| 24 | +# "unquantize" a quantized tensor |
| 25 | +def recover(data, scale, zp): |
| 26 | + return scale * (np.asarray(data) - zp) |
| 27 | + |
| 28 | + |
| 29 | +def generate_golden_output(x_recovered, y_recovered, scale, zp): |
| 30 | + mul = x_recovered * y_recovered |
| 31 | + output = np.around(mul / scale + zp) |
| 32 | + |
| 33 | + q_min = np.iinfo(np.uint8).min |
| 34 | + q_max = np.iinfo(np.uint8).max |
| 35 | + return np.clip(output, q_min, q_max) |
| 36 | + |
| 37 | + |
| 38 | +def test_tflite_same_io_qnn_params(): |
| 39 | + data_dtype = "uint8" |
| 40 | + |
| 41 | + lhs_scale = rhs_scale = output_scale = 0.00784314 |
| 42 | + lhs_zero_point = rhs_zero_point = output_zero_point = 127 |
| 43 | + |
| 44 | + x = relay.var("x", shape=(1, 4), dtype=data_dtype) |
| 45 | + y = relay.var("y", shape=(1, 4), dtype=data_dtype) |
| 46 | + z = relay.qnn.op.mul(lhs=x, rhs=y, |
| 47 | + lhs_scale=lhs_scale, |
| 48 | + lhs_zero_point=lhs_zero_point, |
| 49 | + rhs_scale=rhs_scale, |
| 50 | + rhs_zero_point=rhs_zero_point, |
| 51 | + output_scale=output_scale, |
| 52 | + output_zero_point=output_zero_point) |
| 53 | + |
| 54 | + func = relay.Function([x, y], z) |
| 55 | + mod = relay.Module.from_expr(func) |
| 56 | + mod = relay.qnn.transform.CanonicalizeOps()(mod) |
| 57 | + func = mod["main"] |
| 58 | + |
| 59 | + x_datas = [ |
| 60 | + np.array((1, 153, 2, 178)).reshape((1, 4)), |
| 61 | + np.array((25, 1, 178, 216)).reshape((1, 4)), |
| 62 | + np.array((25, 153, 1, 165)).reshape((1, 4)), |
| 63 | + ] |
| 64 | + y_datas = [ |
| 65 | + np.array((204, 178, 1, 8)).reshape((1, 4)), |
| 66 | + np.array((204, 178, 191, 1)).reshape((1, 4)), |
| 67 | + np.array((204, 178, 1, 191)).reshape((1, 4)), |
| 68 | + ] |
| 69 | + |
| 70 | + for i in range(0, 3): |
| 71 | + x_data = x_datas[i] |
| 72 | + y_data = y_datas[i] |
| 73 | + |
| 74 | + x_rec = recover(x_data, lhs_scale, lhs_zero_point) |
| 75 | + y_rec = recover(y_data, rhs_scale, rhs_zero_point) |
| 76 | + golden = generate_golden_output(x_rec, y_rec, output_scale, |
| 77 | + output_zero_point) |
| 78 | + |
| 79 | + intrp = relay.create_executor("graph", ctx=tvm.cpu(0), target="llvm") |
| 80 | + op_res = intrp.evaluate(func)(x_data, y_data) |
| 81 | + |
| 82 | + np.testing.assert_equal(op_res.asnumpy(), np.uint8(golden)) |
| 83 | + |
| 84 | + |
| 85 | +def test_tflite_different_io_qnn_params(): |
| 86 | + data_dtype = "uint8" |
| 87 | + |
| 88 | + lhs_scale = 0.0156863 |
| 89 | + lhs_zero_point = 127 |
| 90 | + rhs_scale = 0.0117647 |
| 91 | + rhs_zero_point = 85 |
| 92 | + output_scale = 0.0235294 |
| 93 | + output_zero_point = 128 |
| 94 | + |
| 95 | + x = relay.var("x", shape=(1, 4), dtype=data_dtype) |
| 96 | + y = relay.var("y", shape=(1, 4), dtype=data_dtype) |
| 97 | + z = relay.qnn.op.mul(lhs=x, rhs=y, |
| 98 | + lhs_scale=lhs_scale, |
| 99 | + lhs_zero_point=lhs_zero_point, |
| 100 | + rhs_scale=rhs_scale, |
| 101 | + rhs_zero_point=rhs_zero_point, |
| 102 | + output_scale=output_scale, |
| 103 | + output_zero_point=output_zero_point) |
| 104 | + |
| 105 | + func = relay.Function([x, y], z) |
| 106 | + mod = relay.Module.from_expr(func) |
| 107 | + mod = relay.qnn.transform.CanonicalizeOps()(mod) |
| 108 | + func = mod["main"] |
| 109 | + |
| 110 | + x_datas = [ |
| 111 | + np.array((76, 140, 153, 172)).reshape((1, 4)), |
| 112 | + np.array((133, 140, 146, 153)).reshape((1, 4)), |
| 113 | + np.array((76, 140, 172, 146)).reshape((1, 4)), |
| 114 | + ] |
| 115 | + y_datas = [ |
| 116 | + np.array((136, 119, 128, 17)).reshape((1, 4)), |
| 117 | + np.array((136, 119, 111, 94)).reshape((1, 4)), |
| 118 | + np.array((136, 119, 17, 128)).reshape((1, 4)), |
| 119 | + ] |
| 120 | + |
| 121 | + for i in range(0, 3): |
| 122 | + x_data = x_datas[i] |
| 123 | + y_data = y_datas[i] |
| 124 | + |
| 125 | + x_rec = recover(x_data, lhs_scale, lhs_zero_point) |
| 126 | + y_rec = recover(y_data, rhs_scale, rhs_zero_point) |
| 127 | + golden = generate_golden_output(x_rec, y_rec, output_scale, |
| 128 | + output_zero_point) |
| 129 | + |
| 130 | + intrp = relay.create_executor("graph", ctx=tvm.cpu(0), target="llvm") |
| 131 | + op_res = intrp.evaluate(func)(x_data, y_data) |
| 132 | + np.testing.assert_equal(op_res.asnumpy(), np.uint8(golden)) |
| 133 | + |
| 134 | + |
| 135 | +def test_saturation(): |
| 136 | + # Same params |
| 137 | + data_dtype = "uint8" |
| 138 | + lhs_scale = rhs_scale = output_scale = 0.125 |
| 139 | + lhs_zero_point = rhs_zero_point = output_zero_point = 0 |
| 140 | + |
| 141 | + x = relay.var("x", shape=(1, 4), dtype=data_dtype) |
| 142 | + y = relay.var("y", shape=(1, 4), dtype=data_dtype) |
| 143 | + z = relay.qnn.op.mul(lhs=x, rhs=y, |
| 144 | + lhs_scale=lhs_scale, |
| 145 | + lhs_zero_point=lhs_zero_point, |
| 146 | + rhs_scale=rhs_scale, |
| 147 | + rhs_zero_point=rhs_zero_point, |
| 148 | + output_scale=output_scale, |
| 149 | + output_zero_point=output_zero_point) |
| 150 | + |
| 151 | + func = relay.Function([x, y], z) |
| 152 | + mod = relay.Module.from_expr(func) |
| 153 | + mod = relay.qnn.transform.CanonicalizeOps()(mod) |
| 154 | + func = mod["main"] |
| 155 | + |
| 156 | + x_data = np.array((255, 1, 1, 0)).reshape((1, 4)) |
| 157 | + y_data = np.array((255, 255, 128, 0)).reshape((1, 4)) |
| 158 | + |
| 159 | + x_rec = recover(x_data, lhs_scale, lhs_zero_point) |
| 160 | + y_rec = recover(y_data, rhs_scale, rhs_zero_point) |
| 161 | + |
| 162 | + golden = generate_golden_output(x_rec, y_rec, output_scale, |
| 163 | + output_zero_point) |
| 164 | + |
| 165 | + intrp = relay.create_executor("graph", ctx=tvm.cpu(0), target="llvm") |
| 166 | + op_res = intrp.evaluate(func)(x_data, y_data) |
| 167 | + np.testing.assert_equal(op_res.asnumpy(), np.uint8(golden)) |
| 168 | + |
| 169 | + # Same params, different scale |
| 170 | + |
| 171 | + lhs_scale = rhs_scale = 0.125 |
| 172 | + output_scale = 0.25 |
| 173 | + |
| 174 | + z = relay.qnn.op.mul(lhs=x, rhs=y, |
| 175 | + lhs_scale=lhs_scale, |
| 176 | + lhs_zero_point=lhs_zero_point, |
| 177 | + rhs_scale=rhs_scale, |
| 178 | + rhs_zero_point=rhs_zero_point, |
| 179 | + output_scale=output_scale, |
| 180 | + output_zero_point=output_zero_point) |
| 181 | + |
| 182 | + func = relay.Function([x, y], z) |
| 183 | + mod = relay.Module.from_expr(func) |
| 184 | + mod = relay.qnn.transform.CanonicalizeOps()(mod) |
| 185 | + func = mod["main"] |
| 186 | + |
| 187 | + x_data = np.array((255, 1, 1, 0)).reshape((1, 4)) |
| 188 | + y_data = np.array((255, 255, 127, 0)).reshape((1, 4)) |
| 189 | + |
| 190 | + x_rec = recover(x_data, lhs_scale, lhs_zero_point) |
| 191 | + y_rec = recover(y_data, rhs_scale, rhs_zero_point) |
| 192 | + |
| 193 | + golden = generate_golden_output(x_rec, y_rec, output_scale, |
| 194 | + output_zero_point) |
| 195 | + |
| 196 | + intrp = relay.create_executor("graph", ctx=tvm.cpu(0), target="llvm") |
| 197 | + op_res = intrp.evaluate(func)(x_data, y_data) |
| 198 | + np.testing.assert_equal(op_res.asnumpy(), np.uint8(golden)) |
| 199 | + |
| 200 | + # All params different |
| 201 | + |
| 202 | + lhs_scale = 0.5 |
| 203 | + rhs_scale = 0.25 |
| 204 | + output_scale = 0.125 |
| 205 | + |
| 206 | + z = relay.qnn.op.mul(lhs=x, rhs=y, |
| 207 | + lhs_scale=lhs_scale, |
| 208 | + lhs_zero_point=lhs_zero_point, |
| 209 | + rhs_scale=rhs_scale, |
| 210 | + rhs_zero_point=rhs_zero_point, |
| 211 | + output_scale=output_scale, |
| 212 | + output_zero_point=output_zero_point) |
| 213 | + |
| 214 | + func = relay.Function([x, y], z) |
| 215 | + mod = relay.Module.from_expr(func) |
| 216 | + mod = relay.qnn.transform.CanonicalizeOps()(mod) |
| 217 | + func = mod["main"] |
| 218 | + |
| 219 | + x_data = np.array((255, 0, 1, 0)).reshape((1, 4)) |
| 220 | + y_data = np.array((0, 128, 64, 0)).reshape((1, 4)) |
| 221 | + |
| 222 | + x_rec = recover(x_data, lhs_scale, lhs_zero_point) |
| 223 | + y_rec = recover(y_data, rhs_scale, rhs_zero_point) |
| 224 | + |
| 225 | + golden = generate_golden_output(x_rec, y_rec, output_scale, |
| 226 | + output_zero_point) |
| 227 | + |
| 228 | + intrp = relay.create_executor("graph", ctx=tvm.cpu(0), target="llvm") |
| 229 | + op_res = intrp.evaluate(func)(x_data, y_data) |
| 230 | + np.testing.assert_equal(op_res.asnumpy(), np.uint8(golden)) |
| 231 | + |
| 232 | + |
| 233 | +if __name__ == "__main__": |
| 234 | + test_tflite_same_io_qnn_params() |
| 235 | + test_tflite_different_io_qnn_params() |
| 236 | + test_saturation() |
0 commit comments