def testSameDim(self): A = CuMatrix() B = CuMatrix() self.assertTrue(same_dim_cu_matrix(A, B)) A = CuMatrix.new_from_size(10, 10) B = CuMatrix.new_from_size(10, 9) self.assertFalse(same_dim_cu_matrix(A, B))
def testApproxEqual(self): A = CuMatrix() B = CuMatrix() self.assertTrue(approx_equal_cu_matrix(A, B)) A.SetZero() B.SetZero() self.assertTrue(approx_equal_cu_matrix(A, B)) B.set_randn() B.Scale(10.0) self.assertFalse(approx_equal_cu_matrix(A, B))
def testNew(self): A = CuMatrix() self.assertIsNotNone(A) self.assertEqual(0, A.num_rows()) self.assertEqual(0, A.num_cols()) dim = A.dim() self.assertEqual(0, dim.rows) self.assertEqual(0, dim.cols) A = CuMatrix.new_from_size(10, 10) self.assertIsNotNone(A) self.assertEqual(10, A.num_rows()) self.assertEqual(10, A.num_cols()) dim = A.dim() self.assertEqual(10, dim.rows) self.assertEqual(10, dim.cols) A = CuMatrix.new_from_matrix(Matrix([[2, 3], [5, 7]])) self.assertIsNotNone(A) self.assertEqual(2, A.num_rows()) self.assertEqual(2, A.num_cols()) B = CuMatrix.new_from_other(A) self.assertIsNotNone(B) self.assertEqual(2, B.num_rows()) self.assertEqual(2, B.num_cols())
def testResize(self): A = CuMatrix() A.resize(10, 10) self.assertEqual(10, A.num_rows()) self.assertEqual(10, A.num_cols()) # A.resize(-1, -1) #This hard-crashes A.resize(0, 0)
def aux(size_multiple): num_matrices = 256 time_in_secs = 0.2 sizes = [] for i in range(num_matrices): num_rows = kaldi_math.rand_int(1, 10) num_rows *= num_rows num_rows *= size_multiple num_cols = kaldi_math.rand_int(1, 10) num_cols *= num_cols num_cols *= size_multiple # There is a typo in kaldi sizes.append((num_rows, num_rows)) matrices = [CuMatrix() for _ in range(num_matrices)] tim = Timer() num_floats_processed = 0 while tim.elapsed() < time_in_secs: matrix = kaldi_math.rand_int(0, num_matrices - 1) if matrices[matrix].num_rows() == 0: num_rows, num_cols = sizes[matrix] matrices[matrix].resize(num_rows, num_cols, MatrixResizeType.UNDEFINED) num_floats_processed += num_rows * num_cols else: matrices[matrix].resize(0, 0) gflops = num_floats_processed / (tim.elapsed() * 1.0e9) print( "For CuMatrix::Reize float, for size multiple {}, speed was {} gigaflops" .format(size_multiple, gflops))