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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. from megengine import tensor
  13. from megengine.quantization.fake_quant import TQT_Function
  14. from megengine.quantization.internal_fake_quant import *
  15. from megengine.test import assertTensorClose
  16. class numpy_TQT_Function:
  17. def __init__(self, lowerbound, upperbound):
  18. super().__init__()
  19. self.lowerbound = lowerbound
  20. self.upperbound = upperbound
  21. def forward(self, inp, scale):
  22. t = 2 ** scale
  23. # t = F.maximum(t, 1e-4)
  24. inp_scaled = inp / t
  25. inp_clipped = np.maximum(
  26. np.minimum(inp_scaled, self.upperbound), self.lowerbound
  27. )
  28. inp_rounded = np.round(inp_clipped)
  29. inp_flq = inp_rounded * t
  30. self.saved_tensors = (inp_scaled, inp_rounded, t)
  31. return inp_flq
  32. def backward(self, grad_inp_flq):
  33. (inp_scaled, inp_rounded, t) = self.saved_tensors
  34. mask_clip = (inp_scaled < -0.5 + self.lowerbound) + (
  35. inp_scaled > self.upperbound + 0.5
  36. ) # mask for accumulating the gradients of |data_scaled|>L
  37. mask_quant = np.abs(
  38. mask_clip - 1
  39. ) # mask for accumulating the gradients with |data_scaled|<=L
  40. grad_quant = (
  41. grad_inp_flq * mask_quant * (inp_rounded - inp_scaled)
  42. ) # gradient within |data_scaled|<=L
  43. grad_clip = (
  44. grad_inp_flq * mask_clip * inp_rounded
  45. ) # gradient with | data_scaled|>L
  46. grad_s = grad_clip.sum() + grad_quant.sum()
  47. # dL/ds = dL/dt * t * ln(2)
  48. grad_s = grad_s * t * np.log(2)
  49. grad_inp = grad_inp_flq * mask_quant
  50. return grad_inp, grad_s
  51. def test_TQT():
  52. f = TQT_Function(-127, 127)
  53. nf = numpy_TQT_Function(-127, 127)
  54. def check_inp(a, b, c, a_np, b_np, c_np):
  55. assertTensorClose(
  56. f.forward(a, b).numpy(), nf.forward(a_np, b_np).astype("float32")
  57. )
  58. c1, c2 = f.backward(c)
  59. c1_np, c2_np = nf.backward(c_np)
  60. assertTensorClose(c1.numpy(), c1_np.astype("float32"))
  61. assertTensorClose(c2.numpy(), c2_np.astype("float32"))
  62. a_np = np.random.random((4, 3)).astype("float32")
  63. b_np = np.random.random((1)).astype("float32")
  64. a = tensor(a_np)
  65. b = tensor(b_np)
  66. check_inp(a, b, b, a_np, b_np, b_np)

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