Skip to content

  • 体验新版
    • 正在加载...
  • 登录
  • PaddlePaddle
  • Paddle
  • 合并请求
  • !26044

P
Paddle
  • 项目概览

PaddlePaddle / Paddle
大约 2 年 前同步成功

通知 2325
Star 20933
Fork 5424
  • 代码
    • 文件
    • 提交
    • 分支
    • Tags
    • 贡献者
    • 分支图
    • Diff
  • Issue 1423
    • 列表
    • 看板
    • 标记
    • 里程碑
  • 合并请求 543
  • Wiki 0
    • Wiki
  • 分析
    • 仓库
    • DevOps
  • 项目成员
  • Pages
P
Paddle
  • 项目概览
    • 项目概览
    • 详情
    • 发布
  • 仓库
    • 仓库
    • 文件
    • 提交
    • 分支
    • 标签
    • 贡献者
    • 分支图
    • 比较
  • Issue 1,423
    • Issue 1,423
    • 列表
    • 看板
    • 标记
    • 里程碑
  • 合并请求 543
    • 合并请求 543
  • Pages
  • 分析
    • 分析
    • 仓库分析
    • DevOps
  • Wiki 0
    • Wiki
  • 成员
    • 成员
  • 收起侧边栏
  • 动态
  • 分支图
  • 创建新Issue
  • 提交
  • Issue看板

Add interface to launch parallel dygraph by multiprocessing !26044

  • Report abuse
!26044 已合并 8月 07, 2020 由 saxon_zh@saxon_zh 创建
#<User:0x00007f7e185e99b0>
  • 概览 43
  • 提交 38
  • 变更 18

Created by: chenwhql

PR types

New features

PR changes

APIs

Describe

This PR add multiprocessing start method start_processes and spawnfor dygraph data parallel training.

1. Start method difference

  • start by launch

python -m paddle.distributed.launch --selected_gpus=0,1 train.py

  • start by spawn

python train.py

and add spawn in __main__ method, for example:

paddle.distributed.spawn(train_mnist,
    args=(args,),
    nprocs=args.nprocs,
    join=True)

2. Simple example

from __future__ import print_function

import paddle
import paddle.nn as nn
import paddle.optimizer as opt
import paddle.distributed as dist

class LinearNet(nn.Layer):
    def __init__(self):
        super(LinearNet, self).__init__()
        self._linear1 = nn.Linear(10, 10)
        self._linear2 = nn.Linear(10, 1)
        
    def forward(self, x):
        return self._linear2(self._linear1(x))

def train(print_result=False):
    # 1. enable dynamic mode
    paddle.disable_static()
    
    # 2. initialize parallel environment
    dist.init_parallel_env()

    # 3. create data parallel layer & optimizer
    layer = LinearNet()
    dp_layer = paddle.DataParallel(layer)

    loss_fn = nn.MSELoss()
    adam = opt.Adam(
        learning_rate=0.001, parameters=dp_layer.parameters())

    # 4. run layer
    inputs = paddle.randn([10, 10], 'float32')
    outputs = dp_layer(inputs)
    labels = paddle.randn([10, 1], 'float32')
    loss = loss_fn(outputs, labels)
    
    if print_result is True:
        print("loss:", loss.numpy())
    
    loss = dp_layer.scale_loss(loss)
    loss.backward()
    dp_layer.apply_collective_grads()

    adam.step()
    adam.clear_grad()

# Usage 1: only pass function. 
# If your training method no need any argument, and 
# use all visible devices for parallel training. 
if __name__ == '__main__':
    dist.spawn(train)

# Usage 2: pass function and arguments.
# If your training method need some arguments, and 
# use all visible devices for parallel training.
if __name__ == '__main__':
    dist.spawn(train, args=(True,))

# Usage 3: pass function, arguments and nprocs.
# If your training method need some arguments, and 
# only use part of visible devices for parallel training.
# If your machine hold 8 cards {0,1,2,3,4,5,6,7},
# this case will use cards {0,1}; If you set 
# CUDA_VISIBLE_DEVICES=4,5,6,7, this case will use
# cards {4,5}
if __name__ == '__main__':
    dist.spawn(train, args=(True,), nprocs=2)

# Usage 4: pass function, arguments, nprocs and selected_gpus.
# If your training method need some arguments, and 
# only use part of visible devices for parallel training,
# but you can't set your machine's environment varibale 
# CUDA_VISIBLE_DEVICES, such as it is None or all cards
# {0,1,2,3,4,5,6,7}, you can pass `selelcted_gpus` to 
# select the GPU cards you want to use. For example,
# this case will use cards {4,5} if your machine hold 8 cards.
if __name__ == '__main__':
    dist.spawn(train, args=(True,), nprocs=2, selelcted_gpus='4,5')

3. API change

  • Add 4 new apis:

    • paddle.distributed.spawn: start mulit-process training by spawn method
    • paddle.distributed.init_parallel_env: init parallel environment variables & get paralllel strategy
    • paddle.distributed.get_rank: get current process rank
    • paddle.distributed.get_world_size: get current world size
  • Move 2 old apis:

    • paddle.prepare_context (fluid.dygraph.prepare_context) -> paddle.distributed.prepare_context
    • paddle.ParallelEnv (fluid.dygraph.ParallelEnv) -> paddle.distributed.ParallelEnv
  • Refine 1 old api:

    • paddle.DataParallel (fluid.dygraph.DataParallel): Set strategy as an optional argument
  • Deprecate 1 old apis:

    • paddle.distributed.prepare_context (fluid.dygraph.prepare_context): replace by paddle.distributed.init_parallel_env later

4. Correctness

Verify the correctness of the interface in the following models:

  • Mnist: test_parallel_dygraph_mnist.py
  • SeResNext: test_parallel_dygraph_se_resnext.py
  • Transformer: test_parallel_dygraph_transformer.py

5. Related docs

  • FluidDoc PR: https://github.com/PaddlePaddle/FluidDoc/pull/2482

image image image image image

指派人
分配到
审核者
Request review from
无
里程碑
无
分配里程碑
工时统计
标识: paddlepaddle/Paddle!26044
Source branch: github/fork/chenwhql/dygraph/add_multiprocess_run_interface
渝ICP备2023009037号

京公网安备11010502055752号

网络110报警服务 Powered by GitLab CE v13.7
开源知识
Git 入门 Pro Git 电子书 在线学 Git
Markdown 基础入门 IT 技术知识开源图谱
帮助
使用手册 反馈建议 博客
《GitCode 隐私声明》 《GitCode 服务条款》 关于GitCode
Powered by GitLab CE v13.7