-
Notifications
You must be signed in to change notification settings - Fork 1.2k
change: add modifier for s3_input class #1699
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
495e289
breaking: rename s3_input to TrainingInput
bc4f20c
Merge branch 'zwei' into rename-s3-input
chuyang-deng 50ad633
Merge branch 'zwei' into rename-s3-input
chuyang-deng d15f823
remove TrainingInput import from session
5f030dd
Merge branch 'rename-s3-input' of github.com:ChuyangDeng/sagemaker-py…
a6e4830
update docstring
4cd044b
Merge branch 'zwei' into rename-s3-input
laurenyu 4c08cd7
change: add modifier for s3_input class
e1a0eed
Merge branch 'rename-s3-input' of github.com:ChuyangDeng/sagemaker-py…
083da46
Merge branch 'zwei' into rename-s3-input
chuyang-deng cfb7c2e
modify namespaces
99a9652
Merge branch 'rename-s3-input' of github.com:ChuyangDeng/sagemaker-py…
daff7ac
Merge branch 'zwei' into rename-s3-input
chuyang-deng 060d716
rename namespace
13e1f7d
Merge branch 'rename-s3-input' of github.com:ChuyangDeng/sagemaker-py…
04cdc7b
Merge branch 'zwei' into rename-s3-input
chuyang-deng de40298
Merge branch 'zwei' into rename-s3-input
chuyang-deng 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,5 @@ | |
tf_legacy_mode, | ||
tfs, | ||
training_params, | ||
training_input, | ||
) |
97 changes: 97 additions & 0 deletions
97
src/sagemaker/cli/compatibility/v2/modifiers/training_input.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,97 @@ | ||
# 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 modify TrainingInput code to be compatible | ||
with version 2.0 and later of the SageMaker Python SDK. | ||
""" | ||
from __future__ import absolute_import | ||
|
||
import ast | ||
|
||
from sagemaker.cli.compatibility.v2.modifiers import matching | ||
from sagemaker.cli.compatibility.v2.modifiers.modifier import Modifier | ||
|
||
S3_INPUT_NAME = "s3_input" | ||
S3_INPUT_NAMESPACES = ("sagemaker", "sagemaker.inputs", "sagemaker.session") | ||
|
||
|
||
class TrainingInputConstructorRefactor(Modifier): | ||
"""A class to refactor *s3_input class.""" | ||
|
||
def node_should_be_modified(self, node): | ||
"""Checks if the ``ast.Call`` node instantiates a class of interest. | ||
|
||
This looks for the following calls: | ||
|
||
- ``sagemaker.s3_input`` | ||
- ``sagemaker.session.s3_input`` | ||
- ``s3_input`` | ||
|
||
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`` instantiates a class of interest. | ||
""" | ||
return matching.matches_name_or_namespaces(node, S3_INPUT_NAME, S3_INPUT_NAMESPACES) | ||
|
||
def modify_node(self, node): | ||
"""Modifies the ``ast.Call`` node to call ``TrainingInput`` instead. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a *TrainingInput constructor. | ||
""" | ||
if matching.matches_name(node, S3_INPUT_NAME): | ||
node.func.id = "TrainingInput" | ||
elif matching.matches_attr(node, S3_INPUT_NAME): | ||
node.func.attr = "TrainingInput" | ||
_rename_namespace(node, "session") | ||
|
||
|
||
def _rename_namespace(node, name): | ||
"""Rename namespace ``session`` to ``inputs`` """ | ||
if isinstance(node.func.value, ast.Attribute) and node.func.value.attr == name: | ||
node.func.value.attr = "inputs" | ||
elif isinstance(node.func.value, ast.Name) and node.func.value.id == name: | ||
node.func.value.id = "inputs" | ||
|
||
|
||
class TrainingInputImportFromRenamer(Modifier): | ||
"""A class to update import statements of ``s3_input``.""" | ||
|
||
def node_should_be_modified(self, node): | ||
"""Checks if the import statement imports ``s3_input`` from the correct module. | ||
|
||
Args: | ||
node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. | ||
For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
|
||
Returns: | ||
bool: If the import statement imports ``s3_input`` from the correct module. | ||
""" | ||
return node.module in S3_INPUT_NAMESPACES and any( | ||
name.name == S3_INPUT_NAME for name in node.names | ||
) | ||
|
||
def modify_node(self, node): | ||
"""Changes the ``ast.ImportFrom`` node's name from ``s3_input`` to ``TrainingInput``. | ||
|
||
Args: | ||
node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. | ||
For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
""" | ||
for name in node.names: | ||
if name.name == S3_INPUT_NAME: | ||
name.name = "TrainingInput" | ||
if node.module == "sagemaker.session": | ||
node.module = "sagemaker.inputs" |
113 changes: 113 additions & 0 deletions
113
tests/unit/sagemaker/cli/compatibility/v2/modifiers/test_training_input.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,113 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import pytest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from sagemaker.cli.compatibility.v2.modifiers import training_input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call, ast_import | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@pytest.fixture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def constructors(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"sagemaker.session.s3_input(s3_data='s3://a')", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
laurenyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"sagemaker.inputs.s3_input(s3_data='s3://a')", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"sagemaker.s3_input(s3_data='s3://a')", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"session.s3_input(s3_data='s3://a')", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"inputs.s3_input(s3_data='s3://a')", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"s3_input(s3_data='s3://a')", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@pytest.fixture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def import_statements(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
laurenyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"from sagemaker.session import s3_input", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"from sagemaker.inputs import s3_input", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"from sagemaker import s3_input", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_constructor_node_should_be_modified(constructors): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier = training_input.TrainingInputConstructorRefactor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for constructor in constructors: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call(constructor) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert modifier.node_should_be_modified(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_constructor_node_should_be_modified_random_call(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier = training_input.TrainingInputConstructorRefactor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("FileSystemInput()") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert not modifier.node_should_be_modified(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_constructor_modify_node(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier = training_input.TrainingInputConstructorRefactor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("s3_input(s3_data='s3://a')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert "TrainingInput(s3_data='s3://a')" == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("sagemaker.s3_input(s3_data='s3://a')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert "sagemaker.TrainingInput(s3_data='s3://a')" == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("session.s3_input(s3_data='s3://a')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert "inputs.TrainingInput(s3_data='s3://a')" == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("inputs.s3_input(s3_data='s3://a')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert "inputs.TrainingInput(s3_data='s3://a')" == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("sagemaker.inputs.s3_input(s3_data='s3://a')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert "sagemaker.inputs.TrainingInput(s3_data='s3://a')" == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_call("sagemaker.session.s3_input(s3_data='s3://a')") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert "sagemaker.inputs.TrainingInput(s3_data='s3://a')" == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+59
to
+81
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. optional - you could also make an iterable of tuples to reduce how much code is repeated
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_import_from_node_should_be_modified_training_input(import_statements): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier = training_input.TrainingInputImportFromRenamer() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for statement in import_statements: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_import(statement) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert modifier.node_should_be_modified(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_import_from_node_should_be_modified_random_import(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier = training_input.TrainingInputImportFromRenamer() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_import("from sagemaker.session import Session") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert not modifier.node_should_be_modified(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_import_from_modify_node(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier = training_input.TrainingInputImportFromRenamer() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_import("from sagemaker import s3_input") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expected_result = "from sagemaker import TrainingInput" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert expected_result == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_import("from sagemaker.inputs import s3_input as training_input") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expected_result = "from sagemaker.inputs import TrainingInput as training_input" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert expected_result == pasta.dump(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = ast_import("from sagemaker.session import s3_input as training_input") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier.modify_node(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expected_result = "from sagemaker.inputs import TrainingInput as training_input" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert expected_result == pasta.dump(node) |
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.