Skip to content

Commit a907597

Browse files
Wei-1mvsusp
authored andcommitted
fix issue-987 error by adding instance_type in endpoint_name (#1058)
1 parent 83d53b8 commit a907597

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

src/sagemaker/model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,14 @@ def deploy(
446446
if endpoint_name:
447447
self.endpoint_name = endpoint_name
448448
else:
449-
self.endpoint_name = self.name
449+
self.endpoint_name = self.name + "-" + instance_type.replace(".", "-")
450450
if self._is_compiled_model and not self.endpoint_name.endswith(compiled_model_suffix):
451451
self.endpoint_name += compiled_model_suffix
452452

453453
if update_endpoint:
454+
self.sagemaker_session.delete_endpoint_config(endpoint_config_name=self.endpoint_name)
454455
endpoint_config_name = self.sagemaker_session.create_endpoint_config(
455-
name=self.name,
456+
name=self.endpoint_name,
456457
model_name=self.name,
457458
initial_instance_count=initial_instance_count,
458459
instance_type=instance_type,

src/sagemaker/session.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,39 +1297,47 @@ def endpoint_from_model_data(
12971297
"""
12981298

12991299
model_environment_vars = model_environment_vars or {}
1300-
name = name or name_from_image(deployment_image)
1300+
model_name = name or name_from_image(deployment_image)
1301+
endpoint_name = name or (
1302+
name_from_image(deployment_image) + "-" + instance_type.replace(".", "-")
1303+
)
13011304
model_vpc_config = vpc_utils.sanitize(model_vpc_config)
13021305

13031306
if _deployment_entity_exists(
1304-
lambda: self.sagemaker_client.describe_endpoint(EndpointName=name)
1307+
lambda: self.sagemaker_client.describe_endpoint(EndpointName=endpoint_name)
13051308
):
13061309
raise ValueError(
1307-
'Endpoint with name "{}" already exists; please pick a different name.'.format(name)
1310+
'Endpoint with name "{}" already exists; please pick a different name.'.format(
1311+
endpoint_name
1312+
)
13081313
)
13091314

13101315
if not _deployment_entity_exists(
1311-
lambda: self.sagemaker_client.describe_model(ModelName=name)
1316+
lambda: self.sagemaker_client.describe_model(ModelName=model_name)
13121317
):
13131318
primary_container = container_def(
13141319
image=deployment_image, model_data_url=model_s3_location, env=model_environment_vars
13151320
)
13161321
self.create_model(
1317-
name=name, role=role, container_defs=primary_container, vpc_config=model_vpc_config
1322+
name=model_name,
1323+
role=role,
1324+
container_defs=primary_container,
1325+
vpc_config=model_vpc_config,
13181326
)
13191327

13201328
if not _deployment_entity_exists(
1321-
lambda: self.sagemaker_client.describe_endpoint_config(EndpointConfigName=name)
1329+
lambda: self.sagemaker_client.describe_endpoint_config(EndpointConfigName=endpoint_name)
13221330
):
13231331
self.create_endpoint_config(
1324-
name=name,
1325-
model_name=name,
1332+
name=endpoint_name,
1333+
model_name=model_name,
13261334
initial_instance_count=initial_instance_count,
13271335
instance_type=instance_type,
13281336
accelerator_type=accelerator_type,
13291337
)
13301338

1331-
self.create_endpoint(endpoint_name=name, config_name=name, wait=wait)
1332-
return name
1339+
self.create_endpoint(endpoint_name=endpoint_name, config_name=endpoint_name, wait=wait)
1340+
return endpoint_name
13331341

13341342
def endpoint_from_production_variants(
13351343
self, name, production_variants, tags=None, kms_key=None, wait=True

tests/integ/test_mxnet_train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_deploy_model_with_update_endpoint(
196196
EndpointConfigName=new_config_name
197197
)
198198

199-
assert old_config_name != new_config_name
199+
assert old_config_name == new_config_name
200200
assert new_config["ProductionVariants"][0]["InstanceType"] == cpu_instance_type
201201
assert new_config["ProductionVariants"][0]["InitialInstanceCount"] == 1
202202

tests/unit/test_endpoint_from_model_data.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
DEPLOY_ROLE = "mydeployrole"
3131
ENV_VARS = {"PYTHONUNBUFFERED": "TRUE", "some": "nonsense"}
3232
NAME_FROM_IMAGE = "namefromimage"
33+
DEFAULT_ENDPOINT_NAME = "namefromimage-ml-c4-xlarge"
3334
REGION = "us-west-2"
3435

3536

@@ -64,28 +65,28 @@ def test_all_defaults_no_existing_entities(name_from_image_mock, sagemaker_sessi
6465
)
6566

6667
sagemaker_session.sagemaker_client.describe_endpoint.assert_called_once_with(
67-
EndpointName=NAME_FROM_IMAGE
68+
EndpointName=DEFAULT_ENDPOINT_NAME
6869
)
6970
sagemaker_session.sagemaker_client.describe_model.assert_called_once_with(
7071
ModelName=NAME_FROM_IMAGE
7172
)
7273
sagemaker_session.sagemaker_client.describe_endpoint_config.assert_called_once_with(
73-
EndpointConfigName=NAME_FROM_IMAGE
74+
EndpointConfigName=DEFAULT_ENDPOINT_NAME
7475
)
7576
sagemaker_session.create_model.assert_called_once_with(
7677
name=NAME_FROM_IMAGE, role=DEPLOY_ROLE, container_defs=CONTAINER_DEF, vpc_config=None
7778
)
7879
sagemaker_session.create_endpoint_config.assert_called_once_with(
79-
name=NAME_FROM_IMAGE,
80+
name=DEFAULT_ENDPOINT_NAME,
8081
model_name=NAME_FROM_IMAGE,
8182
initial_instance_count=INITIAL_INSTANCE_COUNT,
8283
instance_type=INSTANCE_TYPE,
8384
accelerator_type=None,
8485
)
8586
sagemaker_session.create_endpoint.assert_called_once_with(
86-
endpoint_name=NAME_FROM_IMAGE, config_name=NAME_FROM_IMAGE, wait=False
87+
endpoint_name=DEFAULT_ENDPOINT_NAME, config_name=DEFAULT_ENDPOINT_NAME, wait=False
8788
)
88-
assert returned_name == NAME_FROM_IMAGE
89+
assert returned_name == DEFAULT_ENDPOINT_NAME
8990

9091

9192
@patch("sagemaker.session.name_from_image", return_value=NAME_FROM_IMAGE)
@@ -152,7 +153,7 @@ def test_model_and_endpoint_config_exist(name_from_image_mock, sagemaker_session
152153
sagemaker_session.create_model.assert_not_called()
153154
sagemaker_session.create_endpoint_config.assert_not_called()
154155
sagemaker_session.create_endpoint.assert_called_once_with(
155-
endpoint_name=NAME_FROM_IMAGE, config_name=NAME_FROM_IMAGE, wait=False
156+
endpoint_name=DEFAULT_ENDPOINT_NAME, config_name=DEFAULT_ENDPOINT_NAME, wait=False
156157
)
157158

158159

tests/unit/test_model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
MODEL_DATA = "s3://bucket/model.tar.gz"
2727
MODEL_IMAGE = "mi"
2828
ENTRY_POINT = "blah.py"
29-
INSTANCE_TYPE = "p2.xlarge"
3029
ROLE = "some-role"
3130

3231
DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data")
@@ -40,6 +39,7 @@
4039
IMAGE_NAME = "fakeimage"
4140
REGION = "us-west-2"
4241
MODEL_NAME = "{}-{}".format(MODEL_IMAGE, TIMESTAMP)
42+
ENDPOINT_NAME = "{}-{}".format(MODEL_NAME, INSTANCE_TYPE.replace(".", "-"))
4343
GIT_REPO = "https://github.com/aws/sagemaker-python-sdk.git"
4444
BRANCH = "test-branch-git-config"
4545
COMMIT = "ae15c9d7d5b97ea95ea451e4662ee43da3401d73"
@@ -210,7 +210,7 @@ def test_deploy(sagemaker_session, tmpdir):
210210
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
211211
model.deploy(instance_type=INSTANCE_TYPE, initial_instance_count=1)
212212
sagemaker_session.endpoint_from_production_variants.assert_called_with(
213-
MODEL_NAME,
213+
ENDPOINT_NAME,
214214
[
215215
{
216216
"InitialVariantWeight": 1,
@@ -255,7 +255,7 @@ def test_deploy_tags(sagemaker_session, tmpdir):
255255
tags = [{"ModelName": "TestModel"}]
256256
model.deploy(instance_type=INSTANCE_TYPE, initial_instance_count=1, tags=tags)
257257
sagemaker_session.endpoint_from_production_variants.assert_called_with(
258-
MODEL_NAME,
258+
ENDPOINT_NAME,
259259
[
260260
{
261261
"InitialVariantWeight": 1,
@@ -280,7 +280,7 @@ def test_deploy_accelerator_type(tfo, time, sagemaker_session):
280280
instance_type=INSTANCE_TYPE, initial_instance_count=1, accelerator_type=ACCELERATOR_TYPE
281281
)
282282
sagemaker_session.endpoint_from_production_variants.assert_called_with(
283-
MODEL_NAME,
283+
ENDPOINT_NAME,
284284
[
285285
{
286286
"InitialVariantWeight": 1,
@@ -305,7 +305,7 @@ def test_deploy_kms_key(tfo, time, sagemaker_session):
305305
model = DummyFrameworkModel(sagemaker_session)
306306
model.deploy(instance_type=INSTANCE_TYPE, initial_instance_count=1, kms_key=key)
307307
sagemaker_session.endpoint_from_production_variants.assert_called_with(
308-
MODEL_NAME,
308+
ENDPOINT_NAME,
309309
[
310310
{
311311
"InitialVariantWeight": 1,
@@ -350,7 +350,7 @@ def test_deploy_update_endpoint(sagemaker_session, tmpdir):
350350
accelerator_type=ACCELERATOR_TYPE,
351351
)
352352
sagemaker_session.create_endpoint_config.assert_called_with(
353-
name=model.name,
353+
name=endpoint_name,
354354
model_name=model.name,
355355
initial_instance_count=INSTANCE_COUNT,
356356
instance_type=INSTANCE_TYPE,
@@ -359,7 +359,7 @@ def test_deploy_update_endpoint(sagemaker_session, tmpdir):
359359
kms_key=None,
360360
)
361361
config_name = sagemaker_session.create_endpoint_config(
362-
name=model.name,
362+
name=endpoint_name,
363363
model_name=model.name,
364364
initial_instance_count=INSTANCE_COUNT,
365365
instance_type=INSTANCE_TYPE,

0 commit comments

Comments
 (0)