LocalMode.rst 6.02 KB
Newer Older
1
**Tutorial: Create and Run an Experiment on local with NNI API**
2
================================================================
3

4
In this tutorial, we will use the example in [nni/examples/trials/mnist-pytorch] to explain how to create and run an experiment on local with NNI API.
5
6
7
8
9
10

..

   Before starts


liuzhe-lz's avatar
liuzhe-lz committed
11
You have an implementation for MNIST classifer using convolutional layers, the Python code is similar to ``mnist.py``.
12
13
14
15
16
17
18
19

..

   Step 1 - Update model codes


To enable NNI API, make the following changes:

20
21
22
1.1 Declare NNI API: include ``import nni`` in your trial code to use NNI APIs.

1.2 Get predefined parameters
23
24
25
26
27

Use the following code snippet:

.. code-block:: python

28
   tuner_params = nni.get_next_parameter()
29

30
to get hyper-parameters' values assigned by tuner. ``tuner_params`` is an object, for example:
31
32
33

.. code-block:: json

34
   {"batch_size": 32, "hidden_size": 128, "lr": 0.01, "momentum": 0.2029}
35

36
37
38
..

1.3 Report NNI results: Use the API: ``nni.report_intermediate_result(accuracy)`` to send ``accuracy`` to assessor. Use the API: ``nni.report_final_result(accuracy)`` to send `accuracy` to tuner.
39
40
41
42
43
44
45

**NOTE**\ :

.. code-block:: bash

   accuracy - The `accuracy` could be any python object, but  if you use NNI built-in tuner/assessor, `accuracy` should be a numerical variable (e.g. float, int).
   tuner    - The tuner will generate next parameters/architecture based on the explore history (final result of all trials).
liuzhe-lz's avatar
liuzhe-lz committed
46
   assessor - The assessor will decide which trial should early stop based on the history performance of trial (intermediate result of one trial).
47
48
49
50
51
52
53
54
55
56

..

   Step 2 - Define SearchSpace


The hyper-parameters used in ``Step 1.2 - Get predefined parameters`` is defined in a ``search_space.json`` file like below:

.. code-block:: bash

57
58
59
60
61
62
    {
        "batch_size": {"_type":"choice", "_value": [16, 32, 64, 128]},
        "hidden_size":{"_type":"choice","_value":[128, 256, 512, 1024]},
        "lr":{"_type":"choice","_value":[0.0001, 0.001, 0.01, 0.1]},
        "momentum":{"_type":"uniform","_value":[0, 1]}
    }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

Refer to `define search space <../Tutorial/SearchSpaceSpec.rst>`__ to learn more about search space.

..

   Step 3 - Define Experiment

   ..

To run an experiment in NNI, you only needed:


* Provide a runnable trial
* Provide or choose a tuner
* Provide a YAML experiment configure file
* (optional) Provide or choose an assessor

**Prepare trial**\ :

..

84
   You can download nni source code and a set of examples can be found in ``nni/examples``, run ``ls nni/examples/trials`` to see all the trial examples.
85
86


liuzhe-lz's avatar
liuzhe-lz committed
87
Let's use a simple trial example, e.g. mnist, provided by NNI. After you cloned NNI source, NNI examples have been put in ~/nni/examples, run ``ls ~/nni/examples/trials`` to see all the trial examples. You can simply execute the following command to run the NNI mnist example:
88
89
90

.. code-block:: bash

liuzhe-lz's avatar
liuzhe-lz committed
91
     python ~/nni/examples/trials/mnist-pytorch/mnist.py
92
93
94
95
96
97
98
99
100


This command will be filled in the YAML configure file below. Please refer to `here <../TrialExample/Trials.rst>`__ for how to write your own trial.

**Prepare tuner**\ : NNI supports several popular automl algorithms, including Random Search, Tree of Parzen Estimators (TPE), Evolution algorithm etc. Users can write their own tuner (refer to `here <../Tuner/CustomizeTuner.rst>`__\ ), but for simplicity, here we choose a tuner provided by NNI as below:

.. code-block:: bash

     tuner:
liuzhe-lz's avatar
liuzhe-lz committed
101
       name: TPE
102
103
104
105
       classArgs:
         optimize_mode: maximize


liuzhe-lz's avatar
liuzhe-lz committed
106
*name* is used to specify a tuner in NNI, *classArgs* are the arguments pass to the tuner (the spec of builtin tuners can be found `here <../Tuner/BuiltinTuner.rst>`__\ ), *optimization_mode* is to indicate whether you want to maximize or minimize your trial's result.
107

liuzhe-lz's avatar
liuzhe-lz committed
108
**Prepare configure file**\ : Since you have already known which trial code you are going to run and which tuner you are going to use, it is time to prepare the YAML configure file. NNI provides a demo configure file for each trial example, ``cat ~/nni/examples/trials/mnist-pytorch/config.yml`` to see it. Its content is basically shown below:
109
110
111

.. code-block:: yaml

liuzhe-lz's avatar
liuzhe-lz committed
112
   experimentName: local training service example
113

liuzhe-lz's avatar
liuzhe-lz committed
114
115
116
   searchSpaceFile ~/nni/examples/trials/mnist-pytorch/search_space.json
   trailCommand: python3 mnist.py
   trialCodeDirectory: ~/nni/examples/trials/mnist-pytorch
117

liuzhe-lz's avatar
liuzhe-lz committed
118
119
120
121
   trialGpuNumber: 0
   trialConcurrency: 1
   maxExperimentDuration: 3h
   maxTrialNumber: 10
122

liuzhe-lz's avatar
liuzhe-lz committed
123
124
   trainingService:
     platform: local
125
126

   tuner:
liuzhe-lz's avatar
liuzhe-lz committed
127
     name: TPE
128
129
130
131
132
133
134
135
     classArgs:
       optimize_mode: maximize


With all these steps done, we can run the experiment with the following command:

.. code-block:: bash

liuzhe-lz's avatar
liuzhe-lz committed
136
     nnictl create --config ~/nni/examples/trials/mnist-pytorch/config.yml
137
138
139
140
141
142
143
144
145
146
147
148


You can refer to `here <../Tutorial/Nnictl.rst>`__ for more usage guide of *nnictl* command line tool.

View experiment results
-----------------------

The experiment has been running now. Other than *nnictl*\ , NNI also provides WebUI for you to view experiment progress, to control your experiment, and some other appealing features.

Using multiple local GPUs to speed up search
--------------------------------------------

liuzhe-lz's avatar
liuzhe-lz committed
149
The following steps assume that you have 4 NVIDIA GPUs installed at local and PyTorch with CUDA support. The demo enables 4 concurrent trail jobs and each trail job uses 1 GPU.
150

liuzhe-lz's avatar
liuzhe-lz committed
151
**Prepare configure file**\ : NNI provides a demo configuration file for the setting above, ``cat ~/nni/examples/trials/mnist-pytorch/config_detailed.yml`` to see it. The trailConcurrency and trialGpuNumber are different from the basic configure file:
152
153
154
155
156

.. code-block:: bash

   ...

liuzhe-lz's avatar
liuzhe-lz committed
157
   trialGpuNumber: 1
158
159
160
161
   trialConcurrency: 4

   ...

liuzhe-lz's avatar
liuzhe-lz committed
162
163
164
165
   trainingService:
     platform: local
     useActiveGpu: false  # set to "true" if you are using graphical OS like Windows 10 and Ubuntu desktop

166
167
168
169
170

We can run the experiment with the following command:

.. code-block:: bash

liuzhe-lz's avatar
liuzhe-lz committed
171
     nnictl create --config ~/nni/examples/trials/mnist-pytorch/config_detailed.yml
172
173
174


You can use *nnictl* command line tool or WebUI to trace the training progress. *nvidia_smi* command line tool can also help you to monitor the GPU usage during training.