From bc57c5e5e2a1dac7ca54da975b5684ee657ca153 Mon Sep 17 00:00:00 2001 From: Karim Nakad Date: Fri, 5 Jul 2019 10:43:24 -0700 Subject: [PATCH 1/4] change: enable wrong-import-position pylint check --- .pylintrc | 1 - src/sagemaker/amazon/record_pb2.py | 3 ++- src/sagemaker/tensorflow/__init__.py | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.pylintrc b/.pylintrc index 2a8bb25fcf..c91ca3be04 100644 --- a/.pylintrc +++ b/.pylintrc @@ -103,7 +103,6 @@ disable= simplifiable-if-expression, # TODO: Simplify expressions too-many-public-methods, # TODO: Resolve ungrouped-imports, # TODO: Group imports - wrong-import-position, # TODO: Correct import positions consider-using-ternary, # TODO: Consider ternary expressions chained-comparison, # TODO: Simplify chained comparison between operands simplifiable-if-statement, # TODO: Simplify ifs diff --git a/src/sagemaker/amazon/record_pb2.py b/src/sagemaker/amazon/record_pb2.py index cf4578c571..183f807980 100644 --- a/src/sagemaker/amazon/record_pb2.py +++ b/src/sagemaker/amazon/record_pb2.py @@ -3,13 +3,14 @@ import sys -_b = sys.version_info[0] < 3 and (lambda x: x) or (lambda x: x.encode("latin1")) from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database from google.protobuf import descriptor_pb2 +_b = sys.version_info[0] < 3 and (lambda x: x) or (lambda x: x.encode("latin1")) + # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() diff --git a/src/sagemaker/tensorflow/__init__.py b/src/sagemaker/tensorflow/__init__.py index 82bfdd9dc8..9b69d7d3f5 100644 --- a/src/sagemaker/tensorflow/__init__.py +++ b/src/sagemaker/tensorflow/__init__.py @@ -19,5 +19,12 @@ # classes for tensorflow serving. Currently tensorflow_serving_api can only be pip-installed for python 2. sys.path.append(os.path.dirname(__file__)) -from sagemaker.tensorflow.estimator import TensorFlow # noqa: E402, F401 -from sagemaker.tensorflow.model import TensorFlowModel, TensorFlowPredictor # noqa: E402, F401 +from sagemaker.tensorflow.estimator import ( # noqa: E402,F401 # pylint: disable=wrong-import-position + TensorFlow, +) +from sagemaker.tensorflow.model import ( # noqa: E402,F401 # pylint: disable=wrong-import-position + TensorFlowModel, +) +from sagemaker.tensorflow.model import ( # noqa: E402,F401 # pylint: disable=wrong-import-position + TensorFlowPredictor, +) From 20197b340dce25b1b2e56de320a8324c795cb6ec Mon Sep 17 00:00:00 2001 From: Karim Nakad Date: Mon, 8 Jul 2019 13:28:44 -0700 Subject: [PATCH 2/4] change: updating import pattern for sagemaker.tensorflow --- src/sagemaker/cli/tensorflow.py | 11 +- src/sagemaker/tensorflow/__init__.py | 11 +- tests/component/test_tf_estimator.py | 12 +- tests/integ/test_local_mode.py | 52 +++--- tests/unit/test_airflow.py | 7 +- tests/unit/test_tf_estimator.py | 232 +++++++++++++++------------ tests/unit/test_tfs.py | 4 +- 7 files changed, 171 insertions(+), 158 deletions(-) diff --git a/src/sagemaker/cli/tensorflow.py b/src/sagemaker/cli/tensorflow.py index 9fbd7dfa1f..5837141b49 100644 --- a/src/sagemaker/cli/tensorflow.py +++ b/src/sagemaker/cli/tensorflow.py @@ -12,6 +12,9 @@ # language governing permissions and limitations under the License. from __future__ import absolute_import +from sagemaker.tensorflow import estimator +from sagemaker.tensorflow import model + from sagemaker.cli.common import HostCommand, TrainCommand @@ -30,9 +33,7 @@ def __init__(self, args): self.evaluation_steps = args.evaluation_steps def create_estimator(self): - from sagemaker.tensorflow import TensorFlow - - return TensorFlow( + return estimator.TensorFlow( training_steps=self.training_steps, evaluation_steps=self.evaluation_steps, py_version=self.python, @@ -47,9 +48,7 @@ def create_estimator(self): class TensorFlowHostCommand(HostCommand): def create_model(self, model_url): - from sagemaker.tensorflow.model import TensorFlowModel - - return TensorFlowModel( + return model.TensorFlowModel( model_data=model_url, role=self.role_name, entry_point=self.script, diff --git a/src/sagemaker/tensorflow/__init__.py b/src/sagemaker/tensorflow/__init__.py index 9b69d7d3f5..7230104071 100644 --- a/src/sagemaker/tensorflow/__init__.py +++ b/src/sagemaker/tensorflow/__init__.py @@ -19,12 +19,7 @@ # classes for tensorflow serving. Currently tensorflow_serving_api can only be pip-installed for python 2. sys.path.append(os.path.dirname(__file__)) -from sagemaker.tensorflow.estimator import ( # noqa: E402,F401 # pylint: disable=wrong-import-position - TensorFlow, -) -from sagemaker.tensorflow.model import ( # noqa: E402,F401 # pylint: disable=wrong-import-position - TensorFlowModel, -) -from sagemaker.tensorflow.model import ( # noqa: E402,F401 # pylint: disable=wrong-import-position - TensorFlowPredictor, +from sagemaker.tensorflow import ( # noqa: E402,F401 # pylint: disable=wrong-import-position + estimator, ) +from sagemaker.tensorflow import model # noqa: E402,F401 # pylint: disable=wrong-import-position diff --git a/tests/component/test_tf_estimator.py b/tests/component/test_tf_estimator.py index b2e4b90fd7..b8ad73cad4 100644 --- a/tests/component/test_tf_estimator.py +++ b/tests/component/test_tf_estimator.py @@ -14,7 +14,7 @@ import pytest from mock import Mock -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator SCRIPT = "resnet_cifar_10.py" @@ -53,7 +53,7 @@ def sagemaker_session(): # Test that we pass all necessary fields from estimator to the session when we call deploy def test_deploy(sagemaker_session, tf_version): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=SCRIPT, source_dir=SOURCE_DIR, role=ROLE, @@ -64,13 +64,13 @@ def test_deploy(sagemaker_session, tf_version): base_job_name="test-cifar", ) - estimator.fit("s3://mybucket/train") - print("job succeeded: {}".format(estimator.latest_training_job.name)) + tensorflow_estimator.fit("s3://mybucket/train") + print("job succeeded: {}".format(tensorflow_estimator.latest_training_job.name)) - estimator.deploy(initial_instance_count=1, instance_type=INSTANCE_TYPE_CPU) + tensorflow_estimator.deploy(initial_instance_count=1, instance_type=INSTANCE_TYPE_CPU) image = IMAGE_URI_FORMAT_STRING.format(REGION, CPU_IMAGE_NAME, tf_version, "cpu", "py2") sagemaker_session.create_model.assert_called_with( - estimator._current_job_name, + tensorflow_estimator._current_job_name, ROLE, { "Environment": { diff --git a/tests/integ/test_local_mode.py b/tests/integ/test_local_mode.py index d5a34d0b3e..fa23a5939d 100644 --- a/tests/integ/test_local_mode.py +++ b/tests/integ/test_local_mode.py @@ -27,7 +27,7 @@ from sagemaker.local import LocalSession, LocalSagemakerRuntimeClient, LocalSagemakerClient from sagemaker.mxnet import MXNet -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator # endpoint tests all use the same port, so we use this lock to prevent concurrent execution LOCK_PATH = os.path.join(tempfile.gettempdir(), "sagemaker_test_local_mode_lock") @@ -90,7 +90,7 @@ def test_tf_local_mode(sagemaker_local_session): with stopit.ThreadingTimeout(5 * 60, swallow_exc=False): script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", framework_version="1.12", @@ -103,16 +103,16 @@ def test_tf_local_mode(sagemaker_local_session): sagemaker_session=sagemaker_local_session, ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=DATA_PATH, key_prefix="integ-test-data/tf_iris" ) - estimator.fit(inputs) - print("job succeeded: {}".format(estimator.latest_training_job.name)) + tensorflow_estimator.fit(inputs) + print("job succeeded: {}".format(tensorflow_estimator.latest_training_job.name)) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with lock.lock(LOCK_PATH): try: - json_predictor = estimator.deploy( + json_predictor = tensorflow_estimator.deploy( initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name ) @@ -124,7 +124,7 @@ def test_tf_local_mode(sagemaker_local_session): assert dict_result == list_result finally: - estimator.delete_endpoint() + tensorflow_estimator.delete_endpoint() @pytest.mark.local_mode @@ -133,7 +133,7 @@ def test_tf_distributed_local_mode(sagemaker_local_session): with stopit.ThreadingTimeout(5 * 60, swallow_exc=False): script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", framework_version="1.12", @@ -147,14 +147,14 @@ def test_tf_distributed_local_mode(sagemaker_local_session): ) inputs = "file://" + DATA_PATH - estimator.fit(inputs) - print("job succeeded: {}".format(estimator.latest_training_job.name)) + tensorflow_estimator.fit(inputs) + print("job succeeded: {}".format(tensorflow_estimator.latest_training_job.name)) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with lock.lock(LOCK_PATH): try: - json_predictor = estimator.deploy( + json_predictor = tensorflow_estimator.deploy( initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name ) @@ -166,7 +166,7 @@ def test_tf_distributed_local_mode(sagemaker_local_session): assert dict_result == list_result finally: - estimator.delete_endpoint() + tensorflow_estimator.delete_endpoint() @pytest.mark.local_mode @@ -175,7 +175,7 @@ def test_tf_local_data(sagemaker_local_session): with stopit.ThreadingTimeout(5 * 60, swallow_exc=False): script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", framework_version="1.12", @@ -189,13 +189,13 @@ def test_tf_local_data(sagemaker_local_session): ) inputs = "file://" + DATA_PATH - estimator.fit(inputs) - print("job succeeded: {}".format(estimator.latest_training_job.name)) + tensorflow_estimator.fit(inputs) + print("job succeeded: {}".format(tensorflow_estimator.latest_training_job.name)) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with lock.lock(LOCK_PATH): try: - json_predictor = estimator.deploy( + json_predictor = tensorflow_estimator.deploy( initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name ) @@ -207,7 +207,7 @@ def test_tf_local_data(sagemaker_local_session): assert dict_result == list_result finally: - estimator.delete_endpoint() + tensorflow_estimator.delete_endpoint() @pytest.mark.local_mode @@ -216,7 +216,7 @@ def test_tf_local_data_local_script(): with stopit.ThreadingTimeout(5 * 60, swallow_exc=False): script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", framework_version="1.12", @@ -231,13 +231,13 @@ def test_tf_local_data_local_script(): inputs = "file://" + DATA_PATH - estimator.fit(inputs) - print("job succeeded: {}".format(estimator.latest_training_job.name)) + tensorflow_estimator.fit(inputs) + print("job succeeded: {}".format(tensorflow_estimator.latest_training_job.name)) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with lock.lock(LOCK_PATH): try: - json_predictor = estimator.deploy( + json_predictor = tensorflow_estimator.deploy( initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name ) @@ -249,7 +249,7 @@ def test_tf_local_data_local_script(): assert dict_result == list_result finally: - estimator.delete_endpoint() + tensorflow_estimator.delete_endpoint() @pytest.mark.local_mode diff --git a/tests/unit/test_airflow.py b/tests/unit/test_airflow.py index 2cd6bbc9af..069eb04dc8 100644 --- a/tests/unit/test_airflow.py +++ b/tests/unit/test_airflow.py @@ -16,7 +16,8 @@ import pytest from mock import Mock, MagicMock, patch -from sagemaker import chainer, estimator, model, mxnet, tensorflow, transformer, tuner +from sagemaker import chainer, estimator, model, mxnet, transformer, tuner +from sagemaker.tensorflow import estimator as tf_estimator from sagemaker.workflow import airflow from sagemaker.amazon import amazon_estimator from sagemaker.amazon import knn, ntm, pca @@ -163,7 +164,7 @@ def test_byo_training_config_all_args(sagemaker_session): @patch("sagemaker.utils.sagemaker_timestamp", MagicMock(return_value=TIME_STAMP)) def test_framework_training_config_required_args(sagemaker_session): - tf = tensorflow.TensorFlow( + tf = tf_estimator.TensorFlow( entry_point="{{ entry_point }}", framework_version="1.10.0", training_steps=1000, @@ -232,7 +233,7 @@ def test_framework_training_config_required_args(sagemaker_session): @patch("sagemaker.utils.sagemaker_timestamp", MagicMock(return_value=TIME_STAMP)) def test_framework_training_config_all_args(sagemaker_session): - tf = tensorflow.TensorFlow( + tf = tf_estimator.TensorFlow( entry_point="{{ entry_point }}", source_dir="{{ source_dir }}", enable_cloudwatch_metrics=False, diff --git a/tests/unit/test_tf_estimator.py b/tests/unit/test_tf_estimator.py index 93040ba6e3..24e3d6b465 100644 --- a/tests/unit/test_tf_estimator.py +++ b/tests/unit/test_tf_estimator.py @@ -22,7 +22,9 @@ from sagemaker.fw_utils import create_image_uri from sagemaker.model import MODEL_SERVER_WORKERS_PARAM_NAME from sagemaker.session import s3_input -from sagemaker.tensorflow import defaults, TensorFlow, TensorFlowModel, TensorFlowPredictor +from sagemaker.tensorflow import defaults +from sagemaker.tensorflow import estimator +from sagemaker.tensorflow import model import sagemaker.tensorflow.estimator as tfe @@ -158,7 +160,7 @@ def _build_tf( evaluation_steps=None, **kwargs ): - return TensorFlow( + return estimator.TensorFlow( entry_point=SCRIPT_PATH, training_steps=training_steps, evaluation_steps=evaluation_steps, @@ -244,7 +246,7 @@ def test_tf_nonexistent_requirements_path(sagemaker_session): def test_create_model(sagemaker_session, tf_version): container_log_level = '"logging.INFO"' source_dir = "s3://mybucket/source" - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -277,7 +279,7 @@ def test_create_model_with_optional_params(sagemaker_session): container_log_level = '"logging.INFO"' source_dir = "s3://mybucket/source" enable_cloudwatch_metrics = "true" - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -311,7 +313,7 @@ def test_transformer_creation_with_endpoint_type(create_model, sagemaker_session model = Mock() create_model.return_value = model - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -348,7 +350,7 @@ def test_transformer_creation_without_endpoint_type(create_model, sagemaker_sess model = Mock() create_model.return_value = model - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -383,7 +385,7 @@ def test_create_model_with_custom_image(sagemaker_session): container_log_level = '"logging.INFO"' source_dir = "s3://mybucket/source" custom_image = "tensorflow:1.0" - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -408,7 +410,7 @@ def test_create_model_with_custom_image(sagemaker_session): @patch("time.strftime", MagicMock(return_value=TIMESTAMP)) @patch("time.time", MagicMock(return_value=TIME)) def test_tf(sagemaker_session, tf_version): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_FILE, role=ROLE, sagemaker_session=sagemaker_session, @@ -434,7 +436,7 @@ def test_tf(sagemaker_session, tf_version): actual_train_args = sagemaker_session.method_calls[0][2] assert actual_train_args == expected_train_args - model = tf.create_model() + tensorflow_model = tf.create_model() environment = { "Environment": { @@ -448,11 +450,11 @@ def test_tf(sagemaker_session, tf_version): "Image": create_image_uri("us-west-2", "tensorflow", INSTANCE_TYPE, tf_version, "py2"), "ModelDataUrl": "s3://m/m.tar.gz", } - assert environment == model.prepare_container_def(INSTANCE_TYPE) + assert environment == tensorflow_model.prepare_container_def(INSTANCE_TYPE) - assert "cpu" in model.prepare_container_def(INSTANCE_TYPE)["Image"] + assert "cpu" in tensorflow_model.prepare_container_def(INSTANCE_TYPE)["Image"] predictor = tf.deploy(1, INSTANCE_TYPE) - assert isinstance(predictor, TensorFlowPredictor) + assert isinstance(predictor, model.TensorFlowPredictor) @patch("time.strftime", return_value=TIMESTAMP) @@ -463,7 +465,7 @@ def test_tf(sagemaker_session, tf_version): def test_run_tensorboard_locally_without_tensorboard_binary( time, strftime, popen, call, access, sagemaker_session ): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -482,16 +484,16 @@ def test_run_tensorboard_locally_without_tensorboard_binary( @patch("sagemaker.utils.create_tar_file", MagicMock()) def test_model(sagemaker_session, tf_version): - model = TensorFlowModel( + tensorflow_model = estimator.TensorFlowModel( MODEL_DATA, role=ROLE, entry_point=SCRIPT_PATH, sagemaker_session=sagemaker_session ) - predictor = model.deploy(1, INSTANCE_TYPE) - assert isinstance(predictor, TensorFlowPredictor) + predictor = tensorflow_model.deploy(1, INSTANCE_TYPE) + assert isinstance(predictor, model.TensorFlowPredictor) @patch("sagemaker.fw_utils.tar_and_upload_dir", MagicMock()) def test_model_image_accelerator(sagemaker_session): - model = TensorFlowModel( + model = estimator.TensorFlowModel( MODEL_DATA, role=ROLE, entry_point=SCRIPT_PATH, sagemaker_session=sagemaker_session ) container_def = model.prepare_container_def(INSTANCE_TYPE, accelerator_type=ACCELERATOR_TYPE) @@ -506,7 +508,7 @@ def test_model_image_accelerator(sagemaker_session): def test_run_tensorboard_locally_without_awscli_binary( time, strftime, popen, call, access, sagemaker_session ): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -536,7 +538,7 @@ def test_run_tensorboard_locally_without_awscli_binary( def test_run_tensorboard_locally( sleep, time, strftime, popen, call, access, rmtree, mkdtemp, sync, sagemaker_session ): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -569,7 +571,7 @@ def test_run_tensorboard_locally( def test_run_tensorboard_locally_port_in_use( sleep, time, strftime, popen, call, access, socket, rmtree, mkdtemp, sync, sagemaker_session ): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -645,7 +647,7 @@ def test_tf_checkpoint_set(sagemaker_session): @patch("sagemaker.utils.create_tar_file", MagicMock()) def test_train_image_default(sagemaker_session): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -690,24 +692,26 @@ def test_attach(sagemaker_session, tf_version): name="describe_training_job", return_value=rjd ) - estimator = TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) - assert estimator.latest_training_job.job_name == "neo" - assert estimator.py_version == "py2" - assert estimator.framework_version == tf_version - assert estimator.role == "arn:aws:iam::366:role/SageMakerRole" - assert estimator.train_instance_count == 1 - assert estimator.train_max_run == 24 * 60 * 60 - assert estimator.input_mode == "File" - assert estimator.training_steps == 100 - assert estimator.evaluation_steps == 10 - assert estimator.input_mode == "File" - assert estimator.base_job_name == "neo" - assert estimator.output_path == "s3://place/output/neo" - assert estimator.output_kms_key == "" - assert estimator.hyperparameters()["training_steps"] == "100" - assert estimator.source_dir == "s3://some/sourcedir.tar.gz" - assert estimator.entry_point == "iris-dnn-classifier.py" - assert estimator.checkpoint_path == "s3://other/1508872349" + tensorflow_estimator = estimator.TensorFlow.attach( + training_job_name="neo", sagemaker_session=sagemaker_session + ) + assert tensorflow_estimator.latest_training_job.job_name == "neo" + assert tensorflow_estimator.py_version == "py2" + assert tensorflow_estimator.framework_version == tf_version + assert tensorflow_estimator.role == "arn:aws:iam::366:role/SageMakerRole" + assert tensorflow_estimator.train_instance_count == 1 + assert tensorflow_estimator.train_max_run == 24 * 60 * 60 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.training_steps == 100 + assert tensorflow_estimator.evaluation_steps == 10 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.base_job_name == "neo" + assert tensorflow_estimator.output_path == "s3://place/output/neo" + assert tensorflow_estimator.output_kms_key == "" + assert tensorflow_estimator.hyperparameters()["training_steps"] == "100" + assert tensorflow_estimator.source_dir == "s3://some/sourcedir.tar.gz" + assert tensorflow_estimator.entry_point == "iris-dnn-classifier.py" + assert tensorflow_estimator.checkpoint_path == "s3://other/1508872349" @patch("sagemaker.utils.create_tar_file", MagicMock()) @@ -744,25 +748,27 @@ def test_attach_new_repo_name(sagemaker_session, tf_version): name="describe_training_job", return_value=rjd ) - estimator = TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) - assert estimator.latest_training_job.job_name == "neo" - assert estimator.py_version == "py2" - assert estimator.framework_version == tf_version - assert estimator.role == "arn:aws:iam::366:role/SageMakerRole" - assert estimator.train_instance_count == 1 - assert estimator.train_max_run == 24 * 60 * 60 - assert estimator.input_mode == "File" - assert estimator.training_steps == 100 - assert estimator.evaluation_steps == 10 - assert estimator.input_mode == "File" - assert estimator.base_job_name == "neo" - assert estimator.output_path == "s3://place/output/neo" - assert estimator.output_kms_key == "" - assert estimator.hyperparameters()["training_steps"] == "100" - assert estimator.source_dir == "s3://some/sourcedir.tar.gz" - assert estimator.entry_point == "iris-dnn-classifier.py" - assert estimator.checkpoint_path == "s3://other/1508872349" - assert estimator.train_image() == training_image + tensorflow_estimator = estimator.TensorFlow.attach( + training_job_name="neo", sagemaker_session=sagemaker_session + ) + assert tensorflow_estimator.latest_training_job.job_name == "neo" + assert tensorflow_estimator.py_version == "py2" + assert tensorflow_estimator.framework_version == tf_version + assert tensorflow_estimator.role == "arn:aws:iam::366:role/SageMakerRole" + assert tensorflow_estimator.train_instance_count == 1 + assert tensorflow_estimator.train_max_run == 24 * 60 * 60 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.training_steps == 100 + assert tensorflow_estimator.evaluation_steps == 10 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.base_job_name == "neo" + assert tensorflow_estimator.output_path == "s3://place/output/neo" + assert tensorflow_estimator.output_kms_key == "" + assert tensorflow_estimator.hyperparameters()["training_steps"] == "100" + assert tensorflow_estimator.source_dir == "s3://some/sourcedir.tar.gz" + assert tensorflow_estimator.entry_point == "iris-dnn-classifier.py" + assert tensorflow_estimator.checkpoint_path == "s3://other/1508872349" + assert tensorflow_estimator.train_image() == training_image @patch("sagemaker.utils.create_tar_file", MagicMock()) @@ -797,24 +803,26 @@ def test_attach_old_container(sagemaker_session): name="describe_training_job", return_value=rjd ) - estimator = TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) - assert estimator.latest_training_job.job_name == "neo" - assert estimator.py_version == "py2" - assert estimator.framework_version == "1.4" - assert estimator.role == "arn:aws:iam::366:role/SageMakerRole" - assert estimator.train_instance_count == 1 - assert estimator.train_max_run == 24 * 60 * 60 - assert estimator.input_mode == "File" - assert estimator.training_steps == 100 - assert estimator.evaluation_steps == 10 - assert estimator.input_mode == "File" - assert estimator.base_job_name == "neo" - assert estimator.output_path == "s3://place/output/neo" - assert estimator.output_kms_key == "" - assert estimator.hyperparameters()["training_steps"] == "100" - assert estimator.source_dir == "s3://some/sourcedir.tar.gz" - assert estimator.entry_point == "iris-dnn-classifier.py" - assert estimator.checkpoint_path == "s3://other/1508872349" + tensorflow_estimator = estimator.TensorFlow.attach( + training_job_name="neo", sagemaker_session=sagemaker_session + ) + assert tensorflow_estimator.latest_training_job.job_name == "neo" + assert tensorflow_estimator.py_version == "py2" + assert tensorflow_estimator.framework_version == "1.4" + assert tensorflow_estimator.role == "arn:aws:iam::366:role/SageMakerRole" + assert tensorflow_estimator.train_instance_count == 1 + assert tensorflow_estimator.train_max_run == 24 * 60 * 60 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.training_steps == 100 + assert tensorflow_estimator.evaluation_steps == 10 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.base_job_name == "neo" + assert tensorflow_estimator.output_path == "s3://place/output/neo" + assert tensorflow_estimator.output_kms_key == "" + assert tensorflow_estimator.hyperparameters()["training_steps"] == "100" + assert tensorflow_estimator.source_dir == "s3://some/sourcedir.tar.gz" + assert tensorflow_estimator.entry_point == "iris-dnn-classifier.py" + assert tensorflow_estimator.checkpoint_path == "s3://other/1508872349" def test_attach_wrong_framework(sagemaker_session): @@ -848,7 +856,7 @@ def test_attach_wrong_framework(sagemaker_session): ) with pytest.raises(ValueError) as error: - TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) + estimator.TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) assert "didn't use image for requested framework" in str(error) @@ -883,14 +891,16 @@ def test_attach_custom_image(sagemaker_session): name="describe_training_job", return_value=rjd ) - estimator = TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) - assert estimator.image_name == training_image - assert estimator.train_image() == training_image + tensorflow_estimator = estimator.TensorFlow.attach( + training_job_name="neo", sagemaker_session=sagemaker_session + ) + assert tensorflow_estimator.image_name == training_image + assert tensorflow_estimator.train_image() == training_image @patch("sagemaker.fw_utils.empty_framework_version_warning") def test_empty_framework_version(warning, sagemaker_session): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -899,8 +909,8 @@ def test_empty_framework_version(warning, sagemaker_session): framework_version=None, ) - assert estimator.framework_version == defaults.TF_VERSION - warning.assert_called_with(defaults.TF_VERSION, estimator.LATEST_VERSION) + assert tensorflow_estimator.framework_version == defaults.TF_VERSION + warning.assert_called_with(defaults.TF_VERSION, tensorflow_estimator.LATEST_VERSION) def _deprecated_args_msg(args): @@ -992,7 +1002,7 @@ def test_script_mode_create_model(create_tfs_model, sagemaker_session): def test_script_mode_tensorboard( sleep, time, strftime, popen, call, access, start, sync, sagemaker_session ): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_PATH, role=ROLE, sagemaker_session=sagemaker_session, @@ -1010,7 +1020,7 @@ def test_script_mode_tensorboard( @patch("time.time", return_value=TIME) @patch("sagemaker.utils.create_tar_file", MagicMock()) def test_tf_script_mode(time, strftime, sagemaker_session): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_FILE, role=ROLE, sagemaker_session=sagemaker_session, @@ -1040,7 +1050,7 @@ def test_tf_script_mode(time, strftime, sagemaker_session): @patch("time.time", return_value=TIME) @patch("sagemaker.utils.create_tar_file", MagicMock()) def test_tf_script_mode_ps(time, strftime, sagemaker_session): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_FILE, role=ROLE, sagemaker_session=sagemaker_session, @@ -1062,7 +1072,9 @@ def test_tf_script_mode_ps(time, strftime, sagemaker_session): "1.11", script_mode=True, repo_name=SM_IMAGE_REPO_NAME, py_version="py3" ) expected_train_args["input_config"][0]["DataSource"]["S3DataSource"]["S3Uri"] = inputs - expected_train_args["hyperparameters"][TensorFlow.LAUNCH_PS_ENV_NAME] = json.dumps(True) + expected_train_args["hyperparameters"][estimator.TensorFlow.LAUNCH_PS_ENV_NAME] = json.dumps( + True + ) actual_train_args = sagemaker_session.method_calls[0][2] assert actual_train_args == expected_train_args @@ -1072,7 +1084,7 @@ def test_tf_script_mode_ps(time, strftime, sagemaker_session): @patch("time.time", return_value=TIME) @patch("sagemaker.utils.create_tar_file", MagicMock()) def test_tf_script_mode_mpi(time, strftime, sagemaker_session): - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point=SCRIPT_FILE, role=ROLE, sagemaker_session=sagemaker_session, @@ -1094,11 +1106,15 @@ def test_tf_script_mode_mpi(time, strftime, sagemaker_session): "1.11", script_mode=True, horovod=True, repo_name=SM_IMAGE_REPO_NAME, py_version="py3" ) expected_train_args["input_config"][0]["DataSource"]["S3DataSource"]["S3Uri"] = inputs - expected_train_args["hyperparameters"][TensorFlow.LAUNCH_MPI_ENV_NAME] = json.dumps(True) - expected_train_args["hyperparameters"][TensorFlow.MPI_NUM_PROCESSES_PER_HOST] = json.dumps(2) - expected_train_args["hyperparameters"][TensorFlow.MPI_CUSTOM_MPI_OPTIONS] = json.dumps( - "options" + expected_train_args["hyperparameters"][estimator.TensorFlow.LAUNCH_MPI_ENV_NAME] = json.dumps( + True ) + expected_train_args["hyperparameters"][ + estimator.TensorFlow.MPI_NUM_PROCESSES_PER_HOST + ] = json.dumps(2) + expected_train_args["hyperparameters"][ + estimator.TensorFlow.MPI_CUSTOM_MPI_OPTIONS + ] = json.dumps("options") actual_train_args = sagemaker_session.method_calls[0][2] assert actual_train_args == expected_train_args @@ -1135,18 +1151,20 @@ def test_tf_script_mode_attach(sagemaker_session, tf_version): name="describe_training_job", return_value=rjd ) - estimator = TensorFlow.attach(training_job_name="neo", sagemaker_session=sagemaker_session) - assert estimator.latest_training_job.job_name == "neo" - assert estimator.py_version == "py3" - assert estimator.framework_version == tf_version - assert estimator.role == "arn:aws:iam::366:role/SageMakerRole" - assert estimator.train_instance_count == 1 - assert estimator.train_max_run == 24 * 60 * 60 - assert estimator.input_mode == "File" - assert estimator.input_mode == "File" - assert estimator.base_job_name == "neo" - assert estimator.output_path == "s3://place/output/neo" - assert estimator.output_kms_key == "" - assert estimator.hyperparameters() is not None - assert estimator.source_dir == "s3://some/sourcedir.tar.gz" - assert estimator.entry_point == "iris-dnn-classifier.py" + tensorflow_estimator = estimator.TensorFlow.attach( + training_job_name="neo", sagemaker_session=sagemaker_session + ) + assert tensorflow_estimator.latest_training_job.job_name == "neo" + assert tensorflow_estimator.py_version == "py3" + assert tensorflow_estimator.framework_version == tf_version + assert tensorflow_estimator.role == "arn:aws:iam::366:role/SageMakerRole" + assert tensorflow_estimator.train_instance_count == 1 + assert tensorflow_estimator.train_max_run == 24 * 60 * 60 + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.input_mode == "File" + assert tensorflow_estimator.base_job_name == "neo" + assert tensorflow_estimator.output_path == "s3://place/output/neo" + assert tensorflow_estimator.output_kms_key == "" + assert tensorflow_estimator.hyperparameters() is not None + assert tensorflow_estimator.source_dir == "s3://some/sourcedir.tar.gz" + assert tensorflow_estimator.entry_point == "iris-dnn-classifier.py" diff --git a/tests/unit/test_tfs.py b/tests/unit/test_tfs.py index 9ab59e6b7d..527d86cd27 100644 --- a/tests/unit/test_tfs.py +++ b/tests/unit/test_tfs.py @@ -19,7 +19,7 @@ import mock import pytest from mock import Mock -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator from sagemaker.tensorflow.predictor import csv_serializer from sagemaker.tensorflow.serving import Model, Predictor @@ -208,7 +208,7 @@ def test_estimator_deploy(sagemaker_session): container_log_level = '"logging.INFO"' source_dir = "s3://mybucket/source" custom_image = "custom:1.0" - tf = TensorFlow( + tf = estimator.TensorFlow( entry_point="script.py", role=ROLE, sagemaker_session=sagemaker_session, From ff57e34e1a1207cc41666ecae5aad7a110e90a39 Mon Sep 17 00:00:00 2001 From: Karim Nakad Date: Tue, 9 Jul 2019 09:50:51 -0700 Subject: [PATCH 3/4] fix: fixing integration tests --- tests/integ/test_horovod.py | 12 +++--- tests/integ/test_tf_cifar.py | 14 +++---- tests/integ/test_tf_keras.py | 12 +++--- tests/integ/test_tf_script_mode.py | 60 +++++++++++++++--------------- tests/integ/test_tuner.py | 38 +++++++++---------- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/tests/integ/test_horovod.py b/tests/integ/test_horovod.py index 76e50b15e6..19aa5e9ab0 100644 --- a/tests/integ/test_horovod.py +++ b/tests/integ/test_horovod.py @@ -22,7 +22,7 @@ import sagemaker.utils import tests.integ as integ -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator from tests.integ import test_region, timeout, HOSTING_NO_P3_REGIONS horovod_dir = os.path.join(os.path.dirname(__file__), "..", "data", "horovod") @@ -47,7 +47,7 @@ def instance_type(request): @pytest.mark.canary_quick def test_horovod(sagemaker_session, instance_type, tmpdir): job_name = sagemaker.utils.unique_name_from_base("tf-horovod") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=os.path.join(horovod_dir, "test_hvd_basic.py"), role="SageMakerRole", train_instance_count=2, @@ -60,10 +60,10 @@ def test_horovod(sagemaker_session, instance_type, tmpdir): ) with timeout.timeout(minutes=integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): - estimator.fit(job_name=job_name) + tensorflow_estimator.fit(job_name=job_name) tmp = str(tmpdir) - extract_files_from_s3(estimator.model_data, tmp) + extract_files_from_s3(tensorflow_estimator.model_data, tmp) for rank in range(2): assert read_json("rank-%s" % rank, tmp)["rank"] == rank @@ -74,7 +74,7 @@ def test_horovod(sagemaker_session, instance_type, tmpdir): def test_horovod_local_mode(sagemaker_local_session, instances, processes, tmpdir): output_path = "file://%s" % tmpdir job_name = sagemaker.utils.unique_name_from_base("tf-horovod") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=os.path.join(horovod_dir, "test_hvd_basic.py"), role="SageMakerRole", train_instance_count=2, @@ -88,7 +88,7 @@ def test_horovod_local_mode(sagemaker_local_session, instances, processes, tmpdi ) with timeout.timeout(minutes=integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): - estimator.fit(job_name=job_name) + tensorflow_estimator.fit(job_name=job_name) tmp = str(tmpdir) extract_files(output_path.replace("file://", ""), tmp) diff --git a/tests/integ/test_tf_cifar.py b/tests/integ/test_tf_cifar.py index 75a1f8635f..d4e7b9d8f0 100644 --- a/tests/integ/test_tf_cifar.py +++ b/tests/integ/test_tf_cifar.py @@ -21,7 +21,7 @@ import tests.integ from tests.integ.timeout import timeout_and_delete_endpoint_by_name, timeout -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator from sagemaker.utils import unique_name_from_base PICKLE_CONTENT_TYPE = "application/python-pickle" @@ -50,7 +50,7 @@ def test_cifar(sagemaker_session): dataset_path = os.path.join(tests.integ.DATA_DIR, "cifar_10", "data") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point="resnet_cifar_10.py", source_dir=script_path, role="SageMakerRole", @@ -64,17 +64,17 @@ def test_cifar(sagemaker_session): base_job_name="test-cifar", ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=dataset_path, key_prefix="data/cifar10" ) job_name = unique_name_from_base("test-tf-cifar") - estimator.fit(inputs, logs=False, job_name=job_name) - print("job succeeded: {}".format(estimator.latest_training_job.name)) + tensorflow_estimator.fit(inputs, logs=False, job_name=job_name) + print("job succeeded: {}".format(tensorflow_estimator.latest_training_job.name)) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): - predictor = estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge") + predictor = tensorflow_estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge") predictor.serializer = PickleSerializer() predictor.content_type = PICKLE_CONTENT_TYPE diff --git a/tests/integ/test_tf_keras.py b/tests/integ/test_tf_keras.py index 9939e67b2a..eb5ce0b382 100644 --- a/tests/integ/test_tf_keras.py +++ b/tests/integ/test_tf_keras.py @@ -20,7 +20,7 @@ import tests.integ from tests.integ.timeout import timeout_and_delete_endpoint_by_name, timeout -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator from sagemaker.utils import unique_name_from_base @@ -37,7 +37,7 @@ def test_keras(sagemaker_session): dataset_path = os.path.join(tests.integ.DATA_DIR, "cifar_10", "data") with timeout(minutes=45): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point="keras_cnn_cifar_10.py", source_dir=script_path, role="SageMakerRole", @@ -51,16 +51,16 @@ def test_keras(sagemaker_session): train_max_run=45 * 60, ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=dataset_path, key_prefix="data/cifar10" ) job_name = unique_name_from_base("test-tf-keras") - estimator.fit(inputs, job_name=job_name) + tensorflow_estimator.fit(inputs, job_name=job_name) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): - predictor = estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge") + predictor = tensorflow_estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge") data = np.random.randn(32, 32, 3) predict_response = predictor.predict(data) diff --git a/tests/integ/test_tf_script_mode.py b/tests/integ/test_tf_script_mode.py index 685dbae36b..12dba41e20 100644 --- a/tests/integ/test_tf_script_mode.py +++ b/tests/integ/test_tf_script_mode.py @@ -19,7 +19,7 @@ import pytest import boto3 -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator from six.moves.urllib.parse import urlparse from sagemaker.utils import unique_name_from_base @@ -57,27 +57,27 @@ def instance_type(request): def test_mnist(sagemaker_session, instance_type): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=SCRIPT, role="SageMakerRole", train_instance_count=1, train_instance_type=instance_type, sagemaker_session=sagemaker_session, script_mode=True, - framework_version=TensorFlow.LATEST_VERSION, + framework_version=estimator.TensorFlow.LATEST_VERSION, py_version=tests.integ.PYTHON_VERSION, metric_definitions=[{"Name": "train:global_steps", "Regex": r"global_step\/sec:\s(.*)"}], ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=os.path.join(MNIST_RESOURCE_PATH, "data"), key_prefix="scriptmode/mnist" ) with tests.integ.timeout.timeout(minutes=tests.integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): - estimator.fit(inputs=inputs, job_name=unique_name_from_base("test-tf-sm-mnist")) + tensorflow_estimator.fit(inputs=inputs, job_name=unique_name_from_base("test-tf-sm-mnist")) _assert_s3_files_exist( - estimator.model_dir, ["graph.pbtxt", "model.ckpt-0.index", "model.ckpt-0.meta"] + tensorflow_estimator.model_dir, ["graph.pbtxt", "model.ckpt-0.index", "model.ckpt-0.meta"] ) - df = estimator.training_job_analytics.dataframe() + df = tensorflow_estimator.training_job_analytics.dataframe() assert df.size > 0 @@ -91,14 +91,14 @@ def test_server_side_encryption(sagemaker_session): bucket_with_kms, "test-server-side-encryption", time.strftime("%y%m%d-%H%M") ) - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=SCRIPT, role=ROLE, train_instance_count=1, train_instance_type="ml.c5.xlarge", sagemaker_session=sagemaker_session, script_mode=True, - framework_version=TensorFlow.LATEST_VERSION, + framework_version=estimator.TensorFlow.LATEST_VERSION, py_version=tests.integ.PYTHON_VERSION, code_location=output_path, output_path=output_path, @@ -106,19 +106,19 @@ def test_server_side_encryption(sagemaker_session): output_kms_key=kms_key, ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=os.path.join(MNIST_RESOURCE_PATH, "data"), key_prefix="scriptmode/mnist" ) with tests.integ.timeout.timeout(minutes=tests.integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): - estimator.fit( + tensorflow_estimator.fit( inputs=inputs, job_name=unique_name_from_base("test-server-side-encryption") ) @pytest.mark.canary_quick def test_mnist_distributed(sagemaker_session, instance_type): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=SCRIPT, role=ROLE, train_instance_count=2, @@ -126,22 +126,22 @@ def test_mnist_distributed(sagemaker_session, instance_type): sagemaker_session=sagemaker_session, py_version=tests.integ.PYTHON_VERSION, script_mode=True, - framework_version=TensorFlow.LATEST_VERSION, + framework_version=estimator.TensorFlow.LATEST_VERSION, distributions=PARAMETER_SERVER_DISTRIBUTION, ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=os.path.join(MNIST_RESOURCE_PATH, "data"), key_prefix="scriptmode/distributed_mnist" ) with tests.integ.timeout.timeout(minutes=tests.integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): - estimator.fit(inputs=inputs, job_name=unique_name_from_base("test-tf-sm-distributed")) + tensorflow_estimator.fit(inputs=inputs, job_name=unique_name_from_base("test-tf-sm-distributed")) _assert_s3_files_exist( - estimator.model_dir, ["graph.pbtxt", "model.ckpt-0.index", "model.ckpt-0.meta"] + tensorflow_estimator.model_dir, ["graph.pbtxt", "model.ckpt-0.index", "model.ckpt-0.meta"] ) def test_mnist_async(sagemaker_session): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=SCRIPT, role=ROLE, train_instance_count=1, @@ -149,25 +149,25 @@ def test_mnist_async(sagemaker_session): py_version=tests.integ.PYTHON_VERSION, sagemaker_session=sagemaker_session, script_mode=True, - framework_version=TensorFlow.LATEST_VERSION, + framework_version=estimator.TensorFlow.LATEST_VERSION, tags=TAGS, ) - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=os.path.join(MNIST_RESOURCE_PATH, "data"), key_prefix="scriptmode/mnist" ) - estimator.fit(inputs=inputs, wait=False, job_name=unique_name_from_base("test-tf-sm-async")) - training_job_name = estimator.latest_training_job.name + tensorflow_estimator.fit(inputs=inputs, wait=False, job_name=unique_name_from_base("test-tf-sm-async")) + training_job_name = tensorflow_estimator.latest_training_job.name time.sleep(20) endpoint_name = training_job_name model_name = "model-name-1" _assert_training_job_tags_match( - sagemaker_session.sagemaker_client, estimator.latest_training_job.name, TAGS + sagemaker_session.sagemaker_client, tensorflow_estimator.latest_training_job.name, TAGS ) with tests.integ.timeout.timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): - estimator = TensorFlow.attach( + tensorflow_estimator = estimator.TensorFlow.attach( training_job_name=training_job_name, sagemaker_session=sagemaker_session ) - predictor = estimator.deploy( + predictor = tensorflow_estimator.deploy( initial_instance_count=1, instance_type="ml.c4.xlarge", endpoint_name=endpoint_name, @@ -178,13 +178,13 @@ def test_mnist_async(sagemaker_session): print("predict result: {}".format(result)) _assert_endpoint_tags_match(sagemaker_session.sagemaker_client, predictor.endpoint, TAGS) _assert_model_tags_match( - sagemaker_session.sagemaker_client, estimator.latest_training_job.name, TAGS + sagemaker_session.sagemaker_client, tensorflow_estimator.latest_training_job.name, TAGS ) _assert_model_name_match(sagemaker_session.sagemaker_client, endpoint_name, model_name) def test_deploy_with_input_handlers(sagemaker_session, instance_type): - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point="inference.py", source_dir=TFS_RESOURCE_PATH, role=ROLE, @@ -193,17 +193,17 @@ def test_deploy_with_input_handlers(sagemaker_session, instance_type): py_version=tests.integ.PYTHON_VERSION, sagemaker_session=sagemaker_session, script_mode=True, - framework_version=TensorFlow.LATEST_VERSION, + framework_version=estimator.TensorFlow.LATEST_VERSION, tags=TAGS, ) - estimator.fit(job_name=unique_name_from_base("test-tf-tfs-deploy")) + tensorflow_estimator.fit(job_name=unique_name_from_base("test-tf-tfs-deploy")) - endpoint_name = estimator.latest_training_job.name + endpoint_name = tensorflow_estimator.latest_training_job.name with timeout.timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): - predictor = estimator.deploy( + predictor = tensorflow_estimator.deploy( initial_instance_count=1, instance_type=instance_type, endpoint_name=endpoint_name ) diff --git a/tests/integ/test_tuner.py b/tests/integ/test_tuner.py index 1d74acbe06..180c1f786d 100644 --- a/tests/integ/test_tuner.py +++ b/tests/integ/test_tuner.py @@ -35,7 +35,7 @@ from sagemaker.mxnet.estimator import MXNet from sagemaker.predictor import json_deserializer from sagemaker.pytorch import PyTorch -from sagemaker.tensorflow import TensorFlow +from sagemaker.tensorflow import estimator from sagemaker.tuner import ( IntegerParameter, ContinuousParameter, @@ -584,7 +584,7 @@ def test_tuning_tf_script_mode(sagemaker_session): resource_path = os.path.join(DATA_DIR, "tensorflow_mnist") script_path = os.path.join(resource_path, "mnist.py") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", train_instance_count=1, @@ -592,7 +592,7 @@ def test_tuning_tf_script_mode(sagemaker_session): script_mode=True, sagemaker_session=sagemaker_session, py_version=PYTHON_VERSION, - framework_version=TensorFlow.LATEST_VERSION, + framework_version=estimator.TensorFlow.LATEST_VERSION, ) hyperparameter_ranges = {"epochs": IntegerParameter(1, 2)} @@ -600,7 +600,7 @@ def test_tuning_tf_script_mode(sagemaker_session): metric_definitions = [{"Name": objective_metric_name, "Regex": "accuracy = ([0-9\\.]+)"}] tuner = HyperparameterTuner( - estimator, + tensorflow_estimator, objective_metric_name, hyperparameter_ranges, metric_definitions, @@ -609,7 +609,7 @@ def test_tuning_tf_script_mode(sagemaker_session): ) with timeout(minutes=TUNING_DEFAULT_TIMEOUT_MINUTES): - inputs = estimator.sagemaker_session.upload_data( + inputs = tensorflow_estimator.sagemaker_session.upload_data( path=os.path.join(resource_path, "data"), key_prefix="scriptmode/mnist" ) @@ -628,7 +628,7 @@ def test_tuning_tf(sagemaker_session): with timeout(minutes=TUNING_DEFAULT_TIMEOUT_MINUTES): script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py") - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", training_steps=1, @@ -646,7 +646,7 @@ def test_tuning_tf(sagemaker_session): metric_definitions = [{"Name": "loss", "Regex": "loss = ([0-9\\.]+)"}] tuner = HyperparameterTuner( - estimator, + tensorflow_estimator, objective_metric_name, hyperparameter_ranges, metric_definitions, @@ -690,7 +690,7 @@ def test_tuning_tf_vpc_multi(sagemaker_session): ) vpc_test_utils.setup_security_group_for_encryption(ec2_client, security_group_id) - estimator = TensorFlow( + tensorflow_estimator = estimator.TensorFlow( entry_point=script_path, role="SageMakerRole", training_steps=1, @@ -712,7 +712,7 @@ def test_tuning_tf_vpc_multi(sagemaker_session): metric_definitions = [{"Name": "loss", "Regex": "loss = ([0-9\\.]+)"}] tuner = HyperparameterTuner( - estimator, + tensorflow_estimator, objective_metric_name, hyperparameter_ranges, metric_definitions, @@ -737,7 +737,7 @@ def test_tuning_chainer(sagemaker_session): script_path = os.path.join(DATA_DIR, "chainer_mnist", "mnist.py") data_path = os.path.join(DATA_DIR, "chainer_mnist") - estimator = Chainer( + chainer_estimator = Chainer( entry_point=script_path, role="SageMakerRole", py_version=PYTHON_VERSION, @@ -747,10 +747,10 @@ def test_tuning_chainer(sagemaker_session): hyperparameters={"epochs": 1}, ) - train_input = estimator.sagemaker_session.upload_data( + train_input = chainer_estimator.sagemaker_session.upload_data( path=os.path.join(data_path, "train"), key_prefix="integ-test-data/chainer_mnist/train" ) - test_input = estimator.sagemaker_session.upload_data( + test_input = chainer_estimator.sagemaker_session.upload_data( path=os.path.join(data_path, "test"), key_prefix="integ-test-data/chainer_mnist/test" ) @@ -765,7 +765,7 @@ def test_tuning_chainer(sagemaker_session): ] tuner = HyperparameterTuner( - estimator, + chainer_estimator, objective_metric_name, hyperparameter_ranges, metric_definitions, @@ -804,7 +804,7 @@ def test_attach_tuning_pytorch(sagemaker_session): mnist_dir = os.path.join(DATA_DIR, "pytorch_mnist") mnist_script = os.path.join(mnist_dir, "mnist.py") - estimator = PyTorch( + pytorch_estimator = PyTorch( entry_point=mnist_script, role="SageMakerRole", train_instance_count=1, @@ -821,7 +821,7 @@ def test_attach_tuning_pytorch(sagemaker_session): hyperparameter_ranges = {"batch-size": IntegerParameter(50, 100)} tuner = HyperparameterTuner( - estimator, + pytorch_estimator, objective_metric_name, hyperparameter_ranges, metric_definitions, @@ -830,7 +830,7 @@ def test_attach_tuning_pytorch(sagemaker_session): early_stopping_type="Auto", ) - training_data = estimator.sagemaker_session.upload_data( + training_data = pytorch_estimator.sagemaker_session.upload_data( path=os.path.join(mnist_dir, "training"), key_prefix="integ-test-data/pytorch_mnist/training", ) @@ -892,7 +892,7 @@ def test_tuning_byo_estimator(sagemaker_session): path=training_data_path, key_prefix=os.path.join(prefix, "train", key) ) - estimator = Estimator( + byo_estimator = Estimator( image_name=image_name, role="SageMakerRole", train_instance_count=1, @@ -900,14 +900,14 @@ def test_tuning_byo_estimator(sagemaker_session): sagemaker_session=sagemaker_session, ) - estimator.set_hyperparameters( + byo_estimator.set_hyperparameters( num_factors=10, feature_dim=784, mini_batch_size=100, predictor_type="binary_classifier" ) hyperparameter_ranges = {"mini_batch_size": IntegerParameter(100, 200)} tuner = HyperparameterTuner( - estimator=estimator, + estimator=byo_estimator, objective_metric_name="test:binary_classification_accuracy", hyperparameter_ranges=hyperparameter_ranges, max_jobs=2, From 4a19235cd97889526bf14858a0a4fb211426c876 Mon Sep 17 00:00:00 2001 From: Karim Nakad Date: Tue, 9 Jul 2019 10:02:09 -0700 Subject: [PATCH 4/4] change: reformatting --- tests/integ/test_tf_cifar.py | 4 +++- tests/integ/test_tf_keras.py | 4 +++- tests/integ/test_tf_script_mode.py | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/integ/test_tf_cifar.py b/tests/integ/test_tf_cifar.py index d4e7b9d8f0..a05f0afaa8 100644 --- a/tests/integ/test_tf_cifar.py +++ b/tests/integ/test_tf_cifar.py @@ -74,7 +74,9 @@ def test_cifar(sagemaker_session): endpoint_name = tensorflow_estimator.latest_training_job.name with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): - predictor = tensorflow_estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge") + predictor = tensorflow_estimator.deploy( + initial_instance_count=1, instance_type="ml.p2.xlarge" + ) predictor.serializer = PickleSerializer() predictor.content_type = PICKLE_CONTENT_TYPE diff --git a/tests/integ/test_tf_keras.py b/tests/integ/test_tf_keras.py index eb5ce0b382..56fa5656b9 100644 --- a/tests/integ/test_tf_keras.py +++ b/tests/integ/test_tf_keras.py @@ -60,7 +60,9 @@ def test_keras(sagemaker_session): endpoint_name = tensorflow_estimator.latest_training_job.name with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): - predictor = tensorflow_estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge") + predictor = tensorflow_estimator.deploy( + initial_instance_count=1, instance_type="ml.p2.xlarge" + ) data = np.random.randn(32, 32, 3) predict_response = predictor.predict(data) diff --git a/tests/integ/test_tf_script_mode.py b/tests/integ/test_tf_script_mode.py index 12dba41e20..dfdd112f16 100644 --- a/tests/integ/test_tf_script_mode.py +++ b/tests/integ/test_tf_script_mode.py @@ -134,7 +134,9 @@ def test_mnist_distributed(sagemaker_session, instance_type): ) with tests.integ.timeout.timeout(minutes=tests.integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): - tensorflow_estimator.fit(inputs=inputs, job_name=unique_name_from_base("test-tf-sm-distributed")) + tensorflow_estimator.fit( + inputs=inputs, job_name=unique_name_from_base("test-tf-sm-distributed") + ) _assert_s3_files_exist( tensorflow_estimator.model_dir, ["graph.pbtxt", "model.ckpt-0.index", "model.ckpt-0.meta"] ) @@ -155,7 +157,9 @@ def test_mnist_async(sagemaker_session): inputs = tensorflow_estimator.sagemaker_session.upload_data( path=os.path.join(MNIST_RESOURCE_PATH, "data"), key_prefix="scriptmode/mnist" ) - tensorflow_estimator.fit(inputs=inputs, wait=False, job_name=unique_name_from_base("test-tf-sm-async")) + tensorflow_estimator.fit( + inputs=inputs, wait=False, job_name=unique_name_from_base("test-tf-sm-async") + ) training_job_name = tensorflow_estimator.latest_training_job.name time.sleep(20) endpoint_name = training_job_name