-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test/sdk): add run-e2e-tune-api.py.
Signed-off-by: Electronic-Waste <[email protected]>
- Loading branch information
1 parent
153cdef
commit b43f603
Showing
5 changed files
with
242 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import argparse | ||
import logging | ||
|
||
from kubeflow.katib import KatibClient | ||
from kubeflow.katib import search | ||
from verify import verify_experiment_results | ||
import yaml | ||
|
||
# Experiment timeout is 40 min. | ||
EXPERIMENT_TIMEOUT = 60 * 40 | ||
|
||
# The default logging config. | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
def run_e2e_experiment_create_by_tune( | ||
katib_client: KatibClient, | ||
exp_name: str, | ||
exp_namespace: str, | ||
): | ||
# Create Katib Experiment and wait until it is finished. | ||
logging.debug("Creating Experiment: {}/{}".format(exp_namespace, exp_name)) | ||
|
||
# Use the test case from get-started tutorial. | ||
# https://www.kubeflow.org/docs/components/katib/getting-started/#getting-started-with-katib-python-sdk | ||
# [1] Create an objective function. | ||
def objective(parameters): | ||
result = 4 * int(parameters["a"]) - float(parameters["b"]) ** 2 | ||
print(f"result={result}") | ||
|
||
# [2] Create hyperparameter search space. | ||
parameters = { | ||
"a": search.int(min=10, max=20), | ||
"b": search.double(min=0.1, max=0.2) | ||
} | ||
|
||
# [3] Create Katib Experiment with 4 Trials and 2 CPUs per Trial. | ||
# And Wait until Experiment reaches Succeeded condition. | ||
katib_client.tune( | ||
name=exp_name, | ||
namespace=exp_namespace, | ||
objective=objective, | ||
parameters=parameters, | ||
objective_metric_name="result", | ||
max_trial_count=4, | ||
resources_per_trial={"cpu": "2"}, | ||
) | ||
experiment = katib_client.wait_for_experiment_condition( | ||
exp_name, exp_namespace, timeout=EXPERIMENT_TIMEOUT | ||
) | ||
|
||
# Verify the Experiment results. | ||
verify_experiment_results(katib_client, experiment, exp_name, exp_namespace) | ||
|
||
# Print the Experiment and Suggestion. | ||
logging.debug(katib_client.get_experiment(exp_name, exp_namespace)) | ||
logging.debug(katib_client.get_suggestion(exp_name, exp_namespace)) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--namespace", type=str, required=True, help="Namespace for the Katib E2E test", | ||
) | ||
parser.add_argument( | ||
"--trial-pod-annotations", type=str, help="Annotation for the pod created by trial", | ||
) | ||
parser.add_argument( | ||
"--verbose", action="store_true", help="Verbose output for the Katib E2E test", | ||
) | ||
args = parser.parse_args() | ||
|
||
katib_client = KatibClient() | ||
|
||
# Test with run_e2e_experiment_create_by_tune | ||
exp_name = "tune-example" | ||
exp_namespace = args.namespace | ||
try: | ||
run_e2e_experiment_create_by_tune(katib_client, exp_name, exp_namespace) | ||
logging.info("---------------------------------------------------------------") | ||
logging.info(f"E2E is succeeded for Experiment created by tune: {exp_namespace}/{exp_name}") | ||
except Exception as e: | ||
logging.info("---------------------------------------------------------------") | ||
logging.info(f"E2E is failed for Experiment created by tune: {exp_namespace}/{exp_name}") | ||
raise e | ||
finally: | ||
# Delete the Experiment. | ||
logging.info("---------------------------------------------------------------") | ||
logging.info("---------------------------------------------------------------") | ||
katib_client.delete_experiment(exp_name, exp_namespace) |
Oops, something went wrong.