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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. import megengine as mge
  16. import megengine.functional as F
  17. from megengine import Buffer, Parameter, Tensor, tensor
  18. from megengine.module import (
  19. BatchNorm1d,
  20. BatchNorm2d,
  21. Conv2d,
  22. Linear,
  23. Module,
  24. Sequential,
  25. )
  26. from megengine.quantization.quantize import quantize, quantize_qat
  27. from megengine.test import assertTensorClose
  28. class MLP(Module):
  29. def __init__(self):
  30. super().__init__()
  31. self.dense0 = Linear(28, 50)
  32. self.dense1 = Linear(50, 20)
  33. def forward(self, x):
  34. x = self.dense0(x)
  35. x = F.relu(x)
  36. x = self.dense1(x)
  37. return x
  38. def has_gpu(num=1):
  39. try:
  40. mgb.comp_node("gpu{}".format(num - 1))
  41. except mgb.MegBrainError:
  42. return False
  43. return True
  44. def randomNp(*args):
  45. for arg in args:
  46. assert isinstance(arg, int)
  47. return np.random.random(args)
  48. def randomTorch(*args):
  49. import torch # pylint: disable=import-outside-toplevel
  50. for arg in args:
  51. assert isinstance(arg, int)
  52. return torch.tensor(randomNp(*args), dtype=torch.float32)
  53. def graph_mode(*modes):
  54. if not set(modes).issubset({"eager", "static"}):
  55. raise ValueError("graph mode must be in (eager, static)")
  56. def decorator(func):
  57. def wrapper(*args, **kwargs):
  58. if "eager" in set(modes):
  59. func(*args, **kwargs)
  60. if "static" in set(modes):
  61. with Graph() as cg:
  62. cg.set_option("eager_evaluation", False)
  63. func(*args, **kwargs)
  64. return wrapper
  65. return decorator
  66. def _default_compare_fn(x, y):
  67. assertTensorClose(x.numpy(), y)
  68. def opr_test(
  69. cases,
  70. func,
  71. mode=("eager", "static", "dynamic_shape"),
  72. compare_fn=_default_compare_fn,
  73. ref_fn=None,
  74. **kwargs
  75. ):
  76. """
  77. mode: the list of test mode which are eager, static and dynamic_shape
  78. will test all the cases if None.
  79. func: the function to run opr.
  80. compare_fn: the function to compare the result and expected, use assertTensorClose if None.
  81. ref_fn: the function to generate expected data, should assign output if None.
  82. cases: the list which have dict element, the list length should be 2 for dynamic shape test.
  83. and the dict should have input,
  84. and should have output if ref_fn is None.
  85. should use list for multiple inputs and outputs for each case.
  86. kwargs: The additional kwargs for opr func.
  87. simple examples:
  88. dtype = np.float32
  89. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  90. opr_test(cases,
  91. F.eye,
  92. ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
  93. dtype=dtype)
  94. """
  95. def check_results(results, expected):
  96. if not isinstance(results, Tuple):
  97. results = (results,)
  98. for r, e in zip(results, expected):
  99. compare_fn(r, e)
  100. def get_trace_fn(func, enabled, symbolic):
  101. jit.trace.enabled = enabled
  102. return jit.trace(func, symbolic=symbolic)
  103. def get_param(cases, idx):
  104. case = cases[idx]
  105. inp = case.get("input", None)
  106. outp = case.get("output", None)
  107. if inp is None:
  108. raise ValueError("the test case should have input")
  109. if not isinstance(inp, List):
  110. inp = (inp,)
  111. else:
  112. inp = tuple(inp)
  113. if ref_fn is not None and callable(ref_fn):
  114. outp = ref_fn(*inp)
  115. if outp is None:
  116. raise ValueError("the test case should have output or reference function")
  117. if not isinstance(outp, List):
  118. outp = (outp,)
  119. else:
  120. outp = tuple(outp)
  121. return inp, outp
  122. if not set(mode).issubset({"eager", "static", "dynamic_shape"}):
  123. raise ValueError("opr test mode must be in (eager, static, dynamic_shape)")
  124. if len(cases) == 0:
  125. raise ValueError("should give one case at least")
  126. if "dynamic_shape" in set(mode):
  127. if len(cases) != 2:
  128. raise ValueError("should give 2 cases for dynamic shape test")
  129. if not callable(func):
  130. raise ValueError("the input func should be callable")
  131. inp, outp = get_param(cases, 0)
  132. def run(*args, **kwargs):
  133. return func(*args, **kwargs)
  134. if "eager" in set(mode):
  135. f = get_trace_fn(run, False, False)
  136. results = f(*inp, **kwargs)
  137. check_results(results, outp)
  138. if "static" in set(mode) or "dynamic_shape" in set(mode):
  139. f = get_trace_fn(run, True, True)
  140. results = f(*inp, **kwargs)
  141. check_results(results, outp)
  142. if "dynamic_shape" in set(mode):
  143. inp, outp = get_param(cases, 1)
  144. results = f(*inp, **kwargs)
  145. check_results(results, outp)
  146. class MyModule(Module):
  147. class InnerModule(Module):
  148. def __init__(self):
  149. super().__init__()
  150. self.bn = BatchNorm2d(4)
  151. def forward(self, x):
  152. return self.bn(x)
  153. def __init__(self):
  154. super().__init__()
  155. self.i = self.InnerModule()
  156. self.bn = BatchNorm2d(4)
  157. self.param = Parameter(np.ones(1, dtype=np.float32))
  158. self.buff = Buffer(np.ones(1, dtype=np.float32))
  159. def forward(self, x):
  160. x = self.i(x)
  161. x = self.bn(x)
  162. return x
  163. def test_module_api():
  164. m = MyModule()
  165. assert list(m.children()) == [m.bn, m.i]
  166. assert list(m.named_children()) == [("bn", m.bn), ("i", m.i)]
  167. assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
  168. assert list(m.named_modules()) == [
  169. ("", m),
  170. ("bn", m.bn),
  171. ("i", m.i),
  172. ("i.bn", m.i.bn),
  173. ]
  174. assert list(m.named_modules(prefix="x")) == [
  175. ("x", m),
  176. ("x.bn", m.bn),
  177. ("x.i", m.i),
  178. ("x.i.bn", m.i.bn),
  179. ]
  180. assert list(m.buffers()) == [
  181. m.bn.running_mean,
  182. m.bn.running_var,
  183. m.buff,
  184. m.i.bn.running_mean,
  185. m.i.bn.running_var,
  186. ]
  187. assert list(m.buffers(recursive=False)) == [m.buff]
  188. assert list(m.named_buffers()) == [
  189. ("bn.running_mean", m.bn.running_mean),
  190. ("bn.running_var", m.bn.running_var),
  191. ("buff", m.buff),
  192. ("i.bn.running_mean", m.i.bn.running_mean),
  193. ("i.bn.running_var", m.i.bn.running_var),
  194. ]
  195. assert list(m.parameters()) == [
  196. m.bn.bias,
  197. m.bn.weight,
  198. m.i.bn.bias,
  199. m.i.bn.weight,
  200. m.param,
  201. ]
  202. assert list(m.named_parameters()) == [
  203. ("bn.bias", m.bn.bias),
  204. ("bn.weight", m.bn.weight),
  205. ("i.bn.bias", m.i.bn.bias),
  206. ("i.bn.weight", m.i.bn.weight),
  207. ("param", m.param),
  208. ]
  209. m.eval()
  210. assert (
  211. m.training == False
  212. and m.bn.training == False
  213. and m.i.training == False
  214. and m.i.bn.training == False
  215. )
  216. m.bn.train()
  217. assert m.training == False and m.bn.training == True and m.i.bn.training == False
  218. m.eval()
  219. m.i.train()
  220. assert (
  221. m.training == False
  222. and m.bn.training == False
  223. and m.i.training == True
  224. and m.i.bn.training == True
  225. )
  226. m.eval()
  227. m.train()
  228. assert m.training == True and m.bn.training == True and m.i.bn.training == True
  229. def fn(m):
  230. m.training = False
  231. m.apply(fn)
  232. assert m.bn.training == False and m.i.bn.training == False
  233. def test_module_api_reuse_submodule():
  234. m = MyModule()
  235. m.h = m.i # pylint: disable=attribute-defined-outside-init
  236. assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
  237. assert list(m.named_modules()) == [
  238. ("", m),
  239. ("bn", m.bn),
  240. ("h", m.i),
  241. ("h.bn", m.i.bn),
  242. ]
  243. def test_module_api_iterable_stability():
  244. m = MyModule()
  245. l = list(m.modules())
  246. for _ in range(100):
  247. assert list(m.modules()) == l
  248. def test_module_api_hooks():
  249. net = MyModule()
  250. pre_hook_num = 0
  251. post_hook_num = 0
  252. hooks = []
  253. def pre_hook(module, inputs):
  254. nonlocal pre_hook_num
  255. pre_hook_num += 1
  256. modified_inputs = tuple(inp + 1 for inp in inputs)
  257. return modified_inputs
  258. def post_hook(module, inputs, outputs):
  259. nonlocal post_hook_num
  260. post_hook_num += 1
  261. outputs += 1
  262. return outputs
  263. net.apply(lambda module: hooks.append(module.register_forward_pre_hook(pre_hook)))
  264. net.apply(lambda module: hooks.append(module.register_forward_hook(post_hook)))
  265. shape = (1, 4, 1, 1)
  266. x = tensor(np.zeros(shape, dtype=np.float32))
  267. y = net(x)
  268. assert pre_hook_num == 4
  269. assert post_hook_num == 4
  270. mean1 = Parameter(np.zeros(shape), dtype=np.float32)
  271. bn1 = F.batch_norm2d(
  272. x + 3, mean1, Parameter(np.ones(shape), dtype=np.float32), training=True
  273. )
  274. assertTensorClose(
  275. net.i.bn.running_mean.numpy(), mean1.numpy(),
  276. )
  277. mean2 = Parameter(np.zeros(shape), dtype=np.float32)
  278. bn2 = F.batch_norm2d(
  279. bn1 + 3, mean2, Parameter(np.ones(shape), dtype=np.float32), training=True
  280. )
  281. assertTensorClose(
  282. net.bn.running_mean.numpy(), mean2.numpy(),
  283. )
  284. assertTensorClose((bn2 + 2).numpy(), y.numpy())
  285. assert len(hooks) == 8
  286. for handler in hooks:
  287. handler.remove()
  288. y = net(x)
  289. assert pre_hook_num == 4
  290. assert post_hook_num == 4
  291. class MyModule2(Module):
  292. class InnerModule(Module):
  293. def __init__(self):
  294. super().__init__()
  295. self.bn = BatchNorm2d(4)
  296. self.test_bool_key = {True: 1, False: 0}
  297. def forward(self, x):
  298. x = self.bn(x)
  299. def __init__(self):
  300. super().__init__()
  301. self.bn = BatchNorm2d(4)
  302. self.a = [
  303. BatchNorm2d(4),
  304. {"x": BatchNorm2d(4), "y": [BatchNorm2d(4), self.InnerModule()], "z": 0},
  305. (self.InnerModule(),),
  306. ]
  307. def forward(self, x):
  308. return x
  309. def test_expand_structure():
  310. m = MyModule2()
  311. assert list(m.named_modules()) == [
  312. ("", m),
  313. ("a.0", m.a[0]),
  314. ("a.1.x", m.a[1]["x"]),
  315. ("a.1.y.0", m.a[1]["y"][0]),
  316. ("a.1.y.1", m.a[1]["y"][1]),
  317. ("a.1.y.1.bn", m.a[1]["y"][1].bn),
  318. ("a.2.0", m.a[2][0]),
  319. ("a.2.0.bn", m.a[2][0].bn),
  320. ("bn", m.bn),
  321. ]
  322. def test_flatten_others():
  323. def be_others(obj):
  324. return not isinstance(obj, (Tensor, Module))
  325. m = MyModule2()
  326. assert len(list(m._flatten(with_key=True, predicate=be_others))) == 0
  327. def test_flatten_with_parent():
  328. m = MyModule2()
  329. assert list(m.named_modules(with_parent=True)) == [
  330. ("", m, None),
  331. ("a.0", m.a[0], m),
  332. ("a.1.x", m.a[1]["x"], m),
  333. ("a.1.y.0", m.a[1]["y"][0], m),
  334. ("a.1.y.1", m.a[1]["y"][1], m),
  335. ("a.1.y.1.bn", m.a[1]["y"][1].bn, m.a[1]["y"][1]),
  336. ("a.2.0", m.a[2][0], m),
  337. ("a.2.0.bn", m.a[2][0].bn, m.a[2][0]),
  338. ("bn", m.bn, m),
  339. ]
  340. assert list(m.modules(with_parent=True)) == [
  341. (m, None),
  342. (m.a[0], m),
  343. (m.a[1]["x"], m),
  344. (m.a[1]["y"][0], m),
  345. (m.a[1]["y"][1], m),
  346. (m.a[1]["y"][1].bn, m.a[1]["y"][1]),
  347. (m.a[2][0], m),
  348. (m.a[2][0].bn, m.a[2][0]),
  349. (m.bn, m),
  350. ]
  351. class MyModule3(Module):
  352. class InnerModule(Module):
  353. def __init__(self):
  354. super().__init__()
  355. self.bn = BatchNorm2d(4)
  356. def forward(self, x):
  357. x = self.bn(x)
  358. def __init__(self):
  359. super().__init__()
  360. self.bn = BatchNorm2d(4)
  361. self.seq = Sequential(BatchNorm2d(4), self.InnerModule(),)
  362. def forward(self, x):
  363. return x
  364. def test_module_api_with_sequential():
  365. m = MyModule3()
  366. assert list(m.named_modules()) == [
  367. ("", m),
  368. ("bn", m.bn),
  369. ("seq", m.seq),
  370. ("seq.0", m.seq[0]),
  371. ("seq.1", m.seq[1]),
  372. ("seq.1.bn", m.seq[1].bn),
  373. ]
  374. def test_sequential_named_children():
  375. modules = OrderedDict()
  376. modules["name0"] = Linear(20, 10)
  377. modules["name1"] = Linear(10, 5)
  378. modules["name2"] = Linear(5, 1)
  379. m = Sequential(modules)
  380. l = list(m.named_children())
  381. assert l[0][0] == "layer_values.0"
  382. assert l[1][0] == "layer_values.1"
  383. assert l[2][0] == "layer_values.2"
  384. def test_state_dict():
  385. data_shape = (2, 28)
  386. data = tensor([])
  387. data.set_value(np.random.random(data_shape))
  388. mlp = MLP()
  389. pred0 = mlp(data)
  390. with BytesIO() as fout:
  391. mge.save(mlp.state_dict(), fout)
  392. fout.seek(0)
  393. state_dict = mge.load(fout)
  394. state_dict["extra"] = None
  395. mlp1 = MLP()
  396. mlp1.load_state_dict(state_dict, strict=False)
  397. pred1 = mlp1(data)
  398. assertTensorClose(pred0.numpy(), pred1.numpy(), max_err=5e-6)
  399. with pytest.raises(KeyError):
  400. mlp1.load_state_dict(state_dict)
  401. del state_dict["extra"]
  402. del state_dict["dense0.bias"]
  403. with pytest.raises(KeyError):
  404. mlp1.load_state_dict(state_dict)
  405. class AssertModule(Module):
  406. def __init__(self):
  407. super().__init__()
  408. self.error_tensor_key = {True: tensor([]), False: 0}
  409. def forward(self, x):
  410. return x
  411. def test_assert_message():
  412. m = AssertModule()
  413. with pytest.raises(
  414. AssertionError, match="keys for Tensor and Module must be str, error key: True"
  415. ):
  416. list(m._flatten())
  417. class Simple(Module):
  418. def __init__(self):
  419. super().__init__()
  420. self.conv0 = Conv2d(1, 1, kernel_size=3, bias=False)
  421. self.conv1 = Conv2d(1, 1, kernel_size=3, bias=False)
  422. self.conv1.weight = self.conv0.weight
  423. def forward(self, inputs):
  424. pass
  425. def test_shared_param():
  426. net = Simple()
  427. assert net.conv0.weight is net.conv1.weight
  428. data = tensor(np.random.random((1, 1, 8, 8)).astype(np.float32))
  429. assertTensorClose(net.conv0(data).numpy(), net.conv1(data).numpy())
  430. with BytesIO() as f:
  431. mge.save(net, f)
  432. f.seek(0)
  433. net1 = mge.load(f)
  434. assert net1.conv0.weight is net1.conv1.weight
  435. assertTensorClose(net1.conv0(data).numpy(), net1.conv1(data).numpy())
  436. with BytesIO() as f:
  437. mge.save(net.conv0, f)
  438. f.seek(0)
  439. conv0 = mge.load(f)
  440. with BytesIO() as f:
  441. mge.save(net.conv1, f)
  442. f.seek(0)
  443. conv1 = mge.load(f)
  444. assert conv0.weight is not conv1.weight
  445. assertTensorClose(conv0(data).numpy(), conv1(data).numpy())
  446. def test_pickle_module():
  447. data_shape = (2, 28)
  448. data = tensor([])
  449. data.set_value(np.random.random(data_shape))
  450. mlp = MLP()
  451. # pickle before forward
  452. with BytesIO() as fout:
  453. mge.save(mlp, fout)
  454. fout.seek(0)
  455. mlp1 = mge.load(fout)
  456. pred0 = mlp1(data)
  457. pred1 = mlp(data)
  458. # pickle after forward
  459. with BytesIO() as fout:
  460. mge.save(mlp, fout)
  461. fout.seek(0)
  462. mlp1 = mge.load(fout)
  463. pred2 = mlp1(data)
  464. assertTensorClose(pred0.numpy(), pred1.numpy(), max_err=5e-6)
  465. assertTensorClose(pred0.numpy(), pred2.numpy(), max_err=5e-6)
  466. @pytest.mark.skip(reason="under development")
  467. def test_dump_model():
  468. data_shape = (2, 28)
  469. data = tensor([])
  470. data.set_value(np.random.random(data_shape))
  471. mlp = MLP()
  472. pred = mlp(data)
  473. f = tempfile.NamedTemporaryFile(delete=False)
  474. f_name = f.name
  475. try:
  476. mge.dump(pred, f_name)
  477. finally:
  478. f.close()
  479. os.unlink(f_name)
  480. def test_load_quantized():
  481. from megengine.core.tensor import dtype
  482. data_shape = (2, 28)
  483. data = tensor(np.random.random(data_shape), dtype="float32")
  484. data = data.astype(dtype.qint8(0.1))
  485. mlp = MLP()
  486. quantize_qat(mlp)
  487. quantize(mlp)
  488. mlp.dense0.weight = Parameter(mlp.dense0.weight.astype(dtype.qint8(0.001)).numpy())
  489. mlp.dense1.weight = Parameter(mlp.dense1.weight.astype(dtype.qint8(0.0002)).numpy())
  490. mlp.eval()
  491. pred0 = mlp(data)
  492. with BytesIO() as fout:
  493. mge.save(mlp.state_dict(), fout)
  494. fout.seek(0)
  495. checkpoint = mge.load(fout)
  496. # change mlp weight.
  497. mlp.dense0.weight = Parameter(
  498. mlp.dense0.weight.astype(dtype.qint8(0.00001)).numpy()
  499. )
  500. mlp.dense1.weight = Parameter(
  501. mlp.dense1.weight.astype(dtype.qint8(0.2)).numpy()
  502. )
  503. mlp.load_state_dict(checkpoint)
  504. pred1 = mlp(data)
  505. assertTensorClose(
  506. pred0.astype("float32").numpy(), pred1.astype("float32").numpy(), max_err=5e-6
  507. )

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