|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import pasta |
| 16 | + |
| 17 | +from sagemaker.cli.compatibility.v2.modifiers import tf_legacy_mode |
| 18 | + |
| 19 | + |
| 20 | +def test_node_should_be_modified_fit_with_tensorboard(): |
| 21 | + fit_calls = ( |
| 22 | + "estimator.fit(run_tensorboard_locally=True)", |
| 23 | + "tensorflow.fit(run_tensorboard_locally=False)", |
| 24 | + ) |
| 25 | + |
| 26 | + modifier = tf_legacy_mode.TensorBoardParameterRemover() |
| 27 | + |
| 28 | + for call in fit_calls: |
| 29 | + node = _ast_call(call) |
| 30 | + assert modifier.node_should_be_modified(node) is True |
| 31 | + |
| 32 | + |
| 33 | +def test_node_should_be_modified_fit_without_tensorboard(): |
| 34 | + fit_calls = ("estimator.fit()", "tensorflow.fit()") |
| 35 | + |
| 36 | + modifier = tf_legacy_mode.TensorBoardParameterRemover() |
| 37 | + |
| 38 | + for call in fit_calls: |
| 39 | + node = _ast_call(call) |
| 40 | + assert modifier.node_should_be_modified(node) is False |
| 41 | + |
| 42 | + |
| 43 | +def test_node_should_be_modified_random_function_call(): |
| 44 | + node = _ast_call("estimator.deploy(1, 'local')") |
| 45 | + modifier = tf_legacy_mode.TensorBoardParameterRemover() |
| 46 | + assert modifier.node_should_be_modified(node) is False |
| 47 | + |
| 48 | + |
| 49 | +def test_modify_node(): |
| 50 | + fit_calls = ( |
| 51 | + "estimator.fit(run_tensorboard_locally=True)", |
| 52 | + "estimator.fit(run_tensorboard_locally=False)", |
| 53 | + ) |
| 54 | + modifier = tf_legacy_mode.TensorBoardParameterRemover() |
| 55 | + |
| 56 | + for call in fit_calls: |
| 57 | + node = _ast_call(call) |
| 58 | + modifier.modify_node(node) |
| 59 | + assert "estimator.fit()" == pasta.dump(node) |
| 60 | + |
| 61 | + |
| 62 | +def _ast_call(code): |
| 63 | + return pasta.parse(code).body[0].value |
0 commit comments