BenchmarksExample.ipynb 13.6 KB
Newer Older
Yuge Zhang's avatar
Yuge Zhang committed
1
{
2
  "cells": [
Yuge Zhang's avatar
Yuge Zhang committed
3
    {
4
5
6
7
8
9
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Example Usages of NAS Benchmarks"
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
10
    {
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pprint\n",
        "import time\n",
        "\n",
        "from nni.nas.benchmarks.nasbench101 import query_nb101_trial_stats\n",
        "from nni.nas.benchmarks.nasbench201 import query_nb201_trial_stats\n",
        "from nni.nas.benchmarks.nds import query_nds_trial_stats\n",
        "\n",
        "ti = time.time()"
      ]
    },
26
    {
27
28
29
30
31
32
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## NAS-Bench-101"
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
33
    {
34
35
36
37
38
39
40
41
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the following architecture as an example:\n",
        "\n",
        "![nas-101](../../img/nas-bench-101-example.png)"
      ]
    },
42
    {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "arch = {\n",
        "    'op1': 'conv3x3-bn-relu',\n",
        "    'op2': 'maxpool3x3',\n",
        "    'op3': 'conv3x3-bn-relu',\n",
        "    'op4': 'conv3x3-bn-relu',\n",
        "    'op5': 'conv1x1-bn-relu',\n",
        "    'input1': [0],\n",
        "    'input2': [1],\n",
        "    'input3': [2],\n",
        "    'input4': [0],\n",
        "    'input5': [0, 3, 4],\n",
        "    'input6': [2, 5]\n",
        "}\n",
        "for t in query_nb101_trial_stats(arch, 108, include_intermediates=True):\n",
        "    pprint.pprint(t)"
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
67
    {
68
69
70
71
72
73
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "An architecture of NAS-Bench-101 could be trained more than once. Each element of the returned generator is a dict which contains one of the training results of this trial config (architecture + hyper-parameters) including train/valid/test accuracy, training time, number of epochs, etc. The results of NAS-Bench-201 and NDS follow similar formats."
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
74
    {
75
76
77
78
79
80
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## NAS-Bench-201"
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
81
    {
82
83
84
85
86
87
88
89
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the following architecture as an example:\n",
        "\n",
        "![nas-201](../../img/nas-bench-201-example.png)"
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
90
    {
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "arch = {\n",
        "    '0_1': 'avg_pool_3x3',\n",
        "    '0_2': 'conv_1x1',\n",
        "    '1_2': 'skip_connect',\n",
        "    '0_3': 'conv_1x1',\n",
        "    '1_3': 'skip_connect',\n",
        "    '2_3': 'skip_connect'\n",
        "}\n",
        "for t in query_nb201_trial_stats(arch, 200, 'cifar100'):\n",
        "    pprint.pprint(t)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Intermediate results are also available."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "for t in query_nb201_trial_stats(arch, None, 'imagenet16-120', include_intermediates=True):\n",
        "    print(t['config'])\n",
        "    print('Intermediates:', len(t['intermediates']))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## NDS"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the following architecture as an example:<br>\n",
        "![nds](../../img/nas-bench-nds-example.png)\n",
        "\n",
        "Here, `bot_muls`, `ds`, `num_gs`, `ss` and `ws` stand for \"bottleneck multipliers\", \"depths\", \"number of groups\", \"strides\" and \"widths\" respectively."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "model_spec = {\n",
        "    'bot_muls': [0.0, 0.25, 0.25, 0.25],\n",
        "    'ds': [1, 16, 1, 4],\n",
        "    'num_gs': [1, 2, 1, 2],\n",
        "    'ss': [1, 1, 2, 2],\n",
        "    'ws': [16, 64, 128, 16]\n",
        "}\n",
        "# Use none as a wildcard\n",
        "for t in query_nds_trial_stats('residual_bottleneck', None, None, model_spec, None, 'cifar10'):\n",
        "    pprint.pprint(t)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "model_spec = {\n",
        "    'bot_muls': [0.0, 0.25, 0.25, 0.25],\n",
        "    'ds': [1, 16, 1, 4],\n",
        "    'num_gs': [1, 2, 1, 2],\n",
        "    'ss': [1, 1, 2, 2],\n",
        "    'ws': [16, 64, 128, 16]\n",
        "}\n",
        "for t in query_nds_trial_stats('residual_bottleneck', None, None, model_spec, None, 'cifar10', include_intermediates=True):\n",
        "    pprint.pprint(t['intermediates'][:10])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "model_spec = {'ds': [1, 12, 12, 12], 'ss': [1, 1, 2, 2], 'ws': [16, 24, 24, 40]}\n",
        "for t in query_nds_trial_stats('residual_basic', 'resnet', 'random', model_spec, {}, 'cifar10'):\n",
        "    pprint.pprint(t)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "# get the first one\n",
        "pprint.pprint(next(query_nds_trial_stats('vanilla', None, None, None, None, None)))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "# count number\n",
        "model_spec = {'num_nodes_normal': 5, 'num_nodes_reduce': 5, 'depth': 12, 'width': 32, 'aux': False, 'drop_prob': 0.0}\n",
        "cell_spec = {\n",
        "    'normal_0_op_x': 'avg_pool_3x3',\n",
        "    'normal_0_input_x': 0,\n",
        "    'normal_0_op_y': 'conv_7x1_1x7',\n",
        "    'normal_0_input_y': 1,\n",
        "    'normal_1_op_x': 'sep_conv_3x3',\n",
        "    'normal_1_input_x': 2,\n",
        "    'normal_1_op_y': 'sep_conv_5x5',\n",
        "    'normal_1_input_y': 0,\n",
        "    'normal_2_op_x': 'dil_sep_conv_3x3',\n",
        "    'normal_2_input_x': 2,\n",
        "    'normal_2_op_y': 'dil_sep_conv_3x3',\n",
        "    'normal_2_input_y': 2,\n",
        "    'normal_3_op_x': 'skip_connect',\n",
        "    'normal_3_input_x': 4,\n",
        "    'normal_3_op_y': 'dil_sep_conv_3x3',\n",
        "    'normal_3_input_y': 4,\n",
        "    'normal_4_op_x': 'conv_7x1_1x7',\n",
        "    'normal_4_input_x': 2,\n",
        "    'normal_4_op_y': 'sep_conv_3x3',\n",
        "    'normal_4_input_y': 4,\n",
        "    'normal_concat': [3, 5, 6],\n",
        "    'reduce_0_op_x': 'avg_pool_3x3',\n",
        "    'reduce_0_input_x': 0,\n",
        "    'reduce_0_op_y': 'dil_sep_conv_3x3',\n",
        "    'reduce_0_input_y': 1,\n",
        "    'reduce_1_op_x': 'sep_conv_3x3',\n",
        "    'reduce_1_input_x': 0,\n",
        "    'reduce_1_op_y': 'sep_conv_3x3',\n",
        "    'reduce_1_input_y': 0,\n",
        "    'reduce_2_op_x': 'skip_connect',\n",
        "    'reduce_2_input_x': 2,\n",
        "    'reduce_2_op_y': 'sep_conv_7x7',\n",
        "    'reduce_2_input_y': 0,\n",
        "    'reduce_3_op_x': 'conv_7x1_1x7',\n",
        "    'reduce_3_input_x': 4,\n",
        "    'reduce_3_op_y': 'skip_connect',\n",
        "    'reduce_3_input_y': 4,\n",
        "    'reduce_4_op_x': 'conv_7x1_1x7',\n",
        "    'reduce_4_input_x': 0,\n",
        "    'reduce_4_op_y': 'conv_7x1_1x7',\n",
        "    'reduce_4_input_y': 5,\n",
        "    'reduce_concat': [3, 6]\n",
        "}\n",
        "\n",
        "for t in query_nds_trial_stats('nas_cell', None, None, model_spec, cell_spec, 'cifar10'):\n",
        "    assert t['config']['model_spec'] == model_spec\n",
        "    assert t['config']['cell_spec'] == cell_spec\n",
        "    pprint.pprint(t)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "# count number\n",
        "print('NDS (amoeba) count:', len(list(query_nds_trial_stats(None, 'amoeba', None, None, None, None, None))))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## NLP"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      },
      "source": [
        "Use the following two architectures as examples. \n",
        "The arch in the paper is called \"receipe\" with nested variable, and now it is nunested in the benchmarks for NNI.\n",
        "An arch has multiple Node, Node_input_n and Node_op, you can refer to doc for more details.\n",
        "\n",
        "arch1 : <img src=\"../../img/nas-bench-nlp-example1.jpeg\" width=400 height=300 /> \n",
        "\n",
        "\n",
        "arch2 : <img src=\"../../img/nas-bench-nlp-example2.jpeg\" width=400 height=300 /> \n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'config': {'arch': {'h_new_0_input_0': 'node_3',\n                     'h_new_0_input_1': 'node_2',\n                     'h_new_0_input_2': 'node_1',\n                     'h_new_0_op': 'blend',\n                     'node_0_input_0': 'x',\n                     'node_0_input_1': 'h_prev_0',\n                     'node_0_op': 'linear',\n                     'node_1_input_0': 'node_0',\n                     'node_1_op': 'activation_tanh',\n                     'node_2_input_0': 'h_prev_0',\n                     'node_2_input_1': 'node_1',\n                     'node_2_input_2': 'x',\n                     'node_2_op': 'linear',\n                     'node_3_input_0': 'node_2',\n                     'node_3_op': 'activation_leaky_relu'},\n            'dataset': 'ptb',\n            'id': 20003},\n 'id': 16291,\n 'test_loss': 4.680262297102549,\n 'train_loss': 4.132040537087838,\n 'training_time': 177.05208373069763,\n 'val_loss': 4.707944253177966}\n"
          ]
        }
      ],
      "source": [
        "import pprint\n",
        "from nni.nas.benchmarks.nlp import query_nlp_trial_stats\n",
        "\n",
        "arch1 = {'h_new_0_input_0': 'node_3', 'h_new_0_input_1': 'node_2', 'h_new_0_input_2': 'node_1', 'h_new_0_op': 'blend', 'node_0_input_0': 'x', 'node_0_input_1': 'h_prev_0', 'node_0_op': 'linear','node_1_input_0': 'node_0', 'node_1_op': 'activation_tanh', 'node_2_input_0': 'h_prev_0', 'node_2_input_1': 'node_1', 'node_2_input_2': 'x', 'node_2_op': 'linear', 'node_3_input_0': 'node_2', 'node_3_op': 'activation_leaky_relu'}\n",
        "for i in query_nlp_trial_stats(arch=arch1, dataset=\"ptb\"):\n",
        "    pprint.pprint(i)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[{'current_epoch': 46,\n  'id': 1796,\n  'test_loss': 6.233430054978619,\n  'train_loss': 6.4866799231542664,\n  'training_time': 146.5680329799652,\n  'val_loss': 6.326836978687959},\n {'current_epoch': 47,\n  'id': 1797,\n  'test_loss': 6.2402057403023825,\n  'train_loss': 6.485401405247535,\n  'training_time': 146.05511450767517,\n  'val_loss': 6.3239741605870865},\n {'current_epoch': 48,\n  'id': 1798,\n  'test_loss': 6.351145308363877,\n  'train_loss': 6.611281181173992,\n  'training_time': 145.8849437236786,\n  'val_loss': 6.436160816865809},\n {'current_epoch': 49,\n  'id': 1799,\n  'test_loss': 6.227155079159031,\n  'train_loss': 6.473414458249545,\n  'training_time': 145.51414465904236,\n  'val_loss': 6.313294354607077}]\n"
          ]
        }
      ],
      "source": [
        "arch2 = {\"h_new_0_input_0\":\"node_0\",\"h_new_0_input_1\":\"node_1\",\"h_new_0_op\":\"elementwise_sum\",\"node_0_input_0\":\"x\",\"node_0_input_1\":\"h_prev_0\",\"node_0_op\":\"linear\",\"node_1_input_0\":\"node_0\",\"node_1_op\":\"activation_tanh\"}\n",
        "for i in query_nlp_trial_stats(arch=arch2, dataset='wikitext-2', include_intermediates=True):\n",
        "    pprint.pprint(i['intermediates'][45:49])"
      ]
    },
Yuge Zhang's avatar
Yuge Zhang committed
351
    {
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "pycharm": {},
        "tags": []
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Elapsed time:  5.60982608795166 seconds\n"
          ]
        }
      ],
      "source": [
        "print('Elapsed time: ', time.time() - ti, 'seconds')"
      ]
Yuge Zhang's avatar
Yuge Zhang committed
370
    }
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
  ],
  "metadata": {
    "file_extension": ".py",
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "name": "python",
      "version": "3.8.5-final"
    },
    "mimetype": "text/x-python",
    "name": "python",
    "npconvert_exporter": "python",
    "orig_nbformat": 2,
    "pygments_lexer": "ipython3",
392
393
    "version": 3
  },
394
395
  "nbformat": 4,
  "nbformat_minor": 2
Yuge Zhang's avatar
Yuge Zhang committed
396
}