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