Skip to content

Commit

Permalink
Fix eslint errors (microsoft#1836)
Browse files Browse the repository at this point in the history
* update eslint rules
* auto fix eslint
* manually fix eslint (microsoft#1833)
  • Loading branch information
chicm-ms authored Dec 10, 2019
1 parent 8c07cf4 commit 1328f41
Show file tree
Hide file tree
Showing 42 changed files with 325 additions and 334 deletions.
7 changes: 6 additions & 1 deletion src/nni_manager/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-explicit-any": 0
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-namespace": 0,
"@typescript-eslint/consistent-type-assertions": 0,
"@typescript-eslint/no-inferrable-types": 0,
"no-inner-declarations": 0,
"@typescript-eslint/no-var-requires": 0
},
"ignorePatterns": [
"node_modules/",
Expand Down
6 changes: 3 additions & 3 deletions src/nni_manager/common/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ interface TrialJobInfo {

interface HyperParameterFormat {
parameter_source: string;
parameters: Object;
parameters: Record<string, any>;
parameter_id: number;
}

interface ExportedDataFormat {
parameter: Object;
value: Object;
parameter: Record<string, any>;
value: Record<string, any>;
id: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/nni_manager/common/experimentStartupInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class ExperimentStartupInfo {
this.initialized = true;

if (logDir !== undefined && logDir.length > 0) {
this.logDir = path.join(path.normalize(logDir), getExperimentId());
this.logDir = path.join(path.normalize(logDir), this.getExperimentId());
} else {
this.logDir = path.join(os.homedir(), 'nni', 'experiments', getExperimentId());
this.logDir = path.join(os.homedir(), 'nni', 'experiments', this.getExperimentId());
}

if (logLevel !== undefined && logLevel.length > 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/nni_manager/common/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Logger {
this.readonly = isReadonly();
}

public close() {
public close(): void {
this.writable.destroy();
}

Expand Down
2 changes: 1 addition & 1 deletion src/nni_manager/common/observableTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ObservableTimer {
return this.observableSource.subscribe(onNext, onError, onCompleted);
}

public unsubscribe( subscription : Rx.IDisposable) {
public unsubscribe( subscription: Rx.IDisposable): void {
if(typeof subscription !== undefined) {
subscription.dispose();
}
Expand Down
27 changes: 13 additions & 14 deletions src/nni_manager/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import { Container } from 'typescript-ioc';
import * as util from 'util';

import { Database, DataStore } from './datastore';
import { ExperimentStartupInfo, getExperimentId, getExperimentStartupInfo, setExperimentStartupInfo } from './experimentStartupInfo';
import { ExperimentStartupInfo, getExperimentStartupInfo, setExperimentStartupInfo } from './experimentStartupInfo';
import { Manager } from './manager';
import { TrialConfig } from '../training_service/common/trialConfig';
import { HyperParameters, TrainingService, TrialJobStatus } from './trainingService';
import { getLogger } from './log';

function getExperimentRootDir(): string {
return getExperimentStartupInfo()
Expand Down Expand Up @@ -236,11 +234,11 @@ function getMsgDispatcherCommand(tuner: any, assessor: any, advisor: any, multiP
* Generate parameter file name based on HyperParameters object
* @param hyperParameters HyperParameters instance
*/
function generateParamFileName(hyperParameters : HyperParameters): string {
function generateParamFileName(hyperParameters: HyperParameters): string {
assert(hyperParameters !== undefined);
assert(hyperParameters.index >= 0);

let paramFileName : string;
let paramFileName: string;
if(hyperParameters.index == 0) {
paramFileName = 'parameter.cfg';
} else {
Expand Down Expand Up @@ -283,7 +281,7 @@ function cleanupUnitTest(): void {
Container.restore(ExperimentStartupInfo);
}

let cachedipv4Address : string = '';
let cachedipv4Address: string = '';
/**
* Get IPv4 address of current machine
*/
Expand Down Expand Up @@ -325,15 +323,15 @@ function getJobCancelStatus(isEarlyStopped: boolean): TrialJobStatus {
* Utility method to calculate file numbers under a directory, recursively
* @param directory directory name
*/
function countFilesRecursively(directory: string, timeoutMilliSeconds?: number): Promise<number> {
function countFilesRecursively(directory: string): Promise<number> {
if(!fs.existsSync(directory)) {
throw Error(`Direcotory ${directory} doesn't exist`);
}

const deferred: Deferred<number> = new Deferred<number>();

let timeoutId : NodeJS.Timer
const delayTimeout : Promise<number> = new Promise((resolve : Function, reject : Function) : void => {
let timeoutId: NodeJS.Timer
const delayTimeout: Promise<number> = new Promise((resolve: Function, reject: Function): void => {
// Set timeout and reject the promise once reach timeout (5 seconds)
timeoutId = setTimeout(() => {
reject(new Error(`Timeout: path ${directory} has too many files`));
Expand All @@ -359,7 +357,7 @@ function countFilesRecursively(directory: string, timeoutMilliSeconds?: number):
}

function validateFileName(fileName: string): boolean {
let pattern: string = '^[a-z0-9A-Z\._-]+$';
const pattern: string = '^[a-z0-9A-Z._-]+$';
const validateResult = fileName.match(pattern);
if(validateResult) {
return true;
Expand All @@ -374,7 +372,7 @@ async function validateFileNameRecursively(directory: string): Promise<boolean>

const fileNameArray: string[] = fs.readdirSync(directory);
let result = true;
for(var name of fileNameArray){
for(const name of fileNameArray){
const fullFilePath: string = path.join(directory, name);
try {
// validate file names and directory names
Expand All @@ -396,7 +394,7 @@ async function validateFileNameRecursively(directory: string): Promise<boolean>
* get the version of current package
*/
async function getVersion(): Promise<string> {
const deferred : Deferred<string> = new Deferred<string>();
const deferred: Deferred<string> = new Deferred<string>();
import(path.join(__dirname, '..', 'package.json')).then((pkg)=>{
deferred.resolve(pkg.version);
}).catch((error)=>{
Expand Down Expand Up @@ -430,7 +428,7 @@ function getTunerProc(command: string, stdio: StdioOptions, newCwd: string, newE
* judge whether the process is alive
*/
async function isAlive(pid: any): Promise<boolean> {
let deferred : Deferred<boolean> = new Deferred<boolean>();
const deferred: Deferred<boolean> = new Deferred<boolean>();
let alive: boolean = false;
if (process.platform === 'win32') {
try {
Expand All @@ -440,6 +438,7 @@ async function isAlive(pid: any): Promise<boolean> {
}
}
catch (error) {
//ignore
}
}
else {
Expand All @@ -458,7 +457,7 @@ async function isAlive(pid: any): Promise<boolean> {
* kill process
*/
async function killPid(pid: any): Promise<void> {
let deferred : Deferred<void> = new Deferred<void>();
const deferred: Deferred<void> = new Deferred<void>();
try {
if (process.platform === "win32") {
await cpp.exec(`cmd.exe /c taskkill /PID ${pid} /F`);
Expand Down
16 changes: 9 additions & 7 deletions src/nni_manager/core/nniDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class NNIDataStore implements DataStore {

public async exportTrialHpConfigs(): Promise<string> {
const jobs: TrialJobInfo[] = await this.listTrialJobs();
let exportedData: ExportedDataFormat[] = [];
const exportedData: ExportedDataFormat[] = [];
for (const job of jobs) {
if (job.hyperParameters && job.finalMetricData) {
if (job.hyperParameters.length === 1 && job.finalMetricData.length === 1) {
Expand All @@ -172,18 +172,18 @@ class NNIDataStore implements DataStore {
};
exportedData.push(oneEntry);
} else {
let paraMap: Map<number, Object> = new Map();
let metricMap: Map<number, Object> = new Map();
const paraMap: Map<number, Record<string, any>> = new Map();
const metricMap: Map<number, Record<string, any>> = new Map();
for (const eachPara of job.hyperParameters) {
const parameters: HyperParameterFormat = <HyperParameterFormat>JSON.parse(eachPara);
paraMap.set(parameters.parameter_id, parameters.parameters);
}
for (const eachMetric of job.finalMetricData) {
const value: Object = JSON.parse(eachMetric.data);
const value: Record<string, any> = JSON.parse(eachMetric.data);
metricMap.set(Number(eachMetric.parameterId), value);
}
paraMap.forEach((value: Object, key: number) => {
const metricValue: Object | undefined = metricMap.get(key);
paraMap.forEach((value: Record<string, any>, key: number) => {
const metricValue: Record<string, any> | undefined = metricMap.get(key);
if (metricValue) {
const oneEntry: ExportedDataFormat = {
parameter: value,
Expand All @@ -201,7 +201,7 @@ class NNIDataStore implements DataStore {
}

public async getImportedData(): Promise<string[]> {
let importedData: string[] = [];
const importedData: string[] = [];
const importDataEvents: TrialJobEventRecord[] = await this.db.queryTrialJobEvent(undefined, 'IMPORT_DATA');
for (const event of importDataEvents) {
if (event.data) {
Expand Down Expand Up @@ -329,6 +329,7 @@ class NNIDataStore implements DataStore {
if (!jobInfo) {
throw new Error('Empty JobInfo');
}
/* eslint-disable no-fallthrough */
switch (record.event) {
case 'RUNNING':
if (record.timestamp !== undefined) {
Expand Down Expand Up @@ -358,6 +359,7 @@ class NNIDataStore implements DataStore {
}
default:
}
/* eslint-enable no-fallthrough */
jobInfo.status = this.getJobStatusByLatestEvent(jobInfo.status, record.event);
if (record.data !== undefined && record.data.trim().length > 0) {
const newHParam: any = this.parseHyperParameter(record.data);
Expand Down
42 changes: 23 additions & 19 deletions src/nni_manager/core/nnimanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
'use strict';

import * as assert from 'assert';
import * as cpp from 'child-process-promise';
import { ChildProcess, spawn, StdioOptions } from 'child_process';
import { ChildProcess, StdioOptions } from 'child_process';
import { Deferred } from 'ts-deferred';
import * as component from '../common/component';
import { DataStore, MetricDataRecord, MetricType, TrialJobInfo } from '../common/datastore';
Expand All @@ -21,7 +20,7 @@ import {
} from '../common/trainingService';
import { delay, getCheckpointDir, getExperimentRootDir, getLogDir, getMsgDispatcherCommand, mkDirP, getTunerProc, getLogLevel, isAlive, killPid } from '../common/utils';
import {
ADD_CUSTOMIZED_TRIAL_JOB, INITIALIZE, INITIALIZED, KILL_TRIAL_JOB, NEW_TRIAL_JOB, NO_MORE_TRIAL_JOBS, PING,
INITIALIZE, INITIALIZED, KILL_TRIAL_JOB, NEW_TRIAL_JOB, NO_MORE_TRIAL_JOBS, PING,
REPORT_METRIC_DATA, REQUEST_TRIAL_JOBS, SEND_TRIAL_JOB_PARAMETER, TERMINATE, TRIAL_END, UPDATE_SEARCH_SPACE, IMPORT_DATA
} from './commands';
import { createDispatcherInterface, IpcInterface } from './ipcInterface';
Expand Down Expand Up @@ -64,7 +63,7 @@ class NNIManager implements Manager {
status: 'INITIALIZED',
errors: []
};
this.trialJobMetricListener = (metric: TrialJobMetric) => {
this.trialJobMetricListener = (metric: TrialJobMetric): void => {
this.onTrialJobMetrics(metric).catch((err: Error) => {
this.criticalError(NNIError.FromError(err, 'Job metrics error: '));
});
Expand Down Expand Up @@ -123,8 +122,8 @@ class NNIManager implements Manager {

// TODO: NNI manager should not peek tuner's internal protocol, let's refactor this later
const packedParameter = {
parameter_id: null,
parameter_source: 'customized',
parameter_id: null, // eslint-disable-line @typescript-eslint/camelcase
parameter_source: 'customized', // eslint-disable-line @typescript-eslint/camelcase
parameters: JSON.parse(hyperParams)
}

Expand Down Expand Up @@ -235,10 +234,10 @@ class NNIManager implements Manager {
// Collect generated trials and imported trials
const finishedTrialData: string = await this.exportData();
const importedData: string[] = await this.dataStore.getImportedData();
let trialData: Object[] = JSON.parse(finishedTrialData);
let trialData: Record<string, any>[] = JSON.parse(finishedTrialData);
for (const oneImportedData of importedData) {
// do not deduplicate
trialData = trialData.concat(<Object[]>JSON.parse(oneImportedData));
trialData = trialData.concat(<Record<string, any>[]>JSON.parse(oneImportedData));
}
this.trialDataForTuner = JSON.stringify(trialData);

Expand Down Expand Up @@ -361,15 +360,15 @@ class NNIManager implements Manager {
includeIntermediateResultsEnv = this.experimentProfile.params.tuner.includeIntermediateResults;
}

let nniEnv = {
const nniEnv = {
NNI_MODE: mode,
NNI_CHECKPOINT_DIRECTORY: dataDirectory,
NNI_LOG_DIRECTORY: getLogDir(),
NNI_LOG_LEVEL: getLogLevel(),
NNI_INCLUDE_INTERMEDIATE_RESULTS: includeIntermediateResultsEnv,
CUDA_VISIBLE_DEVICES: this.getGpuEnvvarValue()
};
let newEnv = Object.assign({}, process.env, nniEnv);
const newEnv = Object.assign({}, process.env, nniEnv);
const tunerProc: ChildProcess = getTunerProc(command,stdio,newCwd,newEnv);
this.dispatcherPid = tunerProc.pid;
this.dispatcher = createDispatcherInterface(tunerProc);
Expand Down Expand Up @@ -502,9 +501,9 @@ class NNIManager implements Manager {
finishedTrialJobNum++;
hyperParams = trialJobDetail.form.hyperParameters.value;
this.dispatcher.sendCommand(TRIAL_END, JSON.stringify({
trial_job_id: trialJobDetail.id,
trial_job_id: trialJobDetail.id, // eslint-disable-line @typescript-eslint/camelcase
event: trialJobDetail.status,
hyper_params: hyperParams
hyper_params: hyperParams // eslint-disable-line @typescript-eslint/camelcase
}));
break;
case 'FAILED':
Expand All @@ -515,9 +514,9 @@ class NNIManager implements Manager {
finishedTrialJobNum++;
hyperParams = trialJobDetail.form.hyperParameters.value;
this.dispatcher.sendCommand(TRIAL_END, JSON.stringify({
trial_job_id: trialJobDetail.id,
trial_job_id: trialJobDetail.id, // eslint-disable-line @typescript-eslint/camelcase
event: trialJobDetail.status,
hyper_params: hyperParams
hyper_params: hyperParams // eslint-disable-line @typescript-eslint/camelcase
}));
break;
case 'WAITING':
Expand Down Expand Up @@ -695,7 +694,7 @@ class NNIManager implements Manager {
private async onTunerCommand(commandType: string, content: string): Promise<void> {
this.log.info(`NNIManager received command from dispatcher: ${commandType}, ${content}`);
switch (commandType) {
case INITIALIZED:
case INITIALIZED: {
// Tuner is intialized, search space is set, request tuner to generate hyper parameters
if (this.trialDataForTuner.length > 0) {
if (this.dispatcher === undefined) {
Expand All @@ -705,7 +704,8 @@ class NNIManager implements Manager {
}
this.requestTrialJobs(this.experimentProfile.params.trialConcurrency);
break;
case NEW_TRIAL_JOB:
}
case NEW_TRIAL_JOB: {
if (this.status.status === 'TUNER_NO_MORE_TRIAL') {
this.log.warning('It is not supposed to receive more trials after NO_MORE_TRIAL is set');
this.setStatus('RUNNING');
Expand All @@ -719,7 +719,8 @@ class NNIManager implements Manager {
};
this.waitingTrials.push(form);
break;
case SEND_TRIAL_JOB_PARAMETER:
}
case SEND_TRIAL_JOB_PARAMETER: {
const tunerCommand: any = JSON.parse(content);
assert(tunerCommand.parameter_index >= 0);
assert(tunerCommand.trial_job_id !== undefined);
Expand All @@ -739,15 +740,18 @@ class NNIManager implements Manager {
'ADD_HYPERPARAMETER', tunerCommand.trial_job_id, content, undefined);
}
break;
case NO_MORE_TRIAL_JOBS:
}
case NO_MORE_TRIAL_JOBS: {
if (!['ERROR', 'STOPPING', 'STOPPED'].includes(this.status.status)) {
this.setStatus('TUNER_NO_MORE_TRIAL');
}
break;
case KILL_TRIAL_JOB:
}
case KILL_TRIAL_JOB: {
this.log.info(`cancelTrialJob: ${JSON.parse(content)}`);
await this.trainingService.cancelTrialJob(JSON.parse(content), true);
break;
}
default:
throw new Error('Error: unsupported command type from tuner');
}
Expand Down
2 changes: 1 addition & 1 deletion src/nni_manager/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function initContainer(platformMode: string, logFileName?: string): Promis
.to(FrameworkControllerTrainingService)
.scope(Scope.Singleton);
} else {
throw new Error(`Error: unsupported mode: ${mode}`);
throw new Error(`Error: unsupported mode: ${platformMode}`);
}
Container.bind(Manager)
.to(NNIManager)
Expand Down
Loading

0 comments on commit 1328f41

Please sign in to comment.