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.

dump_with_testcase_mge.py 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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 argparse
  10. import os
  11. import re
  12. import struct
  13. import cv2
  14. import numpy as np
  15. import megengine as mge
  16. import megengine.core._imperative_rt as rt
  17. import megengine.core.tensor.megbrain_graph as G
  18. from megengine import tensor
  19. from megengine.core.ops import builtin
  20. from megengine.utils import comp_graph_tools as cgtools
  21. logger = mge.get_logger(__name__)
  22. def auto_reformat_image(args, path, data, dst_shape):
  23. """reformat image to target shape
  24. :param data: image data as numpy array
  25. :param dst_shape: target shape
  26. """
  27. dim3_format = False # required input format does not contain batch
  28. hwc_format = False # required input format is NHWC
  29. if not dst_shape: # input tensor shape is not predefined
  30. if len(data.shape) == 2:
  31. chl = 1
  32. h = data.shape[0]
  33. w = data.shape[1]
  34. else:
  35. assert len(data.shape) == 3, "Input image must be of dimension 2 or 3"
  36. h, w, chl = data.shape
  37. dst_shape = (1, chl, h, w)
  38. if len(dst_shape) == 3:
  39. dst_shape = (1,) + dst_shape
  40. dim3_format = True
  41. assert len(dst_shape) == 4, "bad dst_shape: {}".format(dst_shape)
  42. chl = dst_shape[1]
  43. if chl in [1, 3]:
  44. n, c, h, w = dst_shape
  45. dst_shape = (n, h, w, c)
  46. else:
  47. chl = dst_shape[3]
  48. assert chl in [1, 3], "can not infer input format from shape: {}".format(
  49. dst_shape
  50. )
  51. hwc_format = True
  52. # dst_shape has now been normalized to NHWC format
  53. if args.resize_input:
  54. h, w = dst_shape[1:3]
  55. data = cv2.resize(data, (w, h))
  56. logger.info("input {} resized to {}".format(path, data.shape))
  57. if chl == 1:
  58. data = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
  59. data = data[:, :, np.newaxis]
  60. assert data.ndim == 3
  61. data = data[np.newaxis]
  62. # data normalized to NHWC format
  63. if not hwc_format:
  64. data = np.transpose(data, (0, 3, 1, 2))
  65. if dim3_format:
  66. data = np.squeeze(data, 0)
  67. return data
  68. def read_input_data(args, dst_shape, dtype, path, repeat):
  69. def check_shape_equal(dst_shape, data_shape):
  70. if len(dst_shape):
  71. assert len(data_shape) == len(
  72. dst_shape
  73. ), "input/data shapes mismatch: {} vs {}".format(dst_shape, data_shape)
  74. if data_shape[1:] != dst_shape[1:]:
  75. logger.warning(
  76. "dst_shape is {}; data_shape is {}".format(dst_shape, data_shape)
  77. )
  78. if path.startswith("#"):
  79. assert not args.resize_input
  80. assert not args.input_transform
  81. spec = path
  82. m = re.match(r"^#rand\(([-0-9.]*)\s*,\s*([-0-9.]*)\s*(,[^\)]+)?\)$", spec)
  83. assert m, "bad spec {}".format(spec)
  84. rng_min = float(m.group(1))
  85. rng_max = float(m.group(2))
  86. if m.group(3):
  87. shape_str = m.group(3)
  88. try:
  89. shape = shape_str[1:].split(",")
  90. if shape[-1].strip() == "...":
  91. shape = shape[:-1]
  92. shape.extend(list(dst_shape[len(shape) :]))
  93. data_shape = tuple(map(int, shape))
  94. except ValueError as e:
  95. raise ValueError("bad spec {}: {}".format(spec, e.args))
  96. else:
  97. data_shape = dst_shape
  98. check_shape_equal(dst_shape, data_shape)
  99. return np.random.uniform(rng_min, rng_max, data_shape).astype(dtype)
  100. # try to load image
  101. data = cv2.imread(path, cv2.IMREAD_COLOR)
  102. if data is None:
  103. assert not args.resize_input
  104. data = np.load(path)
  105. assert isinstance(data, np.ndarray)
  106. else:
  107. # load image succeeds, so we expect input format is image format
  108. data = auto_reformat_image(args, path, data, dst_shape)
  109. data = np.repeat(data, repeat, axis=0)
  110. if repeat > 1:
  111. logger.info(
  112. "repeat input for {} times, data shape is {}".format(repeat, data.shape)
  113. )
  114. check_shape_equal(dst_shape, data.shape)
  115. if args.input_transform:
  116. data = eval(args.input_transform, {"data": data, "np": np})
  117. return data
  118. def gen_one_testcase(args, inputs, spec):
  119. paths = spec.split(";")
  120. if len(paths) != len(inputs):
  121. if len(paths) == 1 and paths[0].startswith("#"):
  122. paths = ["{}:{}".format(name, paths[0]) for name in inputs.keys()]
  123. assert len(paths) == len(inputs), "required inputs: {}; data paths: {}".format(
  124. inputs.keys(), paths
  125. )
  126. if len(paths) == 1 and ":" not in paths[0]:
  127. paths[0] = next(iter(inputs.keys())) + ":" + paths[0]
  128. ret = {}
  129. for path in paths:
  130. var, path = path.split(":")
  131. if args.repeat:
  132. repeat = args.repeat
  133. else:
  134. repeat = 1
  135. ret[var] = read_input_data(
  136. args, inputs[var].shape, inputs[var].dtype, path, repeat
  137. )
  138. return ret
  139. def make_feeds(args):
  140. ret = G.load_graph(args.input)
  141. cg_rt, outputs = ret.graph, ret.output_vars_list
  142. inputs = cgtools.get_dep_vars(outputs, "Host2DeviceCopy")
  143. inputs = {i.name: i for i in inputs}
  144. if not args.no_assert:
  145. replace_varmap = {}
  146. inp_map = {}
  147. # replace var use InputNode
  148. for name, var in inputs.items():
  149. inp = G.InputNode(
  150. device="xpux", dtype=var.dtype, shape=var.shape, graph=cg_rt
  151. )
  152. replace_varmap[var] = inp.outputs[0]
  153. inp_map[name] = inp
  154. new = cgtools.replace_vars(outputs, replace_varmap)
  155. if isinstance(new, rt.VarNode):
  156. new = list(new)
  157. output_nodes = [G.OutputNode(var) for var in new]
  158. func = cg_rt.compile([node.outputs[0] for node in output_nodes])
  159. def make_dev_tensor(value, dtype=None, device=None):
  160. return tensor(value, dtype=dtype, device=device)._dev_tensor()
  161. def calculate(*args, **kwargs):
  162. output_val = []
  163. # set inputs value
  164. for name, var in inputs.items():
  165. val = kwargs.pop(name, None)
  166. assert val is not None, "miss input name{}".format(name)
  167. dev_tensor = make_dev_tensor(val, dtype=var.dtype, device="xpux")
  168. inp_map[name].set_value(dev_tensor)
  169. func.execute()
  170. for res in output_nodes:
  171. output_val.append(res.get_value().numpy())
  172. return output_val
  173. def expect_name(var):
  174. return "{}:expect".format(var.name)
  175. testcases = []
  176. np.set_printoptions(precision=2, threshold=4, suppress=True)
  177. data_list = []
  178. for item in args.data:
  179. if item.startswith("@"):
  180. with open(item[1:], "r") as f:
  181. data_list.extend([line.rstrip() for line in f if line.rstrip() != ""])
  182. else:
  183. data_list.append(item)
  184. for inp_spec in data_list:
  185. cur_testcase = gen_one_testcase(args, inputs, inp_spec)
  186. assert len(cur_testcase) == len(
  187. inputs
  188. ), "required inputs: {}; given data: {}".format(
  189. inputs.keys(), cur_testcase.keys()
  190. )
  191. if not args.no_assert:
  192. outputs_get = calculate(**cur_testcase)
  193. for var, val in zip(outputs, outputs_get):
  194. cur_testcase[expect_name(var)] = val
  195. logger.info(
  196. "generate test groundtruth: var={} shape={} range=({}, {})"
  197. " mean={} var={}".format(
  198. var, val.shape, val.min(), val.max(), np.mean(val), np.var(val)
  199. )
  200. )
  201. testcases.append(cur_testcase)
  202. logger.info(
  203. "add testcase: \n {}".format(
  204. "\n ".join(
  205. "{}: shape={} dtype={} range=({:.2f},{:.2f}) "
  206. "mean={:.2f} sd={:.2f}".format(
  207. k, v.shape, v.dtype, v.min(), v.max(), np.mean(v), np.std(v)
  208. )
  209. for k, v in sorted(cur_testcase.items())
  210. )
  211. )
  212. )
  213. if not args.no_assert:
  214. def expect_shp(var):
  215. ret = var.shape
  216. if ret:
  217. return ret
  218. return testcases[0][expect_name(var)].shape
  219. def assert_equal(expect, real, **kwargs):
  220. op = builtin.AssertEqual(**kwargs)
  221. (res,) = G.apply_normal_varnode(op, expect, real)
  222. return res
  223. verbose = not args.silent
  224. outputs_new = []
  225. for i in outputs:
  226. device = rt.CompNode("xpux")
  227. dtype = i.dtype
  228. name = expect_name(i)
  229. shape = expect_shp(i)
  230. # make expect output as one input of model.
  231. expect_get = rt.make_h2d(cg_rt, device, dtype, shape, name)
  232. # insert assert opr to check expect and real.
  233. outputs_new.append(
  234. assert_equal(
  235. expect_get,
  236. i,
  237. verbose=verbose,
  238. maxerr=args.maxerr,
  239. )
  240. )
  241. inputs[expect_name(i)] = expect_get
  242. outputs = outputs_new
  243. return {"outputs": outputs, "testcases": testcases}
  244. def optimize_for_inference(args, outputs):
  245. args_list = [
  246. "enable_io16xc32",
  247. "enable_ioc16",
  248. "enable_hwcd4",
  249. "enable_nchw4",
  250. "enable_nchw88",
  251. "enable_nchw44",
  252. "enable_nchw44_dot",
  253. "enable_nchw32",
  254. "enable_chwn4",
  255. "enable_fuse_conv_bias_nonlinearity",
  256. "enable_fuse_conv_bias_with_z",
  257. "enable_fuse_preprocess",
  258. ]
  259. kwargs = {}
  260. for k in args_list:
  261. if getattr(args, k):
  262. kwargs[k] = True
  263. if args.optimize_for_inference:
  264. outputs = G.optimize_for_inference(outputs, **kwargs)
  265. return outputs
  266. def main():
  267. parser = argparse.ArgumentParser(
  268. description="Pack computing graph, input values and expected output "
  269. "values into one file for checking correctness. README.md gives more "
  270. "details on the usage",
  271. formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  272. )
  273. parser.add_argument("input", help="MegEngine dumped model file")
  274. parser.add_argument("-o", "--output", help="output file", required=True)
  275. parser.add_argument(
  276. "-d",
  277. "--data",
  278. default=[],
  279. action="append",
  280. required=True,
  281. help="Given input test data when input file is a network, "
  282. "and current network output would be used as groundtruth. "
  283. "The format is var0:file0;var1:file1... to specify data files for "
  284. "input vars. It can also be #rand(min,max,shape...) for generating "
  285. "random input data, for example, #rand(0,255), "
  286. "#rand(0,255,1,3,224,224) or #rand(0, 255, 1, ...) where `...` means "
  287. "the remaining part of the original shape. "
  288. "If the shape is not specified, the shape of "
  289. "corresponding input tensors in the network will be used. "
  290. "If there is only one input var, its name can be omitted. "
  291. "Each data file can either be an image which can be loaded by opencv, "
  292. "or a pickled numpy.ndarray. "
  293. "This option can be given multiple times to add multiple testcases. "
  294. " *NOTE* "
  295. "If you start the data with the letter @, the rest should be a "
  296. "filename, and each line in the file should be a single datum in "
  297. "the format described above. ",
  298. )
  299. parser.add_argument(
  300. "--repeat",
  301. type=int,
  302. default=1,
  303. help="Specify how many times the input image is repeated. "
  304. "Useful when running benchmark for batch size other than one. "
  305. "Have no effect on randomly generated input data.",
  306. )
  307. parser.add_argument(
  308. "--silent",
  309. action="store_true",
  310. help="set verbose to False in asserti_equal opr",
  311. )
  312. parser.add_argument(
  313. "--optimize-for-inference",
  314. action="store_true",
  315. help="enable optimization for inference",
  316. )
  317. parser.add_argument(
  318. "--no-assert",
  319. action="store_true",
  320. help="do not insert assert_equal opr to check result; "
  321. "this option is useful for benchmarking",
  322. )
  323. parser.add_argument(
  324. "--maxerr",
  325. type=float,
  326. default=1e-4,
  327. help="max error for assert_equal check during runtime",
  328. )
  329. parser.add_argument(
  330. "--resize-input",
  331. action="store_true",
  332. help="resize input image to fit input var shape",
  333. )
  334. parser.add_argument(
  335. "--input-transform",
  336. help="a python expression to transform the input data. "
  337. "Example: data / np.std(data)",
  338. )
  339. parser.add_argument(
  340. "--discard-var-name",
  341. action="store_true",
  342. help="discard variable and param names in the " "generated output",
  343. )
  344. parser.add_argument(
  345. "--output-strip-info", action="store_true", help="output code strip information"
  346. )
  347. parser.add_argument(
  348. "--enable-io16xc32",
  349. action="store_true",
  350. help="transform the mode to float16 io float32 compute",
  351. )
  352. parser.add_argument(
  353. "--enable-ioc16",
  354. action="store_true",
  355. help="transform the dtype of the model to float16 io " "and compute",
  356. )
  357. parser.add_argument(
  358. "--enable-fuse-conv-bias-nonlinearity",
  359. action="store_true",
  360. help="fuse convolution bias and nonlinearity opr to a "
  361. "conv_bias opr and compute",
  362. )
  363. parser.add_argument(
  364. "--enable-hwcd4",
  365. action="store_true",
  366. help="transform the model format from NCHW to NHWCD4 "
  367. "for inference; you may need to disable CUDA and set "
  368. "MGB_USE_MEGDNN_DBG=2",
  369. )
  370. parser.add_argument(
  371. "--enable-nchw4",
  372. action="store_true",
  373. help="transform the model format from NCHW to NCHW4 " "for inference",
  374. )
  375. parser.add_argument(
  376. "--enable-nchw88",
  377. action="store_true",
  378. help="transform the model format from NCHW to NCHW88 " "for inference",
  379. )
  380. parser.add_argument(
  381. "--enable-nchw44",
  382. action="store_true",
  383. help="transform the model format from NCHW to NCHW44 " "for inference",
  384. )
  385. parser.add_argument(
  386. "--enable-nchw44-dot",
  387. action="store_true",
  388. help="transform the model format from NCHW to NCHW44_DOT "
  389. "for optimizing armv8.2 dot in inference",
  390. )
  391. parser.add_argument(
  392. "--enable-nchw32",
  393. action="store_true",
  394. help="transform the model format from NCHW4 to NCHW32 "
  395. "for inference on nvidia TensoCore",
  396. )
  397. parser.add_argument(
  398. "--enable-chwn4",
  399. action="store_true",
  400. help="transform the model format to CHWN4 "
  401. "for inference, mainly used for nvidia tensorcore",
  402. )
  403. parser.add_argument(
  404. "--enable-fuse-conv-bias-with-z",
  405. action="store_true",
  406. help="fuse conv_bias with z input for inference on "
  407. "nvidia GPU (this optimization pass will result in mismatch "
  408. "of the precision of output of training and inference)",
  409. )
  410. parser.add_argument(
  411. "--enable-fuse-preprocess",
  412. action="store_true",
  413. help="fuse astype\pad_channel\dimshuffle and etc opr "
  414. "from h2d opr",
  415. )
  416. args = parser.parse_args()
  417. feeds = make_feeds(args)
  418. assert isinstance(feeds, dict) and feeds["testcases"], "testcases can not be empty"
  419. output_mgbvars = feeds["outputs"]
  420. output_mgbvars = optimize_for_inference(args, output_mgbvars)
  421. inputs = cgtools.get_dep_vars(output_mgbvars, "Host2DeviceCopy")
  422. inputs = sorted((i.name, i.dtype) for i in inputs)
  423. if args.discard_var_name:
  424. sereg_kwargs = dict(keep_var_name=0, keep_param_name=False)
  425. else:
  426. sereg_kwargs = dict(keep_var_name=2, keep_param_name=True)
  427. strip_info_file = args.output + ".json" if args.output_strip_info else None
  428. with open(args.output, "wb") as fout:
  429. fout.write(b"mgbtest0")
  430. fout.write(struct.pack("I", len(feeds["testcases"])))
  431. dump_content, stat = G.dump_graph(
  432. output_mgbvars,
  433. append_json=True,
  434. strip_info_file=strip_info_file,
  435. **sereg_kwargs,
  436. )
  437. fout.write(dump_content)
  438. logger.info(
  439. "graph dump sizes: tot_size={:.3f}KiB overhead={:.3f}KiB".format(
  440. stat.tot_bytes / 1024, (stat.tot_bytes - stat.tensor_value_bytes) / 1024
  441. )
  442. )
  443. def make_dev_tensor(value, dtype=None, device=None):
  444. return tensor(value, dtype=dtype, device=device)._dev_tensor()
  445. for testcase in feeds["testcases"]:
  446. assert isinstance(testcase, dict)
  447. cg = G.Graph()
  448. output_mgbvars = []
  449. for name, dtype in inputs:
  450. output_mgbvars.append(
  451. cg.make_const(
  452. make_dev_tensor(testcase.pop(name), dtype=dtype, device="cpux")
  453. )
  454. )
  455. assert not testcase, "extra inputs provided in testcase: {}".format(
  456. testcase.keys()
  457. )
  458. with open(args.output, "ab") as fout:
  459. dump_content, _ = G.dump_graph(
  460. output_mgbvars, strip_info_file=strip_info_file, append_json=True
  461. )
  462. fout.write(dump_content)
  463. if __name__ == "__main__":
  464. main()