Skip to content

breaking: Add parameters to deploy and remove parameters from create_model #1783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions doc/frameworks/tensorflow/upgrade_from_legacy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ For example, if you want to use JSON serialization and deserialization:
from sagemaker.deserializers import JSONDeserializer
from sagemaker.serializers import JSONSerializer

predictor.serializer = JSONSerializer()
predictor.deserializer = JSONDeserializer()
predictor = model.deploy(..., serializer=JSONSerializer(), deserializer=JSONDeserializer())

predictor.predict(data)
8 changes: 5 additions & 3 deletions doc/frameworks/xgboost/using_xgboost.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ inference against your model.

.. code::

serializer = StringSerializer()
serializer.CONTENT_TYPE = "text/libsvm"
Comment on lines +195 to +196
Copy link
Contributor Author

@bveeramani bveeramani Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty ugly, but it'll be replaced with LibSVMSerializer (#1776) when that gets merged.


predictor = estimator.deploy(
initial_instance_count=1,
instance_type="ml.m5.xlarge"
instance_type="ml.m5.xlarge",
serializer=serializer
)
predictor.serializer = str
predictor.content_type = "text/libsvm"

with open("abalone") as f:
payload = f.read()
Expand Down
7 changes: 7 additions & 0 deletions doc/v2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ Please instantiate the objects instead.
The ``update_endpoint`` argument in ``deploy()`` methods for estimators and models has been deprecated.
Please use :func:`sagemaker.predictor.Predictor.update_endpoint` instead.

``serializer`` and ``deserializer`` in ``create_model()``
---------------------------------------------------------

The ``serializer`` and ``deserializer`` arguments in
:func:`sagemaker.estimator.Estimator.create_model` have been deprecated. Please
specify serializers and deserializers in ``deploy()`` methods instead.

``content_type`` and ``accept`` in the Predictor Constructor
------------------------------------------------------------

Expand Down
14 changes: 14 additions & 0 deletions src/sagemaker/automl/automl.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
candidate=None,
sagemaker_session=None,
name=None,
Expand All @@ -356,6 +358,16 @@ def deploy(
in the ``Endpoint`` created from this ``Model``.
instance_type (str): The EC2 instance type to deploy this Model to.
For example, 'ml.p2.xlarge'.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: None). If ``serializer`` is not None, then
``serializer`` will override the default serializer. The
default serializer is set by the ``predictor_cls``.
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: None). If ``deserializer`` is not None, then
``deserializer`` will override the default deserializer. The
default deserializer is set by the ``predictor_cls``.
candidate (CandidateEstimator or dict): a CandidateEstimator used for deploying
to a SageMaker Inference Pipeline. If None, the best candidate will
be used. If the candidate input is a dict, a CandidateEstimator will be
Expand Down Expand Up @@ -405,6 +417,8 @@ def deploy(
return model.deploy(
initial_instance_count=initial_instance_count,
instance_type=instance_type,
serializer=serializer,
deserializer=deserializer,
endpoint_name=endpoint_name,
tags=tags,
wait=wait,
Expand Down
26 changes: 15 additions & 11 deletions src/sagemaker/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
from sagemaker.debugger import DebuggerHookConfig
from sagemaker.debugger import TensorBoardOutputConfig # noqa: F401 # pylint: disable=unused-import
from sagemaker.debugger import get_rule_container_image_uri
from sagemaker.deserializers import BytesDeserializer
from sagemaker.s3 import S3Uploader, parse_s3_url
from sagemaker.serializers import IdentitySerializer

from sagemaker.fw_utils import (
tar_and_upload_dir,
Expand Down Expand Up @@ -670,6 +668,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
accelerator_type=None,
endpoint_name=None,
use_compiled_model=False,
Expand All @@ -691,6 +691,16 @@ def deploy(
deploy to an endpoint for prediction.
instance_type (str): Type of EC2 instance to deploy to an endpoint
for prediction, for example, 'ml.c4.xlarge'.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: None). If ``serializer`` is not None, then
``serializer`` will override the default serializer. The
default serializer is set by the ``predictor_cls``.
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: None). If ``deserializer`` is not None, then
``deserializer`` will override the default deserializer. The
default deserializer is set by the ``predictor_cls``.
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
Expand Down Expand Up @@ -753,6 +763,8 @@ def deploy(
return model.deploy(
instance_type=instance_type,
initial_instance_count=initial_instance_count,
serializer=serializer,
deserializer=deserializer,
accelerator_type=accelerator_type,
endpoint_name=endpoint_name,
tags=tags or self.tags,
Expand Down Expand Up @@ -1367,8 +1379,6 @@ def create_model(
role=None,
image_uri=None,
predictor_cls=None,
serializer=IdentitySerializer(),
deserializer=BytesDeserializer(),
vpc_config_override=vpc_utils.VPC_CONFIG_DEFAULT,
**kwargs
):
Expand All @@ -1386,12 +1396,6 @@ def create_model(
Defaults to the image used for training.
predictor_cls (Predictor): The predictor class to use when
deploying the model.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: :class:`~sagemaker.serializers.IdentitySerializer`).
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: :class:`~sagemaker.deserializers.BytesDeserializer`).
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on
the model.
Default: use subnets and security groups from this Estimator.
Expand All @@ -1410,7 +1414,7 @@ def create_model(
if predictor_cls is None:

def predict_wrapper(endpoint, session):
return Predictor(endpoint, session, serializer, deserializer)
return Predictor(endpoint, session)

predictor_cls = predict_wrapper

Expand Down
19 changes: 18 additions & 1 deletion src/sagemaker/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
accelerator_type=None,
endpoint_name=None,
tags=None,
Expand All @@ -433,6 +435,16 @@ def deploy(
in the ``Endpoint`` created from this ``Model``.
instance_type (str): The EC2 instance type to deploy this Model to.
For example, 'ml.p2.xlarge', or 'local' for local mode.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: None). If ``serializer`` is not None, then
``serializer`` will override the default serializer. The
default serializer is set by the ``predictor_cls``.
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: None). If ``deserializer`` is not None, then
``deserializer`` will override the default deserializer. The
default deserializer is set by the ``predictor_cls``.
accelerator_type (str): Type of Elastic Inference accelerator to
deploy this model for model loading and inference, for example,
'ml.eia1.medium'. If not specified, no Elastic Inference
Expand Down Expand Up @@ -499,7 +511,12 @@ def deploy(
)

if self.predictor_cls:
return self.predictor_cls(self.endpoint_name, self.sagemaker_session)
predictor = self.predictor_cls(self.endpoint_name, self.sagemaker_session)
if serializer:
predictor.serializer = serializer
if deserializer:
predictor.deserializer = deserializer
return predictor
return None

def transformer(
Expand Down
25 changes: 21 additions & 4 deletions src/sagemaker/multidatamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
accelerator_type=None,
endpoint_name=None,
tags=None,
Expand Down Expand Up @@ -171,6 +173,16 @@ def deploy(
in the ``Endpoint`` created from this ``Model``.
instance_type (str): The EC2 instance type to deploy this Model to.
For example, 'ml.p2.xlarge', or 'local' for local mode.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: None). If ``serializer`` is not None, then
``serializer`` will override the default serializer. The
default serializer is set by the ``predictor_cls``.
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: None). If ``deserializer`` is not None, then
``deserializer`` will override the default deserializer. The
default deserializer is set by the ``predictor_cls``.
accelerator_type (str): Type of Elastic Inference accelerator to
deploy this model for model loading and inference, for example,
'ml.eia1.medium'. If not specified, no Elastic Inference
Expand Down Expand Up @@ -201,12 +213,12 @@ def deploy(
enable_network_isolation = self.model.enable_network_isolation()
role = self.model.role
vpc_config = self.model.vpc_config
predictor = self.model.predictor_cls
predictor_cls = self.model.predictor_cls
else:
enable_network_isolation = self.enable_network_isolation()
role = self.role
vpc_config = self.vpc_config
predictor = self.predictor_cls
predictor_cls = self.predictor_cls

if role is None:
raise ValueError("Role can not be null for deploying a model")
Expand Down Expand Up @@ -245,8 +257,13 @@ def deploy(
data_capture_config_dict=data_capture_config_dict,
)

if predictor:
return predictor(self.endpoint_name, self.sagemaker_session)
if predictor_cls:
predictor = predictor_cls(self.endpoint_name, self.sagemaker_session)
if serializer:
predictor.serializer = serializer
if deserializer:
predictor.deserializer = deserializer
return predictor
return None

def add_model(self, model_data_source, model_data_path=None):
Expand Down
19 changes: 18 additions & 1 deletion src/sagemaker/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
endpoint_name=None,
tags=None,
wait=True,
Expand All @@ -110,6 +112,16 @@ def deploy(
in the ``Endpoint`` created from this ``Model``.
instance_type (str): The EC2 instance type to deploy this Model to.
For example, 'ml.p2.xlarge'.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: None). If ``serializer`` is not None, then
``serializer`` will override the default serializer. The
default serializer is set by the ``predictor_cls``.
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: None). If ``deserializer`` is not None, then
``deserializer`` will override the default deserializer. The
default deserializer is set by the ``predictor_cls``.
endpoint_name (str): The name of the endpoint to create (default:
None). If not specified, a unique endpoint name will be created.
tags (List[dict[str, str]]): The list of tags to attach to this
Expand Down Expand Up @@ -171,7 +183,12 @@ def deploy(
)

if self.predictor_cls:
return self.predictor_cls(self.endpoint_name, self.sagemaker_session)
predictor = self.predictor_cls(self.endpoint_name, self.sagemaker_session)
if serializer:
predictor.serializer = serializer
if deserializer:
predictor.deserializer = deserializer
return predictor
return None

def _create_sagemaker_pipeline_model(self, instance_type):
Expand Down
12 changes: 10 additions & 2 deletions src/sagemaker/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def __init__(
self.sagemaker_session = sagemaker_session or Session()
self.serializer = serializer
self.deserializer = deserializer
self.content_type = serializer.CONTENT_TYPE
self.accept = deserializer.ACCEPT
self._endpoint_config_name = self._get_endpoint_config_name()
self._model_names = self._get_model_names()

Expand Down Expand Up @@ -380,3 +378,13 @@ def _get_model_names(self):
)
production_variants = endpoint_config["ProductionVariants"]
return [d["ModelName"] for d in production_variants]

@property
def content_type(self):
"""The MIME type of the data sent to the inference endpoint."""
return self.serializer.CONTENT_TYPE

@property
def accept(self):
"""The content type(s) that are expected from the inference endpoint."""
return self.deserializer.ACCEPT
4 changes: 4 additions & 0 deletions src/sagemaker/tensorflow/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
accelerator_type=None,
endpoint_name=None,
tags=None,
Expand All @@ -211,6 +213,8 @@ def deploy(
return super(TensorFlowModel, self).deploy(
initial_instance_count=initial_instance_count,
instance_type=instance_type,
serializer=serializer,
deserializer=deserializer,
accelerator_type=accelerator_type,
endpoint_name=endpoint_name,
tags=tags,
Expand Down
14 changes: 14 additions & 0 deletions src/sagemaker/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ def deploy(
self,
initial_instance_count,
instance_type,
serializer=None,
deserializer=None,
accelerator_type=None,
endpoint_name=None,
wait=True,
Expand All @@ -693,6 +695,16 @@ def deploy(
deploy to an endpoint for prediction.
instance_type (str): Type of EC2 instance to deploy to an endpoint
for prediction, for example, 'ml.c4.xlarge'.
serializer (:class:`~sagemaker.serializers.BaseSerializer`): A
serializer object, used to encode data for an inference endpoint
(default: None). If ``serializer`` is not None, then
``serializer`` will override the default serializer. The
default serializer is set by the ``predictor_cls``.
deserializer (:class:`~sagemaker.deserializers.BaseDeserializer`): A
deserializer object, used to decode data from an inference
endpoint (default: None). If ``deserializer`` is not None, then
``deserializer`` will override the default deserializer. The
default deserializer is set by the ``predictor_cls``.
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
Expand Down Expand Up @@ -727,6 +739,8 @@ def deploy(
return best_estimator.deploy(
initial_instance_count=initial_instance_count,
instance_type=instance_type,
serializer=serializer,
deserializer=deserializer,
accelerator_type=accelerator_type,
endpoint_name=endpoint_name or best_training_job["TrainingJobName"],
wait=wait,
Expand Down
Loading