-
Notifications
You must be signed in to change notification settings - Fork 583
/
factory.py
86 lines (67 loc) · 3.11 KB
/
factory.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
85
86
# Copyright 2020, The TensorFlow Federated Authors.
#
# 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.
"""Abstract base factory classes for creation of `AggregationProcess`."""
import abc
from typing import Union
from tensorflow_federated.python.core.impl.types import computation_types
from tensorflow_federated.python.core.templates import aggregation_process
ValueType = Union[computation_types.TensorType, computation_types.StructType]
class UnweightedAggregationFactory(abc.ABC):
"""Factory for creating `tff.templates.AggregationProcess` without weights."""
@abc.abstractmethod
def create(
self, value_type: ValueType
) -> aggregation_process.AggregationProcess:
"""Creates a `tff.aggregators.AggregationProcess` without weights.
The provided `value_type` is a non-federated `tff.Type` object, that is,
`value_type.is_federated()` should return `False`.
The returned `tff.aggregators.AggregationProcess` will be created for
aggregation of values matching `value_type` placed at `tff.CLIENTS`.
That is, its `next` method will expect type
`<S@SERVER, {value_type}@CLIENTS>`, where `S` is the unplaced return type of
its `initialize` method.
Args:
value_type: A non-federated `tff.Type` (`value_type.is_federated()`
returns `False`).
Returns:
A `tff.templates.AggregationProcess`.
"""
raise NotImplementedError
class WeightedAggregationFactory(abc.ABC):
"""Factory for creating `tff.templates.AggregationProcess` with weights."""
@abc.abstractmethod
def create(
self, value_type: ValueType, weight_type: ValueType
) -> aggregation_process.AggregationProcess:
"""Creates a `tff.aggregators.AggregationProcess` with weights.
The provided `value_type` and `weight_type` are non-federated `tff.Type`
objects, that is, `value_type.is_federated()` and
`weight_type.is_federated()` should return `False`.
The returned `tff.aggregators.AggregationProcess` will be created
for aggregation of pairs of values matching `value_type` and `weight_type`
placed at `tff.CLIENTS`. That is, its `next` method will expect type
`<S@SERVER, {value_type}@CLIENTS, {weight_type}@CLIENTS>`, where `S` is the
unplaced return type of its `initialize` method.
Args:
value_type: A non-federated `tff.Type` (`value_type.is_federated()`
returns `False`).
weight_type: A non-federated `tff.Type` (`weight_type.is_federated()`
returns `False`).
Returns:
A `tff.templates.AggregationProcess`.
"""
raise NotImplementedError
AggregationFactory = Union[
UnweightedAggregationFactory, WeightedAggregationFactory
]