@@ -101,6 +101,8 @@ def __init__(
101101 self .sagemaker_session = sagemaker_session
102102 self .endpoint_name = None
103103 self ._is_compiled_model = False
104+ self ._compilation_job_name = None
105+ self ._is_edge_packaged_model = False
104106 self ._enable_network_isolation = enable_network_isolation
105107 self .model_kms_key = model_kms_key
106108
@@ -336,6 +338,50 @@ def _get_framework_version(self):
336338 """Placeholder docstring"""
337339 return getattr (self , "framework_version" , None )
338340
341+ def _edge_packaging_job_config (
342+ self ,
343+ output_path ,
344+ role ,
345+ model_name ,
346+ model_version ,
347+ packaging_job_name ,
348+ compilation_job_name ,
349+ resource_key ,
350+ s3_kms_key ,
351+ tags ,
352+ ):
353+ """Creates a request object for a packaging job.
354+
355+ Args:
356+ output_path (str): where in S3 to store the output of the job
357+ role (str): what role to use when executing the job
358+ packaging_job_name (str): what to name the packaging job
359+ compilation_job_name (str): what compilation job to source the model from
360+ resource_key (str): the kms key to encrypt the disk with
361+ s3_kms_key (str): the kms key to encrypt the output with
362+ tags (list[dict]): List of tags for labeling an edge packaging job. For
363+ more, see
364+ https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
365+ Returns:
366+ dict: the request object to use when creating a packaging job
367+ """
368+ output_model_config = {
369+ "S3OutputLocation" : output_path ,
370+ }
371+ if s3_kms_key is not None :
372+ output_model_config ["KmsKeyId" ] = s3_kms_key
373+
374+ return {
375+ "output_model_config" : output_model_config ,
376+ "role" : role ,
377+ "tags" : tags ,
378+ "model_name" : model_name ,
379+ "model_version" : model_version ,
380+ "job_name" : packaging_job_name ,
381+ "compilation_job_name" : compilation_job_name ,
382+ "resource_key" : resource_key ,
383+ }
384+
339385 def _compilation_job_config (
340386 self ,
341387 target_instance_type ,
@@ -438,6 +484,64 @@ def _compilation_image_uri(self, region, target_instance_type, framework, framew
438484 version = framework_version ,
439485 )
440486
487+ def package_for_edge (
488+ self ,
489+ output_path ,
490+ model_name ,
491+ model_version ,
492+ role = None ,
493+ job_name = None ,
494+ resource_key = None ,
495+ s3_kms_key = None ,
496+ tags = None ,
497+ ):
498+ """Package this ``Model`` with SageMaker Edge.
499+
500+ Creates a new EdgePackagingJob and wait for it to finish.
501+ model_data will now point to the packaged artifacts.
502+
503+ Args:
504+ output_path (str): Specifies where to store the packaged model
505+ role (str): Execution role
506+ model_name (str): the name to attach to the model metadata
507+ model_version (str): the version to attach to the model metadata
508+ job_name (str): The name of the edge packaging job
509+ resource_key (str): the kms key to encrypt the disk with
510+ s3_kms_key (str): the kms key to encrypt the output with
511+ tags (list[dict]): List of tags for labeling an edge packaging job. For
512+ more, see
513+ https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
514+
515+ Returns:
516+ sagemaker.model.Model: A SageMaker ``Model`` object. See
517+ :func:`~sagemaker.model.Model` for full details.
518+ """
519+ if self ._compilation_job_name is None :
520+ raise ValueError ("You must first compile this model" )
521+ if job_name is None :
522+ job_name = f"packaging{ self ._compilation_job_name [11 :]} "
523+ if role is None :
524+ role = self .sagemaker_session .expand_role (role )
525+
526+ self ._init_sagemaker_session_if_does_not_exist (None )
527+ config = self ._edge_packaging_job_config (
528+ output_path ,
529+ role ,
530+ model_name ,
531+ model_version ,
532+ job_name ,
533+ self ._compilation_job_name ,
534+ resource_key ,
535+ s3_kms_key ,
536+ tags ,
537+ )
538+ self .sagemaker_session .package_model_for_edge (** config )
539+ job_status = self .sagemaker_session .wait_for_edge_packaging_job (job_name )
540+ self .model_data = job_status ["ModelArtifact" ]
541+ self ._is_edge_packaged_model = True
542+
543+ return self
544+
441545 def compile (
442546 self ,
443547 target_instance_family ,
@@ -557,6 +661,8 @@ def compile(
557661 "supported for deployment via SageMaker. Please deploy the model manually."
558662 )
559663
664+ self ._compilation_job_name = job_name
665+
560666 return self
561667
562668 def deploy (
0 commit comments