/
replicatedshare_tensor.py
executable file
·245 lines (176 loc) · 6.16 KB
/
replicatedshare_tensor.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
158
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
210
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
244
245
"""Used to abstract multiple shared values held by parties."""
# stdlib
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Set
from sympc.tensor import ShareTensor
from .tensor import SyMPCTensor
PROPERTIES_NEW_SHARE_TENSOR: Set[str] = {"T"}
METHODS_NEW_SHARE_TENSOR: Set[str] = {"unsqueeze", "view", "t", "sum", "clone"}
class ReplicatedSharedTensor(metaclass=SyMPCTensor):
"""RSTensor is used when a party holds more than a single share,required by various protocols.
Arguments:
session (Session): the session
shares: The shares held by the party
Attributes:
shares: The shares held by the party
"""
AUTOGRAD_IS_ON: bool = True
# Used by the SyMPCTensor metaclass
METHODS_FORWARD = {"numel", "t", "unsqueeze", "view", "sum", "clone"}
PROPERTIES_FORWARD = {"T"}
def __init__(self, shares=None, session=None):
"""Initialize ShareTensor.
Args:
shares (Optional[List[ShareTensor]]): Shares from which RSTensor is created.
session (Optional[Session]): The session. Defaults to None.
"""
self.session = session
self.shares = shares
def add(self, y):
"""Apply the "add" operation between "self" and "y".
Args:
y: self+y
"""
def sub(self, y):
"""Apply the "sub" operation between "self" and "y".
Args:
y: self-y
"""
def rsub(self, y):
"""Apply the "sub" operation between "y" and "self".
Args:
y: self-y
"""
def mul(self, y):
"""Apply the "mul" operation between "self" and "y".
Args:
y: self*y
"""
def truediv(self, y):
"""Apply the "div" operation between "self" and "y".
Args:
y: self/y
"""
def matmul(self, y):
"""Apply the "matmul" operation between "self" and "y".
Args:
y: self@y
"""
def rmatmul(self, y):
"""Apply the "rmatmul" operation between "y" and "self".
Args:
y: self@y
"""
def xor(self, y):
"""Apply the "xor" operation between "self" and "y".
Args:
y: self^y
"""
def lt(self, y):
"""Lower than operator.
Args:
y: self<y
"""
def gt(self, y):
"""Greater than operator.
Args:
y: self>y
"""
def eq(self, y):
"""Equal operator.
Args:
y: self==y
"""
def ne(self, y):
"""Not Equal operator.
Args:
y: self!=y
"""
@staticmethod
def hook_property(property_name: str) -> Any:
"""Hook a framework property (only getter).
Ex:
* if we call "shape" we want to call it on the underlying tensor
and return the result
* if we call "T" we want to call it on the underlying tensor
but we want to wrap it in the same tensor type
Args:
property_name (str): property to hook
Returns:
A hooked property
"""
def property_new_share_tensor_getter(_self: "ReplicatedSharedTensor") -> Any:
tensor1 = getattr(_self.shares[0].tensor, property_name)
tensor2 = getattr(_self.shares[1].tensor, property_name)
share1 = ShareTensor(session=_self.session)
share1.tensor = (
tensor1 # assign after instance creation to prevent FP encoding.
)
share2 = ShareTensor(session=_self.session)
share2.tensor = (
tensor2 # assign after instance creation to prevent FP encoding
)
shares = [share1, share2]
res = ReplicatedSharedTensor(session=_self.session, shares=shares)
return res
def property_getter(_self: "ReplicatedSharedTensor") -> Any:
prop = getattr(_self.shares[0].tensor, property_name)
return prop
if property_name in PROPERTIES_NEW_SHARE_TENSOR:
res = property(property_new_share_tensor_getter, None)
else:
res = property(property_getter, None)
return res
@staticmethod
def hook_method(method_name: str) -> Callable[..., Any]:
"""Hook a framework method such that we know how to treat it given that we call it.
Ex:
* if we call "numel" we want to call it on the underlying tensor
and return the result
* if we call "unsqueeze" we want to call it on the underlying tensor
but we want to wrap it in the same tensor type
Args:
method_name (str): method to hook
Returns:
A hooked method
"""
def method_new_rs_tensor(
_self: "ReplicatedSharedTensor", *args: List[Any], **kwargs: Dict[Any, Any]
) -> Any:
tensor1 = getattr(_self.shares[0].tensor, method_name)(*args, **kwargs)
tensor2 = getattr(_self.shares[1].tensor, method_name)(*args, **kwargs)
share1 = ShareTensor(session=_self.session)
share1.tensor = (
tensor1 # assign after instance creation to prevent FP encoding
)
share2 = ShareTensor(data=tensor2, session=_self.session)
share2.tensor = (
tensor2 # assign after instance creation to prevent FP encoding
)
shares = [share1, share2]
res = ReplicatedSharedTensor(session=_self.session, shares=shares)
return res
def method(
_self: "ReplicatedSharedTensor", *args: List[Any], **kwargs: Dict[Any, Any]
) -> Any:
method = getattr(_self.shares[0].tensor, method_name)
res = method(*args, **kwargs)
return res
if method_name in METHODS_NEW_SHARE_TENSOR:
res = method_new_rs_tensor
else:
res = method
return res
__add__ = add
__radd__ = add
__sub__ = sub
__rsub__ = rsub
__mul__ = mul
__rmul__ = mul
__matmul__ = matmul
__rmatmul__ = rmatmul
__truediv__ = truediv
__xor__ = xor