|
| 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 itertools |
| 16 | + |
| 17 | +import pasta |
| 18 | + |
| 19 | +from sagemaker.cli.compatibility.v2.modifiers import training_params |
| 20 | +from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call |
| 21 | + |
| 22 | +ESTIMATORS_TO_NAMESPACES = { |
| 23 | + "AlgorithmEstimator": ("sagemaker", "sagemaker.algorithm"), |
| 24 | + "AmazonAlgorithmEstimatorBase": ("sagemaker.amazon.amazon_estimator",), |
| 25 | + "Chainer": ("sagemaker.chainer", "sagemaker.chainer.estimator"), |
| 26 | + "Estimator": ("sagemaker.estimator",), |
| 27 | + "EstimatorBase": ("sagemaker.estimator",), |
| 28 | + "FactorizationMachines": ("sagemaker", "sagemaker.amazon.factorization_machines"), |
| 29 | + "Framework": ("sagemaker.estimator",), |
| 30 | + "IPInsights": ("sagemaker", "sagemaker.amazon.ipinsights"), |
| 31 | + "KMeans": ("sagemaker", "sagemaker.amazon.kmeans"), |
| 32 | + "KNN": ("sagemaker", "sagemaker.amazon.knn"), |
| 33 | + "LDA": ("sagemaker", "sagemaker.amazon.lda"), |
| 34 | + "LinearLearner": ("sagemaker", "sagemaker.amazon.linear_learner"), |
| 35 | + "MXNet": ("sagemaker.mxnet", "sagemaker.mxnet.estimator"), |
| 36 | + "NTM": ("sagemaker", "sagemaker.amazon.ntm"), |
| 37 | + "Object2Vec": ("sagemaker", "sagemaker.amazon.object2vec"), |
| 38 | + "PCA": ("sagemaker", "sagemaker.amazon.pca"), |
| 39 | + "PyTorch": ("sagemaker.pytorch", "sagemaker.pytorch.estimator"), |
| 40 | + "RandomCutForest": ("sagemaker", "sagemaker.amazon.randomcutforest"), |
| 41 | + "RLEstimator": ("sagemaker.rl", "sagemaker.rl.estimator"), |
| 42 | + "SKLearn": ("sagemaker.sklearn", "sagemaker.sklearn.estimator"), |
| 43 | + "TensorFlow": ("sagemaker.tensorflow", "sagemaker.tensorflow.estimator"), |
| 44 | + "XGBoost": ("sagemaker.xgboost", "sagemaker.xgboost.estimator"), |
| 45 | +} |
| 46 | + |
| 47 | +PARAMS_WITH_VALUES = ( |
| 48 | + "train_instance_count=1", |
| 49 | + "train_instance_type='ml.c4.xlarge'", |
| 50 | + "train_max_run=8 * 60 * 60", |
| 51 | + "train_max_run_wait=1 * 60 * 60", |
| 52 | + "train_use_spot_instances=True", |
| 53 | + "train_volume_size=30", |
| 54 | + "train_volume_kms_key='key'", |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +def _estimators(): |
| 59 | + for estimator, namespaces in ESTIMATORS_TO_NAMESPACES.items(): |
| 60 | + yield estimator |
| 61 | + |
| 62 | + for namespace in namespaces: |
| 63 | + yield ".".join((namespace, estimator)) |
| 64 | + |
| 65 | + |
| 66 | +def test_node_should_be_modified(): |
| 67 | + modifier = training_params.TrainPrefixRemover() |
| 68 | + |
| 69 | + for estimator in _estimators(): |
| 70 | + for param in PARAMS_WITH_VALUES: |
| 71 | + call = ast_call("{}({})".format(estimator, param)) |
| 72 | + assert modifier.node_should_be_modified(call) |
| 73 | + |
| 74 | + |
| 75 | +def test_node_should_be_modified_no_params(): |
| 76 | + modifier = training_params.TrainPrefixRemover() |
| 77 | + |
| 78 | + for estimator in _estimators(): |
| 79 | + call = ast_call("{}()".format(estimator)) |
| 80 | + assert not modifier.node_should_be_modified(call) |
| 81 | + |
| 82 | + |
| 83 | +def test_node_should_be_modified_random_function_call(): |
| 84 | + modifier = training_params.TrainPrefixRemover() |
| 85 | + assert not modifier.node_should_be_modified(ast_call("Session()")) |
| 86 | + |
| 87 | + |
| 88 | +def test_modify_node(): |
| 89 | + modifier = training_params.TrainPrefixRemover() |
| 90 | + |
| 91 | + for params in _parameter_combinations(): |
| 92 | + node = ast_call("Estimator({})".format(params)) |
| 93 | + modifier.modify_node(node) |
| 94 | + |
| 95 | + expected = "Estimator({})".format(params).replace("train_", "") |
| 96 | + assert expected == pasta.dump(node) |
| 97 | + |
| 98 | + |
| 99 | +def _parameter_combinations(): |
| 100 | + for subset_length in range(1, len(PARAMS_WITH_VALUES) + 1): |
| 101 | + for subset in itertools.combinations(PARAMS_WITH_VALUES, subset_length): |
| 102 | + yield ", ".join(subset) |
0 commit comments