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_modification.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 numpy as np
  9. import megengine.functional as F
  10. import megengine.module as M
  11. from megengine.traced_module import trace_module
  12. from megengine.traced_module.expr import CallFunction, GetAttr
  13. class MyBlock(M.Module):
  14. def __init__(self, in_channels=3, channels=3):
  15. super(MyBlock, self).__init__()
  16. self.conv1 = M.Conv2d(in_channels, channels, 3, 1, padding=1, bias=False)
  17. self.bn1 = M.BatchNorm2d(channels)
  18. def forward(self, x):
  19. x = self.conv1(x)
  20. x = self.bn1(x)
  21. x = F.relu(x) + 1
  22. return x
  23. class MyModule(M.Module):
  24. def __init__(self):
  25. super(MyModule, self).__init__()
  26. self.block0 = MyBlock()
  27. self.block1 = MyBlock()
  28. def forward(self, x):
  29. x = self.block0(x)
  30. x = self.block1(x)
  31. return x
  32. def _init_cls(cls):
  33. module = cls()
  34. x = F.ones((1, 3, 3, 3))
  35. y = module(x)
  36. traced_module = trace_module(module, x)
  37. return traced_module, x, y
  38. def _init_block():
  39. return _init_cls(MyBlock)
  40. def _init_module():
  41. return _init_cls(MyModule)
  42. def test_search():
  43. traced_module, *_ = _init_block()
  44. graph = traced_module.graph
  45. relu_expr = graph.get_function_by_type(F.relu).as_unique()
  46. assert isinstance(relu_expr, CallFunction) and relu_expr.func == F.relu
  47. def test_insert():
  48. traced_module, x, expect = _init_block()
  49. graph = traced_module.graph
  50. relu_out = graph.get_function_by_type(F.relu).as_unique().outputs[0]
  51. with graph.insert_exprs():
  52. neg_out = F.neg(relu_out)
  53. graph.replace_node({relu_out: neg_out})
  54. graph.compile()
  55. np.testing.assert_allclose(expect - 1, 1 - traced_module(x), atol=1e-6)
  56. def test_delete():
  57. traced_module, x, expect = _init_block()
  58. graph = traced_module.graph
  59. relu_expr = graph.get_function_by_type(F.relu).as_unique()
  60. node = relu_expr.outputs
  61. repl_node = relu_expr.inputs
  62. graph.replace_node({node[0]: repl_node[0]})
  63. graph.compile()
  64. np.testing.assert_allclose(expect - 1, F.relu(traced_module(x) - 1), atol=1e-6)
  65. def test_flatten():
  66. traced_module, x, expect = _init_module()
  67. traced_module = traced_module.flatten()
  68. traced_module.graph.compile()
  69. assert all(not isinstance(i, GetAttr) for i in traced_module.graph._exprs)
  70. assert len(traced_module.graph._exprs) == 12
  71. def test_extra_block():
  72. class PostProcess(M.Module):
  73. def forward(self, x):
  74. return x * 2
  75. class Net(M.Module):
  76. def __init__(self, traced_module):
  77. super().__init__()
  78. self.post_process = PostProcess()
  79. self.traced_module = traced_module
  80. def forward(self, x):
  81. x = self.traced_module(x)
  82. x = self.post_process(x)
  83. return x
  84. traced_module, x, expect = _init_block()
  85. module = Net(traced_module)
  86. np.testing.assert_allclose(2 * expect, module(x), atol=1e-6)
  87. traced_module = trace_module(module, x)
  88. np.testing.assert_allclose(2 * expect, traced_module(x), atol=1e-6)

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