提交 7a515454 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Add tests for strides with abs value > 1

上级 a42afaf6
...@@ -195,29 +195,31 @@ def test_dot22_strides(): ...@@ -195,29 +195,31 @@ def test_dot22_strides():
mode=mode_with_gpu) mode=mode_with_gpu)
# Try with all stride patterns, and all transposed pattern # Try with all stride patterns, and all transposed pattern
for steps in itertools.product((-1, 1), repeat=4): for step_signs in itertools.product((-1, 1), repeat=4):
b_step1, b_step2, c_step1, c_step2 = steps for step in (1, 2):
b_step1, b_step2, c_step1, c_step2 = (s * step
b.set_value(b_gpu.copy()[::b_step1, ::b_step2], borrow=True) for s in step_signs)
c.set_value(c_gpu.copy()[::c_step1, ::c_step2], borrow=True)
b_t.set_value(bt_gpu.copy()[::b_step2, ::b_step1], borrow=True) b.set_value(b_gpu.copy()[::b_step1, ::b_step2], borrow=True)
c_t.set_value(ct_gpu.copy()[::c_step2, ::c_step1], borrow=True) c.set_value(c_gpu.copy()[::c_step1, ::c_step2], borrow=True)
b_t.set_value(bt_gpu.copy()[::b_step2, ::b_step1], borrow=True)
# Numpy result c_t.set_value(ct_gpu.copy()[::c_step2, ::c_step1], borrow=True)
a_n = numpy.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2]) # Numpy result
a_n = numpy.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2])
f_nn() f_nn()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
f_nt() f_nt()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
f_tn() f_tn()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
f_tt() f_tt()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
cmp((3, 4), (4, 5)) cmp((3, 4), (4, 5))
cmp((1, 4), (4, 5)) cmp((1, 4), (4, 5))
...@@ -263,29 +265,31 @@ def test_dot22scalar_strides(): ...@@ -263,29 +265,31 @@ def test_dot22scalar_strides():
mode=mode_with_gpu) mode=mode_with_gpu)
# Try with all stride patterns, and all transposed pattern # Try with all stride patterns, and all transposed pattern
for steps in itertools.product((-1, 1), repeat=4): for step_signs in itertools.product((-1, 1), repeat=4):
b_step1, b_step2, c_step1, c_step2 = steps for step in (1, 2):
b_step1, b_step2, c_step1, c_step2 = (s * step
for s in step_signs)
b.set_value(b_gpu.copy()[::b_step1, ::b_step2], borrow=True) b.set_value(b_gpu.copy()[::b_step1, ::b_step2], borrow=True)
c.set_value(c_gpu.copy()[::c_step1, ::c_step2], borrow=True) c.set_value(c_gpu.copy()[::c_step1, ::c_step2], borrow=True)
b_t.set_value(bt_gpu.copy()[::b_step2, ::b_step1], borrow=True) b_t.set_value(bt_gpu.copy()[::b_step2, ::b_step1], borrow=True)
c_t.set_value(ct_gpu.copy()[::c_step2, ::c_step1], borrow=True) c_t.set_value(ct_gpu.copy()[::c_step2, ::c_step1], borrow=True)
# Numpy result # Numpy result
a_n = l * numpy.dot(bv[::b_step1, ::b_step2], a_n = l * numpy.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2]) cv[::c_step1, ::c_step2])
f_nn() f_nn()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
f_nt() f_nt()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
f_tn() f_tn()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
f_tt() f_tt()
assert numpy.allclose(a.get_value(), a_n) assert numpy.allclose(a.get_value(), a_n)
cmp((3, 4), (4, 5)) cmp((3, 4), (4, 5))
cmp((1, 4), (4, 5)) cmp((1, 4), (4, 5))
...@@ -341,58 +345,60 @@ def test_gemm_strides(): ...@@ -341,58 +345,60 @@ def test_gemm_strides():
mode=mode_with_gpu) mode=mode_with_gpu)
# Try with all stride patterns, and all transposed pattern # Try with all stride patterns, and all transposed pattern
for steps in itertools.product((-1, 1), repeat=6): for step_signs in itertools.product((-1, 1), repeat=6):
a_step1, a_step2, b_step1, b_step2, c_step1, c_step2 = steps for step in (1, 2):
a_step1, a_step2, b_step1, b_step2, c_step1, c_step2 = \
b.set_value(b_gpu.copy()[::b_step1, ::b_step2], borrow=True) (s * step for s in step_signs)
c.set_value(c_gpu.copy()[::c_step1, ::c_step2], borrow=True)
b_t.set_value(bt_gpu.copy()[::b_step2, ::b_step1], borrow=True) b.set_value(b_gpu.copy()[::b_step1, ::b_step2], borrow=True)
c_t.set_value(ct_gpu.copy()[::c_step2, ::c_step1], borrow=True) c.set_value(c_gpu.copy()[::c_step1, ::c_step2], borrow=True)
b_t.set_value(bt_gpu.copy()[::b_step2, ::b_step1], borrow=True)
# Numpy results c_t.set_value(ct_gpu.copy()[::c_step2, ::c_step1], borrow=True)
a_n = (l * av[::a_step1, ::a_step2]
+ numpy.dot(bv[::b_step1, ::b_step2], # Numpy results
cv[::c_step1, ::c_step2])) a_n = (l * av[::a_step1, ::a_step2]
at_n = (l * av[::a_step1, ::a_step2].T + numpy.dot(bv[::b_step1, ::b_step2],
+ numpy.dot(bv[::b_step1, ::b_step2], cv[::c_step1, ::c_step2]))
cv[::c_step1, ::c_step2]).T) at_n = (l * av[::a_step1, ::a_step2].T
+ numpy.dot(bv[::b_step1, ::b_step2],
# a's value is updated, so we need to reinitialize it each time cv[::c_step1, ::c_step2]).T)
a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True)
f_nnn() # a's value is updated, so we need to reinitialize it each time
assert numpy.allclose(a.get_value(), a_n) a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True)
f_nnn()
a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True) assert numpy.allclose(a.get_value(), a_n)
f_nnt()
assert numpy.allclose(a.get_value(), a_n) a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True)
f_nnt()
a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True) assert numpy.allclose(a.get_value(), a_n)
f_ntn()
assert numpy.allclose(a.get_value(), a_n) a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True)
f_ntn()
a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True) assert numpy.allclose(a.get_value(), a_n)
f_ntt()
assert numpy.allclose(a.get_value(), a_n) a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True)
f_ntt()
a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1], assert numpy.allclose(a.get_value(), a_n)
borrow=True)
f_tnn() a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1],
assert numpy.allclose(a_t.get_value(), at_n) borrow=True)
f_tnn()
a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1], assert numpy.allclose(a_t.get_value(), at_n)
borrow=True)
f_tnt() a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1],
assert numpy.allclose(a_t.get_value(), at_n) borrow=True)
f_tnt()
a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1], assert numpy.allclose(a_t.get_value(), at_n)
borrow=True)
f_ttn() a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1],
assert numpy.allclose(a_t.get_value(), at_n) borrow=True)
f_ttn()
a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1], assert numpy.allclose(a_t.get_value(), at_n)
borrow=True)
f_ttt() a_t.set_value(transpose(a_gpu.copy())[::a_step2, ::a_step1],
assert numpy.allclose(a_t.get_value(), at_n) borrow=True)
f_ttt()
assert numpy.allclose(a_t.get_value(), at_n)
cmp((3, 5), (3, 4), (4, 5)) cmp((3, 5), (3, 4), (4, 5))
...@@ -433,24 +439,26 @@ def test_gemv_strides(): ...@@ -433,24 +439,26 @@ def test_gemv_strides():
mode=mode_with_gpu) mode=mode_with_gpu)
# Try with all stride patterns, and all transposed pattern # Try with all stride patterns, and all transposed pattern
for steps in itertools.product((1, -1), repeat=4): for step_signs in itertools.product((1, -1), repeat=4):
a_step, b_step1, b_step2, c_step = steps for step in (1, 2):
a_step, b_step1, b_step2, c_step = (s * step
a.set_value(a_gpu.copy()[::a_step], borrow=True) for s in step_signs)
b.set_value(b_gpu.copy()[::b_step1, ::b_step2],
borrow=True) a.set_value(a_gpu.copy()[::a_step], borrow=True)
b_t.set_value(transpose(b_gpu.copy())[::b_step2, ::b_step1], b.set_value(b_gpu.copy()[::b_step1, ::b_step2],
borrow=True) borrow=True)
c.set_value(c_gpu.copy()[::c_step], borrow=True) b_t.set_value(transpose(b_gpu.copy())[::b_step2, ::b_step1],
borrow=True)
a_n = (av[::a_step] c.set_value(c_gpu.copy()[::c_step], borrow=True)
+ l * numpy.dot(bv[::b_step1, ::b_step2], cv[::c_step]))
f_n() a_n = (av[::a_step]
assert numpy.allclose(a.get_value(), a_n), (a.get_value(), a_n) + l * numpy.dot(bv[::b_step1, ::b_step2], cv[::c_step]))
f_n()
a.set_value(a_gpu.copy()[::a_step], borrow=True) assert numpy.allclose(a.get_value(), a_n), (a.get_value(), a_n)
f_t()
assert numpy.allclose(a.get_value(), a_n), (a.get_value(), a_n) a.set_value(a_gpu.copy()[::a_step], borrow=True)
f_t()
assert numpy.allclose(a.get_value(), a_n), (a.get_value(), a_n)
cmp(3, (3, 5), 5) cmp(3, (3, 5), 5)
cmp(1, (1, 5), 5) cmp(1, (1, 5), 5)
...@@ -485,23 +493,27 @@ def test_ger_strides(): ...@@ -485,23 +493,27 @@ def test_ger_strides():
mode=mode_with_gpu) mode=mode_with_gpu)
# Try with all stride patterns, and all transposed patterns # Try with all stride patterns, and all transposed patterns
for steps in itertools.product((1, -1), repeat=4): for step_signs in itertools.product((1, -1), repeat=4):
a_step1, a_step2, b_step, c_step = steps for step in (1, 2):
a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True) a_step1, a_step2, b_step, c_step = (s * step
a_t.set_value(transpose(a_gpu.copy())[::a_step1, ::a_step2], for s in step_signs)
borrow=True)
b.set_value(b_gpu.copy()[::b_step], borrow=True) a.set_value(a_gpu.copy()[::a_step1, ::a_step2], borrow=True)
c.set_value(c_gpu.copy()[::c_step], borrow=True) a_t.set_value(transpose(a_gpu.copy())[::a_step1, ::a_step2],
borrow=True)
f_n() b.set_value(b_gpu.copy()[::b_step], borrow=True)
n_n = (av[::a_step1, ::a_step2] c.set_value(c_gpu.copy()[::c_step], borrow=True)
+ l * numpy.outer(bv[::b_step], cv[::c_step]))
assert numpy.allclose(a.get_value(), n_n), (a.get_value(), n_n) f_n()
n_n = (av[::a_step1, ::a_step2]
f_t() + l * numpy.outer(bv[::b_step], cv[::c_step]))
n_t = (av.T[::a_step1, ::a_step2] assert numpy.allclose(a.get_value(), n_n), (a.get_value(), n_n)
+ l * numpy.outer(bv[::b_step], cv[::c_step]).T)
assert numpy.allclose(a_t.get_value(), n_t), (a_t.get_value(), n_t) f_t()
n_t = (av.T[::a_step1, ::a_step2]
+ l * numpy.outer(bv[::b_step], cv[::c_step]).T)
assert numpy.allclose(a_t.get_value(), n_t),\
(a_t.get_value(), n_t)
cmp((3, 5), 3, 5) cmp((3, 5), 3, 5)
cmp((1, 5), 1, 5) cmp((1, 5), 1, 5)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论