experiment.ts 5.21 KB
Newer Older
1
import { MANAGER_IP } from '../const';
2
import { ExperimentConfig, toSeconds } from '../experimentConfig';
3
import { ExperimentProfile, NNIManagerStatus } from '../interface';
Lijiaoa's avatar
Lijiaoa committed
4
import { requestAxios } from '../function';
5
import { SearchSpace } from './searchspace';
6
7
8
9
10
11
12
13
14
15

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);
}

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const emptyProfile: ExperimentProfile = {
    params: {
        searchSpace: undefined,
        trialCommand: '',
        trialCodeDirectory: '',
        trialConcurrency: 0,
        debug: false,
        trainingService: {
            platform: ''
        }
    },
    id: '',
    execDuration: 0,
    logDir: '',
    startTime: 0,
    maxSequenceId: 0,
    revision: 0
};

35
class Experiment {
36
    private profileField?: ExperimentProfile;
37
    private statusField?: NNIManagerStatus = undefined;
38
    private isNestedExperiment: boolean = false;
Lijiaoa's avatar
Lijiaoa committed
39
40
41
42
    private isexperimentError: boolean = false;
    private experimentErrorMessage: string = '';
    private isStatusError: boolean = false;
    private statusErrorMessage: string = '';
43
44
45

    public async init(): Promise<void> {
        while (!this.profileField || !this.statusField) {
Lijiaoa's avatar
Lijiaoa committed
46
47
48
49
50
51
            if (this.isexperimentError) {
                return;
            }
            if (this.isStatusError) {
                return;
            }
52
53
54
55
            await this.update();
        }
    }

56
    public isNestedExp(): boolean {
57
58
59
60
61
62
63
        try {
            return !!Object.values(this.config.searchSpace).find(
                item => (item as any)._value && typeof (item as any)._value[0] == 'object'
            );
        } catch {
            return false;
        }
64
65
    }

Lijiaoa's avatar
Lijiaoa committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    public experimentError(): boolean {
        return this.isexperimentError;
    }

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

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

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

82
83
    public async update(): Promise<boolean> {
        let updated = false;
Lijiaoa's avatar
Lijiaoa committed
84
85
86

        await requestAxios(`${MANAGER_IP}/experiment`)
            .then(data => {
Lijiaoa's avatar
Lijiaoa committed
87
                updated = updated || !compareProfiles(this.profileField, data);
Lijiaoa's avatar
Lijiaoa committed
88
89
90
91
92
93
94
95
96
97
                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
98
                updated = JSON.stringify(this.statusField) !== JSON.stringify(data);
Lijiaoa's avatar
Lijiaoa committed
99
100
101
102
103
104
105
106
                this.statusField = data;
            })
            .catch(error => {
                this.isStatusError = true;
                this.statusErrorMessage = `${error.message}`;
                updated = true;
            });

107
108
109
110
        return updated;
    }

    get profile(): ExperimentProfile {
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        return this.profileField === undefined ? emptyProfile : this.profileField;
    }

    get config(): ExperimentConfig {
        return this.profile.params;
    }

    get maxExperimentDurationSeconds(): number {
        const value = this.config.maxExperimentDuration;
        return value === undefined ? Infinity : toSeconds(value);
    }

    get maxTrialNumber(): number {
        const value = this.config.maxTrialNumber;
        return value === undefined ? Infinity : value;
126
127
128
    }

    get trialConcurrency(): number {
129
        return this.config.trialConcurrency;
130
131
132
    }

    get optimizeMode(): string {
133
134
135
136
137
138
        for (const algo of [this.config.tuner, this.config.advisor, this.config.assessor]) {
            if (algo && algo.classArgs && algo.classArgs['optimizeMode']) {
                return algo.classArgs['optimizeMode'];
            }
        }
        return 'unknown';
139
140
141
    }

    get trainingServicePlatform(): string {
142
        return this.config.trainingService.platform;
143
144
145
    }

    get searchSpace(): object {
146
        return this.config.searchSpace;
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
    get logCollectionEnabled(): boolean {
156
        return false;
157
158
159
160
    }

    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 };