From f61287779781be4945b17bd8d3b1102cac0eb93d Mon Sep 17 00:00:00 2001 From: dongdaxiang Date: Wed, 13 Mar 2019 15:40:11 +0800 Subject: [PATCH] add incubate for unified API --- paddle/fluid/pybind/pybind.cc | 1 + .../fluid/incubate/fleet/base/role_maker.py | 2 +- .../fleet/parameter_server/__init__.py | 47 ++++++++++++------- python/setup.py.in | 7 ++- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index bbf59b95c..b011858a5 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -1358,6 +1358,7 @@ All parameter, weight, gradient are variables in Paddle. BindRecordIOWriter(&m); BindAsyncExecutor(&m); + BindFleetWrapper(&m); BindGraph(&m); BindNode(&m); BindInferenceApi(&m); diff --git a/python/paddle/fluid/incubate/fleet/base/role_maker.py b/python/paddle/fluid/incubate/fleet/base/role_maker.py index c7c6737a7..0ee479dab 100644 --- a/python/paddle/fluid/incubate/fleet/base/role_maker.py +++ b/python/paddle/fluid/incubate/fleet/base/role_maker.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from .helper import MPIHelper class RoleMakerBase(object): @@ -46,6 +45,7 @@ class MPIRoleMaker(RoleMakerBase): from mpi4py import MPI self.comm_ = MPI.COMM_WORLD self.MPI = MPI + self.ips_ = None def get_rank(self): self.rank_ = self.comm_.Get_rank() diff --git a/python/paddle/fluid/incubate/fleet/parameter_server/__init__.py b/python/paddle/fluid/incubate/fleet/parameter_server/__init__.py index e7cf56474..b3dbab065 100644 --- a/python/paddle/fluid/incubate/fleet/parameter_server/__init__.py +++ b/python/paddle/fluid/incubate/fleet/parameter_server/__init__.py @@ -14,19 +14,10 @@ import sys import os from ..base.role_maker import MPISymetricRoleMaker -from paddle.fluid.optimizer import Optimizer - -# this is a temporary solution -# TODO(guru4elephant) -# will make this more flexible for more Parameter Server Archs -fleet_instance = Fleet() - -init = fleet_instance.init -stop = fleet_instance.stop -init_pserver = fleet_instance.init_pserver -init_worker = fleet_instance.init_worker -init_pserver_model = fleet_instance.init_pserver_model -save_pserver_model = fleet_instance.save_pserver_model +from .optimizer_factory import * +from google.protobuf import text_format +import paddle.fluid.optimizer as local_optimizer +import paddle.fluid as fluid class Fleet(object): @@ -35,7 +26,7 @@ class Fleet(object): """ def __init__(self): - self.opt_info = None # for fleet only + self._opt_info = None # for fleet only self.role_maker_ = None def init(self): @@ -44,7 +35,7 @@ class Fleet(object): # we will support more configurable RoleMaker for users in the future self.role_maker_ = MPISymetricRoleMaker() self.role_maker_.generate_role() - self._fleet_ptr = core.FleetWrapper() + self._fleet_ptr = fluid.core.Fleet() def stop(self): self.role_maker_.barrier_worker() @@ -91,6 +82,12 @@ class Fleet(object): print("You should run DistributedOptimizer.minimize() first") sys.exit(-1) + def is_worker(self): + return self.role_maker_.is_worker() + + def is_server(self): + return self.role_maker_.is_server() + def init_pserver_model(self): if self.role_maker_.is_first_worker(): self._fleet_ptr.init_model() @@ -103,7 +100,7 @@ class Fleet(object): self._opt_info = opt_info -class DistributedOptimizer(paddle.fluid.Optimizer): +class DistributedOptimizer(object): def __init__(self, optimizer, dist_config={}): super(DistributedOptimizer, self).__init__() self._optimizer = optimizer @@ -115,7 +112,7 @@ class DistributedOptimizer(paddle.fluid.Optimizer): sys.stderr) self._optimizer_name = "DistributedAdam" - self._distributed_optimizer = globals()[self._optimizer_name]() + self._distributed_optimizer = globals()[self._optimizer_name](optimizer) def backward(self, loss, @@ -135,7 +132,6 @@ class DistributedOptimizer(paddle.fluid.Optimizer): no_grad_set=None): optimize_ops, param_grads, opt_info = \ self._distributed_optimizer.minimize( - self._optimizer, loss, startup_program, parameter_list, @@ -143,3 +139,18 @@ class DistributedOptimizer(paddle.fluid.Optimizer): fleet_instance._set_opt_info(opt_info) return [optimize_ops, param_grads] + + +# this is a temporary solution +# TODO(guru4elephant) +# will make this more flexible for more Parameter Server Archs +fleet_instance = Fleet() + +init = fleet_instance.init +stop = fleet_instance.stop +init_pserver = fleet_instance.init_pserver +init_worker = fleet_instance.init_worker +is_worker = fleet_instance.is_worker +is_server = fleet_instance.is_server +init_pserver_model = fleet_instance.init_pserver_model +save_pserver_model = fleet_instance.save_pserver_model diff --git a/python/setup.py.in b/python/setup.py.in index 68f96273a..801eef741 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -120,7 +120,12 @@ packages=['paddle', 'paddle.fluid.contrib.slim.distillation', 'paddle.fluid.contrib.utils', 'paddle.fluid.transpiler', - 'paddle.fluid.transpiler.details'] + 'paddle.fluid.transpiler.details', + 'paddle.fluid.incubate', + 'paddle.fluid.incubate.fleet', + 'paddle.fluid.incubate.fleet.base', + 'paddle.fluid.incubate.fleet.parameter_server', + 'paddle.fluid.incubate.fleet.p2p'] with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f: setup_requires = f.read().splitlines() -- GitLab