|
| 1 | +import mxnet as mx |
| 2 | +import argparse |
| 3 | +import os, sys |
| 4 | +import time |
| 5 | +import numpy as np |
| 6 | +from mxnet import profiler |
| 7 | + |
| 8 | + |
| 9 | +def parse_args(): |
| 10 | + parser = argparse.ArgumentParser(description='Set network parameters for benchmark test.') |
| 11 | + parser.add_argument('--profile_filename', type=str, default='profile_executor_5iter.json') |
| 12 | + parser.add_argument('--iter_num', type=int, default=5) |
| 13 | + parser.add_argument('--fc1', type=int, default=128) |
| 14 | + parser.add_argument('--fc2', type=int, default=128) |
| 15 | + parser.add_argument('--fc3', type=int, default=128) |
| 16 | + parser.add_argument('--fc4', type=int, default=128) |
| 17 | + return parser.parse_args() |
| 18 | + |
| 19 | + |
| 20 | +def _download(data_dir): |
| 21 | + if not os.path.isdir(data_dir): |
| 22 | + os.system("mkdir " + data_dir) |
| 23 | + os.chdir(data_dir) |
| 24 | + if (not os.path.exists('train-images-idx3-ubyte')) or \ |
| 25 | + (not os.path.exists('train-labels-idx1-ubyte')) or \ |
| 26 | + (not os.path.exists('t10k-images-idx3-ubyte')) or \ |
| 27 | + (not os.path.exists('t10k-labels-idx1-ubyte')): |
| 28 | + os.system("wget http://webdocs.cs.ualberta.ca/~bx3/data/mnist.zip") |
| 29 | + os.system("unzip -u mnist.zip; rm mnist.zip") |
| 30 | + os.chdir("..") |
| 31 | + |
| 32 | + |
| 33 | +def get_data(data_shape): |
| 34 | + data_dir = "mnist/" |
| 35 | + batch_size = 128 |
| 36 | + if '://' not in data_dir: |
| 37 | + _download(data_dir) |
| 38 | + |
| 39 | + train = mx.io.MNISTIter( |
| 40 | + image = data_dir + "train-images-idx3-ubyte", |
| 41 | + label = data_dir + "train-labels-idx1-ubyte", |
| 42 | + input_shape = data_shape, |
| 43 | + batch_size = batch_size, |
| 44 | + shuffle = True, |
| 45 | + ) |
| 46 | + |
| 47 | + val = mx.io.MNISTIter( |
| 48 | + image = data_dir + "t10k-images-idx3-ubyte", |
| 49 | + label = data_dir + "t10k-labels-idx1-ubyte", |
| 50 | + input_shape = data_shape, |
| 51 | + batch_size = batch_size, |
| 52 | + ) |
| 53 | + |
| 54 | + return (train, val) |
| 55 | + |
| 56 | +def get_symbol(): |
| 57 | + data = mx.symbol.Variable('data') |
| 58 | + fc1 = mx.symbol.FullyConnected(data=data, name='fc1', num_hidden=args.fc1) |
| 59 | + act1 = mx.symbol.Activation(data=fc1, name='relu1', act_type='relu') |
| 60 | + fc2 = mx.symbol.FullyConnected(data=act1 , name='fc2', num_hidden=args.fc2) |
| 61 | + act2 = mx.symbol.Activation(data=fc2, name='relu2', act_type='relu') |
| 62 | + fc3 = mx.symbol.FullyConnected(data=act2 , name='fc3', num_hidden=args.fc3) |
| 63 | + act3 = mx.symbol.Activation(data=fc3, name='relu3', act_type='relu') |
| 64 | + fc4 = mx.symbol.FullyConnected(data=act3 , name='fc4', num_hidden=args.fc4) |
| 65 | + act4 = mx.symbol.Activation(data=fc4, name='relu4', act_type='relu') |
| 66 | + fc5 = mx.symbol.FullyConnected(data=act4 , name='fc5', num_hidden=10) |
| 67 | + net = mx.symbol.SoftmaxOutput(data=fc5 , name='softmax') |
| 68 | + return net, [('data', (128, 1, 28, 28))], [('softmax_label', (128, ))] |
| 69 | + |
| 70 | +def get_module(ctx, sym, provide_data, provide_label, batch_size=None, is_train=True, use_memonger=False): |
| 71 | + if use_memonger: |
| 72 | + sym = search_plan(sym, data=data_shapes) |
| 73 | + mod = mx.mod.Module(symbol=sym, |
| 74 | + data_names=[name for name, _ in provide_data], |
| 75 | + label_names=[name for name, _ in provide_label], |
| 76 | + context=ctx) |
| 77 | + if batch_size is not None: |
| 78 | + provide_data = [(name, (batch_size,) + shape[1:]) for name, shape in provide_data] |
| 79 | + provide_label = [(name, (batch_size,) + shape[1:]) for name, shape in provide_label] |
| 80 | + if is_train: |
| 81 | + mod.bind(data_shapes=provide_data, label_shapes=provide_label, for_training=True, inputs_need_grad=False) |
| 82 | + else: |
| 83 | + mod.bind(data_shapes=provide_data, label_shapes=provide_label, for_training=False, inputs_need_grad=False) |
| 84 | + |
| 85 | + mod.init_params(initializer=mx.init.Xavier(magnitude=2.)) |
| 86 | + mod.init_optimizer(optimizer='ccsgd', |
| 87 | + optimizer_params={ |
| 88 | + 'learning_rate': 0.0001, |
| 89 | + 'momentum': 0.0, |
| 90 | + 'wd': 0.0 |
| 91 | + }) |
| 92 | + return mod |
| 93 | + |
| 94 | + |
| 95 | +def benchmark(mod, dry_run=10, iterations=10): |
| 96 | + if len(mod._context) == 1: |
| 97 | + ctx = mod._context[0] |
| 98 | + else: |
| 99 | + ctx = mx.cpu() |
| 100 | + data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx) for _, shape in mod.data_shapes] |
| 101 | + label = [mx.nd.array(np.random.randint(1, 100, size=shape), ctx=ctx) for _, shape in mod.label_shapes] |
| 102 | + batch = mx.io.DataBatch(data, label) |
| 103 | + |
| 104 | + # dry run |
| 105 | + for i in range(dry_run): |
| 106 | + mod.forward(batch, is_train=True) |
| 107 | + mod.backward() |
| 108 | + for output in mod.get_outputs(merge_multi_context=False)[0]: |
| 109 | + output.wait_to_read() |
| 110 | + mod.update() |
| 111 | + |
| 112 | + t0 = time.clock() |
| 113 | + |
| 114 | + profiler.profiler_set_state('run') |
| 115 | + # real run |
| 116 | + for i in range(iterations): |
| 117 | + mod.forward(batch, is_train=True) |
| 118 | + mod.backward() |
| 119 | + mod.update() |
| 120 | + for output in mod.get_outputs(merge_multi_context=False)[0]: |
| 121 | + output.wait_to_read() |
| 122 | + profiler.profiler_set_state('stop') |
| 123 | + |
| 124 | + t1 = time.clock() |
| 125 | + return (t1 - t0)*1000.0 / iterations |
| 126 | + |
| 127 | + |
| 128 | +def executor(num_iteration): |
| 129 | + sym, provide_data, provide_label = get_symbol() |
| 130 | + ctx = [mx.gpu(0)] |
| 131 | + mod = get_module(ctx, sym, provide_data, provide_label, batch_size=128) |
| 132 | + return benchmark(mod, iterations=args.iter_num) |
| 133 | + |
| 134 | + |
| 135 | +args = parse_args() |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + mx.profiler.profiler_set_config(mode='symbolic', filename=args.profile_filename) |
| 139 | + print('profile file save to {0}'.format(args.profile_filename)) |
| 140 | + print('executor num_iteration: {0}'.format(args.iter_num)) |
| 141 | + executor_time = executor(args.iter_num) |
| 142 | + print("executor {0} ms / iteration".format(executor_time)) |
0 commit comments