Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1b5640

Browse files
committed
Improve the correctness_performance tutorial, ignore other files in output
1 parent c310262 commit d1b5640

2 files changed

Lines changed: 78 additions & 39 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.ipynb_checkpoints
2+
/tutorials/output/*
3+
/tutorials/output/*/
4+
!/tutorials/output/README.md

tutorials/CorrectnessVerificationAndPerformanceComparison.ipynb

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 2,
23+
"execution_count": 1,
2424
"metadata": {},
25-
"outputs": [],
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+
],
2635
"source": [
2736
"from __future__ import absolute_import\n",
2837
"from __future__ import division\n",
@@ -33,6 +42,8 @@
3342
"import numpy as np\n",
3443
"import torch\n",
3544
"import onnx\n",
45+
"import torch.nn as nn\n",
46+
"import torch.nn.functional as F\n",
3647
"\n",
3748
"from caffe2.proto import caffe2_pb2\n",
3849
"from caffe2.python import core\n",
@@ -42,24 +53,32 @@
4253
" benchmark_caffe2_model, benchmark_pytorch_model\n",
4354
"\n",
4455
"\n",
45-
"class MyModel(torch.nn.Module):\n",
46-
" '''\n",
47-
" This is simple model for demonstration purpose.\n",
48-
" It requires two 2-D tensors as input,\n",
49-
" and returns the multiply of the two inputs.\n",
50-
" '''\n",
56+
"class MNIST(nn.Module):\n",
57+
"\n",
5158
" def __init__(self):\n",
52-
" super(MyModel, self).__init__()\n",
59+
" super(MNIST, self).__init__()\n",
60+
" self.conv1 = nn.Conv2d(1, 10, kernel_size=5)\n",
61+
" self.conv2 = nn.Conv2d(10, 20, kernel_size=5)\n",
62+
" self.conv2_drop = nn.Dropout2d()\n",
63+
" self.fc1 = nn.Linear(320, 50)\n",
64+
" self.fc2 = nn.Linear(50, 10)\n",
5365
"\n",
54-
" def forward(self, m1, m2):\n",
55-
" return torch.mm(m1, m2)\n",
66+
" def forward(self, x):\n",
67+
" x = F.relu(F.max_pool2d(self.conv1(x), 2))\n",
68+
" x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))\n",
69+
" x = x.view(-1, 320)\n",
70+
" x = F.relu(self.fc1(x))\n",
71+
" x = F.dropout(x, training=self.training)\n",
72+
" x = self.fc2(x)\n",
73+
" return F.log_softmax(x)\n",
5674
"\n",
5775
"\n",
5876
"# Create a pytorch model.\n",
59-
"pytorch_model = MyModel()\n",
77+
"pytorch_model = MNIST()\n",
78+
"pytorch_model.train(False)\n",
6079
"\n",
6180
"# Make the inputs in tuple format.\n",
62-
"inputs = (Variable(torch.randn(3, 4)), Variable(torch.randn(4, 5)))"
81+
"inputs = (Variable(torch.randn(3, 1, 28, 28), requires_grad=True), )"
6382
]
6483
},
6584
{
@@ -71,18 +90,40 @@
7190
},
7291
{
7392
"cell_type": "code",
74-
"execution_count": 3,
93+
"execution_count": 2,
7594
"metadata": {},
7695
"outputs": [
7796
{
7897
"name": "stdout",
7998
"output_type": "stream",
8099
"text": [
81-
"graph(%0 : Float(3, 4)\n",
82-
" %1 : Float(4, 5)) {\n",
83-
" %2 : UNKNOWN_TYPE = Constant[value={0}]()\n",
84-
" %3 : Float(3, 5) = Gemm[alpha=1, beta=0, broadcast=1](%0, %1, %2)\n",
85-
" return (%3);\n",
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",
86127
"}\n",
87128
"\n",
88129
"Check the ONNX model.\n"
@@ -109,7 +150,7 @@
109150
},
110151
{
111152
"cell_type": "code",
112-
"execution_count": 4,
153+
"execution_count": 3,
113154
"metadata": {},
114155
"outputs": [
115156
{
@@ -123,12 +164,7 @@
123164
"source": [
124165
"# Convert the ONNX model to a Caffe2 model.\n",
125166
"print(\"Convert the model to a Caffe2 model.\")\n",
126-
"init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(onnx_model.graph)\n",
127-
"\n",
128-
"# Set the device option.\n",
129-
"device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)\n",
130-
"init_net.device_option.CopyFrom(device_opts)\n",
131-
"predict_net.device_option.CopyFrom(device_opts)"
167+
"init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(onnx_model.graph, device=\"CPU\")"
132168
]
133169
},
134170
{
@@ -140,7 +176,7 @@
140176
},
141177
{
142178
"cell_type": "code",
143-
"execution_count": 5,
179+
"execution_count": 4,
144180
"metadata": {
145181
"collapsed": true
146182
},
@@ -159,7 +195,7 @@
159195
},
160196
{
161197
"cell_type": "code",
162-
"execution_count": 6,
198+
"execution_count": 5,
163199
"metadata": {
164200
"collapsed": true
165201
},
@@ -185,7 +221,7 @@
185221
},
186222
{
187223
"cell_type": "code",
188-
"execution_count": 7,
224+
"execution_count": 6,
189225
"metadata": {
190226
"collapsed": true
191227
},
@@ -208,7 +244,7 @@
208244
},
209245
{
210246
"cell_type": "code",
211-
"execution_count": 8,
247+
"execution_count": 7,
212248
"metadata": {},
213249
"outputs": [
214250
{
@@ -223,8 +259,6 @@
223259
"# Check the decimal precision of the exported Caffe2.\n",
224260
"expected_decimal = 5\n",
225261
"for p, c in zip([pytorch_results], caffe2_results):\n",
226-
" if device_opts.device_type == caffe2_pb2.CUDA:\n",
227-
" p.cpu()\n",
228262
" np.testing.assert_almost_equal(p.data.cpu().numpy(), c, decimal=expected_decimal)\n",
229263
"print(\"The exported model achieves {}-decimal precision.\".format(expected_decimal))"
230264
]
@@ -241,24 +275,26 @@
241275
},
242276
{
243277
"cell_type": "code",
244-
"execution_count": 9,
278+
"execution_count": 8,
245279
"metadata": {},
246280
"outputs": [
247281
{
248282
"name": "stdout",
249283
"output_type": "stream",
250284
"text": [
251-
"PyTorch model's execution time is 0.00998973846436 / iteration.\n",
252-
"Caffe2 model's execution time is 0.00594469998032 / iteration.\n"
285+
"PyTorch model's execution time is 0.637650489807 milliseconds/ iteration, 1568.25724434 iterations per second.\n",
286+
"Caffe2 model's execution time is 0.43121188879 milliseconds / iteration, 2319.04552262 iterations per second\n"
253287
]
254288
}
255289
],
256290
"source": [
257-
"pytorch_times = benchmark_pytorch_model(pytorch_model, inputs)\n",
258-
"caffe2_times = benchmark_caffe2_model(init_net, predict_net)\n",
291+
"pytorch_time = benchmark_pytorch_model(pytorch_model, inputs)\n",
292+
"caffe2_time = benchmark_caffe2_model(init_net, predict_net)\n",
259293
"\n",
260-
"print(\"PyTorch model's execution time is {} / iteration.\".format(pytorch_times[0]))\n",
261-
"print(\"Caffe2 model's execution time is {} / iteration.\".format(caffe2_times[0]))"
294+
"print(\"PyTorch model's execution time is {} milliseconds/ iteration, {} iterations per second.\".format(\n",
295+
" pytorch_time, 1000 / pytorch_time))\n",
296+
"print(\"Caffe2 model's execution time is {} milliseconds / iteration, {} iterations per second\".format(\n",
297+
" caffe2_time, 1000 / caffe2_time))"
262298
]
263299
}
264300
],

0 commit comments

Comments
 (0)