-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmodel.py
2554 lines (2298 loc) · 117 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Placeholder docstring"""
# pylint: skip-file
from __future__ import absolute_import
import abc
import json
import logging
import os
import re
import copy
from typing import List, Dict, Optional, Union, Any
import sagemaker
from sagemaker import (
fw_utils,
local,
s3,
session,
utils,
git_utils,
)
from sagemaker.config import (
COMPILATION_JOB_ROLE_ARN_PATH,
EDGE_PACKAGING_KMS_KEY_ID_PATH,
EDGE_PACKAGING_ROLE_ARN_PATH,
MODEL_CONTAINERS_PATH,
EDGE_PACKAGING_RESOURCE_KEY_PATH,
MODEL_VPC_CONFIG_PATH,
MODEL_ENABLE_NETWORK_ISOLATION_PATH,
MODEL_EXECUTION_ROLE_ARN_PATH,
MODEL_PRIMARY_CONTAINER_ENVIRONMENT_PATH,
ENDPOINT_CONFIG_ASYNC_KMS_KEY_ID_PATH,
load_sagemaker_config,
)
from sagemaker.jumpstart.enums import JumpStartModelType
from sagemaker.model_card import (
ModelCard,
ModelPackageModelCard,
)
from sagemaker.model_card.helpers import _hash_content_str
from sagemaker.model_card.schema_constraints import ModelApprovalStatusEnum
from sagemaker.session import Session
from sagemaker.model_metrics import ModelMetrics
from sagemaker.deprecations import removed_kwargs
from sagemaker.drift_check_baselines import DriftCheckBaselines
from sagemaker.explainer import ExplainerConfig
from sagemaker.metadata_properties import MetadataProperties
from sagemaker.predictor import PredictorBase
from sagemaker.serverless import ServerlessInferenceConfig
from sagemaker.transformer import Transformer
from sagemaker.jumpstart.utils import (
add_jumpstart_uri_tags,
get_jumpstart_base_name_if_jumpstart_model,
)
from sagemaker.utils import (
unique_name_from_base,
update_container_with_inference_params,
to_string,
resolve_value_from_config,
resolve_nested_dict_value_from_config,
format_tags,
Tags,
_resolve_routing_config,
_validate_new_tags,
remove_tag_with_key,
)
from sagemaker.async_inference import AsyncInferenceConfig
from sagemaker.predictor_async import AsyncPredictor
from sagemaker.workflow import is_pipeline_variable
from sagemaker.workflow.entities import PipelineVariable
from sagemaker.workflow.pipeline_context import runnable_by_pipeline, PipelineSession
from sagemaker.inference_recommender.inference_recommender_mixin import (
InferenceRecommenderMixin,
)
from sagemaker.compute_resource_requirements.resource_requirements import ResourceRequirements
from sagemaker.enums import EndpointType
from sagemaker.session import (
get_add_model_package_inference_args,
get_update_model_package_inference_args,
)
from sagemaker.model_life_cycle import ModelLifeCycle
# Setting LOGGER for backward compatibility, in case users import it...
logger = LOGGER = logging.getLogger("sagemaker")
NEO_ALLOWED_FRAMEWORKS = set(
["mxnet", "tensorflow", "keras", "pytorch", "onnx", "xgboost", "tflite"]
)
NEO_IOC_TARGET_DEVICES = [
"ml_c4",
"ml_c5",
"ml_m4",
"ml_m5",
"ml_p2",
"ml_p3",
"ml_g4dn",
]
NEO_MULTIVERSION_UNSUPPORTED = [
"imx8mplus",
"jacinto_tda4vm",
"coreml",
"sitara_am57x",
"amba_cv2",
"amba_cv22",
"amba_cv25",
"lambda",
]
class ModelBase(abc.ABC):
"""An object that encapsulates a trained model.
Models can be deployed to compute services like a SageMaker ``Endpoint``
or Lambda. Deployed models can be used to perform real-time inference.
"""
@abc.abstractmethod
def deploy(self, *args, **kwargs) -> PredictorBase:
"""Deploy this model to a compute service."""
@abc.abstractmethod
def delete_model(self, *args, **kwargs) -> None:
"""Destroy resources associated with this model."""
SCRIPT_PARAM_NAME = "sagemaker_program"
DIR_PARAM_NAME = "sagemaker_submit_directory"
CONTAINER_LOG_LEVEL_PARAM_NAME = "sagemaker_container_log_level"
JOB_NAME_PARAM_NAME = "sagemaker_job_name"
MODEL_SERVER_WORKERS_PARAM_NAME = "sagemaker_model_server_workers"
SAGEMAKER_REGION_PARAM_NAME = "sagemaker_region"
SAGEMAKER_OUTPUT_LOCATION = "sagemaker_s3_output"
class Model(ModelBase, InferenceRecommenderMixin):
"""A SageMaker ``Model`` that can be deployed to an ``Endpoint``."""
def __init__(
self,
image_uri: Optional[Union[str, PipelineVariable]] = None,
model_data: Optional[Union[str, PipelineVariable, dict]] = None,
role: Optional[str] = None,
predictor_cls: Optional[callable] = None,
env: Optional[Dict[str, Union[str, PipelineVariable]]] = None,
name: Optional[str] = None,
vpc_config: Optional[Dict[str, List[Union[str, PipelineVariable]]]] = None,
sagemaker_session: Optional[Session] = None,
enable_network_isolation: Union[bool, PipelineVariable] = None,
model_kms_key: Optional[str] = None,
image_config: Optional[Dict[str, Union[str, PipelineVariable]]] = None,
source_dir: Optional[str] = None,
code_location: Optional[str] = None,
entry_point: Optional[str] = None,
container_log_level: Union[int, PipelineVariable] = logging.INFO,
dependencies: Optional[List[str]] = None,
git_config: Optional[Dict[str, str]] = None,
resources: Optional[ResourceRequirements] = None,
additional_model_data_sources: Optional[Dict[str, Any]] = None,
model_reference_arn: Optional[str] = None,
):
"""Initialize an SageMaker ``Model``.
Args:
image_uri (str or PipelineVariable): A Docker image URI.
model_data (str or PipelineVariable or dict): Location
of SageMaker model data (default: None).
role (str): An AWS IAM role (either name or full ARN). The Amazon
SageMaker training jobs and APIs that create Amazon SageMaker
endpoints use this role to access training data and model
artifacts. After the endpoint is created, the inference code
might use the IAM role if it needs to access some AWS resources.
It can be null if this is being used to create a Model to pass
to a ``PipelineModel`` which has its own Role field. (default:
None)
predictor_cls (callable[string, sagemaker.session.Session]): A
function to call to create a predictor (default: None). If not
None, ``deploy`` will return the result of invoking this
function on the created endpoint name.
env (dict[str, str] or dict[str, PipelineVariable]): Environment variables
to run with ``image_uri`` when hosted in SageMaker (default: None).
name (str): The model name. If None, a default model name will be
selected on each ``deploy``.
vpc_config (dict[str, list[str]] or dict[str, list[PipelineVariable]]):
The VpcConfig set on the model (default: None)
* 'Subnets' (list[str]): List of subnet ids.
* 'SecurityGroupIds' (list[str]): List of security group ids.
sagemaker_session (sagemaker.session.Session): A SageMaker Session
object, used for SageMaker interactions (default: None). If not
specified, one is created using the default AWS configuration
chain.
enable_network_isolation (Boolean or PipelineVariable): Default False.
if True, enables network isolation in the endpoint, isolating the model
container. No inbound or outbound network calls can be made to
or from the model container.
model_kms_key (str): KMS key ARN used to encrypt the repacked
model archive file if the model is repacked
image_config (dict[str, str] or dict[str, PipelineVariable]): Specifies
whether the image of model container is pulled from ECR, or private
registry in your VPC. By default it is set to pull model container
image from ECR. (default: None).
source_dir (str): The absolute, relative, or S3 URI Path to a directory
with any other training source code dependencies aside from the entry
point file (default: None). If ``source_dir`` is an S3 URI, it must
point to a tar.gz file. Structure within this directory is preserved
when training on Amazon SageMaker. If 'git_config' is provided,
'source_dir' should be a relative location to a directory in the Git repo.
If the directory points to S3, no code is uploaded and the S3 location
is used instead.
.. admonition:: Example
With the following GitHub repo directory structure:
>>> |----- README.md
>>> |----- src
>>> |----- inference.py
>>> |----- test.py
You can assign entry_point='inference.py', source_dir='src'.
code_location (str): Name of the S3 bucket where custom code is
uploaded (default: None). If not specified, the default bucket
created by ``sagemaker.session.Session`` is used.
entry_point (str): The absolute or relative path to the local Python
source file that should be executed as the entry point to
model hosting. (Default: None). If ``source_dir`` is specified, then ``entry_point``
must point to a file located at the root of ``source_dir``.
If 'git_config' is provided, 'entry_point' should be
a relative location to the Python source file in the Git repo.
Example:
With the following GitHub repo directory structure:
>>> |----- README.md
>>> |----- src
>>> |----- inference.py
>>> |----- test.py
You can assign entry_point='src/inference.py'.
container_log_level (int or PipelineVariable): Log level to use within the
container (default: logging.INFO). Valid values are defined in the Python
logging module.
dependencies (list[str]): A list of absolute or relative paths to directories
with any additional libraries that should be exported
to the container (default: []). The library folders are
copied to SageMaker in the same folder where the entrypoint is
copied. If 'git_config' is provided, 'dependencies' should be a
list of relative locations to directories with any additional
libraries needed in the Git repo. If the ```source_dir``` points
to S3, code will be uploaded and the S3 location will be used
instead.
.. admonition:: Example
The following call
>>> Model(entry_point='inference.py',
... dependencies=['my/libs/common', 'virtual-env'])
results in the following structure inside the container:
>>> $ ls
>>> opt/ml/code
>>> |------ inference.py
>>> |------ common
>>> |------ virtual-env
This is not supported with "local code" in Local Mode.
git_config (dict[str, str]): Git configurations used for cloning
files, including ``repo``, ``branch``, ``commit``,
``2FA_enabled``, ``username``, ``password`` and ``token``. The
``repo`` field is required. All other fields are optional.
``repo`` specifies the Git repository where your training script
is stored. If you don't provide ``branch``, the default value
'master' is used. If you don't provide ``commit``, the latest
commit in the specified branch is used.
.. admonition:: Example
The following config:
>>> git_config = {'repo': 'https://github.com/aws/sagemaker-python-sdk.git',
>>> 'branch': 'test-branch-git-config',
>>> 'commit': '329bfcf884482002c05ff7f44f62599ebc9f445a'}
results in cloning the repo specified in 'repo', then
checking out the 'master' branch, and checking out the specified
commit.
``2FA_enabled``, ``username``, ``password`` and ``token`` are
used for authentication. For GitHub (or other Git) accounts, set
``2FA_enabled`` to 'True' if two-factor authentication is
enabled for the account, otherwise set it to 'False'. If you do
not provide a value for ``2FA_enabled``, a default value of
'False' is used. CodeCommit does not support two-factor
authentication, so do not provide "2FA_enabled" with CodeCommit
repositories.
For GitHub and other Git repos, when SSH URLs are provided, it
doesn't matter whether 2FA is enabled or disabled. You should
either have no passphrase for the SSH key pairs or have the
ssh-agent configured so that you will not be prompted for the SSH
passphrase when you run the 'git clone' command with SSH URLs. When
HTTPS URLs are provided, if 2FA is disabled, then either ``token``
or ``username`` and ``password`` are be used for authentication if provided.
``Token`` is prioritized. If 2FA is enabled, only ``token`` is used
for authentication if provided. If required authentication info
is not provided, the SageMaker Python SDK attempts to use local credentials
to authenticate. If that fails, an error message is thrown.
For CodeCommit repos, 2FA is not supported, so ``2FA_enabled``
should not be provided. There is no token in CodeCommit, so
``token`` should also not be provided. When ``repo`` is an SSH URL,
the requirements are the same as GitHub repos. When ``repo``
is an HTTPS URL, ``username`` and ``password`` are used for
authentication if they are provided. If they are not provided,
the SageMaker Python SDK attempts to use either the CodeCommit
credential helper or local credential storage for authentication.
resources (Optional[ResourceRequirements]): The compute resource requirements
for a model to be deployed to an endpoint. Only
EndpointType.INFERENCE_COMPONENT_BASED supports this feature.
(Default: None).
additional_model_data_sources (Optional[Dict[str, Any]]): Additional location
of SageMaker model data (default: None).
model_reference_arn (Optional [str]): Hub Content Arn of a Model Reference type
content (default: None).
"""
self.model_data = model_data
self.additional_model_data_sources = additional_model_data_sources
self.image_uri = image_uri
self.predictor_cls = predictor_cls
self.name = name
self._base_name = None
self.sagemaker_session = sagemaker_session
self.algorithm_arn = None
self.model_package_arn = None
# Workaround for config injection if sagemaker_session is None, since in
# that case sagemaker_session will not be initialized until
# `_init_sagemaker_session_if_does_not_exist` is called later
self._sagemaker_config = (
load_sagemaker_config() if (self.sagemaker_session is None) else None
)
self.role = resolve_value_from_config(
role,
MODEL_EXECUTION_ROLE_ARN_PATH,
sagemaker_session=self.sagemaker_session,
sagemaker_config=self._sagemaker_config,
)
self.vpc_config = resolve_value_from_config(
vpc_config,
MODEL_VPC_CONFIG_PATH,
sagemaker_session=self.sagemaker_session,
sagemaker_config=self._sagemaker_config,
)
self.endpoint_name = None
self.inference_component_name = None
self._is_compiled_model = False
self._is_sharded_model = False
self._compilation_job_name = None
self._is_edge_packaged_model = False
self.inference_recommender_job_results = None
self.inference_recommendations = None
self._enable_network_isolation = resolve_value_from_config(
enable_network_isolation,
MODEL_ENABLE_NETWORK_ISOLATION_PATH,
default_value=False,
sagemaker_session=self.sagemaker_session,
sagemaker_config=self._sagemaker_config,
)
self.env = resolve_value_from_config(
env,
MODEL_PRIMARY_CONTAINER_ENVIRONMENT_PATH,
default_value={},
sagemaker_session=self.sagemaker_session,
sagemaker_config=self._sagemaker_config,
)
self.model_kms_key = model_kms_key
self.image_config = image_config
self.entry_point = entry_point
self.source_dir = source_dir
self.dependencies = dependencies or []
self.git_config = git_config
self.container_log_level = container_log_level
if code_location:
self.bucket, self.key_prefix = s3.parse_s3_url(code_location)
else:
self.bucket, self.key_prefix = None, None
if self.git_config:
updates = git_utils.git_clone_repo(
self.git_config, self.entry_point, self.source_dir, self.dependencies
)
self.entry_point = updates["entry_point"]
self.source_dir = updates["source_dir"]
self.dependencies = updates["dependencies"]
self.uploaded_code = None
self.repacked_model_data = None
self.mode = None
self.modes = {}
self.serve_settings = None
self.resources = resources
self.content_types = None
self.response_types = None
self.accept_eula = None
self.model_reference_arn = model_reference_arn
self._tags: Optional[Tags] = None
def add_tags(self, tags: Tags) -> None:
"""Add tags to this ``Model``
Args:
tags (Tags): Tags to add.
"""
self._tags = _validate_new_tags(tags, self._tags)
def remove_tag_with_key(self, key: str) -> None:
"""Remove a tag with the given key from the list of tags.
Args:
key (str): The key of the tag to remove.
"""
self._tags = remove_tag_with_key(key, self._tags)
@classmethod
def attach(
cls,
endpoint_name: str,
inference_component_name: Optional[str] = None,
sagemaker_session=None,
) -> "Model":
"""Attaches a Model object to an existing SageMaker Endpoint."""
raise NotImplementedError
@runnable_by_pipeline
def register(
self,
content_types: List[Union[str, PipelineVariable]] = None,
response_types: List[Union[str, PipelineVariable]] = None,
inference_instances: Optional[List[Union[str, PipelineVariable]]] = None,
transform_instances: Optional[List[Union[str, PipelineVariable]]] = None,
model_package_name: Optional[Union[str, PipelineVariable]] = None,
model_package_group_name: Optional[Union[str, PipelineVariable]] = None,
image_uri: Optional[Union[str, PipelineVariable]] = None,
model_metrics: Optional[ModelMetrics] = None,
metadata_properties: Optional[MetadataProperties] = None,
marketplace_cert: bool = False,
approval_status: Optional[Union[str, PipelineVariable]] = None,
description: Optional[str] = None,
drift_check_baselines: Optional[DriftCheckBaselines] = None,
customer_metadata_properties: Optional[Dict[str, Union[str, PipelineVariable]]] = None,
validation_specification: Optional[Union[str, PipelineVariable]] = None,
domain: Optional[Union[str, PipelineVariable]] = None,
task: Optional[Union[str, PipelineVariable]] = None,
sample_payload_url: Optional[Union[str, PipelineVariable]] = None,
framework: Optional[Union[str, PipelineVariable]] = None,
framework_version: Optional[Union[str, PipelineVariable]] = None,
nearest_model_name: Optional[Union[str, PipelineVariable]] = None,
data_input_configuration: Optional[Union[str, PipelineVariable]] = None,
skip_model_validation: Optional[Union[str, PipelineVariable]] = None,
source_uri: Optional[Union[str, PipelineVariable]] = None,
model_card: Optional[Union[ModelPackageModelCard, ModelCard]] = None,
model_life_cycle: Optional[ModelLifeCycle] = None,
accept_eula: Optional[bool] = None,
model_type: Optional[JumpStartModelType] = None,
):
"""Creates a model package for creating SageMaker models or listing on Marketplace.
Args:
content_types (list[str] or list[PipelineVariable]): The supported MIME types
for the input data.
response_types (list[str] or list[PipelineVariable]): The supported MIME types
for the output data.
inference_instances (list[str] or list[PipelineVariable]): A list of the instance
types that are used to generate inferences in real-time (default: None).
transform_instances (list[str] or list[PipelineVariable]): A list of the instance
types on which a transformation job can be run or on which an endpoint can be
deployed (default: None).
model_package_name (str or PipelineVariable): Model Package name, exclusive to
`model_package_group_name`, using `model_package_name` makes the Model Package
un-versioned (default: None).
model_package_group_name (str or PipelineVariable): Model Package Group name,
exclusive to `model_package_name`, using `model_package_group_name` makes
the Model Package versioned (default: None).
image_uri (str or PipelineVariable): Inference image uri for the container.
Model class' self.image will be used if it is None (default: None).
model_metrics (ModelMetrics): ModelMetrics object (default: None).
metadata_properties (MetadataProperties): MetadataProperties object (default: None).
marketplace_cert (bool): A boolean value indicating if the Model Package is certified
for AWS Marketplace (default: False).
approval_status (str or PipelineVariable): Model Approval Status, values can be
"Approved", "Rejected", or "PendingManualApproval"
(default: "PendingManualApproval").
description (str): Model Package description (default: None).
drift_check_baselines (DriftCheckBaselines): DriftCheckBaselines object (default: None).
customer_metadata_properties (dict[str, str] or dict[str, PipelineVariable]):
A dictionary of key-value paired metadata properties (default: None).
domain (str or PipelineVariable): Domain values can be "COMPUTER_VISION",
"NATURAL_LANGUAGE_PROCESSING", "MACHINE_LEARNING" (default: None).
task (str or PipelineVariable): Task values which are supported by Inference Recommender
are "FILL_MASK", "IMAGE_CLASSIFICATION", "OBJECT_DETECTION", "TEXT_GENERATION",
"IMAGE_SEGMENTATION", "CLASSIFICATION", "REGRESSION", "OTHER" (default: None).
sample_payload_url (str or PipelineVariable): The S3 path where the sample
payload is stored (default: None).
framework (str or PipelineVariable): Machine learning framework of the model package
container image (default: None).
framework_version (str or PipelineVariable): Framework version of the Model Package
Container Image (default: None).
nearest_model_name (str or PipelineVariable): Name of a pre-trained machine learning
benchmarked by Amazon SageMaker Inference Recommender (default: None).
data_input_configuration (str or PipelineVariable): Input object for the model
(default: None).
skip_model_validation (str or PipelineVariable): Indicates if you want to skip model
validation. Values can be "All" or "None" (default: None).
source_uri (str or PipelineVariable): The URI of the source for the model package
(default: None).
model_card (ModeCard or ModelPackageModelCard): document contains qualitative and
quantitative information about a model (default: None).
model_life_cycle (ModelLifeCycle): ModelLifeCycle object (default: None).
Returns:
A `sagemaker.model.ModelPackage` instance or pipeline step arguments
in case the Model instance is built with
:class:`~sagemaker.workflow.pipeline_context.PipelineSession`
"""
if content_types is not None:
self.content_types = content_types
if response_types is not None:
self.response_types = response_types
if image_uri is not None:
self.image_uri = image_uri
if model_package_group_name is None and model_package_name is None:
# If model package group and model package name is not set
# then register to auto-generated model package group
model_package_group_name = utils.base_name_from_image(
self.image_uri, default_base_name=ModelPackage.__name__
)
if (
model_package_group_name is not None
and model_type is not JumpStartModelType.PROPRIETARY
):
container_def = self.prepare_container_def(accept_eula=accept_eula)
container_def = update_container_with_inference_params(
framework=framework,
framework_version=framework_version,
nearest_model_name=nearest_model_name,
data_input_configuration=data_input_configuration,
container_def=container_def,
)
else:
container_def = {
"Image": self.image_uri,
}
if isinstance(self.model_data, dict):
raise ValueError(
"Un-versioned SageMaker Model Package currently cannot be "
"created with ModelDataSource."
)
if self.model_data is not None:
container_def["ModelDataUrl"] = self.model_data
model_pkg_args = sagemaker.get_model_package_args(
self.content_types,
self.response_types,
inference_instances=inference_instances,
transform_instances=transform_instances,
model_package_name=model_package_name,
model_package_group_name=model_package_group_name,
model_metrics=model_metrics,
metadata_properties=metadata_properties,
marketplace_cert=marketplace_cert,
approval_status=approval_status,
description=description,
container_def_list=[container_def],
drift_check_baselines=drift_check_baselines,
customer_metadata_properties=customer_metadata_properties,
validation_specification=validation_specification,
domain=domain,
sample_payload_url=sample_payload_url,
task=task,
skip_model_validation=skip_model_validation,
source_uri=source_uri,
model_card=model_card,
model_life_cycle=model_life_cycle,
)
model_package = self.sagemaker_session.create_model_package_from_containers(
**model_pkg_args
)
if isinstance(self.sagemaker_session, PipelineSession):
return None
return ModelPackage(
role=self.role,
model_data=self.model_data,
model_package_arn=model_package.get("ModelPackageArn"),
sagemaker_session=self.sagemaker_session,
predictor_cls=self.predictor_cls,
)
@runnable_by_pipeline
def create(
self,
instance_type: Optional[str] = None,
accelerator_type: Optional[str] = None,
serverless_inference_config: Optional[ServerlessInferenceConfig] = None,
tags: Optional[Tags] = None,
accept_eula: Optional[bool] = None,
model_reference_arn: Optional[str] = None,
):
"""Create a SageMaker Model Entity
Args:
instance_type (str): The EC2 instance type that this Model will be
used for, this is only used to determine if the image needs GPU
support or not (default: None).
accelerator_type (str): Type of Elastic Inference accelerator to
attach to an endpoint for model loading and inference, for
example, 'ml.eia1.medium'. If not specified, no Elastic
Inference accelerator will be attached to the endpoint (default: None).
serverless_inference_config (ServerlessInferenceConfig):
Specifies configuration related to serverless endpoint. Instance type is
not provided in serverless inference. So this is used to find image URIs
(default: None).
tags (Optional[Tags]): Tags to add to the model (default: None). Example::
tags = [{'Key': 'tagname', 'Value':'tagvalue'}]
# Or
tags = {'tagname', 'tagvalue'}
For more information about tags, see
`boto3 documentation <https://boto3.amazonaws.com/v1/documentation/\
api/latest/reference/services/sagemaker.html#SageMaker.Client.add_tags>`_
accept_eula (bool): For models that require a Model Access Config, specify True or
False to indicate whether model terms of use have been accepted.
The `accept_eula` value must be explicitly defined as `True` in order to
accept the end-user license agreement (EULA) that some
models require. (Default: None).
Returns:
None or pipeline step arguments in case the Model instance is built with
:class:`~sagemaker.workflow.pipeline_context.PipelineSession`
"""
# TODO: we should replace _create_sagemaker_model() with create()
self._create_sagemaker_model(
instance_type=instance_type,
accelerator_type=accelerator_type,
tags=format_tags(tags),
serverless_inference_config=serverless_inference_config,
accept_eula=accept_eula,
model_reference_arn=model_reference_arn,
)
def _init_sagemaker_session_if_does_not_exist(self, instance_type=None):
"""Set ``self.sagemaker_session`` to ``LocalSession`` or ``Session`` if it's not already.
The type of session object is determined by the instance type.
"""
if self.sagemaker_session:
return
if instance_type in ("local", "local_gpu"):
self.sagemaker_session = local.LocalSession(sagemaker_config=self._sagemaker_config)
else:
self.sagemaker_session = session.Session(sagemaker_config=self._sagemaker_config)
def prepare_container_def(
self,
instance_type=None,
accelerator_type=None,
serverless_inference_config=None,
accept_eula=None,
model_reference_arn=None,
): # pylint: disable=unused-argument
"""Return a dict created by ``sagemaker.container_def()``.
It is used for deploying this model to a specified instance type.
Subclasses can override this to provide custom container definitions
for deployment to a specific instance type. Called by ``deploy()``.
Args:
instance_type (str): The EC2 instance type to deploy this Model to.
For example, 'ml.p2.xlarge'.
accelerator_type (str): The Elastic Inference accelerator type to
deploy to the instance for loading and making inferences to the
model. For example, 'ml.eia1.medium'.
serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig):
Specifies configuration related to serverless endpoint. Instance type is
not provided in serverless inference. So this is used to find image URIs.
accept_eula (bool): For models that require a Model Access Config, specify True or
False to indicate whether model terms of use have been accepted.
The `accept_eula` value must be explicitly defined as `True` in order to
accept the end-user license agreement (EULA) that some
models require. (Default: None).
Returns:
dict: A container definition object usable with the CreateModel API.
"""
deploy_key_prefix = fw_utils.model_code_key_prefix(
self.key_prefix, self.name, self.image_uri
)
deploy_env = copy.deepcopy(self.env)
if self.source_dir or self.dependencies or self.entry_point or self.git_config:
self._upload_code(deploy_key_prefix, repack=self.is_repack())
deploy_env.update(self._script_mode_env_vars())
return sagemaker.container_def(
self.image_uri,
self.repacked_model_data or self.model_data,
deploy_env,
image_config=self.image_config,
accept_eula=(
accept_eula if accept_eula is not None else getattr(self, "accept_eula", None)
),
additional_model_data_sources=self.additional_model_data_sources,
model_reference_arn=(
model_reference_arn
if model_reference_arn is not None
else getattr(self, "model_reference_arn", None)
),
)
def is_repack(self) -> bool:
"""Whether the source code needs to be repacked before uploading to S3.
Returns:
bool: if the source need to be repacked or not
"""
return self.source_dir and self.entry_point and not self.git_config
def _upload_code(self, key_prefix: str, repack: bool = False) -> None:
"""Uploads code to S3 to be used with script mode with SageMaker inference.
Args:
key_prefix (str): The S3 key associated with the ``code_location`` parameter of the
``Model`` class.
repack (bool): Optional. Set to ``True`` to indicate that the source code and model
artifact should be repackaged into a new S3 object. (default: False).
"""
local_code = utils.get_config_value("local.local_code", self.sagemaker_session.config)
bucket, key_prefix = s3.determine_bucket_and_prefix(
bucket=self.bucket,
key_prefix=key_prefix,
sagemaker_session=self.sagemaker_session,
)
if (self.sagemaker_session.local_mode and local_code) or self.entry_point is None:
self.uploaded_code = None
elif not repack:
self.uploaded_code = fw_utils.tar_and_upload_dir(
session=self.sagemaker_session.boto_session,
bucket=bucket,
s3_key_prefix=key_prefix,
script=self.entry_point,
directory=self.source_dir,
dependencies=self.dependencies,
kms_key=self.model_kms_key,
settings=self.sagemaker_session.settings,
)
if repack and self.model_data is not None and self.entry_point is not None:
if isinstance(self.model_data, dict):
logging.warning("ModelDataSource currently doesn't support model repacking")
return
if is_pipeline_variable(self.model_data):
# model is not yet there, defer repacking to later during pipeline execution
if not isinstance(self.sagemaker_session, PipelineSession):
logging.warning(
"The model_data is a Pipeline variable of type %s, "
"which should be used under `PipelineSession` and "
"leverage `ModelStep` to create or register model. "
"Otherwise some functionalities e.g. "
"runtime repack may be missing. For more, see: "
"https://sagemaker.readthedocs.io/en/stable/"
"amazon_sagemaker_model_building_pipeline.html#model-step",
type(self.model_data),
)
return
self.sagemaker_session.context.need_runtime_repack.add(id(self))
self.sagemaker_session.context.runtime_repack_output_prefix = s3.s3_path_join(
"s3://", bucket, key_prefix
)
# Add the uploaded_code and repacked_model_data to update the container env
self.repacked_model_data = self.model_data
self.uploaded_code = fw_utils.UploadedCode(
s3_prefix=self.repacked_model_data,
script_name=os.path.basename(self.entry_point),
)
return
if local_code and self.model_data.startswith("file://"):
repacked_model_data = self.model_data
else:
repacked_model_data = "s3://" + "/".join([bucket, key_prefix, "model.tar.gz"])
self.uploaded_code = fw_utils.UploadedCode(
s3_prefix=repacked_model_data,
script_name=os.path.basename(self.entry_point),
)
logger.info(
"Repacking model artifact (%s), script artifact "
"(%s), and dependencies (%s) "
"into single tar.gz file located at %s. "
"This may take some time depending on model size...",
self.model_data,
self.source_dir,
self.dependencies,
repacked_model_data,
)
utils.repack_model(
inference_script=self.entry_point,
source_directory=self.source_dir,
dependencies=self.dependencies,
model_uri=self.model_data,
repacked_model_uri=repacked_model_data,
sagemaker_session=self.sagemaker_session,
kms_key=self.model_kms_key,
)
self.repacked_model_data = repacked_model_data
def _script_mode_env_vars(self):
"""Returns a mapping of environment variables for script mode execution"""
script_name = self.env.get(SCRIPT_PARAM_NAME.upper(), "")
dir_name = self.env.get(DIR_PARAM_NAME.upper(), "")
if self.uploaded_code:
script_name = self.uploaded_code.script_name
if self.repacked_model_data or self.enable_network_isolation():
dir_name = "/opt/ml/model/code"
else:
dir_name = self.uploaded_code.s3_prefix
elif self.entry_point is not None:
script_name = self.entry_point
if self.source_dir is not None:
dir_name = (
self.source_dir
if self.source_dir.startswith("s3://")
else "file://" + self.source_dir
)
return {
SCRIPT_PARAM_NAME.upper(): script_name,
DIR_PARAM_NAME.upper(): dir_name,
CONTAINER_LOG_LEVEL_PARAM_NAME.upper(): to_string(self.container_log_level),
SAGEMAKER_REGION_PARAM_NAME.upper(): self.sagemaker_session.boto_region_name,
}
def enable_network_isolation(self):
"""Whether to enable network isolation when creating this Model
Returns:
bool: If network isolation should be enabled or not.
"""
return False if not self._enable_network_isolation else self._enable_network_isolation
def _create_sagemaker_model(
self,
instance_type=None,
accelerator_type=None,
tags: Optional[Tags] = None,
serverless_inference_config=None,
accept_eula=None,
model_reference_arn: Optional[str] = None,
):
"""Create a SageMaker Model Entity
Args:
instance_type (str): The EC2 instance type that this Model will be
used for, this is only used to determine if the image needs GPU
support or not.
accelerator_type (str): Type of Elastic Inference accelerator to
attach to an endpoint for model loading and inference, for
example, 'ml.eia1.medium'. If not specified, no Elastic
Inference accelerator will be attached to the endpoint.
tags (Optional[Tags]): Optional. The tags to add to
the model. Example: >>> tags = [{'Key': 'tagname', 'Value':
'tagvalue'}] For more information about tags, see
https://boto3.amazonaws.com/v1/documentation
/api/latest/reference/services/sagemaker.html#SageMaker.Client.add_tags
serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig):
Specifies configuration related to serverless endpoint. Instance type is
not provided in serverless inference. So this is used to find image URIs.
accept_eula (bool): For models that require a Model Access Config, specify True or
False to indicate whether model terms of use have been accepted.
The `accept_eula` value must be explicitly defined as `True` in order to
accept the end-user license agreement (EULA) that some
models require. (Default: None).
model_reference_arn (Optional [str]): Hub Content Arn of a Model Reference type
content (default: None).
"""
if self.model_package_arn is not None or self.algorithm_arn is not None:
model_package = ModelPackage(
role=self.role,
model_data=self.model_data,
model_package_arn=self.model_package_arn,
algorithm_arn=self.algorithm_arn,
sagemaker_session=self.sagemaker_session,
predictor_cls=self.predictor_cls,
vpc_config=self.vpc_config,
)
if self.name is not None:
model_package.name = self.name
if self.env is not None:
model_package.env = self.env
model_package._create_sagemaker_model(
instance_type=instance_type,
accelerator_type=accelerator_type,
tags=format_tags(tags),
serverless_inference_config=serverless_inference_config,
)
if self._base_name is None and model_package._base_name is not None:
self._base_name = model_package._base_name
if self.name is None and model_package.name is not None:
self.name = model_package.name
else:
container_def = self.prepare_container_def(
instance_type,
accelerator_type=accelerator_type,
serverless_inference_config=serverless_inference_config,
accept_eula=accept_eula,
model_reference_arn=model_reference_arn,
)
if not isinstance(self.sagemaker_session, PipelineSession):
# _base_name, model_name are not needed under PipelineSession.
# the model_data may be Pipeline variable
# which may break the _base_name generation
self._ensure_base_name_if_needed(
image_uri=container_def["Image"],
script_uri=self.source_dir,
model_uri=self._get_model_uri(),
)
self._set_model_name_if_needed()
self._init_sagemaker_session_if_does_not_exist(instance_type)
# Depending on the instance type, a local session (or) a session is initialized.
self.role = resolve_value_from_config(
self.role,
MODEL_EXECUTION_ROLE_ARN_PATH,
sagemaker_session=self.sagemaker_session,
)
self.vpc_config = resolve_value_from_config(
self.vpc_config,
MODEL_VPC_CONFIG_PATH,
sagemaker_session=self.sagemaker_session,
)
self._enable_network_isolation = resolve_value_from_config(
self._enable_network_isolation,
MODEL_ENABLE_NETWORK_ISOLATION_PATH,
sagemaker_session=self.sagemaker_session,
)
self.env = resolve_nested_dict_value_from_config(
self.env,
["Environment"],
MODEL_CONTAINERS_PATH,
sagemaker_session=self.sagemaker_session,
)
create_model_args = dict(
name=self.name,
role=self.role,
container_defs=container_def,
vpc_config=self.vpc_config,
enable_network_isolation=self._enable_network_isolation,
tags=format_tags(tags),
)
self.sagemaker_session.create_model(**create_model_args)
def _get_model_uri(self):
model_uri = None
if isinstance(self.model_data, (str, PipelineVariable)):
model_uri = self.model_data
elif isinstance(self.model_data, dict):
model_uri = self.model_data.get("S3DataSource", {}).get("S3Uri", None)
return model_uri
def _ensure_base_name_if_needed(self, image_uri, script_uri, model_uri):
"""Create a base name from the image URI if there is no model name provided.
If a JumpStart script or model uri is used, select the JumpStart base name.
"""
if self.name is None: