提交 a561cc11 编写于 作者: Q qjing666

add scheduler in SECAGG and fix compatibility between py2 and py3

上级 5ea8b13c
......@@ -2,3 +2,4 @@ sphinx==2.1.0
mistune
sphinx_rtd_theme
paddlepaddle>=1.6
zqm
......@@ -42,9 +42,9 @@ try:
from ssl import RAND_bytes
rng = RAND_bytes
except(AttributeError, ImportError):
raise RNGError
rng = os.urandom
#raise RNGError
#python2
#rng = os.urandom
class DiffieHellman:
"""
......@@ -115,13 +115,13 @@ class DiffieHellman:
self.shared_secret = pow(other_public_key,
self.private_key,
self.prime)
#python2
#length = self.shared_secret.bit_length() // 8 + 1
#shared_secret_as_bytes = ('%%0%dx' % (length << 1) % self.shared_secret).decode('hex')[-length:]
#python3
shared_secret_as_bytes = self.shared_secret.to_bytes(self.shared_secret.bit_length() // 8 + 1, byteorder='big')
try:
#python3
shared_secret_as_bytes = self.shared_secret.to_bytes(self.shared_secret.bit_length() // 8 + 1, byteorder='big')
except:
#python2
length = self.shared_secret.bit_length() // 8 + 1
shared_secret_as_bytes = ('%%0%dx' % (length << 1) % self.shared_secret).decode('hex')[-length:]
_h = sha256()
_h.update(bytes(shared_secret_as_bytes))
......
......@@ -16,7 +16,8 @@ import logging
from paddle_fl.core.scheduler.agent_master import FLWorkerAgent
import numpy
import hmac
#from .diffiehellman.diffiehellman import DiffieHellman
import hashlib
from .diffiehellman.diffiehellman import DiffieHellman
class FLTrainerFactory(object):
def __init__(self):
......@@ -190,6 +191,8 @@ class SecAggTrainer(FLTrainer):
self._step_id = s
def start(self):
self.agent = FLWorkerAgent(self._scheduler_ep, self._current_ep)
self.agent.connect_scheduler()
self.exe = fluid.Executor(fluid.CPUPlace())
self.exe.run(self._startup_program)
self.cur_step = 0
......@@ -219,7 +222,7 @@ class SecAggTrainer(FLTrainer):
self._logger.debug("begin to run send program")
noise = 0.0
scale = pow(10.0, 5)
digestmod="SHA256"
digestmod=hashlib.sha256
# 1. load priv key and other's pub key
dh = DiffieHellman(group=15, key_length=256)
dh.load_private_key(self._key_dir + str(self._trainer_id) + "_priv_key.txt")
......@@ -245,5 +248,3 @@ class SecAggTrainer(FLTrainer):
self.cur_step += 1
return loss
def stop(self):
return False
from paddle_fl.core.scheduler.agent_master import FLScheduler
worker_num = 2
server_num = 1
scheduler = FLScheduler(worker_num,server_num)
scheduler.set_sample_worker_num(worker_num)
scheduler.init_env()
print("init env done.")
scheduler.start_fl_training()
......@@ -21,5 +21,8 @@ server_id = 0
job_path = "fl_job_config"
job = FLRunTimeJob()
job.load_server_job(job_path, server_id)
job._scheduler_ep = "127.0.0.1:9091"
server.set_server_job(job)
server._current_ep = "127.0.0.1:8181"
server.start()
print("connect")
......@@ -28,8 +28,10 @@ trainer_id = int(sys.argv[1]) # trainer id for each guest
job_path = "fl_job_config"
job = FLRunTimeJob()
job.load_trainer_job(job_path, trainer_id)
job._scheduler_ep = "127.0.0.1:9091"
trainer = FLTrainerFactory().create_fl_trainer(job)
trainer.trainer_id = trainer_id
trainer._current_ep = "127.0.0.1:{}".format(9000+trainer_id)
trainer.trainer_num = trainer_num
trainer.key_dir = "./keys/"
trainer.start()
......
......@@ -5,10 +5,12 @@ if [ ! -d log ];then
mkdir log
fi
python3 fl_master.py
python fl_master.py
sleep 2
python3 -u fl_server.py >log/server0.log &
python -u fl_server.py >log/server0.log &
sleep 2
python3 -u fl_trainer.py 0 >log/trainer0.log &
python -u fl_scheduler.py > log/scheduler.log &
sleep 2
python3 -u fl_trainer.py 1 >log/trainer1.log &
python -u fl_trainer.py 0 >log/trainer0.log &
sleep 2
python -u fl_trainer.py 1 >log/trainer1.log &
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册