From 70cb0a543d01f46e4acbb8e141a5394ff304629e Mon Sep 17 00:00:00 2001 From: Jack Zhou Date: Wed, 10 Nov 2021 14:41:50 +0800 Subject: [PATCH] Fix rnn grad bug in cpu when dropout is zero (#37080) (#37086) * fix rnn grad bug when num_layers is set 2 and dropout_prob is set 0 * add more test for rnn --- paddle/fluid/operators/rnn_op.h | 3 ++ .../fluid/tests/unittests/test_rnn_op.py | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/paddle/fluid/operators/rnn_op.h b/paddle/fluid/operators/rnn_op.h index ab64e50d450..5e19be5e4cf 100644 --- a/paddle/fluid/operators/rnn_op.h +++ b/paddle/fluid/operators/rnn_op.h @@ -965,6 +965,9 @@ class RNNCPUKernel : public framework::OpKernel { } dropout_mask->mutable_data(output->dims(), ctx.GetPlace()); + auto& dev_ctx = ctx.template device_context(); + math::SetConstant ones; + ones(dev_ctx, dropout_mask, static_cast(1)); // init the output and allocate the memory output->mutable_data(ctx.GetPlace()); int gate_num = 4; diff --git a/python/paddle/fluid/tests/unittests/test_rnn_op.py b/python/paddle/fluid/tests/unittests/test_rnn_op.py index 763ec3e7038..79e33166bb6 100644 --- a/python/paddle/fluid/tests/unittests/test_rnn_op.py +++ b/python/paddle/fluid/tests/unittests/test_rnn_op.py @@ -168,5 +168,35 @@ class TestRNNOp4(TestRNNOp): self.is_bidirec = True +class TestRNNOp5(TestRNNOp): + def set_attrs(self): + self.num_layers = 2 + + +class TestRNNOp6(TestRNNOp): + def set_attrs(self): + self.num_layers = 2 + self.is_bidirec = True + + +class TestRNNOp7(TestRNNOp): + def set_attrs(self): + self.num_layers = 2 + self.is_bidirec = True + self.is_test = True + + +class TestRNNOp8(TestRNNOp): + def set_attrs(self): + self.num_layers = 2 + self.is_bidirec = True + self.sequence_length = None + + +class TestRNNOp9(TestRNNOp): + def set_attrs(self): + self.num_layers = 3 + + if __name__ == '__main__': unittest.main() -- GitLab