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

[SDK] Add env & env_from in client tune #2235

Merged
merged 2 commits into from
Nov 17, 2023
Merged
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
18 changes: 18 additions & 0 deletions sdk/python/v1beta1/kubeflow/katib/api/katib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def tune(
parameters: Dict[str, Any],
base_image: str = constants.BASE_IMAGE_TENSORFLOW,
namespace: Optional[str] = None,
env_per_trial: Optional[Union[Dict[str, str], List[Union[client.V1EnvVar, client.V1EnvFromSource]]]] = None,
algorithm_name: str = "random",
algorithm_settings: Union[dict, List[models.V1beta1AlgorithmSetting], None] = None,
objective_metric_name: str = None,
Expand Down Expand Up @@ -172,6 +173,12 @@ def tune(
objective function.
base_image: Image to use when executing the objective function.
namespace: Namespace for the Experiment.
env_per_trial: Environment variable(s) to be attached to each trial container.
You can specify a dictionary as a mapping object representing the environment variables.
Otherwise, you can specify a list, in which the element can either be a kubernetes.client.models.V1EnvVar (documented here:
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1EnvVar.md)
or a kubernetes.client.models.V1EnvFromSource (documented here:
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1EnvFromSource.md)
algorithm_name: Search algorithm for the HyperParameter tuning.
algorithm_settings: Settings for the search algorithm given.
For available fields, check this doc: https://www.kubeflow.org/docs/components/katib/experiment/#search-algorithms-in-detail.
Expand Down Expand Up @@ -318,6 +325,15 @@ def tune(
requests=resources_per_trial,
limits=resources_per_trial,
)

if isinstance(env_per_trial, dict):
env, env_from = [client.V1EnvVar(name=str(k), value=str(v)) for k, v in env_per_trial.items()] or None, None

if env_per_trial:
env = [x for x in env_per_trial if isinstance(x, client.V1EnvVar)] or None
env_from = [x for x in env_per_trial if isinstance(x, client.V1EnvFromSource)] or None
else:
env, env_from = None, None

# Create Trial specification.
trial_spec = client.V1Job(
Expand All @@ -336,6 +352,8 @@ def tune(
image=base_image,
command=["bash", "-c"],
args=[exec_script],
env=env,
env_from=env_from,
resources=resources_per_trial,
)
],
Expand Down