From eb0de41ebc6f92aa37c13c9012c0322e4d021d3d Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Fri, 20 Sep 2024 14:13:42 -0700 Subject: [PATCH 1/3] feat: use mock w/ interface for Vertex samples --- generative_ai/gemma2_predict_gpu.py | 27 +++++++++++ generative_ai/gemma2_predict_gpu_test.py | 59 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 generative_ai/gemma2_predict_gpu.py create mode 100644 generative_ai/gemma2_predict_gpu_test.py diff --git a/generative_ai/gemma2_predict_gpu.py b/generative_ai/gemma2_predict_gpu.py new file mode 100644 index 00000000000..3979c8bffdf --- /dev/null +++ b/generative_ai/gemma2_predict_gpu.py @@ -0,0 +1,27 @@ +# Copyright 2024 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. + +from google.cloud import aiplatform + + +def endpoint_predict_sample( + project: str, location: str, instances: list, endpoint: str +): + aiplatform.init(project=project, location=location) + + endpoint = aiplatform.Endpoint(endpoint) + + prediction = endpoint.predict(instances=instances) + print(prediction) + return prediction diff --git a/generative_ai/gemma2_predict_gpu_test.py b/generative_ai/gemma2_predict_gpu_test.py new file mode 100644 index 00000000000..d335d5bd33d --- /dev/null +++ b/generative_ai/gemma2_predict_gpu_test.py @@ -0,0 +1,59 @@ +# Copyright 2024 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. + +import pytest +from unittest import mock +from google.cloud import aiplatform +from google.cloud.aiplatform import models + +# Interfaces defined here: +# https://github.com/googleapis/googleapis/tree/master/google/cloud/aiplatform/v1 +# https://github.com/googleapis/googleapis-gen/tree/master/google/cloud/aiplatform/v1/aiplatform-v1-py + +from gemma2_predict_gpu import endpoint_predict_sample + + +@pytest.fixture +def mock_init(): + return mock.Mock(spec=aiplatform.init) + + +@pytest.fixture +def mock_prediction(): + return mock.Mock(spec=models.Prediction) + + +@pytest.fixture +def mock_endpoint(): + return mock.Mock(spec=aiplatform.Endpoint) + + +@mock.patch("google.cloud.aiplatform.Endpoint") +@mock.patch("google.cloud.aiplatform.models.Prediction") +@mock.patch("google.cloud.aiplatform.init") +def test_gemma2_predict_gpu(mock_init, mock_prediction, mock_endpoint): + + mock_endpoint.return_value.predict.return_value = mock_prediction + + project_id = "fake-proj" + location = "fake-place" + instance = "Why is the sky blue?" + endpoint = "fake_endpoint_id" + + prediction = endpoint_predict_sample( + project=project_id, location=location, instances=[instance], + endpoint=endpoint + ) + + assert prediction From a8a4c38bd75ed62064770faf5685856acfe5cdb3 Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Fri, 20 Sep 2024 14:16:21 -0700 Subject: [PATCH 2/3] lint --- generative_ai/gemma2_predict_gpu_test.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/generative_ai/gemma2_predict_gpu_test.py b/generative_ai/gemma2_predict_gpu_test.py index d335d5bd33d..94ab92b60eb 100644 --- a/generative_ai/gemma2_predict_gpu_test.py +++ b/generative_ai/gemma2_predict_gpu_test.py @@ -18,8 +18,7 @@ from google.cloud.aiplatform import models # Interfaces defined here: -# https://github.com/googleapis/googleapis/tree/master/google/cloud/aiplatform/v1 -# https://github.com/googleapis/googleapis-gen/tree/master/google/cloud/aiplatform/v1/aiplatform-v1-py +# https://github.com/googleapis/python-aiplatform/blob/main/google/cloud/aiplatform/models.py from gemma2_predict_gpu import endpoint_predict_sample @@ -52,8 +51,7 @@ def test_gemma2_predict_gpu(mock_init, mock_prediction, mock_endpoint): endpoint = "fake_endpoint_id" prediction = endpoint_predict_sample( - project=project_id, location=location, instances=[instance], - endpoint=endpoint + project=project_id, location=location, instances=[instance], endpoint=endpoint ) assert prediction From 3d916bcad75ea617072f50fa09f7d7b31526c641 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 20 Sep 2024 21:20:28 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- generative_ai/gemma2_predict_gpu_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/generative_ai/gemma2_predict_gpu_test.py b/generative_ai/gemma2_predict_gpu_test.py index 94ab92b60eb..08125f315e8 100644 --- a/generative_ai/gemma2_predict_gpu_test.py +++ b/generative_ai/gemma2_predict_gpu_test.py @@ -42,7 +42,6 @@ def mock_endpoint(): @mock.patch("google.cloud.aiplatform.models.Prediction") @mock.patch("google.cloud.aiplatform.init") def test_gemma2_predict_gpu(mock_init, mock_prediction, mock_endpoint): - mock_endpoint.return_value.predict.return_value = mock_prediction project_id = "fake-proj"