-
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 all 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
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