Skip to content

Commit cab3091

Browse files
authored
Merge branch 'master' into add_new_tensorflow_version
2 parents 9a91513 + 554ba08 commit cab3091

File tree

8 files changed

+232
-11
lines changed

8 files changed

+232
-11
lines changed

src/sagemaker/deprecations.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,46 @@ def renamed_warning(phrase):
5050
_warn(f"{phrase} has been renamed")
5151

5252

53+
def deprecation_warn(name, date, msg=None):
54+
"""Raise a warning for soon to be deprecated feature in sagemaker>=2
55+
56+
Args:
57+
name (str): Name of the feature
58+
date (str): the date when the feature will be deprecated
59+
msg (str): the prefix phrase of the warning message.
60+
"""
61+
_warn(f"{name} will be deprecated on {date}.{msg}")
62+
63+
64+
def deprecation_warning(date, msg=None):
65+
"""Decorator for raising deprecation warning for a feature in sagemaker>=2
66+
67+
Args:
68+
date (str): the date when the feature will be deprecated
69+
msg (str): the prefix phrase of the warning message.
70+
71+
Usage:
72+
@deprecation_warning(msg="message", date="date")
73+
def sample_function():
74+
print("xxxx....")
75+
76+
@deprecation_warning(msg="message", date="date")
77+
class SampleClass():
78+
def __init__(self):
79+
print("xxxx....")
80+
81+
"""
82+
83+
def deprecate(obj):
84+
def wrapper(*args, **kwargs):
85+
deprecation_warn(obj.__name__, date, msg)
86+
return obj(*args, **kwargs)
87+
88+
return wrapper
89+
90+
return deprecate
91+
92+
5393
def renamed_kwargs(old_name, new_name, value, kwargs):
5494
"""Checks if the deprecated argument is in kwargs
5595
@@ -106,6 +146,28 @@ def func(*args, **kwargs): # pylint: disable=W0613
106146
return func
107147

108148

149+
def deprecated(obj):
150+
"""Decorator for raising deprecated warning for a feature in sagemaker>=2
151+
152+
Usage:
153+
@deprecated
154+
def sample_function():
155+
print("xxxx....")
156+
157+
@deprecated
158+
class SampleClass():
159+
def __init__(self):
160+
print("xxxx....")
161+
162+
"""
163+
164+
def wrapper(*args, **kwargs):
165+
removed_warning(obj.__name__)
166+
return obj(*args, **kwargs)
167+
168+
return wrapper
169+
170+
109171
def deprecated_function(func, name):
110172
"""Wrap a function with a deprecation warning.
111173

src/sagemaker/serverless/model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
import botocore
2121

2222
from sagemaker.model import ModelBase
23-
23+
from sagemaker.deprecations import deprecation_warning
2424
from .predictor import LambdaPredictor
2525

2626

27+
@deprecation_warning(
28+
msg="Based on customer experience and feedback an"
29+
" alternative support will be added in near future",
30+
date="10/27/2021",
31+
)
2732
class LambdaModel(ModelBase):
2833
"""A model that can be deployed to Lambda."""
2934

src/sagemaker/serverless/predictor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020

2121
from sagemaker import deserializers, serializers
2222
from sagemaker.predictor import PredictorBase
23+
from sagemaker.deprecations import deprecation_warning
2324

2425

26+
@deprecation_warning(
27+
msg="Based on customer experience and feedback an"
28+
" alternative support will be added in near future",
29+
date="10/27/2021",
30+
)
2531
class LambdaPredictor(PredictorBase):
2632
"""A deployed model hosted on Lambda."""
2733

tests/integ/test_marketplace.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
from sagemaker import AlgorithmEstimator, ModelPackage
2525
from sagemaker.serializers import CSVSerializer
2626
from sagemaker.tuner import IntegerParameter, HyperparameterTuner
27-
from sagemaker.utils import sagemaker_timestamp
28-
from sagemaker.utils import _aws_partition
27+
from sagemaker.utils import sagemaker_timestamp, _aws_partition, unique_name_from_base
2928
from tests.integ import DATA_DIR
3029
from tests.integ.timeout import timeout, timeout_and_delete_endpoint_by_name
3130
from tests.integ.marketplace_utils import REGION_ACCOUNT_MAP
@@ -117,7 +116,7 @@ def test_marketplace_attach(sagemaker_session, cpu_instance_type):
117116
instance_count=1,
118117
instance_type=cpu_instance_type,
119118
sagemaker_session=sagemaker_session,
120-
base_job_name="test-marketplace",
119+
base_job_name=unique_name_from_base("test-marketplace"),
121120
)
122121

123122
train_input = mktplace.sagemaker_session.upload_data(
@@ -205,7 +204,7 @@ def test_marketplace_tuning_job(sagemaker_session, cpu_instance_type):
205204
instance_count=1,
206205
instance_type=cpu_instance_type,
207206
sagemaker_session=sagemaker_session,
208-
base_job_name="test-marketplace",
207+
base_job_name=unique_name_from_base("test-marketplace"),
209208
)
210209

211210
train_input = mktplace.sagemaker_session.upload_data(
@@ -218,7 +217,7 @@ def test_marketplace_tuning_job(sagemaker_session, cpu_instance_type):
218217

219218
tuner = HyperparameterTuner(
220219
estimator=mktplace,
221-
base_tuning_job_name="byo",
220+
base_tuning_job_name=unique_name_from_base("byo"),
222221
objective_metric_name="validation:accuracy",
223222
hyperparameter_ranges=hyperparameter_ranges,
224223
max_jobs=2,
@@ -248,7 +247,7 @@ def test_marketplace_transform_job(sagemaker_session, cpu_instance_type):
248247
instance_count=1,
249248
instance_type=cpu_instance_type,
250249
sagemaker_session=sagemaker_session,
251-
base_job_name="test-marketplace",
250+
base_job_name=unique_name_from_base("test-marketplace"),
252251
)
253252

254253
train_input = algo.sagemaker_session.upload_data(

tests/integ/test_model_monitor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def predictor(sagemaker_session, tensorflow_inference_latest_version):
9292
key_prefix="tensorflow-serving/models",
9393
)
9494
with tests.integ.timeout.timeout_and_delete_endpoint_by_name(
95-
endpoint_name=endpoint_name, sagemaker_session=sagemaker_session, hours=2
95+
endpoint_name=endpoint_name,
96+
sagemaker_session=sagemaker_session,
97+
hours=2,
98+
sleep_between_cleanup_attempts=20,
99+
exponential_sleep=True,
96100
):
97101
model = TensorFlowModel(
98102
model_data=model_data,

tests/integ/timeout.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def timeout_and_delete_endpoint_by_name(
5454
minutes=45,
5555
hours=0,
5656
sleep_between_cleanup_attempts=10,
57+
exponential_sleep=False,
5758
):
5859
limit = seconds + 60 * minutes + 3600 * hours
5960

@@ -83,7 +84,13 @@ def timeout_and_delete_endpoint_by_name(
8384
# avoids the inner exception to be overwritten
8485
pass
8586
# trying to delete the resource again in 10 seconds
86-
sleep(sleep_between_cleanup_attempts)
87+
if exponential_sleep:
88+
_sleep_between_cleanup_attempts = sleep_between_cleanup_attempts * (
89+
3 - attempts
90+
)
91+
else:
92+
_sleep_between_cleanup_attempts = sleep_between_cleanup_attempts
93+
sleep(_sleep_between_cleanup_attempts)
8794

8895

8996
@contextmanager
@@ -150,7 +157,9 @@ def _delete_schedules_associated_with_endpoint(sagemaker_session, endpoint_name)
150157
monitor.delete_monitoring_schedule()
151158
except Exception as e:
152159
LOGGER.warning(
153-
"Failed to delete monitor {}".format(monitor.monitoring_schedule_name), e
160+
"Failed to delete monitor {},\nError: {}".format(
161+
monitor.monitoring_schedule_name, e
162+
)
154163
)
155164

156165

tests/unit/test_deprecations.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
removed_function,
2424
removed_kwargs,
2525
renamed_kwargs,
26+
deprecation_warning,
27+
deprecated,
2628
)
2729

2830

@@ -60,6 +62,106 @@ def test_removed_kwargs():
6062
removed_kwargs("b", kwarg)
6163

6264

65+
def test_deprecation_warning_for_function():
66+
@deprecation_warning(msg="message", date="date")
67+
def sample_function():
68+
return "xxxx...."
69+
70+
with pytest.warns(DeprecationWarning) as w:
71+
output = sample_function()
72+
assert output == "xxxx...."
73+
msg = (
74+
"sample_function will be deprecated on date.message in sagemaker>=2.\n"
75+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
76+
)
77+
assert str(w[-1].message) == msg
78+
79+
80+
def test_deprecation_warning_for_class():
81+
@deprecation_warning(msg="message", date="date")
82+
class SampleClass:
83+
def __init__(self):
84+
pass
85+
86+
with pytest.warns(DeprecationWarning) as w:
87+
SampleClass()
88+
msg = (
89+
"SampleClass will be deprecated on date.message in sagemaker>=2.\n"
90+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
91+
)
92+
assert str(w[-1].message) == msg
93+
94+
95+
def test_deprecation_warning_for_class_method():
96+
class SampleClass:
97+
def __init__(self):
98+
pass
99+
100+
@deprecation_warning(msg="message", date="date")
101+
def sample_method(self):
102+
return "xxxx...."
103+
104+
s = SampleClass()
105+
with pytest.warns(DeprecationWarning) as w:
106+
output = s.sample_method()
107+
assert output == "xxxx...."
108+
msg = (
109+
"sample_method will be deprecated on date.message in sagemaker>=2.\n"
110+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
111+
)
112+
assert str(w[-1].message) == msg
113+
114+
115+
def test_deprecated_for_function():
116+
@deprecated
117+
def sample_function():
118+
return "xxxx...."
119+
120+
with pytest.warns(DeprecationWarning) as w:
121+
output = sample_function()
122+
assert output == "xxxx...."
123+
msg = (
124+
"sample_function is a no-op in sagemaker>=2.\n"
125+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
126+
)
127+
assert str(w[-1].message) == msg
128+
129+
130+
def test_deprecated_for_class():
131+
@deprecated
132+
class SampleClass:
133+
def __init__(self):
134+
pass
135+
136+
with pytest.warns(DeprecationWarning) as w:
137+
SampleClass()
138+
msg = (
139+
"SampleClass is a no-op in sagemaker>=2.\n"
140+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
141+
)
142+
assert str(w[-1].message) == msg
143+
144+
145+
def test_deprecated_for_class_method():
146+
class SampleClass:
147+
def __init__(self):
148+
pass
149+
150+
@deprecated
151+
def sample_method(self):
152+
return "xxxx...."
153+
154+
s = SampleClass()
155+
with pytest.warns(DeprecationWarning) as w:
156+
output = s.sample_method()
157+
assert output == "xxxx...."
158+
msg = (
159+
"sample_method is a no-op in sagemaker>=2.\n"
160+
"See: https://sagemaker.readthedocs.io/en/stable/v2.html for details."
161+
)
162+
assert str(w[-1].message) == msg
163+
164+
63165
def test_removed_function():
64166
removed = removed_function("foo")
65167
with pytest.warns(DeprecationWarning):

tests/unit/test_timeout.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import time
2020

2121
import pytest
22-
from mock import Mock, patch
22+
from mock import Mock, patch, call
2323
import stopit
2424

2525
from botocore.exceptions import ClientError
@@ -44,6 +44,7 @@
4444
LONG_DURATION_TO_EXCEED_TIMEOUT = 0.002
4545
LONG_TIMEOUT_THAT_WILL_NEVER_BE_EXCEEDED = 10
4646
DURATION_TO_SLEEP_TO_ALLOW_BACKGROUND_THREAD_TO_COMPLETE = 0.2
47+
DURATION_TO_SLEEP = 0.01
4748

4849

4950
@pytest.fixture()
@@ -174,6 +175,39 @@ def test_timeout_and_delete_endpoint_by_name_retries_resource_deletion_on_failur
174175
assert session.delete_endpoint.call_count == 3
175176

176177

178+
@patch("tests.integ.timeout._show_logs", return_value=None, autospec=True)
179+
@patch("tests.integ.timeout._cleanup_logs", return_value=None, autospec=True)
180+
@patch(
181+
"tests.integ.timeout._delete_schedules_associated_with_endpoint",
182+
return_value=None,
183+
autospec=True,
184+
)
185+
@patch("tests.integ.timeout.sleep", return_value=None)
186+
def test_timeout_and_delete_endpoint_by_name_retries_resource_deletion_on_failure_with_exp_sleep(
187+
mock_sleep, _show_logs, _cleanup_logs, _delete_schedules_associated_with_endpoint, session
188+
):
189+
session.delete_endpoint = Mock(
190+
side_effect=ClientError(
191+
error_response={"Error": {"Code": 403, "Message": "ValidationException"}},
192+
operation_name="Unit Test",
193+
)
194+
)
195+
196+
with timeout_and_delete_endpoint_by_name(
197+
endpoint_name=ENDPOINT_NAME,
198+
sagemaker_session=session,
199+
hours=0,
200+
minutes=0,
201+
seconds=LONG_TIMEOUT_THAT_WILL_NEVER_BE_EXCEEDED,
202+
sleep_between_cleanup_attempts=DURATION_TO_SLEEP,
203+
exponential_sleep=True,
204+
):
205+
pass
206+
assert session.delete_endpoint.call_count == 3
207+
assert mock_sleep.call_count == 3
208+
assert mock_sleep.mock_calls == [call(0.01), call(0.02), call(0.03)]
209+
210+
177211
@patch("tests.integ.timeout._show_logs", return_value=None, autospec=True)
178212
@patch("tests.integ.timeout._cleanup_logs", return_value=None, autospec=True)
179213
@patch(

0 commit comments

Comments
 (0)