test_auto_parallel_api.py 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   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.

from __future__ import print_function

import unittest
import paddle
import paddle.fluid as fluid
import paddle.nn as nn
import paddle.distributed as dist
22 23
from paddle.distributed.auto_parallel.dist_context import get_default_distributed_context
from paddle.distributed.auto_parallel.process_mesh import ProcessMesh
24 25 26

paddle.enable_static()

27 28
process_mesh1 = [0, 1, 2, 3]
process_mesh2 = [[0, 1, 2], [3, 4, 5]]
29 30 31


class SimpleNet(nn.Layer):
32

33 34 35 36 37 38 39
    def __init__(self, vocab_size=128, hidden_size=4):
        super(SimpleNet, self).__init__()
        self.word_embeddings = nn.Embedding(vocab_size, hidden_size)
        self.dense1 = nn.Linear(hidden_size, hidden_size)
        self.dense2 = nn.Linear(hidden_size, hidden_size // 2)

    def forward(self, x, y):
40
        # Test shard_tensor interface with dist_attr arg
41 42 43 44 45
        x = dist.shard_tensor(x,
                              dist_attr={
                                  "process_mesh": process_mesh1,
                                  "dims_mapping": [0, -1]
                              })
46
        emb_out = self.word_embeddings(x)
47 48
        # Test shard_tensor interface with no dist_attr arg
        y = dist.shard_tensor(y)
49 50 51
        linear1 = self.dense1(y)
        out = self.dense2(linear1)

52
        return x, y
53 54 55


class TestAutoParallelAPI(unittest.TestCase):
56

57
    def test_api(self):
58 59
        dist_context = get_default_distributed_context()

60 61
        net = SimpleNet()
        data1 = fluid.layers.fill_constant(shape=[2, 4], value=1, dtype="int64")
62 63 64 65 66 67
        data2 = fluid.layers.fill_constant(shape=[2, 4],
                                           value=2,
                                           dtype="float32")
        data3 = fluid.layers.fill_constant(shape=[2, 4],
                                           value=4,
                                           dtype="float32")
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
        x, y = net.forward(data1, data2)

        dist_x = dist_context.get_dist_tensor_for_program(x)
        self.assertEqual(dist_x.dist_attr.process_mesh.processes, process_mesh1)
        self.assertEqual(dist_x.dist_attr.dims_mapping, [0, -1])
        self.assertEqual(dist_x.dist_attr.shard_sizes, None)
        self.assertEqual(dist_x.dist_attr.device_placement, None)
        self.assertTrue(dist_x.dist_attr.is_annotated("process_mesh"))
        self.assertTrue(dist_x.dist_attr.is_annotated("dims_mapping"))
        self.assertFalse(dist_x.dist_attr.is_annotated("shard_sizes"))
        self.assertFalse(dist_x.dist_attr.is_annotated("device_placement"))

        dist_y = dist_context.get_dist_tensor_for_program(y)
        self.assertEqual(dist_y.dist_attr.process_mesh, None)
        self.assertEqual(dist_y.dist_attr.dims_mapping, [-1, -1])
        self.assertEqual(dist_y.dist_attr.shard_sizes, None)
        self.assertEqual(dist_y.dist_attr.device_placement, None)
        self.assertFalse(dist_y.dist_attr.is_annotated("process_mesh"))
        self.assertFalse(dist_y.dist_attr.is_annotated("dims_mapping"))
        self.assertFalse(dist_y.dist_attr.is_annotated("shard_sizes"))
        self.assertFalse(dist_y.dist_attr.is_annotated("device_placement"))

        # Test shard_op interface with dist_attr
        dims_mapping1 = [0, 1]
        dims_mapping2 = [-1, 0]
94 95 96 97 98 99 100 101 102 103
        dist_add = dist.shard_op(paddle.add,
                                 dist_attr={
                                     data2: {
                                         "process_mesh": process_mesh2,
                                         "dims_mapping": dims_mapping1
                                     },
                                     data3: {
                                         "dims_mapping": dims_mapping2
                                     }
                                 })
104
        results = dist_add(data2, data3)
105 106 107
        ops = paddle.static.default_main_program().block(0).ops
        last_op = ops[-1]

108 109 110 111
        dist_op = dist_context.get_dist_op_for_program(last_op)
        self.assertEqual(dist_op.dist_attr.process_mesh,
                         ProcessMesh(process_mesh2))
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
112
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
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
        self.assertTrue(dist_op.dist_attr.is_annotated("process_mesh"))

        data2_dist_attr = dist_op.dist_attr.get_input_dist_attr(data2.name)
        self.assertEqual(data2_dist_attr.process_mesh,
                         dist_op.dist_attr.process_mesh)
        self.assertEqual(data2_dist_attr.dims_mapping, dims_mapping1)
        self.assertEqual(data2_dist_attr.shard_sizes, None)
        self.assertEqual(data2_dist_attr.device_placement, None)
        self.assertTrue(data2_dist_attr.is_annotated("process_mesh"))
        self.assertTrue(data2_dist_attr.is_annotated("dims_mapping"))
        self.assertFalse(data2_dist_attr.is_annotated("shard_sizes"))
        self.assertFalse(data2_dist_attr.is_annotated("device_placement"))

        data3_dist_attr = dist_op.dist_attr.get_input_dist_attr(data3.name)
        self.assertEqual(data3_dist_attr.process_mesh,
                         dist_op.dist_attr.process_mesh)
        self.assertEqual(data3_dist_attr.dims_mapping, dims_mapping2)
        self.assertEqual(data3_dist_attr.shard_sizes, None)
        self.assertEqual(data3_dist_attr.device_placement, None)
        self.assertTrue(data3_dist_attr.is_annotated("process_mesh"))
        self.assertTrue(data3_dist_attr.is_annotated("dims_mapping"))
        self.assertFalse(data3_dist_attr.is_annotated("shard_sizes"))
        self.assertFalse(data3_dist_attr.is_annotated("device_placement"))

        # Test shard_op interface with dist_attr
        dist_add = dist.shard_op(paddle.add)
        results = dist_add(data2, data3)
        ops = paddle.static.default_main_program().block(0).ops
        last_op = ops[-1]
        dist_op = dist_context.get_dist_op_for_program(last_op)
        self.assertEqual(dist_op.dist_attr.process_mesh, None)
        self.assertEqual(dist_op.dist_attr.impl_type, "default")
145
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        self.assertFalse(dist_op.dist_attr.is_annotated("process_mesh"))

        data2_dist_attr = dist_op.dist_attr.get_input_dist_attr(data2.name)
        self.assertEqual(data2_dist_attr.process_mesh,
                         dist_op.dist_attr.process_mesh)
        self.assertEqual(data2_dist_attr.dims_mapping, [-1, -1])
        self.assertEqual(data2_dist_attr.shard_sizes, None)
        self.assertEqual(data2_dist_attr.device_placement, None)
        self.assertFalse(data2_dist_attr.is_annotated("process_mesh"))
        self.assertFalse(data2_dist_attr.is_annotated("dims_mapping"))
        self.assertFalse(data2_dist_attr.is_annotated("shard_sizes"))
        self.assertFalse(data2_dist_attr.is_annotated("device_placement"))

        data3_dist_attr = dist_op.dist_attr.get_input_dist_attr(data3.name)
        self.assertEqual(data3_dist_attr.process_mesh,
                         dist_op.dist_attr.process_mesh)
        self.assertEqual(data3_dist_attr.dims_mapping, [-1, -1])
        self.assertEqual(data3_dist_attr.shard_sizes, None)
        self.assertEqual(data3_dist_attr.device_placement, None)
        self.assertFalse(data3_dist_attr.is_annotated("process_mesh"))
        self.assertFalse(data3_dist_attr.is_annotated("dims_mapping"))
        self.assertFalse(data3_dist_attr.is_annotated("shard_sizes"))
        self.assertFalse(data3_dist_attr.is_annotated("device_placement"))
169 170 171 172


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