-
Notifications
You must be signed in to change notification settings - Fork 1.2k
change: make v2 migration script remove script_mode param and set model_dir=False if needed #1540
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
laurenyu
merged 6 commits into
aws:zwei
from
laurenyu:migration-script-no-more-script-mode-arg
Jun 3, 2020
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b381033
change: make v2 migration script remove script_mode param and set mod…
laurenyu 01b39ae
add missing file
laurenyu 9d0e4ff
fix unit tests
laurenyu 7216855
add init files
laurenyu 40b31c7
address PR comment
laurenyu 6616918
Merge branch 'zwei' into migration-script-no-more-script-mode-arg
laurenyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/sagemaker/cli/compatibility/v2/modifiers/deprecated_params.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# 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. | ||
"""Classes to remove deprecated parameters.""" | ||
from __future__ import absolute_import | ||
|
||
import ast | ||
|
||
from sagemaker.cli.compatibility.v2.modifiers.modifier import Modifier | ||
|
||
|
||
class TensorFlowScriptModeParameterRemover(Modifier): | ||
"""A class to remove ``script_mode`` from TensorFlow estimators (because it's the only mode).""" | ||
|
||
def node_should_be_modified(self, node): | ||
"""Checks if the ``ast.Call`` node instantiates a TensorFlow estimator with | ||
``script_mode`` set. | ||
|
||
This looks for the following formats: | ||
|
||
- ``TensorFlow`` | ||
- ``sagemaker.tensorflow.TensorFlow`` | ||
|
||
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`` is instantiating a TensorFlow estimator with ``script_mode``. | ||
""" | ||
return self._is_tf_constructor(node) and self._has_script_mode_param(node) | ||
|
||
def _is_tf_constructor(self, node): | ||
"""Checks if the ``ast.Call`` node represents a call of the form | ||
``TensorFlow`` or ``sagemaker.tensorflow.TensorFlow``. | ||
""" | ||
# Check for TensorFlow() | ||
if isinstance(node.func, ast.Name): | ||
return node.func.id == "TensorFlow" | ||
|
||
# Check for sagemaker.tensorflow.TensorFlow() | ||
ends_with_tensorflow_constructor = ( | ||
isinstance(node.func, ast.Attribute) and node.func.attr == "TensorFlow" | ||
) | ||
|
||
is_in_tensorflow_module = ( | ||
isinstance(node.func.value, ast.Attribute) | ||
and node.func.value.attr == "tensorflow" | ||
and isinstance(node.func.value.value, ast.Name) | ||
and node.func.value.value.id == "sagemaker" | ||
) | ||
|
||
return ends_with_tensorflow_constructor and is_in_tensorflow_module | ||
|
||
def _has_script_mode_param(self, node): | ||
"""Checks if the ``ast.Call`` node's keywords include ``script_mode``.""" | ||
for kw in node.keywords: | ||
if kw.arg == "script_mode": | ||
return True | ||
|
||
return False | ||
|
||
def modify_node(self, node): | ||
"""Modifies the ``ast.Call`` node's keywords to remove ``script_mode``. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a TensorFlow constructor. | ||
""" | ||
for kw in node.keywords: | ||
if kw.arg == "script_mode": | ||
node.keywords.remove(kw) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,17 @@ | |
|
||
import ast | ||
|
||
import boto3 | ||
import six | ||
|
||
from sagemaker.cli.compatibility.v2.modifiers import framework_version | ||
from sagemaker.cli.compatibility.v2.modifiers.modifier import Modifier | ||
from sagemaker import fw_utils | ||
|
||
|
||
class TensorFlowLegacyModeConstructorUpgrader(Modifier): | ||
"""A class to turn legacy mode parameters into hyperparameters when | ||
instantiating a TensorFlow estimator. | ||
"""A class to turn legacy mode parameters into hyperparameters, disable the ``model_dir`` | ||
hyperparameter, and set the image URI when instantiating a TensorFlow estimator. | ||
""" | ||
|
||
LEGACY_MODE_PARAMETERS = ( | ||
|
@@ -89,7 +92,7 @@ def _is_legacy_mode(self, node): | |
|
||
def modify_node(self, node): | ||
"""Modifies the ``ast.Call`` node's keywords to turn TensorFlow legacy mode parameters | ||
into hyperparameters and set ``script_mode=False``. | ||
into hyperparameters and sets ``model_dir=False``. | ||
|
||
The parameters that are converted into hyperparameters: | ||
|
||
|
@@ -105,9 +108,11 @@ def modify_node(self, node): | |
additional_hps = {} | ||
kw_to_remove = [] # remove keyword args after so that none are skipped during iteration | ||
|
||
add_image_uri = True | ||
|
||
for kw in node.keywords: | ||
if kw.arg == "script_mode": | ||
# remove here because is set to False later regardless of current value | ||
if kw.arg in ("script_mode", "model_dir"): | ||
# model_dir is removed so that it can be set to False later | ||
kw_to_remove.append(kw) | ||
if kw.arg == "hyperparameters" and kw.value: | ||
base_hps = dict(zip(kw.value.keys, kw.value.values)) | ||
|
@@ -116,11 +121,17 @@ def modify_node(self, node): | |
hp_key = self._hyperparameter_key_for_param(kw.arg) | ||
additional_hps[hp_key] = kw.value | ||
kw_to_remove.append(kw) | ||
if kw.arg == "image_name": | ||
add_image_uri = False | ||
|
||
self._remove_keywords(node, kw_to_remove) | ||
self._add_updated_hyperparameters(node, base_hps, additional_hps) | ||
|
||
node.keywords.append(ast.keyword(arg="script_mode", value=ast.NameConstant(value=False))) | ||
if add_image_uri: | ||
image_uri = self._image_uri_from_args(node.keywords) | ||
node.keywords.append(ast.keyword(arg="image_name", value=ast.Str(s=image_uri))) | ||
|
||
node.keywords.append(ast.keyword(arg="model_dir", value=ast.NameConstant(value=False))) | ||
|
||
def _hyperparameter_key_for_param(self, arg): | ||
"""Returns an ``ast.Str`` for a hyperparameter key replacing a legacy mode parameter.""" | ||
|
@@ -148,6 +159,20 @@ def _to_ast_keyword(self, hps): | |
|
||
return None | ||
|
||
def _image_uri_from_args(self, keywords): | ||
"""Returns a legacy TensorFlow image URI based on the estimator arguments.""" | ||
tf_version = framework_version.FRAMEWORK_DEFAULTS["TensorFlow"] | ||
instance_type = "ml.m4.xlarge" # CPU default (exact type doesn't matter) | ||
|
||
for kw in keywords: | ||
if kw.arg == "framework_version": | ||
tf_version = kw.value.s | ||
if kw.arg == "train_instance_type": | ||
instance_type = kw.value.s | ||
|
||
region = boto3.Session().region_name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aside: tiny optimization opportunity, given should be same for the entire ast |
||
return fw_utils.create_image_uri(region, "tensorflow", instance_type, tf_version, "py2") | ||
|
||
|
||
class TensorBoardParameterRemover(Modifier): | ||
"""A class for removing the ``run_tensorboard_locally`` parameter from ``fit()``.""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 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 |
13 changes: 13 additions & 0 deletions
13
tests/unit/sagemaker/cli/compatibility/v2/modifiers/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 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 |
19 changes: 19 additions & 0 deletions
19
tests/unit/sagemaker/cli/compatibility/v2/modifiers/ast_converter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 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 | ||
|
||
|
||
def ast_call(code): | ||
return pasta.parse(code).body[0].value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to check it if ends_with_tensorflow_constructor == false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is technically possible that someone could create asagemaker.tensorflow.TensorBoard
object directly: https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/estimator.py#L57edit: wait, you're right. let me submit a new PR. this also made me realize that I forgot about
sagemaker.tensorflow.estimator.TensorFlow