This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add more models to benchmark_score #12780
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a51ba4b
add models to cnn benchmark
xinyu-intel 6be4b37
improve benchmark score
xinyu-intel 1c583df
add benchmark_gluon
xinyu-intel a3d7926
improve lint
xinyu-intel 1c2fd17
improve lint
xinyu-intel 6725cd1
add licsence for script
xinyu-intel 1cb5290
improve script lint
xinyu-intel 0b17d6f
mv benchmark_gluon to new location
xinyu-intel d5e8a5d
support multi-gpus
xinyu-intel bca8573
Merge remote-tracking branch 'upstream/master' into benchmark_model
xinyu-intel 1298025
Add a new parameter 'global batchsize' for the batch size multiplicat…
9b43b33
Merge remote-tracking branch 'upstream/master' into benchmark_model
xinyu-intel 3983411
add batch size argument help
xinyu-intel 68e8009
improve help and change default batchsize
xinyu-intel ada3038
Merge remote-tracking branch 'upstream/master' into benchmark_model
xinyu-intel e21c8f5
simplify benchmark_gluon
xinyu-intel 0e1d30b
Merge remote-tracking branch 'upstream/master' into benchmark_model
xinyu-intel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import mxnet as mx | ||
| import mxnet.gluon.model_zoo.vision as models | ||
| import time | ||
| import logging | ||
| import argparse | ||
| import subprocess | ||
| import os | ||
| import errno | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
| parser = argparse.ArgumentParser(description='Gluon modelzoo-based CNN performance benchmark') | ||
|
|
||
| parser.add_argument('--model', type=str, default='all', | ||
| choices=['all', 'alexnet', 'densenet121', 'densenet161', | ||
| 'densenet169', 'densenet201', 'inceptionv3', 'mobilenet0.25', | ||
| 'mobilenet0.5', 'mobilenet0.75', 'mobilenet1.0', 'mobilenetv2_0.25', | ||
| 'mobilenetv2_0.5', 'mobilenetv2_0.75', 'mobilenetv2_1.0', 'resnet101_v1', | ||
| 'resnet101_v2', 'resnet152_v1', 'resnet152_v2', 'resnet18_v1', | ||
| 'resnet18_v2', 'resnet34_v1', 'resnet34_v2', 'resnet50_v1', | ||
| 'resnet50_v2', 'squeezenet1.0', 'squeezenet1.1', 'vgg11', | ||
| 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', | ||
| 'vgg19', 'vgg19_bn']) | ||
| parser.add_argument('--batch-size', type=int, default=0, | ||
| help='Batch size to use for benchmarking. Example: 32, 64, 128.' | ||
| 'By default, runs benchmark for batch sizes - 1, 32, 64, 128, 256') | ||
| parser.add_argument('--num-batches', type=int, default=10) | ||
| parser.add_argument('--gpus', type=str, default='', | ||
| help='GPU IDs to use for this benchmark task. Example: --gpus=0,1,2,3 to use 4 GPUs.' | ||
| 'By default, use CPU only.') | ||
| parser.add_argument('--type', type=str, default='inference', choices=['all', 'training', 'inference']) | ||
|
|
||
| opt = parser.parse_args() | ||
|
|
||
| num_batches = opt.num_batches | ||
| dry_run = 10 # use 10 iterations to warm up | ||
| batch_inf = [1, 32, 64, 128, 256] | ||
| batch_train = [1, 32, 64, 128, 256] | ||
| image_shapes = [(3, 224, 224), (3, 299, 299)] | ||
|
|
||
| def score(network, batch_size, ctx): | ||
| assert (batch_size >= len(ctx)), "ERROR: batch size should not be smaller than num of GPUs." | ||
| net = models.get_model(network) | ||
| if 'inceptionv3' == network: | ||
| data_shape = [('data', (batch_size,) + image_shapes[1])] | ||
| else: | ||
| data_shape = [('data', (batch_size,) + image_shapes[0])] | ||
|
|
||
| data = mx.sym.var('data') | ||
| out = net(data) | ||
| softmax = mx.sym.SoftmaxOutput(out, name='softmax') | ||
| mod = mx.mod.Module(softmax, context=ctx) | ||
| mod.bind(for_training = False, | ||
| inputs_need_grad = False, | ||
| data_shapes = data_shape) | ||
| mod.init_params(initializer=mx.init.Xavier(magnitude=2.)) | ||
| data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx[0]) for _, shape in mod.data_shapes] | ||
| batch = mx.io.DataBatch(data, []) | ||
| for i in range(dry_run + num_batches): | ||
| if i == dry_run: | ||
| tic = time.time() | ||
| mod.forward(batch, is_train=False) | ||
| for output in mod.get_outputs(): | ||
| output.wait_to_read() | ||
| fwd = time.time() - tic | ||
| return fwd | ||
|
|
||
|
|
||
| def train(network, batch_size, ctx): | ||
| assert (batch_size >= len(ctx)), "ERROR: batch size should not be smaller than num of GPUs." | ||
| net = models.get_model(network) | ||
| if 'inceptionv3' == network: | ||
| data_shape = [('data', (batch_size,) + image_shapes[1])] | ||
| else: | ||
| data_shape = [('data', (batch_size,) + image_shapes[0])] | ||
|
|
||
| data = mx.sym.var('data') | ||
| out = net(data) | ||
| softmax = mx.sym.SoftmaxOutput(out, name='softmax') | ||
| mod = mx.mod.Module(softmax, context=ctx) | ||
| mod.bind(for_training = True, | ||
| inputs_need_grad = False, | ||
| data_shapes = data_shape) | ||
| mod.init_params(initializer=mx.init.Xavier(magnitude=2.)) | ||
| if len(ctx) > 1: | ||
| mod.init_optimizer(kvstore='device', optimizer='sgd') | ||
| else: | ||
| mod.init_optimizer(kvstore='local', optimizer='sgd') | ||
| data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx[0]) for _, shape in mod.data_shapes] | ||
| batch = mx.io.DataBatch(data, []) | ||
| for i in range(dry_run + num_batches): | ||
| if i == dry_run: | ||
| tic = time.time() | ||
| mod.forward(batch, is_train=True) | ||
| for output in mod.get_outputs(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not mod.forward_backward()?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, will change later:) |
||
| output.wait_to_read() | ||
| mod.backward() | ||
| mod.update() | ||
| bwd = time.time() - tic | ||
| return bwd | ||
|
|
||
| if __name__ == '__main__': | ||
| runtype = opt.type | ||
| bs = opt.batch_size | ||
|
|
||
| if opt.model == 'all': | ||
| networks = ['alexnet', 'densenet121', 'densenet161', 'densenet169', 'densenet201', | ||
| 'inceptionv3', 'mobilenet0.25', 'mobilenet0.5', 'mobilenet0.75', | ||
| 'mobilenet1.0', 'mobilenetv2_0.25', 'mobilenetv2_0.5', 'mobilenetv2_0.75', | ||
| 'mobilenetv2_1.0', 'resnet101_v1', 'resnet101_v2', 'resnet152_v1', 'resnet152_v2', | ||
| 'resnet18_v1', 'resnet18_v2', 'resnet34_v1', 'resnet34_v2', 'resnet50_v1', | ||
| 'resnet50_v2', 'squeezenet1.0', 'squeezenet1.1', 'vgg11', 'vgg11_bn', 'vgg13', | ||
| 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19', 'vgg19_bn'] | ||
| logging.info('It may take some time to run all models, ' | ||
| 'set --network to run a specific one') | ||
| else: | ||
| networks = [opt.model] | ||
|
|
||
| devs = [mx.gpu(int(i)) for i in opt.gpus.split(',')] if opt.gpus.strip() else [mx.cpu()] | ||
| num_gpus = len(devs) | ||
|
|
||
| for network in networks: | ||
| logging.info('network: %s', network) | ||
| logging.info('device: %s', devs) | ||
| if runtype == 'inference' or runtype == 'all': | ||
| if bs != 0: | ||
| fwd_time = score(network, bs, devs) | ||
| fps = (bs * num_batches)/fwd_time | ||
| logging.info(network + ' inference perf for BS %d is %f img/s', bs, fps) | ||
| else: | ||
| logging.info('run batchsize [1, 2, 4, 8, 16, 32] by default, ' | ||
| 'set --batch-size to run a specific one') | ||
| for batch_size in batch_inf: | ||
| fwd_time = score(network, batch_size, devs) | ||
| fps = (batch_size * num_batches) / fwd_time | ||
| logging.info(network + ' inference perf for BS %d is %f img/s', batch_size, fps) | ||
| if runtype == 'training' or runtype == 'all': | ||
| if bs != 0: | ||
| bwd_time = train(network, bs, devs) | ||
| fps = (bs * num_batches) / bwd_time | ||
| logging.info(network + ' training perf for BS %d is %f img/s', bs, fps) | ||
| else: | ||
| logging.info('run batchsize [1, 2, 4, 8, 16, 32] by default, ' | ||
| 'set --batch-size to run a specific one') | ||
| for batch_size in batch_train: | ||
| bwd_time = train(network, batch_size, devs) | ||
| fps = (batch_size * num_batches) / bwd_time | ||
| logging.info(network + ' training perf for BS %d is %f img/s', batch_size, fps) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the default batch sizes be [32, 64, 128, 256] which is more common batch sizes for these networks and typical data loads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, i will change this to [1,32,64,128,256], and batchsize=1 will show the lantency.