You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_fake_quant_perchannel.py 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. from mindspore.common.tensor import Tensor
  19. from mindspore import nn
  20. from mindspore.ops.operations import _quant_ops as Q
  21. context.set_context(device_target='GPU', device_id=0)
  22. class Net(nn.Cell):
  23. def __init__(self, num_bits=8, symmetric=False, narrow_range=False, channel_axis=1):
  24. super(Net, self).__init__()
  25. self.op = Q.FakeQuantPerChannel(num_bits=num_bits,
  26. symmetric=symmetric,
  27. narrow_range=narrow_range,
  28. channel_axis=channel_axis)
  29. def construct(self, x, minq, maxq):
  30. return self.op(x, minq, maxq)
  31. @pytest.mark.level0
  32. @pytest.mark.platform_x86_gpu_training
  33. @pytest.mark.env_onecard
  34. def test_fake_quant_perchannel1():
  35. # WithVarsPerChannel_ZeroMinAndMax
  36. x = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
  37. min_val = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
  38. max_val = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
  39. expect = np.array([0.0, 0.0, 0.0, 0.0]).astype(np.float32)
  40. net = Net(num_bits=8, narrow_range=False, channel_axis=0)
  41. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  42. error = np.ones(shape=expect.shape) * 1.0e-5
  43. diff = output.asnumpy() - expect
  44. print("output: ", output)
  45. print("expect: ", expect)
  46. assert np.all(np.abs(diff) < error)
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_gpu_training
  49. @pytest.mark.env_onecard
  50. def test_fake_quant_perchannel2():
  51. # WithVarsPerChannelDim1NudgedDown_RegularRange
  52. # scale 1/4, zp 0.4, nudge 0. nudged ranges [0.0, 63.75]
  53. x = np.array([-0.1, 0.0, 63.75, 63.8]).astype(np.float32)
  54. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).astype(np.float32)
  55. max_val = np.array([63.65, 63.65, 63.65, 63.65]).astype(np.float32)
  56. expect = np.array([0.0, 0.0, 63.75, 63.75]).astype(np.float32)
  57. net = Net(num_bits=8, narrow_range=False, channel_axis=0)
  58. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  59. error = np.ones(shape=expect.shape) * 1.0e-5
  60. diff = output.asnumpy() - expect
  61. print("output: ", output)
  62. print("expect: ", expect)
  63. assert np.all(np.abs(diff) < error)
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_fake_quant_perchannel3():
  68. # WithVarsPerChannelDim1NudgedDown_NarrowRange
  69. # scale 1/4, zp 1.4, nudge 1. nudged ranges[0.0, 63.5]
  70. x = np.array([-0.1, 0.0, 63.5, 63.6]).astype(np.float32)
  71. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).astype(np.float32)
  72. max_val = np.array([63.4, 63.4, 63.4, 63.4]).astype(np.float32)
  73. expect = np.array([0.0, 0.0, 63.5, 63.5]).astype(np.float32)
  74. net = Net(num_bits=8, narrow_range=True, channel_axis=0)
  75. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  76. error = np.ones(shape=expect.shape) * 1.0e-5
  77. diff = output.asnumpy() - expect
  78. print("output: ", output)
  79. print("expect: ", expect)
  80. assert np.all(np.abs(diff) < error)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_fake_quant_perchannel4():
  85. # WithVarsPerChannelDim1NudgedUp_RegularRange
  86. # [-0.125, 63.625]
  87. # scale 1/4, zp: 0.5, nudge 0. nudged range [-0.25, 63.5]
  88. x = np.array([-0.26, -0.25, -0.24, 63.6]).astype(np.float32)
  89. expect = np.array([-0.25, -0.25, -0.25, 63.5]).astype(np.float32)
  90. min_val = np.array([-0.125, -0.125, -0.125, -0.125]).astype(np.float32)
  91. max_val = np.array([63.625, 63.625, 63.625, 63.625]).astype(np.float32)
  92. net = Net(num_bits=8, narrow_range=False, channel_axis=0)
  93. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  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. @pytest.mark.level0
  100. @pytest.mark.platform_x86_gpu_training
  101. @pytest.mark.env_onecard
  102. def test_fake_quant_perchannel5():
  103. # WithVarsPerChannelDim1NudgedUp_NarrowRange
  104. # scale 1/4, zp: 1.5, nudge 2. nudged range [-0.25, 63.25]
  105. x = np.array([-0.26, -0.25, -0.24, 63.3]).astype(np.float32)
  106. expect = np.array([-0.25, -0.25, -0.25, 63.25]).astype(np.float32)
  107. min_val = np.array([-0.125, -0.125, -0.125, -0.125]).astype(np.float32)
  108. max_val = np.array([63.375, 63.375, 63.375, 63.375]).astype(np.float32)
  109. net = Net(num_bits=8, narrow_range=True, channel_axis=0)
  110. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  111. error = np.ones(shape=expect.shape) * 1.0e-5
  112. diff = output.asnumpy() - expect
  113. print("output: ", output)
  114. print("expect: ", expect)
  115. assert np.all(np.abs(diff) < error)
  116. @pytest.mark.level0
  117. @pytest.mark.platform_x86_gpu_training
  118. @pytest.mark.env_onecard
  119. def test_fake_quant_perchannel6():
  120. # WithVarsPerChannelDim2NudgedDown_RegularRange
  121. # scale 1/4, zp: 0.4, nudge 0. nudged range [-0.25, 63.75]
  122. x = np.array([-0.1, 0.0, 0.1, 0.25, 63.75, 63.80]
  123. ).reshape(2, 3).astype(np.float32)
  124. expect = np.array([-0.0, 0.0, 0.0, 0.25, 63.75, 63.75]).astype(np.float32)
  125. min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
  126. max_val = np.array([63.65, 63.65, 63.65]).reshape(3).astype(np.float32)
  127. net = Net(num_bits=8, narrow_range=False, channel_axis=1)
  128. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  129. error = np.ones(shape=expect.shape) * 1.0e-5
  130. diff = output.asnumpy().flatten() - expect
  131. print("output: ", output)
  132. print("expect: ", expect)
  133. assert np.all(np.abs(diff) < error)
  134. @pytest.mark.level0
  135. @pytest.mark.platform_x86_gpu_training
  136. @pytest.mark.env_onecard
  137. def test_fake_quant_perchannel7():
  138. # WithVarsPerChannelDim2NudgedDown_NarrowRange
  139. # scale 1/4, zp: 1.4, nudge 1. nudged range [-0.25, 63.5]
  140. x = np.array([-0.1, 0.0, 0.1, 0.25, 63.5, 63.6]
  141. ).reshape(2, 3).astype(np.float32)
  142. expect = np.array([0.0, 0.0, 0.0, 0.25, 63.5, 63.5]).astype(np.float32)
  143. min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
  144. max_val = np.array([63.4, 63.4, 63.4]).reshape(3).astype(np.float32)
  145. net = Net(num_bits=8, narrow_range=True, channel_axis=1)
  146. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  147. error = np.ones(shape=expect.shape) * 1.0e-5
  148. diff = output.asnumpy().flatten() - expect
  149. print("output: ", output)
  150. print("expect: ", expect)
  151. assert np.all(np.abs(diff) < error)
  152. @pytest.mark.level0
  153. @pytest.mark.platform_x86_gpu_training
  154. @pytest.mark.env_onecard
  155. def test_fake_quant_perchannel8():
  156. # WithVarsPerChannelDim2NudgedUp_RegularRange
  157. # scale 1/4, zp: 0.5, nudge 1. nudged range [-0.25, 63.5]
  158. x = np.array([-0.26, -0.25, -0.24, 0.0, 63.5, 63.6]
  159. ).reshape(2, 3).astype(np.float32)
  160. expect = np.array([-0.25, -0.25, -0.25, 0.0, 63.5, 63.5]
  161. ).astype(np.float32)
  162. min_val = np.array([-0.125, -0.125, -0.125]).reshape(3).astype(np.float32)
  163. max_val = np.array([63.625, 63.625, 63.625]).reshape(3).astype(np.float32)
  164. net = Net(num_bits=8, narrow_range=False, channel_axis=1)
  165. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  166. error = np.ones(shape=expect.shape) * 1.0e-5
  167. diff = output.asnumpy().flatten() - expect
  168. print("output: ", output)
  169. print("expect: ", expect)
  170. assert np.all(np.abs(diff) < error)
  171. @pytest.mark.level0
  172. @pytest.mark.platform_x86_gpu_training
  173. @pytest.mark.env_onecard
  174. def test_fake_quant_perchannel9():
  175. # WithVarsPerChannelDim2NudgedUp_NarrowRange
  176. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  177. x = np.array([-0.26, -0.25, -0.24, 0.0, 63.25, 63.3]
  178. ).reshape(2, 3).astype(np.float32)
  179. expect = np.array(
  180. [-0.25, -0.25, -0.25, 0.0, 63.25, 63.25]).astype(np.float32)
  181. min_val = np.array([-0.125, -0.125, -0.125]).reshape(3).astype(np.float32)
  182. max_val = np.array([63.375, 63.375, 63.375]).reshape(3).astype(np.float32)
  183. net = Net(num_bits=8, narrow_range=True, channel_axis=1)
  184. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  185. error = np.ones(shape=expect.shape) * 1.0e-5
  186. diff = output.asnumpy().flatten() - expect
  187. print("output: ", output)
  188. print("expect: ", expect)
  189. assert np.all(np.abs(diff) < error)
  190. @pytest.mark.level0
  191. @pytest.mark.platform_x86_gpu_training
  192. @pytest.mark.env_onecard
  193. def test_fake_quant_perchannel10():
  194. # WithVarsPerChannelDim4NudgedDown_RegularRange
  195. # scale 1/4, zp: 0.4, nudge 0. nudged range [-0.25, 63.25]
  196. x = np.array([-0.1, 0.0, 0.1, 0.25, 0.5, 0.75,
  197. 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
  198. 63.0, 63.25, 63.5, 63.7, 63.75, 63.8,
  199. 63.9, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  200. expect = np.array([0.0, 0.0, 0.0, 0.25, 0.5, 0.75,
  201. 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
  202. 63.0, 63.25, 63.5, 63.75, 63.75, 63.75,
  203. 63.75, 63.75, 63.75, 63.75, 63.75, 63.75]).astype(np.float32)
  204. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
  205. max_val = np.array([63.65, 63.65, 63.65, 63.65]
  206. ).reshape(4).astype(np.float32)
  207. net = Net(num_bits=8, narrow_range=False, channel_axis=1)
  208. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  209. error = np.ones(shape=expect.shape) * 1.0e-5
  210. diff = output.asnumpy().flatten() - expect
  211. print("output: ", output)
  212. print("expect: ", expect)
  213. assert np.all(np.abs(diff) < error)
  214. @pytest.mark.level0
  215. @pytest.mark.platform_x86_gpu_training
  216. @pytest.mark.env_onecard
  217. def test_fake_quant_perchannel11():
  218. # WithVarsPerChannelDim4NudgedDown_NarrowRange
  219. # scale 1/4, zp: 1.4, nudge 1. nudged range [0.0, 63.25]
  220. x = np.array([-0.1, 0.0, 0.1, 0.25, 0.5, 0.75,
  221. 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
  222. 63.0, 63.25, 63.3, 63.4, 63.5, 63.6,
  223. 63.7, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  224. expect = np.array([0.0, 0.0, 0.0, 0.25, 0.5, 0.75,
  225. 1.0, 1.25, 1.5, 1.75, 2.0, 2.25,
  226. 63.0, 63.25, 63.25, 63.5, 63.5, 63.5,
  227. 63.5, 63.5, 63.5, 63.5, 63.5, 63.5]).astype(np.float32)
  228. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
  229. max_val = np.array([63.4, 63.4, 63.4, 63.4]).reshape(4).astype(np.float32)
  230. net = Net(num_bits=8, narrow_range=True, channel_axis=1)
  231. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  232. error = np.ones(shape=expect.shape) * 1.0e-5
  233. diff = output.asnumpy().flatten() - expect
  234. print("output: ", output)
  235. print("expect: ", expect)
  236. assert np.all(np.abs(diff) < error)
  237. @pytest.mark.level0
  238. @pytest.mark.platform_x86_gpu_training
  239. @pytest.mark.env_onecard
  240. def test_fake_quant_perchannel12():
  241. # WithVarsPerChannelDim4NudgedUp_RegularRange
  242. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  243. x = np.array([-0.3, -0.25, -0.2, 0.0, 0.25, 0.5,
  244. 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
  245. 63.0, 63.25, 63.4, 63.5, 63.6, 63.7,
  246. 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  247. expect = np.array([-0.25, -0.25, -0.25, 0.0, 0.25, 0.5,
  248. 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
  249. 63.0, 63.25, 63.5, 63.5, 63.5, 63.5,
  250. 63.5, 63.5, 63.5, 63.5, 63.5, 63.5]).astype(np.float32)
  251. min_val = np.array([-0.125, -0.125, -0.125, -0.125]
  252. ).reshape(4).astype(np.float32)
  253. max_val = np.array([63.625, 63.625, 63.625, 63.625]
  254. ).reshape(4).astype(np.float32)
  255. net = Net(num_bits=8, narrow_range=False, channel_axis=1)
  256. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  257. error = np.ones(shape=expect.shape) * 1.0e-5
  258. diff = output.asnumpy().flatten() - expect
  259. print("output: ", output)
  260. print("expect: ", expect)
  261. assert np.all(np.abs(diff) < error)
  262. @pytest.mark.level0
  263. @pytest.mark.platform_x86_gpu_training
  264. @pytest.mark.env_onecard
  265. def test_fake_quant_perchannel13():
  266. # WithVarsPerChannelDim4NudgedUp_NarrowRange
  267. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  268. x = np.array([-0.3, -0.25, -0.2, 0.0, 0.25, 0.5,
  269. 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
  270. 63.0, 63.2, 63.25, 63.3, 63.4, 63.5,
  271. 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  272. expect = np.array([-0.25, -0.25, -0.25, 0.0, 0.25, 0.5,
  273. 0.75, 1.0, 1.25, 1.5, 1.75, 2.0,
  274. 63.0, 63.25, 63.25, 63.25, 63.25, 63.25,
  275. 63.25, 63.25, 63.25, 63.25, 63.25, 63.25]).astype(np.float32)
  276. min_val = np.array([-0.125, -0.125, -0.125, -0.125]
  277. ).reshape(4).astype(np.float32)
  278. max_val = np.array([63.375, 63.375, 63.375, 63.375]
  279. ).reshape(4).astype(np.float32)
  280. net = Net(num_bits=8, narrow_range=True, channel_axis=1)
  281. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  282. error = np.ones(shape=expect.shape) * 1.0e-5
  283. diff = output.asnumpy().flatten() - expect
  284. print("output: ", output)
  285. print("expect: ", expect)
  286. assert np.all(np.abs(diff) < error)
  287. @pytest.mark.level0
  288. @pytest.mark.platform_x86_gpu_training
  289. @pytest.mark.env_onecard
  290. def test_fake_quant_perchannel14():
  291. # WithVarsPerChannelDim1NudgedDown_4Bits_RegularRange
  292. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  293. x = np.array([-0.1, 0.0, 7.5, 7.6]).reshape(4).astype(np.float32)
  294. expect = np.array([0.0, 0.0, 7.5, 7.5]).astype(np.float32)
  295. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
  296. max_val = np.array([7.4, 7.4, 7.4, 7.4]).reshape(4).astype(np.float32)
  297. net = Net(num_bits=4, narrow_range=False, channel_axis=0)
  298. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  299. error = np.ones(shape=expect.shape) * 1.0e-5
  300. diff = output.asnumpy().flatten() - expect
  301. print("output: ", output)
  302. print("expect: ", expect)
  303. assert np.all(np.abs(diff) < error)
  304. @pytest.mark.level0
  305. @pytest.mark.platform_x86_gpu_training
  306. @pytest.mark.env_onecard
  307. def test_fake_quant_perchannel15():
  308. # WithVarsPerChannelDim1NudgedDown_4Bits_NarrowRange
  309. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  310. x = np.array([-0.1, 0.0, 7.0, 7.1]).reshape(4).astype(np.float32)
  311. expect = np.array([0.0, 0.0, 7.0, 7.0]).astype(np.float32)
  312. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
  313. max_val = np.array([6.9, 6.9, 6.9, 6.9]).reshape(4).astype(np.float32)
  314. net = Net(num_bits=4, narrow_range=True, channel_axis=0)
  315. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  316. error = np.ones(shape=expect.shape) * 1.0e-5
  317. diff = output.asnumpy() - expect
  318. print("output: ", output)
  319. print("expect: ", expect)
  320. assert np.all(np.abs(diff) < error)
  321. @pytest.mark.level0
  322. @pytest.mark.platform_x86_gpu_training
  323. @pytest.mark.env_onecard
  324. def test_fake_quant_perchannel16():
  325. # WithVarsPerChannelDim1NudgedUp_4Bits_RegularRange
  326. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  327. x = np.array([-0.6, -0.5, 7.0, 7.1]).reshape(4).astype(np.float32)
  328. expect = np.array([-0.5, -0.5, 7.0, 7.0]).astype(np.float32)
  329. min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
  330. max_val = np.array([7.1, 7.1, 7.1, 7.1]).reshape(4).astype(np.float32)
  331. net = Net(num_bits=4, narrow_range=False, channel_axis=0)
  332. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  333. error = np.ones(shape=expect.shape) * 1.0e-5
  334. diff = output.asnumpy() - expect
  335. print("output: ", output)
  336. print("expect: ", expect)
  337. assert np.all(np.abs(diff) < error)
  338. @pytest.mark.level0
  339. @pytest.mark.platform_x86_gpu_training
  340. @pytest.mark.env_onecard
  341. def test_fake_quant_perchannel17():
  342. # WithVarsPerChannelDim1NudgedUp_4Bits_NarrowRange
  343. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  344. x = np.array([-0.6, -0.5, 6.5, 6.6]).reshape(4).astype(np.float32)
  345. expect = np.array([-0.5, -0.5, 6.5, 6.5]).astype(np.float32)
  346. min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
  347. max_val = np.array([6.6, 6.6, 6.6, 6.6]).reshape(4).astype(np.float32)
  348. net = Net(num_bits=4, narrow_range=True, channel_axis=0)
  349. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  350. error = np.ones(shape=expect.shape) * 1.0e-5
  351. diff = output.asnumpy() - expect
  352. print("output: ", output)
  353. print("expect: ", expect)
  354. assert np.all(np.abs(diff) < error)
  355. @pytest.mark.level0
  356. @pytest.mark.platform_x86_gpu_training
  357. @pytest.mark.env_onecard
  358. def test_fake_quant_perchannel18():
  359. # WithVarsPerChannelDim2NudgedDown_4Bits_RegularRange
  360. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  361. x = np.array([-0.1, 0.0, 0.1, 0.5, 7.5, 7.6]
  362. ).reshape(2, 3).astype(np.float32)
  363. expect = np.array([0.0, 0.0, 0.0, 0.5, 7.5, 7.5]).astype(np.float32)
  364. min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
  365. max_val = np.array([7.4, 7.4, 7.4]).reshape(3).astype(np.float32)
  366. net = Net(num_bits=4, narrow_range=False, channel_axis=1)
  367. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  368. error = np.ones(shape=expect.shape) * 1.0e-5
  369. diff = output.asnumpy().flatten() - expect
  370. print("output: ", output)
  371. print("expect: ", expect)
  372. assert np.all(np.abs(diff) < error)
  373. @pytest.mark.level0
  374. @pytest.mark.platform_x86_gpu_training
  375. @pytest.mark.env_onecard
  376. def test_fake_quant_perchannel19():
  377. # WithVarsPerChannelDim2NudgedDown_4Bits_NarrowRange
  378. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  379. x = np.array([-0.1, 0.0, 0.1, 0.5, 7.0, 7.1]
  380. ).reshape(2, 3).astype(np.float32)
  381. expect = np.array([0.0, 0.0, 0.0, 0.5, 7.0, 7.0]).astype(np.float32)
  382. min_val = np.array([-0.1, -0.1, -0.1]).reshape(3).astype(np.float32)
  383. max_val = np.array([6.9, 6.9, 6.9]).reshape(3).astype(np.float32)
  384. net = Net(num_bits=4, narrow_range=True, channel_axis=1)
  385. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  386. error = np.ones(shape=expect.shape) * 1.0e-5
  387. diff = output.asnumpy().flatten() - expect
  388. print("output: ", output)
  389. print("expect: ", expect)
  390. assert np.all(np.abs(diff) < error)
  391. @pytest.mark.level0
  392. @pytest.mark.platform_x86_gpu_training
  393. @pytest.mark.env_onecard
  394. def test_fake_quant_perchannel20():
  395. # WithVarsPerChannelDim2NudgedUp_4Bits_RegularRange
  396. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  397. x = np.array([-0.51, -0.5, -0.24, 0.0, 7.0, 7.1]
  398. ).reshape(2, 3).astype(np.float32)
  399. expect = np.array([-0.5, -0.5, 0.0, 0.0, 7.0, 7.0]).astype(np.float32)
  400. min_val = np.array([-0.4, -0.4, -0.4]).reshape(3).astype(np.float32)
  401. max_val = np.array([7.1, 7.1, 7.1]).reshape(3).astype(np.float32)
  402. net = Net(num_bits=4, narrow_range=False, channel_axis=1)
  403. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  404. error = np.ones(shape=expect.shape) * 1.0e-5
  405. diff = output.asnumpy().flatten() - expect
  406. print("output: ", output)
  407. print("expect: ", expect)
  408. assert np.all(np.abs(diff) < error)
  409. @pytest.mark.level0
  410. @pytest.mark.platform_x86_gpu_training
  411. @pytest.mark.env_onecard
  412. def test_fake_quant_perchannel21():
  413. # WithVarsPerChannelDim2NudgedUp_4Bits_NarrowRange
  414. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  415. x = np.array([-0.6, -0.5, -0.24, 0.0, 6.5, 6.6]
  416. ).reshape(2, 3).astype(np.float32)
  417. expect = np.array([-0.5, -0.5, 0.0, 0.0, 6.5, 6.5]).astype(np.float32)
  418. min_val = np.array([-0.4, -0.4, -0.4]).reshape(3).astype(np.float32)
  419. max_val = np.array([6.6, 6.6, 6.6]).reshape(3).astype(np.float32)
  420. net = Net(num_bits=4, narrow_range=True, channel_axis=1)
  421. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  422. error = np.ones(shape=expect.shape) * 1.0e-5
  423. diff = output.asnumpy().flatten() - expect
  424. print("output: ", output)
  425. print("expect: ", expect)
  426. assert np.all(np.abs(diff) < error)
  427. @pytest.mark.level0
  428. @pytest.mark.platform_x86_gpu_training
  429. @pytest.mark.env_onecard
  430. def test_fake_quant_perchannel22():
  431. # WithVarsPerChannelDim4NudgedDown_4Bits_RegularRange
  432. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  433. x = np.array([-0.1, 0.0, 0.1, 0.5, 1.0, 1.5,
  434. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  435. 6.0, 6.5, 7.0, 7.4, 7.5, 7.7,
  436. 7.8, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  437. expect = np.array([0.0, 0.0, 0.0, 0.5, 1.0, 1.5,
  438. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  439. 6.0, 6.5, 7.0, 7.5, 7.5, 7.5,
  440. 7.5, 7.5, 7.5, 7.5, 7.5, 7.5]).astype(np.float32)
  441. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
  442. max_val = np.array([7.4, 7.4, 7.4, 7.4]).reshape(4).astype(np.float32)
  443. net = Net(num_bits=4, narrow_range=False, channel_axis=1)
  444. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  445. error = np.ones(shape=expect.shape) * 1.0e-5
  446. diff = output.asnumpy().flatten() - expect
  447. print("output: ", output)
  448. print("expect: ", expect)
  449. assert np.all(np.abs(diff) < error)
  450. @pytest.mark.level0
  451. @pytest.mark.platform_x86_gpu_training
  452. @pytest.mark.env_onecard
  453. def test_fake_quant_perchannel23():
  454. # WithVarsPerChannelDim4NudgedDown_4Bits_NarrowRange
  455. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  456. x = np.array([-0.1, 0.0, 0.1, 0.5, 1.0, 1.5,
  457. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  458. 6.0, 6.5, 6.8, 6.9, 7.0, 7.1,
  459. 7.2, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  460. expect = np.array([0.0, 0.0, 0.0, 0.5, 1.0, 1.5,
  461. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  462. 6.0, 6.5, 7.0, 7.0, 7.0, 7.0,
  463. 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]).astype(np.float32)
  464. min_val = np.array([-0.1, -0.1, -0.1, -0.1]).reshape(4).astype(np.float32)
  465. max_val = np.array([6.9, 6.9, 6.9, 6.9]).reshape(4).astype(np.float32)
  466. net = Net(num_bits=4, narrow_range=True, channel_axis=1)
  467. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  468. error = np.ones(shape=expect.shape) * 1.0e-5
  469. diff = output.asnumpy().flatten() - expect
  470. print("output: ", output)
  471. print("expect: ", expect)
  472. assert np.all(np.abs(diff) < error)
  473. @pytest.mark.level0
  474. @pytest.mark.platform_x86_gpu_training
  475. @pytest.mark.env_onecard
  476. def test_fake_quant_perchannel24():
  477. # WithVarsPerChannelDim4NudgedUp_4Bits_RegularRange
  478. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  479. x = np.array([-0.6, -0.5, -0.4, 0.0, 0.5, 1.0,
  480. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  481. 6.0, 6.5, 6.9, 7.0, 7.1, 7.7,
  482. 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  483. expect = np.array([-0.5, -0.5, -0.5, 0.0, 0.5, 1.0,
  484. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  485. 6.0, 6.5, 7.0, 7.0, 7.0, 7.0,
  486. 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]).astype(np.float32)
  487. min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
  488. max_val = np.array([7.1, 7.1, 7.1, 7.1]).reshape(4).astype(np.float32)
  489. net = Net(num_bits=4, narrow_range=False, channel_axis=1)
  490. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  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. @pytest.mark.level0
  497. @pytest.mark.platform_x86_gpu_training
  498. @pytest.mark.env_onecard
  499. def test_fake_quant_perchannel25():
  500. # WithVarsPerChannelDim4NudgedUp_4Bits_NarrowRange
  501. # scale 1/4, zp: 0.5, nudge 2. nudged range [-0.25, 63.25]
  502. x = np.array([-0.6, -0.5, -0.4, 0.0, 0.5, 1.0,
  503. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  504. 5.5, 6.0, 6.4, 6.5, 6.6, 6.7,
  505. 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0]).reshape(1, 4, 2, 3).astype(np.float32)
  506. expect = np.array([-0.5, -0.5, -0.5, 0.0, 0.5, 1.0,
  507. 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
  508. 5.5, 6.0, 6.5, 6.5, 6.5, 6.5,
  509. 6.5, 6.5, 6.5, 6.5, 6.5, 6.5]).astype(np.float32)
  510. min_val = np.array([-0.4, -0.4, -0.4, -0.4]).reshape(4).astype(np.float32)
  511. max_val = np.array([6.6, 6.6, 6.6, 6.6]).reshape(4).astype(np.float32)
  512. net = Net(num_bits=4, narrow_range=True, channel_axis=1)
  513. output = net(Tensor(x), Tensor(min_val), Tensor(max_val))
  514. error = np.ones(shape=expect.shape) * 1.0e-5
  515. diff = output.asnumpy().flatten() - expect
  516. print("output: ", output)
  517. print("expect: ", expect)
  518. assert np.all(np.abs(diff) < error)