使用FleetAPI进行分布式训练

FleetAPI 设计说明

Fleet是PaddlePaddle分布式训练的高级API。Fleet的命名出自于PaddlePaddle,象征一个舰队中的多只双桨船协同工作。Fleet的设计在易用性和算法可扩展性方面做出了权衡。用户可以很容易从单机版的训练程序,通过添加几行代码切换到分布式训练程序。此外,分布式训练的算法也可以通过Fleet API接口灵活定义。

Fleet API快速上手示例

下面会针对Fleet API最常见的两种使用场景,用一个模型做示例,目的是让用户有快速上手体验的模板。

  • 假设我们定义MLP网络如下:

    1. import paddle
    2. def mlp(input_x, input_y, hid_dim=128, label_dim=2):
    3. fc_1 = paddle.static.nn.fc(input=input_x, size=hid_dim, act='tanh')
    4. fc_2 = paddle.static.nn.fc(input=fc_1, size=hid_dim, act='tanh')
    5. prediction = paddle.static.nn.fc(input=[fc_2], size=label_dim, act='softmax')
    6. cost = paddle.static.nn.cross_entropy(input=prediction, label=input_y)
    7. avg_cost = paddle.static.nn.mean(x=cost)
    8. return avg_cost
  • 定义一个在内存生成数据的Reader如下:

    1. import numpy as np
    2. def gen_data():
    3. return {"x": np.random.random(size=(128, 32)).astype('float32'),
    4. "y": np.random.randint(2, size=(128, 1)).astype('int64')}
  • 单机Trainer定义

    1. import paddle
    2. from nets import mlp
    3. from utils import gen_data
    4. input_x = paddle.static.data(name="x", shape=[None, 32], dtype='float32')
    5. input_y = paddle.static.data(name="y", shape=[None, 1], dtype='int64')
    6. cost = mlp(input_x, input_y)
    7. optimizer = paddle.optimizer.SGD(learning_rate=0.01)
    8. optimizer.minimize(cost)
    9. place = paddle.CUDAPlace(0)
    10. exe = paddle.static.Executor(place)
    11. exe.run(paddle.static.default_startup_program())
    12. step = 1001
    13. for i in range(step):
    14. cost_val = exe.run(feed=gen_data(), fetch_list=[cost.name])
    15. print("step%d cost=%f" % (i, cost_val[0]))
  • Parameter Server训练方法

    参数服务器方法对于大规模数据,简单模型的并行训练非常适用,我们基于单机模型的定义给出使用Parameter Server进行训练的示例如下:

    1. import paddle
    2. paddle.enable_static()
    3. import paddle.distributed.fleet.base.role_maker as role_maker
    4. import paddle.distributed.fleet as fleet
    5. from nets import mlp
    6. from utils import gen_data
    7. input_x = paddle.static.data(name="x", shape=[None, 32], dtype='float32')
    8. input_y = paddle.static.data(name="y", shape=[None, 1], dtype='int64')
    9. cost = mlp(input_x, input_y)
    10. optimizer = paddle.optimizer.SGD(learning_rate=0.01)
    11. role = role_maker.PaddleCloudRoleMaker()
    12. fleet.init(role)
    13. strategy = paddle.distributed.fleet.DistributedStrategy()
    14. strategy.a_sync = True
    15. optimizer = fleet.distributed_optimizer(optimizer, strategy)
    16. optimizer.minimize(cost)
    17. if fleet.is_server():
    18. fleet.init_server()
    19. fleet.run_server()
    20. elif fleet.is_worker():
    21. place = paddle.CPUPlace()
    22. exe = paddle.static.Executor(place)
    23. exe.run(paddle.static.default_startup_program())
    24. step = 1001
    25. for i in range(step):
    26. cost_val = exe.run(
    27. program=paddle.static.default_main_program(),
    28. feed=gen_data(),
    29. fetch_list=[cost.name])
    30. print("worker_index: %d, step%d cost = %f" %
    31. (fleet.worker_index(), i, cost_val[0]))
  • Collective训练方法

    Collective Training通常在GPU多机多卡训练中使用,一般在复杂模型的训练中比较常见,我们基于上面的单机模型定义给出使用Collective方法进行分布式训练的示例如下:

    1. import paddle
    2. paddle.enable_static()
    3. import paddle.distributed.fleet.base.role_maker as role_maker
    4. import paddle.distributed.fleet as fleet
    5. from nets import mlp
    6. from utils import gen_data
    7. input_x = paddle.static.data(name="x", shape=[None, 32], dtype='float32')
    8. input_y = paddle.static.data(name="y", shape=[None, 1], dtype='int64')
    9. cost = mlp(input_x, input_y)
    10. optimizer = paddle.optimizer.SGD(learning_rate=0.01)
    11. role = role_maker.PaddleCloudRoleMaker(is_collective=True)
    12. fleet.init(role)
    13. optimizer = fleet.distributed_optimizer(optimizer)
    14. optimizer.minimize(cost)
    15. place = paddle.CUDAPlace(0)
    16. exe = paddle.static.Executor(place)
    17. exe.run(paddle.static.default_startup_program())
    18. step = 1001
    19. for i in range(step):
    20. cost_val = exe.run(
    21. program=paddle.static.default_main_program(),
    22. feed=gen_data(),
    23. fetch_list=[cost.name])
    24. print("worker_index: %d, step%d cost = %f" %
    25. (fleet.worker_index(), i, cost_val[0]))

Fleet API相关的接口说明

Fleet API接口

  • init(role_maker=None)

    • fleet初始化,需要在使用fleet其他接口前先调用,用于定义多机的环境配置
  • is_worker()

    • Parameter Server训练中使用,判断当前节点是否是Worker节点,是则返回True,否则返回False
  • is_server(model_dir=None)

    • Parameter Server训练中使用,判断当前节点是否是Server节点,是则返回True,否则返回False
  • init_server()

    • Parameter Server训练中,fleet加载model_dir中保存的模型相关参数进行parameter server的初始化
  • run_server()

    • Parameter Server训练中使用,用来启动server端服务
  • init_worker()

    • Parameter Server训练中使用,用来启动worker端服务
  • stop_worker()

    • 训练结束后,停止worker
  • distributed_optimizer(optimizer, strategy=None)

    • 分布式优化算法装饰器,用户可带入单机optimizer,并配置分布式训练策略,返回一个分布式的optimizer

RoleMaker

  • PaddleCloudRoleMaker

    • 描述:PaddleCloudRoleMaker是一个高级封装,支持使用paddle.distributed.launch或者paddle.distributed.launch_ps启动脚本

    • Parameter Server训练示例:

      1. import paddle
      2. paddle.enable_static()
      3. import paddle.distributed.fleet.base.role_maker as role_maker
      4. import paddle.distributed.fleet as fleet
      5. role = role_maker.PaddleCloudRoleMaker()
      6. fleet.init(role)
    • 启动方法:

      1. python -m paddle.distributed.launch_ps --worker_num 2 --server_num 2 trainer.py
    • Collective训练示例:

      1. import paddle
      2. paddle.enable_static()
      3. import paddle.distributed.fleet.base.role_maker as role_maker
      4. import paddle.distributed.fleet as fleet
      5. role = role_maker.PaddleCloudRoleMaker(is_collective=True)
      6. fleet.init(role)
    • 启动方法:

      1. python -m paddle.distributed.launch trainer.py
  • UserDefinedRoleMaker

    • 描述:用户自定义节点的角色信息,IP和端口信息

    • 示例:

      1. import paddle
      2. paddle.enable_static()
      3. import paddle.distributed.fleet.base.role_maker as role_maker
      4. import paddle.distributed.fleet as fleet
      5. role = role_maker.UserDefinedRoleMaker(
      6. current_id=0,
      7. role=role_maker.Role.SERVER,
      8. worker_num=2,
      9. server_endpoints=["127.0.0.1:36011", "127.0.0.1:36012"])
      10. fleet.init(role)