kubeflowConfig.ts 6.56 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
3

4
5
import assert from 'assert';
import { MethodNotImplementedError } from 'common/errors';
6
7
8
import { AzureStorage, KeyVaultConfig, KubernetesClusterConfig, KubernetesClusterConfigAzure, KubernetesClusterConfigNFS,
    KubernetesStorageKind, KubernetesTrialConfig, KubernetesTrialConfigTemplate, NFSConfig, StorageConfig
} from '../kubernetesConfig';
9

10
// operator types that kubeflow supported
11
12
13
export type KubeflowOperator = 'tf-operator' | 'pytorch-operator' ;
export type DistTrainRole = 'worker' | 'ps' | 'master';
export type KubeflowJobStatus = 'Created' | 'Running' | 'Failed' | 'Succeeded';
14
export type OperatorApiVersion = 'v1alpha2' | 'v1beta1' | 'v1beta2' | 'v1';
15

16
17
18
/**
 * Kubeflow Cluster Configuration
 */
19
20
export class KubeflowClusterConfig extends KubernetesClusterConfig {
    public readonly operator: KubeflowOperator;
J-shang's avatar
J-shang committed
21
22
    constructor(apiVersion: string, operator: KubeflowOperator, namespace?: string) {
        super(apiVersion, undefined, namespace);
23
24
25
26
27
28
29
        this.operator = operator;
    }
}

export class KubeflowClusterConfigNFS extends KubernetesClusterConfigNFS {
    public readonly operator: KubeflowOperator;
    constructor(
30
31
            operator: KubeflowOperator,
            apiVersion: string,
32
            nfs: NFSConfig,
J-shang's avatar
J-shang committed
33
34
            storage?: KubernetesStorageKind,
            namespace?: string
35
        ) {
J-shang's avatar
J-shang committed
36
        super(apiVersion, nfs, storage, namespace);
37
38
39
40
41
42
43
44
        this.operator = operator;
    }

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

    public static getInstance(jsonObject: object): KubeflowClusterConfigNFS {
45
46
47
        const kubeflowClusterConfigObjectNFS: KubeflowClusterConfigNFS = <KubeflowClusterConfigNFS>jsonObject;
        assert (kubeflowClusterConfigObjectNFS !== undefined);

48
49
50
51
        return new KubeflowClusterConfigNFS(
            kubeflowClusterConfigObjectNFS.operator,
            kubeflowClusterConfigObjectNFS.apiVersion,
            kubeflowClusterConfigObjectNFS.nfs,
J-shang's avatar
J-shang committed
52
53
            kubeflowClusterConfigObjectNFS.storage,
            kubeflowClusterConfigObjectNFS.namespace
54
55
56
57
        );
    }
}

58
export class KubeflowClusterConfigAzure extends KubernetesClusterConfigAzure {
59
    public readonly operator: KubeflowOperator;
60

61
    constructor(
62
63
            operator: KubeflowOperator,
            apiVersion: string,
64
            keyVault: KeyVaultConfig,
65
            azureStorage: AzureStorage,
J-shang's avatar
J-shang committed
66
67
            storage?: KubernetesStorageKind,
            namespace?: string
68
        ) {
J-shang's avatar
J-shang committed
69
        super(apiVersion, keyVault, azureStorage, storage, undefined, namespace);
70
71
72
        this.operator = operator;
    }

73
    public get storageType(): KubernetesStorageKind {
74
75
76
77
        return 'azureStorage';
    }

    public static getInstance(jsonObject: object): KubeflowClusterConfigAzure {
78
79
        const kubeflowClusterConfigObjectAzure: KubeflowClusterConfigAzure = <KubeflowClusterConfigAzure>jsonObject;

80
81
82
83
84
        return new KubeflowClusterConfigAzure(
            kubeflowClusterConfigObjectAzure.operator,
            kubeflowClusterConfigObjectAzure.apiVersion,
            kubeflowClusterConfigObjectAzure.keyVault,
            kubeflowClusterConfigObjectAzure.azureStorage,
J-shang's avatar
J-shang committed
85
86
            kubeflowClusterConfigObjectAzure.storage,
            kubeflowClusterConfigObjectAzure.namespace
87
88
89
90
91
92
93
        );
    }
}

export class KubeflowClusterConfigFactory {

    public static generateKubeflowClusterConfig(jsonObject: object): KubeflowClusterConfig {
94
95
96
         const storageConfig: StorageConfig = <StorageConfig>jsonObject;
         if (storageConfig === undefined) {
            throw new Error('Invalid json object as a StorageConfig instance');
97
        }
98
         if (storageConfig.storage !== undefined && storageConfig.storage === 'azureStorage') {
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
            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();
    }
}

117
export class KubeflowTrialConfigTemplate extends KubernetesTrialConfigTemplate {
118
    public readonly replicas: number;
chicm-ms's avatar
chicm-ms committed
119
    constructor(replicas: number, command: string, gpuNum: number,
120
121
                cpuNum: number, memoryMB: number, image: string, privateRegistryAuthPath?: string) {
        super(command, gpuNum, cpuNum, memoryMB, image, privateRegistryAuthPath);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        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 {
    public static generateKubeflowTrialConfig(jsonObject: object, operator: KubeflowOperator): KubeflowTrialConfig {
158
159
160
        if (operator === 'tf-operator') {
            const kubeflowTrialConfigObject: KubeflowTrialConfigTensorflow = <KubeflowTrialConfigTensorflow>jsonObject;

161
162
163
164
165
            return new KubeflowTrialConfigTensorflow(
                kubeflowTrialConfigObject.codeDir,
                kubeflowTrialConfigObject.worker,
                kubeflowTrialConfigObject.ps
            );
166
167
168
        } else if (operator === 'pytorch-operator') {
            const kubeflowTrialConfigObject: KubeflowTrialConfigPytorch = <KubeflowTrialConfigPytorch>jsonObject;

169
170
171
172
173
174
            return new KubeflowTrialConfigPytorch(
                kubeflowTrialConfigObject.codeDir,
                kubeflowTrialConfigObject.master,
                kubeflowTrialConfigObject.worker
            );
        }
175
        throw new Error(`Invalid json object ${jsonObject}`);
176
177
    }
}