main.ts 6.18 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5
6
7

'use strict';

import { Container, Scope } from 'typescript-ioc';

8
import * as fs from 'fs';
9
import * as component from './common/component';
Deshui Yu's avatar
Deshui Yu committed
10
11
import { Database, DataStore } from './common/datastore';
import { setExperimentStartupInfo } from './common/experimentStartupInfo';
chicm-ms's avatar
chicm-ms committed
12
import { getLogger, Logger, logLevelNameMap } from './common/log';
SparkSnail's avatar
SparkSnail committed
13
import { Manager, ExperimentStartUpMode } from './common/manager';
Deshui Yu's avatar
Deshui Yu committed
14
import { TrainingService } from './common/trainingService';
15
import { getLogDir, mkDirP, parseArg, uniqueString } from './common/utils';
Deshui Yu's avatar
Deshui Yu committed
16
17
18
import { NNIDataStore } from './core/nniDataStore';
import { NNIManager } from './core/nnimanager';
import { SqlDB } from './core/sqlDatabase';
19
import { NNIRestServer } from './rest_server/nniRestServer';
20
21
22
23
import { FrameworkControllerTrainingService } from './training_service/kubernetes/frameworkcontroller/frameworkcontrollerTrainingService';
import { KubeflowTrainingService } from './training_service/kubernetes/kubeflow/kubeflowTrainingService';
import { LocalTrainingService } from './training_service/local/localTrainingService';
import { PAITrainingService } from './training_service/pai/paiTrainingService';
Deshui Yu's avatar
Deshui Yu committed
24
25
26
27
import {
    RemoteMachineTrainingService
} from './training_service/remote_machine/remoteMachineTrainingService';

28
29
function initStartupInfo(
    startExpMode: string, resumeExperimentId: string, basePort: number,
SparkSnail's avatar
SparkSnail committed
30
31
    logDirectory: string, experimentLogLevel: string, readonly: boolean): void {
    const createNew: boolean = (startExpMode === ExperimentStartUpMode.NEW);
Deshui Yu's avatar
Deshui Yu committed
32
    const expId: string = createNew ? uniqueString(8) : resumeExperimentId;
SparkSnail's avatar
SparkSnail committed
33
    setExperimentStartupInfo(createNew, expId, basePort, logDirectory, experimentLogLevel, readonly);
Deshui Yu's avatar
Deshui Yu committed
34
35
}

SparkSnail's avatar
SparkSnail committed
36
async function initContainer(platformMode: string, logFileName?: string): Promise<void> {
Deshui Yu's avatar
Deshui Yu committed
37
    if (platformMode === 'local') {
38
39
40
        Container.bind(TrainingService)
            .to(LocalTrainingService)
            .scope(Scope.Singleton);
Deshui Yu's avatar
Deshui Yu committed
41
    } else if (platformMode === 'remote') {
42
43
44
        Container.bind(TrainingService)
            .to(RemoteMachineTrainingService)
            .scope(Scope.Singleton);
45
    } else if (platformMode === 'pai') {
46
47
48
        Container.bind(TrainingService)
            .to(PAITrainingService)
            .scope(Scope.Singleton);
49
    } else if (platformMode === 'kubeflow') {
50
51
52
        Container.bind(TrainingService)
            .to(KubeflowTrainingService)
            .scope(Scope.Singleton);
53
    } else if (platformMode === 'frameworkcontroller') {
54
55
56
57
        Container.bind(TrainingService)
            .to(FrameworkControllerTrainingService)
            .scope(Scope.Singleton);
    } else {
Deshui Yu's avatar
Deshui Yu committed
58
59
        throw new Error(`Error: unsupported mode: ${mode}`);
    }
60
61
62
63
64
65
66
67
68
    Container.bind(Manager)
        .to(NNIManager)
        .scope(Scope.Singleton);
    Container.bind(Database)
        .to(SqlDB)
        .scope(Scope.Singleton);
    Container.bind(DataStore)
        .to(NNIDataStore)
        .scope(Scope.Singleton);
SparkSnail's avatar
SparkSnail committed
69
70
71
    Container.bind(Logger).provider({
        get: (): Logger => new Logger(logFileName)
    });
Deshui Yu's avatar
Deshui Yu committed
72
73
74
75
76
77
    const ds: DataStore = component.get(DataStore);

    await ds.init();
}

function usage(): void {
78
79
    console.info('usage: node main.js --port <port> --mode \
    <local/remote/pai/kubeflow/frameworkcontroller> --start_mode <new/resume> --experiment_id <id>');
Deshui Yu's avatar
Deshui Yu committed
80
81
82
}

const strPort: string = parseArg(['--port', '-p']);
goooxu's avatar
goooxu committed
83
84
85
if (!strPort || strPort.length === 0) {
    usage();
    process.exit(1);
Deshui Yu's avatar
Deshui Yu committed
86
87
}

goooxu's avatar
goooxu committed
88
89
const port: number = parseInt(strPort, 10);

Deshui Yu's avatar
Deshui Yu committed
90
const mode: string = parseArg(['--mode', '-m']);
91
if (!['local', 'remote', 'pai', 'kubeflow', 'frameworkcontroller'].includes(mode)) {
92
    console.log(`FATAL: unknown mode: ${mode}`);
Deshui Yu's avatar
Deshui Yu committed
93
94
95
96
97
    usage();
    process.exit(1);
}

const startMode: string = parseArg(['--start_mode', '-s']);
SparkSnail's avatar
SparkSnail committed
98
if (![ExperimentStartUpMode.NEW, ExperimentStartUpMode.RESUME].includes(startMode)) {
99
    console.log(`FATAL: unknown start_mode: ${startMode}`);
Deshui Yu's avatar
Deshui Yu committed
100
101
102
103
104
    usage();
    process.exit(1);
}

const experimentId: string = parseArg(['--experiment_id', '-id']);
SparkSnail's avatar
SparkSnail committed
105
106
if ((startMode === ExperimentStartUpMode.RESUME) && experimentId.trim().length < 1) {
    console.log(`FATAL: cannot resume the experiment, invalid experiment_id: ${experimentId}`);
Deshui Yu's avatar
Deshui Yu committed
107
108
109
110
    usage();
    process.exit(1);
}

111
112
113
114
115
116
117
118
const logDir: string = parseArg(['--log_dir', '-ld']);
if (logDir.length > 0) {
    if (!fs.existsSync(logDir)) {
        console.log(`FATAL: log_dir ${logDir} does not exist`);
    }
}

const logLevel: string = parseArg(['--log_level', '-ll']);
chicm-ms's avatar
chicm-ms committed
119
if (logLevel.length > 0 && !logLevelNameMap.has(logLevel)) {
120
121
122
    console.log(`FATAL: invalid log_level: ${logLevel}`);
}

SparkSnail's avatar
SparkSnail committed
123
124
125
126
127
128
129
130
131
const readonlyArg: string = parseArg(['--readonly', '-r']);
if (!('true' || 'false').includes(readonlyArg.toLowerCase())) {
    console.log(`FATAL: readonly property should only be true or false`);
    usage();
    process.exit(1);
}
const readonly = readonlyArg.toLowerCase() == 'true' ? true : false;

initStartupInfo(startMode, experimentId, port, logDir, logLevel, readonly);
Deshui Yu's avatar
Deshui Yu committed
132

133
134
mkDirP(getLogDir())
    .then(async () => {
Deshui Yu's avatar
Deshui Yu committed
135
136
    try {
        await initContainer(mode);
137
        const restServer: NNIRestServer = component.get(NNIRestServer);
138
        await restServer.start();
SparkSnail's avatar
SparkSnail committed
139
        const log: Logger = getLogger();
Deshui Yu's avatar
Deshui Yu committed
140
141
        log.info(`Rest server listening on: ${restServer.endPoint}`);
    } catch (err) {
SparkSnail's avatar
SparkSnail committed
142
        const log: Logger = getLogger();
Deshui Yu's avatar
Deshui Yu committed
143
        log.error(`${err.stack}`);
144
        throw err;
Deshui Yu's avatar
Deshui Yu committed
145
    }
146
147
})
.catch((err: Error) => {
Deshui Yu's avatar
Deshui Yu committed
148
149
    console.error(`Failed to create log dir: ${err.stack}`);
});
150

151
152
153
154
155
156
157
158
159
160
function getStopSignal(): any {
    if (process.platform === "win32") {
        return 'SIGBREAK';
    }
    else{
        return 'SIGTERM';
    }
}

process.on(getStopSignal(), async () => {
161
    const log: Logger = getLogger();
SparkSnail's avatar
SparkSnail committed
162
    let hasError: boolean = false;
163
    try {
SparkSnail's avatar
SparkSnail committed
164
165
166
167
168
169
        const nniManager: Manager = component.get(Manager);
        await nniManager.stopExperiment();
        const ds: DataStore = component.get(DataStore);
        await ds.close();
        const restServer: NNIRestServer = component.get(NNIRestServer);
        await restServer.stop();
170
    } catch (err) {
SparkSnail's avatar
SparkSnail committed
171
172
        hasError = true;
        log.error(`${err.stack}`);
173
    } finally {
SparkSnail's avatar
SparkSnail committed
174
        await log.close();
175
        process.exit(hasError ? 1 : 0);
SparkSnail's avatar
SparkSnail committed
176
    }
177
});