DataParallel_cn.rst 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
.. _cn_api_fluid_dygraph_DataParallel:

DataParallel
------------

.. py:class:: paddle.fluid.dygraph.DataParallel(layers, strategy)

:api_attr: 命令式编程模式(动态图)

通过数据并行模式执行动态图模型。

12
目前,``DataParallel`` 仅支持以多进程的方式执行动态图模型。
13

14
支持两种使用方式:
15

16 17 18 19 20 21 22 23 24
1. 使用 ``paddle.distributed.spawn`` 方法启动,例如:

 ``python demo.py`` (spawn need to be called in ``__main__`` method)

2. 使用 ``paddle.distributed.launch`` 方法启动,例如:

``python -m paddle.distributed.launch –selected_gpus=0,1 demo.py``

其中 ``demo.py`` 脚本的代码可以是下面的示例代码。
25 26 27

参数:
    - **Layer** (Layer) - 需要通过数据并行方式执行的模型。
28
    - **strategy** (ParallelStrategy,可选) - (deprecated) 数据并行的策略,包括并行执行的环境配置。默认为None。
29 30 31 32 33 34 35 36 37

返回:支持数据并行的 ``Layer``

返回类型:Layer实例

**代码示例**:

.. code-block:: python

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    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():
        # 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)
        
        loss = dp_layer.scale_loss(loss)
        loss.backward()
        dp_layer.apply_collective_grads()

        adam.step()
        adam.clear_grad()

    if __name__ == '__main__':
        # 1. start by ``paddle.distributed.spawn`` (default)
        dist.spawn(train, nprocs=2)
        # 2. start by ``paddle.distributed.launch``
        # train()
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

.. py:method:: scale_loss(loss)

缩放模型损失值 ``loss`` 。在数据并行模式中,损失值 ``loss`` 需要根据并行训练进程的数目进行缩放。

如果不在数据并行模式下,会直接返回原 ``loss`` 。

参数:
    - **loss** (Variable) - 当前模型的损失值。

返回:缩放后的损失值 ``loss``

返回类型:Variable

**代码示例**

.. code-block:: python

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    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():
        # 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)
        
        loss = dp_layer.scale_loss(loss)
        loss.backward()
        dp_layer.apply_collective_grads()

        adam.step()
        adam.clear_grad()

    if __name__ == '__main__':
        # 1. start by ``paddle.distributed.spawn`` (default)
        dist.spawn(train, nprocs=2)
        # 2. start by ``paddle.distributed.launch``
        # train()
150 151 152 153 154 155 156 157 158 159 160 161


.. py:method:: apply_collective_grads()

AllReduce(规约)参数的梯度值。

返回:无

**代码示例**

.. code-block:: python

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    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():
        # 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)
        
        loss = dp_layer.scale_loss(loss)
        loss.backward()
        dp_layer.apply_collective_grads()

        adam.step()
        adam.clear_grad()

    if __name__ == '__main__':
        # 1. start by ``paddle.distributed.spawn`` (default)
        dist.spawn(train, nprocs=2)
        # 2. start by ``paddle.distributed.launch``
        # train()