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.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import numpy as np
  10. import pytest
  11. import megengine as mge
  12. import megengine._internal as mgb
  13. from megengine.core import tensor
  14. from megengine.quantization.fake_quant import TQT_Function
  15. from megengine.quantization.internal_fake_quant import *
  16. from megengine.test import assertTensorClose
  17. class numpy_TQT_Function:
  18. def __init__(self, lowerbound, upperbound):
  19. super().__init__()
  20. self.lowerbound = lowerbound
  21. self.upperbound = upperbound
  22. def forward(self, inp, scale):
  23. t = 2 ** scale
  24. # t = F.maximum(t, 1e-4)
  25. inp_scaled = inp / t
  26. inp_clipped = np.maximum(
  27. np.minimum(inp_scaled, self.upperbound), self.lowerbound
  28. )
  29. inp_rounded = np.round(inp_clipped)
  30. inp_flq = inp_rounded * t
  31. self.saved_tensors = (inp_scaled, inp_rounded, t)
  32. return inp_flq
  33. def backward(self, grad_inp_flq):
  34. (inp_scaled, inp_rounded, t) = self.saved_tensors
  35. mask_clip = (inp_scaled < -0.5 + self.lowerbound) + (
  36. inp_scaled > self.upperbound + 0.5
  37. ) # mask for accumulating the gradients of |data_scaled|>L
  38. mask_quant = np.abs(
  39. mask_clip - 1
  40. ) # mask for accumulating the gradients with |data_scaled|<=L
  41. grad_quant = (
  42. grad_inp_flq * mask_quant * (inp_rounded - inp_scaled)
  43. ) # gradient within |data_scaled|<=L
  44. grad_clip = (
  45. grad_inp_flq * mask_clip * inp_rounded
  46. ) # gradient with | data_scaled|>L
  47. grad_s = grad_clip.sum() + grad_quant.sum()
  48. # dL/ds = dL/dt * t * ln(2)
  49. grad_s = grad_s * t * np.log(2)
  50. grad_inp = grad_inp_flq * mask_quant
  51. return grad_inp, grad_s
  52. def test_TQT():
  53. f = TQT_Function(-127, 127)
  54. nf = numpy_TQT_Function(-127, 127)
  55. def check_inp(a, b, c, a_np, b_np, c_np):
  56. assertTensorClose(
  57. f.forward(a, b).numpy(), nf.forward(a_np, b_np).astype("float32")
  58. )
  59. c1, c2 = f.backward(c)
  60. c1_np, c2_np = nf.backward(c_np)
  61. assertTensorClose(c1.numpy(), c1_np.astype("float32"))
  62. assertTensorClose(c2.numpy(), c2_np.astype("float32"))
  63. a = tensor()
  64. b = tensor()
  65. a_np = np.random.random((4, 3)).astype("float32")
  66. b_np = np.random.random((1)).astype("float32")
  67. a.set_value(a_np)
  68. b.set_value(b_np)
  69. check_inp(a, b, b, a_np, b_np, b_np)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台