Skip to content

Add update_endpoint support to session class and add optional flag to… - #606

Merged
chuyang-deng merged 21 commits into
aws:masterfrom
chuyang-deng:master
Jan 29, 2019
Merged

chuyang-deng merged 21 commits into
aws:masterfrom
chuyang-deng:master

Conversation

@chuyang-deng

@chuyang-deng chuyang-deng commented Jan 18, 2019 •

Copy link
Copy Markdown
Contributor

Issue #, if available:
#58

Description of changes:
Add update_endpoint to support session class to update endpoint instead of creating new one. Add an optional flag in .deploy method accordingly.

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have updated the changelog with a description of my changes (if appropriate)
  • I have updated any necessary documentation (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov-io

codecov-io commented Jan 18, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #606 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #606      +/-   ##
==========================================
+ Coverage    92.7%   92.72%   +0.01%     
==========================================
  Files          71       71              
  Lines        5418     5428      +10     
==========================================
+ Hits         5023     5033      +10     
  Misses        395      395
Impacted Files Coverage Δ
src/sagemaker/estimator.py 90.48% <ø> (ø) ⬆️
src/sagemaker/model.py 80.22% <100%> (+0.34%) ⬆️
src/sagemaker/session.py 83.58% <100%> (+0.15%) ⬆️
src/sagemaker/local/local_session.py 85.84% <100%> (+0.27%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c94079...41be0ba. Read the comment docs.

@mvsusp mvsusp left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for your contribution!

Please add your changes to the changelog and update documentation as well.

@ChoiByungWook ChoiByungWook left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

Please update the changelog with your feature addition.

Let's add an integration test!

Comment thread src/sagemaker/local/local_session.py Resolved
Comment thread src/sagemaker/estimator.py Resolved
Comment thread src/sagemaker/model.py Resolved
Comment thread src/sagemaker/model.py Resolved
Comment thread tests/unit/test_local_session.py Resolved
Comment thread tests/unit/test_model.py Resolved
Comment thread tests/unit/test_session.py Resolved
@chuyang-deng

Copy link
Copy Markdown
Contributor Author

Address previous comments and add integration tests. New integration tests passed.

@ChoiByungWook ChoiByungWook left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Almost there!

Comment thread src/sagemaker/model.py Resolved
Comment thread CHANGELOG.rst
=========


1.18.1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's append .dev to the end, since this hasn't been released to PyPI yet.

Comment thread src/sagemaker/session.py Outdated
self.wait_for_endpoint(endpoint_name)
return endpoint_name

def update_endpoint(self, endpoint_name, config_name):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't catch this earlier either.

Can we rename config_name parameter to endpoint-config-name, so that we are consistent with https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html

Comment thread src/sagemaker/session.py Outdated

Args:
endpoint_name (str): Name of the Amazon SageMaker ``Endpoint`` to update.
config_name (str): Name of the Amazon SageMaker endpoint configuration to deploy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same thing here. Thanks!

Comment thread tests/integ/test_local_mode.py Outdated
predictor.delete_endpoint()


def test_local_deploy_with_update_endpoint(tmpdir, sagemaker_local_session, mxnet_model):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can remove this integration test. I don't think this is necessary, as the unit test already catches this use case.

If you want to keep the test, let's assert on the exception message and catch the specific exception as well.

Comment thread tests/unit/test_local_session.py Outdated
local_sagemaker_client = sagemaker.local.local_session.LocalSagemakerClient()
endpoint_name = 'my-endpoint'
endpoint_config = 'my-endpoint-config'
with pytest.raises(Exception) as exception:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same thing here.

Comment thread tests/integ/test_mxnet_train.py Outdated
predictor = model.deploy(1, 'ml.m4.xlarge', update_endpoint=True, endpoint_name=endpoint_name)
new_endpoint_config = sagemaker_session.describe_endpoint(EndpointName=endpoint_name)
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't have to do a prediction, since we are only testing the update_functionality. This use case is also fulfilled with the other deploy tests.

Comment thread tests/integ/test_mxnet_train.py Outdated
py_version=PYTHON_VERSION, sagemaker_session=sagemaker_session)
model.deploy(1, 'ml.t2.medium', endpoint_name=endpoint_name)
old_endpoint_config = sagemaker_session.describe_endpoint(EndpointName=endpoint_name)
predictor = model.deploy(1, 'ml.m4.xlarge', update_endpoint=True, endpoint_name=endpoint_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Up to you on this one as it is just a styling issue, however can we add a space here?

I think it will become a little easier to read, since we are splitting from the first deploy to the update.

Thanks!

Comment thread tests/integ/test_mxnet_train.py Outdated
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)

print(old_endpoint_config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would personally not include these print statements. If you do plan on leaving them, please prepend a description of what is being printed. For example:

print('old endpoint configuration: {}'.format(old_endpoint_config))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ohh, this is unintentional.

Comment thread tests/integ/test_mxnet_train.py Outdated
print()
print(new_endpoint_config)

if old_endpoint_config['EndpointConfigName'] == new_endpoint_config['EndpointConfigName']:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's add some assertions that the new_endpoint_config, the one that was deployed as part of the update, has the values that are expected.

You can either call describe_endpoint_config or do an assertion against the production variant which I believe should be a part of output for describe_endpoint done above.

https://docs.aws.amazon.com/cli/latest/reference/sagemaker/describe-endpoint-config.html
https://docs.aws.amazon.com/cli/latest/reference/sagemaker/describe-endpoint.html

@ChoiByungWook ChoiByungWook self-assigned this Jan 23, 2019
Comment thread src/sagemaker/local/local_session.py Outdated
endpoint.serve()

def update_endpoint(self, EndpointName, EndpointConfigName):
del EndpointName, EndpointConfigName

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You don't have to call del here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

pylint will complain if EndpointName, EndpointConfigName are not used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good call!

Let's add this: https://docs.pylint.org/en/latest/faq.html#do-i-have-to-remember-all-these-numbers

You can follow this line:

def create_model(self, ModelName, PrimaryContainer, *args, **kwargs): # pylint: disable=unused-argument

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please remove the del.

@ChoiByungWook ChoiByungWook left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks great!

Comment thread tests/integ/test_mxnet_train.py Outdated
new_production_variants = sagemaker_session.describe_endpoint(EndpointName=endpoint_name)['ProductionVariants']

assert new_production_variants['InstanceType'] == 'ml.m4.xlarge'
assert old_production_variants['VariantName'] != new_production_variants['VariantName']

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's assert that the sagemaker_session.describe_endpoint(EndpointName=endpoint_name)['EndpointConfigName'] aren't the same.

Other than that, I think doing the assertion of the values such as: assert new_production_variants['InstanceType'] == 'ml.m4.xlarge' would be better then doing a comparison.

Comment thread tests/integ/test_mxnet_train.py Outdated
model.deploy(1, 'ml.t2.medium', endpoint_name=endpoint_name)
sagemaker_session.describe_endpoint(EndpointName=endpoint_name)
model.deploy(1, 'ml.m4.xlarge', update_endpoint=True, endpoint_name='non-existing-endpoint')
except ValueError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think using the pytest.raises might look cleaner here.

with pytest.raises(ValueError):

Let's assert that the exception has the right error message.

Comment thread tests/integ/test_mxnet_train.py Outdated

assert old_config_name != new_config_name
assert new_production_variants['InstanceType'] == 'ml.m4.xlarge'
assert old_production_variants['InitialInstanceCount'] == 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's assert on the new_production_variants. Since that is the endpoint config we want to verify is being used.

Comment thread tests/unit/test_session.py Resolved
ChoiByungWook
ChoiByungWook previously approved these changes Jan 24, 2019

@ChoiByungWook ChoiByungWook left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NICE!!!

ChoiByungWook
ChoiByungWook previously approved these changes Jan 24, 2019

@mvsusp mvsusp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We are getting there ! Thanks for the hard work on this.

Please add a section on the documentation on how to use update endpoints as well.

Comment thread tests/integ/test_mxnet_train.py Resolved
Comment thread tests/integ/test_mxnet_train.py Resolved
Comment thread tests/integ/test_mxnet_train.py Outdated
py_version=PYTHON_VERSION, sagemaker_session=sagemaker_session)
model.deploy(1, 'ml.t2.medium', endpoint_name=endpoint_name)
sagemaker_session.describe_endpoint(EndpointName=endpoint_name)
model.deploy(1, 'ml.m4.xlarge', update_endpoint=True, endpoint_name='non-existing-endpoint')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your test is missing assertions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test expects an error, which is asserted with pytest.raises(ValueError, message=error_message) here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would make the pytest.raiser more explicitly then. Leaving under model.deploy under the pytest.raises denotes that the error can be raise only from that specific method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread tests/unit/test_estimator.py Outdated


def test_deploy_with_update_endpoint(sagemaker_session):
e = Estimator(IMAGE_NAME, ROLE, INSTANCE_COUNT, INSTANCE_TYPE, output_path=OUTPUT_PATH,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Avoid one letter variables.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated. Do we need to update all other 1-letter variable names in this test? Many of the tests here use 'e' for estimator.

Comment thread tests/unit/test_session.py Outdated


def test_update_endpoint_non_existing_endpoint(sagemaker_session):
ims = sagemaker_session

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove this line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread tests/unit/test_session.py Outdated
def test_update_endpoint_non_existing_endpoint(sagemaker_session):
ims = sagemaker_session
error = ClientError({'Error': {'Code': 'ValidationException', 'Message': 'Could not find entity'}}, 'foo')
error_message = 'Endpoint with name "non-existing-endpoint" does not exist; please use an existing endpoint name'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this error message necessary? I don't see any assertion about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add the error message to pytest.raises(ValueError, message=error_message)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Got it, I got confused with the order of statements. nit: rename it to expected_error_msg or something like that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Also changed the naming in test_local_session and test_mxnet_train.

Comment thread tests/unit/test_estimator.py Outdated
e.fit({'train': 's3://bucket/training-prefix'})
e.deploy(INSTANCE_COUNT, INSTANCE_TYPE, endpoint_name="endpoint-name", update_endpoint=True)
sagemaker_session.create_endpoint_config.assert_called()
sagemaker_session.update_endpoint.assert_called()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use assert_called_with instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated with using call_args and assert with argument values. The model_name/endpoint_config_name is not accessible from estimator, so not able to use assert_called_with directly.

Comment thread src/sagemaker/model.py Outdated
If not specified, a unique endpoint name will be created.
update_endpoint (bool): Flag to update the model in an existing Amazon SageMaker endpoint.
If True, this will deploy a new EndpointConfig to an already existing endpoint and delete resources
corresponding to the previous EndpointConfig. Default: False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please docstring what happens when this flag is false as well. Is it possible to throw an exception for example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated documentation. The error is raised in session.update_endpoint so the corresponding information is documented there.

ChoiByungWook
ChoiByungWook previously approved these changes Jan 26, 2019

@ChoiByungWook ChoiByungWook left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Waiting on @mvsusp for approval.

@mvsusp mvsusp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it possible to add the new feature in the documentation?

@chuyang-deng

Copy link
Copy Markdown
Contributor Author

@mvsusp Are you referring to adding the new feature to the GitHub README (and do we want to add this new feature in the README?) or docs.aws.amazon.com?

@mvsusp

mvsusp commented Jan 29, 2019

Copy link
Copy Markdown
Contributor

@mvsusp Are you referring to adding the new feature to the GitHub README (and do we want to add this new feature in the README?) or docs.aws.amazon.com?

@ChuyangDeng , the GitHub README (and other readme pages about the estimators) include documentation about the deploy method. Ideally we should review these, and update if necessary.

The same should be done for notebook examples, although I understand that is outside the scope of this PR.

@chuyang-deng

Copy link
Copy Markdown
Contributor Author

@mvsusp I added a code snippet under 'Using Estimators' to demonstrate this function because there's no sections for the method deploy itself. And I don't think it's necessary to update the documentation on every deploy occurrence.

mvsusp
mvsusp previously approved these changes Jan 29, 2019
Comment thread README.rst Outdated
mxnet_estimator.delete_endpoint()


Or, if you want to deploy to an existing endpoint instead of letting SageMaker creating a new one, you can specify to update an endpoint.

@ChoiByungWook ChoiByungWook Jan 29, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Up to you, I think the wording below might be more specific.

Additionally, it is possible to deploy a different endpoint configuration, which links to your model, to an already existing SageMaker endpoint. This can be done by specifying the existing endpoint name for the endpoint_name parameter along with the update_endpoint parameter as True within your deploy() call. For more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants