提交 62fcda84 authored 作者: Frederic's avatar Frederic

Reuse test_comparison in gpuarray to test sgn() isnan on GPU. Make gpuarray…

Reuse test_comparison in gpuarray to test sgn() isnan on GPU. Make gpuarray better handle nan. At the same time, enable c code for EQ with complex.
上级 c50df515
...@@ -384,6 +384,14 @@ class G_reshape(test_basic.T_reshape): ...@@ -384,6 +384,14 @@ class G_reshape(test_basic.T_reshape):
assert self.op == GpuReshape assert self.op == GpuReshape
class G_comparison(test_basic.test_comparison):
def setUp(self):
utt.seed_rng()
self.mode = mode_with_gpu
self.shared = gpuarray_shared_constructor
self.dtypes = ['float64', 'float32']
class G_Join_and_Split(test_basic.T_Join_and_Split): class G_Join_and_Split(test_basic.T_Join_and_Split):
def setUp(self): def setUp(self):
super(G_Join_and_Split, self).setUp() super(G_Join_and_Split, self).setUp()
......
...@@ -121,7 +121,20 @@ class GpuArrayType(Type): ...@@ -121,7 +121,20 @@ class GpuArrayType(Type):
return False return False
if a.typecode != b.typecode: if a.typecode != b.typecode:
return False return False
return numpy.asarray(compare(a, '==', b)).all() a_eq_b = numpy.asarray(compare(a, '==', b))
if a_eq_b.all():
return True
# maybe the trouble is that there are NaNs
a = numpy.asarray(a)
b = numpy.asarray(b)
a_missing = numpy.isnan(a)
if a_missing.any():
b_missing = numpy.isnan(b)
return numpy.all(a_eq_b + (a_missing == b_missing))
else:
return False
@staticmethod @staticmethod
def values_eq_approx(a, b, def values_eq_approx(a, b,
...@@ -157,7 +170,15 @@ class GpuArrayType(Type): ...@@ -157,7 +170,15 @@ class GpuArrayType(Type):
op_tmpl="res[i] = (fabs(%%(a)s - %%(b)s) <" op_tmpl="res[i] = (fabs(%%(a)s - %%(b)s) <"
"(%(atol_)s + %(rtol_)s * fabs(%%(b)s)))" % "(%(atol_)s + %(rtol_)s * fabs(%%(b)s)))" %
locals()) locals())
return numpy.asarray(res).all() ret = numpy.asarray(res).all()
if ret:
return True
# maybe the trouble is that there are NaNs
an = numpy.asarray(a)
bn = numpy.asarray(b)
return tensor.TensorType.values_eq_approx(
an, bn, allow_remove_inf=allow_remove_inf,
allow_remove_nan=allow_remove_nan, rtol=rtol, atol=atol)
@staticmethod @staticmethod
def may_share_memory(a, b): def may_share_memory(a, b):
......
...@@ -1096,8 +1096,6 @@ class EQ(LogicalComparison): ...@@ -1096,8 +1096,6 @@ class EQ(LogicalComparison):
def c_code(self, node, name, inputs, outputs, sub): def c_code(self, node, name, inputs, outputs, sub):
(x, y) = inputs (x, y) = inputs
(z,) = outputs (z,) = outputs
if node.inputs[0].type in complex_types:
raise NotImplementedError()
return "%(z)s = (%(x)s == %(y)s);" % locals() return "%(z)s = (%(x)s == %(y)s);" % locals()
eq = EQ() eq = EQ()
......
...@@ -4024,116 +4024,136 @@ class test_comparison(unittest.TestCase): ...@@ -4024,116 +4024,136 @@ class test_comparison(unittest.TestCase):
work(futur behavior) or raise an error(current NumPy release). work(futur behavior) or raise an error(current NumPy release).
""" """
def setUp(self):
utt.seed_rng()
self.mode = None
self.shared = shared
self.dtypes = ['float64', 'float32', 'complex64', 'complex128']
def inplace_func(self, inputs, outputs, check_isfinite=None):
mode = self.mode
if check_isfinite is False:
if mode is None:
mode = get_default_mode()
mode.check_isfinite = False
f = inplace_func(inputs, outputs, mode=mode)
return f
def test_gt(self): def test_gt(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray([0., -1., 1.], dtype=dtype) l = numpy.asarray([0., -1., 1.], dtype=dtype)
r = numpy.asarray([0., 1., -1.], dtype=dtype) r = numpy.asarray([0., 1., -1.], dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(tensor.constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (tensor.constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), tensor.constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), tensor.constant(r), False),
]: ]:
try: try:
fn = inplace_func([], x > y) fn = self.inplace_func([], x > y)
v = fn() v = fn()
self.assertTrue(numpy.all(v == (l > r)), (v, (l > r))) self.assertTrue(numpy.all(v == (l > r)), (v, (l > r)))
except TypeError: except TypeError:
assert err assert err
def test_lt(self): def test_lt(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray([0., -1., 1.], dtype=dtype) l = numpy.asarray([0., -1., 1.], dtype=dtype)
r = numpy.asarray([0., 1., -1.], dtype=dtype) r = numpy.asarray([0., 1., -1.], dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)), self.shared(r.astype(dtype)), False),
(l, shared(r.astype(dtype)), True), (l, self.shared(r.astype(dtype)), True),
(tensor.constant(l), shared(r.astype(dtype)), False), (tensor.constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), r, False), (self.shared(l.astype(dtype)), r, False),
(shared(l.astype(dtype)), tensor.constant(r), False), (self.shared(l.astype(dtype)), tensor.constant(r), False),
]: ]:
try: try:
fn = inplace_func([], x < y) fn = self.inplace_func([], x < y)
v = fn() v = fn()
self.assertTrue(numpy.all(v == (l < r)), (v, (l < r))) self.assertTrue(numpy.all(v == (l < r)), (v, (l < r)))
except TypeError: except TypeError:
assert err assert err
def test_le(self): def test_le(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray([0., -1., 1.], dtype=dtype) l = numpy.asarray([0., -1., 1.], dtype=dtype)
r = numpy.asarray([0., 1., -1.], dtype=dtype) r = numpy.asarray([0., 1., -1.], dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(tensor.constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (tensor.constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), tensor.constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), tensor.constant(r), False),
]: ]:
try: try:
fn = inplace_func([], x <= y) fn = self.inplace_func([], x <= y)
v = fn() v = fn()
self.assertTrue(numpy.all(v == (l <= r)), (v, (l <= r))) self.assertTrue(numpy.all(v == (l <= r)), (v, (l <= r)))
except TypeError: except TypeError:
assert err assert err
def test_ge(self): def test_ge(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray([0., -1., 1.], dtype=dtype) l = numpy.asarray([0., -1., 1.], dtype=dtype)
r = numpy.asarray([0., 1., -1.], dtype=dtype) r = numpy.asarray([0., 1., -1.], dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(tensor.constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (tensor.constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), tensor.constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), tensor.constant(r), False),
]: ]:
try: try:
fn = inplace_func([], x >= y) fn = self.inplace_func([], x >= y)
v = fn() v = fn()
self.assertTrue(numpy.all(v == (l >= r)), (v, (l >= r))) self.assertTrue(numpy.all(v == (l >= r)), (v, (l >= r)))
except TypeError: except TypeError:
assert err assert err
def test_eq(self): def test_eq(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray([0., -1., 1.], dtype=dtype) l = numpy.asarray([0., -1., 1.], dtype=dtype)
r = numpy.asarray([0., 1., -1.], dtype=dtype) r = numpy.asarray([0., 1., -1.], dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(tensor.constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (tensor.constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), tensor.constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), tensor.constant(r), False),
]: ]:
try: try:
fn = inplace_func([], eq(x, y)) fn = self.inplace_func([], eq(x, y))
v = fn() v = fn()
self.assertTrue(numpy.all(v == (l == r)), (v, (l == r))) self.assertTrue(numpy.all(v == (l == r)), (v, (l == r)))
except TypeError: except TypeError:
assert err assert err
def test_neq(self): def test_neq(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray([0., -1., 1.], dtype=dtype) l = numpy.asarray([0., -1., 1.], dtype=dtype)
r = numpy.asarray([0., 1., -1.], dtype=dtype) r = numpy.asarray([0., 1., -1.], dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(tensor.constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (tensor.constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), tensor.constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), tensor.constant(r), False),
]: ]:
try: try:
fn = inplace_func([], neq(x, y)) fn = self.inplace_func([], neq(x, y))
v = fn() v = fn()
self.assertTrue(numpy.all(v == (l != r)), (v, (l != r))) self.assertTrue(numpy.all(v == (l != r)), (v, (l != r)))
except TypeError: except TypeError:
assert err assert err
def test_isclose(self): def test_isclose(self):
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray( l = numpy.asarray(
[0., 1., -1., 0., [0., 1., -1., 0.,
numpy.nan, numpy.inf, -numpy.inf, numpy.inf], numpy.nan, numpy.inf, -numpy.inf, numpy.inf],
...@@ -4143,20 +4163,21 @@ class test_comparison(unittest.TestCase): ...@@ -4143,20 +4163,21 @@ class test_comparison(unittest.TestCase):
numpy.nan, numpy.inf, numpy.inf, 0.], numpy.nan, numpy.inf, numpy.inf, 0.],
dtype=dtype) dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), constant(r), False),
]: ]:
mode = get_default_mode() mode = get_default_mode()
mode.check_isfinite = False mode.check_isfinite = False
try: try:
o1 = isclose(x, y, equal_nan=False) o1 = isclose(x, y, equal_nan=False)
fn1 = inplace_func([], o1, mode=mode) fn1 = self.inplace_func([], o1, check_isfinite=False)
o2 = isclose(x, y, equal_nan=True) o2 = isclose(x, y, equal_nan=True)
fn2 = inplace_func([], o2, mode=mode) fn2 = self.inplace_func([], o2, check_isfinite=False)
v1 = fn1() v1 = fn1()
v2 = fn2() v2 = fn2()
...@@ -4178,12 +4199,13 @@ class test_comparison(unittest.TestCase): ...@@ -4178,12 +4199,13 @@ class test_comparison(unittest.TestCase):
) )
except TypeError: except TypeError:
if not dtype.startswith('complex'): if not dtype.startswith('complex'):
raise
assert err assert err
def test_allclose(self): def test_allclose(self):
# equal_nan argument not in current version of numpy allclose, # equal_nan argument not in current version of numpy allclose,
# force it to False. # force it to False.
for dtype in ['float64', 'float32', 'complex64', 'complex128']: for dtype in self.dtypes:
l = numpy.asarray( l = numpy.asarray(
[0., 1., -1., 0., [0., 1., -1., 0.,
numpy.nan, numpy.inf, -numpy.inf, numpy.inf], numpy.nan, numpy.inf, -numpy.inf, numpy.inf],
...@@ -4193,14 +4215,16 @@ class test_comparison(unittest.TestCase): ...@@ -4193,14 +4215,16 @@ class test_comparison(unittest.TestCase):
numpy.nan, numpy.inf, numpy.inf, 0.], numpy.nan, numpy.inf, numpy.inf, 0.],
dtype=dtype) dtype=dtype)
for x, y, err in [ for x, y, err in [
(shared(l.astype(dtype)), shared(r.astype(dtype)), False), (self.shared(l.astype(dtype)),
(l, shared(r.astype(dtype)), True), self.shared(r.astype(dtype)), False),
(constant(l), shared(r.astype(dtype)), False), (l, self.shared(r.astype(dtype)), True),
(shared(l.astype(dtype)), r, False), (constant(l), self.shared(r.astype(dtype)), False),
(shared(l.astype(dtype)), constant(r), False), (self.shared(l.astype(dtype)), r, False),
(self.shared(l.astype(dtype)), constant(r), False),
]: ]:
try: try:
fn = inplace_func([], allclose(x, y, equal_nan=False)) fn = self.inplace_func([], allclose(x, y, equal_nan=False),
check_isfinite=False)
v = fn() v = fn()
self.assertTrue(numpy.all(v == numpy.allclose(l, r))) self.assertTrue(numpy.all(v == numpy.allclose(l, r)))
except TypeError: except TypeError:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论