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

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

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