test_auto_parallel_api.py 7.5 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 32 33 34 35 36 37 38


class SimpleNet(nn.Layer):
    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):
39 40 41 42 43
        # Test shard_tensor interface with dist_attr arg
        x = dist.shard_tensor(
            x,
            dist_attr={"process_mesh": process_mesh1,
                       "dims_mapping": [0, -1]})
44
        emb_out = self.word_embeddings(x)
45 46
        # Test shard_tensor interface with no dist_attr arg
        y = dist.shard_tensor(y)
47 48 49
        linear1 = self.dense1(y)
        out = self.dense2(linear1)

50
        return x, y
51 52 53 54


class TestAutoParallelAPI(unittest.TestCase):
    def test_api(self):
55 56
        dist_context = get_default_distributed_context()

57 58 59 60 61 62 63
        net = SimpleNet()
        data1 = fluid.layers.fill_constant(shape=[2, 4], value=1, dtype="int64")
        data2 = fluid.layers.fill_constant(
            shape=[2, 4], value=2, dtype="float32")
        data3 = fluid.layers.fill_constant(
            shape=[2, 4], value=4, dtype="float32")

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
        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]
        dist_add = dist.shard_op(
90
            paddle.add,
91 92 93 94 95 96 97 98 99 100
            dist_attr={
                data2: {
                    "process_mesh": process_mesh2,
                    "dims_mapping": dims_mapping1
                },
                data3: {
                    "dims_mapping": dims_mapping2
                }
            })
        results = dist_add(data2, data3)
101 102 103
        ops = paddle.static.default_main_program().block(0).ops
        last_op = ops[-1]

104 105 106 107
        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")
108
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
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
        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")
141
        self.assertEqual(dist_op.dist_attr.impl_idx, 0)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        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"))
165 166 167 168


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