|
| 1 | +# Copyright 2022 The Orbit Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Provides a utility class for managing summary writing.""" |
| 16 | + |
| 17 | +import abc |
| 18 | + |
| 19 | + |
| 20 | +class SummaryManagerInterface(abc.ABC): |
| 21 | + """A utility interface for managing summary writing.""" |
| 22 | + |
| 23 | + @abc.abstractmethod |
| 24 | + def flush(self): |
| 25 | + """Flushes the the recorded summaries.""" |
| 26 | + raise NotImplementedError |
| 27 | + |
| 28 | + @abc.abstractmethod |
| 29 | + def summary_writer(self, relative_path=""): |
| 30 | + """Returns the underlying summary writer for scoped writers.""" |
| 31 | + raise NotImplementedError |
| 32 | + |
| 33 | + @abc.abstractmethod |
| 34 | + def write_summaries(self, summary_dict): |
| 35 | + """Writes summaries for the given dictionary of values. |
| 36 | +
|
| 37 | + The summary_dict can be any nested dict. The SummaryManager should |
| 38 | + recursively creates summaries, yielding a hierarchy of summaries which will |
| 39 | + then be reflected in the corresponding UIs. |
| 40 | +
|
| 41 | + For example, users may evaluate on multiple datasets and return |
| 42 | + `summary_dict` as a nested dictionary: |
| 43 | +
|
| 44 | + { |
| 45 | + "dataset1": { |
| 46 | + "loss": loss1, |
| 47 | + "accuracy": accuracy1 |
| 48 | + }, |
| 49 | + "dataset2": { |
| 50 | + "loss": loss2, |
| 51 | + "accuracy": accuracy2 |
| 52 | + }, |
| 53 | + } |
| 54 | +
|
| 55 | + This will create two set of summaries, "dataset1" and "dataset2". Each |
| 56 | + summary dict will contain summaries including both "loss" and "accuracy". |
| 57 | +
|
| 58 | + Args: |
| 59 | + summary_dict: A dictionary of values. If any value in `summary_dict` is |
| 60 | + itself a dictionary, then the function will create a new summary_dict |
| 61 | + with name given by the corresponding key. This is performed recursively. |
| 62 | + Leaf values are then summarized using the parent relative path. |
| 63 | + """ |
| 64 | + raise NotImplementedError |
0 commit comments