def __repr__(self): """Returns a representation of the tensor useful for debugging.""" from crypten.debug import debug_mode share = self.share plain_text = self._tensor.get_plain_text() if debug_mode( ) else "HIDDEN" ptype = self.ptype return (f"MPCTensor(\n\t_tensor={share}\n" f"\tplain_text={plain_text}\n\tptype={ptype}\n)")
def div_(self, y): """Divide two tensors element-wise""" # TODO: Add test coverage for this code path (next 4 lines) if isinstance(y, float) and int(y) == y: y = int(y) if is_float_tensor(y) and y.frac().eq(0).all(): y = y.long() if isinstance(y, int) or is_int_tensor(y): if debug_mode(): tolerance = 1.0 tensor = self.get_plain_text() # Truncate protocol for dividing by public integers: if comm.get().get_world_size() > 2: wraps = self.wraps() self.share //= y # NOTE: The multiplication here must be split into two parts # to avoid long out-of-bounds when y <= 2 since (2 ** 63) is # larger than the largest long integer. self -= wraps * 4 * (int(2 ** 62) // y) else: self.share //= y if debug_mode(): if not torch.lt( torch.abs(self.get_plain_text() * y - tensor), tolerance ).all(): raise ValueError("Final result of division is incorrect.") return self # Otherwise multiply by reciprocal if isinstance(y, float): y = torch.tensor([y], dtype=torch.float, device=self.device) assert is_float_tensor(y), "Unsupported type for div_: %s" % type(y) return self.mul_(y.reciprocal())