MCPcopy Create free account
hub / github.com/OpenMined/SyMPC / apply_function

Method apply_function

src/sympc/tensor/share_tensor.py:181–212  ·  view source on GitHub ↗

Apply a given operation. Args: y (Union["ShareTensor", torch.Tensor, int, float]): tensor to apply the operator. op_str (str): Operator. Returns: ShareTensor: Result of the operation.

(
        self, y: Union["ShareTensor", torch.Tensor, int, float], op_str: str
    )

Source from the content-addressed store, hash-verified

179 return y
180
181 def apply_function(
182 self, y: Union["ShareTensor", torch.Tensor, int, float], op_str: str
183 ) -> "ShareTensor":
184 """Apply a given operation.
185
186 Args:
187 y (Union["ShareTensor", torch.Tensor, int, float]): tensor to apply the operator.
188 op_str (str): Operator.
189
190 Returns:
191 ShareTensor: Result of the operation.
192 """
193 op = getattr(operator, op_str)
194
195 if isinstance(y, ShareTensor):
196 value = op(self.tensor, y.tensor)
197 else:
198 value = op(self.tensor, y)
199
200 session_uuid = self.session_uuid or y.session_uuid
201 if session_uuid is not None:
202 session = sympc.session.get_session(str(session_uuid))
203 ring_size = session.ring_size
204 config = session.config
205 else:
206 # Use the values from "self"
207 ring_size = self.ring_size
208 config = self.config
209
210 res = ShareTensor(ring_size=ring_size, session_uuid=session_uuid, config=config)
211 res.tensor = value
212 return res
213
214 def add(self, y: Union[int, float, torch.Tensor, "ShareTensor"]) -> "ShareTensor":
215 """Apply the "add" operation between "self" and "y".

Callers 6

addMethod · 0.95
subMethod · 0.95
mulMethod · 0.95
xorMethod · 0.95
matmulMethod · 0.95
rsubMethod · 0.80

Calls 1

ShareTensorClass · 0.85

Tested by

no test coverage detected