Skip to content

Commit

Permalink
Adjust the relative error of QR's grad (#44785)
Browse files Browse the repository at this point in the history
* Adjust the relative error of QR's grad (#42221)

* Fix the format
  • Loading branch information
aoyulong committed Aug 3, 2022
1 parent cd59df5 commit 627e5bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
44 changes: 27 additions & 17 deletions python/paddle/fluid/tests/unittests/test_qr_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@


class TestQrOp(OpTest):

def setUp(self):
paddle.enable_static()
np.random.seed(4)
np.random.seed(7)
self.op_type = "qr"
a, q, r = self.get_input_and_output()
self.inputs = {"X": a}
Expand Down Expand Up @@ -74,30 +75,37 @@ def test_check_output(self):
self.check_output()

def test_check_grad_normal(self):
self.check_grad(['X'], ['Q', 'R'])
self.check_grad(['X'], ['Q', 'R'],
numeric_grad_delta=1e-5,
max_relative_error=1e-6)


class TestQrOpCase1(TestQrOp):

def get_shape(self):
return (10, 12)


class TestQrOpCase2(TestQrOp):

def get_shape(self):
return (16, 15)


class TestQrOpCase3(TestQrOp):

def get_shape(self):
return (2, 12, 16)


class TestQrOpCase4(TestQrOp):

def get_shape(self):
return (3, 16, 15)


class TestQrOpCase5(TestQrOp):

def get_mode(self):
return "complete"

Expand All @@ -106,6 +114,7 @@ def get_shape(self):


class TestQrOpCase6(TestQrOp):

def get_mode(self):
return "complete"

Expand All @@ -114,8 +123,10 @@ def get_shape(self):


class TestQrAPI(unittest.TestCase):

def test_dygraph(self):
paddle.disable_static()
np.random.seed(7)

def run_qr_dygraph(shape, mode, dtype):
if dtype == "float32":
Expand Down Expand Up @@ -174,12 +185,13 @@ def run_qr_dygraph(shape, mode, dtype):
]
modes = ["reduced", "complete", "r"]
dtypes = ["float32", "float64"]
for tensor_shape, mode, dtype in itertools.product(tensor_shapes, modes,
dtypes):
for tensor_shape, mode, dtype in itertools.product(
tensor_shapes, modes, dtypes):
run_qr_dygraph(tensor_shape, mode, dtype)

def test_static(self):
paddle.enable_static()
np.random.seed(7)

def run_qr_static(shape, mode, dtype):
if dtype == "float32":
Expand Down Expand Up @@ -216,29 +228,27 @@ def run_qr_static(shape, mode, dtype):
tmp_q, tmp_r = np.linalg.qr(a[coord], mode=mode)
np_q[coord] = tmp_q
np_r[coord] = tmp_r
x = paddle.fluid.data(
name="input", shape=shape, dtype=dtype)
x = paddle.fluid.data(name="input",
shape=shape,
dtype=dtype)
if mode == "r":
r = paddle.linalg.qr(x, mode=mode)
exe = fluid.Executor(place)
fetches = exe.run(fluid.default_main_program(),
feed={"input": a},
fetch_list=[r])
self.assertTrue(
np.allclose(
fetches[0], np_r, atol=1e-5))
self.assertTrue(np.allclose(fetches[0], np_r,
atol=1e-5))
else:
q, r = paddle.linalg.qr(x, mode=mode)
exe = fluid.Executor(place)
fetches = exe.run(fluid.default_main_program(),
feed={"input": a},
fetch_list=[q, r])
self.assertTrue(
np.allclose(
fetches[0], np_q, atol=1e-5))
self.assertTrue(
np.allclose(
fetches[1], np_r, atol=1e-5))
self.assertTrue(np.allclose(fetches[0], np_q,
atol=1e-5))
self.assertTrue(np.allclose(fetches[1], np_r,
atol=1e-5))

tensor_shapes = [
(3, 5),
Expand All @@ -253,8 +263,8 @@ def run_qr_static(shape, mode, dtype):
]
modes = ["reduced", "complete", "r"]
dtypes = ["float32", "float64"]
for tensor_shape, mode, dtype in itertools.product(tensor_shapes, modes,
dtypes):
for tensor_shape, mode, dtype in itertools.product(
tensor_shapes, modes, dtypes):
run_qr_static(tensor_shape, mode, dtype)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'matrix_power', \
'cholesky_solve', \
'solve', \
'qr', \
]

NEED_FIX_FP64_CHECK_OUTPUT_THRESHOLD_OP_LIST = ['bilinear_interp',\
Expand Down

0 comments on commit 627e5bd

Please sign in to comment.