未验证 提交 d95293f3 编写于 作者: W Weilong Wu 提交者: GitHub

[Eager] fix 2 fused op test and add retain_grad flag under eager (#43258)

上级 8c3777df
...@@ -26,7 +26,9 @@ from paddle import tensor ...@@ -26,7 +26,9 @@ from paddle import tensor
from paddle.fluid import layers from paddle.fluid import layers
import unittest import unittest
from op_test import OpTest from op_test import OpTest
from paddle.fluid.framework import default_main_program from paddle.fluid.framework import default_main_program, _enable_legacy_dygraph
_enable_legacy_dygraph()
default_main_program().random_seed = 42 default_main_program().random_seed = 42
......
...@@ -21,9 +21,11 @@ import unittest ...@@ -21,9 +21,11 @@ import unittest
from op_test import OpTest, convert_float_to_uint16 from op_test import OpTest, convert_float_to_uint16
from test_sparse_attention_op import get_cuda_version from test_sparse_attention_op import get_cuda_version
from paddle import _C_ops from paddle import _C_ops
from paddle.fluid.framework import default_main_program from paddle.fluid.framework import default_main_program, _enable_legacy_dygraph
from paddle.fluid import core from paddle.fluid import core
_enable_legacy_dygraph()
@unittest.skipIf(not core.is_compiled_with_cuda(), @unittest.skipIf(not core.is_compiled_with_cuda(),
"Paddle is not compiled with CUDA") "Paddle is not compiled with CUDA")
......
...@@ -30,10 +30,10 @@ class TensorFillDiagTensor_Test(unittest.TestCase): ...@@ -30,10 +30,10 @@ class TensorFillDiagTensor_Test(unittest.TestCase):
def test_dim2(self): def test_dim2(self):
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True}) fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
expected_np = np.array( expected_np = np.array([[1, 2, 2], [2, 1, 2], [2, 2, 1],
[[1, 2, 2], [2, 1, 2], [2, 2, 1], [2, 2, 2]]).astype('float32') [2, 2, 2]]).astype('float32')
expected_grad = np.array( expected_grad = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0],
[[0, 1, 1], [1, 0, 1], [1, 1, 0], [1, 1, 1]]).astype('float32') [1, 1, 1]]).astype('float32')
for idx, p in enumerate(self.places): for idx, p in enumerate(self.places):
if idx == 0: if idx == 0:
...@@ -59,10 +59,10 @@ class TensorFillDiagTensor_Test(unittest.TestCase): ...@@ -59,10 +59,10 @@ class TensorFillDiagTensor_Test(unittest.TestCase):
def test_dim2_offset_1(self): def test_dim2_offset_1(self):
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True}) fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
expected_np = np.array( expected_np = np.array([[2, 2, 2], [1, 2, 2], [2, 1, 2],
[[2, 2, 2], [1, 2, 2], [2, 1, 2], [2, 2, 1]]).astype('float32') [2, 2, 1]]).astype('float32')
expected_grad = np.array( expected_grad = np.array([[1, 1, 1], [0, 1, 1], [1, 0, 1],
[[1, 1, 1], [0, 1, 1], [1, 0, 1], [1, 1, 0]]).astype('float32') [1, 1, 0]]).astype('float32')
for idx, p in enumerate(self.places): for idx, p in enumerate(self.places):
if idx == 0: if idx == 0:
...@@ -88,10 +88,10 @@ class TensorFillDiagTensor_Test(unittest.TestCase): ...@@ -88,10 +88,10 @@ class TensorFillDiagTensor_Test(unittest.TestCase):
def test_dim2_offset1(self): def test_dim2_offset1(self):
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True}) fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
expected_np = np.array( expected_np = np.array([[2, 1, 2], [2, 2, 1], [2, 2, 2],
[[2, 1, 2], [2, 2, 1], [2, 2, 2], [2, 2, 2]]).astype('float32') [2, 2, 2]]).astype('float32')
expected_grad = np.array( expected_grad = np.array([[1, 0, 1], [1, 1, 0], [1, 1, 1],
[[1, 0, 1], [1, 1, 0], [1, 1, 1], [1, 1, 1]]).astype('float32') [1, 1, 1]]).astype('float32')
for idx, p in enumerate(self.places): for idx, p in enumerate(self.places):
if idx == 0: if idx == 0:
...@@ -117,16 +117,20 @@ class TensorFillDiagTensor_Test(unittest.TestCase): ...@@ -117,16 +117,20 @@ class TensorFillDiagTensor_Test(unittest.TestCase):
def test_dim4(self): def test_dim4(self):
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True}) fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
expected_np = np.array( expected_np = np.array([[[[0, 3], [2, 2], [2, 2]],
[[[[0, 3], [2, 2], [2, 2]], [[2, 2], [1, 4], [2, 2]], [[2, 2], [1, 4], [2, 2]],
[[2, 2], [2, 2], [2, 5]], [[2, 2], [2, 2], [2, 2]]], [[2, 2], [2, 2], [2, 5]],
[[[6, 9], [2, 2], [2, 2]], [[2, 2], [7, 10], [2, 2]], [[2, 2], [2, 2], [2, 2]]],
[[[6, 9], [2, 2], [2, 2]],
[[2, 2], [7, 10], [2, 2]],
[[2, 2], [2, 2], [8, 11]], [[2, 2], [2, 2], [8, 11]],
[[2, 2], [2, 2], [2, 2]]]]).astype('float32') [[2, 2], [2, 2], [2, 2]]]]).astype('float32')
expected_grad = np.array( expected_grad = np.array([[[[0, 0], [1, 1], [1, 1]],
[[[[0, 0], [1, 1], [1, 1]], [[1, 1], [0, 0], [1, 1]], [[1, 1], [0, 0], [1, 1]],
[[1, 1], [1, 1], [0, 0]], [[1, 1], [1, 1], [1, 1]]], [[1, 1], [1, 1], [0, 0]],
[[[0, 0], [1, 1], [1, 1]], [[1, 1], [0, 0], [1, 1]], [[1, 1], [1, 1], [1, 1]]],
[[[0, 0], [1, 1], [1, 1]],
[[1, 1], [0, 0], [1, 1]],
[[1, 1], [1, 1], [0, 0]], [[1, 1], [1, 1], [0, 0]],
[[1, 1], [1, 1], [1, 1]]]]).astype('float32') [[1, 1], [1, 1], [1, 1]]]]).astype('float32')
...@@ -154,6 +158,7 @@ class TensorFillDiagTensor_Test(unittest.TestCase): ...@@ -154,6 +158,7 @@ class TensorFillDiagTensor_Test(unittest.TestCase):
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": False}) fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": False})
def test_largedim(self): def test_largedim(self):
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
if len(self.places) > 1: if len(self.places) > 1:
bsdim = 1024 bsdim = 1024
fsdim = 128 fsdim = 128
...@@ -175,6 +180,7 @@ class TensorFillDiagTensor_Test(unittest.TestCase): ...@@ -175,6 +180,7 @@ class TensorFillDiagTensor_Test(unittest.TestCase):
self.assertEqual((ny == expected_pred).all(), True) self.assertEqual((ny == expected_pred).all(), True)
self.assertEqual((y.grad == expected_grad).all(), True) self.assertEqual((y.grad == expected_grad).all(), True)
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": False})
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册