-
Notifications
You must be signed in to change notification settings - Fork 283
/
_model.py
1281 lines (1111 loc) · 41.8 KB
/
_model.py
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from __future__ import print_function
from verta._internal_utils._utils import check_unnecessary_params_warning
from verta.tracking import _Context
from verta.tracking.entities import _entity
from verta._internal_utils import _artifact_utils, _utils, arg_handler, model_validator
from verta._protos.public.common import CommonService_pb2 as _CommonCommonService
from verta._protos.public.registry import RegistryService_pb2 as _RegistryService
from .. import _constants, VertaModelBase
from ._modelversion import RegisteredModelVersion
from ._modelversions import RegisteredModelVersions
from .. import task_type as task_type_module
from .. import data_type as data_type_module
from .. import check_model_dependencies as check_model_dependencies_fn
class RegisteredModel(_entity._ModelDBEntity):
"""
Object representing a registered model.
There should not be a need to instantiate this class directly; please use
:meth:`Client.get_or_create_registered_model()
<verta.Client.get_or_create_registered_model>`
Attributes
----------
id : int
ID of this Registered Model.
name : str
Name of this Registered Model.
url : str
Verta web app URL.
versions : iterable of :class:`~verta.registry.entities.RegisteredModelVersion`
Versions of this RegisteredModel.
pii: bool
Whether the registered_model ingests personally identifiable information.
"""
def __init__(self, conn, conf, msg):
super(RegisteredModel, self).__init__(
conn, conf, _RegistryService, "registered_model", msg
)
def __repr__(self):
self._refresh_cache()
msg = self._msg
return "\n".join(
(
"name: {}".format(msg.name),
"url: {}".format(self.url),
"time created: {}".format(
_utils.timestamp_to_str(int(msg.time_created))
),
"time updated: {}".format(
_utils.timestamp_to_str(int(msg.time_updated))
),
"description: {}".format(msg.description),
"labels: {}".format(msg.labels),
"id: {}".format(msg.id),
"pii: {}".format(msg.pii),
)
)
@property
def name(self):
self._refresh_cache()
return self._msg.name
@property
def url(self):
return "{}://{}/{}/registry/{}".format(
self._conn.scheme,
self._conn.socket,
self.workspace,
self.id,
)
@property
def workspace(self):
self._refresh_cache()
if self._msg.workspace_id:
return self._conn.get_workspace_name_from_id(self._msg.workspace_id)
else:
return self._conn._OSS_DEFAULT_WORKSPACE
def get_or_create_version(
self,
name=None,
desc=None,
labels=None,
attrs=None,
time_created=None,
lock_level=None,
id=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""
Gets or creates a Model Version.
If an accessible Model Version with name `name` does not already exist under this
Registered Model, it will be created and initialized with specified metadata
parameters. If such a Model Version does already exist, it will be retrieved;
specifying metadata parameters in this case will raise a warning.
Parameters
----------
name : str, optional
Name of the Model Version. If no name is provided, one will be generated.
desc : str, optional
Description of the Model Version.
labels : list of str, optional
Labels of the Model Version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the Model Version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
id : str, optional
ID of the Model Version. This parameter cannot be provided alongside `name`, and other
parameters will be ignored.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Raises
------
ValueError
If `name` and `id` are both passed in.
"""
if name is not None and id is not None:
raise ValueError("cannot specify both `name` and `id`")
resource_name = "Model Version"
param_names = "`desc`, `labels`, `attrs`, `time_created`, `lock_level`, `input_description`, `hide_input_label`, `output_description`, or `hide_output_label`"
params = (
desc,
labels,
attrs,
time_created,
lock_level,
input_description,
hide_input_label,
output_description,
hide_output_label,
)
if id is not None:
check_unnecessary_params_warning(
resource_name, "id {}".format(id), param_names, params
)
return RegisteredModelVersion._get_by_id(self._conn, self._conf, id)
else:
ctx = _Context(self._conn, self._conf)
ctx.registered_model = self
return RegisteredModelVersion._get_or_create_by_name(
self._conn,
name,
lambda name: RegisteredModelVersion._get_by_name(
self._conn, self._conf, name, self.id
),
lambda name: RegisteredModelVersion._create(
self._conn,
self._conf,
ctx,
name=name,
desc=desc,
tags=labels,
attrs=attrs,
date_created=time_created,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
),
lambda: check_unnecessary_params_warning(
resource_name,
"name {}".format(name),
param_names,
params,
),
)
def set_version(self, *args, **kwargs):
"""
Alias for :meth:`RegisteredModel.get_or_create_version()`.
"""
return self.get_or_create_version(*args, **kwargs)
def create_version(
self,
name=None,
desc=None,
labels=None,
attrs=None,
time_created=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""
Creates a model registry entry.
Parameters
----------
name : str, optional
Name of the Model Version. If no name is provided, one will be generated.
desc : str, optional
Description of the Model Version.
labels : list of str, optional
Labels of the Model Version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the Model Version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
"""
ctx = _Context(self._conn, self._conf)
ctx.registered_model = self
return RegisteredModelVersion._create(
self._conn,
self._conf,
ctx,
name=name,
desc=desc,
tags=labels,
attrs=attrs,
date_created=time_created,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
def _create_standard_model_from_spec(
self,
model,
environment,
code_dependencies=None,
model_api=None,
artifacts=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
artifacts = artifacts or {}
for key in artifacts.keys():
_artifact_utils.validate_key(key)
attrs = attrs or {}
attrs.update(
{
_constants.MODEL_LANGUAGE_ATTR_KEY: _constants.ModelLanguage.PYTHON,
_constants.MODEL_TYPE_ATTR_KEY: _constants.ModelType.STANDARD_VERTA_MODEL,
}
)
model_ver = self.create_version(
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
try:
for key, artifact in artifacts.items():
model_ver.log_artifact(key, artifact)
model_ver.log_model(
model=model,
custom_modules=code_dependencies,
model_api=model_api,
artifacts=list(artifacts.keys()),
)
model_ver.log_environment(environment)
except Exception as e:
model_ver.delete()
raise e
return model_ver
def create_standard_model(
self,
model_cls,
environment,
code_dependencies=None,
model_api=None,
artifacts=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
check_model_dependencies=False,
):
"""Create a Standard Verta Model version from a Verta Model Specification.
.. versionadded:: 0.22.2
The `check_model_dependencies` parameter.
.. versionadded:: 0.18.2
.. note::
The following artifact keys are reserved for internal use within the
Verta system:
- ``"custom_modules"``
- ``"model"``
- ``"model.pkl"``
- ``"model_api.json"``
- ``"requirements.txt"``
- ``"train_data"``
- ``"tf_saved_model"``
- ``"setup_script"``
.. note::
If using an XGBoost model from their scikit-learn API,
``"scikit-learn"`` must also be specified in `environment`
(in addition to ``"xgboost"``).
Parameters
----------
model_cls : subclass of :class:`~verta.registry.VertaModelBase`
Model class that implements ``VertaModelBase``.
environment : :class:`~verta.environment.Python`
pip and apt dependencies.
code_dependencies : list of str, optional
Paths to local Python code files that `model_cls` depends on. This
parameter has the same behavior as ``custom_modules`` in
:meth:`RegisteredModelVersion.log_model`.
model_api : :class:`~verta.utils.ModelAPI`, optional
Model API specifying the model's expected input and output
artifacts : dict of str to obj
A mapping from artifact keys to artifacts. These will be logged
and uploaded, then provided to the model when deployed.
name : str, optional
Name of the model version. If no name is provided, one will be
generated.
desc : str, optional
Description of the model version.
labels : list of str, optional
Labels of the model version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the model version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
check_model_dependencies : bool, default False
Whether to verify the model's dependencies are specified in the environment
and raise an exception if any are missing.
Raises
------
RuntimeError
If `check_model_dependencies` is ``True`` and any dependencies detected in
the model class are not specified in the environment.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Examples
--------
.. code-block:: python
from verta.environment import Python
from verta.registry import VertaModelBase
class VertaModel(VertaModelBase):
def __init__(self, artifacts):
import pickle
with open(artifacts["weights"], "rb") as f:
self.weights = pickle.load(f)
def predict(self, input):
import numpy as np
return np.matmul(self.weights, input)
model_ver = reg_model.create_standard_model(
VertaModel,
Python(["numpy"]),
artifacts={"weights": np.array(weights)},
)
endpoint.update(model_ver, wait=True)
endpoint.get_deployed_model().predict(input)
"""
model_validator.must_verta(model_cls)
if check_model_dependencies:
check_model_dependencies_fn(
model_cls=model_cls, environment=environment, raise_for_missing=True
)
return self._create_standard_model_from_spec(
model=model_cls,
environment=environment,
code_dependencies=code_dependencies,
model_api=model_api,
artifacts=artifacts,
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
def create_standard_model_from_keras(
self,
obj,
environment,
model_api=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""Create a Standard Verta Model version from a TensorFlow-backend Keras model.
.. versionadded:: 0.18.2
Parameters
----------
obj : `tf.keras.Sequential <https://keras.io/guides/sequential_model/>`__ or `functional API keras.Model <https://keras.io/guides/functional_api/>`__
Keras model.
environment : :class:`~verta.environment.Python`
pip and apt dependencies.
model_api : :class:`~verta.utils.ModelAPI`, optional
Model API specifying the model's expected input and output
name : str, optional
Name of the model version. If no name is provided, one will be
generated.
desc : str, optional
Description of the model version.
labels : list of str, optional
Labels of the model version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the model version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Examples
--------
.. code-block:: python
from tensorflow import keras
from verta.environment import Python
inputs = keras.Input(shape=(3,))
x = keras.layers.Dense(2, activation="relu")(inputs)
outputs = keras.layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
train(model, data)
model_ver = reg_model.create_standard_model_from_keras(
model,
Python(["tensorflow"]),
)
endpoint.update(model_ver, wait=True)
endpoint.get_deployed_model().predict(input)
"""
model_validator.must_keras(obj)
return self._create_standard_model_from_spec(
model=obj,
environment=environment,
model_api=model_api,
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
def create_standard_model_from_sklearn(
self,
obj,
environment,
model_api=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""Create a Standard Verta Model version from a scikit-learn model.
.. versionadded:: 0.18.2
Parameters
----------
obj : `sklearn.base.BaseEstimator <https://scikit-learn.org/stable/modules/generated/sklearn.base.BaseEstimator.html>`__
scikit-learn model.
environment : :class:`~verta.environment.Python`
pip and apt dependencies.
model_api : :class:`~verta.utils.ModelAPI`, optional
Model API specifying the model's expected input and output
name : str, optional
Name of the model version. If no name is provided, one will be
generated.
desc : str, optional
Description of the model version.
labels : list of str, optional
Labels of the model version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the model version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Examples
--------
.. code-block:: python
from sklearn.svm import LinearSVC
from verta.environment import Python
model = LinearSVC(**hyperparams)
model.fit(X_train, y_train)
model_ver = reg_model.create_standard_model_from_sklearn(
model,
Python(["scikit-learn"]),
)
endpoint.update(model_ver, wait=True)
endpoint.get_deployed_model().predict(input)
"""
model_validator.must_sklearn(obj)
return self._create_standard_model_from_spec(
model=obj,
environment=environment,
model_api=model_api,
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
def create_standard_model_from_torch(
self,
obj,
environment,
model_api=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""Create a Standard Verta Model version from a PyTorch model.
.. versionadded:: 0.18.2
Parameters
----------
obj : `torch.nn.Module <https://pytorch.org/docs/stable/generated/torch.nn.Module.html>`__
PyTorch model.
environment : :class:`~verta.environment.Python`
pip and apt dependencies.
model_api : :class:`~verta.utils.ModelAPI`, optional
Model API specifying the model's expected input and output
name : str, optional
Name of the model version. If no name is provided, one will be
generated.
desc : str, optional
Description of the model version.
labels : list of str, optional
Labels of the model version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the model version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Examples
--------
.. code-block:: python
import torch
from verta.environment import Python
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.layer1 = torch.nn.Linear(3, 2)
self.layer2 = torch.nn.Linear(2, 1)
def forward(self, x):
x = torch.nn.functional.relu(self.layer1(x))
return torch.sigmoid(self.layer2(x))
model = Model()
train(model, data)
model_ver = reg_model.create_standard_model_from_torch(
model,
Python(["torch"]),
)
endpoint.update(model_ver, wait=True)
endpoint.get_deployed_model().predict(input)
"""
model_validator.must_torch(obj)
return self._create_standard_model_from_spec(
model=obj,
environment=environment,
model_api=model_api,
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
def create_standard_model_from_xgboost(
self,
obj,
environment,
model_api=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""Create a Standard Verta Model version from an XGBoost model.
.. versionadded:: 0.18.2
.. note::
If using an XGBoost model from their scikit-learn API,
``"scikit-learn"`` must also be specified in `environment`
(in addition to ``"xgboost"``).
Parameters
----------
obj : `xgboost.sklearn.XGBModel <https://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.sklearn>`__
XGBoost model using their scikit-learn wrapper interface.
environment : :class:`~verta.environment.Python`
pip and apt dependencies.
model_api : :class:`~verta.utils.ModelAPI`, optional
Model API specifying the model's expected input and output
name : str, optional
Name of the model version. If no name is provided, one will be
generated.
desc : str, optional
Description of the model version.
labels : list of str, optional
Labels of the model version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the model version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Examples
--------
.. code-block:: python
import xgboost as xgb
from verta.environment import Python
model = xgb.XGBClassifier(**hyperparams)
model.fit(X_train, y_train)
model_ver = reg_model.create_standard_model_from_xgboost(
model,
Python(["scikit-learn", "xgboost"]),
)
endpoint.update(model_ver, wait=True)
endpoint.get_deployed_model().predict(input)
"""
model_validator.must_xgboost_sklearn(obj)
return self._create_standard_model_from_spec(
model=obj,
environment=environment,
model_api=model_api,
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
def create_containerized_model(
self,
docker_image,
model_api=None,
name=None,
desc=None,
labels=None,
attrs=None,
lock_level=None,
input_description=None,
hide_input_label=False,
output_description=None,
hide_output_label=False,
):
"""Create a Containerized Model version from a Docker image.
.. versionadded:: 0.20.0
.. note::
|experimental|
Parameters
----------
docker_image : :class:`~verta.registry.DockerImage`
Docker image information.
model_api : :class:`~verta.utils.ModelAPI`, optional
Model API specifying the model's expected input and output
name : str, optional
Name of the model version. If no name is provided, one will be
generated.
desc : str, optional
Description of the model version.
labels : list of str, optional
Labels of the model version.
attrs : dict of str to {None, bool, float, int, str}, optional
Attributes of the model version.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
input_description : str, optional
Description of the model version's input.
hide_input_label : bool, default False
Whether to hide the model version's input label.
output_description : str, optional
Description of the model version's output.
hide_output_label : bool, default False
Whether to hide the model version's output label.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
Examples
--------
.. code-block:: python
from verta.registry import DockerImage
docker_image = DockerImage(
port=5000,
request_path="/predict_json",
health_path="/health",
repository="012345678901.dkr.ecr.apne2-az1.amazonaws.com/models/example",
tag="example",
env_vars={"CUDA_VISIBLE_DEVICES": "0,1"},
)
model_ver = reg_model.create_containerized_model(
docker_image,
)
endpoint.update(model_ver, wait=True)
endpoint.get_deployed_model().predict(input)
"""
attrs = attrs or {}
attrs.update(
{
_constants.MODEL_LANGUAGE_ATTR_KEY: _constants.ModelLanguage.UNKNOWN,
_constants.MODEL_TYPE_ATTR_KEY: _constants.ModelType.USER_CONTAINERIZED_MODEL,
}
)
model_ver = self.create_version(
name=name,
desc=desc,
labels=labels,
attrs=attrs,
lock_level=lock_level,
input_description=input_description,
hide_input_label=hide_input_label,
output_description=output_description,
hide_output_label=hide_output_label,
)
try:
model_ver.log_docker(
docker_image=docker_image,
model_api=model_api,
)
except Exception as e:
model_ver.delete()
raise e
return model_ver
def create_version_from_run(self, run_id, name=None, lock_level=None):
"""Create a model version copied from an experiment run.
Parameters
----------
run_id : str or :class:`~verta.tracking.entities.ExperimentRun`
Run from which to create the model version.
name : str, optional
Name of the model version. If no name is provided, one will be generated.
lock_level : :mod:`~verta.registry.lock`, default :class:`~verta.registry.lock.Open`
Lock level to set when creating this model version.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
"""
run_id = arg_handler.extract_id(run_id)
ctx = _Context(self._conn, self._conf)
ctx.registered_model = self
return RegisteredModelVersion._create(
self._conn,
self._conf,
ctx,
name=name,
experiment_run_id=run_id,
lock_level=lock_level,
)
def get_version(self, name=None, id=None):
"""
Gets a Model Version of this Registered Model by `name` or `id`
Parameters
----------
name : str, optional
Name of the Model Version. If no name is provided, one will be generated.
id : str, optional
ID of the Model Version. This parameter cannot be provided alongside `name`, and other
parameters will be ignored.
Returns
-------
:class:`~verta.registry.entities.RegisteredModelVersion`
"""
if name is not None and id is not None:
raise ValueError("cannot specify both `name` and `id`")
if name is None and id is None:
raise ValueError("must specify either `name` or `id`")
if id is not None:
version = RegisteredModelVersion._get_by_id(self._conn, self._conf, id)
else:
version = RegisteredModelVersion._get_by_name(
self._conn, self._conf, name, self.id
)
if version is None:
raise ValueError("Registered model version not found")
return version
@property
def versions(self):