loss_sync.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. from federatedml.util import consts
  18. class Arbiter(object):
  19. def _register_loss_sync(self, loss_transfer):
  20. self.loss_transfer = loss_transfer
  21. def sync_loss_info(self, suffix=tuple()):
  22. loss = self.loss_transfer.get(idx=0, suffix=suffix)
  23. return loss
  24. class Guest(object):
  25. def _register_loss_sync(self, host_loss_regular_transfer, loss_transfer, loss_intermediate_transfer):
  26. self.host_loss_regular_transfer = host_loss_regular_transfer
  27. self.loss_transfer = loss_transfer
  28. self.loss_intermediate_transfer = loss_intermediate_transfer
  29. def sync_loss_info(self, loss, suffix=tuple()):
  30. self.loss_transfer.remote(loss, role=consts.ARBITER, idx=0, suffix=suffix)
  31. def get_host_loss_intermediate(self, suffix=tuple()):
  32. loss_intermediate = self.loss_intermediate_transfer.get(idx=-1, suffix=suffix)
  33. return loss_intermediate
  34. def get_host_loss_regular(self, suffix=tuple()):
  35. losses = self.host_loss_regular_transfer.get(idx=-1, suffix=suffix)
  36. return losses
  37. class Host(object):
  38. def _register_loss_sync(self, host_loss_regular_transfer, loss_transfer, loss_intermediate_transfer):
  39. self.host_loss_regular_transfer = host_loss_regular_transfer
  40. self.loss_transfer = loss_transfer
  41. self.loss_intermediate_transfer = loss_intermediate_transfer
  42. def remote_loss_intermediate(self, loss_intermediate, suffix=tuple()):
  43. self.loss_intermediate_transfer.remote(obj=loss_intermediate, role=consts.GUEST, idx=0, suffix=suffix)
  44. def remote_loss_regular(self, loss_regular, suffix=tuple()):
  45. self.host_loss_regular_transfer.remote(obj=loss_regular, role=consts.GUEST, idx=0, suffix=suffix)