Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dropout static when axis != None #37223

Merged
merged 16 commits into from
Nov 26, 2021
14 changes: 12 additions & 2 deletions python/paddle/fluid/tests/unittests/test_dropout_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def setUp(self):

def check_static_result(self, place):
with fluid.program_guard(fluid.Program(), fluid.Program()):
input = fluid.data(name="input", shape=[40, 40], dtype="float32")
input = fluid.data(name="input", shape=[-1, -1], dtype="float32")
res1 = paddle.nn.functional.dropout(x=input, p=0., training=False)
res2 = paddle.nn.functional.dropout(
x=input, p=0., axis=0, training=True, mode='upscale_in_train')
Expand Down Expand Up @@ -380,7 +380,11 @@ def check_static_result(self, place):
training=False,
mode='upscale_in_train')

in_np = np.random.random([40, 40]).astype("float32")
res13 = paddle.nn.functional.dropout(
x=input, p=0.7, axis=1, training=True, mode='upscale_in_train')

in_np = np.ones([40, 40]).astype("float32")
in_np2 = np.ones([1, 250000000]).astype("float32")
Copy link
Contributor

Choose a reason for hiding this comment

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

因为这个指定axis的分支不会走dropout op,感觉加的单测不用和去dropout OP比较,能跑下来就行,这个大size的单测可能会带来其他问题

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

res_np = in_np
res_np2 = np.zeros_like(in_np)

Expand All @@ -398,6 +402,12 @@ def check_static_result(self, place):
feed={"input": in_np},
fetch_list=[res10])
self.assertTrue(np.allclose(fetches2[0], res_np2))
fetches3 = exe.run(fluid.default_main_program(),
feed={"input": in_np2},
fetch_list=[res13])
self.assertTrue(
np.isclose(
np.sum(fetches3[0] == 0) / np.sum(in_np2), 0.7, atol=1e-04))

def test_static(self):
for place in self.places:
Expand Down
10 changes: 8 additions & 2 deletions python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ def get_attrs(prog, dropout_prob, is_test, seed):

#get mask shape
input_shape = x.shape
if not in_dygraph_mode():
input_shape_tensor = paddle.shape(x)
drop_axes = [axis] if isinstance(axis, int) else list(axis)
if min(drop_axes) < 0 or max(drop_axes) > len(input_shape) - 1:
raise ValueError("axis value should be greater than or equal to 0 and less than dimensions of x:{}, but get axis value:{} " \
Expand All @@ -949,8 +951,12 @@ def get_attrs(prog, dropout_prob, is_test, seed):
"length of axis should not be greater than dimensions of x:{}, but get length of axis: {}".
format(len(input_shape), len(drop_axes)))
mask_shape = [1] * len(input_shape)
for i in drop_axes:
mask_shape[i] = input_shape[i]
if not in_dygraph_mode():
for i in drop_axes:
mask_shape[i] = input_shape_tensor[i]
else:
for i in drop_axes:
mask_shape[i] = input_shape[i]

#get mask
random_tensor = paddle.uniform(
Expand Down