test_conditional_block_reshard.py 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

import unittest

import paddle
import paddle.nn.functional as F
19
from paddle import nn
20
from paddle.distributed.fleet import auto
21
from paddle.static import InputSpec
22 23 24


class MLPLayer(nn.Layer):
25 26 27
    def __init__(
        self, hidden_size=64, intermediate_size=4 * 64, initializer_range=0.02
    ):
28
        super().__init__()
29 30 31 32
        self.norm = nn.LayerNorm(hidden_size, epsilon=1e-5)
        self.linear0 = nn.Linear(
            hidden_size,
            intermediate_size,
33 34 35 36 37 38 39
            paddle.ParamAttr(
                initializer=nn.initializer.Normal(
                    mean=0.0, std=initializer_range
                )
            ),
            bias_attr=None,
        )
40 41 42
        self.linear1 = nn.Linear(
            intermediate_size,
            hidden_size,
43 44 45 46 47 48 49
            paddle.ParamAttr(
                initializer=nn.initializer.Normal(
                    mean=0.0, std=initializer_range
                )
            ),
            bias_attr=None,
        )
50 51 52 53

    def forward(self, input):
        out = self.norm(input)

54
        auto.shard_tensor(
55
            self.linear0.weight, auto.ProcessMesh([0, 1], ["x"]), [None, "x"]
56
        )
57 58 59
        out = self.linear0(out)
        out = F.gelu(out, approximate=True)

60
        auto.shard_tensor(
61
            self.linear1.weight, auto.ProcessMesh([0, 1], ["x"]), ["x", None]
62
        )
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        out = self.linear1(out)

        if paddle.mean(out) < 2:
            out = self.norm(out)
            out = self.linear0(out)
            out = F.gelu(out, approximate=True)
            out = self.linear1(out)
        else:
            out = self.norm(out)
            out = self.linear0(out)
            out = self.linear1(out)

        return out


def loss_fn(predict, label):
    error_cost = paddle.nn.functional.square_error_cost(predict, label)
    loss = paddle.mean(error_cost)
    return loss


class TestSubblock(unittest.TestCase):
    def test_subblock(self):

        mlp = MLPLayer()

        strategy = auto.Strategy()
        strategy.auto_mode = "semi"

        engine = auto.Engine(model=mlp, loss=loss_fn, strategy=strategy)

        input_sepc = InputSpec([4, 64], 'float32', 'input')
        label_spec = InputSpec([4, 1], 'float32', 'label')
96 97 98
        engine.prepare(
            inputs_spec=[input_sepc], labels_spec=[label_spec], mode="predict"
        )
99 100 101 102


if __name__ == "__main__":
    unittest.main()