|
| 1 | +# Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | +# A copy of the License is located at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is distributed |
| 10 | +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +# express or implied. See the License for the specific language governing |
| 12 | +# permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import os |
| 16 | +import time |
| 17 | + |
| 18 | +import pytest |
| 19 | +from sagemaker import utils |
| 20 | +from sagemaker.tensorflow import TensorFlow |
| 21 | + |
| 22 | +from test.integration import RESOURCE_PATH |
| 23 | +from timeout import timeout |
| 24 | + |
| 25 | +DATA_PATH = os.path.join(RESOURCE_PATH, "mnist") |
| 26 | +SCRIPT_PATH = os.path.join(DATA_PATH, "mnist_gluon_basic_hook_demo.py") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.skip_py2_containers |
| 30 | +def test_training(sagemaker_session, ecr_image, instance_type, framework_version, py_version): |
| 31 | + |
| 32 | + if py_version is None or '2' in py_version: |
| 33 | + pytest.skip('Skipping python2 {}'.format(py_version)) |
| 34 | + return |
| 35 | + |
| 36 | + from smexperiments.experiment import Experiment |
| 37 | + from smexperiments.trial import Trial |
| 38 | + from smexperiments.trial_component import TrialComponent |
| 39 | + |
| 40 | + sm_client = sagemaker_session.sagemaker_client |
| 41 | + |
| 42 | + experiment_name = "tf-container-integ-test-{}".format(int(time.time())) |
| 43 | + |
| 44 | + experiment = Experiment.create( |
| 45 | + experiment_name=experiment_name, |
| 46 | + description="Integration test experiment from sagemaker-tf-container", |
| 47 | + sagemaker_boto_client=sm_client, |
| 48 | + ) |
| 49 | + |
| 50 | + trial_name = "tf-container-integ-test-{}".format(int(time.time())) |
| 51 | + trial = Trial.create( |
| 52 | + experiment_name=experiment_name, trial_name=trial_name, sagemaker_boto_client=sm_client |
| 53 | + ) |
| 54 | + |
| 55 | + training_job_name = utils.unique_name_from_base("test-tf-experiments-mnist") |
| 56 | + |
| 57 | + # create a training job and wait for it to complete |
| 58 | + with timeout(minutes=15): |
| 59 | + resource_path = os.path.join(os.path.dirname(__file__), "..", "..", "resources") |
| 60 | + script = os.path.join(resource_path, "mnist", "mnist.py") |
| 61 | + estimator = TensorFlow( |
| 62 | + entry_point=script, |
| 63 | + role="SageMakerRole", |
| 64 | + train_instance_type=instance_type, |
| 65 | + train_instance_count=1, |
| 66 | + sagemaker_session=sagemaker_session, |
| 67 | + image_name=ecr_image, |
| 68 | + framework_version=framework_version, |
| 69 | + script_mode=True, |
| 70 | + ) |
| 71 | + inputs = estimator.sagemaker_session.upload_data( |
| 72 | + path=os.path.join(resource_path, "mnist", "data"), key_prefix="scriptmode/mnist" |
| 73 | + ) |
| 74 | + estimator.fit(inputs, job_name=training_job_name) |
| 75 | + |
| 76 | + training_job = sm_client.describe_training_job(TrainingJobName=training_job_name) |
| 77 | + training_job_arn = training_job["TrainingJobArn"] |
| 78 | + |
| 79 | + # verify trial component auto created from the training job |
| 80 | + trial_components = list( |
| 81 | + TrialComponent.list(source_arn=training_job_arn, sagemaker_boto_client=sm_client) |
| 82 | + ) |
| 83 | + |
| 84 | + trial_component_summary = trial_components[0] |
| 85 | + trial_component = TrialComponent.load( |
| 86 | + trial_component_name=trial_component_summary.trial_component_name, |
| 87 | + sagemaker_boto_client=sm_client, |
| 88 | + ) |
| 89 | + |
| 90 | + # associate the trial component with the trial |
| 91 | + trial.add_trial_component(trial_component) |
| 92 | + |
| 93 | + # cleanup |
| 94 | + trial.remove_trial_component(trial_component_summary.trial_component_name) |
| 95 | + trial_component.delete() |
| 96 | + trial.delete() |
| 97 | + experiment.delete() |
0 commit comments