-
Notifications
You must be signed in to change notification settings - Fork 25
[29] Logging in json format #68
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
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
614ad74
work
tjhunter 3cf38e4
work
tjhunter ce55c18
changes
tjhunter 22701ce
Merge branch 'develop' into tjh/dev/29-logging
tjhunter e4d1b57
changes
tjhunter 3241135
Merge branch 'tjh/dev/29-logging' of github.com:ecmwf/WeatherGenerato…
tjhunter 3a3977c
Merge remote-tracking branch 'origin' into tjh/dev/29-logging
tjhunter 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 |
---|---|---|
|
@@ -8,6 +8,10 @@ | |
# nor does it submit to any jurisdiction. | ||
|
||
import datetime | ||
import json | ||
import math | ||
import os.path | ||
import time | ||
|
||
import numpy as np | ||
|
||
|
@@ -19,33 +23,65 @@ class TrainLogger: | |
def __init__(self, cf, path_run) -> None: | ||
self.cf = cf | ||
self.path_run = path_run | ||
# TODO: add header with col names (loadtxt has an option to skip k header lines) | ||
|
||
def log_metrics(self, metrics: dict[str, float]) -> None: | ||
""" | ||
Log metrics to a file. | ||
For now, just scalar values are expected. There is no check. | ||
""" | ||
# Clean all the metrics to convert to float. Any other type (numpy etc.) will trigger a serialization error. | ||
clean_metrics = { | ||
"weathergen.timestamp": time.time_ns() // 1_000_000, | ||
"weathergen.time": int(datetime.datetime.now().strftime("%Y%m%d%H%M%S")), | ||
} | ||
for key, value in metrics.items(): | ||
v = float(value) | ||
if math.isnan(v) or math.isinf(v): | ||
v = str(v) | ||
clean_metrics[key] = v | ||
|
||
# TODO: performance: we repeatedly open the file for each call. Better for multiprocessing | ||
# but we can probably do better and rely for example on the logging module. | ||
with open(os.path.join(self.path_run, "metrics.json"), "ab") as f: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest that we start with this simple version, we can always improve performance if it turns out to be a bottleneck |
||
s = json.dumps(clean_metrics) + "\n" | ||
f.write(s.encode("utf-8")) | ||
|
||
####################################### | ||
def add_train(self, samples, lr, loss_avg, stddev_avg, perf_gpu=0.0, perf_mem=0.0) -> None: | ||
""" | ||
Log training data | ||
""" | ||
|
||
metrics = dict(num_samples=samples) | ||
|
||
log_vals = [int(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))] | ||
log_vals += [samples] | ||
|
||
metrics["loss_avg_0_mean"] = loss_avg[0].mean() | ||
metrics["learning_rate"] = lr | ||
log_vals += [loss_avg[0].mean()] | ||
log_vals += [lr] | ||
|
||
for i_obs, _rt in enumerate(self.cf.streams): | ||
for j, _ in enumerate(self.cf.loss_fcts): | ||
metrics[f"stream_{i_obs}.loss_{j}.loss_avg"] = loss_avg[j, i_obs] | ||
log_vals += [loss_avg[j, i_obs]] | ||
if len(stddev_avg) > 0: | ||
for i_obs, _rt in enumerate(self.cf.streams): | ||
log_vals += [stddev_avg[i_obs]] | ||
metrics[f"stream_{i_obs}.stddev_avg"] = stddev_avg[i_obs] | ||
|
||
with open(self.path_run + self.cf.run_id + "_train_log.txt", "ab") as f: | ||
np.savetxt(f, log_vals) | ||
|
||
log_vals = [] | ||
log_vals += [perf_gpu] | ||
log_vals += [perf_mem] | ||
if perf_gpu > 0.0: | ||
metrics["perf.gpu"] = perf_gpu | ||
if perf_mem > 0.0: | ||
metrics["perf.memory"] = perf_mem | ||
self.log_metrics(metrics) | ||
with open(self.path_run + self.cf.run_id + "_perf_log.txt", "ab") as f: | ||
np.savetxt(f, log_vals) | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to make this change to use uv on the hpc2020 cluster. I am not sure if this is going to be a breaking change for people. @clessig , do we assume that different HPCs can use different versions of CUDA? That sounds like a nightmare.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not assume it, we know it ;) One can write a script that detects the available CUDA (and the python version if this is a variable) and then assembles the string that defines the wheel to be downloaded. @tjhunter : To what extent could one integrate this into pyproject toml?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And could we open an issues to track this? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the script in branch of the private repo but not committed yet:
#57