kubeflowConfig.ts 7.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 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 * as assert from 'assert';
import { MethodNotImplementedError } from '../../../common/errors';
24
25
26
import { AzureStorage, KeyVaultConfig, KubernetesClusterConfig, KubernetesClusterConfigAzure, KubernetesClusterConfigNFS,
    KubernetesStorageKind, KubernetesTrialConfig, KubernetesTrialConfigTemplate, NFSConfig, StorageConfig
} from '../kubernetesConfig';
27

28
// operator types that kubeflow supported
29
30
31
export type KubeflowOperator = 'tf-operator' | 'pytorch-operator' ;
export type DistTrainRole = 'worker' | 'ps' | 'master';
export type KubeflowJobStatus = 'Created' | 'Running' | 'Failed' | 'Succeeded';
32
export type OperatorApiVersion = 'v1alpha2' | 'v1beta1' | 'v1beta2';
33

34
35
36
/**
 * Kubeflow Cluster Configuration
 */
37
38
export class KubeflowClusterConfig extends KubernetesClusterConfig {
    public readonly operator: KubeflowOperator;
39
40
    constructor(apiVersion: string, operator: KubeflowOperator) {
        super(apiVersion);
41
42
43
44
        this.operator = operator;
    }
}

45
// tslint:disable:completed-docs
46
47
48
export class KubeflowClusterConfigNFS extends KubernetesClusterConfigNFS {
    public readonly operator: KubeflowOperator;
    constructor(
49
50
            operator: KubeflowOperator,
            apiVersion: string,
51
52
53
54
55
56
57
58
59
60
61
            nfs: NFSConfig,
            storage?: KubernetesStorageKind
        ) {
        super(apiVersion, nfs, storage);
        this.operator = operator;
    }

    public get storageType(): KubernetesStorageKind {
        return 'nfs';
    }

62
    // tslint:disable-next-line:function-name
63
    public static getInstance(jsonObject: object): KubeflowClusterConfigNFS {
64
65
66
        const kubeflowClusterConfigObjectNFS: KubeflowClusterConfigNFS = <KubeflowClusterConfigNFS>jsonObject;
        assert (kubeflowClusterConfigObjectNFS !== undefined);

67
68
69
70
71
72
73
74
75
        return new KubeflowClusterConfigNFS(
            kubeflowClusterConfigObjectNFS.operator,
            kubeflowClusterConfigObjectNFS.apiVersion,
            kubeflowClusterConfigObjectNFS.nfs,
            kubeflowClusterConfigObjectNFS.storage
        );
    }
}

76
export class KubeflowClusterConfigAzure extends KubernetesClusterConfigAzure {
77
    public readonly operator: KubeflowOperator;
78

79
    constructor(
80
81
            operator: KubeflowOperator,
            apiVersion: string,
82
            keyVault: KeyVaultConfig,
83
            azureStorage: AzureStorage,
84
85
            storage?: KubernetesStorageKind
        ) {
86
        super(apiVersion, keyVault, azureStorage, storage);
87
88
89
        this.operator = operator;
    }

90
    public get storageType(): KubernetesStorageKind {
91
92
93
        return 'azureStorage';
    }

94
    // tslint:disable-next-line:function-name
95
    public static getInstance(jsonObject: object): KubeflowClusterConfigAzure {
96
97
        const kubeflowClusterConfigObjectAzure: KubeflowClusterConfigAzure = <KubeflowClusterConfigAzure>jsonObject;

98
99
100
101
102
103
104
105
106
107
108
109
        return new KubeflowClusterConfigAzure(
            kubeflowClusterConfigObjectAzure.operator,
            kubeflowClusterConfigObjectAzure.apiVersion,
            kubeflowClusterConfigObjectAzure.keyVault,
            kubeflowClusterConfigObjectAzure.azureStorage,
            kubeflowClusterConfigObjectAzure.storage
        );
    }
}

export class KubeflowClusterConfigFactory {

110
    // tslint:disable-next-line:function-name
111
    public static generateKubeflowClusterConfig(jsonObject: object): KubeflowClusterConfig {
112
113
114
         const storageConfig: StorageConfig = <StorageConfig>jsonObject;
         if (storageConfig === undefined) {
            throw new Error('Invalid json object as a StorageConfig instance');
115
        }
116
         if (storageConfig.storage !== undefined && storageConfig.storage === 'azureStorage') {
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
            return KubeflowClusterConfigAzure.getInstance(jsonObject);
         } else if (storageConfig.storage === undefined || storageConfig.storage === 'nfs') {
            return KubeflowClusterConfigNFS.getInstance(jsonObject);
         }
         throw new Error(`Invalid json object ${jsonObject}`);
    }
}

export class KubeflowTrialConfig extends KubernetesTrialConfig {
    constructor(codeDir: string) {
        super(codeDir);
    }

    public get operatorType(): KubeflowOperator {
        throw new MethodNotImplementedError();
    }
}

135
export class KubeflowTrialConfigTemplate extends KubernetesTrialConfigTemplate {
136
    public readonly replicas: number;
137
    constructor(replicas: number, command : string, gpuNum : number,
138
                cpuNum: number, memoryMB: number, image: string) {
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        super(command, gpuNum, cpuNum, memoryMB, image);
        this.replicas = replicas;
    }
}

export class KubeflowTrialConfigTensorflow extends KubeflowTrialConfig {
    public readonly ps?: KubeflowTrialConfigTemplate;
    public readonly worker: KubeflowTrialConfigTemplate;

    constructor(codeDir: string, worker: KubeflowTrialConfigTemplate,  ps?: KubeflowTrialConfigTemplate) {
        super(codeDir);
        this.ps = ps;
        this.worker = worker;
    }

    public get operatorType(): KubeflowOperator {
        return 'tf-operator';
    }
}

export class KubeflowTrialConfigPytorch extends KubeflowTrialConfig {
    public readonly master: KubeflowTrialConfigTemplate;
    public readonly worker?: KubeflowTrialConfigTemplate;

    constructor(codeDir: string, master: KubeflowTrialConfigTemplate, worker?: KubeflowTrialConfigTemplate) {
        super(codeDir);
        this.master = master;
        this.worker = worker;
    }

    public get operatorType(): KubeflowOperator {
        return 'pytorch-operator';
    }
}

export class KubeflowTrialConfigFactory {

176
    // tslint:disable-next-line:function-name
177
    public static generateKubeflowTrialConfig(jsonObject: object, operator: KubeflowOperator): KubeflowTrialConfig {
178
179
180
        if (operator === 'tf-operator') {
            const kubeflowTrialConfigObject: KubeflowTrialConfigTensorflow = <KubeflowTrialConfigTensorflow>jsonObject;

181
182
183
184
185
            return new KubeflowTrialConfigTensorflow(
                kubeflowTrialConfigObject.codeDir,
                kubeflowTrialConfigObject.worker,
                kubeflowTrialConfigObject.ps
            );
186
187
188
        } else if (operator === 'pytorch-operator') {
            const kubeflowTrialConfigObject: KubeflowTrialConfigPytorch = <KubeflowTrialConfigPytorch>jsonObject;

189
190
191
192
193
194
            return new KubeflowTrialConfigPytorch(
                kubeflowTrialConfigObject.codeDir,
                kubeflowTrialConfigObject.master,
                kubeflowTrialConfigObject.worker
            );
        }
195
        throw new Error(`Invalid json object ${jsonObject}`);
196
197
    }
}