Skip to content

Commit

Permalink
fix model parallel test, test=allcase
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxicoding committed Aug 27, 2021
1 parent eb9f0dd commit babd8a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
29 changes: 19 additions & 10 deletions python/paddle/fluid/tests/unittests/static_model_parallel_by_col.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,38 @@
#fluid.default_main_program().random_seed = 1


def get_param_attr(weight, bias):
weight_attr = paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(weight))
bias_attr = paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(bias))
return weight_attr, bias_attr


def create_model(data, rank):
np.random.seed(2021)
np_weight = np.random.uniform(-1, 1, size=(IN_SIZE, OUT_SIZE)).astype(DTYPE)
np_bias = np.random.uniform(-1, 1, size=(OUT_SIZE, )).astype(DTYPE)
if rank is not None:
start_col = 0 if rank == 0 else OUT_SIZE // 2
np_weight_part = np_weight[:, start_col:start_col + OUT_SIZE // 2]
np_bias_part = np_bias[start_col:start_col + OUT_SIZE // 2]

weight_attr, bias_attr = get_param_attr(np_weight_part, np_bias_part)
result = paddle.distributed.split(
data,
size=(IN_SIZE, OUT_SIZE),
operation='linear',
axis=1,
num_partitions=MODEL_PARALLEL_SIZE,
weight_attr=paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(
np_weight_part)),
bias_attr=False, )
weight_attr=weight_attr,
bias_attr=bias_attr)
else:
result = fluid.layers.fc(
data,
size=OUT_SIZE,
param_attr=paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(np_weight)),
bias_attr=False, )
weight_attr, bias_attr = get_param_attr(np_weight, np_bias)
result = fluid.layers.fc(data,
size=OUT_SIZE,
param_attr=weight_attr,
bias_attr=bias_attr)

predict = paddle.sum(result)
return predict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,39 @@
#fluid.default_main_program().random_seed = 1


def get_param_attr(weight, bias):
weight_attr = paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(weight))
bias_attr = paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(bias))
return weight_attr, bias_attr


def create_model(data, rank):
np.random.seed(2021)
np_weight = np.random.uniform(-1, 1, size=(IN_SIZE, OUT_SIZE)).astype(DTYPE)
np_bias = np.random.uniform(-1, 1, size=(OUT_SIZE, )).astype(DTYPE)
if rank is not None:
start_row = 0 if rank == 0 else IN_SIZE // 2
np_weight_part = np_weight[start_row:start_row + IN_SIZE // 2, :]

weight_attr, bias_attr = get_param_attr(np_weight_part, np_bias)
result = paddle.distributed.split(
data,
size=(IN_SIZE, OUT_SIZE),
operation='linear',
axis=0,
num_partitions=MODEL_PARALLEL_SIZE,
weight_attr=paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(
np_weight_part)),
bias_attr=False, )
weight_attr=weight_attr,
bias_attr=bias_attr)
else:
weight_attr, bias_attr = get_param_attr(np_weight, np_bias)
result = fluid.layers.fc(
data,
size=OUT_SIZE,
param_attr=paddle.ParamAttr(
initializer=fluid.initializer.NumpyArrayInitializer(np_weight)),
bias_attr=False, )
bias_attr=bias_attr)

predict = paddle.sum(result)
return predict
Expand Down

1 comment on commit babd8a3

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.