Skip to content

Commit 6f61393

Browse files
authored
Merge pull request #4924 from jtrmal/python_and_apple
Fix reported issues w.r.t python2.7 and some apple silicone quirks
2 parents 67548a3 + 816d438 commit 6f61393

File tree

13 files changed

+166
-102
lines changed

13 files changed

+166
-102
lines changed

src/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ endif
6262

6363
# Don't call rm -rf.
6464
rmlibdir:
65+
ifeq ($(KALDI_FLAVOR), dynamic)
6566
ifneq ($(KALDILIBDIR), )
66-
-rm -f $(KALDILIBDIR)/*{.so,.a,.o}
67+
-rm -f $(KALDILIBDIR)/*{.so,.a,.o,.dylib}
6768
-rmdir 2>/dev/null $(KALDILIBDIR); true
6869
else
6970
# KALDILIBDIR might have been unset because of reconfigure. Do a best guess.
7071
@echo "Something seems wrong. Please re-run configure."
7172
@echo "I will continue but the cleanup might not be complete."
7273
endif
74+
endif
7375

7476
kaldi.mk:
7577
@echo "ERROR: kaldi.mk does not exist; run ./configure first.";

src/configure

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,8 @@ elif [ "`uname`" == "Darwin" ]; then
11501150
cat makefiles/darwin_clapack.mk >> kaldi.mk
11511151
echo "Warning (CLAPACK): this part of the configure process is not properly tested and may not work."
11521152
echo "Successfully configured for Darwin with CLAPACK libs from $CLAPACKROOT"
1153+
elif [ "$(uname -m)" == "arm64" ]; then
1154+
cat makefiles/darwin_arm64.mk >> kaldi.mk
11531155
else
11541156
cat makefiles/darwin.mk >> kaldi.mk
11551157
fi

src/cudamatrix/cu-array.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,12 @@ class CuArrayBase {
105105
protected:
106106
/// Default constructor: make it protected so the user cannot
107107
/// instantiate this class.
108-
CuArrayBase<T>(): data_(NULL), dim_(0) { }
108+
CuArrayBase(): data_(NULL), dim_(0) { }
109109

110110

111111
T *data_; ///< GPU data pointer (if GPU not available,
112112
///< will point to CPU memory).
113113
MatrixIndexT dim_; ///< dimension of the vector
114-
115114
};
116115

117116
/**
@@ -123,22 +122,21 @@ class CuArrayBase {
123122
template<typename T>
124123
class CuArray: public CuArrayBase<T> {
125124
public:
126-
127125
/// Default constructor, initialized data_ to NULL and dim_ to 0 via
128126
/// constructor of CuArrayBase.
129-
CuArray<T>() { }
127+
CuArray() { }
130128

131129
/// Constructor with memory initialisation. resize_type may be kSetZero or
132130
/// kUndefined.
133-
explicit CuArray<T>(MatrixIndexT dim, MatrixResizeType resize_type = kSetZero)
131+
explicit CuArray(MatrixIndexT dim, MatrixResizeType resize_type = kSetZero)
134132
{ Resize(dim, resize_type); }
135133

136134
/// Constructor from CPU-based int vector
137-
explicit CuArray<T>(const std::vector<T> &src) { CopyFromVec(src); }
135+
explicit CuArray(const std::vector<T> &src) { CopyFromVec(src); }
138136

139137
/// Copy constructor. We don't make this explicit because we want to be able
140138
/// to create a std::vector<CuArray>.
141-
CuArray<T>(const CuArray<T> &src) { CopyFromArray(src); }
139+
CuArray(const CuArray<T> &src) { CopyFromArray(src); }
142140

143141
/// Destructor
144142
~CuArray() { Destroy(); }
@@ -172,7 +170,6 @@ class CuArray: public CuArrayBase<T> {
172170
/// I/O
173171
void Read(std::istream &is, bool binary);
174172
void Write(std::ostream &is, bool binary) const;
175-
176173
};
177174

178175

@@ -182,7 +179,7 @@ class CuSubArray: public CuArrayBase<T> {
182179
/// Constructor as a range of an existing CuArray or CuSubArray. Note: like
183180
/// similar constructors in class CuVector and others, it can be used to evade
184181
/// 'const' constraints; don't do that.
185-
explicit CuSubArray<T>(const CuArrayBase<T> &src,
182+
explicit CuSubArray(const CuArrayBase<T> &src,
186183
MatrixIndexT offset, MatrixIndexT dim);
187184

188185
/// Construct from raw pointers

src/cudamatrix/cu-matrix.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class CuMatrixBase {
250250
template<typename OtherReal>
251251
void CopyFromTp(const CuTpMatrix<OtherReal> &M,
252252
MatrixTransposeType trans = kNoTrans);
253-
253+
254254
// This function will copy from source rows (start_range, end_range]
255255
// if the range is outside of the clamped region then the clamped
256256
// row will be replicated across the out of range areas
@@ -307,9 +307,9 @@ class CuMatrixBase {
307307
void PowAbs(const CuMatrixBase<Real> &src, Real power, bool include_sign=false);
308308

309309
void Floor(const CuMatrixBase<Real> &src, Real floor_val);
310-
310+
311311
void Ceiling(const CuMatrixBase<Real> &src, Real ceiling_val);
312-
312+
313313
/// This is equivalent to running:
314314
/// Floor(src, lower_limit);
315315
/// Ceiling(src, upper_limit);
@@ -320,7 +320,7 @@ class CuMatrixBase {
320320
/// (x < 0 ? exp(x) : x + 1). This function is used
321321
/// in our RNNLM training.
322322
void ExpSpecial(const CuMatrixBase<Real> &src);
323-
323+
324324
/// Softmax nonlinearity
325325
/// Y = Softmax(X) : Yij = e^Xij / sum_k(e^Xik), done to each row,
326326
/// with attention to avoiding overflow or underflow.
@@ -333,7 +333,7 @@ class CuMatrixBase {
333333
/// Supports in-place operation (i.e. this == &src).
334334
void LogSoftMaxPerRow(const CuMatrixBase<Real> &src);
335335

336-
336+
337337
/// Apply the function y = log(1 + exp(x)), to each element.
338338
/// Note: the derivative of this function is the sigmoid function.
339339
/// This is like a soft ReLU.
@@ -439,23 +439,23 @@ class CuMatrixBase {
439439
this -> Pow(*this, power);
440440
};
441441

442-
442+
443443
inline void ApplyPowAbs(Real power, bool include_sign=false) {
444444
this -> PowAbs(*this, power, include_sign);
445445
};
446-
446+
447447
inline void ApplyHeaviside() {
448448
this -> Heaviside(*this);
449449
};
450-
450+
451451
inline void ApplyFloor(Real floor_val) {
452452
this -> Floor(*this, floor_val);
453453
};
454-
454+
455455
inline void ApplyCeiling(Real ceiling_val) {
456456
this -> Ceiling(*this, ceiling_val);
457457
};
458-
458+
459459
inline void ApplyExp() {
460460
this -> Exp(*this);
461461
};
@@ -924,7 +924,7 @@ class CuSubMatrix: public CuMatrixBase<Real> {
924924

925925
/// This type of constructor is needed for Range() to work [in CuMatrix base
926926
/// class]. Cannot make it explicit or that breaks.
927-
inline CuSubMatrix<Real> (const CuSubMatrix &other):
927+
inline CuSubMatrix(const CuSubMatrix &other):
928928
CuMatrixBase<Real> (other.data_, other.num_rows_, other.num_cols_,
929929
other.stride_) {}
930930
private:

src/cudamatrix/cu-tp-matrix.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ class CuTpMatrix : public CuPackedMatrix<Real> {
4848
CuTpMatrix() : CuPackedMatrix<Real>() {}
4949
explicit CuTpMatrix(MatrixIndexT r, MatrixResizeType resize_type = kSetZero)
5050
: CuPackedMatrix<Real>(r, resize_type) {}
51-
52-
explicit CuTpMatrix<Real>(const TpMatrix<Real> &orig)
51+
52+
explicit CuTpMatrix(const TpMatrix<Real> &orig)
5353
: CuPackedMatrix<Real>(orig) {}
5454
// This constructor lacks the "explicit" keyword so that
5555
// we can include this class in std::vector.
56-
CuTpMatrix<Real>(const CuTpMatrix<Real> &orig)
56+
CuTpMatrix(const CuTpMatrix<Real> &orig)
5757
: CuPackedMatrix<Real>(orig) {}
58-
59-
explicit CuTpMatrix<Real>(const CuMatrixBase<Real> &orig,
58+
59+
explicit CuTpMatrix(const CuMatrixBase<Real> &orig,
6060
MatrixTransposeType trans = kNoTrans);
6161

62-
62+
6363
~CuTpMatrix() {}
6464

6565
void CopyFromMat(const CuMatrixBase<Real> &M,
@@ -70,12 +70,12 @@ class CuTpMatrix : public CuPackedMatrix<Real> {
7070
}
7171
void CopyFromTp(const TpMatrix<Real> &other) {
7272
CuPackedMatrix<Real>::CopyFromPacked(other);
73-
}
73+
}
7474
void Cholesky(const CuSpMatrix<Real>& Orig);
7575
void Invert();
7676

7777
CuTpMatrix<Real> &operator = (const CuTpMatrix<Real> &in);
78-
78+
7979
protected:
8080
inline const TpMatrix<Real> &Mat() const {
8181
return *(reinterpret_cast<const TpMatrix<Real>* >(this));

src/cudamatrix/cu-vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class CuVectorBase {
243243

244244
/// Default constructor: make it protected so the user cannot
245245
/// instantiate this class.
246-
CuVectorBase<Real>(): data_(NULL), dim_(0) { }
246+
CuVectorBase(): data_(NULL), dim_(0) { }
247247

248248
Real *data_; ///< GPU data pointer (or regular data pointer
249249
///< if CUDA is not compiled in or we have no GPU).

src/makefiles/darwin_arm64.mk

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Darwin (macOS) configuration
2+
3+
ifndef DOUBLE_PRECISION
4+
$(error DOUBLE_PRECISION not defined.)
5+
endif
6+
ifndef OPENFSTINC
7+
$(error OPENFSTINC not defined.)
8+
endif
9+
ifndef OPENFSTLIBS
10+
$(error OPENFSTLIBS not defined.)
11+
endif
12+
13+
CXXFLAGS = -std=c++14 -I.. -I$(OPENFSTINC) -O1 $(EXTRA_CXXFLAGS) \
14+
-Wall -Wno-sign-compare -Wno-unused-local-typedefs \
15+
-Wno-deprecated-declarations -Winit-self \
16+
-DKALDI_DOUBLEPRECISION=$(DOUBLE_PRECISION) \
17+
-DHAVE_EXECINFO_H=1 -DHAVE_CXXABI_H -DHAVE_CLAPACK \
18+
-pthread \
19+
-g # -O0 -DKALDI_PARANOID
20+
21+
ifeq ($(KALDI_FLAVOR), dynamic)
22+
CXXFLAGS += -fPIC
23+
endif
24+
25+
# Compiler specific flags
26+
COMPILER = $(shell $(CXX) -v 2>&1)
27+
ifeq ($(findstring clang,$(COMPILER)),clang)
28+
# Suppress annoying clang warnings that are perfectly valid per spec.
29+
CXXFLAGS += -Wno-mismatched-tags
30+
else ifeq ($(findstring GCC,$(COMPILER)),GCC)
31+
# Allow implicit conversions between vectors.
32+
CXXFLAGS += -flax-vector-conversions
33+
endif
34+
35+
LDFLAGS = $(EXTRA_LDFLAGS) $(OPENFSTLDFLAGS) -g
36+
LDLIBS = $(EXTRA_LDLIBS) $(OPENFSTLIBS) -framework Accelerate -lm -lpthread -ldl

src/matrix/kaldi-matrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,11 @@ class SubMatrix : public MatrixBase<Real> {
10061006
MatrixIndexT num_cols,
10071007
MatrixIndexT stride);
10081008

1009-
~SubMatrix<Real>() {}
1009+
~SubMatrix() {}
10101010

10111011
/// This type of constructor is needed for Range() to work [in Matrix base
10121012
/// class]. Cannot make it explicit.
1013-
SubMatrix<Real> (const SubMatrix &other):
1013+
SubMatrix(const SubMatrix &other):
10141014
MatrixBase<Real> (other.data_, other.num_cols_, other.num_rows_,
10151015
other.stride_) {}
10161016

src/matrix/qr.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void House(MatrixIndexT dim, const Real *x, Real *v, Real *beta) {
5757
if (max_x == 0.0) max_x = 1.0;
5858
s = 1.0 / max_x;
5959
}
60-
60+
6161
Real sigma = 0.0;
6262
v[0] = 1.0;
6363
for (MatrixIndexT i = 1; i < dim; i++) {
@@ -73,7 +73,7 @@ void House(MatrixIndexT dim, const Real *x, Real *v, Real *beta) {
7373
v[0] = x1 - mu;
7474
} else {
7575
v[0] = -sigma / (x1 + mu);
76-
KALDI_ASSERT(KALDI_ISFINITE(v[dim-1]));
76+
KALDI_ASSERT(KALDI_ISFINITE(v[dim-1]));
7777
}
7878
Real v1 = v[0];
7979
Real v1sq = v1 * v1;
@@ -155,11 +155,11 @@ void HouseBackward(MatrixIndexT dim, const Real *x, Real *v, Real *beta) {
155155
with packed lower-triangular matrices to do it this way. There's also
156156
a shift from one-based to zero-based indexing, so the index
157157
k is transformed k -> n - k, and a corresponding transpose...
158-
158+
159159
Let the original *this be A. This algorithms replaces *this with
160160
a tridiagonal matrix T such that T = Q A Q^T for an orthogonal Q.
161161
Caution: Q is transposed vs. Golub and Van Loan.
162-
If Q != NULL it outputs Q.
162+
If Q != NULL it outputs Q.
163163
*/
164164
template<typename Real>
165165
void SpMatrix<Real>::Tridiagonalize(MatrixBase<Real> *Q) {
@@ -195,7 +195,7 @@ void SpMatrix<Real>::Tridiagonalize(MatrixBase<Real> *Q) {
195195
if (Q != NULL) { // C.f. Golub, Q is H_1 .. H_n-2... in this
196196
// case we apply them in the opposite order so it's H_n-1 .. H_1,
197197
// but also Q is transposed so we really have Q = H_1 .. H_n-1.
198-
// It's a double negative.
198+
// It's a double negative.
199199
// Anyway, we left-multiply Q by each one. The H_n would each be
200200
// diag(I + beta v v', I) but we don't ever touch the last dims.
201201
// We do (in Matlab notation):
@@ -309,7 +309,7 @@ void QrStep(MatrixIndexT n,
309309
if (k < n-2) {
310310
// Next is the elements (k+2, k) and (k+2, k-1), to be rotated, again
311311
// backwards.
312-
Real &elem_kp2_k = z,
312+
Real &elem_kp2_k = z,
313313
&elem_kp2_kp1 = off_diag[k+1];
314314
// Note: elem_kp2_k == z would start off as zero because it's
315315
// two off the diagonal, and not been touched yet. Therefore
@@ -338,7 +338,7 @@ void QrInternal(MatrixIndexT n,
338338
MatrixIndexT counter = 0, max_iters = 500 + 4*n, // Should never take this many iters.
339339
large_iters = 100 + 2*n;
340340
Real epsilon = (pow(2.0, sizeof(Real) == 4 ? -23.0 : -52.0));
341-
341+
342342
for (; counter < max_iters; counter++) { // this takes the place of "until
343343
// q=n"... we'll break out of the
344344
// loop when we converge.
@@ -356,7 +356,7 @@ void QrInternal(MatrixIndexT n,
356356
off_diag[i] = 0.0;
357357
}
358358
// The next code works out p, q, and npq which is n - p - q.
359-
// For the definitions of q and p, see Golub and Van Loan; we
359+
// For the definitions of q and p, see Golub and Van Loan; we
360360
// partition the n dims into pieces of size (p, n-p-q, q) where
361361
// the part of size q is diagonal and the part of size n-p-p is
362362
// "unreduced", i.e. has no zero off-diagonal elements.
@@ -392,7 +392,7 @@ void QrInternal(MatrixIndexT n,
392392
} else {
393393
QrStep(npq, diag + p, off_diag + p,
394394
static_cast<MatrixBase<Real>*>(NULL));
395-
}
395+
}
396396
}
397397
if (counter == max_iters) {
398398
KALDI_WARN << "Failure to converge in QR algorithm. "
@@ -490,7 +490,7 @@ void SpMatrix<Real>::TopEigs(VectorBase<Real> *s, MatrixBase<Real> *P,
490490
r.AddSpVec(1.0, S, Q.Row(d), 0.0);
491491
// r = S * q_d
492492
MatrixIndexT counter = 0;
493-
Real end_prod;
493+
Real end_prod = 0;
494494
while (1) { // Normally we'll do this loop only once:
495495
// we repeat to handle cases where r gets very much smaller
496496
// and we want to orthogonalize again.
@@ -528,11 +528,11 @@ void SpMatrix<Real>::TopEigs(VectorBase<Real> *s, MatrixBase<Real> *P,
528528
}
529529
}
530530

531-
Matrix<Real> R(lanczos_dim, lanczos_dim);
531+
Matrix<Real> R(lanczos_dim, lanczos_dim);
532532
R.SetUnit();
533533
T.Qr(&R); // Diagonalizes T.
534534
Vector<Real> s_tmp(lanczos_dim);
535-
s_tmp.CopyDiagFromSp(T);
535+
s_tmp.CopyDiagFromSp(T);
536536

537537
// Now T = R * diag(s_tmp) * R^T.
538538
// The next call sorts the elements of s from greatest to least absolute value,
@@ -544,7 +544,7 @@ void SpMatrix<Real>::TopEigs(VectorBase<Real> *s, MatrixBase<Real> *P,
544544
SubMatrix<Real> Rsub(R, 0, eig_dim, 0, lanczos_dim);
545545
SubVector<Real> s_sub(s_tmp, 0, eig_dim);
546546
s->CopyFromVec(s_sub);
547-
547+
548548
// For working out what to do now, just assume the other eigenvalues were
549549
// zero. This is just for purposes of knowing how to get the result, and
550550
// not getting things wrongly transposed.

0 commit comments

Comments
 (0)