|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 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 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.api.gax.longrunning.OperationFuture; |
| 23 | +import com.google.cloud.aiplatform.v1beta1.DeleteOperationMetadata; |
| 24 | +import com.google.cloud.aiplatform.v1beta1.PipelineServiceClient; |
| 25 | +import com.google.cloud.aiplatform.v1beta1.PipelineServiceSettings; |
| 26 | +import com.google.cloud.testing.junit4.MultipleAttemptsRule; |
| 27 | +import com.google.protobuf.Empty; |
| 28 | +import io.grpc.StatusRuntimeException; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.PrintStream; |
| 32 | +import java.util.UUID; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.concurrent.TimeoutException; |
| 36 | +import org.junit.After; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.Rule; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.junit.runners.JUnit4; |
| 43 | + |
| 44 | +@RunWith(JUnit4.class) |
| 45 | +public class CreatePipelineJobModelTuningSampleTest { |
| 46 | + @Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); |
| 47 | + |
| 48 | + private static final String PROJECT = System.getenv("UCAIP_PROJECT_ID"); |
| 49 | + private static final String LOCATION = "europe-west4"; |
| 50 | + private static final String OUTPUT_DIR = |
| 51 | + "gs://ucaip-samples-europe-west4/training_pipeline_output"; |
| 52 | + private static final String DATASET_URI = |
| 53 | + "gs://cloud-samples-data/ai-platform/generative_ai/headline_classification.jsonl"; |
| 54 | + private static final int TRAINING_STEPS = 100; |
| 55 | + private String pipelineJobName; |
| 56 | + private ByteArrayOutputStream bout; |
| 57 | + private PrintStream originalPrintStream; |
| 58 | + |
| 59 | + private static void requireEnvVar(String varName) { |
| 60 | + String errorMessage = |
| 61 | + String.format("Environment variable '%s' is required to perform these tests.", varName); |
| 62 | + assertNotNull(errorMessage, System.getenv(varName)); |
| 63 | + } |
| 64 | + |
| 65 | + @BeforeClass |
| 66 | + public static void checkRequirements() { |
| 67 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 68 | + requireEnvVar("UCAIP_PROJECT_ID"); |
| 69 | + } |
| 70 | + |
| 71 | + @Before |
| 72 | + public void setUp() { |
| 73 | + bout = new ByteArrayOutputStream(); |
| 74 | + PrintStream out = new PrintStream(bout); |
| 75 | + originalPrintStream = System.out; |
| 76 | + System.setOut(out); |
| 77 | + } |
| 78 | + |
| 79 | + @After |
| 80 | + public void tearDown() |
| 81 | + throws IOException, InterruptedException, TimeoutException, ExecutionException { |
| 82 | + final String endpoint = String.format("%s-aiplatform.googleapis.com:443", LOCATION); |
| 83 | + PipelineServiceSettings pipelineServiceSettings = |
| 84 | + PipelineServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 85 | + |
| 86 | + try (PipelineServiceClient pipelineServiceClient = |
| 87 | + PipelineServiceClient.create(pipelineServiceSettings)) { |
| 88 | + // Cancel the PipelineJob |
| 89 | + pipelineServiceClient.cancelPipelineJob(pipelineJobName); |
| 90 | + TimeUnit.MINUTES.sleep(2); |
| 91 | + |
| 92 | + // Delete the PipelineJob |
| 93 | + int retryCount = 3; |
| 94 | + while (retryCount > 0) { |
| 95 | + retryCount--; |
| 96 | + try { |
| 97 | + OperationFuture<Empty, DeleteOperationMetadata> operationFuture = |
| 98 | + pipelineServiceClient.deletePipelineJobAsync(pipelineJobName); |
| 99 | + operationFuture.get(300, TimeUnit.SECONDS); |
| 100 | + |
| 101 | + // if delete operation is successful, break out of the loop and continue |
| 102 | + break; |
| 103 | + } catch (StatusRuntimeException e) { |
| 104 | + // wait for another 1 minute, then retry |
| 105 | + System.out.println("Retrying (due to unfinished cancellation operation)..."); |
| 106 | + TimeUnit.MINUTES.sleep(1); |
| 107 | + } catch (Exception otherExceptions) { |
| 108 | + // other exception, let them throw |
| 109 | + throw otherExceptions; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + System.out.flush(); |
| 115 | + System.setOut(originalPrintStream); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void createTrainingPipelineModelTuningSample() throws IOException { |
| 120 | + final String pipelineJobDisplayName = |
| 121 | + String.format( |
| 122 | + "temp_create_pipeline_job_test_%s", |
| 123 | + UUID.randomUUID().toString().replaceAll("-", "_").substring(0, 26)); |
| 124 | + |
| 125 | + final String modelDisplayName = |
| 126 | + String.format( |
| 127 | + "temp_create_pipeline_job_model_test_%s", |
| 128 | + UUID.randomUUID().toString().replaceAll("-", "_").substring(0, 26)); |
| 129 | + |
| 130 | + // Act |
| 131 | + CreatePipelineJobModelTuningSample.createPipelineJobModelTuningSample( |
| 132 | + PROJECT, |
| 133 | + LOCATION, |
| 134 | + pipelineJobDisplayName, |
| 135 | + modelDisplayName, |
| 136 | + OUTPUT_DIR, |
| 137 | + DATASET_URI, |
| 138 | + TRAINING_STEPS); |
| 139 | + |
| 140 | + // Assert |
| 141 | + String got = bout.toString(); |
| 142 | + assertThat(got).contains(pipelineJobDisplayName); |
| 143 | + pipelineJobName = got.split("Name: ")[1].split("\n")[0]; |
| 144 | + } |
| 145 | +} |
0 commit comments