Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 381415d8 authored by hhakim's avatar hhakim
Browse files

Make tests compatible with float32 dtype.

parent 7b1e8d81
No related branches found
No related tags found
No related merge requests found
......@@ -601,6 +601,7 @@ test_macos_pkg_release:
- python$MACOS_PY_VER -c "import pyfaust; print(pyfaust.version()); print(pyfaust.rand(5,5).toarray());"
- python$MACOS_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'real')"
- python$MACOS_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'complex')"
- python$MACOS_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'float32')"
only:
- tags
tags:
......@@ -620,12 +621,14 @@ test_linux_pkg_release:
- python$NUX_PY_VER -c "import pyfaust; print(pyfaust.version()); print(pyfaust.rand(5,5).toarray());"
- python$NUX_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'real')"
- python$NUX_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'complex')"
- python$NUX_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'float32')"
- sudo rpm -e faust
- sudo rpm -i --nodeps build/faust-$CI_COMMIT_TAG-static-x86_64.rpm
- matlab -nojvm -nodisplay -r "disp(matfaust.version());disp(full(matfaust.rand(5,5))); exit;" | tee /tmp/faust && grep $CI_COMMIT_TAG /tmp/faust
- python$NUX_PY_VER -c "import pyfaust; print(pyfaust.version()); print(pyfaust.rand(5,5).toarray());"
- python$NUX_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'real')"
- python$NUX_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'complex')"
- python$NUX_PY_VER -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'float32')"
- sudo rpm -e faust
only:
- tags
......@@ -652,6 +655,7 @@ test_linux_pkg_release:
- python -c "import pyfaust; print(pyfaust.version()); print(pyfaust.rand(5,5).toarray());"
- python -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'real')"
- python -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'complex')"
- python -c "import pyfaust.tests; pyfaust.tests.run_tests('cpu', 'float32')"
- cd "C:\Program Files\Faust"
#- call 'uninstall.exe' /S
- python -c "import os; os.system('uninstall.exe /S')"
......
......@@ -17,32 +17,24 @@ class TestFaust(unittest.TestCase):
MAX_DIM_SIZE = 256
MIN_DIM_SIZE = 3
def __init__(self, methodName='runTest', dev='cpu', field='real'):
def __init__(self, methodName='runTest', dev='cpu', dtype='double'):
super(TestFaust, self).__init__(methodName)
if dtype == 'real': # backward compat
dtype = 'double'
self.dtype = dtype
self.dev = dev
self.field = field
def setUp(self):
"""
"""
# self.dev = 'cpu'
# self.field = 'real'
# if 'dev' in os.environ:
# self.dev = os.environ['dev']
# if 'field' in os.environ:
# self.field = os.environ['field']
nrows = randint(TestFaust.MIN_DIM_SIZE,
TestFaust.MAX_DIM_SIZE+1)
ncols = randint(TestFaust.MIN_DIM_SIZE,
TestFaust.MAX_DIM_SIZE+1)
nfacts = randint(TestFaust.MIN_NUM_FACTORS,
TestFaust.MAX_NUM_FACTORS+1)
dtype = 'double'
if self.field not in ['real', 'complex']:
dtype = self.field
self.field = 'real'
self.F = frand(nrows, ncols, num_factors=nfacts, dev=self.dev,
field=self.field, dtype=dtype)
dtype=self.dtype)
self.nrows = nrows
self.ncols = ncols
self.nfacts = nfacts
......@@ -64,15 +56,11 @@ class TestFaust(unittest.TestCase):
def sparse_fac_size(sfac):
int_size = 4
double_size = 8
cplx_size = 16
sfac.dtype.itemsize
nnz = sfac.nnz
nrows = sfac.shape[0]
size = nnz*int_size+(nrows+1)*int_size
if sfac.dtype == np.complex128:
size += cplx_size*nnz
elif sfac.dtype == np.float64:
size += double_size*nnz
size += sfac.dtype.itemsize * nnz
return size
Fsize = 0
for i in range(0, self.F.numfactors()):
......@@ -128,7 +116,7 @@ class TestFaust(unittest.TestCase):
def test_add(self):
print("Faust.__add__, __radd__")
G = frand(self.nrows, self.ncols, dev=self.dev)
G = frand(self.nrows, self.ncols, dev=self.dev, dtype=self.dtype)
self.assertTrue(np.allclose((self.F+G).toarray(),
self.F.toarray()+G.toarray()))
self.assertTrue(np.allclose((self.F+G.toarray()).toarray(),
......@@ -136,7 +124,7 @@ class TestFaust(unittest.TestCase):
def test_sub(self):
print("Faust.__sub__, __rsub__")
G = frand(self.nrows, self.ncols, dev=self.dev)
G = frand(self.nrows, self.ncols, dev=self.dev, dtype=self.dtype)
self.assertTrue(np.allclose((self.F-G).toarray(),
self.F.toarray()-G.toarray()))
self.assertTrue(np.allclose((self.F-G.toarray()).toarray(),
......@@ -148,7 +136,7 @@ class TestFaust(unittest.TestCase):
def test_matmul(self):
print("Faust.__matmul__, dot, __rmatmul__")
G = frand(self.ncols, self.nrows, dev=self.dev)
G = frand(self.ncols, self.nrows, dev=self.dev, dtype=self.dtype)
self.assertTrue(np.allclose((self.F@G).toarray(),
self.F.toarray()@G.toarray()))
self.assertTrue(np.allclose((self.F@G.toarray()),
......@@ -163,7 +151,7 @@ class TestFaust(unittest.TestCase):
def test_concatenate(self):
print("Faust.concatenate, pyfaust.vstack, pyfaust.hstack")
G = frand(self.nrows, self.ncols, dev=self.dev)
G = frand(self.nrows, self.ncols, dev=self.dev, dtype=self.dtype)
self.assertTrue(np.allclose((self.F.concatenate(G)).toarray(),
np.concatenate((self.F.toarray(),
G.toarray()))))
......@@ -318,7 +306,7 @@ class TestFaust(unittest.TestCase):
Fa = self.F.toarray()
sFa = sF.toarray()
self._assertAlmostEqual(sFa[:, j1], Fa[:, j2])
self.assertAlmostEqual(sF.norm(), self.F.norm())
self.assertAlmostEqual(sF.norm(), self.F.norm(), places=3)
def test_swap_rows(self):
print("Faust.swap_rows")
......@@ -328,7 +316,7 @@ class TestFaust(unittest.TestCase):
Fa = self.F.toarray()
sFa = sF.toarray()
self._assertAlmostEqual(sFa[i1, :], Fa[i2, :])
self.assertAlmostEqual(sF.norm(), self.F.norm())
self.assertAlmostEqual(sF.norm(), self.F.norm(), places=3)
def test_optimize_memory(self):
print("Faust.optimize_memory")
......@@ -362,7 +350,8 @@ class TestFaust(unittest.TestCase):
def test_average(self):
print("Faust.average")
weights = [ np.random.rand(self.F.shape[0]), np.random.rand(self.F.shape[1])]
weights = [ np.random.rand(self.F.shape[0]).astype(self.dtype),
np.random.rand(self.F.shape[1]).astype(self.dtype)]
for i in [0, 1]:
self._assertAlmostEqual(self.F.average(axis=i).toarray().reshape(1, self.F.shape[(i+1)%2]),
np.average(self.F.toarray(), axis=i))
......
......@@ -8,24 +8,18 @@ from scipy.sparse.linalg import expm_multiply as scipy_expm_multiply
import tempfile
import os
dev = 'cpu'
field = 'real'
class TestPoly(unittest.TestCase):
def __init__(self, methodName='runTest', dev='cpu', field='real'):
def __init__(self, methodName='runTest', dev='cpu', dtype='double'):
super(TestPoly, self).__init__(methodName)
self.dev = dev
self.field = field
if dtype == 'real': # backward compat
dtype = 'double'
self.dtype = dtype
def setUp(self):
self.d = 50
self.density = 0.02
if self.field == 'complex':
self.dtype = 'complex'
else:
self.dtype = 'double'
self.L = random(self.d, self.d, .02, format='csr', dtype=self.dtype)
self.L @= self.L.H
self.K = 5
......@@ -92,8 +86,8 @@ class TestPoly(unittest.TestCase):
L = self.L
K = self.K
density = self.density
F = basis(L, K, 'chebyshev', dev=self.dev)
coeffs = np.random.rand(K+1).astype(L.dtype)
F = basis(L, K, 'chebyshev', dev=self.dev).astype(self.dtype)
coeffs = np.random.rand(K+1).astype(self.dtype)
G = poly(coeffs, F)
# Test polynomial as Faust
poly_ref = np.zeros((d,d))
......@@ -102,28 +96,32 @@ class TestPoly(unittest.TestCase):
self.assertAlmostEqual((G-poly_ref).norm(), 0)
# Test polynomial as array
GM = poly(coeffs, F.toarray())
self.assertTrue(isinstance(GM, np.ndarray) and np.allclose(GM,
poly_ref.toarray()))
self.assertTrue(isinstance(GM, np.ndarray))
err = norm(GM - poly_ref.toarray())/norm(poly_ref.toarray())
self.assertLessEqual(err, 1e-6)
# Test polynomial-vector product
x = np.random.rand(F.shape[1], 1).astype(L.dtype)
# Three ways to do (not all as efficient as each other)
Fx1 = poly(coeffs, F, dev=self.dev)@x
Fx2 = poly(coeffs, F@x, dev=self.dev)
Fx3 = poly(coeffs, F, X=x, dev=self.dev)
self.assertTrue(np.allclose(Fx1, Fx2))
err = norm(Fx1-Fx2)/norm(Fx1)
self.assertLessEqual(err, 1e-6)
self.assertTrue(np.allclose(Fx1, Fx3))
# Test polynomial-matrix product
X = np.random.rand(F.shape[1], 18).astype(L.dtype)
FX1 = poly(coeffs, F, dev=self.dev)@X
FX2 = poly(coeffs, F@X, dev=self.dev)
FX3 = poly(coeffs, F, X=X, dev=self.dev)
self.assertTrue(np.allclose(FX1, FX2))
err = norm(FX1-FX2)/norm(FX1)
self.assertLessEqual(err, 1e-6)
self.assertTrue(np.allclose(FX2, FX3))
# Test creating the polynomial basis on the fly
G2 = poly(coeffs, 'chebyshev', L)
self.assertAlmostEqual((G-G2).norm(), 0)
GX = poly(coeffs, 'chebyshev', L, X=X, dev=self.dev)
self.assertTrue(np.allclose(GX, FX1))
err = norm(FX1-GX)/norm(FX1)
self.assertLessEqual(err, 1e-6)
# Test polynomial-matrix product with arbitrary T0
F_ = basis(L, K, 'chebyshev', dev=self.dev, T0=csr_matrix(X))
GT0eqX = poly(coeffs, F_, dev=self.dev).toarray()
......
......@@ -3,10 +3,10 @@ from pyfaust.tests.TestFaust import TestFaust
from pyfaust.tests.TestPoly import TestPoly
def run_tests(dev, field):
def run_tests(dev, dtype):
"""
Runs all available tests using device dev ('cpu' or 'gpu') and scalar type
field ('real' or 'complex') when it applies.
dtype ('real' or 'complex') when it applies.
"""
runner = unittest.TextTestRunner()
suite = unittest.TestSuite()
......@@ -14,6 +14,6 @@ def run_tests(dev, field):
testloader = unittest.TestLoader()
test_names = eval("testloader.getTestCaseNames("+class_name+")")
for meth_name in test_names:
test = eval(""+class_name+"('"+meth_name+"', dev=dev, field=field)")
test = eval(""+class_name+"('"+meth_name+"', dev=dev, dtype=dtype)")
suite.addTest(test)
runner.run(suite)
......@@ -14,8 +14,8 @@ if __name__ == "__main__":
raise ValueError("dev argument must be cpu or gpu.")
if(nargs > 2):
field = sys.argv[2]
if field not in ['complex', 'real']:
raise ValueError("field must be complex or float")
if field not in ['complex', 'real', 'float32', 'float64', 'double']:
raise ValueError("field must be 'complex', 'real', 'float32', 'float64', 'double'")
del sys.argv[2] # deleted to avoid interfering with unittest
del sys.argv[1]
if(nargs > 1):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment