Skip to content

change: handle image_uri rename for Session methods #1681

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 4 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
129 changes: 119 additions & 10 deletions src/sagemaker/cli/compatibility/v2/modifiers/renamed_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ def modify_node(self, node):
keyword.arg = self.new_param_name


class MethodParamRenamer(ParamRenamer):
"""Abstract class to handle parameter renames for methods that belong to objects.

This differs from ``ParamRenamer`` in that a node for a standalone function call
(i.e. where ``node.func`` is an ``ast.Name`` rather than an ``ast.Attribute``) is not modified.
"""

def node_should_be_modified(self, node):
"""Checks if the node matches any of the relevant functions and
contains the parameter to be renamed.

This looks for a call of the form ``<object>.<method>``, and
assumes the method cannot be called on its own.

Args:
node (ast.Call): a node that represents a function call. For more,
see https://docs.python.org/3/library/ast.html#abstract-grammar.

Returns:
bool: If the ``ast.Call`` matches the relevant function calls and
contains the parameter to be renamed.
"""
if isinstance(node.func, ast.Name):
return False

return super(MethodParamRenamer, self).node_should_be_modified(node)


class DistributionParameterRenamer(ParamRenamer):
"""A class to rename the ``distributions`` attribute to ``distrbution`` in
MXNet and TensorFlow estimators.
Expand Down Expand Up @@ -100,7 +128,7 @@ def new_param_name(self):
return "distribution"


class S3SessionRenamer(ParamRenamer):
class S3SessionRenamer(MethodParamRenamer):
"""A class to rename the ``session`` attribute to ``sagemaker_session`` in
``S3Uploader`` and ``S3Downloader``.

Expand Down Expand Up @@ -139,15 +167,6 @@ def new_param_name(self):
"""The new name for the SageMaker session argument."""
return "sagemaker_session"

def node_should_be_modified(self, node):
"""Checks if the node is one of the S3 utility functions and
contains the ``session`` parameter.
"""
if isinstance(node.func, ast.Name):
return False

return super(S3SessionRenamer, self).node_should_be_modified(node)


class EstimatorImageURIRenamer(ParamRenamer):
"""A class to rename the ``image_name`` attribute to ``image_uri`` in estimators."""
Expand Down Expand Up @@ -209,3 +228,93 @@ def old_param_name(self):
def new_param_name(self):
"""The new name for the image URI argument."""
return "image_uri"


class EstimatorCreateModelImageURIRenamer(MethodParamRenamer):
"""A class to rename ``image`` to ``image_uri`` in estimator ``create_model()`` methods."""

@property
def calls_to_modify(self):
"""A mapping of ``create_model`` to common variable names for estimators."""
return {
"create_model": (
"estimator",
"chainer",
"mxnet",
"mx",
"pytorch",
"rl",
"sklearn",
"tensorflow",
"tf",
"xgboost",
"xgb",
)
}

@property
def old_param_name(self):
"""The previous name for the image URI argument."""
return "image"

@property
def new_param_name(self):
"""The new name for the the image URI argument."""
return "image_uri"


class SessionCreateModelImageURIRenamer(MethodParamRenamer):
"""A class to rename ``primary_container_image`` to ``image_uri``.

This looks for the following calls:

- ``sagemaker_session.create_model_from_job()``
- ``sess.create_model_from_job()``
"""

@property
def calls_to_modify(self):
"""A mapping of ``create_model_from_job`` to common variable names for Session."""
return {
"create_model_from_job": ("sagemaker_session", "sess"),
}

@property
def old_param_name(self):
"""The previous name for the image URI argument."""
return "primary_container_image"

@property
def new_param_name(self):
"""The new name for the the image URI argument."""
return "image_uri"


class SessionCreateEndpointImageURIRenamer(MethodParamRenamer):
"""A class to rename ``deployment_image`` to ``image_uri``.

This looks for the following calls:

- ``sagemaker_session.endpoint_from_job()``
- ``sess.endpoint_from_job()``
- ``sagemaker_session.endpoint_from_model_data()``
- ``sess.endpoint_from_model_data()``
"""

@property
def calls_to_modify(self):
"""A mapping of the ``endpoint_from_*`` functions to common variable names for Session."""
return {
"endpoint_from_job": ("sagemaker_session", "sess"),
"endpoint_from_model_data": ("sagemaker_session", "sess"),
}

@property
def old_param_name(self):
"""The previous name for the image URI argument."""
return "deployment_image"

@property
def new_param_name(self):
"""The new name for the the image URI argument."""
return "image_uri"
Comment on lines +317 to +320
Copy link
Contributor

Choose a reason for hiding this comment

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

fft: although i don't like promoting deep class inheritance hierarchy, this method is common to all *ImageURIRenamer and may benefit from some inheritance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(talked offline) I'd also noticed the duplication too, but agree that too many layers of inheritance also isn't great

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import pasta

from sagemaker.cli.compatibility.v2.modifiers import renamed_params
from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call

ESTIMATORS = (
"estimator",
"chainer",
"mxnet",
"mx",
"pytorch",
"rl",
"sklearn",
"tensorflow",
"tf",
"xgboost",
"xgb",
)


def test_node_should_be_modified():
modifier = renamed_params.EstimatorCreateModelImageURIRenamer()

for estimator in ESTIMATORS:
call = "{}.create_model(image='my-image:latest')".format(estimator)
assert modifier.node_should_be_modified(ast_call(call))


def test_node_should_be_modified_no_distribution():
modifier = renamed_params.EstimatorCreateModelImageURIRenamer()

for estimator in ESTIMATORS:
call = "{}.create_model()".format(estimator)
assert not modifier.node_should_be_modified(ast_call(call))


def test_node_should_be_modified_random_function_call():
modifier = renamed_params.EstimatorCreateModelImageURIRenamer()
assert not modifier.node_should_be_modified(ast_call("create_model()"))


def test_modify_node():
node = ast_call("estimator.create_model(image=my_image)")
modifier = renamed_params.EstimatorCreateModelImageURIRenamer()
modifier.modify_node(node)

expected = "estimator.create_model(image_uri=my_image)"
assert expected == pasta.dump(node)
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import pasta

from sagemaker.cli.compatibility.v2.modifiers import renamed_params
from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call

CREATE_MODEL_TEMPLATES = (
"sagemaker_session.create_model_from_job({})",
"sess.create_model_from_job({})",
)

CREATE_ENDPOINT_TEMPLATES = (
"sagemaker_session.endpoint_from_job({})",
"sagemaker_session.endpoint_from_model_data({})",
"sess.endpoint_from_job({})",
"sess.endpoint_from_model_data({})",
)


def test_create_model_node_should_be_modified():
modifier = renamed_params.SessionCreateModelImageURIRenamer()

for template in CREATE_MODEL_TEMPLATES:
call = ast_call(template.format("primary_container_image=my_image"))
assert modifier.node_should_be_modified(call)


def test_create_model_node_should_be_modified_no_image():
modifier = renamed_params.SessionCreateModelImageURIRenamer()

for template in CREATE_MODEL_TEMPLATES:
call = ast_call(template.format(""))
assert not modifier.node_should_be_modified(call)


def test_create_model_node_should_be_modified_random_function_call():
modifier = renamed_params.SessionCreateModelImageURIRenamer()
assert not modifier.node_should_be_modified(ast_call("create_model()"))


def test_create_model_modify_node():
modifier = renamed_params.SessionCreateModelImageURIRenamer()

for template in CREATE_MODEL_TEMPLATES:
call = ast_call(template.format("primary_container_image=my_image"))
modifier.modify_node(call)

expected = template.format("image_uri=my_image")
assert expected == pasta.dump(call)


def test_create_endpoint_node_should_be_modified():
modifier = renamed_params.SessionCreateEndpointImageURIRenamer()

for template in CREATE_ENDPOINT_TEMPLATES:
call = ast_call(template.format("deployment_image=my_image"))
assert modifier.node_should_be_modified(call)


def test_create_endpoint_node_should_be_modified_no_image():
modifier = renamed_params.SessionCreateEndpointImageURIRenamer()

for template in CREATE_ENDPOINT_TEMPLATES:
call = ast_call(template.format(""))
assert not modifier.node_should_be_modified(call)


def test_create_endpoint_node_should_be_modified_random_function_call():
modifier = renamed_params.SessionCreateEndpointImageURIRenamer()
assert not modifier.node_should_be_modified(ast_call("create_endpoint()"))


def test_create_endpoint_modify_node():
modifier = renamed_params.SessionCreateEndpointImageURIRenamer()

for template in CREATE_ENDPOINT_TEMPLATES:
call = ast_call(template.format("deployment_image=my_image"))
modifier.modify_node(call)

expected = template.format("image_uri=my_image")
assert expected == pasta.dump(call)