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

'use strict';

import * as assert from 'assert';
import { MethodNotImplementedError } from '../../../common/errors';
8
9
10
import { AzureStorage, KeyVaultConfig, KubernetesClusterConfig, KubernetesClusterConfigAzure, KubernetesClusterConfigNFS,
    KubernetesStorageKind, KubernetesTrialConfig, KubernetesTrialConfigTemplate, NFSConfig, StorageConfig
} from '../kubernetesConfig';
11

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

18
19
20
/**
 * Kubeflow Cluster Configuration
 */
21
22
export class KubeflowClusterConfig extends KubernetesClusterConfig {
    public readonly operator: KubeflowOperator;
23
24
    constructor(apiVersion: string, operator: KubeflowOperator) {
        super(apiVersion);
25
26
27
28
        this.operator = operator;
    }
}

29
// tslint:disable:completed-docs
30
31
32
export class KubeflowClusterConfigNFS extends KubernetesClusterConfigNFS {
    public readonly operator: KubeflowOperator;
    constructor(
33
34
            operator: KubeflowOperator,
            apiVersion: string,
35
36
37
38
39
40
41
42
43
44
45
            nfs: NFSConfig,
            storage?: KubernetesStorageKind
        ) {
        super(apiVersion, nfs, storage);
        this.operator = operator;
    }

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

46
    // tslint:disable-next-line:function-name
47
    public static getInstance(jsonObject: object): KubeflowClusterConfigNFS {
48
49
50
        const kubeflowClusterConfigObjectNFS: KubeflowClusterConfigNFS = <KubeflowClusterConfigNFS>jsonObject;
        assert (kubeflowClusterConfigObjectNFS !== undefined);

51
52
53
54
55
56
57
58
59
        return new KubeflowClusterConfigNFS(
            kubeflowClusterConfigObjectNFS.operator,
            kubeflowClusterConfigObjectNFS.apiVersion,
            kubeflowClusterConfigObjectNFS.nfs,
            kubeflowClusterConfigObjectNFS.storage
        );
    }
}

60
export class KubeflowClusterConfigAzure extends KubernetesClusterConfigAzure {
61
    public readonly operator: KubeflowOperator;
62

63
    constructor(
64
65
            operator: KubeflowOperator,
            apiVersion: string,
66
            keyVault: KeyVaultConfig,
67
            azureStorage: AzureStorage,
68
69
            storage?: KubernetesStorageKind
        ) {
70
        super(apiVersion, keyVault, azureStorage, storage);
71
72
73
        this.operator = operator;
    }

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

78
    // tslint:disable-next-line:function-name
79
    public static getInstance(jsonObject: object): KubeflowClusterConfigAzure {
80
81
        const kubeflowClusterConfigObjectAzure: KubeflowClusterConfigAzure = <KubeflowClusterConfigAzure>jsonObject;

82
83
84
85
86
87
88
89
90
91
92
93
        return new KubeflowClusterConfigAzure(
            kubeflowClusterConfigObjectAzure.operator,
            kubeflowClusterConfigObjectAzure.apiVersion,
            kubeflowClusterConfigObjectAzure.keyVault,
            kubeflowClusterConfigObjectAzure.azureStorage,
            kubeflowClusterConfigObjectAzure.storage
        );
    }
}

export class KubeflowClusterConfigFactory {

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

119
export class KubeflowTrialConfigTemplate extends KubernetesTrialConfigTemplate {
120
    public readonly replicas: number;
121
    constructor(replicas: number, command : string, gpuNum : number,
122
123
                cpuNum: number, memoryMB: number, image: string, privateRegistryAuthPath?: string) {
        super(command, gpuNum, cpuNum, memoryMB, image, privateRegistryAuthPath);
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
158
159
        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 {

160
    // tslint:disable-next-line:function-name
161
    public static generateKubeflowTrialConfig(jsonObject: object, operator: KubeflowOperator): KubeflowTrialConfig {
162
163
164
        if (operator === 'tf-operator') {
            const kubeflowTrialConfigObject: KubeflowTrialConfigTensorflow = <KubeflowTrialConfigTensorflow>jsonObject;

165
166
167
168
169
            return new KubeflowTrialConfigTensorflow(
                kubeflowTrialConfigObject.codeDir,
                kubeflowTrialConfigObject.worker,
                kubeflowTrialConfigObject.ps
            );
170
171
172
        } else if (operator === 'pytorch-operator') {
            const kubeflowTrialConfigObject: KubeflowTrialConfigPytorch = <KubeflowTrialConfigPytorch>jsonObject;

173
174
175
176
177
178
            return new KubeflowTrialConfigPytorch(
                kubeflowTrialConfigObject.codeDir,
                kubeflowTrialConfigObject.master,
                kubeflowTrialConfigObject.worker
            );
        }
179
        throw new Error(`Invalid json object ${jsonObject}`);
180
181
    }
}