|
| 1 | +/* |
| 2 | + * Copyright 2024 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 java.util.stream.Collectors.toList; |
| 21 | +import static junit.framework.TestCase.assertNotNull; |
| 22 | + |
| 23 | +import com.google.api.gax.longrunning.OperationFuture; |
| 24 | +import com.google.cloud.aiplatform.v1.CancelPipelineJobRequest; |
| 25 | +import com.google.cloud.aiplatform.v1.DeleteOperationMetadata; |
| 26 | +import com.google.cloud.aiplatform.v1.PipelineJob; |
| 27 | +import com.google.cloud.aiplatform.v1.PipelineServiceClient; |
| 28 | +import com.google.cloud.aiplatform.v1.PipelineServiceSettings; |
| 29 | +import com.google.cloud.aiplatform.v1.PipelineState; |
| 30 | +import com.google.cloud.testing.junit4.MultipleAttemptsRule; |
| 31 | +import com.google.protobuf.Empty; |
| 32 | +import io.github.resilience4j.retry.Retry; |
| 33 | +import io.github.resilience4j.retry.RetryConfig; |
| 34 | +import io.github.resilience4j.retry.RetryRegistry; |
| 35 | +import io.vavr.CheckedRunnable; |
| 36 | +import java.io.IOException; |
| 37 | +import java.time.Duration; |
| 38 | +import java.util.LinkedList; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Queue; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import java.util.concurrent.TimeoutException; |
| 43 | +import org.junit.AfterClass; |
| 44 | +import org.junit.BeforeClass; |
| 45 | +import org.junit.Rule; |
| 46 | +import org.junit.Test; |
| 47 | +import org.junit.runner.RunWith; |
| 48 | +import org.junit.runners.JUnit4; |
| 49 | + |
| 50 | +@RunWith(JUnit4.class) |
| 51 | +public class EmbeddingModelTuningSampleTest { |
| 52 | + @Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); |
| 53 | + |
| 54 | + private static final String API_ENDPOINT = "us-central1-aiplatform.googleapis.com:443"; |
| 55 | + private static final String PROJECT = System.getenv("UCAIP_PROJECT_ID"); |
| 56 | + private static final String BASE_MODEL_VERSION_ID = "textembedding-gecko@003"; |
| 57 | + private static final String TASK_TYPE = "DEFAULT"; |
| 58 | + private static final String JOB_DISPLAY_NAME = "embedding-customization-pipeline-sample"; |
| 59 | + private static final String QUERIES = |
| 60 | + "gs://embedding-customization-pipeline/dataset/queries.jsonl"; |
| 61 | + private static final String CORPUS = "gs://embedding-customization-pipeline/dataset/corpus.jsonl"; |
| 62 | + private static final String TRAIN_LABEL = |
| 63 | + "gs://embedding-customization-pipeline/dataset/train.tsv"; |
| 64 | + private static final String TEST_LABEL = "gs://embedding-customization-pipeline/dataset/test.tsv"; |
| 65 | + private static final String OUTPUT_DIR = |
| 66 | + "gs://ucaip-samples-us-central1/training_pipeline_output"; |
| 67 | + private static final int BATCH_SIZE = 50; |
| 68 | + private static final int ITERATIONS = 300; |
| 69 | + |
| 70 | + private static Queue<String> JobNames = new LinkedList<String>(); |
| 71 | + private static final RetryConfig RETRY_CONFIG = |
| 72 | + RetryConfig.custom() |
| 73 | + .maxAttempts(30) |
| 74 | + .waitDuration(Duration.ofSeconds(6)) |
| 75 | + .retryExceptions(TimeoutException.class) |
| 76 | + .failAfterMaxAttempts(false) |
| 77 | + .build(); |
| 78 | + private static final RetryRegistry RETRY_REGISTRY = RetryRegistry.of(RETRY_CONFIG); |
| 79 | + |
| 80 | + private static void requireEnvVar(String varName) { |
| 81 | + String errorMessage = String.format("Test requires environment variable '%s'.", varName); |
| 82 | + assertNotNull(errorMessage, System.getenv(varName)); |
| 83 | + } |
| 84 | + |
| 85 | + @BeforeClass |
| 86 | + public static void checkRequirements() { |
| 87 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 88 | + requireEnvVar("UCAIP_PROJECT_ID"); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterClass |
| 92 | + public static void tearDown() throws Throwable { |
| 93 | + PipelineServiceSettings settings = |
| 94 | + PipelineServiceSettings.newBuilder().setEndpoint(API_ENDPOINT).build(); |
| 95 | + try (PipelineServiceClient client = PipelineServiceClient.create(settings)) { |
| 96 | + List<CancelPipelineJobRequest> requests = |
| 97 | + JobNames.stream() |
| 98 | + .map(n -> CancelPipelineJobRequest.newBuilder().setName(n).build()) |
| 99 | + .collect(toList()); |
| 100 | + CheckedRunnable runnable = |
| 101 | + Retry.decorateCheckedRunnable( |
| 102 | + RETRY_REGISTRY.retry("delete-pipeline-jobs", RETRY_CONFIG), |
| 103 | + () -> { |
| 104 | + List<OperationFuture<Empty, DeleteOperationMetadata>> deletions = |
| 105 | + requests.stream() |
| 106 | + .map( |
| 107 | + req -> { |
| 108 | + client.cancelPipelineJobCallable().futureCall(req); |
| 109 | + return client.deletePipelineJobAsync(req.getName()); |
| 110 | + }) |
| 111 | + .collect(toList()); |
| 112 | + for (OperationFuture<Empty, DeleteOperationMetadata> d : deletions) { |
| 113 | + d.get(0, TimeUnit.SECONDS); |
| 114 | + } |
| 115 | + }); |
| 116 | + try { |
| 117 | + runnable.run(); |
| 118 | + } catch (TimeoutException e) { |
| 119 | + // Do nothing. |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void createPipelineJobEmbeddingModelTuningSample() throws IOException { |
| 126 | + PipelineJob job = |
| 127 | + EmbeddingModelTuningSample.createEmbeddingModelTuningPipelineJob( |
| 128 | + API_ENDPOINT, |
| 129 | + PROJECT, |
| 130 | + BASE_MODEL_VERSION_ID, |
| 131 | + TASK_TYPE, |
| 132 | + JOB_DISPLAY_NAME, |
| 133 | + OUTPUT_DIR, |
| 134 | + QUERIES, |
| 135 | + CORPUS, |
| 136 | + TRAIN_LABEL, |
| 137 | + TEST_LABEL, |
| 138 | + BATCH_SIZE, |
| 139 | + ITERATIONS); |
| 140 | + assertThat(job.getState()).isNotEqualTo(PipelineState.PIPELINE_STATE_FAILED); |
| 141 | + JobNames.add(job.getName()); |
| 142 | + } |
| 143 | +} |
0 commit comments