|
| 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 time |
| 16 | + |
| 17 | +import pytest |
| 18 | +from sagemaker.pytorch import PyTorch |
| 19 | +from sagemaker import utils |
| 20 | +from smexperiments.experiment import Experiment |
| 21 | +from smexperiments.trial import Trial |
| 22 | +from smexperiments.trial_component import TrialComponent |
| 23 | +from test.integration import training_dir, smdebug_mnist_script, DEFAULT_TIMEOUT |
| 24 | +from test.integration.sagemaker.timeout import timeout |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.skip_py2_containers |
| 28 | +def test_training(sagemaker_session, ecr_image, instance_type): |
| 29 | + |
| 30 | + sm_client = sagemaker_session.sagemaker_client |
| 31 | + |
| 32 | + experiment_name = "pytorch-container-integ-test-{}".format(int(time.time())) |
| 33 | + |
| 34 | + experiment = Experiment.create( |
| 35 | + experiment_name=experiment_name, |
| 36 | + description="Integration test full customer e2e from sagemaker-pytorch-container", |
| 37 | + sagemaker_boto_client=sm_client, |
| 38 | + ) |
| 39 | + |
| 40 | + trial_name = "pytorch-container-integ-test-{}".format(int(time.time())) |
| 41 | + trial = Trial.create( |
| 42 | + experiment_name=experiment_name, trial_name=trial_name, sagemaker_boto_client=sm_client |
| 43 | + ) |
| 44 | + |
| 45 | + hyperparameters = { |
| 46 | + "random_seed": True, |
| 47 | + "num_steps": 50, |
| 48 | + "smdebug_path": "/opt/ml/output/tensors", |
| 49 | + "epochs": 1, |
| 50 | + "data_dir": training_dir, |
| 51 | + } |
| 52 | + |
| 53 | + training_job_name = utils.unique_name_from_base("test-pytorch-experiments-image") |
| 54 | + |
| 55 | + # create a training job and wait for it to complete |
| 56 | + with timeout(minutes=DEFAULT_TIMEOUT): |
| 57 | + pytorch = PyTorch( |
| 58 | + entry_point=smdebug_mnist_script, |
| 59 | + role="SageMakerRole", |
| 60 | + train_instance_count=1, |
| 61 | + train_instance_type=instance_type, |
| 62 | + sagemaker_session=sagemaker_session, |
| 63 | + image_name=ecr_image, |
| 64 | + hyperparameters=hyperparameters, |
| 65 | + ) |
| 66 | + training_input = pytorch.sagemaker_session.upload_data( |
| 67 | + path=training_dir, key_prefix="pytorch/mnist" |
| 68 | + ) |
| 69 | + pytorch.fit({"training": training_input}, job_name=training_job_name) |
| 70 | + |
| 71 | + training_job = sm_client.describe_training_job(TrainingJobName=training_job_name) |
| 72 | + training_job_arn = training_job["TrainingJobArn"] |
| 73 | + |
| 74 | + # verify trial component auto created from the training job |
| 75 | + trial_components = list( |
| 76 | + TrialComponent.list(source_arn=training_job_arn, sagemaker_boto_client=sm_client) |
| 77 | + ) |
| 78 | + |
| 79 | + trial_component_summary = trial_components[0] |
| 80 | + trial_component = TrialComponent.load( |
| 81 | + trial_component_name=trial_component_summary.trial_component_name, |
| 82 | + sagemaker_boto_client=sm_client, |
| 83 | + ) |
| 84 | + |
| 85 | + # associate the trial component with the trial |
| 86 | + trial.add_trial_component(trial_component) |
| 87 | + |
| 88 | + # cleanup |
| 89 | + trial.remove_trial_component(trial_component_summary.trial_component_name) |
| 90 | + trial_component.delete() |
| 91 | + trial.delete() |
| 92 | + experiment.delete() |
0 commit comments