forked from kubeflow/model-registry
-
Notifications
You must be signed in to change notification settings - Fork 8
/
serve_model.go
223 lines (193 loc) · 6.96 KB
/
serve_model.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package core
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/kubeflow/model-registry/internal/apiutils"
"github.com/kubeflow/model-registry/internal/converter"
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
"github.com/kubeflow/model-registry/pkg/api"
"github.com/kubeflow/model-registry/pkg/openapi"
)
// SERVE MODEL
// UpsertServeModel creates a new serve model if the provided serve model's ID is nil,
// or updates an existing serve model if the ID is provided.
func (serv *ModelRegistryService) UpsertServeModel(serveModel *openapi.ServeModel, inferenceServiceId *string) (*openapi.ServeModel, error) {
if serveModel == nil {
return nil, fmt.Errorf("invalid serve model pointer, can't upsert nil: %w", api.ErrBadRequest)
}
var err error
var existing *openapi.ServeModel
if serveModel.Id == nil {
// create
glog.Info("Creating new ServeModel")
if inferenceServiceId == nil {
return nil, fmt.Errorf("missing inferenceServiceId, cannot create ServeModel without parent resource InferenceService: %w", api.ErrBadRequest)
}
_, err = serv.GetInferenceServiceById(*inferenceServiceId)
if err != nil {
return nil, err
}
} else {
// update
glog.Infof("Updating ServeModel %s", *serveModel.Id)
existing, err = serv.GetServeModelById(*serveModel.Id)
if err != nil {
return nil, err
}
withNotEditable, err := serv.openapiConv.OverrideNotEditableForServeModel(converter.NewOpenapiUpdateWrapper(existing, serveModel))
if err != nil {
return nil, err
}
serveModel = &withNotEditable
_, err = serv.getInferenceServiceByServeModel(*serveModel.Id)
if err != nil {
return nil, err
}
}
_, err = serv.GetModelVersionById(serveModel.ModelVersionId)
if err != nil {
return nil, err
}
// if already existing assure the name is the same
if existing != nil && serveModel.Name == nil {
// user did not provide it
// need to set it to avoid mlmd error "artifact name should not be empty"
serveModel.Name = existing.Name
}
execution, err := serv.mapper.MapFromServeModel(serveModel, *inferenceServiceId)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
executionsResp, err := serv.mlmdClient.PutExecutions(context.Background(), &proto.PutExecutionsRequest{
Executions: []*proto.Execution{execution},
})
if err != nil {
return nil, err
}
// add explicit Association between ServeModel and InferenceService
if inferenceServiceId != nil && serveModel.Id == nil {
inferenceServiceId, err := converter.StringToInt64(inferenceServiceId)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
associations := []*proto.Association{}
for _, a := range executionsResp.ExecutionIds {
associations = append(associations, &proto.Association{
ContextId: inferenceServiceId,
ExecutionId: &a,
})
}
_, err = serv.mlmdClient.PutAttributionsAndAssociations(context.Background(), &proto.PutAttributionsAndAssociationsRequest{
Attributions: make([]*proto.Attribution, 0),
Associations: associations,
})
if err != nil {
return nil, err
}
}
idAsString := converter.Int64ToString(&executionsResp.ExecutionIds[0])
mapped, err := serv.GetServeModelById(*idAsString)
if err != nil {
return nil, err
}
return mapped, nil
}
// getInferenceServiceByServeModel retrieves the inference service associated with the specified serve model ID.
func (serv *ModelRegistryService) getInferenceServiceByServeModel(id string) (*openapi.InferenceService, error) {
glog.Infof("Getting InferenceService for ServeModel %s", id)
idAsInt, err := converter.StringToInt64(&id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
getParentResp, err := serv.mlmdClient.GetContextsByExecution(context.Background(), &proto.GetContextsByExecutionRequest{
ExecutionId: idAsInt,
})
if err != nil {
return nil, err
}
if len(getParentResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple InferenceService found for ServeModel %s: %w", id, api.ErrNotFound)
}
if len(getParentResp.Contexts) == 0 {
return nil, fmt.Errorf("no InferenceService found for ServeModel %s: %w", id, api.ErrNotFound)
}
toReturn, err := serv.mapper.MapToInferenceService(getParentResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return toReturn, nil
}
// GetServeModelById retrieves a serve model by its unique identifier (ID).
func (serv *ModelRegistryService) GetServeModelById(id string) (*openapi.ServeModel, error) {
idAsInt, err := converter.StringToInt64(&id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
executionsResp, err := serv.mlmdClient.GetExecutionsByID(context.Background(), &proto.GetExecutionsByIDRequest{
ExecutionIds: []int64{int64(*idAsInt)},
})
if err != nil {
return nil, err
}
if len(executionsResp.Executions) > 1 {
return nil, fmt.Errorf("multiple ServeModels found for id %s: %w", id, api.ErrNotFound)
}
if len(executionsResp.Executions) == 0 {
return nil, fmt.Errorf("no ServeModel found for id %s: %w", id, api.ErrNotFound)
}
result, err := serv.mapper.MapToServeModel(executionsResp.Executions[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return result, nil
}
// GetServeModels retrieves a list of serve models based on the provided list options and optional inference service ID.
func (serv *ModelRegistryService) GetServeModels(listOptions api.ListOptions, inferenceServiceId *string) (*openapi.ServeModelList, error) {
listOperationOptions, err := apiutils.BuildListOperationOptions(listOptions)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
var executions []*proto.Execution
var nextPageToken *string
if inferenceServiceId != nil {
ctxId, err := converter.StringToInt64(inferenceServiceId)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
executionsResp, err := serv.mlmdClient.GetExecutionsByContext(context.Background(), &proto.GetExecutionsByContextRequest{
ContextId: ctxId,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
executions = executionsResp.Executions
nextPageToken = executionsResp.NextPageToken
} else {
executionsResp, err := serv.mlmdClient.GetExecutionsByType(context.Background(), &proto.GetExecutionsByTypeRequest{
TypeName: &serv.nameConfig.ServeModelTypeName,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
executions = executionsResp.Executions
nextPageToken = executionsResp.NextPageToken
}
results := []openapi.ServeModel{}
for _, a := range executions {
mapped, err := serv.mapper.MapToServeModel(a)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
results = append(results, *mapped)
}
toReturn := openapi.ServeModelList{
NextPageToken: apiutils.ZeroIfNil(nextPageToken),
PageSize: apiutils.ZeroIfNil(listOptions.PageSize),
Size: int32(len(results)),
Items: results,
}
return &toReturn, nil
}