Skip to content

Commit a66edcb

Browse files
VVildVVolfpotiuk
andauthored
CreateBatchPredictionJobOperator Add batch_size param for Vertex AI BatchPredictionJob objects (#31118)
* Add batch_size param for BatchPredictionJob objects Co-authored-by: Jarek Potiuk <jarek@potiuk.com> --------- Co-authored-by: Jarek Potiuk <jarek@potiuk.com>
1 parent 779af82 commit a66edcb

6 files changed

Lines changed: 36 additions & 3 deletions

File tree

airflow/providers/google/cloud/hooks/vertex_ai/batch_prediction_job.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def create_batch_prediction_job(
114114
labels: dict[str, str] | None = None,
115115
encryption_spec_key_name: str | None = None,
116116
sync: bool = True,
117+
create_request_timeout: float | None = None,
118+
batch_size: int | None = None,
117119
) -> BatchPredictionJob:
118120
"""
119121
Create a batch prediction job.
@@ -207,6 +209,14 @@ def create_batch_prediction_job(
207209
:param sync: Whether to execute this method synchronously. If False, this method will be executed in
208210
concurrent Future and any downstream object will be immediately returned and synced when the
209211
Future has completed.
212+
:param create_request_timeout: Optional. The timeout for the create request in seconds.
213+
:param batch_size: Optional. The number of the records (e.g. instances)
214+
of the operation given in each batch
215+
to a machine replica. Machine type, and size of a single record should be considered
216+
when setting this parameter, higher value speeds up the batch operation's execution,
217+
but too high value will result in a whole batch not fitting in a machine's memory,
218+
and the whole operation will fail.
219+
The default value is same as in the aiplatform's BatchPredictionJob.
210220
"""
211221
self._batch_prediction_job = BatchPredictionJob.create(
212222
job_display_name=job_display_name,
@@ -232,6 +242,8 @@ def create_batch_prediction_job(
232242
credentials=self.get_credentials(),
233243
encryption_spec_key_name=encryption_spec_key_name,
234244
sync=sync,
245+
create_request_timeout=create_request_timeout,
246+
batch_size=batch_size,
235247
)
236248
return self._batch_prediction_job
237249

airflow/providers/google/cloud/operators/vertex_ai/batch_prediction_job.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ class CreateBatchPredictionJobOperator(GoogleCloudBaseOperator):
139139
:param sync: Whether to execute this method synchronously. If False, this method will be executed in
140140
concurrent Future and any downstream object will be immediately returned and synced when the
141141
Future has completed.
142+
:param create_request_timeout: Optional. The timeout for the create request in seconds.
143+
:param batch_size: Optional. The number of the records (e.g. instances)
144+
of the operation given in each batch
145+
to a machine replica. Machine type, and size of a single record should be considered
146+
when setting this parameter, higher value speeds up the batch operation's execution,
147+
but too high value will result in a whole batch not fitting in a machine's memory,
148+
and the whole operation will fail.
149+
The default value is same as in the aiplatform's BatchPredictionJob.
142150
:param retry: Designation of what errors, if any, should be retried.
143151
:param timeout: The timeout for this request.
144152
:param metadata: Strings which should be sent along with the request as metadata.
@@ -181,6 +189,8 @@ def __init__(
181189
labels: dict[str, str] | None = None,
182190
encryption_spec_key_name: str | None = None,
183191
sync: bool = True,
192+
create_request_timeout: float | None = None,
193+
batch_size: int | None = None,
184194
gcp_conn_id: str = "google_cloud_default",
185195
impersonation_chain: str | Sequence[str] | None = None,
186196
**kwargs,
@@ -208,6 +218,8 @@ def __init__(
208218
self.labels = labels
209219
self.encryption_spec_key_name = encryption_spec_key_name
210220
self.sync = sync
221+
self.create_request_timeout = create_request_timeout
222+
self.batch_size = batch_size
211223
self.gcp_conn_id = gcp_conn_id
212224
self.impersonation_chain = impersonation_chain
213225
self.hook: BatchPredictionJobHook | None = None
@@ -241,6 +253,8 @@ def execute(self, context: Context):
241253
labels=self.labels,
242254
encryption_spec_key_name=self.encryption_spec_key_name,
243255
sync=self.sync,
256+
create_request_timeout=self.create_request_timeout,
257+
batch_size=self.batch_size,
244258
)
245259

246260
batch_prediction_job = result.to_dict()

airflow/providers/google/provider.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ dependencies:
9595
- google-api-python-client>=1.6.0,<2.0.0
9696
- google-auth>=1.0.0
9797
- google-auth-httplib2>=0.0.1
98-
- google-cloud-aiplatform>=1.7.1,<2.0.0
98+
- google-cloud-aiplatform>=1.13.1,<2.0.0
9999
- google-cloud-automl>=2.1.0
100100
- google-cloud-bigquery-datatransfer>=3.0.0
101101
- google-cloud-bigtable>=2.0.0,<3.0.0

docs/apache-airflow-providers-google/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ PIP package Version required
115115
``google-api-python-client`` ``>=1.6.0,<2.0.0``
116116
``google-auth`` ``>=1.0.0``
117117
``google-auth-httplib2`` ``>=0.0.1``
118-
``google-cloud-aiplatform`` ``>=1.7.1,<2.0.0``
118+
``google-cloud-aiplatform`` ``>=1.13.1,<2.0.0``
119119
``google-cloud-automl`` ``>=2.1.0``
120120
``google-cloud-bigquery-datatransfer`` ``>=3.0.0``
121121
``google-cloud-bigtable`` ``>=2.0.0,<3.0.0``

generated/provider_dependencies.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
"google-auth-httplib2>=0.0.1",
347347
"google-auth-oauthlib<1.0.0,>=0.3.0",
348348
"google-auth>=1.0.0",
349-
"google-cloud-aiplatform>=1.7.1,<2.0.0",
349+
"google-cloud-aiplatform>=1.13.1,<2.0.0",
350350
"google-cloud-automl>=2.1.0",
351351
"google-cloud-bigquery-datatransfer>=3.0.0",
352352
"google-cloud-bigtable>=2.0.0,<3.0.0",

tests/providers/google/cloud/operators/test_vertex_ai.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@
165165
"export_format_id": "tf-saved-model",
166166
}
167167

168+
TEST_CREATE_REQUEST_TIMEOUT = 100.5
169+
TEST_BATCH_SIZE = 4000
170+
168171

169172
class TestVertexAICreateCustomContainerTrainingJobOperator:
170173
@mock.patch(VERTEX_AI_PATH.format("custom_job.CustomJobHook"))
@@ -989,6 +992,8 @@ def test_execute(self, mock_hook, to_dict_mock):
989992
model_name=TEST_MODEL_NAME,
990993
instances_format="jsonl",
991994
predictions_format="jsonl",
995+
create_request_timeout=TEST_CREATE_REQUEST_TIMEOUT,
996+
batch_size=TEST_BATCH_SIZE,
992997
)
993998
op.execute(context={"ti": mock.MagicMock()})
994999
mock_hook.assert_called_once_with(gcp_conn_id=GCP_CONN_ID, impersonation_chain=IMPERSONATION_CHAIN)
@@ -1015,6 +1020,8 @@ def test_execute(self, mock_hook, to_dict_mock):
10151020
labels=None,
10161021
encryption_spec_key_name=None,
10171022
sync=True,
1023+
create_request_timeout=TEST_CREATE_REQUEST_TIMEOUT,
1024+
batch_size=TEST_BATCH_SIZE,
10181025
)
10191026

10201027

0 commit comments

Comments
 (0)