Skip to content
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

FLModel summary #2544

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions nvflare/app_common/abstract/fl_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,36 @@ def __init__(
else:
meta = {}
self.meta = meta
self._summary: dict = {}

def __str__(self):
return (
f"FLModel(params:{self.params}, params_type: {self.params_type},"
f" optimizer_params: {self.optimizer_params}, metrics: {self.metrics},"
f" current_round: {self.current_round}, meta: {self.meta})"
def _add_to_summary(self, kvs: Dict):
for key, value in kvs.items():
if value:
if isinstance(value, dict):
self._summary[key] = len(value)
elif isinstance(value, ParamsType):
self._summary[key] = value
elif isinstance(value, int):
self._summary[key] = value
else:
self._summary[key] = type(value)

def summary(self):
kvs = dict(
params=self.params,
optimizer_params=self.optimizer_params,
metrics=self.metrics,
meta=self.meta,
params_type=self.params_type,
start_round=self.start_round,
current_round=self.current_round,
total_rounds=self.total_rounds,
)
self._add_to_summary(kvs)
return self._summary

def __repr__(self):
return str(self.summary())

def __str__(self):
return str(self.summary())
13 changes: 13 additions & 0 deletions tests/unit_test/app_common/abstract/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2024, NVIDIA CORPORATION. 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.
34 changes: 34 additions & 0 deletions tests/unit_test/app_common/abstract/fl_model_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2024, NVIDIA CORPORATION. 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.

from nvflare.app_common.abstract.fl_model import FLModel, ParamsType


class TestFLModel:
model = FLModel(
params_type=ParamsType.FULL,
params={"a": 100, "b": 200, "c": {"c1": 100}},
optimizer_params={},
metrics={"loss": 100, "accuracy": 0.9},
start_round=1,
current_round=100,
total_rounds=12000,
)
summary = model.summary()
assert summary["params"] == 3
assert summary["metrics"] == 2
assert summary["params_type"] == ParamsType.FULL
assert summary["start_round"] == 1
assert summary["current_round"] == 100
assert summary["total_rounds"] == 12000
Loading