Skip to content

Commit 6e09223

Browse files
author
Chuyang Deng
committed
Support for Predictor to delete SageMaker model.
1 parent bf6805f commit 6e09223

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ Here is an end to end example of how to use a SageMaker Estimator:
192192
# Tears down the SageMaker endpoint and endpoint configuration
193193
mxnet_predictor.delete_endpoint()
194194
195+
# Deletes SageMaker model
196+
mxnet_predictor.delete_model()
195197
196198
The example above will eventually delete both the SageMaker endpoint and endpoint configuration through `delete_endpoint()`. If you want to keep your SageMaker endpoint configuration, use the value False for the `delete_endpoint_config` parameter, as shown below.
197199

@@ -284,6 +286,9 @@ We can take the example in `Using Estimators <#using-estimators>`__ , and use e
284286
# Tears down the endpoint container and deletes the corresponding endpoint configuration
285287
mxnet_predictor.delete_endpoint()
286288
289+
# Deletes the model
290+
mxnet_predictor.delete_model()
291+
287292
288293
If you have an existing model and want to deploy it locally, don't specify a sagemaker_session argument to the ``MXNetModel`` constructor.
289294
The correct session is generated when you call ``model.deploy()``.
@@ -307,6 +312,9 @@ Here is an end-to-end example:
307312
# Tear down the endpoint container and delete the corresponding endpoint configuration
308313
predictor.delete_endpoint()
309314
315+
# Deletes the model
316+
predictor.delete_model()
317+
310318
311319
If you don't want to deploy your model locally, you can also choose to perform a Local Batch Transform Job. This is
312320
useful if you want to test your container before creating a Sagemaker Batch Transform Job. Note that the performance

src/sagemaker/predictor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ def delete_endpoint(self, delete_endpoint_config=True):
126126

127127
self.sagemaker_session.delete_endpoint(self.endpoint)
128128

129+
def delete_model(self):
130+
"""Deletes the Amazon SageMaker models backing this predictor.
131+
132+
"""
133+
model_names = self._get_model_names()
134+
for model_name in model_names:
135+
self.sagemaker_session.delete_model(model_name)
136+
137+
def _get_model_names(self):
138+
endpoint_desc = self.sagemaker_session.sagemaker_client.describe_endpoint(EndpointName=self.endpoint)
139+
endpoint_config_name = endpoint_desc['EndpointConfigName']
140+
endpoint_config = self.sagemaker_session.sagemaker_client.describe_endpoint_config(EndpointConfigName=
141+
endpoint_config_name)
142+
production_variants = endpoint_config['ProductionVariants']
143+
return map(lambda d: d['ModelName'], production_variants)
144+
129145

130146
class _CsvSerializer(object):
131147
def __init__(self):

tests/integ/test_kmeans.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def test_kmeans(sagemaker_session):
7575
assert record.label["closest_cluster"] is not None
7676
assert record.label["distance_to_cluster"] is not None
7777

78+
predictor.delete_model()
79+
with pytest.raises(Exception) as exception:
80+
sagemaker_session.sagemaker_client.describe_model(ModelName=model.name)
81+
assert 'Could not find model' in str(exception.value)
82+
7883

7984
def test_async_kmeans(sagemaker_session):
8085
training_job_name = ""

tests/integ/test_mxnet_train.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def test_deploy_model(mxnet_training_job, sagemaker_session, mxnet_full_version)
7272
data = numpy.zeros(shape=(1, 1, 28, 28))
7373
predictor.predict(data)
7474

75+
predictor.delete_model()
76+
with pytest.raises(Exception) as exception:
77+
sagemaker_session.sagemaker_client.describe_model(ModelName=model.name)
78+
assert 'Could not find model' in str(exception.value)
79+
7580

7681
def test_deploy_model_with_update_endpoint(mxnet_training_job, sagemaker_session, mxnet_full_version):
7782
endpoint_name = 'test-mxnet-deploy-model-{}'.format(sagemaker_timestamp())

tests/unit/test_predictor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,18 @@ def test_delete_endpoint_only():
466466

467467
sagemaker_session.delete_endpoint.assert_called_with(ENDPOINT)
468468
sagemaker_session.delete_endpoint_config.assert_not_called()
469+
470+
471+
def test_delete_model():
472+
endpoint_desc = {
473+
'ProductionVariants': [{
474+
'VariantName': 'my-model'
475+
}]
476+
}
477+
sagemaker_session = empty_sagemaker_session()
478+
sagemaker_session.sagemaker_client.describe_endpoint = Mock(return_value=endpoint_desc)
479+
predictor = RealTimePredictor(ENDPOINT, sagemaker_session=sagemaker_session)
480+
481+
predictor.delete_model()
482+
sagemaker_session.delete_model.assert_called_with('my-model')
483+

0 commit comments

Comments
 (0)