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 12 kB

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

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