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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.experimental.traced_module import trace_module
  12. from megengine.experimental.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_node = graph.get_function_by_type(F.relu).as_unique().outputs
  51. neg_node = graph.insert_function(lambda x: F.neg(x), *relu_node)
  52. graph.replace_node({relu_node[0]: neg_node})
  53. graph.compile()
  54. np.testing.assert_allclose(expect - 1, 1 - traced_module(x), atol=1e-6)
  55. def test_delete():
  56. traced_module, x, expect = _init_block()
  57. graph = traced_module.graph
  58. relu_expr = graph.get_function_by_type(F.relu).as_unique()
  59. node = relu_expr.outputs
  60. repl_node = relu_expr.inputs
  61. graph.replace_node({node[0]: repl_node[0]})
  62. graph.compile()
  63. np.testing.assert_allclose(expect - 1, F.relu(traced_module(x) - 1), atol=1e-6)
  64. def test_flatten():
  65. traced_module, x, expect = _init_module()
  66. traced_module = traced_module.flatten()
  67. traced_module.graph.compile()
  68. assert all(not isinstance(i, GetAttr) for i in traced_module.graph._exprs)
  69. assert len(traced_module.graph._exprs) == 12
  70. def test_extra_block():
  71. class PostProcess(M.Module):
  72. def forward(self, x):
  73. return x * 2
  74. class Net(M.Module):
  75. def __init__(self, traced_module):
  76. super().__init__()
  77. self.post_process = PostProcess()
  78. self.traced_module = traced_module
  79. def forward(self, x):
  80. x = self.traced_module(x)
  81. x = self.post_process(x)
  82. return x
  83. traced_module, x, expect = _init_block()
  84. module = Net(traced_module)
  85. np.testing.assert_allclose(2 * expect, module(x), atol=1e-6)
  86. traced_module = trace_module(module, x)
  87. np.testing.assert_allclose(2 * expect, traced_module(x), atol=1e-6)

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