Notebook-based Katib validation
Scenario 4. Launching Katib via a notebook
This scenario verifies that a Katib experiment can be launched from a MLOps Studio notebook in the user’s own namespace.
Prerequisites
The scenario is run for your namespace.
The experiment name is mnist-katib-quick. If this name already exists, it must be changed.
Steps
Create a notebook with MLOps Studio Pipeline.
In the first cell, add and run:
!pip install kfp==1.8.12 !pip install kubeflow-katib==0.13.0
In the next cell, add and run:
import kfp from kfp import dsl, components from kubeflow.katib import ( ApiClient, V1beta1ExperimentSpec, V1beta1AlgorithmSpec, V1beta1ObjectiveSpec, V1beta1ParameterSpec, V1beta1FeasibleSpace, V1beta1TrialTemplate, V1beta1TrialParameterSpec, )
In the next cell, add and run:
def create_katib_experiment_task(experiment_name, experiment_namespace, training_steps: int): max_trial_count = 2 max_failed_trial_count = 1 parallel_trial_count = 2 objective = V1beta1ObjectiveSpec( type="minimize", goal=0.01, objective_metric_name="loss", ) algorithm = V1beta1AlgorithmSpec( algorithm_name="random", ) parameters = [ V1beta1ParameterSpec( name="learning_rate", parameter_type="double", feasible_space=V1beta1FeasibleSpace( min="0.01", max="0.05", ), ), V1beta1ParameterSpec( name="batch_size", parameter_type="int", feasible_space=V1beta1FeasibleSpace( min="80", max="100", ), ), ] trial_spec = { "apiVersion": "kubeflow.org/v1", "kind": "TFJob", "spec": { "tfReplicaSpecs": { "Chief": { "replicas": 1, "restartPolicy": "OnFailure", "template": { "metadata": { "annotations": { "sidecar.istio.io/inject": "false" } }, "spec": { "containers": [ { "name": "tensorflow", "image": "docker.io/liuhougangxa/tf-estimator-mnist", "command": [ "python", "/opt/model.py", "--tf-train-steps=" + str(training_steps), "--tf-learning-rate=${trialParameters.learningRate}", "--tf-batch-size=${trialParameters.batchSize}", ], } ] }, }, }, "Worker": { "replicas": 1, "restartPolicy": "OnFailure", "template": { "metadata": { "annotations": { "sidecar.istio.io/inject": "false" } }, "spec": { "containers": [ { "name": "tensorflow", "image": "docker.io/liuhougangxa/tf-estimator-mnist", "command": [ "python", "/opt/model.py", "--tf-train-steps=" + str(training_steps), "--tf-learning-rate=${trialParameters.learningRate}", "--tf-batch-size=${trialParameters.batchSize}", ], } ] }, }, }, } }, } trial_template = V1beta1TrialTemplate( primary_container_name="tensorflow", trial_parameters=[ V1beta1TrialParameterSpec( name="learningRate", description="Learning rate for the training model", reference="learning_rate", ), V1beta1TrialParameterSpec( name="batchSize", description="Batch size for the model", reference="batch_size", ), ], trial_spec=trial_spec, ) experiment_spec = V1beta1ExperimentSpec( max_trial_count=max_trial_count, max_failed_trial_count=max_failed_trial_count, parallel_trial_count=parallel_trial_count, objective=objective, algorithm=algorithm, parameters=parameters, trial_template=trial_template, ) katib_experiment_launcher_op = components.load_component_from_url( "https://raw.githubusercontent.com/kubeflow/pipelines/34d23aa924720ead13fe67ebca5c1ab9926585ee/components/kubeflow/katib-launcher/component.yaml" ) op = katib_experiment_launcher_op( experiment_name=experiment_name, experiment_namespace=experiment_namespace, experiment_spec=ApiClient().sanitize_for_serialization(experiment_spec), experiment_timeout_minutes=15, delete_finished_experiment=False, ) return op
In the next cell, add and run:
@dsl.pipeline( name="mnist-katib-quick", description="Minimal pipeline: Katib Experiment for MNIST", ) def mnist_katib_quick_pipeline(): create_katib_experiment_task( experiment_name="mnist-katib-quick", experiment_namespace="<namespace_name>", training_steps=50, )
In the next cell, add and run:
client = kfp.Client() run = client.create_run_from_pipeline_func( mnist_katib_quick_pipeline, arguments={}, experiment_name="mnist-katib-quick-exp", namespace="<namespace_name>", ) run
Results
All commands run without errors.
At the end, there are links to the experiment and the pipeline.
The pipeline and experiment finish successfully.
Scenario 5. Attempt to launch Katib via a notebook in a namespace the user does not have access to
Description
Verify if the Profiles, RBAC, and PodDefault configuration actually isolates users to their namespace and prevents creating experiments in other people’s spaces. This is a critical test of the multi-tenancy configuration correctness in MLOps Studio and Katib.
Prerequisites
The logged-in user is working in their namespace.
The user is not an owner or contributor in another namespace, for example kubeflow-user-other-example-com, or another actually existing namespace in the cluster to which they have no permissions.
A PodDefault with the user’s token exists, just like in the positive scenario.
The pipeline name can remain the same, but the experiment name, if it exists, must be changed, for example by adding the forbidden suffix.
Steps
Create a MLOps Studio notebook in your namespace, as in the positive scenario.
In the first cell, add and run:
!pip install kfp==1.8.12 !pip install kubeflow-katib==0.13.0
In the next cell, add and run:
import kfp from kfp import dsl, components from kubeflow.katib import ( ApiClient, V1beta1ExperimentSpec, V1beta1AlgorithmSpec, V1beta1ObjectiveSpec, V1beta1ParameterSpec, V1beta1FeasibleSpace, V1beta1TrialTemplate, V1beta1TrialParameterSpec, )
In the next cell, paste the create_katib_experiment_task function without changes, as in the previous scenario, and run it.
In the next cell, define the pipeline, but deliberately set the wrong experiment namespace, different from the notebook’s namespace and without user permissions, for example:
@dsl.pipeline( name="mnist-katib-quick-forbidden", description="Negative pipeline: Katib Experiment in a foreign namespace", ) def mnist_katib_quick_pipeline_forbidden(): create_katib_experiment_task( experiment_name="mnist-katib-quick-forbidden", experiment_namespace="kubeflow-user-other-example-com", training_steps=50, )
In the next cell, run the pipeline in your namespace, just like before:
client = kfp.Client() run = client.create_run_from_pipeline_func( mnist_katib_quick_pipeline_forbidden, arguments={}, experiment_name="mnist-katib-quick-exp-forbidden", namespace="<namespace_name>", ) run
Go to the MLOps Studio Pipelines UI, open this Run and check:
the status of the katib-launcher component task,
logs of the katib-launcher pod, especially messages from the Katib and K8s server API.
Expected result (negative)
The notebook starts correctly. pip install cells, imports, and function definitions do not return errors.
The Run in MLOps Studio Pipelines fails. The katib-launcher step has a Failed status.
The katib-launcher step logs show a 403 / Forbidden error when trying to create a Katib experiment in a foreign namespace, for example:
User "system:serviceaccount:..." cannot create resource "experiments" in API group "kubeflow.org" in the namespace "kubeflow-user-other-example-com": Forbidden
In the namespace kubeflow-user-other-example-com, no Katib experiment named mnist-katib-quick-forbidden is created.
In your namespace, this experiment is also not created, because the attempt was made on another namespace.
No workarounds. The component does not automatically try to create the experiment in another namespace.