"...composable_kernel_rocm.git" did not exist on "5dee1b6498a664700d3344b878c08d935c9e2535"
HowToLaunchFromPython.rst 11.1 KB
Newer Older
kvartet's avatar
kvartet committed
1
2
How to Launch an Experiment from Python
=======================================
3

4
.. code-block::
5

6
7
8
9
10
    ..  toctree::
        :hidden:

        Start Usage <python_api_start>
        Connect Usage <python_api_connect>
11

12
13
Overview
--------
kvartet's avatar
kvartet committed
14
15

Since ``v2.0``, NNI provides a new way to launch the experiments. Before that, you need to configure the experiment in the YAML configuration file and then use the ``nnictl`` command to launch the experiment. Now, you can also configure and run experiments directly in the Python file. If you are familiar with Python programming, this will undoubtedly bring you more convenience.
16

17
18
Run a New Experiment
--------------------
19

kvartet's avatar
kvartet committed
20
After successfully installing ``nni`` and prepare the `trial code <../TrialExample/Trials.rst>`__, you can start the experiment with a Python script in the following 2 steps.
21

kvartet's avatar
kvartet committed
22
23
Step 1 - Initialize an experiment instance and configure it
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24
25
26

.. code-block:: python

27
28
    from nni.experiment import Experiment
    experiment = Experiment('local')
29

30
Now, you have a ``Experiment`` instance, and this experiment will launch trials on your local machine due to ``training_service='local'``.
31
32
33
34
35

See all `training services <../training_services.rst>`__ supported in NNI.

.. code-block:: python

36
    experiment.config.experiment_name = 'MNIST example'
37
    experiment.config.trial_concurrency = 2
38
    experiment.config.max_trial_number = 10
39
40
41
    experiment.config.search_space = search_space
    experiment.config.trial_command = 'python3 mnist.py'
    experiment.config.trial_code_directory = Path(__file__).parent
42
43
    experiment.config.tuner.name = 'TPE'
    experiment.config.tuner.class_args['optimize_mode'] = 'maximize'
44
45
46
47
    experiment.config.training_service.use_active_gpu = True

Use the form like ``experiment.config.foo = 'bar'`` to configure your experiment.

48
49
See all real `builtin tuners <../builtin_tuner.rst>`__ supported in NNI.

kvartet's avatar
kvartet committed
50
See `configuration reference <../reference/experiment_config.rst>`__ for more detailed usage of these fields.
51
52


kvartet's avatar
kvartet committed
53
54
Step 2 - Just run
^^^^^^^^^^^^^^^^^
55
56
57

.. code-block:: python

58
    experiment.run(port=8080)
59

60
Now, you have successfully launched an NNI experiment. And you can type ``localhost:8080`` in your browser to observe your experiment in real time.
61

kvartet's avatar
kvartet committed
62
63
64
In this way, experiment will run in the foreground and will automatically exit when the experiment finished. 

.. Note:: If you want to run an experiment in an interactive way, use ``start()`` in Step 2. If you launch the experiment in Python script, please use ``run()``, as ``start()`` is designed for the interactive scenarios.
65

66
Example
67
^^^^^^^
kvartet's avatar
kvartet committed
68
69

Below is an example for this new launching approach. You can find this code in :githublink:`mnist-tfv2/launch.py <examples/trials/mnist-tfv2/launch.py>`.
70
71
72
73
74

.. code-block:: python

    from pathlib import Path

75
    from nni.experiment import Experiment
76
77
78
79
80
81
82
83
84

    search_space = {
        "dropout_rate": { "_type": "uniform", "_value": [0.5, 0.9] },
        "conv_size": { "_type": "choice", "_value": [2, 3, 5, 7] },
        "hidden_size": { "_type": "choice", "_value": [124, 512, 1024] },
        "batch_size": { "_type": "choice", "_value": [16, 32] },
        "learning_rate": { "_type": "choice", "_value": [0.0001, 0.001, 0.01, 0.1] }
    }

85
86
    experiment = Experiment('local')
    experiment.config.experiment_name = 'MNIST example'
87
    experiment.config.trial_concurrency = 2
88
    experiment.config.max_trial_number = 10
89
90
91
    experiment.config.search_space = search_space
    experiment.config.trial_command = 'python3 mnist.py'
    experiment.config.trial_code_directory = Path(__file__).parent
92
93
    experiment.config.tuner.name = 'TPE'
    experiment.config.tuner.class_args['optimize_mode'] = 'maximize'
94
95
    experiment.config.training_service.use_active_gpu = True

96
    experiment.run(8080)
97

kvartet's avatar
kvartet committed
98

99
100
Start and Manage a New Experiment
---------------------------------
kvartet's avatar
kvartet committed
101
102

NNI migrates the API in ``NNI Client`` to this new launching approach. Launch the experiment by ``start()`` instead of ``run()``, then you can use these APIs in interactive mode.
103
104
105
106
107

Please refer to `example usage <./python_api_start.rst>`__ and code file :githublink:`python_api_start.ipynb <examples/trials/sklearn/classification/python_api_start.ipynb>`.

.. Note:: ``run()`` polls the experiment status and will automatically call ``stop()`` when the experiment finished. ``start()`` just launched a new experiment, so you need to manually stop the experiment by calling ``stop()``.

kvartet's avatar
kvartet committed
108

109
110
Connect and Manage an Exist Experiment
--------------------------------------
kvartet's avatar
kvartet committed
111
112

If you launch an experiment by ``nnictl`` and also want to use these APIs, you can use ``Experiment.connect()`` to connect to an existing experiment.
113
114
115
116
117

Please refer to `example usage <./python_api_connect.rst>`__ and code file :githublink:`python_api_connect.ipynb <examples/trials/sklearn/classification/python_api_connect.ipynb>`.

.. Note:: You can use ``stop()`` to stop the experiment when connecting to an existing experiment.

118
119
120
121
122
Resume/View and Manage a Stopped Experiment
-------------------------------------------

You can use ``Experiment.resume()`` and ``Experiment.view()`` to resume and view a stopped experiment, these functions behave like ``nnictl resume`` and ``nnictl view``.

kvartet's avatar
kvartet committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
If you want to manage the experiment, set ``wait_completion`` as ``False`` and the functions will return an ``Experiment`` instance. For more parameters, please refer to API reference.


API Reference
-------------

Detailed usage could be found `here <../reference/experiment_config.rst>`__. 

* `Experiment`_
* `Experiment Config <#Experiment-Config>`_
* `Algorithm Config <#Algorithm-Config>`_
* `Training Service Config <#Training-Service-Config>`_
  * `Local Config <#Local-Config>`_ 
  * `Remote Config <#Remote-Config>`_
  * `Openpai Config <#Openpai-Config>`_
  * `AML Config <#AML-Config>`_
* `Shared Storage Config <Shared-Storage-Config>`_


Experiment
^^^^^^^^^^
144
145
146

..  autoclass:: nni.experiment.Experiment
    :members:
kvartet's avatar
kvartet committed
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


Experiment Config
^^^^^^^^^^^^^^^^^

..  autoattribute:: nni.experiment.config.ExperimentConfig.experiment_name

..  autoattribute:: nni.experiment.config.ExperimentConfig.search_space_file

..  autoattribute:: nni.experiment.config.ExperimentConfig.search_space

..  autoattribute:: nni.experiment.config.ExperimentConfig.trial_command

..  autoattribute:: nni.experiment.config.ExperimentConfig.trial_code_directory

..  autoattribute:: nni.experiment.config.ExperimentConfig.trial_concurrency

..  autoattribute:: nni.experiment.config.ExperimentConfig.trial_gpu_number

..  autoattribute:: nni.experiment.config.ExperimentConfig.max_experiment_duration

..  autoattribute:: nni.experiment.config.ExperimentConfig.max_trial_number

..  autoattribute:: nni.experiment.config.ExperimentConfig.nni_manager_ip

..  autoattribute:: nni.experiment.config.ExperimentConfig.use_annotation

..  autoattribute:: nni.experiment.config.ExperimentConfig.debug

..  autoattribute:: nni.experiment.config.ExperimentConfig.log_level

..  autoattribute:: nni.experiment.config.ExperimentConfig.experiment_working_directory

..  autoattribute:: nni.experiment.config.ExperimentConfig.tuner_gpu_indices

..  autoattribute:: nni.experiment.config.ExperimentConfig.tuner

..  autoattribute:: nni.experiment.config.ExperimentConfig.assessor

..  autoattribute:: nni.experiment.config.ExperimentConfig.advisor

..  autoattribute:: nni.experiment.config.ExperimentConfig.training_service

..  autoattribute:: nni.experiment.config.ExperimentConfig.shared_storage


Algorithm Config
^^^^^^^^^^^^^^^^

..  autoattribute:: nni.experiment.config.AlgorithmConfig.name

..  autoattribute:: nni.experiment.config.AlgorithmConfig.class_args

..  autoattribute:: nni.experiment.config.CustomAlgorithmConfig.class_name

..  autoattribute:: nni.experiment.config.CustomAlgorithmConfig.code_directory

..  autoattribute:: nni.experiment.config.CustomAlgorithmConfig.class_args


Training Service Config
^^^^^^^^^^^^^^^^^^^^^^^

Local Config
************

..  autoattribute:: nni.experiment.config.LocalConfig.platform

..  autoattribute:: nni.experiment.config.LocalConfig.use_active_gpu

..  autoattribute:: nni.experiment.config.LocalConfig.max_trial_number_per_gpu

..  autoattribute:: nni.experiment.config.LocalConfig.gpu_indices

Remote Config
*************

..  autoattribute:: nni.experiment.config.RemoteConfig.platform

..  autoattribute:: nni.experiment.config.RemoteConfig.reuse_mode

..  autoattribute:: nni.experiment.config.RemoteConfig.machine_list

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.host

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.port

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.user

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.password

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.ssh_key_file

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.ssh_passphrase

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.use_active_gpu

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.max_trial_number_per_gpu

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.gpu_indices

..  autoattribute:: nni.experiment.config.RemoteMachineConfig.python_path


Openpai Config
**************

..  autoattribute:: nni.experiment.config.OpenpaiConfig.platform

..  autoattribute:: nni.experiment.config.OpenpaiConfig.host

..  autoattribute:: nni.experiment.config.OpenpaiConfig.username

..  autoattribute:: nni.experiment.config.OpenpaiConfig.token

..  autoattribute:: nni.experiment.config.OpenpaiConfig.trial_cpu_number

..  autoattribute:: nni.experiment.config.OpenpaiConfig.trial_memory_size

..  autoattribute:: nni.experiment.config.OpenpaiConfig.storage_config_name

..  autoattribute:: nni.experiment.config.OpenpaiConfig.docker_image

..  autoattribute:: nni.experiment.config.OpenpaiConfig.local_storage_mount_point

..  autoattribute:: nni.experiment.config.OpenpaiConfig.container_storage_mount_point

..  autoattribute:: nni.experiment.config.OpenpaiConfig.reuse_mode

..  autoattribute:: nni.experiment.config.OpenpaiConfig.openpai_config

..  autoattribute:: nni.experiment.config.OpenpaiConfig.openpai_config_file

AML Config
**********

..  autoattribute:: nni.experiment.config.AmlConfig.platform

..  autoattribute:: nni.experiment.config.AmlConfig.subscription_id

..  autoattribute:: nni.experiment.config.AmlConfig.resource_group

..  autoattribute:: nni.experiment.config.AmlConfig.workspace_name

..  autoattribute:: nni.experiment.config.AmlConfig.compute_target

..  autoattribute:: nni.experiment.config.AmlConfig.docker_image

..  autoattribute:: nni.experiment.config.AmlConfig.max_trial_number_per_gpu


Shared Storage Config
^^^^^^^^^^^^^^^^^^^^^

Nfs Config
**********

..  autoattribute:: nni.experiment.config.NfsConfig.storage_type

..  autoattribute:: nni.experiment.config.NfsConfig.nfs_server

..  autoattribute:: nni.experiment.config.NfsConfig.exported_directory

Azure Blob Config
*****************

..  autoattribute:: nni.experiment.config.AzureBlobConfig.storage_type

..  autoattribute:: nni.experiment.config.AzureBlobConfig.storage_account_name

..  autoattribute:: nni.experiment.config.AzureBlobConfig.storage_account_key

..  autoattribute:: nni.experiment.config.AzureBlobConfig.container_name