secure_add_host.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import numpy as np
  19. from fate_arch.session import computing_session as session
  20. from federatedml.model_base import ModelBase, ComponentOutput
  21. from federatedml.param.secure_add_example_param import SecureAddExampleParam
  22. from federatedml.transfer_variable.transfer_class.secure_add_example_transfer_variable import \
  23. SecureAddExampleTransferVariable
  24. from federatedml.util import LOGGER
  25. class SecureAddHost(ModelBase):
  26. def __init__(self):
  27. super(SecureAddHost, self).__init__()
  28. self.y = None
  29. self.y1 = None
  30. self.y2 = None
  31. self.x2 = None
  32. self.x2_plus_y2 = None
  33. self.transfer_inst = SecureAddExampleTransferVariable()
  34. self.model_param = SecureAddExampleParam()
  35. self.data_output = None
  36. self.model_output = None
  37. def _init_runtime_parameters(self, cpn_input):
  38. self.model_param.update(cpn_input.parameters)
  39. self._init_model()
  40. def _init_model(self):
  41. self.data_num = self.model_param.data_num
  42. self.partition = self.model_param.partition
  43. self.seed = self.model_param.seed
  44. def _init_data(self):
  45. kvs = [(i, 1) for i in range(self.data_num)]
  46. self.y = session.parallelize(kvs, include_key=True, partition=self.partition)
  47. def share(self, y):
  48. first = np.random.uniform(y, -y)
  49. return first, y - first
  50. def secure(self):
  51. y_shares = self.y.mapValues(self.share)
  52. self.y1 = y_shares.mapValues(lambda shares: shares[0])
  53. self.y2 = y_shares.mapValues(lambda shares: shares[1])
  54. def add(self):
  55. self.x2_plus_y2 = self.y2.join(self.x2, lambda y, x: y + x)
  56. host_sum = self.x2_plus_y2.reduce(lambda x, y: x + y)
  57. return host_sum
  58. def sync_share_to_guest(self):
  59. self.transfer_inst.host_share.remote(self.y1,
  60. role="guest",
  61. idx=0)
  62. def recv_share_from_guest(self):
  63. self.x2 = self.transfer_inst.guest_share.get(idx=0)
  64. def sync_host_sum_to_guest(self, host_sum):
  65. self.transfer_inst.host_sum.remote(host_sum,
  66. role="guest",
  67. idx=0)
  68. def run(self, cpn_input):
  69. LOGGER.info("begin to init parameters of secure add example host")
  70. self._init_runtime_parameters(cpn_input)
  71. LOGGER.info("begin to make host data")
  72. self._init_data()
  73. LOGGER.info("split data into two random parts")
  74. self.secure()
  75. LOGGER.info("get share of one random part data from guest")
  76. self.recv_share_from_guest()
  77. LOGGER.info("share one random part data to guest")
  78. self.sync_share_to_guest()
  79. LOGGER.info("begin to get sum of host and guest")
  80. host_sum = self.add()
  81. LOGGER.info("send host sum to guest")
  82. self.sync_host_sum_to_guest(host_sum)
  83. return ComponentOutput(self.save_data(), self.export_model(), self.save_cache())