Mentions légales du service

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

Add unit tests for lazy slicing/indexing with pyfaust and fix three bugs it...

Add unit tests for lazy slicing/indexing with pyfaust and fix three bugs it helped to find in TransformHelperGen::copy_fancy_idx_state/init_fancy_idx_transform/init_sliced_transform.
parent 2a60bfed
Branches
Tags
No related merge requests found
...@@ -317,6 +317,8 @@ class TestFaustPy(unittest.TestCase): ...@@ -317,6 +317,8 @@ class TestFaustPy(unittest.TestCase):
if(n2 == 0): # avoid nan error if(n2 == 0): # avoid nan error
self.assertLessEqual(n1,0.0005) self.assertLessEqual(n1,0.0005)
else: else:
print("slices:", rand_i, rand_ii, rand_j, rand_jj)
self.F.save('test_sF.mat')
self.assertLessEqual(n1/n2,0.005) self.assertLessEqual(n1/n2,0.005)
# test a second slice on sF only if the shape allows it # min shape (1,1) # test a second slice on sF only if the shape allows it # min shape (1,1)
if(sF.shape[0] >= 2 and sF.shape[1] >= 2): if(sF.shape[0] >= 2 and sF.shape[1] >= 2):
...@@ -462,9 +464,40 @@ class TestFaustPy(unittest.TestCase): ...@@ -462,9 +464,40 @@ class TestFaustPy(unittest.TestCase):
else: else:
self.assertLessEqual(n1/n2, 0.005) self.assertLessEqual(n1/n2, 0.005)
def testLazySlicingIndexing(self):
print("testLazySlicingIndexing()")
from pyfaust import rand as frand
for F in [frand(512, 512, fac_type='dense'),
frand(512, 512, fac_type='sparse')] + \
[frand(512, 512, fac_type='dense', num_factors=1),
frand(512, 512, fac_type='sparse', num_factors=1)]:
# test that two slicings in a row make a consistent Faust
self.assertTrue(np.allclose(F[:15, :42][:12, :13].toarray(),
F.toarray()[:15, :42][:12, :13]))
# the same but with a transpose between
self.assertTrue(np.allclose(F[:15, :42].T[:12, :13].toarray(),
F.toarray()[:15, :42].T[:12, :13]))
# test that two indexings in a row make a consistent Faust
I = list(range(0, 15, 3))
J = list(range(0, 42, 5))
I2 = list(range(0, len(I), 2))
J2 = list(range(0, len(J), 2))
self.assertTrue(np.allclose(F[I][:,J][I2][:,J2].toarray(),
F.toarray()[I][:,J][I2][:,J2]))
# the same with a transpose between
self.assertTrue(np.allclose(F[I][:,J].T[J2][:,I2].toarray(),
F.toarray()[I][:,J].T[J2][:,I2]))
# test that a slicing followed by an indexing works
self.assertTrue(np.allclose(F[:15, :42][I][:,J].toarray(),
F.toarray()[:15, :42][I][:,J]))
# the same with a transpose between
self.assertTrue(np.allclose(F[:15, :42].T[J][:,I].toarray(),
F.toarray()[:15, :42].T[J][:,I]))
# now do the same but the indexing first
self.assertTrue(np.allclose(F[I][:,J][:len(I)//2, :len(J)//2].toarray(),
F.toarray()[I][:,J][:len(I)//2, :len(J)//2]))
self.assertTrue(np.allclose(F[I][:,J].T[:len(I)//2, :len(J)//2].toarray(),
F.toarray()[I][:,J].T[:len(I)//2, :len(J)//2]))
def testToDense(self): def testToDense(self):
print("testToDense()") print("testToDense()")
...@@ -540,6 +573,7 @@ class TestFaustPy(unittest.TestCase): ...@@ -540,6 +573,7 @@ class TestFaustPy(unittest.TestCase):
from pyfaust import FaustMulMode from pyfaust import FaustMulMode
print("test GREEDY and DYNPROG prod opt methods and optimize_time.") print("test GREEDY and DYNPROG prod opt methods and optimize_time.")
GREEDY = 4 GREEDY = 4
self.F.save('/tmp/F.mat')
H = self.F.clone() H = self.F.clone()
H.m_faust.set_FM_mul_mode(GREEDY) # FaustMulMode.GREEDY replaced by GREEDY local variable because GREEDY is not a visible opt. method anymore H.m_faust.set_FM_mul_mode(GREEDY) # FaustMulMode.GREEDY replaced by GREEDY local variable because GREEDY is not a visible opt. method anymore
G = self.F.clone() G = self.F.clone()
......
...@@ -57,7 +57,7 @@ namespace Faust ...@@ -57,7 +57,7 @@ namespace Faust
this->fancy_indices[0][i] = th->fancy_indices[0][id]; this->fancy_indices[0][i] = th->fancy_indices[0][id];
// else error delayed to the sanity check below // else error delayed to the sanity check below
} }
for(int j=0;j < num_rows; j++) for(int j=0;j < num_cols; j++)
{ {
auto id = this->fancy_indices[1][j]; auto id = this->fancy_indices[1][j];
if(id < th->fancy_num_cols) if(id < th->fancy_num_cols)
...@@ -89,7 +89,7 @@ namespace Faust ...@@ -89,7 +89,7 @@ namespace Faust
void TransformHelperGen<FPP,DEV>::init_sliced_transform(TransformHelper<FPP,DEV>* th, Slice s[2]) void TransformHelperGen<FPP,DEV>::init_sliced_transform(TransformHelper<FPP,DEV>* th, Slice s[2])
{ {
eval_fancy_idx_Transform(); th->eval_fancy_idx_Transform();
this->transform = th->transform; //do not remove this line, necessary for eval_sliced_Transform() this->transform = th->transform; //do not remove this line, necessary for eval_sliced_Transform()
this->copy_transconj_state(*th); this->copy_transconj_state(*th);
if(! (s[0].belong_to(0, th->getNbRow()) || s[1].belong_to(0, th->getNbCol()))) if(! (s[0].belong_to(0, th->getNbRow()) || s[1].belong_to(0, th->getNbCol())))
...@@ -346,6 +346,8 @@ namespace Faust ...@@ -346,6 +346,8 @@ namespace Faust
fancy_num_cols = th.fancy_num_cols; fancy_num_cols = th.fancy_num_cols;
id0 = 0, id1 = 1; id0 = 0, id1 = 1;
} }
fancy_indices[0] = new faust_unsigned_int[fancy_num_rows];
fancy_indices[1] = new faust_unsigned_int[fancy_num_cols];
memcpy(fancy_indices[0], th.fancy_indices[id0], fancy_num_rows*sizeof(faust_unsigned_int)); memcpy(fancy_indices[0], th.fancy_indices[id0], fancy_num_rows*sizeof(faust_unsigned_int));
memcpy(fancy_indices[1], th.fancy_indices[id1], fancy_num_cols*sizeof(faust_unsigned_int)); memcpy(fancy_indices[1], th.fancy_indices[id1], fancy_num_cols*sizeof(faust_unsigned_int));
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment