This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
/
tensorflow.go
160 lines (133 loc) · 5.5 KB
/
tensorflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
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
160
package tensorflow
import (
"context"
"time"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/k8s/kfoperators/common"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/plugins"
flyteerr "github.com/flyteorg/flyteplugins/go/tasks/errors"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/flytek8s"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
pluginsCore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/k8s"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/utils"
//commonOp "github.com/kubeflow/common/pkg/apis/common/v1" // switch to real 'common' once https://github.com/kubeflow/pytorch-operator/issues/263 resolved
commonOp "github.com/kubeflow/tf-operator/pkg/apis/common/v1"
tfOp "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type tensorflowOperatorResourceHandler struct {
}
// Sanity test that the plugin implements method of k8s.Plugin
var _ k8s.Plugin = tensorflowOperatorResourceHandler{}
func (tensorflowOperatorResourceHandler) GetProperties() k8s.PluginProperties {
return k8s.PluginProperties{}
}
// Defines a func to create a query object (typically just object and type meta portions) that's used to query k8s
// resources.
func (tensorflowOperatorResourceHandler) BuildIdentityResource(ctx context.Context, taskCtx pluginsCore.TaskExecutionMetadata) (client.Object, error) {
return &tfOp.TFJob{
TypeMeta: metav1.TypeMeta{
Kind: tfOp.Kind,
APIVersion: tfOp.SchemeGroupVersion.String(),
},
}, nil
}
// Defines a func to create the full resource object that will be posted to k8s.
func (tensorflowOperatorResourceHandler) BuildResource(ctx context.Context, taskCtx pluginsCore.TaskExecutionContext) (client.Object, error) {
taskTemplate, err := taskCtx.TaskReader().Read(ctx)
if err != nil {
return nil, flyteerr.Errorf(flyteerr.BadTaskSpecification, "unable to fetch task specification [%v]", err.Error())
} else if taskTemplate == nil {
return nil, flyteerr.Errorf(flyteerr.BadTaskSpecification, "nil task specification")
}
tensorflowTaskExtraArgs := plugins.DistributedTensorflowTrainingTask{}
err = utils.UnmarshalStruct(taskTemplate.GetCustom(), &tensorflowTaskExtraArgs)
if err != nil {
return nil, flyteerr.Errorf(flyteerr.BadTaskSpecification, "invalid TaskSpecification [%v], Err: [%v]", taskTemplate.GetCustom(), err.Error())
}
podSpec, err := flytek8s.ToK8sPodSpec(ctx, taskCtx)
if err != nil {
return nil, flyteerr.Errorf(flyteerr.BadTaskSpecification, "Unable to create pod spec: [%v]", err.Error())
}
common.OverrideDefaultContainerName(taskCtx, podSpec, tfOp.DefaultContainerName)
workers := tensorflowTaskExtraArgs.GetWorkers()
psReplicas := tensorflowTaskExtraArgs.GetPsReplicas()
chiefReplicas := tensorflowTaskExtraArgs.GetChiefReplicas()
jobSpec := tfOp.TFJobSpec{
TTLSecondsAfterFinished: nil,
TFReplicaSpecs: map[tfOp.TFReplicaType]*commonOp.ReplicaSpec{
tfOp.TFReplicaTypePS: {
Replicas: &psReplicas,
Template: v1.PodTemplateSpec{
Spec: *podSpec,
},
RestartPolicy: commonOp.RestartPolicyNever,
},
tfOp.TFReplicaTypeChief: {
Replicas: &chiefReplicas,
Template: v1.PodTemplateSpec{
Spec: *podSpec,
},
RestartPolicy: commonOp.RestartPolicyNever,
},
tfOp.TFReplicaTypeWorker: {
Replicas: &workers,
Template: v1.PodTemplateSpec{
Spec: *podSpec,
},
RestartPolicy: commonOp.RestartPolicyNever,
},
},
}
job := &tfOp.TFJob{
TypeMeta: metav1.TypeMeta{
Kind: tfOp.Kind,
APIVersion: tfOp.SchemeGroupVersion.String(),
},
Spec: jobSpec,
}
return job, nil
}
// Analyses the k8s resource and reports the status as TaskPhase. This call is expected to be relatively fast,
// any operations that might take a long time (limits are configured system-wide) should be offloaded to the
// background.
func (tensorflowOperatorResourceHandler) GetTaskPhase(_ context.Context, pluginContext k8s.PluginContext, resource client.Object) (pluginsCore.PhaseInfo, error) {
app := resource.(*tfOp.TFJob)
workersCount := app.Spec.TFReplicaSpecs[tfOp.TFReplicaTypeWorker].Replicas
psReplicasCount := app.Spec.TFReplicaSpecs[tfOp.TFReplicaTypePS].Replicas
chiefCount := app.Spec.TFReplicaSpecs[tfOp.TFReplicaTypeChief].Replicas
taskLogs, err := common.GetLogs(common.TensorflowTaskType, app.Name, app.Namespace,
*workersCount, *psReplicasCount, *chiefCount)
if err != nil {
return pluginsCore.PhaseInfoUndefined, err
}
currentCondition, err := common.ExtractCurrentCondition(app.Status.Conditions)
if err != nil {
return pluginsCore.PhaseInfoUndefined, err
}
occurredAt := time.Now()
statusDetails, _ := utils.MarshalObjToStruct(app.Status)
taskPhaseInfo := pluginsCore.TaskInfo{
Logs: taskLogs,
OccurredAt: &occurredAt,
CustomInfo: statusDetails,
}
return common.GetPhaseInfo(currentCondition, occurredAt, taskPhaseInfo)
}
func init() {
if err := tfOp.AddToScheme(scheme.Scheme); err != nil {
panic(err)
}
pluginmachinery.PluginRegistry().RegisterK8sPlugin(
k8s.PluginEntry{
ID: common.TensorflowTaskType,
RegisteredTaskTypes: []pluginsCore.TaskType{common.TensorflowTaskType},
ResourceToWatch: &tfOp.TFJob{},
Plugin: tensorflowOperatorResourceHandler{},
IsDefault: false,
DefaultForTaskTypes: []pluginsCore.TaskType{common.TensorflowTaskType},
})
}