GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: Support intermediary BQ Table for Custom Training (#166) · googleapis/python-aiplatform@4e3587e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • google/cloud/aiplatform
    • training_jobs.py
  • tests/unit/aiplatform
    • test_training_jobs.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎google/cloud/aiplatform/training_jobs.py‎

Copy file name to clipboardExpand all lines: google/cloud/aiplatform/training_jobs.py
+89-1Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def _create_input_data_config(
122122
test_fraction_split: float = 0.1,
123123
predefined_split_column_name: Optional[str] = None,
124124
gcs_destination_uri_prefix: Optional[str] = None,
125+
bigquery_destination: Optional[str] = None,
125126
) -> Optional[gca_training_pipeline.InputDataConfig]:
126127
"""Constructs a input data config to pass to the training pipeline.
127128
@@ -160,6 +161,20 @@ def _create_input_data_config(
160161
- AIP_TRAINING_DATA_URI = "gcs_destination/training-*"
161162
- AIP_VALIDATION_DATA_URI = "gcs_destination/validation-*"
162163
- AIP_TEST_DATA_URI = "gcs_destination/test-*".
164+
bigquery_destination (str):
165+
The BigQuery project location where the training data is to
166+
be written to. In the given project a new dataset is created
167+
with name
168+
``dataset_<dataset-id>_<annotation-type>_<timestamp-of-training-call>``
169+
where timestamp is in YYYY_MM_DDThh_mm_ss_sssZ format. All
170+
training input data will be written into that dataset. In
171+
the dataset three tables will be created, ``training``,
172+
``validation`` and ``test``.
173+
174+
- AIP_DATA_FORMAT = "bigquery".
175+
- AIP_TRAINING_DATA_URI ="bigquery_destination.dataset_*.training"
176+
- AIP_VALIDATION_DATA_URI = "bigquery_destination.dataset_*.validation"
177+
- AIP_TEST_DATA_URI = "bigquery_destination.dataset_*.test"
163178
"""
164179

165180
input_data_config = None
@@ -193,12 +208,20 @@ def _create_input_data_config(
193208
output_uri_prefix=gcs_destination_uri_prefix
194209
)
195210

211+
# TODO(b/177416223) validate managed BQ dataset is passed in
212+
bigquery_destination_proto = None
213+
if bigquery_destination:
214+
bigquery_destination_proto = gca_io.BigQueryDestination(
215+
output_uri=bigquery_destination
216+
)
217+
196218
# create input data config
197219
input_data_config = gca_training_pipeline.InputDataConfig(
198220
fraction_split=fraction_split,
199221
predefined_split=predefined_split,
200222
dataset_id=dataset.name,
201223
gcs_destination=gcs_destination,
224+
bigquery_destination=bigquery_destination_proto,
202225
)
203226

204227
return input_data_config
@@ -214,6 +237,7 @@ def _run_job(
214237
predefined_split_column_name: Optional[str] = None,
215238
model: Optional[gca_model.Model] = None,
216239
gcs_destination_uri_prefix: Optional[str] = None,
240+
bigquery_destination: Optional[str] = None,
217241
) -> Optional[models.Model]:
218242
"""Runs the training job.
219243
@@ -295,6 +319,20 @@ def _run_job(
295319
- AIP_TRAINING_DATA_URI = "gcs_destination/training-*"
296320
- AIP_VALIDATION_DATA_URI = "gcs_destination/validation-*"
297321
- AIP_TEST_DATA_URI = "gcs_destination/test-*".
322+
bigquery_destination (str):
323+
The BigQuery project location where the training data is to
324+
be written to. In the given project a new dataset is created
325+
with name
326+
``dataset_<dataset-id>_<annotation-type>_<timestamp-of-training-call>``
327+
where timestamp is in YYYY_MM_DDThh_mm_ss_sssZ format. All
328+
training input data will be written into that dataset. In
329+
the dataset three tables will be created, ``training``,
330+
``validation`` and ``test``.
331+
332+
- AIP_DATA_FORMAT = "bigquery".
333+
- AIP_TRAINING_DATA_URI ="bigquery_destination.dataset_*.training"
334+
- AIP_VALIDATION_DATA_URI = "bigquery_destination.dataset_*.validation"
335+
- AIP_TEST_DATA_URI = "bigquery_destination.dataset_*.test"
298336
"""
299337

300338
input_data_config = self._create_input_data_config(
@@ -304,6 +342,7 @@ def _run_job(
304342
test_fraction_split=test_fraction_split,
305343
predefined_split_column_name=predefined_split_column_name,
306344
gcs_destination_uri_prefix=gcs_destination_uri_prefix,
345+
bigquery_destination=bigquery_destination,
307346
)
308347

309348
# create training pipeline
@@ -1418,6 +1457,7 @@ def run(
14181457
dataset: Optional[datasets.Dataset] = None,
14191458
model_display_name: Optional[str] = None,
14201459
base_output_dir: Optional[str] = None,
1460+
bigquery_destination: Optional[str] = None,
14211461
args: Optional[List[Union[str, float, int]]] = None,
14221462
replica_count: int = 0,
14231463
machine_type: str = "n1-standard-4",
@@ -1463,6 +1503,21 @@ def run(
14631503
base_output_dir (str):
14641504
GCS output directory of job. If not provided a
14651505
timestamped directory in the staging directory will be used.
1506+
bigquery_destination (str):
1507+
Provide this field if `dataset` is a BiqQuery dataset.
1508+
The BigQuery project location where the training data is to
1509+
be written to. In the given project a new dataset is created
1510+
with name
1511+
``dataset_<dataset-id>_<annotation-type>_<timestamp-of-training-call>``
1512+
where timestamp is in YYYY_MM_DDThh_mm_ss_sssZ format. All
1513+
training input data will be written into that dataset. In
1514+
the dataset three tables will be created, ``training``,
1515+
``validation`` and ``test``.
1516+
1517+
- AIP_DATA_FORMAT = "bigquery".
1518+
- AIP_TRAINING_DATA_URI ="bigquery_destination.dataset_*.training"
1519+
- AIP_VALIDATION_DATA_URI = "bigquery_destination.dataset_*.validation"
1520+
- AIP_TEST_DATA_URI = "bigquery_destination.dataset_*.test"
14661521
args (List[Unions[str, int, float]]):
14671522
Command line arguments to be passed to the Python script.
14681523
replica_count (int):
@@ -1815,6 +1870,7 @@ def run(
18151870
dataset: Optional[datasets.Dataset] = None,
18161871
model_display_name: Optional[str] = None,
18171872
base_output_dir: Optional[str] = None,
1873+
bigquery_destination: Optional[str] = None,
18181874
args: Optional[List[Union[str, float, int]]] = None,
18191875
replica_count: int = 0,
18201876
machine_type: str = "n1-standard-4",
@@ -1844,7 +1900,7 @@ def run(
18441900
Args:
18451901
dataset (aiplatform.Dataset):
18461902
AI Platform to fit this training against. Custom training script should
1847-
retrieve datasets through passed in environement variables uris:
1903+
retrieve datasets through passed in environment variables uris:
18481904
18491905
os.environ["AIP_TRAINING_DATA_URI"]
18501906
os.environ["AIP_VALIDATION_DATA_URI"]
@@ -1860,6 +1916,21 @@ def run(
18601916
base_output_dir (str):
18611917
GCS output directory of job. If not provided a
18621918
timestamped directory in the staging directory will be used.
1919+
bigquery_destination (str):
1920+
Provide this field if `dataset` is a BiqQuery dataset.
1921+
The BigQuery project location where the training data is to
1922+
be written to. In the given project a new dataset is created
1923+
with name
1924+
``dataset_<dataset-id>_<annotation-type>_<timestamp-of-training-call>``
1925+
where timestamp is in YYYY_MM_DDThh_mm_ss_sssZ format. All
1926+
training input data will be written into that dataset. In
1927+
the dataset three tables will be created, ``training``,
1928+
``validation`` and ``test``.
1929+
1930+
- AIP_DATA_FORMAT = "bigquery".
1931+
- AIP_TRAINING_DATA_URI ="bigquery_destination.dataset_*.training"
1932+
- AIP_VALIDATION_DATA_URI = "bigquery_destination.dataset_*.validation"
1933+
- AIP_TEST_DATA_URI = "bigquery_destination.dataset_*.test"
18631934
args (List[Unions[str, int, float]]):
18641935
Command line arguments to be passed to the Python script.
18651936
replica_count (int):
@@ -1921,6 +1992,7 @@ def run(
19211992
managed_model=managed_model,
19221993
args=args,
19231994
base_output_dir=base_output_dir,
1995+
bigquery_destination=bigquery_destination,
19241996
training_fraction_split=training_fraction_split,
19251997
validation_fraction_split=validation_fraction_split,
19261998
test_fraction_split=test_fraction_split,
@@ -1936,6 +2008,7 @@ def _run(
19362008
managed_model: Optional[gca_model.Model] = None,
19372009
args: Optional[List[Union[str, float, int]]] = None,
19382010
base_output_dir: Optional[str] = None,
2011+
bigquery_destination: Optional[str] = None,
19392012
training_fraction_split: float = 0.8,
19402013
validation_fraction_split: float = 0.1,
19412014
test_fraction_split: float = 0.1,
@@ -1955,6 +2028,20 @@ def _run(
19552028
base_output_dir (str):
19562029
GCS output directory of job. If not provided a
19572030
timestamped directory in the staging directory will be used.
2031+
bigquery_destination (str):
2032+
The BigQuery project location where the training data is to
2033+
be written to. In the given project a new dataset is created
2034+
with name
2035+
``dataset_<dataset-id>_<annotation-type>_<timestamp-of-training-call>``
2036+
where timestamp is in YYYY_MM_DDThh_mm_ss_sssZ format. All
2037+
training input data will be written into that dataset. In
2038+
the dataset three tables will be created, ``training``,
2039+
``validation`` and ``test``.
2040+
2041+
- AIP_DATA_FORMAT = "bigquery".
2042+
- AIP_TRAINING_DATA_URI ="bigquery_destination.dataset_*.training"
2043+
- AIP_VALIDATION_DATA_URI = "bigquery_destination.dataset_*.validation"
2044+
- AIP_TEST_DATA_URI = "bigquery_destination.dataset_*.test"
19582045
training_fraction_split (float):
19592046
The fraction of the input data that is to be
19602047
used to train the Model.
@@ -2010,6 +2097,7 @@ def _run(
20102097
predefined_split_column_name=predefined_split_column_name,
20112098
model=managed_model,
20122099
gcs_destination_uri_prefix=base_output_dir,
2100+
bigquery_destination=bigquery_destination,
20132101
)
20142102

20152103
return model
Collapse file

‎tests/unit/aiplatform/test_training_jobs.py‎

Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_training_jobs.py
+139Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
_TEST_DATASET_NAME = "test-dataset-name"
7979
_TEST_BASE_OUTPUT_DIR = "gs://test-base-output-dir"
80+
_TEST_BIGQUERY_DESTINATION = "bq://test-project"
8081
_TEST_RUN_ARGS = ["-v", 0.1, "--test=arg"]
8182
_TEST_REPLICA_COUNT = 1
8283
_TEST_MACHINE_TYPE = "n1-standard-4"
@@ -1216,6 +1217,144 @@ def test_run_call_pipeline_service_create(
12161217

12171218
assert job.state == gca_pipeline_state.PipelineState.PIPELINE_STATE_SUCCEEDED
12181219

1220+
@pytest.mark.parametrize("sync", [True, False])
1221+
def test_run_call_pipeline_service_create_with_bigquery_destination(
1222+
self, mock_pipeline_service_create, mock_dataset, mock_model_service_get, sync,
1223+
):
1224+
aiplatform.init(project=_TEST_PROJECT, staging_bucket=_TEST_BUCKET_NAME)
1225+
1226+
job = training_jobs.CustomContainerTrainingJob(
1227+
display_name=_TEST_DISPLAY_NAME,
1228+
container_uri=_TEST_TRAINING_CONTAINER_IMAGE,
1229+
command=_TEST_TRAINING_CONTAINER_CMD,
1230+
model_serving_container_image_uri=_TEST_SERVING_CONTAINER_IMAGE,
1231+
model_serving_container_predict_route=_TEST_SERVING_CONTAINER_PREDICTION_ROUTE,
1232+
model_serving_container_health_route=_TEST_SERVING_CONTAINER_HEALTH_ROUTE,
1233+
model_instance_schema_uri=_TEST_MODEL_INSTANCE_SCHEMA_URI,
1234+
model_parameters_schema_uri=_TEST_MODEL_PARAMETERS_SCHEMA_URI,
1235+
model_prediction_schema_uri=_TEST_MODEL_PREDICTION_SCHEMA_URI,
1236+
model_serving_container_command=_TEST_MODEL_SERVING_CONTAINER_COMMAND,
1237+
model_serving_container_args=_TEST_MODEL_SERVING_CONTAINER_ARGS,
1238+
model_serving_container_environment_variables=_TEST_MODEL_SERVING_CONTAINER_ENVIRONMENT_VARIABLES,
1239+
model_serving_container_ports=_TEST_MODEL_SERVING_CONTAINER_PORTS,
1240+
model_description=_TEST_MODEL_DESCRIPTION,
1241+
)
1242+
1243+
model_from_job = job.run(
1244+
dataset=mock_dataset,
1245+
base_output_dir=_TEST_BASE_OUTPUT_DIR,
1246+
bigquery_destination=_TEST_BIGQUERY_DESTINATION,
1247+
args=_TEST_RUN_ARGS,
1248+
replica_count=1,
1249+
machine_type=_TEST_MACHINE_TYPE,
1250+
accelerator_type=_TEST_ACCELERATOR_TYPE,
1251+
accelerator_count=_TEST_ACCELERATOR_COUNT,
1252+
model_display_name=_TEST_MODEL_DISPLAY_NAME,
1253+
training_fraction_split=_TEST_TRAINING_FRACTION_SPLIT,
1254+
validation_fraction_split=_TEST_VALIDATION_FRACTION_SPLIT,
1255+
test_fraction_split=_TEST_TEST_FRACTION_SPLIT,
1256+
predefined_split_column_name=_TEST_PREDEFINED_SPLIT_COLUMN_NAME,
1257+
sync=sync,
1258+
)
1259+
1260+
if not sync:
1261+
model_from_job.wait()
1262+
1263+
true_args = _TEST_RUN_ARGS
1264+
1265+
true_worker_pool_spec = {
1266+
"replicaCount": _TEST_REPLICA_COUNT,
1267+
"machineSpec": {
1268+
"machineType": _TEST_MACHINE_TYPE,
1269+
"acceleratorType": _TEST_ACCELERATOR_TYPE,
1270+
"acceleratorCount": _TEST_ACCELERATOR_COUNT,
1271+
},
1272+
"containerSpec": {
1273+
"imageUri": _TEST_TRAINING_CONTAINER_IMAGE,
1274+
"command": _TEST_TRAINING_CONTAINER_CMD,
1275+
"args": true_args,
1276+
},
1277+
}
1278+
1279+
true_fraction_split = gca_training_pipeline.FractionSplit(
1280+
training_fraction=_TEST_TRAINING_FRACTION_SPLIT,
1281+
validation_fraction=_TEST_VALIDATION_FRACTION_SPLIT,
1282+
test_fraction=_TEST_TEST_FRACTION_SPLIT,
1283+
)
1284+
1285+
env = [
1286+
env_var.EnvVar(name=str(key), value=str(value))
1287+
for key, value in _TEST_MODEL_SERVING_CONTAINER_ENVIRONMENT_VARIABLES.items()
1288+
]
1289+
1290+
ports = [
1291+
gca_model.Port(container_port=port)
1292+
for port in _TEST_MODEL_SERVING_CONTAINER_PORTS
1293+
]
1294+
1295+
true_container_spec = gca_model.ModelContainerSpec(
1296+
image_uri=_TEST_SERVING_CONTAINER_IMAGE,
1297+
predict_route=_TEST_SERVING_CONTAINER_PREDICTION_ROUTE,
1298+
health_route=_TEST_SERVING_CONTAINER_HEALTH_ROUTE,
1299+
command=_TEST_MODEL_SERVING_CONTAINER_COMMAND,
1300+
args=_TEST_MODEL_SERVING_CONTAINER_ARGS,
1301+
env=env,
1302+
ports=ports,
1303+
)
1304+
1305+
true_managed_model = gca_model.Model(
1306+
display_name=_TEST_MODEL_DISPLAY_NAME,
1307+
description=_TEST_MODEL_DESCRIPTION,
1308+
container_spec=true_container_spec,
1309+
predict_schemata=gca_model.PredictSchemata(
1310+
instance_schema_uri=_TEST_MODEL_INSTANCE_SCHEMA_URI,
1311+
parameters_schema_uri=_TEST_MODEL_PARAMETERS_SCHEMA_URI,
1312+
prediction_schema_uri=_TEST_MODEL_PREDICTION_SCHEMA_URI,
1313+
),
1314+
)
1315+
1316+
true_input_data_config = gca_training_pipeline.InputDataConfig(
1317+
fraction_split=true_fraction_split,
1318+
predefined_split=gca_training_pipeline.PredefinedSplit(
1319+
key=_TEST_PREDEFINED_SPLIT_COLUMN_NAME
1320+
),
1321+
dataset_id=mock_dataset.name,
1322+
bigquery_destination=gca_io.BigQueryDestination(
1323+
output_uri=_TEST_BIGQUERY_DESTINATION
1324+
),
1325+
)
1326+
1327+
true_training_pipeline = gca_training_pipeline.TrainingPipeline(
1328+
display_name=_TEST_DISPLAY_NAME,
1329+
training_task_definition=schema.training_job.definition.custom_task,
1330+
training_task_inputs=json_format.ParseDict(
1331+
{
1332+
"workerPoolSpecs": [true_worker_pool_spec],
1333+
"baseOutputDirectory": {"output_uri_prefix": _TEST_BASE_OUTPUT_DIR},
1334+
},
1335+
struct_pb2.Value(),
1336+
),
1337+
model_to_upload=true_managed_model,
1338+
input_data_config=true_input_data_config,
1339+
)
1340+
1341+
mock_pipeline_service_create.assert_called_once_with(
1342+
parent=initializer.global_config.common_location_path(),
1343+
training_pipeline=true_training_pipeline,
1344+
)
1345+
1346+
assert job._gca_resource is mock_pipeline_service_create.return_value
1347+
1348+
mock_model_service_get.assert_called_once_with(name=_TEST_MODEL_NAME)
1349+
1350+
assert model_from_job._gca_resource is mock_model_service_get.return_value
1351+
1352+
assert job.get_model()._gca_resource is mock_model_service_get.return_value
1353+
1354+
assert not job.has_failed
1355+
1356+
assert job.state == gca_pipeline_state.PipelineState.PIPELINE_STATE_SUCCEEDED
1357+
12191358
@pytest.mark.parametrize("sync", [True, False])
12201359
def test_run_called_twice_raises(
12211360
self,

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.