|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
| 17 | +import copy |
| 18 | +import datetime |
17 | 19 | import pytest |
18 | 20 |
|
19 | | -import copy |
20 | 21 | from importlib import reload |
21 | 22 | from unittest import mock |
22 | 23 | from unittest.mock import patch |
@@ -523,6 +524,152 @@ def test_create_hyperparameter_tuning_job( |
523 | 524 | assert job.network == _TEST_NETWORK |
524 | 525 | assert job.trials == [] |
525 | 526 |
|
| 527 | + def test_create_hyperparameter_tuning_job_with_zero_max_wait_duration( |
| 528 | + self, |
| 529 | + create_hyperparameter_tuning_job_mock, |
| 530 | + get_hyperparameter_tuning_job_mock, |
| 531 | + ): |
| 532 | + |
| 533 | + aiplatform.init( |
| 534 | + project=_TEST_PROJECT, |
| 535 | + location=_TEST_LOCATION, |
| 536 | + staging_bucket=_TEST_STAGING_BUCKET, |
| 537 | + encryption_spec_key_name=_TEST_DEFAULT_ENCRYPTION_KEY_NAME, |
| 538 | + ) |
| 539 | + |
| 540 | + custom_job = aiplatform.CustomJob( |
| 541 | + display_name=test_constants.TrainingJobConstants._TEST_DISPLAY_NAME, |
| 542 | + worker_pool_specs=test_constants.TrainingJobConstants._TEST_WORKER_POOL_SPEC, |
| 543 | + base_output_dir=test_constants.TrainingJobConstants._TEST_BASE_OUTPUT_DIR, |
| 544 | + ) |
| 545 | + |
| 546 | + job = aiplatform.HyperparameterTuningJob( |
| 547 | + display_name=_TEST_DISPLAY_NAME, |
| 548 | + custom_job=custom_job, |
| 549 | + metric_spec={_TEST_METRIC_SPEC_KEY: _TEST_METRIC_SPEC_VALUE}, |
| 550 | + parameter_spec={ |
| 551 | + "lr": hpt.DoubleParameterSpec(min=0.001, max=0.1, scale="log"), |
| 552 | + "units": hpt.IntegerParameterSpec(min=4, max=1028, scale="linear"), |
| 553 | + "activation": hpt.CategoricalParameterSpec( |
| 554 | + values=["relu", "sigmoid", "elu", "selu", "tanh"] |
| 555 | + ), |
| 556 | + "batch_size": hpt.DiscreteParameterSpec( |
| 557 | + values=[4, 8, 16, 32, 64], |
| 558 | + scale="linear", |
| 559 | + conditional_parameter_spec={ |
| 560 | + "decay": _TEST_CONDITIONAL_PARAMETER_DECAY, |
| 561 | + "learning_rate": _TEST_CONDITIONAL_PARAMETER_LR, |
| 562 | + }, |
| 563 | + ), |
| 564 | + }, |
| 565 | + parallel_trial_count=_TEST_PARALLEL_TRIAL_COUNT, |
| 566 | + max_trial_count=_TEST_MAX_TRIAL_COUNT, |
| 567 | + max_failed_trial_count=_TEST_MAX_FAILED_TRIAL_COUNT, |
| 568 | + search_algorithm=_TEST_SEARCH_ALGORITHM, |
| 569 | + measurement_selection=_TEST_MEASUREMENT_SELECTION, |
| 570 | + labels=_TEST_LABELS, |
| 571 | + ) |
| 572 | + |
| 573 | + job.run( |
| 574 | + service_account=_TEST_SERVICE_ACCOUNT, |
| 575 | + network=_TEST_NETWORK, |
| 576 | + timeout=_TEST_TIMEOUT, |
| 577 | + restart_job_on_worker_restart=_TEST_RESTART_JOB_ON_WORKER_RESTART, |
| 578 | + sync=True, |
| 579 | + create_request_timeout=None, |
| 580 | + disable_retries=_TEST_DISABLE_RETRIES, |
| 581 | + max_wait_duration=0, |
| 582 | + ) |
| 583 | + |
| 584 | + job.wait() |
| 585 | + |
| 586 | + expected_hyperparameter_tuning_job = _get_hyperparameter_tuning_job_proto() |
| 587 | + expected_hyperparameter_tuning_job.trial_job_spec.scheduling.max_wait_duration = datetime.timedelta( |
| 588 | + seconds=0 |
| 589 | + ) |
| 590 | + |
| 591 | + create_hyperparameter_tuning_job_mock.assert_called_once_with( |
| 592 | + parent=_TEST_PARENT, |
| 593 | + hyperparameter_tuning_job=expected_hyperparameter_tuning_job, |
| 594 | + timeout=None, |
| 595 | + ) |
| 596 | + assert job.state == gca_job_state_compat.JobState.JOB_STATE_SUCCEEDED |
| 597 | + |
| 598 | + def test_create_hyperparameter_tuning_job_with_default_max_wait_duration( |
| 599 | + self, |
| 600 | + create_hyperparameter_tuning_job_mock, |
| 601 | + get_hyperparameter_tuning_job_mock, |
| 602 | + ): |
| 603 | + |
| 604 | + aiplatform.init( |
| 605 | + project=_TEST_PROJECT, |
| 606 | + location=_TEST_LOCATION, |
| 607 | + staging_bucket=_TEST_STAGING_BUCKET, |
| 608 | + encryption_spec_key_name=_TEST_DEFAULT_ENCRYPTION_KEY_NAME, |
| 609 | + ) |
| 610 | + |
| 611 | + custom_job = aiplatform.CustomJob( |
| 612 | + display_name=test_constants.TrainingJobConstants._TEST_DISPLAY_NAME, |
| 613 | + worker_pool_specs=test_constants.TrainingJobConstants._TEST_WORKER_POOL_SPEC, |
| 614 | + base_output_dir=test_constants.TrainingJobConstants._TEST_BASE_OUTPUT_DIR, |
| 615 | + ) |
| 616 | + |
| 617 | + job = aiplatform.HyperparameterTuningJob( |
| 618 | + display_name=_TEST_DISPLAY_NAME, |
| 619 | + custom_job=custom_job, |
| 620 | + metric_spec={_TEST_METRIC_SPEC_KEY: _TEST_METRIC_SPEC_VALUE}, |
| 621 | + parameter_spec={ |
| 622 | + "lr": hpt.DoubleParameterSpec(min=0.001, max=0.1, scale="log"), |
| 623 | + "units": hpt.IntegerParameterSpec(min=4, max=1028, scale="linear"), |
| 624 | + "activation": hpt.CategoricalParameterSpec( |
| 625 | + values=["relu", "sigmoid", "elu", "selu", "tanh"] |
| 626 | + ), |
| 627 | + "batch_size": hpt.DiscreteParameterSpec( |
| 628 | + values=[4, 8, 16, 32, 64], |
| 629 | + scale="linear", |
| 630 | + conditional_parameter_spec={ |
| 631 | + "decay": _TEST_CONDITIONAL_PARAMETER_DECAY, |
| 632 | + "learning_rate": _TEST_CONDITIONAL_PARAMETER_LR, |
| 633 | + }, |
| 634 | + ), |
| 635 | + }, |
| 636 | + parallel_trial_count=_TEST_PARALLEL_TRIAL_COUNT, |
| 637 | + max_trial_count=_TEST_MAX_TRIAL_COUNT, |
| 638 | + max_failed_trial_count=_TEST_MAX_FAILED_TRIAL_COUNT, |
| 639 | + search_algorithm=_TEST_SEARCH_ALGORITHM, |
| 640 | + measurement_selection=_TEST_MEASUREMENT_SELECTION, |
| 641 | + labels=_TEST_LABELS, |
| 642 | + ) |
| 643 | + |
| 644 | + job.run( |
| 645 | + service_account=_TEST_SERVICE_ACCOUNT, |
| 646 | + network=_TEST_NETWORK, |
| 647 | + timeout=_TEST_TIMEOUT, |
| 648 | + restart_job_on_worker_restart=_TEST_RESTART_JOB_ON_WORKER_RESTART, |
| 649 | + sync=True, |
| 650 | + create_request_timeout=None, |
| 651 | + disable_retries=_TEST_DISABLE_RETRIES, |
| 652 | + ) |
| 653 | + |
| 654 | + job.wait() |
| 655 | + |
| 656 | + expected_hyperparameter_tuning_job = _get_hyperparameter_tuning_job_proto() |
| 657 | + expected_hyperparameter_tuning_job.trial_job_spec.scheduling.max_wait_duration = ( |
| 658 | + None |
| 659 | + ) |
| 660 | + |
| 661 | + create_hyperparameter_tuning_job_mock.assert_called_once_with( |
| 662 | + parent=_TEST_PARENT, |
| 663 | + hyperparameter_tuning_job=expected_hyperparameter_tuning_job, |
| 664 | + timeout=None, |
| 665 | + ) |
| 666 | + |
| 667 | + assert ( |
| 668 | + "max_wait_duration" |
| 669 | + not in expected_hyperparameter_tuning_job.trial_job_spec.scheduling |
| 670 | + ) |
| 671 | + assert job.state == gca_job_state_compat.JobState.JOB_STATE_SUCCEEDED |
| 672 | + |
526 | 673 | @pytest.mark.parametrize("sync", [True, False]) |
527 | 674 | def test_create_hyperparameter_tuning_job_with_timeout( |
528 | 675 | self, |
|
0 commit comments