-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
aggregator_base.py
84 lines (63 loc) · 3.16 KB
/
aggregator_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from federatedml.framework.homo.blocks import ServerCommunicator, ClientCommunicator
class AutoSuffix(object):
"""
A auto suffix that will auto increase count
"""
def __init__(self, suffix_str=""):
self._count = 0
self.suffix_str = suffix_str
def __call__(self):
concat_suffix = self.suffix_str + "_" + str(self._count)
self._count += 1
return concat_suffix
class AggregatorBaseClient(object):
def __init__(self, communicate_match_suffix: str = None):
"""Base class of client aggregator
Parameters
----------
communicate_match_suffix : str, you can give a unique name to aggregator, to avoid reusing of same transfer variable tag,
To make sure that client and server can communicate correctly,
the server-side and client-side aggregators need to have the same suffix
"""
self.communicator = ClientCommunicator(prefix=communicate_match_suffix)
self.suffix = {}
def _get_suffix(self, var_name, user_suffix=tuple()):
assert var_name in self.suffix, 'var name {} not found in suffix list'.format(
var_name)
if user_suffix is not None and not isinstance(user_suffix, tuple):
raise ValueError('suffix must be None, tuples contains str or number. got {} whose type is {}'.format(
user_suffix, type(user_suffix)))
if user_suffix is None or len(user_suffix) == 0:
return self.suffix[var_name]()
else:
return (var_name, ) + user_suffix
def send(self, obj, suffix):
self.communicator.send_obj(obj, suffix=suffix)
def get(self, suffix):
return self.communicator.get_obj(suffix=suffix)
class AggregatorBaseServer(object):
def __init__(self, communicate_match_suffix=None):
"""Base class of server aggregator
Parameters
----------
communicate_match_suffix : str, you can give a unique name to aggregator, to avoid reusing of same transfer variable tag,
To make sure that client and server can communicate correctly,
the server-side and client-side aggregators need to have the same suffix
"""
self.communicator = ServerCommunicator(prefix=communicate_match_suffix)
self.suffix = {}
def _get_suffix(self, var_name, user_suffix=tuple()):
assert var_name in self.suffix, 'var name {} not found in suffix list'.format(
var_name)
if user_suffix is not None and not isinstance(user_suffix, tuple):
raise ValueError('suffix must be None, tuples contains str or number. got {} whose type is {}'.format(
user_suffix, type(user_suffix)))
if user_suffix is None or len(user_suffix) == 0:
return self.suffix[var_name]()
else:
return (var_name, ) + user_suffix
def broadcast(self, obj, suffix, party_idx=-1):
self.communicator.broadcast_obj(obj, suffix=suffix, party_idx=party_idx)
def collect(self, suffix, party_idx=-1):
objs = self.communicator.get_obj(suffix=suffix, party_idx=party_idx)
return objs