forked from kubeflow/model-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_service.go
256 lines (214 loc) · 8.71 KB
/
inference_service.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package core
import (
"context"
"fmt"
"strings"
"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"
)
// INFERENCE SERVICE
// UpsertInferenceService creates a new inference service if the provided inference service's ID is nil,
// or updates an existing inference service if the ID is provided.
func (serv *ModelRegistryService) UpsertInferenceService(inferenceService *openapi.InferenceService) (*openapi.InferenceService, error) {
if inferenceService == nil {
return nil, fmt.Errorf("invalid inference service pointer, can't upsert nil: %w", api.ErrBadRequest)
}
var err error
var existing *openapi.InferenceService
var servingEnvironment *openapi.ServingEnvironment
if inferenceService.Id == nil {
// create
glog.Info("Creating new InferenceService")
servingEnvironment, err = serv.GetServingEnvironmentById(inferenceService.ServingEnvironmentId)
if err != nil {
return nil, err
}
} else {
// update
glog.Infof("Updating InferenceService %s", *inferenceService.Id)
existing, err = serv.GetInferenceServiceById(*inferenceService.Id)
if err != nil {
return nil, err
}
withNotEditable, err := serv.openapiConv.OverrideNotEditableForInferenceService(converter.NewOpenapiUpdateWrapper(existing, inferenceService))
if err != nil {
return nil, err
}
inferenceService = &withNotEditable
servingEnvironment, err = serv.getServingEnvironmentByInferenceServiceId(*inferenceService.Id)
if err != nil {
return nil, err
}
}
// validate RegisteredModelId is also valid
if _, err := serv.GetRegisteredModelById(inferenceService.RegisteredModelId); err != nil {
return nil, err
}
// if already existing assure the name is the same
if existing != nil && inferenceService.Name == nil {
// user did not provide it
// need to set it to avoid mlmd error "context name should not be empty"
inferenceService.Name = existing.Name
}
protoCtx, err := serv.mapper.MapFromInferenceService(inferenceService, *servingEnvironment.Id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
protoCtxResp, err := serv.mlmdClient.PutContexts(context.Background(), &proto.PutContextsRequest{
Contexts: []*proto.Context{
protoCtx,
},
})
if err != nil {
return nil, err
}
inferenceServiceId := &protoCtxResp.ContextIds[0]
if inferenceService.Id == nil {
servingEnvironmentId, err := converter.StringToInt64(servingEnvironment.Id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
_, err = serv.mlmdClient.PutParentContexts(context.Background(), &proto.PutParentContextsRequest{
ParentContexts: []*proto.ParentContext{{
ChildId: inferenceServiceId,
ParentId: servingEnvironmentId,
}},
TransactionOptions: &proto.TransactionOptions{},
})
if err != nil {
return nil, err
}
}
idAsString := converter.Int64ToString(inferenceServiceId)
toReturn, err := serv.GetInferenceServiceById(*idAsString)
if err != nil {
return nil, err
}
return toReturn, nil
}
// getServingEnvironmentByInferenceServiceId retrieves the serving environment associated with the specified inference service ID.
func (serv *ModelRegistryService) getServingEnvironmentByInferenceServiceId(id string) (*openapi.ServingEnvironment, error) {
glog.Infof("Getting ServingEnvironment for InferenceService %s", id)
idAsInt, err := converter.StringToInt64(&id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
getParentResp, err := serv.mlmdClient.GetParentContextsByContext(context.Background(), &proto.GetParentContextsByContextRequest{
ContextId: idAsInt,
})
if err != nil {
return nil, err
}
if len(getParentResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple ServingEnvironments found for InferenceService %s: %w", id, api.ErrNotFound)
}
if len(getParentResp.Contexts) == 0 {
return nil, fmt.Errorf("no ServingEnvironments found for InferenceService %s: %w", id, api.ErrNotFound)
}
toReturn, err := serv.mapper.MapToServingEnvironment(getParentResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return toReturn, nil
}
// GetInferenceServiceById retrieves an inference service by its unique identifier (ID).
func (serv *ModelRegistryService) GetInferenceServiceById(id string) (*openapi.InferenceService, error) {
glog.Infof("Getting InferenceService by id %s", id)
idAsInt, err := converter.StringToInt64(&id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
getByIdResp, err := serv.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{*idAsInt},
})
if err != nil {
return nil, err
}
if len(getByIdResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple InferenceServices found for id %s: %w", id, api.ErrNotFound)
}
if len(getByIdResp.Contexts) == 0 {
return nil, fmt.Errorf("no InferenceService found for id %s: %w", id, api.ErrNotFound)
}
toReturn, err := serv.mapper.MapToInferenceService(getByIdResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return toReturn, nil
}
// GetInferenceServiceByParams retrieves an inference service based on specified parameters, such as (name and serving environment ID), or external ID.
// If multiple or no serving environments are found, an error is returned accordingly.
func (serv *ModelRegistryService) GetInferenceServiceByParams(name *string, servingEnvironmentId *string, externalId *string) (*openapi.InferenceService, error) {
filterQuery := ""
if name != nil && servingEnvironmentId != nil {
filterQuery = fmt.Sprintf("name = \"%s\"", converter.PrefixWhenOwned(servingEnvironmentId, *name))
} else if externalId != nil {
filterQuery = fmt.Sprintf("external_id = \"%s\"", *externalId)
} else {
return nil, fmt.Errorf("invalid parameters call, supply either (name and servingEnvironmentId), or externalId: %w", api.ErrBadRequest)
}
getByParamsResp, err := serv.mlmdClient.GetContextsByType(context.Background(), &proto.GetContextsByTypeRequest{
TypeName: &serv.nameConfig.InferenceServiceTypeName,
Options: &proto.ListOperationOptions{
FilterQuery: &filterQuery,
},
})
if err != nil {
return nil, err
}
if len(getByParamsResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple inference services found for name=%v, servingEnvironmentId=%v, externalId=%v: %w", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(servingEnvironmentId), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
if len(getByParamsResp.Contexts) == 0 {
return nil, fmt.Errorf("no inference services found for name=%v, servingEnvironmentId=%v, externalId=%v: %w", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(servingEnvironmentId), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
toReturn, err := serv.mapper.MapToInferenceService(getByParamsResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return toReturn, nil
}
// GetInferenceServices retrieves a list of inference services based on the provided list options and optional serving environment ID and runtime.
func (serv *ModelRegistryService) GetInferenceServices(listOptions api.ListOptions, servingEnvironmentId *string, runtime *string) (*openapi.InferenceServiceList, error) {
listOperationOptions, err := apiutils.BuildListOperationOptions(listOptions)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
queries := []string{}
if servingEnvironmentId != nil {
queryParentCtxId := fmt.Sprintf("parent_contexts_a.id = %s", *servingEnvironmentId)
queries = append(queries, queryParentCtxId)
}
if runtime != nil {
queryRuntimeProp := fmt.Sprintf("properties.runtime.string_value = \"%s\"", *runtime)
queries = append(queries, queryRuntimeProp)
}
query := strings.Join(queries, " and ")
listOperationOptions.FilterQuery = &query
contextsResp, err := serv.mlmdClient.GetContextsByType(context.Background(), &proto.GetContextsByTypeRequest{
TypeName: &serv.nameConfig.InferenceServiceTypeName,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
results := []openapi.InferenceService{}
for _, c := range contextsResp.Contexts {
mapped, err := serv.mapper.MapToInferenceService(c)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
results = append(results, *mapped)
}
toReturn := openapi.InferenceServiceList{
NextPageToken: apiutils.ZeroIfNil(contextsResp.NextPageToken),
PageSize: apiutils.ZeroIfNil(listOptions.PageSize),
Size: int32(len(results)),
Items: results,
}
return &toReturn, nil
}