Skip to content

Commit df2b400

Browse files
author
lilong12
authored
set dim[0] to -1 if dim[0] < 0 during compiling for c_allgather op (#21402) (#21512)
* set dim[0] to -1 if dim[0] < 0 and remove assertion to runtime, test=develop
1 parent 1fbc45b commit df2b400

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

paddle/fluid/operators/collective/c_allgather_op.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CAllGatherOp : public framework::OperatorWithKernel {
2929
PADDLE_ENFORCE_GE(nranks, 2, "nranks should be >=2");
3030
framework::DDim dim = ctx->GetInputDim("X");
3131
dim[0] = dim[0] * nranks;
32+
if (dim[0] < 0) dim[0] = -1;
3233
ctx->SetOutputDim("Out", dim);
3334
}
3435
};

paddle/fluid/operators/collective/c_reducescatter_op.cu.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class CReduceScatterOpCUDAKernel : public framework::OpKernel<T> {
3636
int nranks = comm->nranks();
3737

3838
auto out_dims = in->dims();
39+
PADDLE_ENFORCE_EQ(out_dims[0] % nranks, 0,
40+
platform::errors::InvalidArgument(
41+
"The input tensor X's "
42+
"dim[0] (%d) should be divisible by nranks(%d)",
43+
out_dims[0], nranks));
3944
out_dims[0] = out_dims[0] / nranks;
4045
out->mutable_data<T>(out_dims, place);
4146

python/paddle/fluid/layers/collective.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _c_reducescatter(x, nranks, ring_id=0, use_calc_stream=False):
134134
if not isinstance(x, Variable):
135135
raise TypeError('x must be a Variable')
136136

137-
if x.shape[0] % nranks != 0:
137+
if x.shape[0] > 0 and x.shape[0] % nranks != 0:
138138
raise ValueError('x.shape[0](%d) cannot be evenly divided by nranks(%d)'
139139
% (x.shape[0], nranks))
140140

python/paddle/fluid/tests/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if(NOT WITH_GPU OR WIN32)
2626
LIST(REMOVE_ITEM TEST_OPS test_allreduce)
2727
LIST(REMOVE_ITEM TEST_OPS test_broadcast)
2828
LIST(REMOVE_ITEM TEST_OPS test_reducescatter)
29+
LIST(REMOVE_ITEM TEST_OPS test_reducescatter_api)
2930
endif()
3031

3132
if(WIN32)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2018 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 print_function
16+
17+
import numpy as np
18+
import argparse
19+
import os
20+
import sys
21+
import signal
22+
import time
23+
from contextlib import closing
24+
from six import string_types
25+
import math
26+
import paddle
27+
import paddle.fluid as fluid
28+
import paddle.fluid.profiler as profiler
29+
import paddle.fluid.unique_name as nameGen
30+
from paddle.fluid import core
31+
import unittest
32+
from multiprocessing import Process
33+
import paddle.fluid.layers as layers
34+
from functools import reduce
35+
from test_collective_base import TestCollectiveRunnerBase, runtime_main
36+
37+
38+
class TestCollectiveReduceScatter(TestCollectiveRunnerBase):
39+
def __init__(self):
40+
self.global_ring_id = 0
41+
42+
def get_model(self, main_prog, startup_program):
43+
ring_id = 0
44+
nranks = 2
45+
with fluid.program_guard(main_prog, startup_program):
46+
tindata = layers.data(
47+
name="tindata", shape=[10, 1000], dtype='float32')
48+
toutdata = fluid.layers.collective._c_reducescatter(tindata, nranks)
49+
return toutdata
50+
51+
52+
if __name__ == "__main__":
53+
runtime_main(TestCollectiveReduceScatter, "reducescatter", 0)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2018 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 print_function
16+
import unittest
17+
import numpy as np
18+
import paddle.fluid as fluid
19+
20+
from test_collective_base import TestDistBase
21+
22+
23+
class TestReduceScatterAPI(TestDistBase):
24+
def _setup_config(self):
25+
pass
26+
27+
def test_reducescatter(self, col_type="reduce_scatter"):
28+
self.check_with_place("collective_reducescatter.py", col_type)
29+
30+
def test_reducescatter_with_error(self):
31+
nranks = 2
32+
tindata = fluid.data(name="tindata", shape=[5, 1000], dtype='float32')
33+
try:
34+
toutdata = fluid.layers.collective._c_reducescatter(tindata, nranks)
35+
except ValueError:
36+
pass
37+
38+
39+
if __name__ == '__main__':
40+
unittest.main()

0 commit comments

Comments
 (0)