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

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

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