"tools/vscode:/vscode.git/clone" did not exist on "8b25fd3e3de3cd9ef8ba4623e5cfdbd488227833"
main.ts 6.93 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 path from 'path';
10
import * as component from './common/component';
Deshui Yu's avatar
Deshui Yu committed
11
12
import { Database, DataStore } from './common/datastore';
import { setExperimentStartupInfo } from './common/experimentStartupInfo';
chicm-ms's avatar
chicm-ms committed
13
import { getLogger, Logger, logLevelNameMap } from './common/log';
SparkSnail's avatar
SparkSnail committed
14
import { Manager, ExperimentStartUpMode } from './common/manager';
15
import { ExperimentManager } from './common/experimentManager';
Deshui Yu's avatar
Deshui Yu committed
16
import { TrainingService } from './common/trainingService';
17
import { getLogDir, mkDirP, parseArg } from './common/utils';
Deshui Yu's avatar
Deshui Yu committed
18
19
20
import { NNIDataStore } from './core/nniDataStore';
import { NNIManager } from './core/nnimanager';
import { SqlDB } from './core/sqlDatabase';
21
import { NNIExperimentsManager } from './core/nniExperimentsManager';
22
import { NNIRestServer } from './rest_server/nniRestServer';
23
import { FrameworkControllerTrainingService } from './training_service/kubernetes/frameworkcontroller/frameworkcontrollerTrainingService';
24
import { AdlTrainingService } from './training_service/kubernetes/adl/adlTrainingService';
25
26
import { KubeflowTrainingService } from './training_service/kubernetes/kubeflow/kubeflowTrainingService';
import { LocalTrainingService } from './training_service/local/localTrainingService';
27
import { RouterTrainingService } from './training_service/reusable/routerTrainingService';
George Cheng's avatar
George Cheng committed
28
import { DLTSTrainingService } from './training_service/dlts/dltsTrainingService';
Deshui Yu's avatar
Deshui Yu committed
29

30

31
function initStartupInfo(
32
    startExpMode: string, experimentId: string, basePort: number, platform: string,
33
    logDirectory: string, experimentLogLevel: string, readonly: boolean, dispatcherPipe: string): void {
SparkSnail's avatar
SparkSnail committed
34
    const createNew: boolean = (startExpMode === ExperimentStartUpMode.NEW);
35
    setExperimentStartupInfo(createNew, experimentId, basePort, platform, logDirectory, experimentLogLevel, readonly, dispatcherPipe);
Deshui Yu's avatar
Deshui Yu committed
36
37
}

38
async function initContainer(foreground: boolean, platformMode: string, logFileName?: string): Promise<void> {
39
    const routerPlatformMode = ['remote', 'pai', 'aml', 'hybrid'];
40
    if (routerPlatformMode.includes(platformMode)) {
41
        Container.bind(TrainingService)
42
            .to(RouterTrainingService)
43
44
            .scope(Scope.Singleton);
    } else if (platformMode === 'local') {
45
46
47
        Container.bind(TrainingService)
            .to(LocalTrainingService)
            .scope(Scope.Singleton);
48
    } else if (platformMode === 'kubeflow') {
49
50
51
        Container.bind(TrainingService)
            .to(KubeflowTrainingService)
            .scope(Scope.Singleton);
52
    } else if (platformMode === 'frameworkcontroller') {
53
54
55
        Container.bind(TrainingService)
            .to(FrameworkControllerTrainingService)
            .scope(Scope.Singleton);
George Cheng's avatar
George Cheng committed
56
57
58
59
    } else if (platformMode === 'dlts') {
        Container.bind(TrainingService)
            .to(DLTSTrainingService)
            .scope(Scope.Singleton);
60
    } else if (platformMode === 'adl') {
SparkSnail's avatar
SparkSnail committed
61
        Container.bind(TrainingService)
62
            .to(AdlTrainingService)
SparkSnail's avatar
SparkSnail committed
63
            .scope(Scope.Singleton);
64
    } else {
chicm-ms's avatar
chicm-ms committed
65
        throw new Error(`Error: unsupported mode: ${platformMode}`);
Deshui Yu's avatar
Deshui Yu committed
66
    }
67
68
69
70
71
72
73
74
75
    Container.bind(Manager)
        .to(NNIManager)
        .scope(Scope.Singleton);
    Container.bind(Database)
        .to(SqlDB)
        .scope(Scope.Singleton);
    Container.bind(DataStore)
        .to(NNIDataStore)
        .scope(Scope.Singleton);
76
77
78
    Container.bind(ExperimentManager)
        .to(NNIExperimentsManager)
        .scope(Scope.Singleton);
79
80
81
82
83
84
    const DEFAULT_LOGFILE: string = path.join(getLogDir(), 'nnimanager.log');
    if (foreground) {
        logFileName = undefined;
    } else if (logFileName === undefined) {
        logFileName = DEFAULT_LOGFILE;
    }
SparkSnail's avatar
SparkSnail committed
85
86
87
    Container.bind(Logger).provider({
        get: (): Logger => new Logger(logFileName)
    });
Deshui Yu's avatar
Deshui Yu committed
88
89
90
91
92
93
    const ds: DataStore = component.get(DataStore);

    await ds.init();
}

function usage(): void {
94
    console.info('usage: node main.js --port <port> --mode \
SparkSnail's avatar
SparkSnail committed
95
    <local/remote/pai/kubeflow/frameworkcontroller/aml/adl/hybrid> --start_mode <new/resume> --experiment_id <id> --foreground <true/false>');
Deshui Yu's avatar
Deshui Yu committed
96
97
98
}

const strPort: string = parseArg(['--port', '-p']);
goooxu's avatar
goooxu committed
99
100
101
if (!strPort || strPort.length === 0) {
    usage();
    process.exit(1);
Deshui Yu's avatar
Deshui Yu committed
102
103
}

104
105
106
107
108
109
110
111
const foregroundArg: string = parseArg(['--foreground', '-f']);
if (!('true' || 'false').includes(foregroundArg.toLowerCase())) {
    console.log(`FATAL: foreground property should only be true or false`);
    usage();
    process.exit(1);
}
const foreground: boolean = foregroundArg.toLowerCase() === 'true' ? true : false;

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

Deshui Yu's avatar
Deshui Yu committed
114
const mode: string = parseArg(['--mode', '-m']);
SparkSnail's avatar
SparkSnail committed
115
if (!['local', 'remote', 'pai', 'kubeflow', 'frameworkcontroller', 'dlts', 'aml', 'adl', 'hybrid'].includes(mode)) {
116
    console.log(`FATAL: unknown mode: ${mode}`);
Deshui Yu's avatar
Deshui Yu committed
117
118
119
120
121
    usage();
    process.exit(1);
}

const startMode: string = parseArg(['--start_mode', '-s']);
SparkSnail's avatar
SparkSnail committed
122
if (![ExperimentStartUpMode.NEW, ExperimentStartUpMode.RESUME].includes(startMode)) {
123
    console.log(`FATAL: unknown start_mode: ${startMode}`);
Deshui Yu's avatar
Deshui Yu committed
124
125
126
127
128
    usage();
    process.exit(1);
}

const experimentId: string = parseArg(['--experiment_id', '-id']);
129
if (experimentId.trim().length < 1) {
SparkSnail's avatar
SparkSnail committed
130
    console.log(`FATAL: cannot resume the experiment, invalid experiment_id: ${experimentId}`);
Deshui Yu's avatar
Deshui Yu committed
131
132
133
134
    usage();
    process.exit(1);
}

135
136
137
138
139
140
141
142
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
143
if (logLevel.length > 0 && !logLevelNameMap.has(logLevel)) {
144
145
146
    console.log(`FATAL: invalid log_level: ${logLevel}`);
}

SparkSnail's avatar
SparkSnail committed
147
148
149
150
151
152
153
154
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;

155
156
157
const dispatcherPipe: string = parseArg(['--dispatcher_pipe']);

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

159
160
mkDirP(getLogDir())
    .then(async () => {
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        try {
            await initContainer(foreground, mode);
            const restServer: NNIRestServer = component.get(NNIRestServer);
            await restServer.start();
            const log: Logger = getLogger();
            log.info(`Rest server listening on: ${restServer.endPoint}`);
        } catch (err) {
            const log: Logger = getLogger();
            log.error(`${err.stack}`);
            throw err;
        }
    })
    .catch((err: Error) => {
        console.error(`Failed to create log dir: ${err.stack}`);
    });
176

177
178
function cleanUp(): void {
    (component.get(Manager) as Manager).stopExperiment();
179
180
181
182
183
}

process.on('SIGTERM', cleanUp);
process.on('SIGBREAK', cleanUp);
process.on('SIGINT', cleanUp);