Skip to content

Commit

Permalink
Fix Python IndexError of Case14: paddle.nn.functional.glu (#50016)
Browse files Browse the repository at this point in the history
* 为split增加取值范围维度的判断

* 为glu的axis进行取值判断并添加单测

* 完善glu的单测

* fix glu
  • Loading branch information
longranger2 authored Feb 10, 2023
1 parent 3374600 commit 62fe3cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/paddle/fluid/tests/unittests/test_glu.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,15 @@ def test_case(self):
self.check_identity(fluid.CUDAPlace(0))


class TestGlu(unittest.TestCase):
def glu_axis_size(self):
paddle.enable_static()
x = paddle.static.data(name='x', shape=[1, 2, 3], dtype='float32')
paddle.nn.functional.glu(x, axis=256)

def test_errors(self):
self.assertRaises(ValueError, self.glu_axis_size)


if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions python/paddle/nn/functional/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,13 @@ def glu(x, axis=-1, name=None):
check_variable_and_dtype(
x, 'input', ['float16', 'float32', 'float64'], "glu"
)
rank = len(x.shape)
if not (-rank <= axis < rank):
raise ValueError(
"Expected value range of `axis` is [{}, {}), but received axis: {}".format(
-rank, rank, axis
)
)
a, b = chunk(x, 2, axis=axis, name=name)
gate = sigmoid(b, name=name)
out = paddle.multiply(a, gate, name=name)
Expand Down

0 comments on commit 62fe3cf

Please sign in to comment.