Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkSnail committed Aug 11, 2020
1 parent aa708ca commit 0080891
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
10 changes: 5 additions & 5 deletions docs/en_US/Tutorial/Nnictl.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ Debug mode will disable version check function in Trialkeeper.
|------|------|------ |------|
|id| True| |The id of the experiment you want to save|
|--path, -p| False| |the folder path to store nni experiment data, default current working directory|
|--saveCodeDir, -s| | |save codeDir data of the experiment, default False|
|--saveCodeDir, -s| False| |save codeDir data of the experiment, default False|

* Examples

Expand All @@ -558,7 +558,7 @@ Debug mode will disable version check function in Trialkeeper.
* __nnictl experiment load__
* Description

Load a nni experiment.
Load an nni experiment.

* Usage

Expand All @@ -570,9 +570,9 @@ Debug mode will disable version check function in Trialkeeper.

|Name, shorthand|Required|Default|Description|
|------|------|------ |------|
|--path, -p| True| |the full file path of nni package|
|--codeDir, -c| True| |the path of codeDir|
|--logDir, -l| False| |the path of logDir|
|--path, -p| True| |the file path of nni package|
|--codeDir, -c| True| |the path of codeDir for loaded experiment, this path will also put the code in the loaded experiment package|
|--logDir, -l| False| |the path of logDir for loaded experiment|

* Examples

Expand Down
13 changes: 13 additions & 0 deletions tools/nni_cmd/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import os
import sys
import json
import tempfile
import socket
import string
import random
import ruamel.yaml as yaml
import psutil
from colorama import Fore
Expand Down Expand Up @@ -83,3 +86,13 @@ def check_tensorboard_version():
print_error('import tensorboard error!')
exit(1)

def generate_temp_dir():
'''generate a temp folder'''
def generate_folder_name():
return os.path.join(tempfile.gettempdir(), 'nni', ''.join(random.sample(string.ascii_letters + string.digits, 8)))
temp_dir = generate_folder_name()
while os.path.exists(temp_dir):
temp_dir = generate_folder_name()
os.makedirs(temp_dir, exist_ok=True)
return temp_dir

2 changes: 2 additions & 0 deletions tools/nni_cmd/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

NNICTL_HOME_DIR = os.path.join(os.path.expanduser('~'), '.local', 'nnictl')

NNI_HOME_DIR = os.path.join(os.path.expanduser('~'), 'nni-experiments')

ERROR_INFO = 'ERROR: '
NORMAL_INFO = 'INFO: '
WARNING_INFO = 'WARNING: '
Expand Down
25 changes: 13 additions & 12 deletions tools/nni_cmd/nnictl.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,21 @@ def parse_args():
parser_trial_export.add_argument('--type', '-t', choices=['json', 'csv'], required=True, dest='type', help='target file type')
parser_trial_export.add_argument('--filename', '-f', required=True, dest='path', help='target file path')
parser_trial_export.set_defaults(func=export_trials_data)
#save experiment data
parser_save_data = parser_experiment_subparsers.add_parser('save', help='save experiment data')
parser_save_data.add_argument('id', nargs='?', help='the id of experiment')
parser_save_data.add_argument('--path', '-p', required=False, help='the folder path to store nni experiment data, \
#save an NNI experiment
parser_save_experiment = parser_experiment_subparsers.add_parser('save', help='save an experiment')
parser_save_experiment.add_argument('id', nargs='?', help='the id of experiment')
parser_save_experiment.add_argument('--path', '-p', required=False, help='the folder path to store nni experiment data, \
default current working directory')
parser_save_data.add_argument('--saveCodeDir', '-s', action='store_true', default=False, help='save codeDir data \
parser_save_experiment.add_argument('--saveCodeDir', '-s', action='store_true', default=False, help='save codeDir data \
of the experiment')
parser_save_data.set_defaults(func=save_experiment)
#open experiment data
parser_open_data = parser_experiment_subparsers.add_parser('load', help='load experiment data')
parser_open_data.add_argument('--path', '-p', required=True, help='the path of nni package file')
parser_open_data.add_argument('--codeDir', '-c', required=True, help='the path of codeDir')
parser_open_data.add_argument('--logDir', '-l', required=False, help='the path of logDir')
parser_open_data.set_defaults(func=load_experiment)
parser_save_experiment.set_defaults(func=save_experiment)
#load an NNI experiment
parser_load_experiment = parser_experiment_subparsers.add_parser('load', help='load an experiment')
parser_load_experiment.add_argument('--path', '-p', required=True, help='the path of nni package file')
parser_load_experiment.add_argument('--codeDir', '-c', required=True, help='the path of codeDir for loaded experiment, \
this path will also put the code in the loaded experiment package')
parser_load_experiment.add_argument('--logDir', '-l', required=False, help='the path of logDir for loaded experiment')
parser_load_experiment.set_defaults(func=load_experiment)

#parse platform command
parser_platform = subparsers.add_parser('platform', help='get platform information')
Expand Down
20 changes: 10 additions & 10 deletions tools/nni_cmd/nnictl_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import os
import sys
import json
import tempfile
import time
import random
import re
import string
import shutil
import subprocess
from datetime import datetime, timezone
Expand All @@ -21,9 +18,9 @@
from .rest_utils import rest_get, rest_delete, check_rest_server_quick, check_response
from .url_utils import trial_jobs_url, experiment_url, trial_job_id_url, export_data_url
from .config_utils import Config, Experiments
from .constants import NNICTL_HOME_DIR, EXPERIMENT_INFORMATION_FORMAT, EXPERIMENT_DETAIL_FORMAT, \
from .constants import NNICTL_HOME_DIR, NNI_HOME_DIR, EXPERIMENT_INFORMATION_FORMAT, EXPERIMENT_DETAIL_FORMAT, \
EXPERIMENT_MONITOR_INFO, TRIAL_MONITOR_HEAD, TRIAL_MONITOR_CONTENT, TRIAL_MONITOR_TAIL, REST_TIME_OUT
from .common_utils import print_normal, print_error, print_warning, detect_process, get_yml_content
from .common_utils import print_normal, print_error, print_warning, detect_process, get_yml_content, generate_temp_dir
from .command_utils import check_output_command, kill_command
from .ssh_utils import create_ssh_sftp_client, remove_remote_directory

Expand Down Expand Up @@ -745,17 +742,20 @@ def save_experiment(args):
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if args.id is None:
print_error('please set experiment id.')
print_error('Please set experiment id.')
exit(1)
if args.id not in experiment_dict:
print_error('Cannot find experiment {0}.'.format(args.id))
exit(1)
if experiment_dict[args.id].get('status') != 'STOPPED':
print_error('Can only save stopped experiment!')
exit(1)
print_normal('Saving...')
nni_config = Config(experiment_dict[args.id]['fileName'])
logDir = os.path.join(os.path.expanduser("~"), 'nni-experiments', args.id)
logDir = os.path.join(NNI_HOME_DIR, args.id)
if nni_config.get_config('logDir'):
logDir = os.path.join(nni_config.get_config('logDir'), args.id)
temp_root_dir = os.path.join(tempfile.gettempdir(), 'nni', ''.join(random.sample(string.ascii_letters + string.digits, 8)))
temp_root_dir = generate_temp_dir()

# Step1. Copy logDir to temp folder
if not os.path.exists(logDir):
Expand Down Expand Up @@ -799,7 +799,7 @@ def load_experiment(args):
if not os.path.exists(args.path):
print_error('file path %s does not exist!' % args.path)
exit(1)
temp_root_dir = os.path.join(tempfile.gettempdir(), 'nni', ''.join(random.sample(string.ascii_letters + string.digits, 8)))
temp_root_dir = generate_temp_dir()
shutil.unpack_archive(package_path, temp_root_dir)
print_normal('Opening...')
# Step1. Validation
Expand Down Expand Up @@ -867,7 +867,7 @@ def load_experiment(args):
# Step4. Copy code dir
codeDir = os.path.expanduser(args.codeDir)
if not os.path.isabs(codeDir):
codeDir = os.path.join(os. getcwd(), codeDir)
codeDir = os.path.join(os.getcwd(), codeDir)
print_normal('Expand codeDir to %s' % codeDir)
nnictl_exp_config['trial']['codeDir'] = codeDir
archive_code_dir = os.path.join(temp_root_dir, 'code')
Expand Down

0 comments on commit 0080891

Please sign in to comment.