|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- # Copyright 2020 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ============================================================================
-
- import numpy as np
- import pytest
-
- import mindspore.context as context
- from mindspore.common.tensor import Tensor
- from mindspore import nn
- from mindspore.ops.operations import _quant_ops as Q
-
- context.set_context(device_target='GPU', device_id=0)
-
-
- class Net(nn.Cell):
- def __init__(self, num_bits=8, symmetric=False, narrow_range=False, channel_axis=1):
- super(Net, self).__init__()
- self.op = Q.FakeQuantPerChannel(num_bits=num_bits,
- symmetric=symmetric,
- narrow_range=narrow_range,
- channel_axis=channel_axis)
-
- def construct(self, x, minq, maxq):
- return self.op(x, minq, maxq)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel1():
- # WithVarsPerChannel_ZeroMinAndMax
- x = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
- min_val = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
- max_val = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel2():
- # WithVarsPerChannelDim1NudgedDown_RegularRange
- # scale 1/4, zp 0.4, nudge 0. nudged ranges [0.0, 63.75]
- x = np.array([-0.1, 0.0, 63.75, 63.8]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).astype(np.float32)
- max_val = np.array([63.65, 63.65, 63.65, 63.65]).astype(np.float32)
- expect = np.array([0.0, 0.0, 63.75, 63.75]).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel3():
- # WithVarsPerChannelDim1NudgedDown_NarrowRange
- # scale 1/4, zp 1.4, nudge 1. nudged ranges[0.0, 63.5]
- x = np.array([-0.1, 0.0, 63.5, 63.6]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).astype(np.float32)
- max_val = np.array([63.4, 63.4, 63.4, 63.4]).astype(np.float32)
- expect = np.array([0.0, 0.0, 63.5, 63.5]).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=True, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel4():
- # WithVarsPerChannelDim1NudgedUp_RegularRange
- # [-0.125, 63.625]
- # scale 1/4, zp: 0.5, nudge 0. nudged range [-0.25, 63.5]
- x = np.array([-0.26, -0.25, -0.24, 63.6]).astype(np.float32)
- expect = np.array([-0.25, -0.25, -0.25, 63.5]).astype(np.float32)
- min_val = np.array([-0.125, -0.125, -0.125, -0.125]).astype(np.float32)
- max_val = np.array([63.625, 63.625, 63.625, 63.625]).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel5():
- # WithVarsPerChannelDim1NudgedUp_NarrowRange
- # scale 1/4, zp: 1.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.26, -0.25, -0.24, 63.3]).astype(np.float32)
- expect = np.array([-0.25, -0.25, -0.25, 63.25]).astype(np.float32)
- min_val = np.array([-0.125, -0.125, -0.125, -0.125]).astype(np.float32)
- max_val = np.array([63.375, 63.375, 63.375, 63.375]).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=True, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel6():
- # WithVarsPerChannelDim2NudgedDown_RegularRange
- # scale 1/4, zp: 0.4, nudge 0. nudged range [-0.25, 63.75]
- x = np.array([-0.1, 0.0, 0.1, 0.25, 63.75, 63.80]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([-0.0, 0.0, 0.0, 0.25, 63.75, 63.75]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
- max_val = np.array([63.65, 63.65, 63.65]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel7():
- # WithVarsPerChannelDim2NudgedDown_NarrowRange
- # scale 1/4, zp: 1.4, nudge 1. nudged range [-0.25, 63.5]
- x = np.array([-0.1, 0.0, 0.1, 0.25, 63.5, 63.6]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.25, 63.5, 63.5]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
- max_val = np.array([63.4, 63.4, 63.4]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel8():
- # WithVarsPerChannelDim2NudgedUp_RegularRange
- # scale 1/4, zp: 0.5, nudge 1. nudged range [-0.25, 63.5]
- x = np.array([-0.26, -0.25, -0.24, 0.0, 63.5, 63.6]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([-0.25, -0.25, -0.25, 0.0, 63.5, 63.5]
- ).astype(np.float32)
- min_val = np.array([-0.125, -0.125, -0.125]).reshape(3).astype(np.float32)
- max_val = np.array([63.625, 63.625, 63.625]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel9():
- # WithVarsPerChannelDim2NudgedUp_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.26, -0.25, -0.24, 0.0, 63.25, 63.3]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array(
- [-0.25, -0.25, -0.25, 0.0, 63.25, 63.25]).astype(np.float32)
- min_val = np.array([-0.125, -0.125, -0.125]).reshape(3).astype(np.float32)
- max_val = np.array([63.375, 63.375, 63.375]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel10():
- # WithVarsPerChannelDim4NudgedDown_RegularRange
- # scale 1/4, zp: 0.4, nudge 0. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 0.1, 0.25, 0.5, 0.75,
- 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
- 63.0, 63.25, 63.5, 63.7, 63.75, 63.8,
- 63.9, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.25, 0.5, 0.75,
- 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
- 63.0, 63.25, 63.5, 63.75, 63.75, 63.75,
- 63.75, 63.75, 63.75, 63.75, 63.75, 63.75]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
- max_val = np.array([63.65, 63.65, 63.65, 63.65]
- ).reshape(4).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
-
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel11():
- # WithVarsPerChannelDim4NudgedDown_NarrowRange
- # scale 1/4, zp: 1.4, nudge 1. nudged range [0.0, 63.25]
- x = np.array([-0.1, 0.0, 0.1, 0.25, 0.5, 0.75,
- 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
- 63.0, 63.25, 63.3, 63.4, 63.5, 63.6,
- 63.7, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.25, 0.5, 0.75,
- 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
- 63.0, 63.25, 63.25, 63.5, 63.5, 63.5,
- 63.5, 63.5, 63.5, 63.5, 63.5, 63.5]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
- max_val = np.array([63.4, 63.4, 63.4, 63.4]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel12():
- # WithVarsPerChannelDim4NudgedUp_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.3, -0.25, -0.2, 0.0, 0.25, 0.5,
- 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
- 63.0, 63.25, 63.4, 63.5, 63.6, 63.7,
- 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([-0.25, -0.25, -0.25, 0.0, 0.25, 0.5,
- 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
- 63.0, 63.25, 63.5, 63.5, 63.5, 63.5,
- 63.5, 63.5, 63.5, 63.5, 63.5, 63.5]).astype(np.float32)
- min_val = np.array([-0.125, -0.125, -0.125, -0.125]
- ).reshape(4).astype(np.float32)
- max_val = np.array([63.625, 63.625, 63.625, 63.625]
- ).reshape(4).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel13():
- # WithVarsPerChannelDim4NudgedUp_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.3, -0.25, -0.2, 0.0, 0.25, 0.5,
- 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
- 63.0, 63.2, 63.25, 63.3, 63.4, 63.5,
- 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([-0.25, -0.25, -0.25, 0.0, 0.25, 0.5,
- 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
- 63.0, 63.25, 63.25, 63.25, 63.25, 63.25,
- 63.25, 63.25, 63.25, 63.25, 63.25, 63.25]).astype(np.float32)
- min_val = np.array([-0.125, -0.125, -0.125, -0.125]
- ).reshape(4).astype(np.float32)
- max_val = np.array([63.375, 63.375, 63.375, 63.375]
- ).reshape(4).astype(np.float32)
-
- net = Net(num_bits=8, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel14():
- # WithVarsPerChannelDim1NudgedDown_4Bits_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 7.5, 7.6]).reshape(4).astype(np.float32)
- expect = np.array([0.0, 0.0, 7.5, 7.5]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
- max_val = np.array([7.4, 7.4, 7.4, 7.4]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=False, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel15():
- # WithVarsPerChannelDim1NudgedDown_4Bits_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 7.0, 7.1]).reshape(4).astype(np.float32)
- expect = np.array([0.0, 0.0, 7.0, 7.0]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
- max_val = np.array([6.9, 6.9, 6.9, 6.9]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=True, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel16():
- # WithVarsPerChannelDim1NudgedUp_4Bits_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.6, -0.5, 7.0, 7.1]).reshape(4).astype(np.float32)
- expect = np.array([-0.5, -0.5, 7.0, 7.0]).astype(np.float32)
- min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
- max_val = np.array([7.1, 7.1, 7.1, 7.1]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=False, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel17():
- # WithVarsPerChannelDim1NudgedUp_4Bits_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.6, -0.5, 6.5, 6.6]).reshape(4).astype(np.float32)
- expect = np.array([-0.5, -0.5, 6.5, 6.5]).astype(np.float32)
- min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
- max_val = np.array([6.6, 6.6, 6.6, 6.6]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=True, channel_axis=0)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel18():
- # WithVarsPerChannelDim2NudgedDown_4Bits_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 0.1, 0.5, 7.5, 7.6]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.5, 7.5, 7.5]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
- max_val = np.array([7.4, 7.4, 7.4]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel19():
- # WithVarsPerChannelDim2NudgedDown_4Bits_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 0.1, 0.5, 7.0, 7.1]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.5, 7.0, 7.0]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
- max_val = np.array([6.9, 6.9, 6.9]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel20():
- # WithVarsPerChannelDim2NudgedUp_4Bits_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.51, -0.5, -0.24, 0.0, 7.0, 7.1]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([-0.5, -0.5, 0.0, 0.0, 7.0, 7.0]).astype(np.float32)
- min_val = np.array([-0.4, -0.4, -0.4]).reshape(3).astype(np.float32)
- max_val = np.array([7.1, 7.1, 7.1]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel21():
- # WithVarsPerChannelDim2NudgedUp_4Bits_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.6, -0.5, -0.24, 0.0, 6.5, 6.6]
- ).reshape(2, 3).astype(np.float32)
- expect = np.array([-0.5, -0.5, 0.0, 0.0, 6.5, 6.5]).astype(np.float32)
- min_val = np.array([-0.4, -0.4, -0.4]).reshape(3).astype(np.float32)
- max_val = np.array([6.6, 6.6, 6.6]).reshape(3).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel22():
- # WithVarsPerChannelDim4NudgedDown_4Bits_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 0.1, 0.5, 1.0, 1.5,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 6.0, 6.5, 7.0, 7.4, 7.5, 7.7,
- 7.8, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.5, 1.0, 1.5,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 6.0, 6.5, 7.0, 7.5, 7.5, 7.5,
- 7.5, 7.5, 7.5, 7.5, 7.5, 7.5]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
- max_val = np.array([7.4, 7.4, 7.4, 7.4]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel23():
- # WithVarsPerChannelDim4NudgedDown_4Bits_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.1, 0.0, 0.1, 0.5, 1.0, 1.5,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 6.0, 6.5, 6.8, 6.9, 7.0, 7.1,
- 7.2, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([0.0, 0.0, 0.0, 0.5, 1.0, 1.5,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 6.0, 6.5, 7.0, 7.0, 7.0, 7.0,
- 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]).astype(np.float32)
- min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
- max_val = np.array([6.9, 6.9, 6.9, 6.9]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel24():
- # WithVarsPerChannelDim4NudgedUp_4Bits_RegularRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.6, -0.5, -0.4, 0.0, 0.5, 1.0,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 6.0, 6.5, 6.9, 7.0, 7.1, 7.7,
- 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([-0.5, -0.5, -0.5, 0.0, 0.5, 1.0,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 6.0, 6.5, 7.0, 7.0, 7.0, 7.0,
- 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]).astype(np.float32)
- min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
- max_val = np.array([7.1, 7.1, 7.1, 7.1]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=False, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_fake_quant_perchannel25():
- # WithVarsPerChannelDim4NudgedUp_4Bits_NarrowRange
- # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
- x = np.array([-0.6, -0.5, -0.4, 0.0, 0.5, 1.0,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 5.5, 6.0, 6.4, 6.5, 6.6, 6.7,
- 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
- expect = np.array([-0.5, -0.5, -0.5, 0.0, 0.5, 1.0,
- 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
- 5.5, 6.0, 6.5, 6.5, 6.5, 6.5,
- 6.5, 6.5, 6.5, 6.5, 6.5, 6.5]).astype(np.float32)
- min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
- max_val = np.array([6.6, 6.6, 6.6, 6.6]).reshape(4).astype(np.float32)
-
- net = Net(num_bits=4, narrow_range=True, channel_axis=1)
- output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
-
- error = np.ones(shape=expect.shape) * 1.0e-5
- diff = output.asnumpy().flatten() - expect
- print("output: ", output)
- print("expect: ", expect)
- assert np.all(np.abs(diff) < error)
|