forked from kubeflow/model-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact.go
398 lines (353 loc) · 14.2 KB
/
artifact.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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"
)
// ARTIFACTS
// UpsertModelVersionArtifact creates a new artifact if the provided artifact's ID is nil, or updates an existing artifact if the
// ID is provided.
// Upon creation, new artifacts will be associated with their corresponding model version.
func (serv *ModelRegistryService) UpsertModelVersionArtifact(artifact *openapi.Artifact, modelVersionId string) (*openapi.Artifact, error) {
if artifact == nil {
return nil, fmt.Errorf("invalid artifact pointer, can't upsert nil: %w", api.ErrBadRequest)
}
art, err := serv.upsertArtifact(artifact, &modelVersionId)
if err != nil {
return nil, err
}
// upsertArtifact already validates modelVersion
var id *string
if art.ModelArtifact != nil {
id = art.ModelArtifact.Id
} else if art.DocArtifact != nil {
id = art.DocArtifact.Id
} else {
return nil, fmt.Errorf("unexpected artifact type: %v", art)
}
mv, _ := serv.getModelVersionByArtifactId(*id)
if mv == nil {
// add explicit Attribution between Artifact and ModelVersion
modelVersionId, err := converter.StringToInt64(&modelVersionId)
if err != nil {
// unreachable
return nil, fmt.Errorf("%v", err)
}
artifactId, err := converter.StringToInt64(id)
if err != nil {
return nil, fmt.Errorf("%v", err)
}
attributions := []*proto.Attribution{}
attributions = append(attributions, &proto.Attribution{
ContextId: modelVersionId,
ArtifactId: artifactId,
})
_, err = serv.mlmdClient.PutAttributionsAndAssociations(context.Background(), &proto.PutAttributionsAndAssociationsRequest{
Attributions: attributions,
Associations: make([]*proto.Association, 0),
})
if err != nil {
return nil, err
}
} else if *mv.Id != modelVersionId {
return nil, fmt.Errorf("artifact %s is already associated with a different model version %s: %w", *id, *mv.Id, api.ErrBadRequest)
}
return art, nil
}
func (serv *ModelRegistryService) upsertArtifact(artifact *openapi.Artifact, modelVersionId *string) (*openapi.Artifact, error) {
if artifact == nil {
return nil, fmt.Errorf("invalid artifact pointer, can't upsert nil: %w", api.ErrBadRequest)
}
if ma := artifact.ModelArtifact; ma != nil {
if ma.Id == nil {
glog.Info("Creating model artifact")
} else {
glog.Info("Updating model artifact")
existing, err := serv.GetModelArtifactById(*ma.Id)
if err != nil {
return nil, fmt.Errorf("mismatched types, artifact with id %s is not a model artifact: %w", *ma.Id, api.ErrBadRequest)
}
withNotEditable, err := serv.openapiConv.OverrideNotEditableForModelArtifact(converter.NewOpenapiUpdateWrapper(existing, ma))
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
artifact.ModelArtifact = &withNotEditable
}
} else if da := artifact.DocArtifact; da != nil {
if da.Id == nil {
glog.Info("Creating doc artifact")
} else {
glog.Info("Updating doc artifact")
existing, err := serv.GetArtifactById(*da.Id)
if err != nil {
return nil, err
}
if existing.DocArtifact == nil {
return nil, fmt.Errorf("mismatched types, artifact with id %s is not a doc artifact: %w", *da.Id, api.ErrBadRequest)
}
withNotEditable, err := serv.openapiConv.OverrideNotEditableForDocArtifact(converter.NewOpenapiUpdateWrapper(existing.DocArtifact, da))
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
artifact.DocArtifact = &withNotEditable
}
} else {
return nil, fmt.Errorf("invalid artifact type, must be either ModelArtifact or DocArtifact: %w", api.ErrBadRequest)
}
if modelVersionId != nil {
if _, err := serv.GetModelVersionById(*modelVersionId); err != nil {
return nil, fmt.Errorf("no model version found for id %s: %w", *modelVersionId, api.ErrNotFound)
}
}
pa, err := serv.mapper.MapFromArtifact(artifact, modelVersionId)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
artifactsResp, err := serv.mlmdClient.PutArtifacts(context.Background(), &proto.PutArtifactsRequest{
Artifacts: []*proto.Artifact{pa},
})
if err != nil {
return nil, err
}
idAsString := converter.Int64ToString(&artifactsResp.ArtifactIds[0])
return serv.GetArtifactById(*idAsString)
}
// UpsertArtifact creates a new artifact if the provided artifact's ID is nil, or updates an existing artifact if the
// ID is provided.
func (serv *ModelRegistryService) UpsertArtifact(artifact *openapi.Artifact) (*openapi.Artifact, error) {
return serv.upsertArtifact(artifact, nil)
}
func (serv *ModelRegistryService) GetArtifactById(id string) (*openapi.Artifact, error) {
idAsInt, err := converter.StringToInt64(&id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
artifactsResp, err := serv.mlmdClient.GetArtifactsByID(context.Background(), &proto.GetArtifactsByIDRequest{
ArtifactIds: []int64{int64(*idAsInt)},
})
if err != nil {
return nil, err
}
if len(artifactsResp.Artifacts) > 1 {
return nil, fmt.Errorf("multiple artifacts found for id %s: %w", id, api.ErrNotFound)
}
if len(artifactsResp.Artifacts) == 0 {
return nil, fmt.Errorf("no artifact found for id %s: %w", id, api.ErrNotFound)
}
return serv.mapper.MapToArtifact(artifactsResp.Artifacts[0])
}
// GetArtifactByParams retrieves an artifact based on specified parameters, such as (artifact name and model version ID), or external ID.
// If multiple or no model artifacts are found, an error is returned.
func (serv *ModelRegistryService) GetArtifactByParams(artifactName *string, modelVersionId *string, externalId *string) (*openapi.Artifact, error) {
var artifact0 *proto.Artifact
filterQuery := ""
if externalId != nil {
filterQuery = fmt.Sprintf("external_id = \"%s\"", *externalId)
} else if artifactName != nil && modelVersionId != nil {
filterQuery = fmt.Sprintf("name = \"%s\"", converter.PrefixWhenOwned(modelVersionId, *artifactName))
} else {
return nil, fmt.Errorf("invalid parameters call, supply either (artifactName and modelVersionId), or externalId: %w", api.ErrBadRequest)
}
glog.Info("filterQuery ", filterQuery)
artifactsResponse, err := serv.mlmdClient.GetArtifacts(context.Background(), &proto.GetArtifactsRequest{
Options: &proto.ListOperationOptions{
FilterQuery: &filterQuery,
},
})
if err != nil {
return nil, err
}
if len(artifactsResponse.Artifacts) > 1 {
return nil, fmt.Errorf("multiple model artifacts found for artifactName=%v, modelVersionId=%v, externalId=%v: %w", apiutils.ZeroIfNil(artifactName), apiutils.ZeroIfNil(modelVersionId), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
if len(artifactsResponse.Artifacts) == 0 {
return nil, fmt.Errorf("no model artifacts found for artifactName=%v, modelVersionId=%v, externalId=%v: %w", apiutils.ZeroIfNil(artifactName), apiutils.ZeroIfNil(modelVersionId), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
artifact0 = artifactsResponse.Artifacts[0]
result, err := serv.mapper.MapToArtifact(artifact0)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return result, nil
}
// GetArtifacts retrieves a list of artifacts based on the provided list options and optional model version ID.
func (serv *ModelRegistryService) GetArtifacts(listOptions api.ListOptions, modelVersionId *string) (*openapi.ArtifactList, error) {
listOperationOptions, err := apiutils.BuildListOperationOptions(listOptions)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
var artifacts []*proto.Artifact
var nextPageToken *string
if modelVersionId != nil {
ctxId, err := converter.StringToInt64(modelVersionId)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
artifactsResp, err := serv.mlmdClient.GetArtifactsByContext(context.Background(), &proto.GetArtifactsByContextRequest{
ContextId: ctxId,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
artifacts = artifactsResp.Artifacts
nextPageToken = artifactsResp.NextPageToken
} else {
artifactsResp, err := serv.mlmdClient.GetArtifacts(context.Background(), &proto.GetArtifactsRequest{
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
artifacts = artifactsResp.Artifacts
nextPageToken = artifactsResp.NextPageToken
}
results := []openapi.Artifact{}
for _, a := range artifacts {
mapped, err := serv.mapper.MapToArtifact(a)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
results = append(results, *mapped)
}
toReturn := openapi.ArtifactList{
NextPageToken: apiutils.ZeroIfNil(nextPageToken),
PageSize: apiutils.ZeroIfNil(listOptions.PageSize),
Size: int32(len(results)),
Items: results,
}
return &toReturn, nil
}
// MODEL ARTIFACTS
// UpsertModelArtifact creates a new model artifact if the provided model artifact's ID is nil,
// or updates an existing model artifact if the ID is provided.
func (serv *ModelRegistryService) UpsertModelArtifact(modelArtifact *openapi.ModelArtifact) (*openapi.ModelArtifact, error) {
if modelArtifact == nil {
return nil, fmt.Errorf("invalid artifact pointer, can't upsert nil: %w", api.ErrBadRequest)
}
art, err := serv.UpsertArtifact(&openapi.Artifact{
ModelArtifact: modelArtifact,
})
if err != nil {
return nil, err
}
return art.ModelArtifact, err
}
// GetModelArtifactById retrieves a model artifact by its unique identifier (ID).
func (serv *ModelRegistryService) GetModelArtifactById(id string) (*openapi.ModelArtifact, error) {
art, err := serv.GetArtifactById(id)
if err != nil {
return nil, err
}
ma := art.ModelArtifact
if ma == nil {
return nil, fmt.Errorf("artifact with id %s is not a model artifact: %w", id, api.ErrNotFound)
}
return ma, err
}
// GetModelArtifactByInferenceService retrieves the model artifact associated with the specified inference service ID.
func (serv *ModelRegistryService) GetModelArtifactByInferenceService(inferenceServiceId string) (*openapi.ModelArtifact, error) {
mv, err := serv.GetModelVersionByInferenceService(inferenceServiceId)
if err != nil {
return nil, err
}
artifactList, err := serv.GetModelArtifacts(api.ListOptions{}, mv.Id)
if err != nil {
return nil, err
}
if artifactList.Size == 0 {
return nil, fmt.Errorf("no artifacts found for model version %s: %w", *mv.Id, api.ErrNotFound)
}
return &artifactList.Items[0], nil
}
// GetModelArtifactByParams retrieves a model artifact based on specified parameters, such as (artifact name and model version ID), or external ID.
// If multiple or no model artifacts are found, an error is returned.
func (serv *ModelRegistryService) GetModelArtifactByParams(artifactName *string, modelVersionId *string, externalId *string) (*openapi.ModelArtifact, error) {
var artifact0 *proto.Artifact
filterQuery := ""
if externalId != nil {
filterQuery = fmt.Sprintf("external_id = \"%s\"", *externalId)
} else if artifactName != nil && modelVersionId != nil {
filterQuery = fmt.Sprintf("name = \"%s\"", converter.PrefixWhenOwned(modelVersionId, *artifactName))
} else {
return nil, fmt.Errorf("invalid parameters call, supply either (artifactName and modelVersionId), or externalId: %w", api.ErrBadRequest)
}
glog.Info("filterQuery ", filterQuery)
artifactsResponse, err := serv.mlmdClient.GetArtifactsByType(context.Background(), &proto.GetArtifactsByTypeRequest{
TypeName: &serv.nameConfig.ModelArtifactTypeName,
Options: &proto.ListOperationOptions{
FilterQuery: &filterQuery,
},
})
if err != nil {
return nil, err
}
if len(artifactsResponse.Artifacts) > 1 {
return nil, fmt.Errorf("multiple model artifacts found for artifactName=%v, modelVersionId=%v, externalId=%v: %w", apiutils.ZeroIfNil(artifactName), apiutils.ZeroIfNil(modelVersionId), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
if len(artifactsResponse.Artifacts) == 0 {
return nil, fmt.Errorf("no model artifacts found for artifactName=%v, modelVersionId=%v, externalId=%v: %w", apiutils.ZeroIfNil(artifactName), apiutils.ZeroIfNil(modelVersionId), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
artifact0 = artifactsResponse.Artifacts[0]
result, err := serv.mapper.MapToModelArtifact(artifact0)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return result, nil
}
// GetModelArtifacts retrieves a list of model artifacts based on the provided list options and optional model version ID.
func (serv *ModelRegistryService) GetModelArtifacts(listOptions api.ListOptions, modelVersionId *string) (*openapi.ModelArtifactList, error) {
listOperationOptions, err := apiutils.BuildListOperationOptions(listOptions)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
var artifacts []*proto.Artifact
var nextPageToken *string
if modelVersionId != nil {
ctxId, err := converter.StringToInt64(modelVersionId)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
typeQuery := fmt.Sprintf("type = '%v'", serv.nameConfig.ModelArtifactTypeName)
listOperationOptions.FilterQuery = &typeQuery
artifactsResp, err := serv.mlmdClient.GetArtifactsByContext(context.Background(), &proto.GetArtifactsByContextRequest{
ContextId: ctxId,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
artifacts = artifactsResp.Artifacts
nextPageToken = artifactsResp.NextPageToken
} else {
artifactsResp, err := serv.mlmdClient.GetArtifactsByType(context.Background(), &proto.GetArtifactsByTypeRequest{
TypeName: &serv.nameConfig.ModelArtifactTypeName,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
artifacts = artifactsResp.Artifacts
nextPageToken = artifactsResp.NextPageToken
}
results := []openapi.ModelArtifact{}
for _, a := range artifacts {
mapped, err := serv.mapper.MapToModelArtifact(a)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
results = append(results, *mapped)
}
toReturn := openapi.ModelArtifactList{
NextPageToken: apiutils.ZeroIfNil(nextPageToken),
PageSize: apiutils.ZeroIfNil(listOptions.PageSize),
Size: int32(len(results)),
Items: results,
}
return &toReturn, nil
}