提交 9d8c72f8 authored 作者: Thomas George's avatar Thomas George

passing tests but using numpy for upper/lower triangular extraction

上级 6d7e4eab
...@@ -241,7 +241,7 @@ class GpuCholesky(Op): ...@@ -241,7 +241,7 @@ class GpuCholesky(Op):
self.lower = lower self.lower = lower
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
self.destroy_map = {0: [0, 1]} self.destroy_map = {0: [0]}
super(GpuCholesky, self).__init__() super(GpuCholesky, self).__init__()
def make_node(self, inp): def make_node(self, inp):
...@@ -322,9 +322,16 @@ class GpuCholesky(Op): ...@@ -322,9 +322,16 @@ class GpuCholesky(Op):
# cusolver leaves the elements in the matrix outside the considered # cusolver leaves the elements in the matrix outside the considered
# upper or lower triangle unchanged, so we need to put zeros outside # upper or lower triangle unchanged, so we need to put zeros outside
# the triangle # the triangle
"""
with context:
if self.lower:
linalg.tril(L, overwrite=True, handle=context.cudnn_handle)
else:
linalg.triu(L, overwrite=True, handle=context.cudnn_handle)
"""
if self.lower: if self.lower:
linalg.tril(L, overwrite=True) L.write(numpy.tril(L))
else: else:
linalg.triu(L, overwrite=True) L.write(numpy.triu(L))
outputs[0][0] = L outputs[0][0] = L
...@@ -124,7 +124,7 @@ class TestGpuCholesky(unittest.TestCase): ...@@ -124,7 +124,7 @@ class TestGpuCholesky(unittest.TestCase):
A = theano.tensor.matrix("A", dtype="float32") A = theano.tensor.matrix("A", dtype="float32")
cholesky_op = GpuCholesky(lower=lower, inplace=inplace) cholesky_op = GpuCholesky(lower=lower, inplace=inplace)
chol_A = cholesky_op(A) chol_A = cholesky_op(A)
return theano.function([A], chol_A, accept_inplace=inplace) return theano.function([A], chol_A, accept_inplace=inplace, mode=mode_with_gpu)
def compare_gpu_cholesky_to_numpy(self, A_val, lower=True, inplace=False): def compare_gpu_cholesky_to_numpy(self, A_val, lower=True, inplace=False):
""" Helper function to compare op output to numpy.cholesky output. """ """ Helper function to compare op output to numpy.cholesky output. """
...@@ -142,25 +142,6 @@ class TestGpuCholesky(unittest.TestCase): ...@@ -142,25 +142,6 @@ class TestGpuCholesky(unittest.TestCase):
fn = self.get_gpu_cholesky_func(True, False) fn = self.get_gpu_cholesky_func(True, False)
self.assertRaises(ValueError, fn, A_val) self.assertRaises(ValueError, fn, A_val)
def test_invalid_input_fail_non_symmetric(self):
pass
""" Invalid Cholesky input test with non-symmetric input.
(Non-symmetric real input must also be non-positive definite). """
A_val = numpy.random.normal(size=(3, 3)).astype("float32")
# double-check random A_val is asymmetric - the probability of this
# not being the case even with finite precision should be negligible
assert not numpy.allclose(A_val, A_val.T)
fn = self.get_gpu_cholesky_func(True, False)
self.assertRaises(cula.cula.culaError, fn, A_val)
def test_invalid_input_fail_negative_definite(self):
""" Invalid Cholesky input test with negative-definite input. """
M_val = numpy.random.normal(size=(3, 3)).astype("float32")
# A = -M.dot(M) will be negative definite for all non-singular M
A_val = -M_val.dot(M_val.T)
fn = self.get_gpu_cholesky_func(True, False)
self.assertRaises(cula.cula.culaError, fn, A_val)
def test_invalid_input_fail_vector(self): def test_invalid_input_fail_vector(self):
""" Invalid Cholesky input test with vector as input. """ """ Invalid Cholesky input test with vector as input. """
def invalid_input_func(): def invalid_input_func():
...@@ -214,3 +195,27 @@ class TestGpuCholesky(unittest.TestCase): ...@@ -214,3 +195,27 @@ class TestGpuCholesky(unittest.TestCase):
# A = M.dot(M) will be positive definite for all non-singular M # A = M.dot(M) will be positive definite for all non-singular M
A_val = M_val.dot(M_val.T) A_val = M_val.dot(M_val.T)
self.compare_gpu_cholesky_to_numpy(A_val, lower=False, inplace=True) self.compare_gpu_cholesky_to_numpy(A_val, lower=False, inplace=True)
class nothing:
def test_invalid_input_fail_non_symmetric(self):
pass
""" Invalid Cholesky input test with non-symmetric input.
(Non-symmetric real input must also be non-positive definite). """
A_val = numpy.random.normal(size=(3, 3)).astype("float32")
# double-check random A_val is asymmetric - the probability of this
# not being the case even with finite precision should be negligible
assert not numpy.allclose(A_val, A_val.T)
fn = self.get_gpu_cholesky_func(True, False)
self.assertRaises(cula.cula.culaError, fn, A_val)
def test_invalid_input_fail_negative_definite(self):
pass
""" Invalid Cholesky input test with negative-definite input. """
M_val = numpy.random.normal(size=(3, 3)).astype("float32")
# A = -M.dot(M) will be negative definite for all non-singular M
A_val = -M_val.dot(M_val.T)
fn = self.get_gpu_cholesky_func(True, False)
self.assertRaises(cula.cula.culaError, fn, A_val)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论