Skip to content

Commit

Permalink
Use general AttributesExportable
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanTingHsieh committed Dec 8, 2023
1 parent 097d31f commit 13dfce1
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 207 deletions.
2 changes: 1 addition & 1 deletion job_templates/sag_np_metrics/config_fed_client.conf
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
{
# we use this component so the client api `flare.init()` can get required information
id = "client_api_config_preparer"
path = "nvflare.app_common.widgets.client_api_configurator.ClientAPIConfigurator"
path = "nvflare.app_common.widgets.external_configurator.ExternalConfigurator"
args {
component_ids = ["metric_relay"]
}
Expand Down
4 changes: 2 additions & 2 deletions job_templates/sag_pt/config_fed_client.conf
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
},
{
# we use this component so the client api `flare.init()` can get required information
id = "client_api_config_preparer"
path = "nvflare.app_common.widgets.client_api_configurator.ClientAPIConfigurator"
id = "config_preparer"
path = "nvflare.app_common.widgets.external_configurator.ExternalConfigurator"
args {
component_ids = ["metric_relay"]
}
Expand Down
24 changes: 0 additions & 24 deletions nvflare/apis/client_api_exportable.py

This file was deleted.

34 changes: 27 additions & 7 deletions nvflare/app_common/executors/client_api_launcher_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from typing import Optional

from nvflare.apis.fl_context import FLContext
from nvflare.app_common.executors.launcher_executor import LauncherExecutor
from nvflare.app_common.widgets.client_api_configurator import ClientAPIConfigurator
from nvflare.client.config import ConfigKey, ExchangeFormat, TransferType
from nvflare.client.config import ConfigKey, ExchangeFormat, TransferType, write_config_to_file
from nvflare.client.constants import CLIENT_API_CONFIG
from nvflare.fuel.utils.attributes_exportable import ExportMode


class ClientAPILauncherExecutor(LauncherExecutor):
Expand All @@ -42,6 +44,7 @@ def __init__(
to_nvflare_converter_id: Optional[str] = None,
params_exchange_format: ExchangeFormat = ExchangeFormat.NUMPY,
params_transfer_type: TransferType = TransferType.FULL,
config_file_name: str = CLIENT_API_CONFIG,
) -> None:
"""Initializes the ClientAPILauncherExecutor.
Expand Down Expand Up @@ -69,6 +72,7 @@ def __init__(
params_exchange_format (ExchangeFormat): What format to exchange the parameters.
params_transfer_type (TransferType): How to transfer the parameters. FULL means the whole model parameters are sent.
DIFF means that only the difference is sent.
config_file_name (str): The config file name to write attributes into, the client api will read in this file.
"""
LauncherExecutor.__init__(
self,
Expand All @@ -93,12 +97,14 @@ def __init__(

self._params_exchange_format = params_exchange_format
self._params_transfer_type = params_transfer_type
self._config_file_name = config_file_name

def initialize(self, fl_ctx: FLContext) -> None:
self.prepare_config_for_launch(fl_ctx)
super().initialize(fl_ctx)

def prepare_config_for_launch(self, fl_ctx: FLContext):
pipe_export_class, pipe_export_args = self.pipe.export(ExportMode.PEER)
task_exchange_attributes = {
ConfigKey.TRAIN_WITH_EVAL: self._train_with_evaluation,
ConfigKey.EXCHANGE_FORMAT: self._params_exchange_format,
Expand All @@ -107,10 +113,24 @@ def prepare_config_for_launch(self, fl_ctx: FLContext):
ConfigKey.EVAL_TASK_NAME: self._evaluate_task_name,
ConfigKey.SUBMIT_MODEL_TASK_NAME: self._submit_model_task_name,
ConfigKey.PIPE_CHANNEL_NAME: self.get_pipe_channel_name(),
ConfigKey.PIPE_CLASS: ClientAPIConfigurator.get_external_pipe_class(self.get_pipe()),
ConfigKey.PIPE_ARGS: ClientAPIConfigurator.get_external_pipe_args(self.get_pipe()),
ConfigKey.PIPE: {
ConfigKey.CLASS_NAME: pipe_export_class,
ConfigKey.ARG: pipe_export_args,
},
}

ClientAPIConfigurator.write_config_to_file(
config_data={ConfigKey.TASK_EXCHANGE: task_exchange_attributes}, fl_ctx=fl_ctx
)
config_data = {
ConfigKey.TASK_EXCHANGE: task_exchange_attributes,
ConfigKey.SITE_NAME: fl_ctx.get_identity_name(),
ConfigKey.JOB_ID: fl_ctx.get_job_id(),
}

config_file_path = self._get_external_config_file_path(fl_ctx)
write_config_to_file(config_data=config_data, config_file_path=config_file_path)

def _get_external_config_file_path(self, fl_ctx: FLContext):
engine = fl_ctx.get_engine()
workspace = engine.get_workspace()
app_config_directory = workspace.get_app_config_dir(fl_ctx.get_job_id())
config_file_path = os.path.join(app_config_directory, self._config_file_name)
return config_file_path
124 changes: 0 additions & 124 deletions nvflare/app_common/widgets/client_api_configurator.py

This file was deleted.

68 changes: 68 additions & 0 deletions nvflare/app_common/widgets/external_configurator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (c) 2023, 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.

import os
from typing import List

from nvflare.apis.event_type import EventType
from nvflare.apis.fl_context import FLContext
from nvflare.client.config import ConfigKey, write_config_to_file
from nvflare.client.constants import CLIENT_API_CONFIG
from nvflare.fuel.utils.attributes_exportable import ExportMode, export_components
from nvflare.fuel.utils.validation_utils import check_object_type
from nvflare.widgets.widget import Widget


class ExternalConfigurator(Widget):
def __init__(
self,
component_ids: List[str],
config_file_name: str = CLIENT_API_CONFIG,
):
"""Prepares any external configuration files.
Args:
component_ids: A list of components that are `AttributesExportable`
config_file_name: The file name of the external config.
"""
super().__init__()
check_object_type("component_ids", component_ids, list)

# the components that needs to export attributes
self._component_ids = component_ids
self._config_file_name = config_file_name

def handle_event(self, event_type: str, fl_ctx: FLContext):
if event_type == EventType.ABOUT_TO_START_RUN:
components_data = self._export_all_components(fl_ctx)
components_data[ConfigKey.SITE_NAME] = fl_ctx.get_identity_name()
components_data[ConfigKey.JOB_ID] = fl_ctx.get_job_id()

config_file_path = self._get_external_config_file_path(fl_ctx)
write_config_to_file(config_data=components_data, config_file_path=config_file_path)

def _get_external_config_file_path(self, fl_ctx: FLContext):
engine = fl_ctx.get_engine()
workspace = engine.get_workspace()
app_config_directory = workspace.get_app_config_dir(fl_ctx.get_job_id())
config_file_path = os.path.join(app_config_directory, self._config_file_name)
return config_file_path

def _export_all_components(self, fl_ctx: FLContext) -> dict:
"""Exports all components."""
engine = fl_ctx.get_engine()
all_components = engine.get_all_components()
components = {i: all_components.get(i) for i in self._component_ids}
reserved_keys = [ConfigKey.SITE_NAME, ConfigKey.JOB_ID]
return export_components(components=components, reserved_keys=reserved_keys, export_mode=ExportMode.PEER)
18 changes: 11 additions & 7 deletions nvflare/app_common/widgets/metric_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nvflare.apis.client_api_exportable import ClientAPIExportable
from typing import Tuple

from nvflare.apis.dxo import DXO
from nvflare.apis.event_type import EventType
from nvflare.apis.fl_context import FLContext
from nvflare.apis.utils.analytix_utils import send_analytic_dxo
from nvflare.app_common.tracking.tracker_types import ANALYTIC_EVENT_TYPE
from nvflare.app_common.widgets.client_api_configurator import ClientAPIConfigurator
from nvflare.client.config import ConfigKey
from nvflare.fuel.utils.attributes_exportable import AttributesExportable
from nvflare.fuel.utils.constants import PipeChannelName
from nvflare.fuel.utils.pipe.pipe import Message, Pipe
from nvflare.fuel.utils.pipe.pipe_handler import PipeHandler
from nvflare.widgets.widget import Widget


class MetricRelay(Widget, ClientAPIExportable):
class MetricRelay(Widget, AttributesExportable):
def __init__(
self,
pipe_id: str,
Expand Down Expand Up @@ -82,10 +83,13 @@ def _pipe_msg_cb(self, msg: Message):
self.logger.error(f"bad metric data: expect DXO but got {type(msg.data)}")
send_analytic_dxo(self, msg.data, self._fl_ctx, self._event_type, fire_fed_event=True)

def export_for_client_api(self) -> dict:
def export(self, export_mode: str) -> Tuple[str, dict]:
pipe_export_class, pipe_export_args = self.pipe.export(export_mode)
config_dict = {
ConfigKey.PIPE_CHANNEL_NAME: self.pipe_channel_name,
ConfigKey.PIPE_CLASS: ClientAPIConfigurator.get_external_pipe_class(self.pipe),
ConfigKey.PIPE_ARGS: ClientAPIConfigurator.get_external_pipe_args(self.pipe),
ConfigKey.PIPE: {
ConfigKey.CLASS_NAME: pipe_export_class,
ConfigKey.ARG: pipe_export_args,
},
}
return {ConfigKey.METRICS_EXCHANGE: config_dict}
return ConfigKey.METRICS_EXCHANGE, config_dict
Loading

0 comments on commit 13dfce1

Please sign in to comment.