Skip to content

Commit cd3ae2d

Browse files
author
danielpovey
committed
sandbox/nnet3: committing some more partial work.
git-svn-id: http://svn.code.sf.net/p/kaldi/code@5057 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
1 parent 23df91e commit cd3ae2d

23 files changed

Lines changed: 3732 additions & 1677 deletions

‎src/Makefile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ SHELL := /bin/bash
88
SUBDIRS = base matrix util feat tree thread gmm transform sgmm \
99
fstext hmm lm decoder lat cudamatrix nnet \
1010
bin fstbin gmmbin fgmmbin sgmmbin featbin \
11-
nnetbin latbin sgmm2 sgmm2bin nnet2 nnet2bin kwsbin \
11+
nnetbin latbin sgmm2 sgmm2bin nnet2 nnet3 nnet2bin kwsbin \
1212
ivector ivectorbin online2 online2bin lmbin
1313

1414
MEMTESTDIRS = base matrix util feat tree thread gmm transform sgmm \
1515
fstext hmm lm decoder lat nnet \
1616
bin fstbin gmmbin fgmmbin sgmmbin featbin \
17-
nnetbin latbin sgmm2 nnet2 nnet2bin sgmm2bin kwsbin \
17+
nnetbin latbin sgmm2 nnet2 nnet3 nnet2bin sgmm2bin kwsbin \
1818
ivector ivectorbin online2 online2bin lmbin
1919

2020
CUDAMEMTESTDIR = cudamatrix
@@ -169,6 +169,7 @@ lat: base util hmm tree matrix
169169
cudamatrix: base util matrix
170170
nnet: base util matrix cudamatrix
171171
nnet2: base util matrix thread lat gmm hmm tree transform cudamatrix
172+
nnet3: base util matrix thread lat gmm hmm tree transform cudamatrix
172173
ivector: base util matrix thread transform tree gmm
173174
#3)Dependencies for optional parts of Kaldi
174175
onlinebin: base matrix util feat tree optimization gmm transform sgmm sgmm2 fstext hmm lm decoder lat cudamatrix nnet nnet2 online thread

‎src/cudamatrix/cu-matrix.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ template<class Real>
196196
template<class OtherReal>
197197
void CuMatrixBase<Real>::CopyFromMat(const CuMatrixBase<OtherReal> &M,
198198
MatrixTransposeType Trans) {
199+
if (sizeof(Real) == sizeof(OtherReal) &&
200+
static_cast<const void*>(M.Data()) ==
201+
static_cast<const void*>(this->Data())) {
202+
203+
// CopyFromMat called on same data. Nothing to do (except sanity checks)
204+
KALDI_ASSERT(Trans == kNoTrans && M.NumRows() == NumRows() &&
205+
M.NumCols() == NumCols() && M.Stride() == Stride());
206+
return;
207+
}
199208
#if HAVE_CUDA == 1
200209
if (CuDevice::Instantiate().Enabled()) {
201210
if (Trans == kNoTrans) {

‎src/cudamatrix/cu-matrix.h‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,22 +482,30 @@ class CuMatrixBase {
482482
// Output stored to 'mask', values : 1.0 = equal, 0.0 = not-equal.
483483
void EqualElementMask(const CuMatrixBase<Real> &mat, CuMatrix<Real> *mask) const;
484484

485+
486+
/// Get raw row pointer (const). Warning: may return a pointer to GPU memory. Use at
487+
/// your own risk.
488+
inline const Real* RowData(MatrixIndexT r) const { return data_ + r * stride_; }
489+
/// Get raw row pointer. Warning: may return a pointer to GPU memory. Use at
490+
/// your own risk.
491+
inline Real* RowData(MatrixIndexT r) { return data_ + r * stride_; }
492+
/// Return data pointer (const). Warning: may return a pointer to GPU memory.
493+
/// Use at your own risk.
494+
inline const Real *Data() const { return data_; }
495+
/// Return data pointer. Warning: may return a pointer to GPU memory. Use at
496+
/// your own risk.
497+
inline Real *Data() { return data_; }
485498
protected:
486-
// The following two functions should only be called if we did not compile with CUDA
487-
// or could not get a CUDA card; in that case the contents are interpreted the
488-
// same as a regular matrix.
499+
// The following two functions should only be called if we did not compile
500+
// with CUDA or could not get a CUDA card; in that case the contents are
501+
// interpreted the same as a regular matrix.
489502
inline const MatrixBase<Real> &Mat() const {
490503
return *(reinterpret_cast<const MatrixBase<Real>* >(this));
491504
}
492505
inline MatrixBase<Real> &Mat() {
493506
return *(reinterpret_cast<MatrixBase<Real>* >(this));
494507
}
495508

496-
/// Get raw row pointer
497-
inline const Real* RowData(MatrixIndexT r) const { return data_ + r * stride_; }
498-
inline Real* RowData(MatrixIndexT r) { return data_ + r * stride_; }
499-
inline const Real *Data() const { return data_; }
500-
inline Real *Data() { return data_; }
501509

502510

503511

‎src/matrix/kaldi-matrix.cc‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,16 @@ void Matrix<Real>::Resize(const MatrixIndexT rows,
704704

705705
template<typename Real>
706706
template<typename OtherReal>
707-
void MatrixBase<Real>::CopyFromMat(const MatrixBase<OtherReal> & M,
707+
void MatrixBase<Real>::CopyFromMat(const MatrixBase<OtherReal> &M,
708708
MatrixTransposeType Trans) {
709-
if (sizeof(Real) == sizeof(OtherReal) && (void*)(&M) == (void*)this)
710-
return; // CopyFromMat called from ourself. Nothing to do.
709+
if (sizeof(Real) == sizeof(OtherReal) &&
710+
static_cast<const void*>(M.Data()) ==
711+
static_cast<const void*>(this->Data())) {
712+
// CopyFromMat called on same data. Nothing to do (except sanity checks).
713+
KALDI_ASSERT(Trans == kNoTrans && M.NumRows() == NumRows() &&
714+
M.NumCols() == NumCols() && M.Stride() == Stride());
715+
return;
716+
}
711717
if (Trans == kNoTrans) {
712718
KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == M.NumCols());
713719
for (MatrixIndexT i = 0; i < num_rows_; i++)

‎src/nnet2/nnet-component.h‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ class NormalizeComponent: public NonlinearComponent {
496496
virtual std::string Type() const { return "NormalizeComponent"; }
497497
virtual Component* Copy() const { return new NormalizeComponent(*this); }
498498
virtual bool BackpropNeedsInput() const { return true; }
499-
virtual bool BackpropNeedsOutput() const { return true; }
499+
virtual bool BackpropNeedsOutput() const { return false; }
500500
using Component::Propagate; // to avoid name hiding
501501
virtual void Propagate(const ChunkInfo &in_info,
502502
const ChunkInfo &out_info,
@@ -747,7 +747,7 @@ class FixedAffineComponent;
747747

748748
// Affine means a linear function plus an offset.
749749
// Note: although this class can be instantiated, it also
750-
// function as a base-class for more specialized versions of
750+
// functions as a base-class for more specialized versions of
751751
// AffineComponent.
752752
class AffineComponent: public UpdatableComponent {
753753
friend class SoftmaxComponent; // Friend declaration relates to mixing up.
@@ -902,13 +902,6 @@ class AffineComponentPreconditioned: public AffineComponent {
902902
/// AffineComponentPreconditionedOnline is, like AffineComponentPreconditioned,
903903
/// a version of AffineComponent that has a non-(multiple of unit) learning-rate
904904
/// matrix. See nnet-precondition-online.h for a description of the technique.
905-
/// This method maintains an orthogonal matrix N with a small number of rows,
906-
/// actually two (for input and output dims) which gets modified each time;
907-
/// we maintain a mutex for access to this (we just use it to copy it when
908-
/// we need it and write to it when we change it). For multi-threaded use,
909-
/// the parallelization method is to lock a mutex whenever we want to
910-
/// read N or change it, but just quickly make a copy and release the mutex;
911-
/// this is to ensure operations on N are atomic.
912905
class AffineComponentPreconditionedOnline: public AffineComponent {
913906
public:
914907
virtual std::string Type() const {

‎src/nnet3/Makefile‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@ LDFLAGS += $(CUDA_LDFLAGS)
88
LDLIBS += $(CUDA_LDLIBS)
99

1010

11-
TESTFILES = nnet-component-test nnet-precondition-test \
12-
nnet-precondition-online-test nnet-example-functions-test \
13-
nnet-nnet-test am-nnet-test online-nnet2-decodable-test \
14-
nnet-compute-test
15-
16-
OBJFILES = nnet-component.o nnet-nnet.o train-nnet.o train-nnet-ensemble.o nnet-update.o \
17-
nnet-compute.o am-nnet.o nnet-functions.o \
18-
nnet-precondition.o shrink-nnet.o combine-nnet.o combine-nnet-a.o \
19-
mixup-nnet.o nnet-update-parallel.o combine-nnet-fast.o \
20-
nnet-fix.o nnet-stats.o rescale-nnet.o nnet-limit-rank.o nnet-example.o \
21-
get-feature-transform.o widen-nnet.o nnet-precondition-online.o \
22-
nnet-example-functions.o nnet-compute-discriminative.o \
23-
nnet-compute-discriminative-parallel.o online-nnet2-decodable.o \
24-
train-nnet-perturbed.o nnet-compute-online.o
11+
TESTFILES = natural-gradient-online-test
12+
13+
OBJFILES = nnet-compile.o nnet-component.o nnet-parse.o natural-gradient-online.o \
14+
nnet-component-itf.o nnet-optimize.o
2515

2616
LIBNAME = kaldi-nnet3
2717

0 commit comments

Comments
 (0)