|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import division |
| 16 | +from __future__ import print_function |
| 17 | + |
| 18 | +import unittest |
| 19 | + |
| 20 | +import paddle |
| 21 | +import numpy as np |
| 22 | +import random |
| 23 | +import paddle.distributed as dist |
| 24 | +import paddle.fluid as fluid |
| 25 | +import paddle.distributed.fleet as fleet |
| 26 | +from paddle import framework |
| 27 | +import os |
| 28 | + |
| 29 | +paddle.enable_static() |
| 30 | + |
| 31 | + |
| 32 | +class ColumnLinearNet(fluid.dygraph.Layer): |
| 33 | + def __init__(self, input_size, output_size): |
| 34 | + super(ColumnLinearNet, self).__init__() |
| 35 | + self.parallel_linear = fleet.meta_parallel.ColumnParallelLinear( |
| 36 | + in_features=input_size, |
| 37 | + out_features=output_size, |
| 38 | + weight_attr=None, |
| 39 | + has_bias=True, |
| 40 | + gather_output=True, |
| 41 | + name="test_column_linear") |
| 42 | + |
| 43 | + def forward(self, x): |
| 44 | + output = self.parallel_linear(x) |
| 45 | + return output |
| 46 | + |
| 47 | + |
| 48 | +class RowLinearNet(fluid.dygraph.Layer): |
| 49 | + def __init__(self, input_size, output_size): |
| 50 | + super(RowLinearNet, self).__init__() |
| 51 | + self.parallel_linear = fleet.meta_parallel.RowParallelLinear( |
| 52 | + in_features=input_size, |
| 53 | + out_features=output_size, |
| 54 | + has_bias=True, |
| 55 | + input_is_parallel=False, |
| 56 | + name="test_row_linear") |
| 57 | + |
| 58 | + def forward(self, x): |
| 59 | + output = self.parallel_linear(x) |
| 60 | + return output |
| 61 | + |
| 62 | + |
| 63 | +class EmbeddingNet(fluid.dygraph.Layer): |
| 64 | + def __init__(self, vocab_size, hidden_size): |
| 65 | + super(EmbeddingNet, self).__init__() |
| 66 | + self.embedding = fleet.meta_parallel.VocabParallelEmbedding(vocab_size, |
| 67 | + hidden_size) |
| 68 | + |
| 69 | + def forward(self, x): |
| 70 | + output = self.embedding(x) |
| 71 | + return output |
| 72 | + |
| 73 | + |
| 74 | +class TestDistTraning(unittest.TestCase): |
| 75 | + def setUp(self): |
| 76 | + os.environ["PADDLE_TRAINER_ID"] = "2" |
| 77 | + os.environ[ |
| 78 | + "PADDLE_TRAINER_ENDPOINTS"] = "127.0.0.1:36001,127.0.0.1:36002,127.0.0.1:36003,127.0.0.1:36004" |
| 79 | + |
| 80 | + strategy = fleet.DistributedStrategy() |
| 81 | + self.model_parallel_size = 2 |
| 82 | + strategy.sharding = True |
| 83 | + strategy.sharding_configs = { |
| 84 | + "mp_degree": self.model_parallel_size, |
| 85 | + "sharding_degree": 2, |
| 86 | + } |
| 87 | + fleet.init(is_collective=True, strategy=strategy) |
| 88 | + |
| 89 | + def get_program(self): |
| 90 | + return paddle.static.Program(), paddle.static.Program() |
| 91 | + |
| 92 | + def test_column_parallel_layer(self): |
| 93 | + main_program, startup_program = self.get_program() |
| 94 | + with paddle.static.program_guard(main_program, startup_program): |
| 95 | + input_size, output_size = 28, 64 |
| 96 | + model_a = ColumnLinearNet(input_size, output_size) |
| 97 | + |
| 98 | + x = paddle.static.data(name='x', shape=[None, input_size]) |
| 99 | + y = model_a(x) |
| 100 | + |
| 101 | + #print(main_program) |
| 102 | + ops = main_program.global_block().ops |
| 103 | + ops = [op.type for op in ops] |
| 104 | + self.assertEqual( |
| 105 | + ops, ['c_identity', 'matmul', 'elementwise_add', 'c_concat']) |
| 106 | + |
| 107 | + weight = model_a.parallel_linear.weight |
| 108 | + bias = model_a.parallel_linear.bias |
| 109 | + self.assertEqual(weight.shape, (input_size, output_size // |
| 110 | + self.model_parallel_size)) |
| 111 | + self.assertEqual(bias.shape, |
| 112 | + (output_size // self.model_parallel_size, )) |
| 113 | + |
| 114 | + def test_row_parallel_layer(self): |
| 115 | + main_program, startup_program = self.get_program() |
| 116 | + with paddle.static.program_guard(main_program, startup_program): |
| 117 | + input_size, output_size = 28, 64 |
| 118 | + model_a = RowLinearNet(input_size, output_size) |
| 119 | + |
| 120 | + x = paddle.static.data(name='x', shape=[None, input_size]) |
| 121 | + y = model_a(x) |
| 122 | + |
| 123 | + #print(main_program) |
| 124 | + ops = main_program.global_block().ops |
| 125 | + ops = [op.type for op in ops] |
| 126 | + self.assertEqual( |
| 127 | + ops, |
| 128 | + ['c_split', 'matmul', 'c_allreduce_sum', 'elementwise_add']) |
| 129 | + |
| 130 | + weight = model_a.parallel_linear.weight |
| 131 | + bias = model_a.parallel_linear.bias |
| 132 | + self.assertEqual(weight.shape, ( |
| 133 | + input_size // self.model_parallel_size, output_size)) |
| 134 | + self.assertEqual(bias.shape, (output_size, )) |
| 135 | + |
| 136 | + def test_parallel_embedding(self): |
| 137 | + main_program, startup_program = self.get_program() |
| 138 | + with paddle.static.program_guard(main_program, startup_program): |
| 139 | + vocab_size, hidden_size = 1000, 512 |
| 140 | + seq_len = 128 |
| 141 | + |
| 142 | + # model_a |
| 143 | + model_a = EmbeddingNet(vocab_size, hidden_size) |
| 144 | + |
| 145 | + x = paddle.static.data( |
| 146 | + name='x', shape=[None, seq_len], dtype='int64') |
| 147 | + y = model_a(x) |
| 148 | + |
| 149 | + #print(main_program) |
| 150 | + ops = main_program.global_block().ops |
| 151 | + ops = [op.type for op in ops] |
| 152 | + self.assertEqual(ops, ['c_embedding', 'c_allreduce_sum']) |
| 153 | + |
| 154 | + weight = model_a.embedding.weight |
| 155 | + self.assertEqual(weight.shape, ( |
| 156 | + vocab_size // self.model_parallel_size, hidden_size)) |
| 157 | + |
| 158 | + def test_parallel_cross_entropy(self): |
| 159 | + main_program, startup_program = self.get_program() |
| 160 | + with paddle.static.program_guard(main_program, startup_program): |
| 161 | + batch_size = 8 |
| 162 | + seq_length = 16 |
| 163 | + class_size = 1000 |
| 164 | + class_size_per_card = class_size // self.model_parallel_size |
| 165 | + |
| 166 | + # model_a |
| 167 | + model_a = fleet.meta_parallel.ParallelCrossEntropy() |
| 168 | + |
| 169 | + x = paddle.static.data( |
| 170 | + name='x', shape=[batch_size, seq_length, class_size_per_card]) |
| 171 | + label = paddle.static.data( |
| 172 | + name='label', shape=[batch_size, seq_length], dtype='int64') |
| 173 | + loss_a = model_a(x, label) |
| 174 | + |
| 175 | + #print(main_program) |
| 176 | + ops = main_program.global_block().ops |
| 177 | + ops = [op.type for op in ops] |
| 178 | + self.assertEqual(ops, |
| 179 | + ['unsqueeze2', 'c_softmax_with_cross_entropy']) |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == '__main__': |
| 183 | + unittest.main() |
0 commit comments