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_passes.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 types
  9. import numpy as np
  10. import pytest
  11. import megengine as mge
  12. import megengine.functional as F
  13. import megengine.module as M
  14. import megengine.traced_module as tm
  15. class myconv(M.Conv2d):
  16. pass
  17. class mybn(M.BatchNorm2d):
  18. pass
  19. class MyBlock(M.Module):
  20. def __init__(self, conv_cls, bn_cls):
  21. super().__init__()
  22. self.conv = conv_cls(3, 3, 1, 1, 0)
  23. self.bn = bn_cls(3)
  24. self.conv2 = conv_cls(3, 3, 1, 1, 0)
  25. self.bn2 = bn_cls(3)
  26. self.scale = mge.Tensor([3, 4])
  27. def forward(self, x):
  28. x1 = self.conv(x)
  29. x1 = self.bn(x1)
  30. x1 = F.relu(x1)
  31. x1 = x1 * self.scale[0]
  32. x2 = self.conv2(x)
  33. x2 = self.bn2(x2)
  34. x2 = F.relu(x2)
  35. x2 = x2 * self.scale[1]
  36. y = x1 + x2
  37. y = y + 4
  38. y = self.scale[0] + y
  39. y = F.relu(y) * 3
  40. return y
  41. class MyModule(M.Module):
  42. def __init__(self, conv_cls, bn_cls):
  43. super().__init__()
  44. self.block_0 = MyBlock(conv_cls, bn_cls)
  45. self.block_1 = MyBlock(conv_cls, bn_cls)
  46. def forward(self, x):
  47. x1 = self.block_0(x)
  48. x2 = self.block_1(x)
  49. y = x1 + x2
  50. y = F.reshape(y, (-1))
  51. y = y * 3
  52. return y
  53. @pytest.mark.parametrize("conv_cls", [M.Conv2d, myconv])
  54. @pytest.mark.parametrize("bn_cls", [M.BatchNorm2d, mybn])
  55. def test_backward_fold_scale(conv_cls, bn_cls):
  56. module = MyModule(conv_cls, bn_cls)
  57. module.eval()
  58. inp = mge.Tensor(np.random.random((1, 3, 32, 32)))
  59. desired = module(inp)
  60. traced_net = tm.trace_module(module, inp)
  61. traced_net = traced_net.flatten()
  62. optimized_net = tm.optimize(traced_net, "BackwardFoldScale")
  63. actual = optimized_net(inp)
  64. np.testing.assert_allclose(desired=desired, actual=actual, atol=1e-4)
  65. # fuse all mul to conv
  66. mul_list = optimized_net.graph.get_method_by_type("__mul__").as_list()
  67. assert len(mul_list) == 0
  68. @pytest.mark.parametrize("conv_cls", [M.Conv2d, myconv])
  69. @pytest.mark.parametrize("bn_cls", [M.BatchNorm2d, mybn])
  70. def test_fuse_bn(conv_cls, bn_cls):
  71. module = MyModule(conv_cls, bn_cls)
  72. module.eval()
  73. inp = mge.Tensor(np.random.random((1, 3, 32, 32)))
  74. desired = module(inp)
  75. traced_net = tm.trace_module(module, inp)
  76. traced_net = traced_net.flatten()
  77. optimized_net = tm.optimize(traced_net, "FuseConvBn")
  78. actual = optimized_net(inp)
  79. np.testing.assert_allclose(desired=desired, actual=actual, atol=1e-4)
  80. # fuse all mul to conv
  81. bn_list = optimized_net.graph.get_function_by_type(F.batch_norm).as_list()
  82. assert len(bn_list) == 0
  83. bn_list = optimized_net.graph.get_module_by_type(M.BatchNorm2d).as_list()
  84. assert len(bn_list) == 0

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