test_interface.py 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2021 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
16

17 18
import paddle
import paddle.nn.functional as F
19
from paddle import nn, static
20 21
from paddle.distributed.auto_parallel.process_mesh import ProcessMesh
from paddle.distributed.auto_parallel.static.dist_context import (
22 23
    get_default_distributed_context,
)
24
from paddle.distributed.fleet import auto
25 26 27 28 29 30 31

paddle.enable_static()

batch_size = 4
epoch_num = 10
hidden_size = 1024
sequence_len = 512
32 33 34
process_mesh1 = ProcessMesh(
    mesh=[[0, 1, 2, 3], [4, 5, 6, 7]], dim_names=["x", "y"]
)
35 36 37 38
process_mesh2 = ProcessMesh(mesh=[0, 1, 2, 3], dim_names=["x"])


class MLPLayer(nn.Layer):
39 40 41 42 43 44 45
    def __init__(
        self,
        hidden_size=1024,
        intermediate_size=4 * 1024,
        dropout_ratio=0.1,
        initializer_range=0.02,
    ):
46
        super().__init__()
47 48
        d_model = hidden_size
        dim_feedforward = intermediate_size
49 50 51
        param_initializer = nn.initializer.Normal(
            mean=0.0, std=initializer_range
        )
52 53 54 55 56

        self.linear0 = nn.Linear(
            d_model,
            dim_feedforward,
            weight_attr=paddle.ParamAttr(initializer=param_initializer),
57 58
            bias_attr=None,
        )
59 60 61 62
        self.linear1 = nn.Linear(
            dim_feedforward,
            d_model,
            weight_attr=paddle.ParamAttr(initializer=param_initializer),
63 64
            bias_attr=None,
        )
65 66 67

    def forward(self, input):
        auto.shard_tensor(self.linear0.weight, process_mesh1[0], [None, "y"])
68 69 70 71 72 73
        linear0 = auto.shard_op(
            self.linear0,
            process_mesh1,
            [["y", None, None]],
            [[None, "x", None]],
        )
74 75 76 77 78 79
        linear0_out = linear0(input)

        gelu = auto.shard_op(F.gelu, process_mesh1, [["y", "x", None], None])
        gelu_out = gelu(linear0_out, approximate=True)

        auto.shard_tensor(self.linear1.weight, shard_spec=["y", None])
80 81 82
        linear1 = auto.shard_op(
            self.linear1, process_mesh1[1], out_shard_specs=[["y", None, None]]
        )
83 84 85 86 87 88 89 90
        linear1_out = linear1(gelu_out)

        return self.linear0, self.linear1, linear0_out, gelu_out, linear1_out


class TestAutoParallelAPI(unittest.TestCase):
    def test_api(self):
        # input
91 92 93 94 95 96 97 98
        input = static.data(
            name="input",
            shape=[batch_size, sequence_len, hidden_size],
            dtype='float32',
        )
        label = static.data(
            name="label", shape=[batch_size, sequence_len, 1], dtype='float32'
        )
99 100 101 102

        auto.shard_tensor(input, process_mesh1, ["x", None, None])
        auto.shard_tensor(label, process_mesh1, ["y", None, None])

103 104 105 106 107 108
        mlp = MLPLayer(
            hidden_size=hidden_size,
            intermediate_size=4 * hidden_size,
            dropout_ratio=0.1,
            initializer_range=0.02,
        )
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

        with ProcessMesh(process_mesh1.mesh, process_mesh1.dim_names):
            linear0, linear1, linear0_out, gelu_out, linear1_out = mlp(input)

        default_program = paddle.fluid.default_main_program()
        default_dist_context = get_default_distributed_context()

        self.assertEqual(len(default_program.blocks[0].ops), 5)
        matmul0 = default_program.blocks[0].ops[0]
        self.assertEqual(matmul0.type, "matmul_v2")
        ewise_add0 = default_program.blocks[0].ops[1]
        self.assertEqual(ewise_add0.type, "elementwise_add")
        gelu = default_program.blocks[0].ops[2]
        self.assertEqual(gelu.type, "gelu")
        matmul1 = default_program.blocks[0].ops[3]
        self.assertEqual(matmul1.type, "matmul_v2")
        ewise_add1 = default_program.blocks[0].ops[4]
        self.assertEqual(ewise_add1.type, "elementwise_add")

        dist_input = default_dist_context.get_dist_tensor_for_program(input)
        self.assertEqual(dist_input.dist_attr.process_mesh, process_mesh1)
        self.assertEqual(dist_input.dist_attr.dims_mapping, [0, -1, -1])
        self.assertTrue(dist_input.dist_attr.is_annotated("process_mesh"))
        self.assertTrue(dist_input.dist_attr.is_annotated("dims_mapping"))

        dist_input = default_dist_context.get_dist_tensor_for_program(label)
        self.assertEqual(dist_input.dist_attr.process_mesh, process_mesh1)
        self.assertEqual(dist_input.dist_attr.dims_mapping, [1, -1, -1])
        self.assertTrue(dist_input.dist_attr.is_annotated("process_mesh"))
        self.assertTrue(dist_input.dist_attr.is_annotated("dims_mapping"))

        dist_linear0_weight = default_dist_context.get_dist_tensor_for_program(
141 142 143 144 145
            linear0.weight
        )
        self.assertEqual(
            dist_linear0_weight.dist_attr.process_mesh, process_mesh1[0]
        )
146 147
        self.assertEqual(dist_linear0_weight.dist_attr.dims_mapping, [-1, 0])
        self.assertTrue(
148 149
            dist_linear0_weight.dist_attr.is_annotated("process_mesh")
        )
150
        self.assertTrue(
151 152
            dist_linear0_weight.dist_attr.is_annotated("dims_mapping")
        )
153 154

        dist_linear1_weight = default_dist_context.get_dist_tensor_for_program(
155 156 157 158 159
            linear1.weight
        )
        self.assertEqual(
            dist_linear1_weight.dist_attr.process_mesh, process_mesh1
        )
160 161
        self.assertEqual(dist_linear1_weight.dist_attr.dims_mapping, [1, -1])
        self.assertTrue(
162 163
            dist_linear1_weight.dist_attr.is_annotated("process_mesh")
        )
164
        self.assertTrue(
165 166
            dist_linear1_weight.dist_attr.is_annotated("dims_mapping")
        )
167 168

        dist_linear1_out = default_dist_context.get_dist_tensor_for_program(
169 170
            linear1_out
        )
171 172 173 174
        self.assertEqual(dist_linear1_out.dist_attr.process_mesh, process_mesh1)
        self.assertEqual(dist_linear1_out.dist_attr.dims_mapping, [-1, -1, -1])
        self.assertTrue(dist_linear1_out.dist_attr.is_annotated("process_mesh"))
        self.assertFalse(
175 176
            dist_linear1_out.dist_attr.is_annotated("dims_mapping")
        )
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

        dist_op = default_dist_context.get_dist_op_for_program(matmul0)
        self.assertEqual(dist_op.dist_attr.process_mesh, process_mesh1)
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
        self.assertTrue(dist_op.dist_attr.is_annotated("process_mesh"))
        tensor_dist_attr = dist_op.dist_attr.get_input_dist_attr(input.name)
        self.assertEqual(tensor_dist_attr.process_mesh, process_mesh1)
        self.assertEqual(tensor_dist_attr.dims_mapping, [1, -1, -1])
        self.assertTrue(tensor_dist_attr.is_annotated("process_mesh"))
        self.assertTrue(tensor_dist_attr.is_annotated("dims_mapping"))

        dist_op = default_dist_context.get_dist_op_for_program(ewise_add0)
        self.assertEqual(dist_op.dist_attr.process_mesh, process_mesh1)
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
        tensor_dist_attr = dist_op.dist_attr.get_output_dist_attr(
194 195
            linear0_out.name
        )
196 197 198 199 200 201 202 203 204 205 206 207
        self.assertEqual(tensor_dist_attr.process_mesh, process_mesh1)
        self.assertEqual(tensor_dist_attr.dims_mapping, [-1, 0, -1])
        self.assertTrue(tensor_dist_attr.is_annotated("process_mesh"))
        self.assertTrue(tensor_dist_attr.is_annotated("dims_mapping"))
        self.assertTrue(dist_op.dist_attr.is_annotated("process_mesh"))

        dist_op = default_dist_context.get_dist_op_for_program(gelu)
        self.assertEqual(dist_op.dist_attr.process_mesh, process_mesh1)
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
        self.assertTrue(dist_op.dist_attr.is_annotated("process_mesh"))
        tensor_dist_attr = dist_op.dist_attr.get_input_dist_attr(
208 209
            linear0_out.name
        )
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        self.assertEqual(tensor_dist_attr.process_mesh, process_mesh1)
        self.assertEqual(tensor_dist_attr.dims_mapping, [1, 0, -1])
        self.assertTrue(tensor_dist_attr.is_annotated("process_mesh"))
        self.assertTrue(tensor_dist_attr.is_annotated("dims_mapping"))
        tensor_dist_attr = dist_op.dist_attr.get_output_dist_attr(gelu_out.name)
        self.assertEqual(tensor_dist_attr.process_mesh, process_mesh1)
        self.assertEqual(tensor_dist_attr.dims_mapping, [-1, -1, -1])
        self.assertTrue(tensor_dist_attr.is_annotated("process_mesh"))
        self.assertFalse(tensor_dist_attr.is_annotated("dims_mapping"))

        dist_op = default_dist_context.get_dist_op_for_program(matmul1)
        self.assertEqual(dist_op.dist_attr.process_mesh, process_mesh1[1])
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
        self.assertTrue(dist_op.dist_attr.is_annotated("process_mesh"))
        tensor_dist_attr = dist_op.dist_attr.get_input_dist_attr(gelu_out.name)
        self.assertEqual(tensor_dist_attr.process_mesh, process_mesh1[1])
        self.assertEqual(tensor_dist_attr.dims_mapping, [-1, -1, -1])
        self.assertTrue(tensor_dist_attr.is_annotated("process_mesh"))
        self.assertFalse(tensor_dist_attr.is_annotated("dims_mapping"))

        dist_op = default_dist_context.get_dist_op_for_program(ewise_add1)
        self.assertEqual(dist_op.dist_attr.process_mesh, process_mesh1[1])
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
        self.assertTrue(dist_op.dist_attr.is_annotated("process_mesh"))
        tensor_dist_attr = dist_op.dist_attr.get_output_dist_attr(
237 238
            linear1_out.name
        )
239 240 241 242 243 244 245 246
        self.assertEqual(tensor_dist_attr.process_mesh, process_mesh1[1])
        self.assertEqual(tensor_dist_attr.dims_mapping, [0, -1, -1])
        self.assertTrue(tensor_dist_attr.is_annotated("process_mesh"))
        self.assertTrue(tensor_dist_attr.is_annotated("dims_mapping"))


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