From 5155dee5edd86fb700a91dfca01bddd4d6393410 Mon Sep 17 00:00:00 2001 From: Morgan Du Date: Wed, 18 Nov 2020 10:59:22 -0800 Subject: [PATCH] feat: add create_hyperparameter_tuning_job_python_package sample (#76) --- ...ameter_tuning_job_python_package_sample.py | 101 ++++++++++++++++++ ...r_tuning_job_python_package_sample_test.py | 85 +++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 samples/snippets/create_hyperparameter_tuning_job_python_package_sample.py create mode 100644 samples/snippets/create_hyperparameter_tuning_job_python_package_sample_test.py diff --git a/samples/snippets/create_hyperparameter_tuning_job_python_package_sample.py b/samples/snippets/create_hyperparameter_tuning_job_python_package_sample.py new file mode 100644 index 0000000000..8ffe7cd9b8 --- /dev/null +++ b/samples/snippets/create_hyperparameter_tuning_job_python_package_sample.py @@ -0,0 +1,101 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START aiplatform_create_hyperparameter_tuning_job_python_package_sample] +from google.cloud import aiplatform + + +def create_hyperparameter_tuning_job_python_package_sample( + project: str, + display_name: str, + executor_image_uri: str, + package_uri: str, + python_module: str, + location: str = "us-central1", + api_endpoint: str = "us-central1-aiplatform.googleapis.com", +): + client_options = {"api_endpoint": api_endpoint} + # Initialize client that will be used to create and send requests. + # This client only needs to be created once, and can be reused for multiple requests. + client = aiplatform.gapic.JobServiceClient(client_options=client_options) + + # study_spec + metric = { + "metric_id": "val_rmse", + "goal": aiplatform.gapic.StudySpec.MetricSpec.GoalType.MINIMIZE, + } + + conditional_parameter_decay = { + "parameter_spec": { + "parameter_id": "decay", + "double_value_spec": {"min_value": 1e-07, "max_value": 1}, + "scale_type": aiplatform.gapic.StudySpec.ParameterSpec.ScaleType.UNIT_LINEAR_SCALE, + }, + "parent_discrete_values": {"values": [32, 64]}, + } + conditional_parameter_learning_rate = { + "parameter_spec": { + "parameter_id": "learning_rate", + "double_value_spec": {"min_value": 1e-07, "max_value": 1}, + "scale_type": aiplatform.gapic.StudySpec.ParameterSpec.ScaleType.UNIT_LINEAR_SCALE, + }, + "parent_discrete_values": {"values": [4, 8, 16]}, + } + parameter = { + "parameter_id": "batch_size", + "discrete_value_spec": {"values": [4, 8, 16, 32, 64, 128]}, + "scale_type": aiplatform.gapic.StudySpec.ParameterSpec.ScaleType.UNIT_LINEAR_SCALE, + "conditional_parameter_specs": [ + conditional_parameter_decay, + conditional_parameter_learning_rate, + ], + } + + # trial_job_spec + machine_spec = { + "machine_type": "n1-standard-4", + "accelerator_type": aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, + "accelerator_count": 1, + } + worker_pool_spec = { + "machine_spec": machine_spec, + "replica_count": 1, + "python_package_spec": { + "executor_image_uri": executor_image_uri, + "package_uris": [package_uri], + "python_module": python_module, + "args": [], + }, + } + + # hyperparameter_tuning_job + hyperparameter_tuning_job = { + "display_name": display_name, + "max_trial_count": 4, + "parallel_trial_count": 2, + "study_spec": { + "metrics": [metric], + "parameters": [parameter], + "algorithm": aiplatform.gapic.StudySpec.Algorithm.RANDOM_SEARCH, + }, + "trial_job_spec": {"worker_pool_specs": [worker_pool_spec]}, + } + parent = f"projects/{project}/locations/{location}" + response = client.create_hyperparameter_tuning_job( + parent=parent, hyperparameter_tuning_job=hyperparameter_tuning_job + ) + print("response:", response) + + +# [END aiplatform_create_hyperparameter_tuning_job_python_package_sample] diff --git a/samples/snippets/create_hyperparameter_tuning_job_python_package_sample_test.py b/samples/snippets/create_hyperparameter_tuning_job_python_package_sample_test.py new file mode 100644 index 0000000000..4a328f5170 --- /dev/null +++ b/samples/snippets/create_hyperparameter_tuning_job_python_package_sample_test.py @@ -0,0 +1,85 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import uuid +import pytest +import os + +import helpers + +import create_hyperparameter_tuning_job_python_package_sample + +from google.cloud import aiplatform + +PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") + +API_ENDPOINT = "us-central1-aiplatform.googleapis.com" + +DISPLAY_NAME = ( + f"temp_create_hyperparameter_tuning_job_python_package_test_{uuid.uuid4()}" +) + +EXECUTOR_IMAGE_URI = "us.gcr.io/cloud-aiplatform/training/tf-gpu.2-1:latest" +PACKAGE_URI = "gs://ucaip-test-us-central1/training/pythonpackages/trainer.tar.bz2" +PYTHON_MODULE = "trainer.hptuning_trainer" + +@pytest.fixture +def shared_state(): + state = {} + yield state + + +@pytest.fixture +def job_client(): + client_options = {"api_endpoint": API_ENDPOINT} + job_client = aiplatform.gapic.JobServiceClient( + client_options=client_options) + yield job_client + + +@pytest.fixture(scope="function", autouse=True) +def teardown(shared_state, job_client): + yield + + # Cancel the created hyperparameter tuning job + job_client.cancel_hyperparameter_tuning_job( + name=shared_state["hyperparameter_tuning_job_name"] + ) + + # Waiting for hyperparameter tuning job to be in CANCELLED state + helpers.wait_for_job_state( + get_job_method=job_client.get_hyperparameter_tuning_job, + name=shared_state["hyperparameter_tuning_job_name"], + ) + + # Delete the created hyperparameter tuning job + job_client.delete_hyperparameter_tuning_job( + name=shared_state["hyperparameter_tuning_job_name"] + ) + + +def test_create_hyperparameter_tuning_job_python_package_sample(capsys, shared_state): + + create_hyperparameter_tuning_job_python_package_sample.create_hyperparameter_tuning_job_python_package_sample( + project=PROJECT_ID, + display_name=DISPLAY_NAME, + executor_image_uri=EXECUTOR_IMAGE_URI, + package_uri=PACKAGE_URI, + python_module=PYTHON_MODULE, + ) + + out, _ = capsys.readouterr() + assert "response" in out + + shared_state["hyperparameter_tuning_job_name"] = helpers.get_name(out)