51 if (
i % 5 == 0) { dimM = 0; }
54 std::ostringstream os;
55 bool binary = (
i % 4 < 2);
56 vec.
Write(os, binary);
59 std::istringstream is(os.str());
60 vec2.
Read(is, binary);
66 template<
typename Real,
typename OtherReal>
84 template<
typename Real>
86 for (
int32 iter = 0 ; iter < 10; iter++) {
87 int32 M1 = 1 + rand () % 10, M2 = 1 +
Rand() % 1, M3 = 1 +
Rand() % 10, M = M1 + M2 + M3,
92 subvec2 = vec.
Range(M1, M2);
93 Real f1 = vec(M1 + m), f2 = subvec1(m), f3 = subvec2(m);
101 template<
typename Real>
121 template<
typename Real>
151 prod += vec1(
i) * vec2(
i);
179 Real alpha = 0.43243;
186 Real alpha = 0.43243;
200 BaseFloat alpha = 0.43243, beta = 1.4321;
201 vec1.AddVec(alpha, vec2, beta);
212 for (
int n = 0;
n < 2;
n++) {
213 bool transpose = (
n == 0);
220 std::vector<int32> elements(dim);
244 sum += v1(
i) * M(
i,
j) * v2(
j);
251 BaseFloat alpha = 10.0143432, beta = 43.4321;
266 BaseFloat alpha = 10.0143432, beta = 43.4321;
287 Real sumsq = 0.0, sumsq_orig = 0.0;
289 sumsq += (vec1(
j) - vec2(
j)) * (vec1(
j) - vec2(
j));
290 sumsq_orig += vec1(
j) * vec1(
j);
292 Real rms = sqrt(sumsq), rms_orig = sqrt(sumsq_orig);
293 KALDI_ASSERT(vec1.ApproxEqual(vec2, tol) == (rms <= tol * rms_orig));
301 Real orig = 0.1 * (
Rand() % 100), changed = 0.1 * (
Rand() % 50);
317 Real quiet_nan = nan(
"");
319 vec.
Range(0, start_dim).Set(quiet_nan);
320 vec.
Range(start_dim, dim).Set(1.0);
321 vec.
Range(start_dim + dim, end_dim).Set(quiet_nan);
337 Real prod =
VecVec(vec3, vec3);
342 for (
int32 p = 1; p <= 1000000; p *= 2) {
350 Real diff = std::abs(x - y);
356 Real s = A_host.
Sum();
390 vector.CopyRowsFromMat(cu_matrix);
399 if (
Rand() % 500 == 0) {
427 if (
Rand() % 3 == 0) M = 0;
441 if (
Rand() % 3 == 0) M = 0;
483 for (
int32 p = 1; p <= 1000000; p *= 2) {
488 Real min1 = cu_vector.
Min(), min2 = vector.
Min();
495 for (
int32 p = 1; p <= 1000000; p *= 2) {
500 Real max1 = cu_vector.
Max(), max2 = vector.
Max();
542 vector(
j) = 1.0 - vector(
j);
555 for (
int32 l = 0; l < 10; l++) {
570 KALDI_WARN <<
"ApplyFloor return code broken...";
577 for (
int32 l = 0; l < 10; l++) {
592 for (
int32 l = 0; l < 10; l++) {
607 KALDI_WARN <<
"ApplyCeiling return code broken...";
614 for (
int32 l = 0; l < 10; l++) {
629 for (
int32 l = 0; l < 10; l++) {
660 cu_vector.
AddVecVec(alpha, cuR, cuV, beta);
664 std::cout<<cu2(0)<<
' '<<cu_vector(0)<<std::endl;
669 for (
int p = 0; p < 4; p++) {
691 template<
typename Real>
696 dimN = 5 +
Rand() % 300;
713 MN.
AddMatMat(1.0, M, transM, N, transN, 0.0);
729 bool transpose = (
i % 2 == 0);
771 dst_cu.
AddSpVec(alpha, mat_cu, src_cu, beta);
772 dst.
AddSpVec(alpha, mat, src, beta);
781 UnitTestCuVectorCopyFromVec<Real, float>();
783 if (CuDevice::Instantiate().DoublePrecisionSupported())
785 UnitTestCuVectorCopyFromVec<Real, double>();
786 UnitTestCuVectorIO<Real>();
787 CuVectorUnitTestVecVec<Real>();
788 CuVectorUnitTestAddVec<Real>();
789 CuVectorUnitTestAddVecCross<Real>();
790 CuVectorUnitTestAddVecExtra<Real>();
791 CuVectorUnitTestApproxEqual<Real>();
792 CuVectorUnitTestScale<Real>();
793 CuVectorUnitTestSum<Real>();
794 CuVectorUnitTestInvertElements<Real>();
795 UnitTestCuVectorSum<Real>();
796 CuVectorUnitTestAddRowSumMat<Real>();
797 CuVectorUnitTestAddColSumMat<Real>();
798 UnitTestCuVectorReplaceValue<Real>();
799 UnitTestCuVectorAddTp<Real>();
800 UnitTestCuVectorMulTp<Real>();
801 UnitTestCuSubVector<Real>();
802 CuVectorUnitTestCopyFromMat<Real>();
803 CuVectorUnitTestMin<Real>();
804 CuVectorUnitTestMax<Real>();
805 CuVectorUnitTestApplySoftMax<Real>();
806 CuVectorUnitTestCopyDiagFromPacked<Real>();
807 CuVectorUnitTestCopyDiagFromMat<Real>();
808 CuVectorUnitTestCopyCross<Real>();
809 CuVectorUnitTestCopyCross2<Real>();
810 CuVectorUnitTestNorm<Real>();
811 CuVectorUnitTestApplyExp<Real>();
812 CuVectorUnitTestApplyLog<Real>();
813 CuVectorUnitTestApplyFloor<Real>();
814 CuVectorUnitTestApplyFloorNoCount<Real>();
815 CuVectorUnitTestApplyCeilingNoCount<Real>();
816 CuVectorUnitTestApplyCeiling<Real>();
817 CuVectorUnitTestApplyPow<Real>();
818 CuVectorUnitTestAddMatVec<Real>();
819 CuVectorUnitTestAddSpVec<Real>();
820 CuVectorUnitTestAddVecVec<Real>();
821 CuVectorUnitTestAddDiagMat2<Real>();
822 CuVectorUnitTestAddDiagMatMat<Real>();
823 CuVectorUnitTestCopyElements<Real>();
824 UnitTestVecMatVec<Real>();
831 int main(
int argc,
char *argv[]) {
832 using namespace kaldi;
834 const char *usage =
"Usage: cu-vector-test [options]";
837 std::string use_gpu =
"yes";
838 po.
Register(
"use-gpu", &use_gpu,
"yes|no|optional");
848 for (; loop < 2; loop++) {
849 CuDevice::Instantiate().SetDebugStrideMode(
true);
851 CuDevice::Instantiate().SelectGpuId(
"no");
853 CuDevice::Instantiate().SelectGpuId(use_gpu);
856 kaldi::CuVectorUnitTest<float>();
858 if (CuDevice::Instantiate().DoublePrecisionSupported()) {
859 kaldi::CuVectorUnitTest<double>();
861 KALDI_WARN <<
"Double precision not supported";
864 kaldi::CuVectorUnitTest<double>();
868 KALDI_LOG <<
"Tests without GPU use succeeded.";
870 KALDI_LOG <<
"Tests with GPU use (if available) succeeded.";
873 CuDevice::Instantiate().PrintProfile();
void MulElements(const CuVectorBase< Real > &v)
void CuVectorUnitTestCopyCross2()
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Packed symetric matrix class.
void CopyDiagFromPacked(const CuPackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
Real Trace(bool check_square=true) const
Return the trace. If check_square = true, will crash if matrix is not square.
void CuVectorUnitTestApproxEqual()
void CuVectorUnitTestInvertElements()
void CuVectorUnitTestApplyCeiling()
void MulTp(const CuTpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular marix: *this <– *this *M.
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
void Transpose()
Transpose the matrix.
void CuVectorUnitTestApplySoftMax()
void ApplyCeiling(Real ceiling_val, MatrixIndexT *ceiled_count=NULL)
void CopyDiagFromMat(const MatrixBase< Real > &M)
Extracts the diagonal of the matrix M.
void CopyColFromMat(const CuMatrixBase< Real > &mat, MatrixIndexT col)
void CuVectorUnitTestSum()
void CuVectorUnitTestApplyLog()
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void ApplyCeiling(Real ceil_val, MatrixIndexT *ceiled_count=nullptr)
Applies ceiling to all elements.
Real Trace(bool check_square=true) const
Returns trace of matrix.
void CuVectorUnitTestApplyFloor()
void CopyDiagFromMat(const CuMatrix< Real > &M)
Extracts the diagonal of a matrix.
void AddDiagMat2(Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, Real beta)
Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans)...
void AddDiagMat2(Real alpha, const MatrixBase< Real > &M, MatrixTransposeType trans=kNoTrans, Real beta=1.0)
Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans)...
Real VecMatVec(const VectorBase< Real > &v1, const MatrixBase< Real > &M, const VectorBase< Real > &v2)
Returns .
void MulTp(const TpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular matrix: *this <– *this *M.
void CuVectorUnitTestCopyDiagFromPacked()
A class for storing matrices.
void CuVectorUnitTestApplyExp()
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
void CopyRowsFromVec(const CuVectorBase< Real > &v)
This function has two modes of operation.
void AddSpVec(const Real alpha, const SpMatrix< Real > &M, const VectorBase< Real > &v, const Real beta)
Add symmetric positive definite matrix times vector: this <– beta*this + alpha*M*v.
void CuVectorUnitTestAddVecCross()
Real ApplySoftMax()
Apply soft-max to vector and return normalizer (log sum of exponentials).
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=NULL)
void SetRandn()
< Set to unit matrix.
void Register(const std::string &name, bool *ptr, const std::string &doc)
void CuVectorUnitTestAddColSumMat()
void ReplaceValue(Real orig, Real changed)
void SetRandn()
< Set to unit matrix.
void AddDiagMatMat(Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const CuMatrixBase< Real > &N, MatrixTransposeType transN, Real beta=1.0)
Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoT...
void CopyElements(const CuMatrixBase< Real > &mat, const MatrixTransposeType trans, const CuArrayBase< int32 > &elements)
Copies selected elements from 'mat' to *this.
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
void AddVecVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element product of vectors:
void CuVectorUnitTestAddVec()
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void Write(std::ostream &is, bool binary) const
void CuVectorUnitTestScale()
void CopyDiagFromPacked(const PackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
void CuVectorUnitTestAddMatVec()
void AddMatVec(const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, const CuVectorBase< Real > &v, const Real beta)
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
int main(int argc, char *argv[])
void AddColSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the columns of the matrix, add to vector.
void CuVectorUnitTestAddRowSumMat()
void CuVectorUnitTestMax()
static void UnitTestCuVectorAddTp()
void CuVectorUnitTestMin()
void CuVectorUnitTestAddVecExtra()
void CuVectorUnitTestCopyFromMat()
void SetRandn()
Sets to random values of a normal distribution.
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
void AddSpVec(const Real alpha, const CuSpMatrix< Real > &S, const CuVectorBase< Real > &v, const Real beta)
void CuVectorUnitTestAddVecVec()
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void ApplyPow(Real power)
Packed symetric matrix class.
void AddMatMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType transA, const CuMatrixBase< Real > &B, MatrixTransposeType transB, Real beta)
C = alpha * A(^T)*B(^T) + beta * C.
static void UnitTestCuVectorIO()
void CuVectorUnitTestApplyPow()
static void UnitTestCuVectorReplaceValue()
void Scale(Real alpha)
Multiplies all elements by this constant.
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
void CuVectorUnitTestApplyFloorNoCount()
int Rand(struct RandomState *state)
Real Sum() const
Returns sum of the elements.
void CuVectorUnitTestAddDiagMat2()
void SetRandn()
Set vector to random normally-distributed noise.
static void CuVectorUnitTestAddDiagMatMat()
void CuVectorUnitTestCopyCross()
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void AddVec(Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
CuSubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
void CuVectorUnitTestCopyDiagFromMat()
void AddTpVec(const Real alpha, const TpMatrix< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add triangular matrix times vector: this <– beta*this + alpha*M*v.
A class representing a vector.
#define KALDI_ASSERT(cond)
void ApplyPow(Real power)
Take all elements of vector to a power.
void CuVectorUnitTestAddSpVec()
void Read(std::istream &is, bool binary)
I/O.
void AddVecVec(Real alpha, const CuVectorBase< Real > &v, const CuVectorBase< Real > &r, Real beta)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
static void UnitTestCuSubVector()
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
static void UnitTestCuVectorCopyFromVec()
void CuVectorUnitTestApplyCeilingNoCount()
void ReplaceValue(Real orig, Real changed)
Set each element to y = (x == orig ? changed : x).
void CuVectorUnitTestNorm()
void ApplyAbs()
Take absolute value of each of the elements.
void AddColSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of columns of M) + beta * *this.
void AddTpVec(const Real alpha, const CuTpMatrix< Real > &M, const MatrixTransposeType trans, const CuVectorBase< Real > &v, const Real beta)
Add triangular matrix times vector: this <– beta*this + alpha*M*v.
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
static void UnitTestCuVectorMulTp()
static void UnitTestCuVectorSum()
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
void CopyRowsFromMat(const CuMatrixBase< Real > &M)
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
void AddRowSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the rows of the matrix, add to vector.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
void CuVectorUnitTestCopyElements()
void CuVectorUnitTestVecVec()
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).