-
Notifications
You must be signed in to change notification settings - Fork 1k
Add MetricsRecord
#2802
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
Add MetricsRecord
#2802
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a749a0e
v0
jafermarq 29d1a41
w/ previous
jafermarq b399c38
new `parametersrecord.py`; ranamed `Tensor`->`Array`; more
jafermarq 44de0e7
updates
jafermarq d7b23db
`MetricsRecord` init with tests
jafermarq 7ed2b82
w/ previous
jafermarq 46e1f3a
fix
jafermarq 092a74b
merge w/ main; tweaks
jafermarq 32b1155
better tests; definitions in typing
jafermarq 36d3f3b
Merge branch 'main' into add-metricsrecord
jafermarq d3316f8
double space top of file
jafermarq a79aab8
w/ previous
jafermarq 22d48f4
no `str` in `MetricsRecords` values
jafermarq 376ffb8
fix docstrings
jafermarq 3a62bb5
Merge branch 'main' into add-metricsrecord
jafermarq b18b05d
more info in TypeError messsage
jafermarq 12ed920
updates; more tests
jafermarq f9804a6
tweak
jafermarq 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2024 Flower Labs GmbH. All Rights Reserved. | ||
# | ||
# 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. | ||
# ============================================================================== | ||
"""MetricsRecord.""" | ||
|
||
|
||
from dataclasses import dataclass, field | ||
from typing import Dict, Optional, get_args | ||
|
||
from .typing import MetricsRecordValues, MetricsScalar | ||
|
||
|
||
@dataclass | ||
class MetricsRecord: | ||
"""Metrics record.""" | ||
|
||
keep_input: bool | ||
data: Dict[str, MetricsRecordValues] = field(default_factory=dict) | ||
|
||
def __init__( | ||
self, | ||
metrics_dict: Optional[Dict[str, MetricsRecordValues]] = None, | ||
keep_input: bool = True, | ||
): | ||
"""Construct a MetricsRecord object. | ||
|
||
Parameters | ||
---------- | ||
metrics_dict : Optional[Dict[str, MetricsRecordValues]] | ||
A dictionary that stores basic types (i.e. `int`, `float` as defined | ||
in `MetricsScalar`) and list of such types (see `MetricsScalarList`). | ||
keep_input : bool (default: True) | ||
A boolean indicating whether metrics should be deleted from the input | ||
dictionary immediately after adding them to the record. When set | ||
to True, the data is duplicated in memory. If memory is a concern, set | ||
it to False. | ||
""" | ||
self.keep_input = keep_input | ||
self.data = {} | ||
if metrics_dict: | ||
self.set_metrics(metrics_dict) | ||
|
||
def set_metrics(self, metrics_dict: Dict[str, MetricsRecordValues]) -> None: | ||
"""Add metrics to the record. | ||
|
||
Parameters | ||
---------- | ||
metrics_dict : Dict[str, MetricsRecordValues] | ||
A dictionary that stores basic types (i.e. `int`, `float` as defined | ||
in `MetricsScalar`) and list of such types (see `MetricsScalarList`). | ||
""" | ||
if any(not isinstance(k, str) for k in metrics_dict.keys()): | ||
raise TypeError(f"Not all keys are of valid type. Expected {str}.") | ||
|
||
def is_valid(value: MetricsScalar) -> None: | ||
"""Check if value is of expected type.""" | ||
if not isinstance(value, get_args(MetricsScalar)): | ||
raise TypeError( | ||
"Not all values are of valid type." | ||
f" Expected {MetricsRecordValues} but you passed {type(value)}." | ||
) | ||
|
||
# Check types of values | ||
# Split between those values that are list and those that aren't | ||
# then process in the same way | ||
for value in metrics_dict.values(): | ||
if isinstance(value, list): | ||
# If your lists are large (e.g. 1M+ elements) this will be slow | ||
# 1s to check 10M element list on a M2 Pro | ||
# In such settings, you'd be better of treating such metric as | ||
# an array and pass it to a ParametersRecord. | ||
for list_value in value: | ||
is_valid(list_value) | ||
else: | ||
is_valid(value) | ||
|
||
# Add metrics to record | ||
if self.keep_input: | ||
# Copy | ||
self.data = metrics_dict.copy() | ||
else: | ||
# Add entries to dataclass without duplicating memory | ||
for key in list(metrics_dict.keys()): | ||
self.data[key] = metrics_dict[key] | ||
del metrics_dict[key] |
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
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.