diff --git a/docs/requirements.txt b/docs/requirements.txt index 1560ebc5f9d74fbae773ac5bc45c5b42b044287a..c5afc4d9c892d39aca6660fe79f9ad0ad1a6eb20 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -2,3 +2,4 @@ sphinx==2.1.0 mistune sphinx_rtd_theme paddlepaddle>=1.6 +zqm diff --git a/paddle_fl/core/trainer/diffiehellman/diffiehellman.py b/paddle_fl/core/trainer/diffiehellman/diffiehellman.py index 3d3e18a706a15865d9ef50c8bd3138645e0cff8c..d6010fe970a29846bd7f5033073c7313de80effe 100644 --- a/paddle_fl/core/trainer/diffiehellman/diffiehellman.py +++ b/paddle_fl/core/trainer/diffiehellman/diffiehellman.py @@ -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)) diff --git a/paddle_fl/core/trainer/fl_trainer.py b/paddle_fl/core/trainer/fl_trainer.py index 7d162165d38faaaefe2c4b8f0eef5baf5ad5ed32..b58ac1c8b74330918f54b331ab3621f98d8b531f 100755 --- a/paddle_fl/core/trainer/fl_trainer.py +++ b/paddle_fl/core/trainer/fl_trainer.py @@ -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 diff --git a/paddle_fl/examples/secagg_demo/fl_scheduler.py b/paddle_fl/examples/secagg_demo/fl_scheduler.py new file mode 100644 index 0000000000000000000000000000000000000000..7a0c8f5b3411aac07f7e50e3daf3a1c0a53144e4 --- /dev/null +++ b/paddle_fl/examples/secagg_demo/fl_scheduler.py @@ -0,0 +1,9 @@ +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() diff --git a/paddle_fl/examples/secagg_demo/fl_server.py b/paddle_fl/examples/secagg_demo/fl_server.py index 7ef353b8fe99c4b335dda4648977f292ffb07dae..3671838a60b678c8c29ad530888ec75252c5430a 100644 --- a/paddle_fl/examples/secagg_demo/fl_server.py +++ b/paddle_fl/examples/secagg_demo/fl_server.py @@ -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") diff --git a/paddle_fl/examples/secagg_demo/fl_trainer.py b/paddle_fl/examples/secagg_demo/fl_trainer.py index d6f9fdffb6e270674831a6c4f826f6673d073d44..e97ffbc0fc33b384699edbc5f0703aed412846be 100644 --- a/paddle_fl/examples/secagg_demo/fl_trainer.py +++ b/paddle_fl/examples/secagg_demo/fl_trainer.py @@ -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() diff --git a/paddle_fl/examples/secagg_demo/run.sh b/paddle_fl/examples/secagg_demo/run.sh index 9d51fa27c464f3f18a10185e0adbb8166e0ea27b..1e40c1a3ea8301299ecc8bc52b78bcfca4d037bb 100644 --- a/paddle_fl/examples/secagg_demo/run.sh +++ b/paddle_fl/examples/secagg_demo/run.sh @@ -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 &