1# Copyright 2020 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15 16import numpy as np 17import pytest 18 19import mindspore.context as context 20from mindspore.common.tensor import Tensor 21from mindspore import nn 22from mindspore.ops.operations import _quant_ops as Q 23 24context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 25 26 27class Net(nn.Cell): 28 def __init__(self, num_bits=8, symmetric=False, narrow_range=False, channel_axis=1): 29 super(Net, self).__init__() 30 self.op = Q.FakeQuantPerChannel(num_bits=num_bits, 31 symmetric=symmetric, 32 narrow_range=narrow_range, 33 channel_axis=channel_axis) 34 35 def construct(self, x, minq, maxq): 36 return self.op(x, minq, maxq) 37 38 39@pytest.mark.level0 40@pytest.mark.platform_x86_gpu_training 41@pytest.mark.env_onecard 42def test_fake_quant_perchannel1(): 43 # WithVarsPerChannel_ZeroMinAndMax 44 x = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32) 45 min_val = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32) 46 max_val = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32) 47 expect = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32) 48 49 net = Net(num_bits=8, narrow_range=False, channel_axis=0) 50 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 51 52 error = np.ones(shape=expect.shape) * 1.0e-5 53 diff = output.asnumpy() - expect 54 print("output: ", output) 55 print("expect: ", expect) 56 assert np.all(np.abs(diff) < error) 57 58 59@pytest.mark.level1 60@pytest.mark.platform_x86_gpu_training 61@pytest.mark.env_onecard 62def test_fake_quant_perchannel2(): 63 # WithVarsPerChannelDim1NudgedDown_RegularRange 64 # scale 1/4, zp 0.4, nudge 0. nudged ranges [0.0, 63.75] 65 x = np.array([-0.1, 0.0, 63.75, 63.8]).astype(np.float32) 66 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).astype(np.float32) 67 max_val = np.array([63.65, 63.65, 63.65, 63.65]).astype(np.float32) 68 expect = np.array([0.0, 0.0, 63.75, 63.75]).astype(np.float32) 69 70 net = Net(num_bits=8, narrow_range=False, channel_axis=0) 71 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 72 73 error = np.ones(shape=expect.shape) * 1.0e-5 74 diff = output.asnumpy() - expect 75 print("output: ", output) 76 print("expect: ", expect) 77 assert np.all(np.abs(diff) < error) 78 79 80@pytest.mark.level0 81@pytest.mark.platform_x86_gpu_training 82@pytest.mark.env_onecard 83def test_fake_quant_perchannel3(): 84 # WithVarsPerChannelDim1NudgedDown_NarrowRange 85 # scale 1/4, zp 1.4, nudge 1. nudged ranges[0.0, 63.5] 86 x = np.array([-0.1, 0.0, 63.5, 63.6]).astype(np.float32) 87 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).astype(np.float32) 88 max_val = np.array([63.4, 63.4, 63.4, 63.4]).astype(np.float32) 89 expect = np.array([0.0, 0.0, 63.5, 63.5]).astype(np.float32) 90 91 net = Net(num_bits=8, narrow_range=True, channel_axis=0) 92 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 93 94 error = np.ones(shape=expect.shape) * 1.0e-5 95 diff = output.asnumpy() - expect 96 print("output: ", output) 97 print("expect: ", expect) 98 assert np.all(np.abs(diff) < error) 99 100 101@pytest.mark.level1 102@pytest.mark.platform_x86_gpu_training 103@pytest.mark.env_onecard 104def test_fake_quant_perchannel4(): 105 # WithVarsPerChannelDim1NudgedUp_RegularRange 106 # [-0.125, 63.625] 107 # scale 1/4, zp: 0.5, nudge 0. nudged range [-0.25, 63.5] 108 x = np.array([-0.26, -0.25, -0.24, 63.6]).astype(np.float32) 109 expect = np.array([-0.25, -0.25, -0.25, 63.5]).astype(np.float32) 110 min_val = np.array([-0.125, -0.125, -0.125, -0.125]).astype(np.float32) 111 max_val = np.array([63.625, 63.625, 63.625, 63.625]).astype(np.float32) 112 113 net = Net(num_bits=8, narrow_range=False, channel_axis=0) 114 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 115 116 error = np.ones(shape=expect.shape) * 1.0e-5 117 diff = output.asnumpy() - expect 118 print("output: ", output) 119 print("expect: ", expect) 120 assert np.all(np.abs(diff) < error) 121 122 123@pytest.mark.level1 124@pytest.mark.platform_x86_gpu_training 125@pytest.mark.env_onecard 126def test_fake_quant_perchannel5(): 127 # WithVarsPerChannelDim1NudgedUp_NarrowRange 128 # scale 1/4, zp: 1.5, nudge 2. nudged range [-0.25, 63.25] 129 x = np.array([-0.26, -0.25, -0.24, 63.3]).astype(np.float32) 130 expect = np.array([-0.25, -0.25, -0.25, 63.25]).astype(np.float32) 131 min_val = np.array([-0.125, -0.125, -0.125, -0.125]).astype(np.float32) 132 max_val = np.array([63.375, 63.375, 63.375, 63.375]).astype(np.float32) 133 134 net = Net(num_bits=8, narrow_range=True, channel_axis=0) 135 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 136 137 error = np.ones(shape=expect.shape) * 1.0e-5 138 diff = output.asnumpy() - expect 139 print("output: ", output) 140 print("expect: ", expect) 141 assert np.all(np.abs(diff) < error) 142 143 144@pytest.mark.level1 145@pytest.mark.platform_x86_gpu_training 146@pytest.mark.env_onecard 147def test_fake_quant_perchannel6(): 148 # WithVarsPerChannelDim2NudgedDown_RegularRange 149 # scale 1/4, zp: 0.4, nudge 0. nudged range [-0.25, 63.75] 150 x = np.array([-0.1, 0.0, 0.1, 0.25, 63.75, 63.80] 151 ).reshape(2, 3).astype(np.float32) 152 expect = np.array([-0.0, 0.0, 0.0, 0.25, 63.75, 63.75]).astype(np.float32) 153 min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32) 154 max_val = np.array([63.65, 63.65, 63.65]).reshape(3).astype(np.float32) 155 156 net = Net(num_bits=8, narrow_range=False, channel_axis=1) 157 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 158 159 error = np.ones(shape=expect.shape) * 1.0e-5 160 diff = output.asnumpy().flatten() - expect 161 print("output: ", output) 162 print("expect: ", expect) 163 assert np.all(np.abs(diff) < error) 164 165 166@pytest.mark.level1 167@pytest.mark.platform_x86_gpu_training 168@pytest.mark.env_onecard 169def test_fake_quant_perchannel7(): 170 # WithVarsPerChannelDim2NudgedDown_NarrowRange 171 # scale 1/4, zp: 1.4, nudge 1. nudged range [-0.25, 63.5] 172 x = np.array([-0.1, 0.0, 0.1, 0.25, 63.5, 63.6] 173 ).reshape(2, 3).astype(np.float32) 174 expect = np.array([0.0, 0.0, 0.0, 0.25, 63.5, 63.5]).astype(np.float32) 175 min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32) 176 max_val = np.array([63.4, 63.4, 63.4]).reshape(3).astype(np.float32) 177 178 net = Net(num_bits=8, narrow_range=True, channel_axis=1) 179 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 180 181 error = np.ones(shape=expect.shape) * 1.0e-5 182 diff = output.asnumpy().flatten() - expect 183 print("output: ", output) 184 print("expect: ", expect) 185 assert np.all(np.abs(diff) < error) 186 187 188@pytest.mark.level1 189@pytest.mark.platform_x86_gpu_training 190@pytest.mark.env_onecard 191def test_fake_quant_perchannel8(): 192 # WithVarsPerChannelDim2NudgedUp_RegularRange 193 # scale 1/4, zp: 0.5, nudge 1. nudged range [-0.25, 63.5] 194 x = np.array([-0.26, -0.25, -0.24, 0.0, 63.5, 63.6] 195 ).reshape(2, 3).astype(np.float32) 196 expect = np.array([-0.25, -0.25, -0.25, 0.0, 63.5, 63.5] 197 ).astype(np.float32) 198 min_val = np.array([-0.125, -0.125, -0.125]).reshape(3).astype(np.float32) 199 max_val = np.array([63.625, 63.625, 63.625]).reshape(3).astype(np.float32) 200 201 net = Net(num_bits=8, narrow_range=False, channel_axis=1) 202 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 203 204 error = np.ones(shape=expect.shape) * 1.0e-5 205 diff = output.asnumpy().flatten() - expect 206 print("output: ", output) 207 print("expect: ", expect) 208 assert np.all(np.abs(diff) < error) 209 210 211@pytest.mark.level1 212@pytest.mark.platform_x86_gpu_training 213@pytest.mark.env_onecard 214def test_fake_quant_perchannel9(): 215 # WithVarsPerChannelDim2NudgedUp_NarrowRange 216 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 217 x = np.array([-0.26, -0.25, -0.24, 0.0, 63.25, 63.3] 218 ).reshape(2, 3).astype(np.float32) 219 expect = np.array( 220 [-0.25, -0.25, -0.25, 0.0, 63.25, 63.25]).astype(np.float32) 221 min_val = np.array([-0.125, -0.125, -0.125]).reshape(3).astype(np.float32) 222 max_val = np.array([63.375, 63.375, 63.375]).reshape(3).astype(np.float32) 223 224 net = Net(num_bits=8, narrow_range=True, channel_axis=1) 225 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 226 227 error = np.ones(shape=expect.shape) * 1.0e-5 228 diff = output.asnumpy().flatten() - expect 229 print("output: ", output) 230 print("expect: ", expect) 231 assert np.all(np.abs(diff) < error) 232 233 234@pytest.mark.level1 235@pytest.mark.platform_x86_gpu_training 236@pytest.mark.env_onecard 237def test_fake_quant_perchannel10(): 238 # WithVarsPerChannelDim4NudgedDown_RegularRange 239 # scale 1/4, zp: 0.4, nudge 0. nudged range [-0.25, 63.25] 240 x = np.array([-0.1, 0.0, 0.1, 0.25, 0.5, 0.75, 241 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 242 63.0, 63.25, 63.5, 63.7, 63.75, 63.8, 243 63.9, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 244 expect = np.array([0.0, 0.0, 0.0, 0.25, 0.5, 0.75, 245 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 246 63.0, 63.25, 63.5, 63.75, 63.75, 63.75, 247 63.75, 63.75, 63.75, 63.75, 63.75, 63.75]).astype(np.float32) 248 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32) 249 max_val = np.array([63.65, 63.65, 63.65, 63.65] 250 ).reshape(4).astype(np.float32) 251 252 net = Net(num_bits=8, narrow_range=False, channel_axis=1) 253 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 254 255 error = np.ones(shape=expect.shape) * 1.0e-5 256 diff = output.asnumpy().flatten() - expect 257 258 print("output: ", output) 259 print("expect: ", expect) 260 assert np.all(np.abs(diff) < error) 261 262 263@pytest.mark.level1 264@pytest.mark.platform_x86_gpu_training 265@pytest.mark.env_onecard 266def test_fake_quant_perchannel11(): 267 # WithVarsPerChannelDim4NudgedDown_NarrowRange 268 # scale 1/4, zp: 1.4, nudge 1. nudged range [0.0, 63.25] 269 x = np.array([-0.1, 0.0, 0.1, 0.25, 0.5, 0.75, 270 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 271 63.0, 63.25, 63.3, 63.4, 63.5, 63.6, 272 63.7, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 273 expect = np.array([0.0, 0.0, 0.0, 0.25, 0.5, 0.75, 274 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 275 63.0, 63.25, 63.25, 63.5, 63.5, 63.5, 276 63.5, 63.5, 63.5, 63.5, 63.5, 63.5]).astype(np.float32) 277 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32) 278 max_val = np.array([63.4, 63.4, 63.4, 63.4]).reshape(4).astype(np.float32) 279 280 net = Net(num_bits=8, narrow_range=True, channel_axis=1) 281 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 282 283 error = np.ones(shape=expect.shape) * 1.0e-5 284 diff = output.asnumpy().flatten() - expect 285 print("output: ", output) 286 print("expect: ", expect) 287 assert np.all(np.abs(diff) < error) 288 289 290@pytest.mark.level1 291@pytest.mark.platform_x86_gpu_training 292@pytest.mark.env_onecard 293def test_fake_quant_perchannel12(): 294 # WithVarsPerChannelDim4NudgedUp_RegularRange 295 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 296 x = np.array([-0.3, -0.25, -0.2, 0.0, 0.25, 0.5, 297 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 298 63.0, 63.25, 63.4, 63.5, 63.6, 63.7, 299 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 300 expect = np.array([-0.25, -0.25, -0.25, 0.0, 0.25, 0.5, 301 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 302 63.0, 63.25, 63.5, 63.5, 63.5, 63.5, 303 63.5, 63.5, 63.5, 63.5, 63.5, 63.5]).astype(np.float32) 304 min_val = np.array([-0.125, -0.125, -0.125, -0.125] 305 ).reshape(4).astype(np.float32) 306 max_val = np.array([63.625, 63.625, 63.625, 63.625] 307 ).reshape(4).astype(np.float32) 308 309 net = Net(num_bits=8, narrow_range=False, channel_axis=1) 310 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 311 312 error = np.ones(shape=expect.shape) * 1.0e-5 313 diff = output.asnumpy().flatten() - expect 314 print("output: ", output) 315 print("expect: ", expect) 316 assert np.all(np.abs(diff) < error) 317 318 319@pytest.mark.level1 320@pytest.mark.platform_x86_gpu_training 321@pytest.mark.env_onecard 322def test_fake_quant_perchannel13(): 323 # WithVarsPerChannelDim4NudgedUp_NarrowRange 324 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 325 x = np.array([-0.3, -0.25, -0.2, 0.0, 0.25, 0.5, 326 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 327 63.0, 63.2, 63.25, 63.3, 63.4, 63.5, 328 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 329 expect = np.array([-0.25, -0.25, -0.25, 0.0, 0.25, 0.5, 330 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 331 63.0, 63.25, 63.25, 63.25, 63.25, 63.25, 332 63.25, 63.25, 63.25, 63.25, 63.25, 63.25]).astype(np.float32) 333 min_val = np.array([-0.125, -0.125, -0.125, -0.125] 334 ).reshape(4).astype(np.float32) 335 max_val = np.array([63.375, 63.375, 63.375, 63.375] 336 ).reshape(4).astype(np.float32) 337 338 net = Net(num_bits=8, narrow_range=True, channel_axis=1) 339 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 340 341 error = np.ones(shape=expect.shape) * 1.0e-5 342 diff = output.asnumpy().flatten() - expect 343 print("output: ", output) 344 print("expect: ", expect) 345 assert np.all(np.abs(diff) < error) 346 347 348@pytest.mark.level1 349@pytest.mark.platform_x86_gpu_training 350@pytest.mark.env_onecard 351def test_fake_quant_perchannel14(): 352 # WithVarsPerChannelDim1NudgedDown_4Bits_RegularRange 353 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 354 x = np.array([-0.1, 0.0, 7.5, 7.6]).reshape(4).astype(np.float32) 355 expect = np.array([0.0, 0.0, 7.5, 7.5]).astype(np.float32) 356 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32) 357 max_val = np.array([7.4, 7.4, 7.4, 7.4]).reshape(4).astype(np.float32) 358 359 net = Net(num_bits=4, narrow_range=False, channel_axis=0) 360 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 361 362 error = np.ones(shape=expect.shape) * 1.0e-5 363 diff = output.asnumpy().flatten() - expect 364 print("output: ", output) 365 print("expect: ", expect) 366 assert np.all(np.abs(diff) < error) 367 368 369@pytest.mark.level1 370@pytest.mark.platform_x86_gpu_training 371@pytest.mark.env_onecard 372def test_fake_quant_perchannel15(): 373 # WithVarsPerChannelDim1NudgedDown_4Bits_NarrowRange 374 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 375 x = np.array([-0.1, 0.0, 7.0, 7.1]).reshape(4).astype(np.float32) 376 expect = np.array([0.0, 0.0, 7.0, 7.0]).astype(np.float32) 377 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32) 378 max_val = np.array([6.9, 6.9, 6.9, 6.9]).reshape(4).astype(np.float32) 379 380 net = Net(num_bits=4, narrow_range=True, channel_axis=0) 381 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 382 383 error = np.ones(shape=expect.shape) * 1.0e-5 384 diff = output.asnumpy() - expect 385 print("output: ", output) 386 print("expect: ", expect) 387 assert np.all(np.abs(diff) < error) 388 389 390@pytest.mark.level0 391@pytest.mark.platform_x86_gpu_training 392@pytest.mark.env_onecard 393def test_fake_quant_perchannel16(): 394 # WithVarsPerChannelDim1NudgedUp_4Bits_RegularRange 395 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 396 x = np.array([-0.6, -0.5, 7.0, 7.1]).reshape(4).astype(np.float32) 397 expect = np.array([-0.5, -0.5, 7.0, 7.0]).astype(np.float32) 398 min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32) 399 max_val = np.array([7.1, 7.1, 7.1, 7.1]).reshape(4).astype(np.float32) 400 401 net = Net(num_bits=4, narrow_range=False, channel_axis=0) 402 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 403 404 error = np.ones(shape=expect.shape) * 1.0e-5 405 diff = output.asnumpy() - expect 406 print("output: ", output) 407 print("expect: ", expect) 408 assert np.all(np.abs(diff) < error) 409 410 411@pytest.mark.level0 412@pytest.mark.platform_x86_gpu_training 413@pytest.mark.env_onecard 414def test_fake_quant_perchannel17(): 415 # WithVarsPerChannelDim1NudgedUp_4Bits_NarrowRange 416 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 417 x = np.array([-0.6, -0.5, 6.5, 6.6]).reshape(4).astype(np.float32) 418 expect = np.array([-0.5, -0.5, 6.5, 6.5]).astype(np.float32) 419 min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32) 420 max_val = np.array([6.6, 6.6, 6.6, 6.6]).reshape(4).astype(np.float32) 421 422 net = Net(num_bits=4, narrow_range=True, channel_axis=0) 423 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 424 425 error = np.ones(shape=expect.shape) * 1.0e-5 426 diff = output.asnumpy() - expect 427 print("output: ", output) 428 print("expect: ", expect) 429 assert np.all(np.abs(diff) < error) 430 431 432@pytest.mark.level1 433@pytest.mark.platform_x86_gpu_training 434@pytest.mark.env_onecard 435def test_fake_quant_perchannel18(): 436 # WithVarsPerChannelDim2NudgedDown_4Bits_RegularRange 437 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 438 x = np.array([-0.1, 0.0, 0.1, 0.5, 7.5, 7.6] 439 ).reshape(2, 3).astype(np.float32) 440 expect = np.array([0.0, 0.0, 0.0, 0.5, 7.5, 7.5]).astype(np.float32) 441 min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32) 442 max_val = np.array([7.4, 7.4, 7.4]).reshape(3).astype(np.float32) 443 444 net = Net(num_bits=4, narrow_range=False, channel_axis=1) 445 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 446 447 error = np.ones(shape=expect.shape) * 1.0e-5 448 diff = output.asnumpy().flatten() - expect 449 print("output: ", output) 450 print("expect: ", expect) 451 assert np.all(np.abs(diff) < error) 452 453 454@pytest.mark.level1 455@pytest.mark.platform_x86_gpu_training 456@pytest.mark.env_onecard 457def test_fake_quant_perchannel19(): 458 # WithVarsPerChannelDim2NudgedDown_4Bits_NarrowRange 459 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 460 x = np.array([-0.1, 0.0, 0.1, 0.5, 7.0, 7.1] 461 ).reshape(2, 3).astype(np.float32) 462 expect = np.array([0.0, 0.0, 0.0, 0.5, 7.0, 7.0]).astype(np.float32) 463 min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32) 464 max_val = np.array([6.9, 6.9, 6.9]).reshape(3).astype(np.float32) 465 466 net = Net(num_bits=4, narrow_range=True, channel_axis=1) 467 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 468 469 error = np.ones(shape=expect.shape) * 1.0e-5 470 diff = output.asnumpy().flatten() - expect 471 print("output: ", output) 472 print("expect: ", expect) 473 assert np.all(np.abs(diff) < error) 474 475 476@pytest.mark.level1 477@pytest.mark.platform_x86_gpu_training 478@pytest.mark.env_onecard 479def test_fake_quant_perchannel20(): 480 # WithVarsPerChannelDim2NudgedUp_4Bits_RegularRange 481 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 482 x = np.array([-0.51, -0.5, -0.24, 0.0, 7.0, 7.1] 483 ).reshape(2, 3).astype(np.float32) 484 expect = np.array([-0.5, -0.5, 0.0, 0.0, 7.0, 7.0]).astype(np.float32) 485 min_val = np.array([-0.4, -0.4, -0.4]).reshape(3).astype(np.float32) 486 max_val = np.array([7.1, 7.1, 7.1]).reshape(3).astype(np.float32) 487 488 net = Net(num_bits=4, narrow_range=False, channel_axis=1) 489 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 490 491 error = np.ones(shape=expect.shape) * 1.0e-5 492 diff = output.asnumpy().flatten() - expect 493 print("output: ", output) 494 print("expect: ", expect) 495 assert np.all(np.abs(diff) < error) 496 497 498@pytest.mark.level1 499@pytest.mark.platform_x86_gpu_training 500@pytest.mark.env_onecard 501def test_fake_quant_perchannel21(): 502 # WithVarsPerChannelDim2NudgedUp_4Bits_NarrowRange 503 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 504 x = np.array([-0.6, -0.5, -0.24, 0.0, 6.5, 6.6] 505 ).reshape(2, 3).astype(np.float32) 506 expect = np.array([-0.5, -0.5, 0.0, 0.0, 6.5, 6.5]).astype(np.float32) 507 min_val = np.array([-0.4, -0.4, -0.4]).reshape(3).astype(np.float32) 508 max_val = np.array([6.6, 6.6, 6.6]).reshape(3).astype(np.float32) 509 510 net = Net(num_bits=4, narrow_range=True, channel_axis=1) 511 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 512 513 error = np.ones(shape=expect.shape) * 1.0e-5 514 diff = output.asnumpy().flatten() - expect 515 print("output: ", output) 516 print("expect: ", expect) 517 assert np.all(np.abs(diff) < error) 518 519 520@pytest.mark.level1 521@pytest.mark.platform_x86_gpu_training 522@pytest.mark.env_onecard 523def test_fake_quant_perchannel22(): 524 # WithVarsPerChannelDim4NudgedDown_4Bits_RegularRange 525 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 526 x = np.array([-0.1, 0.0, 0.1, 0.5, 1.0, 1.5, 527 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 528 6.0, 6.5, 7.0, 7.4, 7.5, 7.7, 529 7.8, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 530 expect = np.array([0.0, 0.0, 0.0, 0.5, 1.0, 1.5, 531 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 532 6.0, 6.5, 7.0, 7.5, 7.5, 7.5, 533 7.5, 7.5, 7.5, 7.5, 7.5, 7.5]).astype(np.float32) 534 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32) 535 max_val = np.array([7.4, 7.4, 7.4, 7.4]).reshape(4).astype(np.float32) 536 537 net = Net(num_bits=4, narrow_range=False, channel_axis=1) 538 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 539 540 error = np.ones(shape=expect.shape) * 1.0e-5 541 diff = output.asnumpy().flatten() - expect 542 print("output: ", output) 543 print("expect: ", expect) 544 assert np.all(np.abs(diff) < error) 545 546 547@pytest.mark.level1 548@pytest.mark.platform_x86_gpu_training 549@pytest.mark.env_onecard 550def test_fake_quant_perchannel23(): 551 # WithVarsPerChannelDim4NudgedDown_4Bits_NarrowRange 552 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 553 x = np.array([-0.1, 0.0, 0.1, 0.5, 1.0, 1.5, 554 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 555 6.0, 6.5, 6.8, 6.9, 7.0, 7.1, 556 7.2, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 557 expect = np.array([0.0, 0.0, 0.0, 0.5, 1.0, 1.5, 558 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 559 6.0, 6.5, 7.0, 7.0, 7.0, 7.0, 560 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]).astype(np.float32) 561 min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32) 562 max_val = np.array([6.9, 6.9, 6.9, 6.9]).reshape(4).astype(np.float32) 563 564 net = Net(num_bits=4, narrow_range=True, channel_axis=1) 565 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 566 567 error = np.ones(shape=expect.shape) * 1.0e-5 568 diff = output.asnumpy().flatten() - expect 569 print("output: ", output) 570 print("expect: ", expect) 571 assert np.all(np.abs(diff) < error) 572 573 574@pytest.mark.level0 575@pytest.mark.platform_x86_gpu_training 576@pytest.mark.env_onecard 577def test_fake_quant_perchannel24(): 578 # WithVarsPerChannelDim4NudgedUp_4Bits_RegularRange 579 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 580 x = np.array([-0.6, -0.5, -0.4, 0.0, 0.5, 1.0, 581 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 582 6.0, 6.5, 6.9, 7.0, 7.1, 7.7, 583 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 584 expect = np.array([-0.5, -0.5, -0.5, 0.0, 0.5, 1.0, 585 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 586 6.0, 6.5, 7.0, 7.0, 7.0, 7.0, 587 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]).astype(np.float32) 588 min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32) 589 max_val = np.array([7.1, 7.1, 7.1, 7.1]).reshape(4).astype(np.float32) 590 591 net = Net(num_bits=4, narrow_range=False, channel_axis=1) 592 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 593 594 error = np.ones(shape=expect.shape) * 1.0e-5 595 diff = output.asnumpy().flatten() - expect 596 print("output: ", output) 597 print("expect: ", expect) 598 assert np.all(np.abs(diff) < error) 599 600 601@pytest.mark.level0 602@pytest.mark.platform_x86_gpu_training 603@pytest.mark.env_onecard 604def test_fake_quant_perchannel25(): 605 # WithVarsPerChannelDim4NudgedUp_4Bits_NarrowRange 606 # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25] 607 x = np.array([-0.6, -0.5, -0.4, 0.0, 0.5, 1.0, 608 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 609 5.5, 6.0, 6.4, 6.5, 6.6, 6.7, 610 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape((1, 4, 2, 3)).astype(np.float32) 611 expect = np.array([-0.5, -0.5, -0.5, 0.0, 0.5, 1.0, 612 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 613 5.5, 6.0, 6.5, 6.5, 6.5, 6.5, 614 6.5, 6.5, 6.5, 6.5, 6.5, 6.5]).astype(np.float32) 615 min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32) 616 max_val = np.array([6.6, 6.6, 6.6, 6.6]).reshape(4).astype(np.float32) 617 618 net = Net(num_bits=4, narrow_range=True, channel_axis=1) 619 output = net(Tensor(x), Tensor(min_val), Tensor(max_val)) 620 621 error = np.ones(shape=expect.shape) * 1.0e-5 622 diff = output.asnumpy().flatten() - expect 623 print("output: ", output) 624 print("expect: ", expect) 625 assert np.all(np.abs(diff) < error) 626