// cudamatrix/cu-vector-test.cc // Copyright 2013 Lucas Ondel // 2013 Johns Hopkins University (author: Daniel Povey) // 2017 Hossein Hadian, Daniel Galvez // See ../../COPYING for clarification regarding multiple authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #include #include #include #include #include "base/kaldi-common.h" #include "util/common-utils.h" #include "cudamatrix/cu-matrix.h" #include "cudamatrix/cu-vector.h" #include "cudamatrix/cu-tp-matrix.h" #include "cudamatrix/cu-sp-matrix.h" #include "cudamatrix/cu-math.h" namespace kaldi { /* * INITIALIZERS */ /* * Unit tests */ template static void UnitTestCuVectorIO() { for (int32 i = 0; i < 10; i++) { int32 dimM = Rand() % 255; if (i % 5 == 0) { dimM = 0; } CuVector vec(dimM); vec.SetRandn(); std::ostringstream os; bool binary = (i % 4 < 2); vec.Write(os, binary); CuVector vec2; std::istringstream is(os.str()); vec2.Read(is, binary); AssertEqual(vec, vec2); } } template static void UnitTestCuVectorCopyFromVec() { for (int32 i = 1; i < 10; i++) { MatrixIndexT dim = 10 * i; Vector A(dim); A.SetRandn(); CuVector B(A); Vector C(B); CuVector D(dim); D.CopyFromVec(C); Vector E(dim); E.CopyFromVec(D); CuVector F(E); CuVector A2(A); AssertEqual(F, A2); } } template static void UnitTestCuSubVector() { for (int32 iter = 0 ; iter < 10; iter++) { int32 M1 = 1 + rand () % 10, M2 = 1 + Rand() % 1, M3 = 1 + Rand() % 10, M = M1 + M2 + M3, m = Rand() % M2; CuVector vec(M); vec.SetRandn(); CuSubVector subvec1(vec, M1, M2), subvec2 = vec.Range(M1, M2); Real f1 = vec(M1 + m), f2 = subvec1(m), f3 = subvec2(m); KALDI_ASSERT(f1 == f2); KALDI_ASSERT(f2 == f3); } } template static void UnitTestCuVectorMulTp() { for (int32 i = 1; i < 10; i++) { MatrixIndexT dim = 10 * i; Vector A(dim); A.SetRandn(); TpMatrix B(dim); B.SetRandn(); CuVector C(A); CuTpMatrix D(B); A.MulTp(B, kNoTrans); C.MulTp(D, kNoTrans); CuVector E(A); AssertEqual(C, E); } } template static void UnitTestCuVectorAddTp() { for (int32 i = 1; i < 10; i++) { MatrixIndexT dim = 10 * i; Vector A(dim); A.SetRandn(); TpMatrix B(dim); B.SetRandn(); Vector C(dim); C.SetRandn(); CuVector D(A); CuTpMatrix E(B); CuVector F(C); A.AddTpVec(1.0, B, kNoTrans, C, 1.0); D.AddTpVec(1.0, E, kNoTrans, F, 1.0); CuVector G(A); AssertEqual(D, G); } } template void CuVectorUnitTestVecVec() { int32 M = 10 + Rand() % 100; CuVector vec1(M), vec2(M); vec1.SetRandn(); vec2.SetRandn(); Real prod = 0.0; for (int32 i = 0; i < M; i++) prod += vec1(i) * vec2(i); AssertEqual(prod, VecVec(vec1, vec2)); } template void CuVectorUnitTestAddVec() { int32 M = 10 + Rand() % 100; CuVector vec1(M); CuVector vec2(M); vec1.SetRandn(); vec2.SetRandn(); CuVector vec1_orig(vec1); BaseFloat alpha = 0.43243; vec1.AddVec(alpha, vec2); for (int32 i = 0; i < M; i++) AssertEqual(vec1_orig(i) + alpha * vec2(i), vec1(i)); } template void CuVectorUnitTestAddVecCross() { for (int32 i = 0; i < 4; i++) { int32 M = 10 + Rand() % 100; CuVector vec1(M); CuVector vec2(M); vec1.SetRandn(); vec2.SetRandn(); if (i == 0) { CuVector vec1_orig(vec1); Real alpha = 0.43243; vec1.AddVec(alpha, vec2); for (int32 i = 0; i < M; i++) AssertEqual(vec1_orig(i) + alpha * vec2(i), vec1(i)); } else { CuVector vec2_orig(vec2); Real alpha = 0.43243; vec2.AddVec(alpha, vec1); for (int32 i = 0; i < M; i++) AssertEqual(vec2_orig(i) + alpha * vec1(i), vec2(i)); } } } template void CuVectorUnitTestAddVecExtra() { int32 M = 10 + Rand() % 100; CuVector vec1(M), vec2(M); vec1.SetRandn(); vec2.SetRandn(); CuVector vec1_orig(vec1); BaseFloat alpha = 0.43243, beta = 1.4321; vec1.AddVec(alpha, vec2, beta); for (int32 i = 0; i < M; i++) AssertEqual(beta * vec1_orig(i) + alpha * vec2(i), vec1(i)); } template void CuVectorUnitTestCopyElements() { int32 dim = 10 + Rand() % 100, N = 20 + Rand() % 50; CuVector V(dim); V.SetRandn(); CuVector V_copy(V); for (int n = 0; n < 2; n++) { bool transpose = (n == 0); CuMatrix M; if (!transpose) M.Resize(dim, N, kUndefined); else M.Resize(N, dim, kUndefined); M.SetRandn(); std::vector elements(dim); for (int32 i = 0; i < dim; i++) { int32 j = elements[i] = Rand() % N; if (!transpose) V_copy(i) = M(i, j); else V_copy(i) = M(j, i); } CuArray cu_elements(elements); V.CopyElements(M, transpose ? kTrans : kNoTrans, cu_elements); AssertEqual(V, V_copy); } } template void UnitTestVecMatVec() { int32 NR = 10 + Rand() % 100, NC = 20 + Rand() % 100; CuVector v1(NR), v2(NC); v1.SetRandn(); v2.SetRandn(); CuMatrix M(NR, NC); M.SetRandn(); Real sum = 0; for (int32 i = 0; i < NR; i++) for (int32 j = 0; j < NC; j++) sum += v1(i) * M(i, j) * v2(j); Real result = VecMatVec(v1, M, v2); AssertEqual(sum, result); } template void CuVectorUnitTestAddRowSumMat() { int32 M = 10 + Rand() % 280, N = 10 + Rand() % 20; BaseFloat alpha = 10.0143432, beta = 43.4321; CuMatrix mat(N, M); mat.SetRandn(); CuVector vec(M); mat.SetRandn(); Matrix mat2(mat); Vector vec2(M); vec.AddRowSumMat(alpha, mat, beta); vec2.AddRowSumMat(alpha, mat2, beta); Vector vec3(vec); AssertEqual(vec2, vec3); } template void CuVectorUnitTestAddColSumMat() { int32 M = 10 + Rand() % 280, N = 10 + Rand() % 20; BaseFloat alpha = 10.0143432, beta = 43.4321; CuMatrix mat(M, N); mat.SetRandn(); CuVector vec(M); mat.SetRandn(); Matrix mat2(mat); Vector vec2(M); vec.AddColSumMat(alpha, mat, beta); vec2.AddColSumMat(alpha, mat2, beta); Vector vec3(vec); AssertEqual(vec2, vec3); } template void CuVectorUnitTestApproxEqual() { int32 M = 10 + Rand() % 100; CuVector vec1(M), vec2(M); vec1.SetRandn(); vec2.SetRandn(); Real tol = 0.5; for (int32 i = 0; i < 10; i++) { Real sumsq = 0.0, sumsq_orig = 0.0; for (int32 j = 0; j < M; j++) { sumsq += (vec1(j) - vec2(j)) * (vec1(j) - vec2(j)); sumsq_orig += vec1(j) * vec1(j); } Real rms = sqrt(sumsq), rms_orig = sqrt(sumsq_orig); KALDI_ASSERT(vec1.ApproxEqual(vec2, tol) == (rms <= tol * rms_orig)); tol *= 2.0; } } template static void UnitTestCuVectorReplaceValue() { for (int32 i = 0; i < 5; i++) { int32 dim = 100 + Rand() % 200; Real orig = 0.1 * (Rand() % 100), changed = 0.1 * (Rand() % 50); Vector vec(dim); vec.SetRandn(); vec(dim / 2) = orig; CuVector vec1(vec); vec.ReplaceValue(orig, changed); vec1.ReplaceValue(orig, changed); Vector vec2(vec1); AssertEqual(vec, vec2); } } template static void UnitTestCuVectorSum() { for (int32 i = 0; i < 200; i++) { int32 start_dim = RandInt(1, 500), end_dim = RandInt(1, 500); int32 dim = RandInt(10, 12000) + start_dim + end_dim; Real quiet_nan = nan(""); // this is from . Vector vec(start_dim + dim + end_dim); vec.Range(0, start_dim).Set(quiet_nan); vec.Range(start_dim, dim).Set(1.0); vec.Range(start_dim + dim, end_dim).Set(quiet_nan); BaseFloat sum = vec.Range(start_dim, dim).Sum(); KALDI_ASSERT(ApproxEqual(sum, dim)); } } template void CuVectorUnitTestInvertElements() { // Also tests MulElements(); int32 M = 256 + Rand() % 100; CuVector vec1(M); vec1.SetRandn(); CuVector vec2(vec1); vec2.InvertElements(); CuVector vec3(vec1); vec3.MulElements(vec2); // vec3 should be all ones. Real prod = VecVec(vec3, vec3); AssertEqual(prod, static_cast(M)); } template void CuVectorUnitTestSum() { for (int32 p = 1; p <= 1000000; p *= 2) { MatrixIndexT dim = p + Rand() % 500; CuVector A(dim), ones(dim); A.SetRandn(); ones.Set(1.0); Real x = VecVec(A, ones); Real y = A.Sum(); Real diff = std::abs(x - y); // Note: CuVectorBase<> does not have an ApplyAbs() member // function, so we copy back to a host vector for simplicity in // this test case. Vector A_host(A); A_host.ApplyAbs(); Real s = A_host.Sum(); KALDI_ASSERT ( diff <= 1.0e-04 * s); } } template void CuVectorUnitTestScale() { for (int32 i = 0; i < 4; i++) { int32 dim = 100 + Rand() % 400; CuVector cu_vec(dim); cu_vec.SetRandn(); Vector vec(cu_vec); BaseFloat scale = 0.333; cu_vec.Scale(scale); vec.Scale(scale); Vector vec2(cu_vec); KALDI_ASSERT(ApproxEqual(vec, vec2)); } } template void CuVectorUnitTestCopyFromMat() { int32 M = 100 + Rand() % 255, N = 100 + Rand() % 255; CuMatrix cu_matrix(M, N); cu_matrix.SetRandn(); for(int32 i = 0; i < N; i++) { CuVector vector(M); vector.CopyColFromMat(cu_matrix, i); for(int32 j = 0; j < M; j++) { KALDI_ASSERT(vector(j)==cu_matrix(j, i)); } } Matrix matrix(cu_matrix), matrix2(M, N); CuMatrix matrix3(M, N); CuVector vector(M * N), vector2(M * N); vector.CopyRowsFromMat(cu_matrix); vector2.CopyRowsFromMat(matrix); matrix2.CopyRowsFromVec(vector2); matrix3.CopyRowsFromVec(Vector(vector2)); Vector vector3(M * N); vector3.CopyRowsFromMat(cu_matrix); for(int32 j = 0; j < M*N; j++) { if (Rand() % 500 == 0) { // random small subset (it was slow) KALDI_ASSERT(vector(j) == cu_matrix(j/N, j%N)); KALDI_ASSERT(vector2(j) == cu_matrix(j/N, j%N)); KALDI_ASSERT(vector2(j) == matrix2(j/N, j%N)); KALDI_ASSERT(vector3(j) == matrix2(j/N, j%N)); KALDI_ASSERT(vector3(j) == matrix3(j/N, j%N)); } } } template void CuVectorUnitTestCopyDiagFromPacked() { for (int32 i = 0; i < 5; i++) { int32 N = 100 + Rand() % 255; CuSpMatrix S(N); S.SetRandn(); CuVector V(N, kUndefined); V.CopyDiagFromPacked(S); SpMatrix cpu_S(S); Vector cpu_V(N); cpu_V.CopyDiagFromPacked(cpu_S); Vector cpu_V2(V); KALDI_ASSERT(cpu_V.ApproxEqual(cpu_V2)); } } template void CuVectorUnitTestCopyCross() { for (int32 i = 0; i < 10; i++) { int32 M = 100 + Rand() % 255; if (Rand() % 3 == 0) M = 0; CuVector v1(M); v1.SetRandn(); CuVector v2(M); v2.CopyFromVec(v1); CuVector v3(M); v3.CopyFromVec(v2); AssertEqual(v1, v3); } } template void CuVectorUnitTestCopyCross2() { for (int32 i = 0; i < 10; i++) { int32 M = 100 + Rand() % 255; if (Rand() % 3 == 0) M = 0; CuVector v1(M); v1.SetRandn(); Vector v2(M); v2.CopyFromVec(v1); CuVector v3(M); v3.CopyFromVec(v2); AssertEqual(v1, v3); } } template void CuVectorUnitTestCopyDiagFromMat() { for (int32 i = 0; i < 5; i++) { int32 M = 100 + Rand() % 255, N = M + Rand() % 2; Matrix matrix(M, N); if (i % 2 == 0) matrix.Transpose(); matrix.SetRandn(); Vector vector(M, kUndefined); vector.CopyDiagFromMat(matrix); CuMatrix cuda_matrix(matrix); CuVector cuda_vector(M, kUndefined); cuda_vector.CopyDiagFromMat(cuda_matrix); Vector vector2(cuda_vector); AssertEqual(vector, vector2); AssertEqual(vector.Sum(), cuda_matrix.Trace(false)); AssertEqual(cuda_vector.Sum(), matrix.Trace(false)); } } template void CuVectorUnitTestNorm() { int32 dim = 2; CuVector cu_vector(dim); cu_vector(0) = 1.0; cu_vector(1) = -2.0; KALDI_ASSERT(ApproxEqual(cu_vector.Norm(1.0), 3.0)); KALDI_ASSERT(ApproxEqual(cu_vector.Norm(2.0), sqrt(5.0))); } template void CuVectorUnitTestMin() { for (int32 p = 1; p <= 1000000; p *= 2) { int32 dim = p + Rand() % 500; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); Real min1 = cu_vector.Min(), min2 = vector.Min(); KALDI_ASSERT(min1 == min2); } } template void CuVectorUnitTestMax() { for (int32 p = 1; p <= 1000000; p *= 2) { int32 dim = p + Rand() % 500; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); Real max1 = cu_vector.Max(), max2 = vector.Max(); KALDI_ASSERT(max1 == max2); } } template void CuVectorUnitTestApplySoftMax() { for (int32 i = 0; i < 10; i++) { int32 dim = 100 + Rand() % 300; //int32 dim = 1024; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); cu_vector.ApplySoftMax(); vector.ApplySoftMax(); CuVector cu_vector2(vector); //std::cout< void CuVectorUnitTestApplyExp() { int32 dim = 100; CuVector vector(dim); vector.SetRandn(); CuVector vector2(vector); vector.ApplyExp(); for(int32 j = 0; j < dim; j++) { //std::cout<<"diff is "< void CuVectorUnitTestApplyLog() { int32 dim = 100; CuVector vector(dim); vector.SetRandn(); for(int32 j = 0; j < dim; j++) { if(vector(j) <= 0.0) vector(j) = 1.0 - vector(j); } CuVector vector2(vector); vector.ApplyLog(); for(int32 j = 0; j < dim; j++) { //std::cout<<"diff is "< void CuVectorUnitTestApplyFloor() { for (int32 l = 0; l < 10; l++) { int32 dim = 100 + Rand() % 700; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); BaseFloat floor = 0.33 * (-5 + Rand() % 10); MatrixIndexT i, j; cu_vector.ApplyFloor(floor, &i); vector.ApplyFloor(floor, &j); CuVector cu2(vector); AssertEqual(cu2, cu_vector); if (i != j) { KALDI_WARN << "ApplyFloor return code broken..."; } KALDI_ASSERT(i==j); } } template void CuVectorUnitTestApplyFloorNoCount() { for (int32 l = 0; l < 10; l++) { int32 dim = 100 + Rand() % 700; CuVector cu_vector1(dim); cu_vector1.SetRandn(); CuVector cu_vector2(cu_vector1); BaseFloat floor = 0.33 * (-5 + Rand() % 10); MatrixIndexT dummy_count; cu_vector1.ApplyFloor(floor, &dummy_count); cu_vector2.ApplyFloor(floor, nullptr); AssertEqual(cu_vector1, cu_vector2); } } template void CuVectorUnitTestApplyCeiling() { for (int32 l = 0; l < 10; l++) { int32 dim = 100 + Rand() % 700; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); BaseFloat floor = 0.33 * (-5 + Rand() % 10); MatrixIndexT i, j; cu_vector.ApplyCeiling(floor, &i); vector.ApplyCeiling(floor, &j); CuVector cu2(vector); AssertEqual(cu2, cu_vector); if (i != j) { KALDI_WARN << "ApplyCeiling return code broken..."; } KALDI_ASSERT(i==j); } } template void CuVectorUnitTestApplyCeilingNoCount() { for (int32 l = 0; l < 10; l++) { int32 dim = 100 + Rand() % 700; CuVector cu_vector1(dim); cu_vector1.SetRandn(); CuVector cu_vector2(cu_vector1); BaseFloat floor = 0.33 * (-5 + Rand() % 10); MatrixIndexT dummy_count; cu_vector1.ApplyCeiling(floor, &dummy_count); cu_vector2.ApplyCeiling(floor, nullptr); AssertEqual(cu_vector1, cu_vector2); } } template void CuVectorUnitTestApplyPow() { for (int32 l = 0; l < 10; l++) { int32 dim = 100 + Rand() % 700; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); BaseFloat pow = -2 + (Rand() % 5); cu_vector.ApplyPow(pow); vector.ApplyPow(pow); CuVector cu2(vector); AssertEqual(cu2, cu_vector); } } template void CuVectorUnitTestAddVecVec() { int32 dim = 100; CuVector cu_vector(dim); cu_vector.SetRandn(); Vector vector(cu_vector); Real beta = Rand(); Real alpha = Rand(); Vector v(dim), r(dim); v.SetRandn(); r.SetRandn(); CuVector cuV(v), cuR(r); cu_vector.AddVecVec(alpha, cuR, cuV, beta); vector.AddVecVec(alpha, r, v, beta); CuVector cu2(vector); std::cout< void CuVectorUnitTestAddDiagMat2() { for (int p = 0; p < 4; p++) { int32 M = 230 + Rand() % 100, N = 230 + Rand() % 100; BaseFloat alpha = 0.2 + Rand() % 3, beta = 0.3 + Rand() % 2; CuVector cu_vector(M); cu_vector.SetRandn(); CuMatrix cu_mat_orig(M, N); cu_mat_orig.SetRandn(); MatrixTransposeType trans = (p % 2 == 0 ? kNoTrans : kTrans); CuMatrix cu_mat(cu_mat_orig, trans); Vector vector(cu_vector); Matrix mat(cu_mat); vector.AddDiagMat2(alpha, mat, trans, beta); cu_vector.AddDiagMat2(alpha, cu_mat, trans, beta); Vector vector2(cu_vector); AssertEqual(vector, vector2); } } template static void CuVectorUnitTestAddDiagMatMat() { for (MatrixIndexT iter = 0; iter < 4; iter++) { BaseFloat alpha = 0.432 + Rand() % 5, beta = 0.043 + Rand() % 2; MatrixIndexT dimM = 10 + Rand() % 300, dimN = 5 + Rand() % 300; CuVector v(dimM); CuMatrix M_orig(dimM, dimN), N_orig(dimN, dimM); M_orig.SetRandn(); N_orig.SetRandn(); MatrixTransposeType transM = (iter % 2 == 0 ? kNoTrans : kTrans); MatrixTransposeType transN = ((iter/2) % 2 == 0 ? kNoTrans : kTrans); CuMatrix M(M_orig, transM), N(N_orig, transN); v.SetRandn(); CuVector w(v); w.AddDiagMatMat(alpha, M, transM, N, transN, beta); { CuVector w2(v); CuMatrix MN(dimM, dimM); MN.AddMatMat(1.0, M, transM, N, transN, 0.0); CuVector d(dimM); d.CopyDiagFromMat(MN); w2.Scale(beta); w2.AddVec(alpha, d); AssertEqual(w, w2); } } } template void CuVectorUnitTestAddMatVec() { for (int32 i = 0; i < 10; i++) { int32 M = 10 + Rand() % 500, N = 10 + Rand() % 400; bool transpose = (i % 2 == 0); CuVector src_cu(M); src_cu.SetRandn(); Vector src(src_cu); CuVector dst_cu(N); dst_cu.SetRandn(); Vector dst(dst_cu); CuMatrix mat_cu(transpose ? M : N, transpose ? N : M); mat_cu.SetRandn(); Matrix mat(mat_cu); BaseFloat alpha = 0.5 * (Rand() % 10), beta = 0.5 * (Rand() % 10); dst_cu.AddMatVec(alpha, mat_cu, transpose ? kTrans : kNoTrans, src_cu, beta); dst.AddMatVec(alpha, mat, transpose ? kTrans : kNoTrans, src, beta); Vector dst2(dst_cu); AssertEqual(dst, dst2); } } template void CuVectorUnitTestAddSpVec() { for (int32 i = 0; i < 5; i++) { int32 M = 100 + Rand() % 256; CuVector src_cu(M); src_cu.SetRandn(); Vector src(src_cu); CuVector dst_cu(M); dst_cu.SetRandn(); Vector dst(dst_cu); CuSpMatrix mat_cu(M); mat_cu.SetRandn(); SpMatrix mat(mat_cu); BaseFloat alpha = 0.5 * (Rand() % 5), beta = 0.5 * (Rand() % 5); dst_cu.AddSpVec(alpha, mat_cu, src_cu, beta); dst.AddSpVec(alpha, mat, src, beta); Vector dst2(dst_cu); AssertEqual(dst, dst2); } } template void CuVectorUnitTest() { UnitTestCuVectorCopyFromVec(); #if HAVE_CUDA == 1 if (CuDevice::Instantiate().DoublePrecisionSupported()) #endif UnitTestCuVectorCopyFromVec(); UnitTestCuVectorIO(); CuVectorUnitTestVecVec(); CuVectorUnitTestAddVec(); CuVectorUnitTestAddVecCross(); CuVectorUnitTestAddVecExtra(); CuVectorUnitTestApproxEqual(); CuVectorUnitTestScale(); CuVectorUnitTestSum(); CuVectorUnitTestInvertElements(); UnitTestCuVectorSum(); CuVectorUnitTestAddRowSumMat(); CuVectorUnitTestAddColSumMat(); UnitTestCuVectorReplaceValue(); UnitTestCuVectorAddTp(); UnitTestCuVectorMulTp(); UnitTestCuSubVector(); CuVectorUnitTestCopyFromMat(); CuVectorUnitTestMin(); CuVectorUnitTestMax(); CuVectorUnitTestApplySoftMax(); CuVectorUnitTestCopyDiagFromPacked(); CuVectorUnitTestCopyDiagFromMat(); CuVectorUnitTestCopyCross(); CuVectorUnitTestCopyCross2(); CuVectorUnitTestNorm(); CuVectorUnitTestApplyExp(); CuVectorUnitTestApplyLog(); CuVectorUnitTestApplyFloor(); CuVectorUnitTestApplyFloorNoCount(); CuVectorUnitTestApplyCeilingNoCount(); CuVectorUnitTestApplyCeiling(); CuVectorUnitTestApplyPow(); CuVectorUnitTestAddMatVec(); CuVectorUnitTestAddSpVec(); CuVectorUnitTestAddVecVec(); CuVectorUnitTestAddDiagMat2(); CuVectorUnitTestAddDiagMatMat(); CuVectorUnitTestCopyElements(); UnitTestVecMatVec(); } } // namespace kaldi int main(int argc, char *argv[]) { using namespace kaldi; SetVerboseLevel(1); const char *usage = "Usage: cu-vector-test [options]"; ParseOptions po(usage); std::string use_gpu = "yes"; po.Register("use-gpu", &use_gpu, "yes|no|optional"); po.Read(argc, argv); if (po.NumArgs() != 0) { po.PrintUsage(); exit(1); } int32 loop = 0; #if HAVE_CUDA == 1 for (; loop < 2; loop++) { CuDevice::Instantiate().SetDebugStrideMode(true); if (loop == 0) CuDevice::Instantiate().SelectGpuId("no"); // -1 means no GPU else CuDevice::Instantiate().SelectGpuId(use_gpu); #endif kaldi::CuVectorUnitTest(); #if HAVE_CUDA == 1 if (CuDevice::Instantiate().DoublePrecisionSupported()) { kaldi::CuVectorUnitTest(); } else { KALDI_WARN << "Double precision not supported"; } #else kaldi::CuVectorUnitTest(); #endif if (loop == 0) KALDI_LOG << "Tests without GPU use succeeded."; else KALDI_LOG << "Tests with GPU use (if available) succeeded."; #if HAVE_CUDA == 1 } CuDevice::Instantiate().PrintProfile(); #endif return 0; }