|
| 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 renamed_params |
| 18 | +from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call |
| 19 | + |
| 20 | +ESTIMATORS = { |
| 21 | + "Chainer": ("sagemaker.chainer", "sagemaker.chainer.estimator"), |
| 22 | + "Estimator": ("sagemaker.estimator",), |
| 23 | + "Framework": ("sagemaker.estimator",), |
| 24 | + "MXNet": ("sagemaker.mxnet", "sagemaker.mxnet.estimator"), |
| 25 | + "PyTorch": ("sagemaker.pytorch", "sagemaker.pytorch.estimator"), |
| 26 | + "RLEstimator": ("sagemaker.rl", "sagemaker.rl.estimator"), |
| 27 | + "SKLearn": ("sagemaker.sklearn", "sagemaker.sklearn.estimator"), |
| 28 | + "TensorFlow": ("sagemaker.tensorflow", "sagemaker.tensorflow.estimator"), |
| 29 | + "XGBoost": ("sagemaker.xgboost", "sagemaker.xgboost.estimator"), |
| 30 | +} |
| 31 | + |
| 32 | +MODELS = { |
| 33 | + "ChainerModel": ("sagemaker.chainer", "sagemaker.chainer.model"), |
| 34 | + "Model": ("sagemaker.model",), |
| 35 | + "MultiDataModel": ("sagemaker.multidatamodel",), |
| 36 | + "FrameworkModel": ("sagemaker.model",), |
| 37 | + "MXNetModel": ("sagemaker.mxnet", "sagemaker.mxnet.model"), |
| 38 | + "PyTorchModel": ("sagemaker.pytorch", "sagemaker.pytorch.model"), |
| 39 | + "SKLearnModel": ("sagemaker.sklearn", "sagemaker.sklearn.model"), |
| 40 | + "TensorFlowModel": ("sagemaker.tensorflow", "sagemaker.tensorflow.model"), |
| 41 | + "XGBoostModel": ("sagemaker.xgboost", "sagemaker.xgboost.model"), |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def test_estimator_node_should_be_modified(): |
| 46 | + modifier = renamed_params.EstimatorImageURIRenamer() |
| 47 | + |
| 48 | + for estimator, namespaces in ESTIMATORS.items(): |
| 49 | + call = "{}(image_name='my-image:latest')".format(estimator) |
| 50 | + assert modifier.node_should_be_modified(ast_call(call)) |
| 51 | + |
| 52 | + for namespace in namespaces: |
| 53 | + call = "{}.{}(image_name='my-image:latest')".format(namespace, estimator) |
| 54 | + assert modifier.node_should_be_modified(ast_call(call)) |
| 55 | + |
| 56 | + |
| 57 | +def test_estimator_node_should_be_modified_no_distribution(): |
| 58 | + modifier = renamed_params.EstimatorImageURIRenamer() |
| 59 | + |
| 60 | + for estimator, namespaces in ESTIMATORS.items(): |
| 61 | + call = "{}()".format(estimator) |
| 62 | + assert not modifier.node_should_be_modified(ast_call(call)) |
| 63 | + |
| 64 | + for namespace in namespaces: |
| 65 | + call = "{}.{}()".format(namespace, estimator) |
| 66 | + assert not modifier.node_should_be_modified(ast_call(call)) |
| 67 | + |
| 68 | + |
| 69 | +def test_estimator_node_should_be_modified_random_function_call(): |
| 70 | + modifier = renamed_params.EstimatorImageURIRenamer() |
| 71 | + assert not modifier.node_should_be_modified(ast_call("Session()")) |
| 72 | + |
| 73 | + |
| 74 | +def test_estimator_modify_node(): |
| 75 | + node = ast_call("TensorFlow(image_name=my_image)") |
| 76 | + modifier = renamed_params.EstimatorImageURIRenamer() |
| 77 | + modifier.modify_node(node) |
| 78 | + |
| 79 | + expected = "TensorFlow(image_uri=my_image)" |
| 80 | + assert expected == pasta.dump(node) |
| 81 | + |
| 82 | + |
| 83 | +def test_model_node_should_be_modified(): |
| 84 | + modifier = renamed_params.ModelImageURIRenamer() |
| 85 | + |
| 86 | + for model, namespaces in MODELS.items(): |
| 87 | + call = "{}(image='my-image:latest')".format(model) |
| 88 | + assert modifier.node_should_be_modified(ast_call(call)) |
| 89 | + |
| 90 | + for namespace in namespaces: |
| 91 | + call = "{}.{}(image='my-image:latest')".format(namespace, model) |
| 92 | + assert modifier.node_should_be_modified(ast_call(call)) |
| 93 | + |
| 94 | + |
| 95 | +def test_model_node_should_be_modified_no_distribution(): |
| 96 | + modifier = renamed_params.ModelImageURIRenamer() |
| 97 | + |
| 98 | + for model, namespaces in MODELS.items(): |
| 99 | + call = "{}()".format(model) |
| 100 | + assert not modifier.node_should_be_modified(ast_call(call)) |
| 101 | + |
| 102 | + for namespace in namespaces: |
| 103 | + call = "{}.{}()".format(namespace, model) |
| 104 | + assert not modifier.node_should_be_modified(ast_call(call)) |
| 105 | + |
| 106 | + |
| 107 | +def test_model_node_should_be_modified_random_function_call(): |
| 108 | + modifier = renamed_params.ModelImageURIRenamer() |
| 109 | + assert not modifier.node_should_be_modified(ast_call("Session()")) |
| 110 | + |
| 111 | + |
| 112 | +def test_model_modify_node(): |
| 113 | + node = ast_call("TensorFlowModel(image=my_image)") |
| 114 | + modifier = renamed_params.ModelImageURIRenamer() |
| 115 | + modifier.modify_node(node) |
| 116 | + |
| 117 | + expected = "TensorFlowModel(image_uri=my_image)" |
| 118 | + assert expected == pasta.dump(node) |
0 commit comments