"src/targets/vscode:/vscode.git/clone" did not exist on "66fa00832aad43aeb0d0751b1b45ef70a2ba7379"
localTrainingServiceForGPU.ts 5.14 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Copyright (c) Microsoft Corporation
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

import { TrialJobDetail, TrialJobStatus } from '../../common/trainingService';
import { GPUScheduler } from './gpuScheduler';
import { LocalTrainingService } from './localTrainingService';
25
import { TrialConfigMetadataKey } from '../common/trialConfigMetadataKey';
Deshui Yu's avatar
Deshui Yu committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

type LocalTrialJobDetailForGPU = TrialJobDetail & { gpuIndices: number[] };

/**
 * Local training service for GPU
 */
class LocalTrainingServiceForGPU extends LocalTrainingService {
    private requiredGPUNum!: number;
    private gpuScheduler!: GPUScheduler;
    private availableGPUIndices: boolean[];

    constructor() {
        super();
        this.availableGPUIndices = Array(16).fill(false); // Assume the maximum gpu number is 16
    }

    public async run(): Promise<void> {
        if (this.gpuScheduler !== undefined) {
            await Promise.all([
                this.gpuScheduler.run(),
                super.run()
            ]);
        } else {
            await super.run();
        }
    }

    public async setClusterMetadata(key: string, value: string): Promise<void> {
        await super.setClusterMetadata(key, value);
        switch (key) {
56
57
58
59
60
61
62
63
            case TrialConfigMetadataKey.TRIAL_CONFIG:
                if(this.localTrailConfig !== undefined) {
                    this.requiredGPUNum = this.localTrailConfig.gpuNum;
                } else {
                    // If no valid trial config is initialized, set requiredGPUNum to 0 as fallback value.
                    this.requiredGPUNum = 0;
                }
                this.log.info('required GPU number is ' + this.requiredGPUNum);
Deshui Yu's avatar
Deshui Yu committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
                if (this.gpuScheduler === undefined) {
                    this.gpuScheduler = new GPUScheduler();
                }
                break;
            default:
        }
    }

    public cleanUp(): Promise<void> {
        if (this.gpuScheduler !== undefined) {
            this.gpuScheduler.stop();
        }

        return super.cleanUp();
    }

    protected onTrialJobStatusChanged(trialJob: LocalTrialJobDetailForGPU, oldStatus: TrialJobStatus): void {
81
        if (trialJob.gpuIndices !== undefined && trialJob.gpuIndices.length !== 0) {
Deshui Yu's avatar
Deshui Yu committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
            if (oldStatus === 'RUNNING' && trialJob.status !== 'RUNNING') {
                for (const index of trialJob.gpuIndices) {
                    this.availableGPUIndices[index] = false;
                }
            }
        }
    }

    protected getEnvironmentVariables(
        trialJobDetail: TrialJobDetail,
        resource: { gpuIndices: number[] }): { key: string; value: string }[] {
        const variables: { key: string; value: string }[] = super.getEnvironmentVariables(trialJobDetail, resource);
        variables.push({
            key: 'CUDA_VISIBLE_DEVICES',
            value: resource.gpuIndices.join(',')
        });

        return variables;
    }

    protected setExtraProperties(trialJobDetail: LocalTrialJobDetailForGPU, resource: { gpuIndices: number[] }): void {
        super.setExtraProperties(trialJobDetail, resource);
        trialJobDetail.gpuIndices = resource.gpuIndices;
    }

    protected tryGetAvailableResource(): [boolean, {}] {
        const [success, resource] = super.tryGetAvailableResource();
        if (!success || this.gpuScheduler === undefined) {
            return [success, resource];
        }

        const availableGPUIndices: number[] = this.gpuScheduler.getAvailableGPUIndices();
        const selectedGPUIndices: number[] = availableGPUIndices.filter((index: number) => this.availableGPUIndices[index] === false);

        if (selectedGPUIndices.length < this.requiredGPUNum) {
            return [false, resource];
        }

        selectedGPUIndices.splice(this.requiredGPUNum);
        Object.assign(resource, { gpuIndices: selectedGPUIndices });

        return [true, resource];
    }

    protected occupyResource(resource: { gpuIndices: number[] }): void {
        super.occupyResource(resource);
        for (const index of resource.gpuIndices) {
            this.availableGPUIndices[index] = true;
        }
    }
}

export { LocalTrainingServiceForGPU };