Skip to content

Commit 0013e4e

Browse files
authored
chore(dataflow/gemma): update dependencies and format code (#14321)
* chore(dataflow/gemma): update dependencies and format code - Update tensorflow base image to 2.20.0-gpu and beam sdk to 3.11/2.74.0 - Update apache_beam, keras, keras_nlp, and protobuf dependencies - Update test dependencies including google-cloud-aiplatform, storage, and pytest - Format custom_model_gemma.py and e2e_test.py - Update ignored python versions in noxfile_config.py * Finished setting config for gemma sample. it's now working * used isort to fix import order. * import order again * more import order stuff * even more linter stuff. * linter * linting * more linting, and refactore message on testing locally. * more linting * adding requested changes * missing one line * fixed typo
1 parent a5dbe88 commit 0013e4e

6 files changed

Lines changed: 59 additions & 50 deletions

File tree

dataflow/gemma/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# This uses Ubuntu with Python 3.11
1616
# You can check the Python version for a given tensorflow
1717
# container at https://hub.docker.com/r/tensorflow/tensorflow/tags
18-
ARG SERVING_BUILD_IMAGE=tensorflow/tensorflow:2.16.1-gpu
18+
ARG SERVING_BUILD_IMAGE=tensorflow/tensorflow:2.20.0-gpu
1919

2020
FROM ${SERVING_BUILD_IMAGE}
2121

@@ -29,7 +29,7 @@ RUN pip install --upgrade --no-cache-dir pip \
2929
&& pip install --no-cache-dir -r requirements.txt
3030

3131
# Copy files from official SDK image, including script/dependencies.
32-
COPY --from=apache/beam_python3.14_sdk:2.73.0 /opt/apache/beam /opt/apache/beam
32+
COPY --from=apache/beam_python3.11_sdk:2.74.0 /opt/apache/beam /opt/apache/beam
3333

3434
# Copy the model directory downloaded from Kaggle and the pipeline code.
3535
COPY gemma_2b gemma_2B

dataflow/gemma/custom_model_gemma.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@
1313
# limitations under the License.
1414

1515
from collections.abc import Iterable, Sequence
16-
17-
import logging
18-
1916
from typing import Any
2017
from typing import Optional
2118

2219
import apache_beam as beam
20+
2321
from apache_beam.ml.inference import utils
2422
from apache_beam.ml.inference.base import ModelHandler
2523
from apache_beam.ml.inference.base import PredictionResult
2624
from apache_beam.ml.inference.base import RunInference
2725
from apache_beam.options.pipeline_options import PipelineOptions
2826

2927
import keras_nlp
28+
3029
from keras_nlp.src.models.gemma.gemma_causal_lm import GemmaCausalLM
3130

31+
import logging
32+
3233

3334
class GemmaModelHandler(ModelHandler[str, PredictionResult, GemmaCausalLM]):
3435
def __init__(
3536
self,
3637
model_name: str = "gemma_2B",
3738
):
38-
""" Implementation of the ModelHandler interface for Gemma using text as input.
39+
"""Implementation of the ModelHandler interface for Gemma using text as input.
3940
4041
Example Usage::
4142
@@ -48,7 +49,7 @@ def __init__(
4849
self._env_vars = {}
4950

5051
def share_model_across_processes(self) -> bool:
51-
""" Indicates if the model should be loaded once-per-VM rather than
52+
"""Indicates if the model should be loaded once-per-VM rather than
5253
once-per-worker-process on a VM. Because Gemma is a large language model,
5354
this will always return True to avoid OOM errors.
5455
"""
@@ -62,7 +63,7 @@ def run_inference(
6263
self,
6364
batch: Sequence[str],
6465
model: GemmaCausalLM,
65-
inference_args: Optional[dict[str, Any]] = None
66+
inference_args: Optional[dict[str, Any]] = None,
6667
) -> Iterable[PredictionResult]:
6768
"""Runs inferences on a batch of text strings.
6869
@@ -85,7 +86,8 @@ def run_inference(
8586
class FormatOutput(beam.DoFn):
8687
def process(self, element, *args, **kwargs):
8788
yield "Input: {input}, Output: {output}".format(
88-
input=element.example, output=element.inference)
89+
input=element.example, output=element.inference
90+
)
8991

9092

9193
if __name__ == "__main__":
@@ -119,13 +121,16 @@ def process(self, element, *args, **kwargs):
119121

120122
pipeline = beam.Pipeline(options=beam_options)
121123
_ = (
122-
pipeline | "Read Topic" >>
123-
beam.io.ReadFromPubSub(subscription=args.messages_subscription)
124+
pipeline
125+
| "Read Topic"
126+
>> beam.io.ReadFromPubSub(subscription=args.messages_subscription)
124127
| "Parse" >> beam.Map(lambda x: x.decode("utf-8"))
125-
| "RunInference-Gemma" >> RunInference(
128+
| "RunInference-Gemma"
129+
>> RunInference(
126130
GemmaModelHandler(args.model_path)
127131
) # Send the prompts to the model and get responses.
128132
| "Format Output" >> beam.ParDo(FormatOutput()) # Format the output.
129-
| "Publish Result" >>
130-
beam.io.gcp.pubsub.WriteStringsToPubSub(topic=args.responses_topic))
133+
| "Publish Result"
134+
>> beam.io.gcp.pubsub.WriteStringsToPubSub(topic=args.responses_topic)
135+
)
131136
pipeline.run()

dataflow/gemma/e2e_test.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@
3434
3535
OPTION B: Run tests with nox
3636
pip install nox
37-
nox -s py-3.10
37+
nox -s py-3.11
3838
39-
NOTE: For the tests to find the conftest in the testing infrastructure,
39+
TODO(developer): For the tests to find the conftest in the testing infrastructure,
4040
add the PYTHONPATH to the "env" in your noxfile_config.py file.
4141
"""
42+
4243
from collections.abc import Callable, Iterator
4344

4445
import conftest # python-docs-samples/dataflow/conftest.py
4546
from conftest import Utils
4647

4748
import pytest
4849

49-
DATAFLOW_MACHINE_TYPE = "g2-standard-4"
50+
DATAFLOW_MACHINE_TYPE = "g2-standard-8"
51+
# TODO(developer): For local testing, ensure the 'gemma_2b_en' directory is uploaded
52+
# to a GCS bucket you manage. Update the constant below to point to
53+
# the root path of this uploaded directory (e.g., 'gs://your-bucket-name/path/to/gemma_2b').
5054
GEMMA_GCS = "gs://perm-dataflow-gemma-example-testdata/gemma_2b"
5155
NAME = "dataflow/gemma/streaming"
5256

@@ -70,8 +74,9 @@ def messages_topic(pubsub_topic: Callable[[str], str]) -> str:
7074

7175

7276
@pytest.fixture(scope="session")
73-
def messages_subscription(pubsub_subscription: Callable[[str, str], str],
74-
messages_topic: str) -> str:
77+
def messages_subscription(
78+
pubsub_subscription: Callable[[str, str], str], messages_topic: str
79+
) -> str:
7580
return pubsub_subscription("messages", messages_topic)
7681

7782

@@ -81,20 +86,21 @@ def responses_topic(pubsub_topic: Callable[[str], str]) -> str:
8186

8287

8388
@pytest.fixture(scope="session")
84-
def responses_subscription(pubsub_subscription: Callable[[str, str], str],
85-
responses_topic: str) -> str:
89+
def responses_subscription(
90+
pubsub_subscription: Callable[[str, str], str], responses_topic: str
91+
) -> str:
8692
return pubsub_subscription("responses", responses_topic)
8793

8894

8995
@pytest.fixture(scope="session")
9096
def dataflow_job(
91-
project: str,
92-
bucket_name: str,
93-
location: str,
94-
unique_name: str,
95-
container_image: str,
96-
messages_subscription: str,
97-
responses_topic: str,
97+
project: str,
98+
bucket_name: str,
99+
location: str,
100+
unique_name: str,
101+
container_image: str,
102+
messages_subscription: str,
103+
responses_topic: str,
98104
) -> Iterator[str]:
99105
# Launch the streaming Dataflow pipeline.
100106
conftest.run_cmd(
@@ -108,6 +114,7 @@ def dataflow_job(
108114
f"--temp_location=gs://{bucket_name}/temp",
109115
f"--region={location}",
110116
f"--machine_type={DATAFLOW_MACHINE_TYPE}",
117+
"--disk_size_gb=100",
111118
f"--sdk_container_image=gcr.io/{project}/{container_image}",
112119
"--dataflow_service_options=worker_accelerator=type:nvidia-l4;count:1;install-nvidia-driver:5xx",
113120
"--requirements_cache=skip",
@@ -127,20 +134,18 @@ def dataflow_job(
127134

128135
@pytest.mark.timeout(3600)
129136
def test_pipeline_dataflow(
130-
project: str,
131-
location: str,
132-
dataflow_job: str,
133-
messages_topic: str,
134-
responses_subscription: str,
137+
project: str,
138+
location: str,
139+
dataflow_job: str,
140+
messages_topic: str,
141+
responses_subscription: str,
135142
) -> None:
136143
print(f"Waiting for the Dataflow workers to start: {dataflow_job}")
137144
conftest.wait_until(
138-
lambda: conftest.dataflow_num_workers(project, location, dataflow_job)
139-
> 0,
145+
lambda: conftest.dataflow_num_workers(project, location, dataflow_job) > 0,
140146
"workers are running",
141147
)
142-
num_workers = conftest.dataflow_num_workers(project, location,
143-
dataflow_job)
148+
num_workers = conftest.dataflow_num_workers(project, location, dataflow_job)
144149
print(f"Dataflow job num_workers: {num_workers}")
145150

146151
messages = ["This is a test for a Python sample."]

dataflow/gemma/noxfile_config.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
# You can opt out from the test for specific Python versions.
1919
# The Python version used is defined by the Dockerfile and the job
2020
# submission enviornment must match.
21-
# Note: Docker-based sample, testing only against version specified in Dockerfile (3.14)
22-
"ignored_versions": ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"],
23-
"envs": {
24-
"PYTHONPATH": ".."
25-
},
21+
# Note: Docker-based sample, testing only against version specified in Dockerfile (3.11)
22+
"ignored_versions": ["3.8", "3.9", "3.10", "3.12", "3.13", "3.14"],
23+
"envs": {"PYTHONPATH": ".."},
2624
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
google-cloud-aiplatform==1.158.0
2-
google-cloud-dataflow-client==0.8.10
3-
google-cloud-storage==2.16.0
4-
pytest==9.0.3; python_version >= "3.10"
5-
pytest-timeout==2.3.1
2+
google-cloud-dataflow-client==0.14.0
3+
google-cloud-storage==3.12.0
4+
pytest==9.0.3
5+
pytest-timeout==2.4.0

dataflow/gemma/requirements.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
apache_beam[gcp]==2.54.0
2-
protobuf==4.25.0
3-
keras_nlp==0.8.2
4-
keras==3.0.5
1+
protobuf==6.33.6
2+
apache_beam[gcp]==2.74.0
3+
keras==3.14.1
4+
keras_nlp==0.29.1
5+
pyOpenSSL==25.3.0

0 commit comments

Comments
 (0)