Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add filter and timestamp splits #549

Closed
wants to merge 16 commits into from
913 changes: 745 additions & 168 deletions google/cloud/aiplatform/training_jobs.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ def test_run_call_pipeline_service_create(
data_granularity_unit=_TEST_TRAINING_DATA_GRANULARITY_UNIT,
data_granularity_count=_TEST_TRAINING_DATA_GRANULARITY_COUNT,
model_display_name=_TEST_MODEL_DISPLAY_NAME,
predefined_split_column_name=_TEST_PREDEFINED_SPLIT_COLUMN_NAME,
weight_column=_TEST_TRAINING_WEIGHT_COLUMN,
time_series_attribute_columns=_TEST_TRAINING_TIME_SERIES_ATTRIBUTE_COLUMNS,
context_window=_TEST_TRAINING_CONTEXT_WINDOW,
Expand All @@ -267,9 +266,6 @@ def test_run_call_pipeline_service_create(

true_input_data_config = gca_training_pipeline.InputDataConfig(
fraction_split=true_fraction_split,
predefined_split=gca_training_pipeline.PredefinedSplit(
key=_TEST_PREDEFINED_SPLIT_COLUMN_NAME
),
dataset_id=mock_dataset_time_series.name,
)

Expand Down
59 changes: 44 additions & 15 deletions tests/unit/aiplatform/test_automl_image_training_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
_TEST_FRACTION_SPLIT_TRAINING = 0.6
_TEST_FRACTION_SPLIT_VALIDATION = 0.2
_TEST_FRACTION_SPLIT_TEST = 0.2
_TEST_DEFAULT_TRAINING_FRACTION_SPLIT = 0.8
_TEST_DEFAULT_VALIDATION_FRACTION_SPLIT = 0.1
_TEST_DEFAULT_TEST_FRACTION_SPLIT = 0.1
_TEST_FILTER_SPLIT_TRAINING = "train"
_TEST_FILTER_SPLIT_VALIDATION = "validate"
_TEST_FILTER_SPLIT_TEST = "test"

_TEST_MODEL_NAME = (
f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/models/{_TEST_MODEL_ID}"
Expand Down Expand Up @@ -156,6 +162,7 @@ def mock_model_service_get():
def mock_dataset_image():
ds = mock.MagicMock(datasets.ImageDataset)
ds.name = _TEST_DATASET_NAME
ds.metadata_schema_uri = _TEST_METADATA_SCHEMA_URI_IMAGE
ds._latest_future = None
ds._exception = None
ds._gca_resource = gca_dataset.Dataset(
Expand Down Expand Up @@ -257,9 +264,9 @@ def test_run_call_pipeline_service_create(
model_from_job = job.run(
dataset=mock_dataset_image,
model_display_name=_TEST_MODEL_DISPLAY_NAME,
training_fraction_split=_TEST_FRACTION_SPLIT_TRAINING,
validation_fraction_split=_TEST_FRACTION_SPLIT_VALIDATION,
test_fraction_split=_TEST_FRACTION_SPLIT_TEST,
training_filter_split=_TEST_FILTER_SPLIT_TRAINING,
validation_filter_split=_TEST_FILTER_SPLIT_VALIDATION,
test_filter_split=_TEST_FILTER_SPLIT_TEST,
budget_milli_node_hours=_TEST_TRAINING_BUDGET_MILLI_NODE_HOURS,
disable_early_stopping=_TEST_TRAINING_DISABLE_EARLY_STOPPING,
sync=sync,
Expand All @@ -268,10 +275,10 @@ def test_run_call_pipeline_service_create(
if not sync:
model_from_job.wait()

true_fraction_split = gca_training_pipeline.FractionSplit(
training_fraction=_TEST_FRACTION_SPLIT_TRAINING,
validation_fraction=_TEST_FRACTION_SPLIT_VALIDATION,
test_fraction=_TEST_FRACTION_SPLIT_TEST,
true_filter_split = gca_training_pipeline.FilterSplit(
training_filter=_TEST_FILTER_SPLIT_TRAINING,
validation_filter=_TEST_FILTER_SPLIT_VALIDATION,
test_filter=_TEST_FILTER_SPLIT_TEST,
)

true_managed_model = gca_model.Model(
Expand All @@ -281,7 +288,7 @@ def test_run_call_pipeline_service_create(
)

true_input_data_config = gca_training_pipeline.InputDataConfig(
fraction_split=true_fraction_split, dataset_id=mock_dataset_image.name,
filter_split=true_filter_split, dataset_id=mock_dataset_image.name,
)

true_training_pipeline = gca_training_pipeline.TrainingPipeline(
Expand Down Expand Up @@ -324,9 +331,6 @@ def test_run_call_pipeline_if_no_model_display_name(

model_from_job = job.run(
dataset=mock_dataset_image,
training_fraction_split=_TEST_FRACTION_SPLIT_TRAINING,
validation_fraction_split=_TEST_FRACTION_SPLIT_VALIDATION,
test_fraction_split=_TEST_FRACTION_SPLIT_TEST,
budget_milli_node_hours=_TEST_TRAINING_BUDGET_MILLI_NODE_HOURS,
disable_early_stopping=_TEST_TRAINING_DISABLE_EARLY_STOPPING,
)
Expand All @@ -335,9 +339,9 @@ def test_run_call_pipeline_if_no_model_display_name(
model_from_job.wait()

true_fraction_split = gca_training_pipeline.FractionSplit(
training_fraction=_TEST_FRACTION_SPLIT_TRAINING,
validation_fraction=_TEST_FRACTION_SPLIT_VALIDATION,
test_fraction=_TEST_FRACTION_SPLIT_TEST,
training_fraction=_TEST_DEFAULT_TRAINING_FRACTION_SPLIT,
validation_fraction=_TEST_DEFAULT_VALIDATION_FRACTION_SPLIT,
test_fraction=_TEST_DEFAULT_TEST_FRACTION_SPLIT,
)

# Test that if defaults to the job display name
Expand All @@ -346,7 +350,7 @@ def test_run_call_pipeline_if_no_model_display_name(
)

true_input_data_config = gca_training_pipeline.InputDataConfig(
fraction_split=true_fraction_split, dataset_id=mock_dataset_image.name,
fraction_split=true_fraction_split, dataset_id=mock_dataset_image.name
)

true_training_pipeline = gca_training_pipeline.TrainingPipeline(
Expand Down Expand Up @@ -386,13 +390,38 @@ def test_run_called_twice_raises(self, mock_dataset_image, sync):

with pytest.raises(RuntimeError):
job.run(
dataset=mock_dataset_image,
model_display_name=_TEST_MODEL_DISPLAY_NAME,
sync=sync,
)

@pytest.mark.usefixtures(
"mock_pipeline_service_create",
"mock_pipeline_service_get",
"mock_model_service_get",
)
@pytest.mark.parametrize("sync", [True, False])
def test_run_with_two_split_raises(
self, mock_dataset_image, sync,
):
aiplatform.init(project=_TEST_PROJECT)

job = training_jobs.AutoMLImageTrainingJob(display_name=_TEST_DISPLAY_NAME,)

with pytest.raises(ValueError):
model_from_job = job.run(
dataset=mock_dataset_image,
model_display_name=_TEST_MODEL_DISPLAY_NAME,
training_fraction_split=_TEST_FRACTION_SPLIT_TRAINING,
validation_fraction_split=_TEST_FRACTION_SPLIT_VALIDATION,
test_fraction_split=_TEST_FRACTION_SPLIT_TEST,
training_filter_split=_TEST_FILTER_SPLIT_TRAINING,
validation_filter_split=_TEST_FILTER_SPLIT_VALIDATION,
test_filter_split=_TEST_FILTER_SPLIT_TEST,
sync=sync,
)
if not sync:
model_from_job.wait()

@pytest.mark.parametrize("sync", [True, False])
def test_run_raises_if_pipeline_fails(
Expand Down
Loading