-
Notifications
You must be signed in to change notification settings - Fork 485
[GSoC] New Interface report_metrics
in Python SDK
#2371
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
Merged
google-oss-prow
merged 7 commits into
kubeflow:master
from
Electronic-Waste:feat/report_metrics
Jul 5, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d9be47
chore: add report_metrics.
Electronic-Waste 01d3e2d
fix: modify the code according to the first review.
Electronic-Waste 3fb2e1d
chore: add validation for metrics value & rename katib_report_metrics…
Electronic-Waste 609ef67
fix: update import path in __init__.py.
Electronic-Waste af69f36
fix: delete blank line.
Electronic-Waste ec8431f
fix: update RuntimeError doc string & correct spelling error & add ne…
Electronic-Waste fa1866b
fix: delete blank in the last line.
Electronic-Waste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,85 @@ | ||
# Copyright 2024 The Kubeflow Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from datetime import datetime, timezone | ||
from typing import Any, Dict | ||
|
||
import grpc | ||
import kubeflow.katib.katib_api_pb2 as katib_api_pb2 | ||
from kubeflow.katib.constants import constants | ||
from kubeflow.katib.utils import utils | ||
|
||
def report_metrics( | ||
metrics: Dict[str, Any], | ||
db_manager_address: str = constants.DEFAULT_DB_MANAGER_ADDRESS, | ||
timeout: int = constants.DEFAULT_TIMEOUT, | ||
): | ||
"""Push Metrics Directly to Katib DB | ||
|
||
Katib always pass Trial name as env variable `KATIB_TRIAL_NAME` to the training container. | ||
|
||
Args: | ||
metrics: Dict of metrics pushed to Katib DB. | ||
For examle, `metrics = {"loss": 0.01, "accuracy": 0.99}`. | ||
db-manager-address: Address for the Katib DB Manager in this format: `ip-address:port`. | ||
timeout: Optional, gRPC API Server timeout in seconds to report metrics. | ||
|
||
Raises: | ||
ValueError: The Trial name is not passed to environment variables. | ||
Electronic-Waste marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RuntimeError: Unable to push Trial metrics to Katib DB. | ||
""" | ||
|
||
# Get Trial's namespace and name | ||
namespace = utils.get_current_k8s_namespace() | ||
name = os.getenv("KATIB_TRIAL_NAME") | ||
if name is None: | ||
raise ValueError( | ||
"The Trial name is not passed to environment variables" | ||
) | ||
|
||
# Get channel for grpc call to db manager | ||
db_manager_address = db_manager_address.split(":") | ||
channel = grpc.beta.implementations.insecure_channel( | ||
db_manager_address[0], int(db_manager_address[1]) | ||
) | ||
|
||
# Validate metrics value in dict | ||
for value in metrics.values(): | ||
utils.validate_metrics_value(value) | ||
|
||
# Dial katib db manager to report metrics | ||
with katib_api_pb2.beta_create_DBManager_stub(channel) as client: | ||
try: | ||
timestamp = datetime.now(timezone.utc).strftime(constants.RFC3339_FORMAT) | ||
client.ReportObservationLog( | ||
request=katib_api_pb2.ReportObservationLogRequest( | ||
trial_name=name, | ||
observation_logs=katib_api_pb2.ObservationLog( | ||
metric_logs=[ | ||
katib_api_pb2.MetricLog( | ||
time_stamp=timestamp, | ||
metric=katib_api_pb2.Metric(name=name,value=str(value)) | ||
) | ||
for name, value in metrics.items() | ||
] | ||
) | ||
), | ||
timeout=timeout, | ||
) | ||
except Exception as e: | ||
raise RuntimeError( | ||
f"Unable to push metrics to Katib DB for Trial {namespace}/{name}. Exception: {e}" | ||
) | ||
|
||
Electronic-Waste marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.