fixedpoint_endec.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Copyright 2021 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import functools
  17. import numpy as np
  18. from fate_arch.session import is_table
  19. class FixedPointEndec(object):
  20. def __init__(self, field: int, base: int, precision_fractional: int, *args, **kwargs):
  21. self.field = field
  22. self.base = base
  23. self.precision_fractional = precision_fractional
  24. def _encode(self, float_tensor: np.ndarray, check_range=True):
  25. upscaled = (float_tensor * self.base ** self.precision_fractional).astype(np.int64)
  26. if check_range:
  27. assert (np.abs(upscaled) < (self.field / 2)).all(), (
  28. f"{float_tensor} cannot be correctly embedded: choose bigger field or a lower precision"
  29. )
  30. field_element = upscaled % self.field
  31. return field_element
  32. def _decode(self, integer_tensor: np.ndarray):
  33. value = integer_tensor % self.field
  34. gate = value > self.field // 2
  35. neg_nums = (value - self.field) * gate
  36. pos_nums = value * (1 - gate)
  37. result = (neg_nums + pos_nums) / (self.base ** self.precision_fractional)
  38. return result
  39. def _truncate(self, integer_tensor, idx=0):
  40. if idx == 0:
  41. return self.field - (self.field - integer_tensor) // (self.base ** self.precision_fractional)
  42. else:
  43. return integer_tensor // (self.base ** self.precision_fractional)
  44. def encode(self, float_tensor, check_range=True):
  45. if isinstance(float_tensor, (float, np.float)):
  46. float_tensor = np.array(float_tensor)
  47. if isinstance(float_tensor, np.ndarray):
  48. return self._encode(float_tensor, check_range)
  49. elif is_table(float_tensor):
  50. f = functools.partial(self._encode, check_range=check_range)
  51. return float_tensor.mapValues(f)
  52. else:
  53. raise ValueError(f"unsupported type: {type(float_tensor)}")
  54. def decode(self, integer_tensor):
  55. if isinstance(integer_tensor, (int, np.int16, np.int32, np.int64)):
  56. integer_tensor = np.array(integer_tensor)
  57. if isinstance(integer_tensor, np.ndarray):
  58. return self._decode(integer_tensor)
  59. elif is_table(integer_tensor):
  60. f = functools.partial(self._decode)
  61. return integer_tensor.mapValues(lambda x: f)
  62. else:
  63. raise ValueError(f"unsupported type: {type(integer_tensor)}")
  64. def truncate(self, integer_tensor, idx=0):
  65. if isinstance(integer_tensor, (int, np.int16, np.int32, np.int64)):
  66. integer_tensor = np.array(integer_tensor)
  67. if isinstance(integer_tensor, np.ndarray):
  68. return self._truncate(integer_tensor, idx)
  69. elif is_table(integer_tensor):
  70. f = functools.partial(self._truncate, idx=idx)
  71. return integer_tensor.mapValues(f)
  72. else:
  73. raise ValueError(f"unsupported type: {type(integer_tensor)}")