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_module.py 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 tempfile
  10. from io import BytesIO
  11. import numpy as np
  12. import pytest
  13. from helpers import MLP
  14. import megengine as mge
  15. import megengine._internal as mgb
  16. from megengine.core import Buffer, Parameter, Tensor, tensor
  17. from megengine.module import BatchNorm1d, BatchNorm2d, Conv2d, Module, Sequential
  18. from megengine.quantization.quantize import quantize, quantize_qat
  19. from megengine.test import assertTensorClose
  20. class MyModule(Module):
  21. class InnerModule(Module):
  22. def __init__(self):
  23. super().__init__()
  24. self.bn = BatchNorm2d(4)
  25. def forward(self, x):
  26. x = self.bn(x)
  27. def __init__(self):
  28. super().__init__()
  29. self.i = self.InnerModule()
  30. self.bn = BatchNorm2d(4)
  31. self.param = Parameter(np.ones(1, dtype=np.float32))
  32. self.buff = Buffer(np.ones(1, dtype=np.float32))
  33. def forward(self, x):
  34. x = self.i(x)
  35. x = self.bn(x)
  36. return x
  37. def test_module_api():
  38. m = MyModule()
  39. assert list(m.children()) == [m.bn, m.i]
  40. assert list(m.named_children()) == [("bn", m.bn), ("i", m.i)]
  41. assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
  42. assert list(m.named_modules()) == [
  43. ("", m),
  44. ("bn", m.bn),
  45. ("i", m.i),
  46. ("i.bn", m.i.bn),
  47. ]
  48. assert list(m.named_modules(prefix="x")) == [
  49. ("x", m),
  50. ("x.bn", m.bn),
  51. ("x.i", m.i),
  52. ("x.i.bn", m.i.bn),
  53. ]
  54. assert list(m.buffers()) == [
  55. m.bn.running_mean,
  56. m.bn.running_var,
  57. m.buff,
  58. m.i.bn.running_mean,
  59. m.i.bn.running_var,
  60. ]
  61. assert list(m.buffers(recursive=False)) == [m.buff]
  62. assert list(m.named_buffers()) == [
  63. ("bn.running_mean", m.bn.running_mean),
  64. ("bn.running_var", m.bn.running_var),
  65. ("buff", m.buff),
  66. ("i.bn.running_mean", m.i.bn.running_mean),
  67. ("i.bn.running_var", m.i.bn.running_var),
  68. ]
  69. assert list(m.parameters()) == [
  70. m.bn.bias,
  71. m.bn.weight,
  72. m.i.bn.bias,
  73. m.i.bn.weight,
  74. m.param,
  75. ]
  76. assert list(m.named_parameters()) == [
  77. ("bn.bias", m.bn.bias),
  78. ("bn.weight", m.bn.weight),
  79. ("i.bn.bias", m.i.bn.bias),
  80. ("i.bn.weight", m.i.bn.weight),
  81. ("param", m.param),
  82. ]
  83. m.eval()
  84. assert (
  85. m.training == False
  86. and m.bn.training == False
  87. and m.i.training == False
  88. and m.i.bn.training == False
  89. )
  90. m.bn.train()
  91. assert m.training == False and m.bn.training == True and m.i.bn.training == False
  92. m.eval()
  93. m.i.train()
  94. assert (
  95. m.training == False
  96. and m.bn.training == False
  97. and m.i.training == True
  98. and m.i.bn.training == True
  99. )
  100. m.eval()
  101. m.train()
  102. assert m.training == True and m.bn.training == True and m.i.bn.training == True
  103. def fn(m):
  104. m.training = False
  105. m.apply(fn)
  106. assert m.bn.training == False and m.i.bn.training == False
  107. def test_module_api_reuse_submodule():
  108. m = MyModule()
  109. m.h = m.i # pylint: disable=attribute-defined-outside-init
  110. assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
  111. assert list(m.named_modules()) == [
  112. ("", m),
  113. ("bn", m.bn),
  114. ("h", m.i),
  115. ("h.bn", m.i.bn),
  116. ]
  117. def test_module_api_iterable_stability():
  118. m = MyModule()
  119. l = list(m.modules())
  120. for _ in range(100):
  121. assert list(m.modules()) == l
  122. class MyModule2(Module):
  123. class InnerModule(Module):
  124. def __init__(self):
  125. super().__init__()
  126. self.bn = BatchNorm2d(4)
  127. self.test_bool_key = {True: 1, False: 0}
  128. def forward(self, x):
  129. x = self.bn(x)
  130. def __init__(self):
  131. super().__init__()
  132. self.bn = BatchNorm2d(4)
  133. self.a = [
  134. BatchNorm2d(4),
  135. {"x": BatchNorm2d(4), "y": [BatchNorm2d(4), self.InnerModule()], "z": 0},
  136. (self.InnerModule(),),
  137. ]
  138. def forward(self, x):
  139. return x
  140. def test_expand_structure():
  141. m = MyModule2()
  142. assert list(m.named_modules()) == [
  143. ("", m),
  144. ("a.0", m.a[0]),
  145. ("a.1.x", m.a[1]["x"]),
  146. ("a.1.y.0", m.a[1]["y"][0]),
  147. ("a.1.y.1", m.a[1]["y"][1]),
  148. ("a.1.y.1.bn", m.a[1]["y"][1].bn),
  149. ("a.2.0", m.a[2][0]),
  150. ("a.2.0.bn", m.a[2][0].bn),
  151. ("bn", m.bn),
  152. ]
  153. def test_flatten_others():
  154. def be_others(obj):
  155. return not isinstance(obj, (Tensor, Module))
  156. m = MyModule2()
  157. assert len(list(m._flatten(with_key=True, predicate=be_others))) == 0
  158. def test_flatten_with_parent():
  159. m = MyModule2()
  160. assert list(m.named_modules(with_parent=True)) == [
  161. ("", m, None),
  162. ("a.0", m.a[0], m),
  163. ("a.1.x", m.a[1]["x"], m),
  164. ("a.1.y.0", m.a[1]["y"][0], m),
  165. ("a.1.y.1", m.a[1]["y"][1], m),
  166. ("a.1.y.1.bn", m.a[1]["y"][1].bn, m.a[1]["y"][1]),
  167. ("a.2.0", m.a[2][0], m),
  168. ("a.2.0.bn", m.a[2][0].bn, m.a[2][0]),
  169. ("bn", m.bn, m),
  170. ]
  171. assert list(m.modules(with_parent=True)) == [
  172. (m, None),
  173. (m.a[0], m),
  174. (m.a[1]["x"], m),
  175. (m.a[1]["y"][0], m),
  176. (m.a[1]["y"][1], m),
  177. (m.a[1]["y"][1].bn, m.a[1]["y"][1]),
  178. (m.a[2][0], m),
  179. (m.a[2][0].bn, m.a[2][0]),
  180. (m.bn, m),
  181. ]
  182. class MyModule3(Module):
  183. class InnerModule(Module):
  184. def __init__(self):
  185. super().__init__()
  186. self.bn = BatchNorm2d(4)
  187. def forward(self, x):
  188. x = self.bn(x)
  189. def __init__(self):
  190. super().__init__()
  191. self.bn = BatchNorm2d(4)
  192. self.seq = Sequential(BatchNorm2d(4), self.InnerModule(),)
  193. def forward(self, x):
  194. return x
  195. def test_module_api_with_sequential():
  196. m = MyModule3()
  197. assert list(m.named_modules()) == [
  198. ("", m),
  199. ("bn", m.bn),
  200. ("seq", m.seq),
  201. ("seq.0", m.seq[0]),
  202. ("seq.1", m.seq[1]),
  203. ("seq.1.bn", m.seq[1].bn),
  204. ]
  205. def test_state_dict():
  206. data_shape = (2, 28)
  207. data = tensor()
  208. data.set_value(np.random.random(data_shape))
  209. mlp = MLP()
  210. pred0 = mlp(data)
  211. with BytesIO() as fout:
  212. mge.save(mlp.state_dict(), fout)
  213. fout.seek(0)
  214. state_dict = mge.load(fout)
  215. state_dict["extra"] = None
  216. mlp1 = MLP()
  217. mlp1.load_state_dict(state_dict, strict=False)
  218. pred1 = mlp1(data)
  219. assertTensorClose(pred0.numpy(), pred1.numpy(), max_err=5e-6)
  220. with pytest.raises(KeyError):
  221. mlp1.load_state_dict(state_dict)
  222. del state_dict["extra"]
  223. del state_dict["dense0.bias"]
  224. with pytest.raises(KeyError):
  225. mlp1.load_state_dict(state_dict)
  226. class AssertModule(Module):
  227. def __init__(self):
  228. super().__init__()
  229. self.error_tensor_key = {True: tensor(), False: 0}
  230. def forward(self, x):
  231. return x
  232. def test_assert_message():
  233. m = AssertModule()
  234. with pytest.raises(
  235. AssertionError, match="keys for Tensor and Module must be str, error key: True"
  236. ):
  237. list(m._flatten())
  238. class Simple(Module):
  239. def __init__(self):
  240. super().__init__()
  241. self.conv0 = Conv2d(1, 1, kernel_size=3, bias=False)
  242. self.conv1 = Conv2d(1, 1, kernel_size=3, bias=False)
  243. self.conv1.weight = self.conv0.weight
  244. def forward(self, inputs):
  245. pass
  246. def test_shared_param():
  247. net = Simple()
  248. assert net.conv0.weight is net.conv1.weight
  249. data = tensor(np.random.random((1, 1, 8, 8)).astype(np.float32))
  250. assertTensorClose(net.conv0(data).numpy(), net.conv1(data).numpy())
  251. with BytesIO() as f:
  252. mge.save(net, f)
  253. f.seek(0)
  254. net1 = mge.load(f)
  255. assert net1.conv0.weight is net1.conv1.weight
  256. assertTensorClose(net1.conv0(data).numpy(), net1.conv1(data).numpy())
  257. with BytesIO() as f:
  258. mge.save(net.conv0, f)
  259. f.seek(0)
  260. conv0 = mge.load(f)
  261. with BytesIO() as f:
  262. mge.save(net.conv1, f)
  263. f.seek(0)
  264. conv1 = mge.load(f)
  265. assert conv0.weight is not conv1.weight
  266. assertTensorClose(conv0(data).numpy(), conv1(data).numpy())
  267. def test_pickle_module():
  268. data_shape = (2, 28)
  269. data = tensor()
  270. data.set_value(np.random.random(data_shape))
  271. mlp = MLP()
  272. # pickle before forward
  273. with BytesIO() as fout:
  274. mge.save(mlp, fout)
  275. fout.seek(0)
  276. mlp1 = mge.load(fout)
  277. pred0 = mlp1(data)
  278. pred1 = mlp(data)
  279. # pickle after forward
  280. with BytesIO() as fout:
  281. mge.save(mlp, fout)
  282. fout.seek(0)
  283. mlp1 = mge.load(fout)
  284. pred2 = mlp1(data)
  285. assertTensorClose(pred0.numpy(), pred1.numpy(), max_err=5e-6)
  286. assertTensorClose(pred0.numpy(), pred2.numpy(), max_err=5e-6)
  287. def test_dump_model():
  288. data_shape = (2, 28)
  289. data = tensor()
  290. data.set_value(np.random.random(data_shape))
  291. mlp = MLP()
  292. pred = mlp(data)
  293. with tempfile.NamedTemporaryFile() as f:
  294. mge.dump(pred, f.name)
  295. def test_load_quantized():
  296. data_shape = (2, 28)
  297. data = tensor(np.random.random(data_shape), dtype="float32")
  298. data = data.astype(mgb.dtype.qint8(0.1))
  299. mlp = MLP()
  300. quantize_qat(mlp)
  301. quantize(mlp)
  302. mlp.dense0.weight = Parameter(
  303. mlp.dense0.weight.astype(mgb.dtype.qint8(0.001)).numpy()
  304. )
  305. mlp.dense1.weight = Parameter(
  306. mlp.dense1.weight.astype(mgb.dtype.qint8(0.0002)).numpy()
  307. )
  308. mlp.eval()
  309. pred0 = mlp(data)
  310. with BytesIO() as fout:
  311. mge.save(mlp.state_dict(), fout)
  312. fout.seek(0)
  313. checkpoint = mge.load(fout)
  314. # change mlp weight.
  315. mlp.dense0.weight = Parameter(
  316. mlp.dense0.weight.astype(mgb.dtype.qint8(0.00001)).numpy()
  317. )
  318. mlp.dense1.weight = Parameter(
  319. mlp.dense1.weight.astype(mgb.dtype.qint8(0.2)).numpy()
  320. )
  321. mlp.load_state_dict(checkpoint)
  322. pred1 = mlp(data)
  323. assertTensorClose(
  324. pred0.astype("float32").numpy(), pred1.astype("float32").numpy(), max_err=5e-6
  325. )

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