GeezerVison is showing github.com — feat: support additional_experiments for AutoML Tables and AutoML For… · googleapis/python-aiplatform@282bb62
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/training_jobs.py +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2420,6 +2420,8 @@ def __init__( | 2420 | 2420 | optimization_objective_precision_value | 2421 | 2421 | ) | 2422 | 2422 |
| | 2423 | + self._additional_experiments = [] | | 2424 | + | 2423 | 2425 | def run( | 2424 | 2426 | self, | 2425 | 2427 | dataset: datasets.TabularDataset, | @@ -2664,6 +2666,9 @@ def _run( | 2664 | 2666 | "optimizationObjectivePrecisionValue": self._optimization_objective_precision_value, | 2665 | 2667 | } | 2666 | 2668 |
| | 2669 | + if self._additional_experiments: | | 2670 | + training_task_inputs_dict["additionalExperiments"] = self._additional_experiments | | 2671 | + | 2667 | 2672 | if model_display_name is None: | 2668 | 2673 | model_display_name = self._display_name | 2669 | 2674 |
| @@ -2691,6 +2696,14 @@ def _model_upload_fail_string(self) -> str: | 2691 | 2696 | "Model." | 2692 | 2697 | ) | 2693 | 2698 |
| | 2699 | + def _add_additional_experiments(self, additional_experiments: List[str]): | | 2700 | + """Add experiment flag to the training job. | | 2701 | + Args: | | 2702 | + additional_experiments (List[str]): | | 2703 | + Experiment flags that can enable some experimental training features. | | 2704 | + """ | | 2705 | + self._additional_experiments.extend(additional_experiments) | | 2706 | + | 2694 | 2707 |
| 2695 | 2708 | class AutoMLForecastingTrainingJob(_TrainingJob): | 2696 | 2709 | _supported_training_schemas = (schema.training_job.definition.automl_forecasting,) | @@ -2746,6 +2759,7 @@ def __init__( | 2746 | 2759 | ) | 2747 | 2760 | self._column_transformations = column_transformations | 2748 | 2761 | self._optimization_objective = optimization_objective | | 2762 | + self._additional_experiments = [] | 2749 | 2763 |
| 2750 | 2764 | def run( | 2751 | 2765 | self, | @@ -3119,6 +3133,9 @@ def _run( | 3119 | 3133 | "overrideExistingTable": export_evaluated_data_items_override_destination, | 3120 | 3134 | } | 3121 | 3135 |
| | 3136 | + if self._additional_experiments: | | 3137 | + training_task_inputs_dict["additionalExperiments"] = self._additional_experiments | | 3138 | + | 3122 | 3139 | if model_display_name is None: | 3123 | 3140 | model_display_name = self._display_name | 3124 | 3141 |
| @@ -3143,6 +3160,14 @@ def _model_upload_fail_string(self) -> str: | 3143 | 3160 | "Model." | 3144 | 3161 | ) | 3145 | 3162 |
| | 3163 | + def _add_additional_experiments(self, additional_experiments: List[str]): | | 3164 | + """Add experiment flag to the training job. | | 3165 | + Args: | | 3166 | + additional_experiments (List[str]): | | 3167 | + Experiment flags that can enable some experimental training features. | | 3168 | + """ | | 3169 | + self._additional_experiments.extend(additional_experiments) | | 3170 | + | 3146 | 3171 |
| 3147 | 3172 | class AutoMLImageTrainingJob(_TrainingJob): | 3148 | 3173 | _supported_training_schemas = ( | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_automl_forecasting_training_jobs.py +104-24Lines changed: 104 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 62 | 62 | _TEST_TRAINING_BUDGET_MILLI_NODE_HOURS = 1000 | 63 | 63 | _TEST_TRAINING_WEIGHT_COLUMN = "weight" | 64 | 64 | _TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME = "minimize-rmse" | | 65 | +_TEST_ADDITIONAL_EXPERIMENTS = ["exp1", "exp2"] | | 66 | +_TEST_TRAINING_TASK_INPUTS_DICT = { | | 67 | + # required inputs | | 68 | + "targetColumn": _TEST_TRAINING_TARGET_COLUMN, | | 69 | + "timeColumn": _TEST_TRAINING_TIME_COLUMN, | | 70 | + "timeSeriesIdentifierColumn": _TEST_TRAINING_TIME_SERIES_IDENTIFIER_COLUMN, | | 71 | + "timeSeriesAttributeColumns": _TEST_TRAINING_TIME_SERIES_ATTRIBUTE_COLUMNS, | | 72 | + "unavailableAtForecastColumns": _TEST_TRAINING_UNAVAILABLE_AT_FORECAST_COLUMNS, | | 73 | + "availableAtForecastColumns": _TEST_TRAINING_AVAILABLE_AT_FORECAST_COLUMNS, | | 74 | + "forecastHorizon": _TEST_TRAINING_FORECAST_HORIZON, | | 75 | + "dataGranularity": { | | 76 | + "unit": _TEST_TRAINING_DATA_GRANULARITY_UNIT, | | 77 | + "quantity": _TEST_TRAINING_DATA_GRANULARITY_COUNT, | | 78 | + }, | | 79 | + "transformations": _TEST_TRAINING_COLUMN_TRANSFORMATIONS, | | 80 | + "trainBudgetMilliNodeHours": _TEST_TRAINING_BUDGET_MILLI_NODE_HOURS, | | 81 | + # optional inputs | | 82 | + "weightColumn": _TEST_TRAINING_WEIGHT_COLUMN, | | 83 | + "contextWindow": _TEST_TRAINING_CONTEXT_WINDOW, | | 84 | + "exportEvaluatedDataItemsConfig": { | | 85 | + "destinationBigqueryUri": _TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS_BIGQUERY_DESTINATION_URI, | | 86 | + "overrideExistingTable": _TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS_OVERRIDE_DESTINATION, | | 87 | + }, | | 88 | + "quantiles": _TEST_TRAINING_QUANTILES, | | 89 | + "validationOptions": _TEST_TRAINING_VALIDATION_OPTIONS, | | 90 | + "optimizationObjective": _TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME, | | 91 | +} | 65 | 92 | _TEST_TRAINING_TASK_INPUTS = json_format.ParseDict( | | 93 | + _TEST_TRAINING_TASK_INPUTS_DICT, | | 94 | + struct_pb2.Value(), | | 95 | +) | | 96 | +_TEST_TRAINING_TASK_INPUTS_WITH_ADDITIONAL_EXPERIMENTS = json_format.ParseDict( | 66 | 97 | { | 67 | | - # required inputs | 68 | | - "targetColumn": _TEST_TRAINING_TARGET_COLUMN, | 69 | | - "timeColumn": _TEST_TRAINING_TIME_COLUMN, | 70 | | - "timeSeriesIdentifierColumn": _TEST_TRAINING_TIME_SERIES_IDENTIFIER_COLUMN, | 71 | | - "timeSeriesAttributeColumns": _TEST_TRAINING_TIME_SERIES_ATTRIBUTE_COLUMNS, | 72 | | - "unavailableAtForecastColumns": _TEST_TRAINING_UNAVAILABLE_AT_FORECAST_COLUMNS, | 73 | | - "availableAtForecastColumns": _TEST_TRAINING_AVAILABLE_AT_FORECAST_COLUMNS, | 74 | | - "forecastHorizon": _TEST_TRAINING_FORECAST_HORIZON, | 75 | | - "dataGranularity": { | 76 | | - "unit": _TEST_TRAINING_DATA_GRANULARITY_UNIT, | 77 | | - "quantity": _TEST_TRAINING_DATA_GRANULARITY_COUNT, | 78 | | - }, | 79 | | - "transformations": _TEST_TRAINING_COLUMN_TRANSFORMATIONS, | 80 | | - "trainBudgetMilliNodeHours": _TEST_TRAINING_BUDGET_MILLI_NODE_HOURS, | 81 | | - # optional inputs | 82 | | - "weightColumn": _TEST_TRAINING_WEIGHT_COLUMN, | 83 | | - "contextWindow": _TEST_TRAINING_CONTEXT_WINDOW, | 84 | | - "exportEvaluatedDataItemsConfig": { | 85 | | - "destinationBigqueryUri": _TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS_BIGQUERY_DESTINATION_URI, | 86 | | - "overrideExistingTable": _TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS_OVERRIDE_DESTINATION, | 87 | | - }, | 88 | | - "quantiles": _TEST_TRAINING_QUANTILES, | 89 | | - "validationOptions": _TEST_TRAINING_VALIDATION_OPTIONS, | 90 | | - "optimizationObjective": _TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME, | | 98 | + **_TEST_TRAINING_TASK_INPUTS_DICT, | | 99 | + "additionalExperiments": _TEST_ADDITIONAL_EXPERIMENTS, | 91 | 100 | }, | 92 | 101 | struct_pb2.Value(), | 93 | 102 | ) | @@ -359,6 +368,77 @@ def test_run_call_pipeline_if_no_model_display_name( | 359 | 368 | training_pipeline=true_training_pipeline, | 360 | 369 | ) | 361 | 370 |
| | 371 | + @pytest.mark.usefixtures("mock_pipeline_service_get") | | 372 | + @pytest.mark.parametrize("sync", [True, False]) | | 373 | + def test_run_call_pipeline_if_set_additional_experiments( | | 374 | + self, | | 375 | + mock_pipeline_service_create, | | 376 | + mock_dataset_time_series, | | 377 | + mock_model_service_get, | | 378 | + sync, | | 379 | + ): | | 380 | + aiplatform.init(project=_TEST_PROJECT, staging_bucket=_TEST_BUCKET_NAME) | | 381 | + | | 382 | + job = AutoMLForecastingTrainingJob( | | 383 | + display_name=_TEST_DISPLAY_NAME, | | 384 | + optimization_objective=_TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME, | | 385 | + column_transformations=_TEST_TRAINING_COLUMN_TRANSFORMATIONS, | | 386 | + ) | | 387 | + | | 388 | + job._add_additional_experiments(_TEST_ADDITIONAL_EXPERIMENTS) | | 389 | + | | 390 | + model_from_job = job.run( | | 391 | + dataset=mock_dataset_time_series, | | 392 | + target_column=_TEST_TRAINING_TARGET_COLUMN, | | 393 | + time_column=_TEST_TRAINING_TIME_COLUMN, | | 394 | + time_series_identifier_column=_TEST_TRAINING_TIME_SERIES_IDENTIFIER_COLUMN, | | 395 | + unavailable_at_forecast_columns=_TEST_TRAINING_UNAVAILABLE_AT_FORECAST_COLUMNS, | | 396 | + available_at_forecast_columns=_TEST_TRAINING_AVAILABLE_AT_FORECAST_COLUMNS, | | 397 | + forecast_horizon=_TEST_TRAINING_FORECAST_HORIZON, | | 398 | + data_granularity_unit=_TEST_TRAINING_DATA_GRANULARITY_UNIT, | | 399 | + data_granularity_count=_TEST_TRAINING_DATA_GRANULARITY_COUNT, | | 400 | + weight_column=_TEST_TRAINING_WEIGHT_COLUMN, | | 401 | + time_series_attribute_columns=_TEST_TRAINING_TIME_SERIES_ATTRIBUTE_COLUMNS, | | 402 | + context_window=_TEST_TRAINING_CONTEXT_WINDOW, | | 403 | + budget_milli_node_hours=_TEST_TRAINING_BUDGET_MILLI_NODE_HOURS, | | 404 | + export_evaluated_data_items=_TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS, | | 405 | + export_evaluated_data_items_bigquery_destination_uri=_TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS_BIGQUERY_DESTINATION_URI, | | 406 | + export_evaluated_data_items_override_destination=_TEST_TRAINING_EXPORT_EVALUATED_DATA_ITEMS_OVERRIDE_DESTINATION, | | 407 | + quantiles=_TEST_TRAINING_QUANTILES, | | 408 | + validation_options=_TEST_TRAINING_VALIDATION_OPTIONS, | | 409 | + sync=sync, | | 410 | + ) | | 411 | + | | 412 | + if not sync: | | 413 | + model_from_job.wait() | | 414 | + | | 415 | + true_fraction_split = gca_training_pipeline.FractionSplit( | | 416 | + training_fraction=_TEST_TRAINING_FRACTION_SPLIT, | | 417 | + validation_fraction=_TEST_VALIDATION_FRACTION_SPLIT, | | 418 | + test_fraction=_TEST_TEST_FRACTION_SPLIT, | | 419 | + ) | | 420 | + | | 421 | + # Test that if defaults to the job display name | | 422 | + true_managed_model = gca_model.Model(display_name=_TEST_DISPLAY_NAME) | | 423 | + | | 424 | + true_input_data_config = gca_training_pipeline.InputDataConfig( | | 425 | + fraction_split=true_fraction_split, | | 426 | + dataset_id=mock_dataset_time_series.name, | | 427 | + ) | | 428 | + | | 429 | + true_training_pipeline = gca_training_pipeline.TrainingPipeline( | | 430 | + display_name=_TEST_DISPLAY_NAME, | | 431 | + training_task_definition=schema.training_job.definition.automl_forecasting, | | 432 | + training_task_inputs=_TEST_TRAINING_TASK_INPUTS_WITH_ADDITIONAL_EXPERIMENTS, | | 433 | + model_to_upload=true_managed_model, | | 434 | + input_data_config=true_input_data_config, | | 435 | + ) | | 436 | + | | 437 | + mock_pipeline_service_create.assert_called_once_with( | | 438 | + parent=initializer.global_config.common_location_path(), | | 439 | + training_pipeline=true_training_pipeline, | | 440 | + ) | | 441 | + | 362 | 442 | @pytest.mark.usefixtures( | 363 | 443 | "mock_pipeline_service_create", | 364 | 444 | "mock_pipeline_service_get", | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_automl_tabular_training_jobs.py +97-11Lines changed: 97 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 56 | 56 | _TEST_TRAINING_DISABLE_EARLY_STOPPING = True | 57 | 57 | _TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME = "minimize-log-loss" | 58 | 58 | _TEST_TRAINING_OPTIMIZATION_PREDICTION_TYPE = "classification" | | 59 | +_TEST_ADDITIONAL_EXPERIMENTS = ["exp1", "exp2"] | | 60 | +_TEST_TRAINING_TASK_INPUTS_DICT = { | | 61 | + # required inputs | | 62 | + "targetColumn": _TEST_TRAINING_TARGET_COLUMN, | | 63 | + "transformations": _TEST_TRAINING_COLUMN_TRANSFORMATIONS, | | 64 | + "trainBudgetMilliNodeHours": _TEST_TRAINING_BUDGET_MILLI_NODE_HOURS, | | 65 | + # optional inputs | | 66 | + "weightColumnName": _TEST_TRAINING_WEIGHT_COLUMN, | | 67 | + "disableEarlyStopping": _TEST_TRAINING_DISABLE_EARLY_STOPPING, | | 68 | + "predictionType": _TEST_TRAINING_OPTIMIZATION_PREDICTION_TYPE, | | 69 | + "optimizationObjective": _TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME, | | 70 | + "optimizationObjectiveRecallValue": None, | | 71 | + "optimizationObjectivePrecisionValue": None, | | 72 | +} | 59 | 73 | _TEST_TRAINING_TASK_INPUTS = json_format.ParseDict( | | 74 | + _TEST_TRAINING_TASK_INPUTS_DICT, | | 75 | + struct_pb2.Value(), | | 76 | +) | | 77 | +_TEST_TRAINING_TASK_INPUTS_WITH_ADDITIONAL_EXPERIMENTS = json_format.ParseDict( | 60 | 78 | { | 61 | | - # required inputs | 62 | | - "targetColumn": _TEST_TRAINING_TARGET_COLUMN, | 63 | | - "transformations": _TEST_TRAINING_COLUMN_TRANSFORMATIONS, | 64 | | - "trainBudgetMilliNodeHours": _TEST_TRAINING_BUDGET_MILLI_NODE_HOURS, | 65 | | - # optional inputs | 66 | | - "weightColumnName": _TEST_TRAINING_WEIGHT_COLUMN, | 67 | | - "disableEarlyStopping": _TEST_TRAINING_DISABLE_EARLY_STOPPING, | 68 | | - "predictionType": _TEST_TRAINING_OPTIMIZATION_PREDICTION_TYPE, | 69 | | - "optimizationObjective": _TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME, | 70 | | - "optimizationObjectiveRecallValue": None, | 71 | | - "optimizationObjectivePrecisionValue": None, | | 79 | + **_TEST_TRAINING_TASK_INPUTS_DICT, | | 80 | + "additionalExperiments": _TEST_ADDITIONAL_EXPERIMENTS, | 72 | 81 | }, | 73 | 82 | struct_pb2.Value(), | 74 | 83 | ) | @@ -430,6 +439,83 @@ def test_run_call_pipeline_service_create_if_no_column_transformations( | 430 | 439 | training_pipeline=true_training_pipeline, | 431 | 440 | ) | 432 | 441 |
| | 442 | + @pytest.mark.parametrize("sync", [True, False]) | | 443 | + # This test checks that default transformations are used if no columns transformations are provided | | 444 | + def test_run_call_pipeline_service_create_if_set_additional_experiments( | | 445 | + self, | | 446 | + mock_pipeline_service_create, | | 447 | + mock_pipeline_service_get, | | 448 | + mock_dataset_tabular, | | 449 | + mock_model_service_get, | | 450 | + sync, | | 451 | + ): | | 452 | + aiplatform.init( | | 453 | + project=_TEST_PROJECT, | | 454 | + staging_bucket=_TEST_BUCKET_NAME, | | 455 | + encryption_spec_key_name=_TEST_DEFAULT_ENCRYPTION_KEY_NAME, | | 456 | + ) | | 457 | + | | 458 | + job = training_jobs.AutoMLTabularTrainingJob( | | 459 | + display_name=_TEST_DISPLAY_NAME, | | 460 | + optimization_objective=_TEST_TRAINING_OPTIMIZATION_OBJECTIVE_NAME, | | 461 | + optimization_prediction_type=_TEST_TRAINING_OPTIMIZATION_PREDICTION_TYPE, | | 462 | + column_transformations=None, | | 463 | + optimization_objective_recall_value=None, | | 464 | + optimization_objective_precision_value=None, | | 465 | + ) | | 466 | + | | 467 | + job._add_additional_experiments(_TEST_ADDITIONAL_EXPERIMENTS) | | 468 | + | | 469 | + model_from_job = job.run( | | 470 | + dataset=mock_dataset_tabular, | | 471 | + target_column=_TEST_TRAINING_TARGET_COLUMN, | | 472 | + model_display_name=_TEST_MODEL_DISPLAY_NAME, | | 473 | + training_fraction_split=_TEST_TRAINING_FRACTION_SPLIT, | | 474 | + validation_fraction_split=_TEST_VALIDATION_FRACTION_SPLIT, | | 475 | + test_fraction_split=_TEST_TEST_FRACTION_SPLIT, | | 476 | + predefined_split_column_name=_TEST_PREDEFINED_SPLIT_COLUMN_NAME, | | 477 | + weight_column=_TEST_TRAINING_WEIGHT_COLUMN, | | 478 | + budget_milli_node_hours=_TEST_TRAINING_BUDGET_MILLI_NODE_HOURS, | | 479 | + disable_early_stopping=_TEST_TRAINING_DISABLE_EARLY_STOPPING, | | 480 | + sync=sync, | | 481 | + ) | | 482 | + | | 483 | + if not sync: | | 484 | + model_from_job.wait() | | 485 | + | | 486 | + true_fraction_split = gca_training_pipeline.FractionSplit( | | 487 | + training_fraction=_TEST_TRAINING_FRACTION_SPLIT, | | 488 | + validation_fraction=_TEST_VALIDATION_FRACTION_SPLIT, | | 489 | + test_fraction=_TEST_TEST_FRACTION_SPLIT, | | 490 | + ) | | 491 | + | | 492 | + true_managed_model = gca_model.Model( | | 493 | + display_name=_TEST_MODEL_DISPLAY_NAME, | | 494 | + encryption_spec=_TEST_DEFAULT_ENCRYPTION_SPEC, | | 495 | + ) | | 496 | + | | 497 | + true_input_data_config = gca_training_pipeline.InputDataConfig( | | 498 | + fraction_split=true_fraction_split, | | 499 | + predefined_split=gca_training_pipeline.PredefinedSplit( | | 500 | + key=_TEST_PREDEFINED_SPLIT_COLUMN_NAME | | 501 | + ), | | 502 | + dataset_id=mock_dataset_tabular.name, | | 503 | + ) | | 504 | + | | 505 | + true_training_pipeline = gca_training_pipeline.TrainingPipeline( | | 506 | + display_name=_TEST_DISPLAY_NAME, | | 507 | + training_task_definition=schema.training_job.definition.automl_tabular, | | 508 | + training_task_inputs=_TEST_TRAINING_TASK_INPUTS_WITH_ADDITIONAL_EXPERIMENTS, | | 509 | + model_to_upload=true_managed_model, | | 510 | + input_data_config=true_input_data_config, | | 511 | + encryption_spec=_TEST_DEFAULT_ENCRYPTION_SPEC, | | 512 | + ) | | 513 | + | | 514 | + mock_pipeline_service_create.assert_called_once_with( | | 515 | + parent=initializer.global_config.common_location_path(), | | 516 | + training_pipeline=true_training_pipeline, | | 517 | + ) | | 518 | + | 433 | 519 | @pytest.mark.usefixtures( | 434 | 520 | "mock_pipeline_service_create", | 435 | 521 | "mock_pipeline_service_get", | |
|