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

Method decode

src/sympc/encoder/fp_encoder.py:64–92  ·  view source on GitHub ↗

Decode a value using the FixedPoint Encoder. Args: value (Union[int, torch.Tensor]): Value to decode. Returns: torch.Tensor: Decoded tensor. Raises: ValueError: If value is a floating torch.Tensor.

(self, value: Union[int, torch.Tensor])

Source from the content-addressed store, hash-verified

62 return long_value
63
64 def decode(self, value: Union[int, torch.Tensor]) -> torch.Tensor:
65 """Decode a value using the FixedPoint Encoder.
66
67 Args:
68 value (Union[int, torch.Tensor]): Value to decode.
69
70 Returns:
71 torch.Tensor: Decoded tensor.
72
73 Raises:
74 ValueError: If value is a floating torch.Tensor.
75 """
76 if isinstance(value, torch.Tensor) and value.dtype.is_floating_point:
77 raise ValueError(f"{value} should be converted to long format")
78
79 if isinstance(value, int):
80 value = torch.LongTensor([value])
81
82 tensor = value
83 if self._precision == 0:
84 return tensor
85
86 correction = (tensor < 0).long()
87 dividend = tensor // self._scale - correction
88 remainder = tensor % self._scale
89 remainder += (remainder == 0).long() * self._scale * correction
90
91 tensor = dividend.float() + remainder.float() / self._scale
92 return tensor
93
94 @property
95 def precision(self):

Callers 6

reconstructMethod · 0.95
ltMethod · 0.95
gtMethod · 0.95
test_fixed_pointFunction · 0.95
test_fp_decodingFunction · 0.95

Calls

no outgoing calls

Tested by 3

test_fixed_pointFunction · 0.76
test_fp_decodingFunction · 0.76