Skip to content

fix: create the correct session for MultiDataModel #1255

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
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/sagemaker/local/local_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def invoke_endpoint(
ContentType=None,
Accept=None,
CustomAttributes=None,
TargetModel=None,
):
"""

Expand All @@ -365,6 +366,9 @@ def invoke_endpoint(
if CustomAttributes is not None:
headers["X-Amzn-SageMaker-Custom-Attributes"] = CustomAttributes

if TargetModel is not None:
headers["X-Amzn-SageMaker-Target-Model"] = TargetModel

r = self.http.request("POST", url, body=Body, preload_content=False, headers=headers)

return {"Body": r, "ContentType": Accept}
Expand Down
5 changes: 4 additions & 1 deletion src/sagemaker/multidatamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from six.moves.urllib.parse import urlparse

import sagemaker
from sagemaker import s3
from sagemaker import local, s3
from sagemaker.model import Model
from sagemaker.session import Session

Expand Down Expand Up @@ -210,6 +210,9 @@ def deploy(
if role is None:
raise ValueError("Role can not be null for deploying a model")

if instance_type == "local" and not isinstance(self.sagemaker_session, local.LocalSession):
self.sagemaker_session = local.LocalSession()

container_def = self.prepare_container_def(instance_type, accelerator_type=accelerator_type)
self.sagemaker_session.create_model(
self.name,
Expand Down
63 changes: 63 additions & 0 deletions tests/integ/test_multidatamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,69 @@ def test_multi_data_model_deploy_pretrained_models(
assert "Could not find endpoint" in str(exception.value)


@pytest.mark.local_mode
def test_multi_data_model_deploy_pretrained_models_local_mode(
container_image, sagemaker_session, cpu_instance_type
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: remove unused parameter cpu_instance_type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Removed.

):
timestamp = sagemaker_timestamp()
endpoint_name = "test-multimodel-endpoint-{}".format(timestamp)
model_name = "test-multimodel-{}".format(timestamp)

# Define pretrained model local path
pretrained_model_data_local_path = os.path.join(DATA_DIR, "sparkml_model", "mleap_model.tar.gz")

with timeout(minutes=30):
model_data_prefix = os.path.join(
"s3://", sagemaker_session.default_bucket(), "multimodel-{}/".format(timestamp)
)
multi_data_model = MultiDataModel(
name=model_name,
model_data_prefix=model_data_prefix,
image=container_image,
role=ROLE,
sagemaker_session=sagemaker_session,
)

# Add model before deploy
multi_data_model.add_model(pretrained_model_data_local_path, PRETRAINED_MODEL_PATH_1)
# Deploy model to an endpoint
multi_data_model.deploy(1, "local", endpoint_name=endpoint_name)
# Add models after deploy
multi_data_model.add_model(pretrained_model_data_local_path, PRETRAINED_MODEL_PATH_2)

endpoint_models = []
for model_path in multi_data_model.list_models():
endpoint_models.append(model_path)
assert PRETRAINED_MODEL_PATH_1 in endpoint_models
assert PRETRAINED_MODEL_PATH_2 in endpoint_models

predictor = RealTimePredictor(
endpoint=endpoint_name,
sagemaker_session=multi_data_model.sagemaker_session,
serializer=npy_serializer,
deserializer=string_deserializer,
)

data = numpy.zeros(shape=(1, 1, 28, 28))
result = predictor.predict(data, target_model=PRETRAINED_MODEL_PATH_1)
assert result == "Invoked model: {}".format(PRETRAINED_MODEL_PATH_1)

result = predictor.predict(data, target_model=PRETRAINED_MODEL_PATH_2)
assert result == "Invoked model: {}".format(PRETRAINED_MODEL_PATH_2)

# Cleanup
multi_data_model.sagemaker_session.sagemaker_client.delete_endpoint_config(
EndpointConfigName=endpoint_name
)
multi_data_model.sagemaker_session.delete_endpoint(endpoint_name)
multi_data_model.delete_model()
with pytest.raises(Exception) as exception:
sagemaker_session.sagemaker_client.describe_model(ModelName=multi_data_model.name)
assert "Could not find model" in str(exception.value)
sagemaker_session.sagemaker_client.describe_endpoint_config(name=endpoint_name)
assert "Could not find endpoint" in str(exception.value)


def test_multi_data_model_deploy_trained_model_from_framework_estimator(
container_image, sagemaker_session, cpu_instance_type
):
Expand Down