".github/vscode:/vscode.git/clone" did not exist on "64fceab8afae962ac2f64b6491d873591a58c051"
experiment.ts 5.87 KB
Newer Older
1
2
import { MANAGER_IP } from '../const';
import { ExperimentProfile, NNIManagerStatus } from '../interface';
Lijiaoa's avatar
Lijiaoa committed
3
import { requestAxios } from '../function';
4
import { SearchSpace } from './searchspace';
5
6
7
8
9
10
11
12
13
14
15
16
17

function compareProfiles(profile1?: ExperimentProfile, profile2?: ExperimentProfile): boolean {
    if (!profile1 || !profile2) {
        return false;
    }
    const copy1 = Object.assign({}, profile1, { execDuration: undefined });
    const copy2 = Object.assign({}, profile2, { execDuration: undefined });
    return JSON.stringify(copy1) === JSON.stringify(copy2);
}

class Experiment {
    private profileField?: ExperimentProfile = undefined;
    private statusField?: NNIManagerStatus = undefined;
18
    private isNestedExperiment: boolean = false;
Lijiaoa's avatar
Lijiaoa committed
19
20
21
22
    private isexperimentError: boolean = false;
    private experimentErrorMessage: string = '';
    private isStatusError: boolean = false;
    private statusErrorMessage: string = '';
23
24
25

    public async init(): Promise<void> {
        while (!this.profileField || !this.statusField) {
Lijiaoa's avatar
Lijiaoa committed
26
27
28
29
30
31
            if (this.isexperimentError) {
                return;
            }
            if (this.isStatusError) {
                return;
            }
32
33
34
35
            await this.update();
        }
    }

36
37
38
39
    public isNestedExp(): boolean {
        return this.isNestedExperiment;
    }

Lijiaoa's avatar
Lijiaoa committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    public experimentError(): boolean {
        return this.isexperimentError;
    }

    public statusError(): boolean {
        return this.isStatusError;
    }

    public getExperimentMessage(): string {
        return this.experimentErrorMessage;
    }

    public getStatusMessage(): string {
        return this.statusErrorMessage;
    }

56
57
    public async update(): Promise<boolean> {
        let updated = false;
Lijiaoa's avatar
Lijiaoa committed
58
59
60

        await requestAxios(`${MANAGER_IP}/experiment`)
            .then(data => {
Lijiaoa's avatar
Lijiaoa committed
61
                updated = updated || !compareProfiles(this.profileField, data);
Lijiaoa's avatar
Lijiaoa committed
62
63
64
65
66
67
68
69
70
71
                this.profileField = data;
            })
            .catch(error => {
                this.isexperimentError = true;
                this.experimentErrorMessage = `${error.message}`;
                updated = true;
            });

        await requestAxios(`${MANAGER_IP}/check-status`)
            .then(data => {
Lijiaoa's avatar
Lijiaoa committed
72
                updated = JSON.stringify(this.statusField) !== JSON.stringify(data);
Lijiaoa's avatar
Lijiaoa committed
73
74
75
76
77
78
79
80
                this.statusField = data;
            })
            .catch(error => {
                this.isStatusError = true;
                this.statusErrorMessage = `${error.message}`;
                updated = true;
            });

81
82
83
84
85
        return updated;
    }

    get profile(): ExperimentProfile {
        if (!this.profileField) {
Lijiaoa's avatar
Lijiaoa committed
86
87
88
89
            // throw Error('Experiment profile not initialized');
            // set initProfile to prevent page broken
            const initProfile = {
                data: {
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                    id: '',
                    revision: 0,
                    execDuration: 0,
                    logDir: '',
                    nextSequenceId: 0,
                    params: {
                        authorName: '',
                        experimentName: '',
                        trialConcurrency: 0,
                        maxExecDuration: 0,
                        maxTrialNum: 0,
                        searchSpace: 'null',
                        trainingServicePlatform: '',
                        tuner: {
                            builtinTunerName: 'TPE',
                            // eslint-disable-next-line @typescript-eslint/camelcase
                            classArgs: { optimize_mode: '' },
                            checkpointDir: ''
Lijiaoa's avatar
Lijiaoa committed
108
                        },
109
110
111
112
113
114
115
116
                        versionCheck: true,
                        clusterMetaData: [
                            { key: '', value: '' },
                            { key: '', value: '' }
                        ]
                    },
                    startTime: 0,
                    endTime: 0
Lijiaoa's avatar
Lijiaoa committed
117
118
119
                }
            };
            this.profileField = initProfile.data as any;
120
        }
Lijiao's avatar
Lijiao committed
121
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
122
123
124
125
126
127
128
129
130
        return this.profileField!;
    }

    get trialConcurrency(): number {
        return this.profile.params.trialConcurrency;
    }

    get optimizeMode(): string {
        const tuner = this.profile.params.tuner;
131
        return tuner && tuner.classArgs && tuner.classArgs.optimize_mode ? tuner.classArgs.optimize_mode : 'unknown';
132
133
134
135
136
137
138
    }

    get trainingServicePlatform(): string {
        return this.profile.params.trainingServicePlatform;
    }

    get searchSpace(): object {
139
140
141
142
143
144
145
146
        const result = JSON.parse(this.profile.params.searchSpace);
        for (const item in result) {
            if (result[item]._value && typeof result[item]._value[0] === 'object') {
                this.isNestedExperiment = true;
                break;
            }
        }
        return result;
147
148
    }

149
150
151
152
153
154
    get searchSpaceNew(): SearchSpace {
        // The search space derived directly from profile
        // eventually this will replace searchSpace
        return new SearchSpace('', '', this.searchSpace);
    }

155
156
157
158
159
160
    get logCollectionEnabled(): boolean {
        return !!(this.profile.params.logCollection && this.profile.params.logCollection !== 'none');
    }

    get status(): string {
        if (!this.statusField) {
Lijiaoa's avatar
Lijiaoa committed
161
162
163
            // throw Error('Experiment status not initialized');
            // this.statusField.status = '';
            return '';
164
        }
Lijiao's avatar
Lijiao committed
165
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
166
167
168
169
        return this.statusField!.status;
    }

    get error(): string {
170
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
171
172
173
        if (!this.statusField) {
            throw Error('Experiment status not initialized');
        }
Lijiao's avatar
Lijiao committed
174
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
175
176
177
178
179
        return this.statusField!.errors[0] || '';
    }
}

export { Experiment };