-
Notifications
You must be signed in to change notification settings - Fork 46
/
registered_model.go
205 lines (171 loc) · 6.78 KB
/
registered_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
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"
)
// REGISTERED MODELS
// UpsertRegisteredModel creates a new registered model if the given registered model's ID is nil,
// or updates an existing registered model if the ID is provided.
func (serv *ModelRegistryService) UpsertRegisteredModel(registeredModel *openapi.RegisteredModel) (*openapi.RegisteredModel, error) {
var err error
var existing *openapi.RegisteredModel
if registeredModel.Id == nil {
glog.Info("Creating new registered model")
} else {
glog.Infof("Updating registered model %s", *registeredModel.Id)
existing, err = serv.GetRegisteredModelById(*registeredModel.Id)
if err != nil {
return nil, err
}
withNotEditable, err := serv.openapiConv.OverrideNotEditableForRegisteredModel(converter.NewOpenapiUpdateWrapper(existing, registeredModel))
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
registeredModel = &withNotEditable
}
modelCtx, err := serv.mapper.MapFromRegisteredModel(registeredModel)
if err != nil {
return nil, err
}
modelCtxResp, err := serv.mlmdClient.PutContexts(context.Background(), &proto.PutContextsRequest{
Contexts: []*proto.Context{
modelCtx,
},
})
if err != nil {
return nil, err
}
idAsString := converter.Int64ToString(&modelCtxResp.ContextIds[0])
model, err := serv.GetRegisteredModelById(*idAsString)
if err != nil {
return nil, err
}
return model, nil
}
// GetRegisteredModelById retrieves a registered model by its unique identifier (ID).
func (serv *ModelRegistryService) GetRegisteredModelById(id string) (*openapi.RegisteredModel, error) {
glog.Infof("Getting registered model %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{int64(*idAsInt)},
})
if err != nil {
return nil, err
}
if len(getByIdResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple registered models found for id %s: %w", id, api.ErrNotFound)
}
if len(getByIdResp.Contexts) == 0 {
return nil, fmt.Errorf("no registered model found for id %s: %w", id, api.ErrNotFound)
}
regModel, err := serv.mapper.MapToRegisteredModel(getByIdResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return regModel, nil
}
// GetRegisteredModelByInferenceService retrieves a registered model associated with the specified inference service ID.
func (serv *ModelRegistryService) GetRegisteredModelByInferenceService(inferenceServiceId string) (*openapi.RegisteredModel, error) {
is, err := serv.GetInferenceServiceById(inferenceServiceId)
if err != nil {
return nil, err
}
return serv.GetRegisteredModelById(is.RegisteredModelId)
}
// getRegisteredModelByVersionId retrieves a registered model associated with the specified model version ID.
func (serv *ModelRegistryService) getRegisteredModelByVersionId(id string) (*openapi.RegisteredModel, error) {
glog.Infof("Getting registered model for model version %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 registered models found for model version %s: %w", id, api.ErrNotFound)
}
if len(getParentResp.Contexts) == 0 {
return nil, fmt.Errorf("no registered model found for model version %s: %w", id, api.ErrNotFound)
}
regModel, err := serv.mapper.MapToRegisteredModel(getParentResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return regModel, nil
}
// GetRegisteredModelByParams retrieves a registered model based on specified parameters, such as name or external ID.
// If multiple or no registered models are found, an error is returned accordingly.
func (serv *ModelRegistryService) GetRegisteredModelByParams(name *string, externalId *string) (*openapi.RegisteredModel, error) {
glog.Infof("Getting registered model by params name=%v, externalId=%v", name, externalId)
filterQuery := ""
if name != nil {
filterQuery = fmt.Sprintf("name = \"%s\"", *name)
} else if externalId != nil {
filterQuery = fmt.Sprintf("external_id = \"%s\"", *externalId)
} else {
return nil, fmt.Errorf("invalid parameters call, supply either name or externalId: %w", api.ErrBadRequest)
}
glog.Info("filterQuery ", filterQuery)
getByParamsResp, err := serv.mlmdClient.GetContextsByType(context.Background(), &proto.GetContextsByTypeRequest{
TypeName: &serv.nameConfig.RegisteredModelTypeName,
Options: &proto.ListOperationOptions{
FilterQuery: &filterQuery,
},
})
if err != nil {
return nil, err
}
if len(getByParamsResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple registered models found for name=%v, externalId=%v: %w", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
if len(getByParamsResp.Contexts) == 0 {
return nil, fmt.Errorf("no registered models found for name=%v, externalId=%v: %w", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
regModel, err := serv.mapper.MapToRegisteredModel(getByParamsResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return regModel, nil
}
// GetRegisteredModels retrieves a list of registered models based on the provided list options.
func (serv *ModelRegistryService) GetRegisteredModels(listOptions api.ListOptions) (*openapi.RegisteredModelList, error) {
listOperationOptions, err := apiutils.BuildListOperationOptions(listOptions)
if err != nil {
return nil, err
}
contextsResp, err := serv.mlmdClient.GetContextsByType(context.Background(), &proto.GetContextsByTypeRequest{
TypeName: &serv.nameConfig.RegisteredModelTypeName,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
results := []openapi.RegisteredModel{}
for _, c := range contextsResp.Contexts {
mapped, err := serv.mapper.MapToRegisteredModel(c)
if err != nil {
return nil, err
}
results = append(results, *mapped)
}
toReturn := openapi.RegisteredModelList{
NextPageToken: apiutils.ZeroIfNil(contextsResp.NextPageToken),
PageSize: apiutils.ZeroIfNil(listOptions.PageSize),
Size: int32(len(results)),
Items: results,
}
return &toReturn, nil
}