|
| 1 | +# Lint as: python3 |
| 2 | +# Copyright 2021 Google LLC. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Integration tests for calling run_experiment_cloud.""" |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import tensorflow as tf |
| 20 | +import tensorflow_cloud as tfc |
| 21 | +from tensorflow_cloud.core.experimental import models |
| 22 | +from tensorflow_cloud.utils import google_api_client |
| 23 | +from official.core import task_factory |
| 24 | +from official.utils.testing import mock_task |
| 25 | + |
| 26 | +# The staging bucket to use for cloudbuild as well as save the model and data. |
| 27 | +_TEST_BUCKET = os.environ["TEST_BUCKET"] |
| 28 | +_PROJECT_ID = os.environ["PROJECT_ID"] |
| 29 | +_PARENT_IMAGE = "gcr.io/deeplearning-platform-release/tf2-gpu.2-5" |
| 30 | +_BASE_PATH = f"gs://{_TEST_BUCKET}" |
| 31 | + |
| 32 | + |
| 33 | +class RunExperimentCloudTest(tf.test.TestCase): |
| 34 | + |
| 35 | + def setUp(self): |
| 36 | + super(RunExperimentCloudTest, self).setUp() |
| 37 | + self.test_data_path = os.path.join( |
| 38 | + os.path.dirname(os.path.abspath(__file__)), "../testdata/" |
| 39 | + ) |
| 40 | + self.requirements_txt = os.path.join(self.test_data_path, |
| 41 | + "requirements.txt") |
| 42 | + |
| 43 | + self._test_config = { |
| 44 | + "trainer": { |
| 45 | + "checkpoint_interval": 10, |
| 46 | + "steps_per_loop": 10, |
| 47 | + "summary_interval": 10, |
| 48 | + "train_steps": 10, |
| 49 | + "validation_steps": 5, |
| 50 | + "validation_interval": 10, |
| 51 | + "continuous_eval_timeout": 1, |
| 52 | + "validation_summary_subdir": "validation", |
| 53 | + "optimizer_config": { |
| 54 | + "optimizer": { |
| 55 | + "type": "sgd", |
| 56 | + }, |
| 57 | + "learning_rate": { |
| 58 | + "type": "constant" |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + self.params = mock_task.mock_experiment() |
| 65 | + self.params.override(self._test_config, is_strict=False) |
| 66 | + self.run_experiment_kwargs = dict( |
| 67 | + params=self.params, |
| 68 | + task=task_factory.get_task(self.params.task), |
| 69 | + mode="train_and_eval", |
| 70 | + ) |
| 71 | + self.docker_config = tfc.DockerConfig( |
| 72 | + parent_image=_PARENT_IMAGE, |
| 73 | + image_build_bucket=_TEST_BUCKET |
| 74 | + ) |
| 75 | + |
| 76 | + def tpu_strategy(self): |
| 77 | + run_kwargs = dict( |
| 78 | + chief_config=tfc.COMMON_MACHINE_CONFIGS["CPU"], |
| 79 | + worker_count=1, |
| 80 | + worker_config=tfc.COMMON_MACHINE_CONFIGS["TPU"], |
| 81 | + requirements_txt=self.requirements_txt, |
| 82 | + job_labels={ |
| 83 | + "job": "tpu_strategy", |
| 84 | + "team": "run_experiment_cloud_tests", |
| 85 | + }, |
| 86 | + docker_config=self.docker_config, |
| 87 | + ) |
| 88 | + run_experiment_kwargs = dict( |
| 89 | + model_dir=os.path.join(_BASE_PATH, "tpu", "saved_model"), |
| 90 | + **self.run_experiment_kwargs, |
| 91 | + ) |
| 92 | + return models.run_experiment_cloud(run_experiment_kwargs, |
| 93 | + run_kwargs) |
| 94 | + |
| 95 | + def multi_mirror_strategy(self): |
| 96 | + run_kwargs = dict( |
| 97 | + chief_config=tfc.COMMON_MACHINE_CONFIGS["P100_1X"], |
| 98 | + worker_count=1, |
| 99 | + worker_config=tfc.COMMON_MACHINE_CONFIGS["P100_1X"], |
| 100 | + requirements_txt=self.requirements_txt, |
| 101 | + job_labels={ |
| 102 | + "job": "multi_mirror_strategy", |
| 103 | + "team": "run_experiment_cloud_tests", |
| 104 | + }, |
| 105 | + docker_config=self.docker_config, |
| 106 | + ) |
| 107 | + run_experiment_kwargs = dict( |
| 108 | + model_dir=os.path.join(_BASE_PATH, "multi_mirror", "saved_model"), |
| 109 | + **self.run_experiment_kwargs, |
| 110 | + ) |
| 111 | + return models.run_experiment_cloud(run_experiment_kwargs, |
| 112 | + run_kwargs) |
| 113 | + |
| 114 | + def mirror_strategy(self): |
| 115 | + run_kwargs = dict( |
| 116 | + chief_config=tfc.COMMON_MACHINE_CONFIGS["P100_4X"], |
| 117 | + requirements_txt=self.requirements_txt, |
| 118 | + job_labels={ |
| 119 | + "job": "mirror", |
| 120 | + "team": "run_experiment_cloud_tests", |
| 121 | + }, |
| 122 | + docker_config=self.docker_config, |
| 123 | + ) |
| 124 | + run_experiment_kwargs = dict( |
| 125 | + model_dir=os.path.join(_BASE_PATH, "mirror", "saved_model"), |
| 126 | + **self.run_experiment_kwargs, |
| 127 | + ) |
| 128 | + return models.run_experiment_cloud(run_experiment_kwargs, |
| 129 | + run_kwargs) |
| 130 | + |
| 131 | + def one_device_strategy(self): |
| 132 | + run_kwargs = dict( |
| 133 | + requirements_txt=self.requirements_txt, |
| 134 | + job_labels={ |
| 135 | + "job": "one_device", |
| 136 | + "team": "run_experiment_cloud_tests", |
| 137 | + }, |
| 138 | + docker_config=self.docker_config, |
| 139 | + ) |
| 140 | + run_experiment_kwargs = dict( |
| 141 | + model_dir=os.path.join(_BASE_PATH, "one_device", "saved_model"), |
| 142 | + **self.run_experiment_kwargs, |
| 143 | + ) |
| 144 | + # Using the default T4 GPU for this test. |
| 145 | + return models.run_experiment_cloud(run_experiment_kwargs, |
| 146 | + run_kwargs) |
| 147 | + |
| 148 | + def test_run_experiment_cloud(self): |
| 149 | + track_status = { |
| 150 | + "one_device_strategy": self.one_device_strategy(), |
| 151 | + "mirror_strategy": self.mirror_strategy(), |
| 152 | + # TODO(b/148619319) Enable when bug is solved |
| 153 | + # "multi_mirror_strategy": self.multi_mirror_strategy(), |
| 154 | + # TODO(b/194857231) Enable when bug is solved |
| 155 | + # "tpu_strategy": self.tpu_strategy(), |
| 156 | + } |
| 157 | + |
| 158 | + for test_name, ret_val in track_status.items(): |
| 159 | + self.assertTrue( |
| 160 | + google_api_client.wait_for_aip_training_job_completion( |
| 161 | + ret_val["job_id"], _PROJECT_ID), |
| 162 | + "Job {} generated from the test: {} has failed".format( |
| 163 | + ret_val["job_id"], test_name)) |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + tf.test.main() |
0 commit comments