Skip to content

Commit 96d7b6c

Browse files
marouenezvinitra
andauthored
Fix benchmark Caffe2 model (#147)
* Fix benchmark Caffe2 model *onnx_graph_to_caffe2_net takes onnx_model as argument and not the graph *Modify bechmark_caffe2_model function to match the same method used in the already built in benchmark_pytorch_model * Update tutorials/CorrectnessVerificationAndPerformanceComparison.ipynb * Update tutorials/CorrectnessVerificationAndPerformanceComparison.ipynb * Update tutorials/CorrectnessVerificationAndPerformanceComparison.ipynb Co-authored-by: Vinitra Swamy <vinitras@gmail.com>
1 parent a8b72dd commit 96d7b6c

1 file changed

Lines changed: 68 additions & 56 deletions

File tree

tutorials/CorrectnessVerificationAndPerformanceComparison.ipynb

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@
2222
"cell_type": "code",
2323
"execution_count": 1,
2424
"metadata": {},
25-
"outputs": [
26-
{
27-
"name": "stderr",
28-
"output_type": "stream",
29-
"text": [
30-
"WARNING:root:This caffe2 python run does not have GPU support. Will run in CPU only mode.\n",
31-
"WARNING:root:Debug message: No module named caffe2_pybind11_state_gpu\n"
32-
]
33-
}
34-
],
25+
"outputs": [],
3526
"source": [
3627
"from __future__ import absolute_import\n",
3728
"from __future__ import division\n",
3829
"from __future__ import print_function\n",
3930
"from __future__ import unicode_literals\n",
4031
"\n",
4132
"import io\n",
33+
"import time\n",
4234
"import numpy as np\n",
4335
"import torch\n",
4436
"import onnx\n",
@@ -49,8 +41,7 @@
4941
"from caffe2.python import core\n",
5042
"from torch.autograd import Variable\n",
5143
"from caffe2.python.onnx.backend import Caffe2Backend\n",
52-
"from caffe2.python.onnx.helper import c2_native_run_net, save_caffe2_net, load_caffe2_net, \\\n",
53-
" benchmark_caffe2_model, benchmark_pytorch_model\n",
44+
"from caffe2.python.onnx.helper import c2_native_run_net, save_caffe2_net, load_caffe2_net,benchmark_pytorch_model\n",
5445
"\n",
5546
"\n",
5647
"class MNIST(nn.Module):\n",
@@ -97,34 +88,28 @@
9788
"name": "stdout",
9889
"output_type": "stream",
9990
"text": [
100-
"graph(%0 : Float(3, 1, 28, 28)\n",
101-
" %1 : Float(10, 1, 5, 5)\n",
102-
" %2 : Float(10)\n",
103-
" %3 : Float(20, 10, 5, 5)\n",
104-
" %4 : Float(20)\n",
105-
" %5 : Float(50, 320)\n",
106-
" %6 : Float(50)\n",
107-
" %7 : Float(10, 50)\n",
108-
" %8 : Float(10)) {\n",
109-
" %9 : UNKNOWN_TYPE = Conv[kernel_shape=[5, 5], strides=[1, 1], pads=[0, 0, 0, 0], dilations=[1, 1], group=1](%0, %1)\n",
110-
" %10 : Float(3, 10, 24, 24) = Add[broadcast=1, axis=1](%9, %2)\n",
111-
" %11 : Float(3, 10, 12, 12) = MaxPool[kernel_shape=[2, 2], pads=[0, 0], strides=[2, 2]](%10)\n",
112-
" %12 : Float(3, 10, 12, 12) = Relu(%11)\n",
113-
" %13 : UNKNOWN_TYPE = Conv[kernel_shape=[5, 5], strides=[1, 1], pads=[0, 0, 0, 0], dilations=[1, 1], group=1](%12, %3)\n",
114-
" %14 : Float(3, 20, 8, 8) = Add[broadcast=1, axis=1](%13, %4)\n",
115-
" %15 : Float(3, 20, 4, 4) = MaxPool[kernel_shape=[2, 2], pads=[0, 0], strides=[2, 2]](%14)\n",
116-
" %16 : Float(3, 20, 4, 4) = Relu(%15)\n",
117-
" %17 : Float(3, 320) = Reshape[shape=[-1, 320]](%16)\n",
118-
" %18 : Float(320!, 50!) = Transpose[perm=[1, 0]](%5)\n",
119-
" %20 : Float(3, 50) = Gemm[alpha=1, beta=1, broadcast=1](%17, %18, %6)\n",
120-
" %21 : Float(3, 50) = Relu(%20)\n",
121-
" %22 : Float(3, 50), %23 : UNKNOWN_TYPE = Dropout[is_test=1, ratio=0.5](%21)\n",
122-
" %24 : Float(50!, 10!) = Transpose[perm=[1, 0]](%7)\n",
123-
" %26 : Float(3, 10) = Gemm[alpha=1, beta=1, broadcast=1](%22, %24, %8)\n",
124-
" %27 : Float(3, 10) = Softmax[axis=1](%26)\n",
125-
" %28 : Float(3, 10) = Log(%27)\n",
126-
" return (%28);\n",
127-
"}\n",
91+
"graph(%input.1 : Float(3, 1, 28, 28),\n",
92+
" %conv1.weight : Float(10, 1, 5, 5),\n",
93+
" %conv1.bias : Float(10),\n",
94+
" %conv2.weight : Float(20, 10, 5, 5),\n",
95+
" %conv2.bias : Float(20),\n",
96+
" %fc1.weight : Float(50, 320),\n",
97+
" %fc1.bias : Float(50),\n",
98+
" %fc2.weight : Float(10, 50),\n",
99+
" %fc2.bias : Float(10)):\n",
100+
" %9 : Float(3, 10, 24, 24) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[5, 5], pads=[0, 0, 0, 0], strides=[1, 1]](%input.1, %conv1.weight, %conv1.bias), scope: MNIST/Conv2d[conv1] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/modules/conv.py:340:0\n",
101+
" %10 : Float(3, 10, 12, 12) = onnx::MaxPool[kernel_shape=[2, 2], pads=[0, 0, 0, 0], strides=[2, 2]](%9), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:487:0\n",
102+
" %11 : Float(3, 10, 12, 12) = onnx::Relu(%10), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:913:0\n",
103+
" %12 : Float(3, 20, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[5, 5], pads=[0, 0, 0, 0], strides=[1, 1]](%11, %conv2.weight, %conv2.bias), scope: MNIST/Dropout2d[conv2_drop] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:844:0\n",
104+
" %13 : Float(3, 20, 4, 4) = onnx::MaxPool[kernel_shape=[2, 2], pads=[0, 0, 0, 0], strides=[2, 2]](%12), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:487:0\n",
105+
" %14 : Float(3, 20, 4, 4) = onnx::Relu(%13), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:913:0\n",
106+
" %15 : Tensor = onnx::Constant[value= -1 320 [ Variable[CPULongType]{2} ]](), scope: MNIST\n",
107+
" %16 : Float(3, 320) = onnx::Reshape(%14, %15), scope: MNIST # <ipython-input-1-c1a2123bd117>:34:0\n",
108+
" %17 : Float(3, 50) = onnx::Gemm[alpha=1, beta=1, transB=1](%16, %fc1.weight, %fc1.bias), scope: MNIST/Linear[fc1] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:1369:0\n",
109+
" %18 : Float(3, 50) = onnx::Relu(%17), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:806:0\n",
110+
" %19 : Float(3, 10) = onnx::Gemm[alpha=1, beta=1, transB=1](%18, %fc2.weight, %fc2.bias), scope: MNIST/Linear[fc2] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:1369:0\n",
111+
" %20 : Float(3, 10) = onnx::LogSoftmax[axis=1](%19), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:1316:0\n",
112+
" return (%20)\n",
128113
"\n",
129114
"Check the ONNX model.\n"
130115
]
@@ -164,7 +149,7 @@
164149
"source": [
165150
"# Convert the ONNX model to a Caffe2 model.\n",
166151
"print(\"Convert the model to a Caffe2 model.\")\n",
167-
"init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(onnx_model.graph, device=\"CPU\")"
152+
"init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(onnx_model, device=\"CPU\")"
168153
]
169154
},
170155
{
@@ -177,9 +162,7 @@
177162
{
178163
"cell_type": "code",
179164
"execution_count": 4,
180-
"metadata": {
181-
"collapsed": true
182-
},
165+
"metadata": {},
183166
"outputs": [],
184167
"source": [
185168
"# Prepare the inputs for Caffe2.\n",
@@ -196,9 +179,7 @@
196179
{
197180
"cell_type": "code",
198181
"execution_count": 5,
199-
"metadata": {
200-
"collapsed": true
201-
},
182+
"metadata": {},
202183
"outputs": [],
203184
"source": [
204185
"# Save the converted Caffe2 model in the protobuf files. (Optional)\n",
@@ -263,6 +244,30 @@
263244
"print(\"The exported model achieves {}-decimal precision.\".format(expected_decimal))"
264245
]
265246
},
247+
{
248+
"cell_type": "code",
249+
"execution_count": 8,
250+
"metadata": {},
251+
"outputs": [],
252+
"source": [
253+
"def benchmark_caffe2_model(init_net, predict_net,inputs, warmup_iters=3, main_iters=10):\n",
254+
" '''\n",
255+
" Run the model several times, and measure the execution time.\n",
256+
" Print the execution time per iteration (millisecond) and the number of iterations per second.\n",
257+
" '''\n",
258+
" for _i in range(warmup_iters):\n",
259+
" ws, caffe2_results = c2_native_run_net(init_net, predict_net, inputs) \n",
260+
"\n",
261+
" total_time = 0.0\n",
262+
" for _i in range(main_iters):\n",
263+
" ts = time.time()\n",
264+
" ws, caffe2_results = c2_native_run_net(init_net, predict_net, inputs)\n",
265+
" te = time.time()\n",
266+
" total_time += te - ts\n",
267+
" \n",
268+
" return total_time / main_iters * 1000"
269+
]
270+
},
266271
{
267272
"cell_type": "markdown",
268273
"metadata": {},
@@ -275,46 +280,53 @@
275280
},
276281
{
277282
"cell_type": "code",
278-
"execution_count": 8,
283+
"execution_count": 9,
279284
"metadata": {},
280285
"outputs": [
281286
{
282287
"name": "stdout",
283288
"output_type": "stream",
284289
"text": [
285-
"PyTorch model's execution time is 0.580716133118 milliseconds/ iteration, 1722.011742 iterations per second.\n",
286-
"Caffe2 model's execution time is 0.399529695511 milliseconds / iteration, 2502.94286316 iterations per second\n"
290+
"PyTorch model's execution time is 0.6218433380126953 milliseconds/ iteration, 1608.1220765278736 iterations per second.\n",
291+
"Caffe2 model's execution time is 3.189969062805176 milliseconds / iteration, 313.48266403581545 iterations per second\n"
287292
]
288293
}
289294
],
290295
"source": [
291296
"pytorch_time = benchmark_pytorch_model(pytorch_model, inputs)\n",
292-
"caffe2_time = benchmark_caffe2_model(init_net, predict_net)\n",
297+
"caffe2_time = benchmark_caffe2_model(init_net, predict_net, caffe2_inputs)\n",
293298
"\n",
294299
"print(\"PyTorch model's execution time is {} milliseconds/ iteration, {} iterations per second.\".format(\n",
295300
" pytorch_time, 1000 / pytorch_time))\n",
296301
"print(\"Caffe2 model's execution time is {} milliseconds / iteration, {} iterations per second\".format(\n",
297302
" caffe2_time, 1000 / caffe2_time))"
298303
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": null,
308+
"metadata": {},
309+
"outputs": [],
310+
"source": []
299311
}
300312
],
301313
"metadata": {
302314
"kernelspec": {
303-
"display_name": "Python 2",
315+
"display_name": "Python 3",
304316
"language": "python",
305-
"name": "python2"
317+
"name": "python3"
306318
},
307319
"language_info": {
308320
"codemirror_mode": {
309321
"name": "ipython",
310-
"version": 2
322+
"version": 3
311323
},
312324
"file_extension": ".py",
313325
"mimetype": "text/x-python",
314326
"name": "python",
315327
"nbconvert_exporter": "python",
316-
"pygments_lexer": "ipython2",
317-
"version": "2.7.13"
328+
"pygments_lexer": "ipython3",
329+
"version": "3.7.4"
318330
}
319331
},
320332
"nbformat": 4,

0 commit comments

Comments
 (0)