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

Create Tune API in the Katib SDK #1951

Merged
merged 6 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,51 @@ katib-ui-5767cfccdc-pwg2x 1/1 Running 0 36s

For the Katib Experiments check the [complete examples list](./examples/v1beta1).

# Quickstart

You can run your first HyperParameter Tuning Experiment using [Katib Python SDK](./sdk/python/v1beta1).

In the following example we are going to maximize a simple objective function:
$F(a,b) = 4a - b^2$. The bigger $a$ and the lesser $b$ value, the bigger the function value $F$.

```python
import kubeflow.katib as katib

# Step 1. Create an objective function.
def objective(parameters):
# Import required packages.
import time
Comment on lines +204 to +205
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why this kind of logic is necesary, but we might find out more friendly way to specify packages by users.
However, as a minimal feature, this is ok if we could instruct users that they couldn't make a mistake to miss required packages in the objective function.

Copy link
Member Author

@andreyvelich andreyvelich Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I think Kubeflow Pipelines follows the same way: https://www.kubeflow.org/docs/components/pipelines/sdk/python-function-components/#building-python-function-based-components
@zijianjoy Do you know if Pipelines SDK verifies that all function imports are defined properly in Python Lightweight component?

@anencore94 Any ideas how to verify this?
cc @tenzen-y @gaocegege @johnugeorge

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to automatically validating this after this PR. How about making an issue for tracking this feature now ?
If we could make an idea for this validation in general, we may apply this for other kf components like pipelines and training-operators and so on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that sound good @anencore94.

time.sleep(5)
# Calculate objective function.
result = 4 * int(parameters["a"]) - float(parameters["b"]) ** 2
# Katib parses metrics in this format: <metric-name>=<metric-value>.
print(f"result={result}")

# Step 2. Create HyperParameter search space.
parameters = {
"a": katib.search.int(min=10, max=20),
"b": katib.search.double(min=0.1, max=0.2)
}

# Step 3. Create Katib Experiment.
katib_client = katib.KatibClient()
name = "tune-experiment"
katib_client.tune(
name=name,
objective=objective,
parameters=parameters,
objective_metric_name="result",
max_trial_count=12
)

# Step 4. Get the best HyperParameters.
print(katib_client.get_optimal_hyperparameters(name))
```

# Documentation

- Run your first Katib Experiment in the
[getting started guide](https://www.kubeflow.org/docs/components/katib/hyperparameter/#example-using-random-algorithm).
- Check
[the Katib getting started guide](https://www.kubeflow.org/docs/components/katib/hyperparameter/#example-using-random-search-algorithm).

- Learn about Katib **Concepts** in this
[guide](https://www.kubeflow.org/docs/components/katib/overview/#katib-concepts).
Expand Down
788 changes: 788 additions & 0 deletions examples/v1beta1/sdk/tune-train-from-func.ipynb

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion hack/gen-python-sdk/post_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ def _rewrite_helper(input_file, output_file, rewrite_rules):
if not any(l in line for l in IGNORE_LINES):
lines.append(line)

# Add Katib client to init file.
# Add Katib APIs to the init file.
if (output_file == "sdk/python/v1beta1/kubeflow/katib/__init__.py"):
lines.append("# Import Katib API client.\n")
lines.append("from kubeflow.katib.api.katib_client import KatibClient\n")
lines.append("# Import Katib helper functions.\n")
lines.append("import kubeflow.katib.api.search as search\n")
lines.append("# Import Katib helper constants.\n")
lines.append("from kubeflow.katib.constants.constants import BASE_IMAGE_TENSORFLOW\n")
lines.append("from kubeflow.katib.constants.constants import BASE_IMAGE_TENSORFLOW_GPU\n")
lines.append("from kubeflow.katib.constants.constants import BASE_IMAGE_PYTORCH\n")
lines.append("from kubeflow.katib.constants.constants import BASE_IMAGE_MXNET\n")

# Add Kubernetes models to proper deserialization of Katib models.
if (output_file == "sdk/python/v1beta1/kubeflow/katib/models/__init__.py"):
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/v1beta1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Distribution / packaging
dist/
7 changes: 7 additions & 0 deletions sdk/python/v1beta1/kubeflow/katib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@

# Import Katib API client.
from kubeflow.katib.api.katib_client import KatibClient
# Import Katib helper functions.
import kubeflow.katib.api.search as search
# Import Katib helper constants.
from kubeflow.katib.constants.constants import BASE_IMAGE_TENSORFLOW
from kubeflow.katib.constants.constants import BASE_IMAGE_TENSORFLOW_GPU
from kubeflow.katib.constants.constants import BASE_IMAGE_PYTORCH
from kubeflow.katib.constants.constants import BASE_IMAGE_MXNET
Loading