Skip to content
Snippets Groups Projects
Commit b885535e authored by Shiyin Kang's avatar Shiyin Kang
Browse files

add speed test and unit test for DiffLogSoftmax

parent 7a525668
No related branches found
No related tags found
No related merge requests found
......@@ -458,7 +458,6 @@ template<typename Real> void TestCuMatrixMulRowsGroupMat(int32 dim) {
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuMatrixDiffSoftmax(int32 dim) {
BaseFloat time_in_secs = 0.025;
CuMatrix<Real> M(dim, dim), N(dim, dim), L(dim, dim);
......@@ -477,6 +476,24 @@ template<typename Real> void TestCuMatrixDiffSoftmax(int32 dim) {
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuMatrixDiffLogSoftmax(int32 dim) {
BaseFloat time_in_secs = 0.025;
CuMatrix<Real> M(dim, dim), N(dim, dim), L(dim, dim);
M.SetRandn();
N.SetRandn();
L.SetRandn();
Timer tim;
int32 iter = 0;
for (; tim.Elapsed() < time_in_secs; iter++) {
N.DiffLogSoftmaxPerRow(M, L);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuMatrix::DiffLogSoftmaxPerRow" << NameOf<Real>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuMatrixSoftmax(int32 dim) {
BaseFloat time_in_secs = 0.025;
CuMatrix<Real> M(dim, dim), N(dim, dim);
......@@ -1021,6 +1038,8 @@ template<typename Real> void CudaMatrixSpeedTest() {
TestCuMatrixSoftmax<Real>(sizes[s]);
for (int32 s = 0; s < ns; s++)
TestCuMatrixDiffSoftmax<Real>(sizes[s]);
for (int32 s = 0; s < ns; s++)
TestCuMatrixDiffLogSoftmax<Real>(sizes[s]);
for (int32 s = 0; s < ns; s++)
TestCuMatrixLogSoftmax<Real>(sizes[s]);
for (int32 s = 0; s < ns; s++)
......
......@@ -2047,6 +2047,43 @@ static void UnitTestCuDiffSoftmax() {
}
template<typename Real>
static void UnitTestCuDiffLogSoftmax() {
int m = 100, n = 111;
Matrix<Real> Hi(m, n);
Matrix<Real> Ho(m, n);
Matrix<Real> Hy(m, n);
Hi.SetRandn();
RandZeroToOneMatrix(&Hy);
CuMatrix<Real> Di(m, n);
CuMatrix<Real> Do(m, n);
CuMatrix<Real> Dy(m, n);
Di.CopyFromMat(Hi);
Dy.CopyFromMat(Hy);
//gpu
Do.DiffLogSoftmaxPerRow(Dy, Di);
//cpu
{
const MatrixBase<Real> &Y(Hy), &E(Hi);
MatrixBase<Real> &D(Ho);
D.CopyFromMat(Y);
D.ApplyExp(); // exp(y)
Vector<Real> E_sum(D.NumRows()); // Initializes to zero
E_sum.AddColSumMat(1.0, E); // Sum(e)
D.MulRowsVec(E_sum); // exp(y) Sum(e)
D.Scale(-1.0); // - exp(y) Sum(e)
D.AddMat(1.0, E, kNoTrans); // e - exp(y_i) Sum(e)
}
Matrix<Real> Ho2(m, n);
Do.CopyToMat(&Ho2);
AssertEqual(Ho, Ho2);
}
template<typename Real>
static void UnitTestCuSoftmax() {
......@@ -2645,6 +2682,7 @@ template<typename Real> void CudaMatrixUnitTest() {
UnitTestCuMatrixAdd2<Real>();
UnitTestCuDiffSigmoid<Real>();
UnitTestCuDiffSoftmax<Real>();
UnitTestCuDiffLogSoftmax<Real>();
UnitTestCuMatrixGroupPnorm<Real>();
UnitTestCuMatrixGroupPnormDeriv<Real>();
UnitTestCuMatrixDiffGroupPnorm<Real>();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment