Skip to content

Commit

Permalink
Add event_type to AnalyticsSender (NVIDIA#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanTingHsieh authored Mar 18, 2022
1 parent 3b8aadf commit 1d3169e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{
"id": "analytic_sender",
"name": "AnalyticsSender",
"args": {}
"args": {"event_type": "analytix_log_stats"}
},
{
"id": "tb_analytics_receive",
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-pt-tb/config/config_fed_client.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{
"id": "analytic_sender",
"name": "AnalyticsSender",
"args": {}
"args": {"event_type": "analytix_log_stats"}
},
{
"id": "event_to_fed",
Expand Down
3 changes: 0 additions & 3 deletions nvflare/app_common/app_event_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,3 @@ class AppEventType(object):
CROSS_VAL_INIT = "_cross_val_init"
VALIDATION_RESULT_RECEIVED = "_validation_result_received"
RECEIVE_BEST_MODEL = "_receive_best_model"

ANALYTIC_EVENT_TYPE = "analytix_log_stats"
LOGGING_EVENT_TYPE = "analytix_logging"
22 changes: 13 additions & 9 deletions nvflare/app_common/widgets/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
from nvflare.apis.fl_constant import EventScope, FLContextKey, ReservedKey
from nvflare.apis.fl_context import FLContext
from nvflare.apis.shareable import Shareable
from nvflare.app_common.app_event_type import AppEventType
from nvflare.widgets.widget import Widget

ANALYTIC_EVENT_TYPE = "analytix_log_stats"

def send_analytic_dxo(
comp: FLComponent, dxo: DXO, fl_ctx: FLContext, event_type: str = AppEventType.ANALYTIC_EVENT_TYPE
):

def send_analytic_dxo(comp: FLComponent, dxo: DXO, fl_ctx: FLContext, event_type: str = ANALYTIC_EVENT_TYPE):
"""Sends analytic dxo.
Args:
Expand Down Expand Up @@ -67,14 +66,19 @@ def create_analytic_dxo(tag: str, value, data_type: AnalyticsDataType, **kwargs)


class AnalyticsSender(Widget):
def __init__(self):
def __init__(self, event_type=ANALYTIC_EVENT_TYPE):
"""Sends analytics data.
This class implements some common methods follows signatures from PyTorch SummaryWriter.
It provides a convenient way for Learner to use.
Note::
This class implements some common methods follows signatures from PyTorch SummaryWriter.
It provides a convenient way for Learner to use.
Args:
event_type (str): event type to fire.
"""
super().__init__()
self.engine = None
self.event_type = event_type

def handle_event(self, event_type: str, fl_ctx: FLContext):
if event_type == EventType.START_RUN:
Expand All @@ -95,7 +99,7 @@ def _add(
kwargs["global_step"] = global_step
dxo = create_analytic_dxo(tag=tag, value=value, data_type=data_type, **kwargs)
with self.engine.new_context() as fl_ctx:
send_analytic_dxo(self, dxo=dxo, fl_ctx=fl_ctx)
send_analytic_dxo(self, dxo=dxo, fl_ctx=fl_ctx, event_type=self.event_type)

def add_scalar(self, tag: str, scalar: float, global_step: Optional[int] = None, **kwargs):
"""Sends a scalar.
Expand Down Expand Up @@ -163,7 +167,7 @@ def __init__(self, events: Optional[List[str]] = None):
"""
super().__init__()
if events is None:
events = [AppEventType.ANALYTIC_EVENT_TYPE, f"fed.{AppEventType.ANALYTIC_EVENT_TYPE}"]
events = [ANALYTIC_EVENT_TYPE, f"fed.{ANALYTIC_EVENT_TYPE}"]
self.events = events
self._save_lock = Lock()
self._end = False
Expand Down

0 comments on commit 1d3169e

Please sign in to comment.