Skip to content

Federation API

Low level api

Attributes

__all__ = ['FederationABC'] module-attribute

Classes

FederationABC

federation, get or remote objects and tables

Attributes

session_id: str property abstractmethod

Functions

get(name, tag, parties, gc) abstractmethod

get objects/tables from parties

Parameters:

Name Type Description Default
name str

name of transfer variable

required
tag str

tag to distinguish each transfer

required
parties typing.List[Party]

parties to get objects/tables from

required
gc GarbageCollectionABC

used to do some clean jobs

required

Returns:

Type Description
list

a list of object or a list of table get from parties with same order of parties

Source code in python/fate_arch/abc/_federation.py
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
@abc.abstractmethod
def get(self, name: str,
        tag: str,
        parties: typing.List[Party],
        gc: GarbageCollectionABC) -> typing.List:
    """
    get objects/tables from ``parties``

    Parameters
    ----------
    name: str
       name of transfer variable
    tag: str
       tag to distinguish each transfer
    parties: typing.List[Party]
       parties to get objects/tables from
    gc: GarbageCollectionABC
       used to do some clean jobs

    Returns
    -------
    list
       a list of object or a list of table get from parties with same order of `parties`

    """
    ...
remote(v, name, tag, parties, gc) abstractmethod

remote object/table to parties

Parameters:

Name Type Description Default
v

object/table to remote

required
name str

name of transfer variable

required
tag str

tag to distinguish each transfer

required
parties typing.List[Party]

parties to remote object/table to

required
gc GarbageCollectionABC

used to do some clean jobs

required

Returns:

Type Description
Notes
Source code in python/fate_arch/abc/_federation.py
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
@abc.abstractmethod
def remote(self, v,
           name: str,
           tag: str,
           parties: typing.List[Party],
           gc: GarbageCollectionABC):
    """
    remote object/table to ``parties``

    Parameters
    ----------
    v: object or table
       object/table to remote
    name: str
       name of transfer variable
    tag: str
       tag to distinguish each transfer
    parties: typing.List[Party]
       parties to remote object/table to
    gc: GarbageCollectionABC
       used to do some clean jobs

    Returns
    -------
    Notes
    """
    ...
destroy(parties) abstractmethod

destroy federation from parties

Parameters:

Name Type Description Default
parties

parties to get objects/tables from

required

Returns:

Type Description
None
Source code in python/fate_arch/abc/_federation.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@abc.abstractmethod
def destroy(self, parties):
    """
    destroy federation from ``parties``

    Parameters
    ----------
    parties: typing.List[Party]
       parties to get objects/tables from

    Returns
    -------
    None
    """
    ...

user api

remoting or getting an object(table) from other parties is quite easy using apis provided in Variable. First to create an instance of BaseTransferVariable, which is simply a collection of Variables:

from federatedml.transfer_variable.transfer_class import secure_add_example_transfer_variable
variable = secure_add_example_transfer_variable.SecureAddExampleTransferVariable()

Then remote or get object(table) by variable provided by this instance:

# remote
variable.guest_share.remote("from guest")

# get
variable.guest_share.get()

Attributes

__all__ = ['Variable', 'BaseTransferVariables'] module-attribute

LOGGER = getLogger() module-attribute

Classes

FederationTagNamespace

Bases: object

Attributes

__namespace = 'default' instance-attribute class-attribute

Functions

set_namespace(namespace) classmethod
Source code in python/fate_arch/federation/transfer_variable.py
33
34
35
@classmethod
def set_namespace(cls, namespace):
    cls.__namespace = namespace
generate_tag(*suffix) classmethod
Source code in python/fate_arch/federation/transfer_variable.py
37
38
39
40
@classmethod
def generate_tag(cls, *suffix):
    tags = (cls.__namespace, *map(str, suffix))
    return ".".join(tags)

Variable(name, src, dst)

Bases: object

variable to distinguish federation by name

Source code in python/fate_arch/federation/transfer_variable.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def __init__(
    self, name: str, src: typing.Tuple[str, ...], dst: typing.Tuple[str, ...]
):

    if name in self.__instances:
        raise RuntimeError(
            f"{self.__instances[name]} with {name} already initialized, which expected to be an singleton object."
        )

    assert (
        len(name.split(".")) >= 3
    ), "incorrect name format, should be `module_name.class_name.variable_name`"
    self._name = name
    self._src = src
    self._dst = dst
    self._get_gc = IterationGC()
    self._remote_gc = IterationGC()
    self._use_short_name = True
    self._short_name = self._get_short_name(self._name)

Attributes

__instances: typing.MutableMapping[str, Variable] = {} instance-attribute class-attribute

Functions

get_or_create(name, create_func) classmethod
Source code in python/fate_arch/federation/transfer_variable.py
50
51
52
53
54
55
56
57
@classmethod
def get_or_create(
    cls, name, create_func: typing.Callable[[], "Variable"]
) -> "Variable":
    if name not in cls.__instances:
        value = create_func()
        cls.__instances[name] = value
    return cls.__instances[name]
__copy__()
Source code in python/fate_arch/federation/transfer_variable.py
86
87
def __copy__(self):
    return self
__deepcopy__(memo)
Source code in python/fate_arch/federation/transfer_variable.py
90
91
def __deepcopy__(self, memo):
    return self
set_preserve_num(n)
Source code in python/fate_arch/federation/transfer_variable.py
93
94
95
96
def set_preserve_num(self, n):
    self._get_gc.set_capacity(n)
    self._remote_gc.set_capacity(n)
    return self
disable_auto_clean()
Source code in python/fate_arch/federation/transfer_variable.py
 98
 99
100
101
def disable_auto_clean(self):
    self._get_gc.disable()
    self._remote_gc.disable()
    return self
clean()
Source code in python/fate_arch/federation/transfer_variable.py
103
104
105
def clean(self):
    self._get_gc.clean()
    self._remote_gc.clean()
remote_parties(obj, parties, suffix=tuple())

remote object to specified parties

Parameters:

Name Type Description Default
obj

object or table to remote

required
parties Union[typing.List[Party], Party]

parties to remote object/table to

required
suffix Union[typing.Any, typing.Tuple]

suffix used to distinguish federation with in variable

tuple()

Returns:

Type Description
None
Source code in python/fate_arch/federation/transfer_variable.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def remote_parties(
    self,
    obj,
    parties: Union[typing.List[Party], Party],
    suffix: Union[typing.Any, typing.Tuple] = tuple(),
):
    """
    remote object to specified parties

    Parameters
    ----------
    obj: object or table
       object or table to remote
    parties: typing.List[Party]
       parties to remote object/table to
    suffix: str or tuple of str
       suffix used to distinguish federation with in variable

    Returns
    -------
    None
    """
    from fate_arch.session import get_session

    session = get_session()
    if isinstance(parties, Party):
        parties = [parties]
    if not isinstance(suffix, tuple):
        suffix = (suffix,)
    tag = FederationTagNamespace.generate_tag(*suffix)

    for party in parties:
        if party.role not in self._dst:
            raise RuntimeError(
                f"not allowed to remote object to {party} using {self._name}"
            )
    local = session.parties.local_party.role
    if local not in self._src:
        raise RuntimeError(
            f"not allowed to remote object from {local} using {self._name}"
        )

    name = self._short_name if self._use_short_name else self._name

    timer = profile.federation_remote_timer(name, self._name, tag, local, parties)
    session.federation.remote(
        v=obj, name=name, tag=tag, parties=parties, gc=self._remote_gc
    )
    timer.done(session.federation)

    self._remote_gc.gc()
get_parties(parties, suffix=tuple())

get objects/tables from specified parties

Parameters:

Name Type Description Default
parties Union[typing.List[Party], Party]

parties to remote object/table to

required
suffix Union[typing.Any, typing.Tuple]

suffix used to distinguish federation with in variable

tuple()

Returns:

Type Description
list

a list of objects/tables get from parties with same order of parties

Source code in python/fate_arch/federation/transfer_variable.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def get_parties(
    self,
    parties: Union[typing.List[Party], Party],
    suffix: Union[typing.Any, typing.Tuple] = tuple(),
):
    """
    get objects/tables from specified parties

    Parameters
    ----------
    parties: typing.List[Party]
       parties to remote object/table to
    suffix: str or tuple of str
       suffix used to distinguish federation with in variable

    Returns
    -------
    list
       a list of objects/tables get from parties with same order of ``parties``

    """
    from fate_arch.session import get_session

    session = get_session()
    if not isinstance(parties, list):
        parties = [parties]
    if not isinstance(suffix, tuple):
        suffix = (suffix,)
    tag = FederationTagNamespace.generate_tag(*suffix)

    for party in parties:
        if party.role not in self._src:
            raise RuntimeError(
                f"not allowed to get object from {party} using {self._name}"
            )
    local = session.parties.local_party.role
    if local not in self._dst:
        raise RuntimeError(
            f"not allowed to get object to {local} using {self._name}"
        )

    name = self._short_name if self._use_short_name else self._name
    timer = profile.federation_get_timer(name, self._name, tag, local, parties)
    rtn = session.federation.get(
        name=name, tag=tag, parties=parties, gc=self._get_gc
    )
    timer.done(session.federation)

    self._get_gc.gc()

    return rtn
remote(obj, role=None, idx=-1, suffix=tuple())

send obj to other parties.

Args: obj: object to be sent role: role of parties to sent to, use one of ['Host', 'Guest', 'Arbiter', None]. The default is None, means sent values to parties regardless their party role idx: id of party to sent to. The default is -1, which means sent values to parties regardless their party id suffix: additional tag suffix, the default is tuple()

Source code in python/fate_arch/federation/transfer_variable.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def remote(self, obj, role=None, idx=-1, suffix=tuple()):
    """
    send obj to other parties.

    Args:
        obj: object to be sent
        role: role of parties to sent to, use one of ['Host', 'Guest', 'Arbiter', None].
            The default is None, means sent values to parties regardless their party role
        idx: id of party to sent to.
            The default is -1, which means sent values to parties regardless their party id
        suffix: additional tag suffix, the default is tuple()
    """
    from fate_arch.session import get_parties

    party_info = get_parties()
    if idx >= 0 and role is None:
        raise ValueError("role cannot be None if idx specified")

    # get subset of dst roles in runtime conf
    if role is None:
        parties = party_info.roles_to_parties(self._dst, strict=False)
    else:
        if isinstance(role, str):
            role = [role]
        parties = party_info.roles_to_parties(role)

    if idx >= 0:
        if idx >= len(parties):
            raise RuntimeError(
                f"try to remote to {idx}th party while only {len(parties)} configurated: {parties}, check {self._name}"
            )
        parties = parties[idx]
    return self.remote_parties(obj=obj, parties=parties, suffix=suffix)
get(idx=-1, role=None, suffix=tuple())

get obj from other parties.

Args: idx: id of party to get from. The default is -1, which means get values from parties regardless their party id suffix: additional tag suffix, the default is tuple()

Returns: object or list of object

Source code in python/fate_arch/federation/transfer_variable.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def get(self, idx=-1, role=None, suffix=tuple()):
    """
    get obj from other parties.

    Args:
        idx: id of party to get from.
            The default is -1, which means get values from parties regardless their party id
        suffix: additional tag suffix, the default is tuple()

    Returns:
        object or list of object
    """
    from fate_arch.session import get_parties

    if role is None:
        src_parties = get_parties().roles_to_parties(roles=self._src, strict=False)
    else:
        if isinstance(role, str):
            role = [role]
        src_parties = get_parties().roles_to_parties(roles=role, strict=False)
    if isinstance(idx, list):
        rtn = self.get_parties(parties=[src_parties[i] for i in idx], suffix=suffix)
    elif isinstance(idx, int):
        if idx < 0:
            rtn = self.get_parties(parties=src_parties, suffix=suffix)
        else:
            if idx >= len(src_parties):
                raise RuntimeError(
                    f"try to get from {idx}th party while only {len(src_parties)} configurated: {src_parties}, check {self._name}"
                )
            rtn = self.get_parties(parties=src_parties[idx], suffix=suffix)[0]
    else:
        raise ValueError(
            f"illegal idx type: {type(idx)}, supported types: int or list of int"
        )
    return rtn

BaseTransferVariables(*args)

Bases: object

Source code in python/fate_arch/federation/transfer_variable.py
284
285
def __init__(self, *args):
    pass

Functions

__copy__()
Source code in python/fate_arch/federation/transfer_variable.py
287
288
def __copy__(self):
    return self
__deepcopy__(memo)
Source code in python/fate_arch/federation/transfer_variable.py
290
291
def __deepcopy__(self, memo):
    return self
set_flowid(flowid) staticmethod

set global namespace for federations.

Parameters:

Name Type Description Default
flowid

namespace

required

Returns:

Type Description
None
Source code in python/fate_arch/federation/transfer_variable.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
@staticmethod
def set_flowid(flowid):
    """
    set global namespace for federations.

    Parameters
    ----------
    flowid: str
       namespace

    Returns
    -------
    None

    """
    FederationTagNamespace.set_namespace(str(flowid))
all_parties() staticmethod

get all parties

Returns:

Type Description
list

list of parties

Source code in python/fate_arch/federation/transfer_variable.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@staticmethod
def all_parties():
    """
    get all parties

    Returns
    -------
    list
       list of parties

    """
    from fate_arch.session import get_parties

    return get_parties().all_parties
local_party() staticmethod

indicate local party

Returns:

Type Description
Party

party this program running on

Source code in python/fate_arch/federation/transfer_variable.py
333
334
335
336
337
338
339
340
341
342
343
344
345
346
@staticmethod
def local_party():
    """
    indicate local party

    Returns
    -------
    Party
       party this program running on

    """
    from fate_arch.session import get_parties

    return get_parties().local_party

Functions


Last update: 2021-11-15