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_repr.py 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import megengine.module as M
  9. from megengine.quantization import quantize, quantize_qat
  10. def test_repr():
  11. class Net(M.Module):
  12. def __init__(self):
  13. super().__init__()
  14. self.conv_bn = M.ConvBnRelu2d(3, 3, 3)
  15. self.linear = M.Linear(3, 3)
  16. def forward(self, x):
  17. return x
  18. net = Net()
  19. ground_truth = (
  20. "Net(\n"
  21. " (conv_bn): ConvBnRelu2d(\n"
  22. " (conv): Conv2d(3, 3, kernel_size=(3, 3))\n"
  23. " (bn): BatchNorm2d(3, eps=1e-05, momentum=0.9, affine=True, track_running_stats=True)\n"
  24. " )\n"
  25. " (linear): Linear(in_features=3, out_features=3, bias=True)\n"
  26. ")"
  27. )
  28. assert net.__repr__() == ground_truth
  29. quantize_qat(net)
  30. ground_truth = (
  31. "Net(\n"
  32. " (conv_bn): QAT.ConvBnRelu2d(\n"
  33. " (conv): Conv2d(3, 3, kernel_size=(3, 3))\n"
  34. " (bn): BatchNorm2d(3, eps=1e-05, momentum=0.9, affine=True, track_running_stats=True)\n"
  35. " (act_observer): ExponentialMovingAverageObserver()\n"
  36. " (act_fake_quant): FakeQuantize()\n"
  37. " (weight_observer): MinMaxObserver()\n"
  38. " (weight_fake_quant): FakeQuantize()\n"
  39. " )\n"
  40. " (linear): QAT.Linear(\n"
  41. " in_features=3, out_features=3, bias=True\n"
  42. " (act_observer): ExponentialMovingAverageObserver()\n"
  43. " (act_fake_quant): FakeQuantize()\n"
  44. " (weight_observer): MinMaxObserver()\n"
  45. " (weight_fake_quant): FakeQuantize()\n"
  46. " )\n"
  47. ")"
  48. )
  49. assert net.__repr__() == ground_truth
  50. quantize(net)
  51. ground_truth = (
  52. "Net(\n"
  53. " (conv_bn): Quantized.ConvBnRelu2d(3, 3, kernel_size=(3, 3))\n"
  54. " (linear): Quantized.Linear()\n"
  55. ")"
  56. )
  57. assert net.__repr__() == ground_truth

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