Skip to content

Commit 507a6ff

Browse files
Electronic-Wasteshashank-iitbhu
authored andcommitted
[GSoC] KEP for Project 6: Push-based Metrics Collection for Katib (kubeflow#2328)
* doc: initial commit of gsoc proposal(project 6). Signed-off-by: Electronic-Waste <[email protected]> * doc: complete KEP for gsoc proposal(Project 6). Signed-off-by: Electronic-Waste <[email protected]> * chore: add non-goals and examples. Signed-off-by: Electronic-Waste <[email protected]> * chore: add . Signed-off-by: Electronic-Waste <[email protected]> * chore: add compatibility changes in trial controller. Signed-off-by: Electronic-Waste <[email protected]> * chore: update architecture figure. Signed-off-by: Electronic-Waste <[email protected]> * chore: update format. Signed-off-by: Electronic-Waste <[email protected]> * chore: update doc after the review in 10th, June. Signed-off-by: Electronic-Waste <[email protected]> * chore: add code link and remove namespace env variable. Signed-off-by: Electronic-Waste <[email protected]> * chore: modify proposal after the review in 14th, June. Signed-off-by: Electronic-Waste <[email protected]> * chore: delete WIP label. Signed-off-by: Electronic-Waste <[email protected]> * chore: add timeout param into report_metrics. Signed-off-by: Electronic-Waste <[email protected]> * fix: metrics_collector_config spelling. Signed-off-by: Electronic-Waste <[email protected]> --------- Signed-off-by: Electronic-Waste <[email protected]>
1 parent cb096ed commit 507a6ff

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
52.6 KB
Loading
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Push-based Metrics Collection Proposal
2+
3+
## Links
4+
5+
- [katib/issues#577([Enhancement Request] Metrics Collector Push-based Implementation)](https://github.com/kubeflow/katib/issues/577)
6+
7+
## Motivation
8+
9+
[Katib](https://github.com/kubeflow/katib) is a Kubernetes-native project for automated machine learning (AutoML). It can not only tune hyperparameters of applications written in any language and natively supports many ML frameworks, but also supports features like early stopping and neural architecture search.
10+
11+
In the procedure of tuning hyperparameters, Metrics Collector, which is implemented as a sidecar container attached to each training container in the [current design](https://github.com/kubeflow/katib/blob/master/docs/proposals/metrics-collector.md), will collect training logs from Trials once the training is complete. Then, the Metrics Collector will parse training logs to get appropriate metrics like accuracy or loss and pass the evaluation results to the HyperParameter tuning algorithm.
12+
13+
However, current implementation of Metrics Collector is pull-based, raising some [design problems](https://github.com/kubeflow/training-operator/issues/722#issuecomment-405669269) such as determining the frequency we scrape the metrics, performance issues like the overhead caused by too many sidecar containers, and restrictions on developing environments which must support sidecar containers. Thus, we should implement a new API for Katib Python SDK to offer users a push-based way to store metrics directly into the Katib DB and resolve those issues raised by pull-based metrics collection.
14+
15+
![](../images/push-based-metrics-collection.png)
16+
17+
Fig.1 Architecture of the new design
18+
19+
### Goals
20+
1. **A new parameter in Python SDK function `tune`**: allow users to specify the method of collecting metrics(push-based/pull-based).
21+
22+
2. **A new interface `report_metrics` in Python SDK**: push the metrics to Katib DB directly.
23+
24+
3. The final metrics of worker pods should be **pushed to Katib DB directly** in the push mode of metrics collection.
25+
26+
### Non-Goals
27+
1. Implement authentication model for Katib DB to push metrics.
28+
29+
2. Support pushing data to different types of storage system(prometheus, self-defined interface etc.)
30+
31+
32+
## API
33+
34+
### New Parameter in Python SDK Function `tune`
35+
36+
We decided to add `metrics_collector_config` to `tune` function in Python SDK.
37+
38+
```Python
39+
def tune(
40+
self,
41+
name: str,
42+
objective: Callable,
43+
parameters: Dict[str, Any],
44+
base_image: str = constants.BASE_IMAGE_TENSORFLOW,
45+
namespace: Optional[str] = None,
46+
env_per_trial: Optional[Union[Dict[str, str], List[Union[client.V1EnvVar, client.V1EnvFromSource]]]] = None,
47+
algorithm_name: str = "random",
48+
algorithm_settings: Union[dict, List[models.V1beta1AlgorithmSetting], None] = None,
49+
objective_metric_name: str = None,
50+
additional_metric_names: List[str] = [],
51+
objective_type: str = "maximize",
52+
objective_goal: float = None,
53+
max_trial_count: int = None,
54+
parallel_trial_count: int = None,
55+
max_failed_trial_count: int = None,
56+
resources_per_trial: Union[dict, client.V1ResourceRequirements, None] = None,
57+
retain_trials: bool = False,
58+
packages_to_install: List[str] = None,
59+
pip_index_url: str = "https://pypi.org/simple",
60+
# The newly added parameter metrics_collector_config.
61+
# It specifies the config of metrics collector, for example,
62+
# metrics_collector_config={"kind": "Push"},
63+
metrics_collector_config: Dict[str, Any] = {"kind": "StdOut"},
64+
)
65+
```
66+
67+
### New Interface `report_metrics` in Python SDK
68+
69+
```Python
70+
"""Push Metrics Directly to Katib DB
71+
72+
[!!!] Trial name should always be passed into Katib Trials as env variable `KATIB_TRIAL_NAME`.
73+
74+
Args:
75+
metrics: Dict of metrics pushed to Katib DB.
76+
For examle, `metrics = {"loss": 0.01, "accuracy": 0.99}`.
77+
db-manager-address: Address for the Katib DB Manager in this format: `ip-address:port`.
78+
timeout: Optional, gRPC API Server timeout in seconds to report metrics.
79+
80+
Raises:
81+
RuntimeError: Unable to push Trial metrics to Katib DB.
82+
"""
83+
def report_metrics(
84+
metrics: Dict[str, Any],
85+
db_manager_address: str = constants.DEFAULT_DB_MANAGER_ADDRESS,
86+
timeout: int = constants.DEFAULT_TIMEOUT,
87+
)
88+
```
89+
90+
### A Simple Example:
91+
92+
```Python
93+
import kubeflow.katib as katib
94+
95+
# Step 1. Create an objective function with push-based metrics collection.
96+
def objective(parameters):
97+
# Import required packages.
98+
import kubeflow.katib as katib
99+
# Calculate objective function.
100+
result = 4 * int(parameters["a"]) - float(parameters["b"]) ** 2
101+
# Push metrics to Katib DB.
102+
katib.report_metrics({"result": result})
103+
104+
# Step 2. Create HyperParameter search space.
105+
parameters = {
106+
"a": katib.search.int(min=10, max=20),
107+
"b": katib.search.double(min=0.1, max=0.2)
108+
}
109+
110+
# Step 3. Create Katib Experiment with 12 Trials and 2 GPUs per Trial.
111+
katib_client = katib.KatibClient(namespace="kubeflow")
112+
name = "tune-experiment"
113+
katib_client.tune(
114+
name=name,
115+
objective=objective,
116+
parameters=parameters,
117+
objective_metric_name="result",
118+
max_trial_count=12,
119+
resources_per_trial={"gpu": "2"},
120+
metrics_collector_config={"kind": "Push"},
121+
)
122+
123+
# Step 4. Get the best HyperParameters.
124+
print(katib_client.get_optimal_hyperparameters(name))
125+
```
126+
127+
## Implementation
128+
129+
### Add New Parameter in `tune`
130+
131+
As mentioned above, we decided to add `metrics_collector_config` to the tune function in Python SDK. Also, we have some changes to be made:
132+
133+
1. Configure the way of metrics collection: set the configuration `spec.metricsCollectionSpec.collector.kind`(specify the way of metrics collection) to `Push`.
134+
135+
2. Rename metrics collector from `None` to `Push`: It's not correct to call push-based metrics collection `None`. We should modify related code to rename it.
136+
137+
3. Write env variables into Trial spec: set `KATIB_TRIAL_NAME` for `report_metrics` function to dial db manager.
138+
139+
### New Interface `report_metrics` in Python SDK
140+
141+
We decide to implement this funcion to push metrics directly to Katib DB with the help of grpc. Trial name should always be passed into Katib Trials (and then into this function) as env variable `KATIB_TRIAL_NAME`.
142+
143+
Also, the function is supposed to be implemented as **global function** because it is called in the user container.
144+
145+
Steps:
146+
147+
1. Wrap metrics into `katib_api_pb2.ReportObservationLogRequest`:
148+
149+
Firstly, convert metrics (in dict format) into `katib_api_pb2.ReportObservationLogRequest` type for the following grpc call, referring to https://github.com/kubeflow/katib/blob/master/pkg/apis/manager/v1beta1/gen-doc/api.md#reportobservationlogrequest
150+
151+
2. Dial Katib DBManager Service
152+
153+
We'll create a DBManager Stub and make a grpc call to report metrics to Katib DB.
154+
155+
### Compatibility Changes in Trial Controller
156+
157+
We need to make appropriate changes in the Trial controller to make sure we insert unavailable value into Katib DB, if user doesn't report metric accidentally. The current implementation handles unavailable metrics in:
158+
159+
```Golang
160+
// If observation is empty metrics collector doesn't finish.
161+
// For early stopping metrics collector are reported logs before Trial status is changed to EarlyStopped.
162+
if jobStatus.Condition == trialutil.JobSucceeded && instance.Status.Observation == nil {
163+
logger.Info("Trial job is succeeded but metrics are not reported, reconcile requeued")
164+
return errMetricsNotReported
165+
}
166+
```
167+
1. Distinguish pull-based and push-based metrics collection
168+
169+
We decide to add a if-else statement in the code above to distinguish pull-based and push-based metrics collection. In the push-based collection, the Trial does not need to be requeued. Instead, we'll insert a unavailable value to Katib DB.
170+
171+
2. Update the status of Trial to `MetricsUnavailable`
172+
173+
In the current implementation of pull-based metrics collection, Trials will be re-queued when the metrics collector finds the `.Status.Observation` is empty. However, it's not compatible with push-based metrics collection because the forgotten metrics won't be reported in the new round of reconcile. So, we need to update its status in the function `UpdateTrialStatusCondition` in accommodation with the pull-based metrics collection. The following code will be insert into lines before [trial_controller_util.go#L69](https://github.com/kubeflow/katib/blob/7959ffd54851216dbffba791e1da13c8485d1085/pkg/controller.v1beta1/trial/trial_controller_util.go#L69)
174+
175+
176+
```Golang
177+
else if instance.Spec.MetricCollector.Collector.Kind == "Push" {
178+
... // Update the status of this Trial to `MetricsUnavailable` and output the reason.
179+
}
180+
```
181+
182+
### Collection of Final Metrics
183+
184+
The final metrics of worker pods should be pushed to Katib DB directly in the push mode of metrics collection.

0 commit comments

Comments
 (0)