Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/pybind/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ LIBNAME = kaldi_pybind

ADDLIBS = ../util/kaldi-util.a ../matrix/kaldi-matrix.a ../base/kaldi-base.a

ifeq ($(shell uname), Darwin)
EXTRA_LDLIBS += $(foreach dep,$(ADDLIBS), ../lib/lib$(notdir $(basename $(dep))).dylib)
else
EXTRA_LDLIBS += $(foreach dep,$(ADDLIBS), $(dir $(dep))lib$(notdir $(basename $(dep))).so)
endif

LIBFILE=$(LIBNAME)$(LIBFILE_EXTENSION)

Expand All @@ -53,12 +57,21 @@ LIBFILE=$(LIBNAME)$(LIBFILE_EXTENSION)
all: $(LIBFILE)

$(LIBFILE): $(ADDLIBS) $(CCFILES)
ifeq ($(shell uname), Darwin)
$(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) $(CCFILES) $(OBJFILES) $(LDLIBS)
install_name_tool -add_rpath ../lib $(LIBFILE)
else ifeq ($(shell uname), Linux)
# Building shared library from static (static was compiled with -fPIC)
$(CXX) $(CXXFLAGS) -shared -o $@ $(CCFILES) -Wl,--no-whole-archive -Wl,-rpath=$(CURDIR)/../lib $(LDFLAGS) $(LDLIBS) $(EXTRA_LDLIBS)
else # Platform not supported
$(error Dynamic libraries not supported on this platform. Run configure with --static flag.)
endif
python3 -c 'import kaldi_pybind' # this line is a test.

clean:
-rm -f *.so
-rm -rf __pycache__
-rm -rf *$(LIBFILE_EXTENSION).dSYM

test: all
python3 tests/test_kaldi_pybind.py
Expand Down
83 changes: 83 additions & 0 deletions src/pybind/hmm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

# make "all" the target.
all:


# Disable linking math libs because not needed here. Just for compilation speed.
# no, it's now needed for context-fst-test.
# MATHLIB = NONE

EXTRA_CXXFLAGS = -Wno-sign-compare

include ../../kaldi.mk


ifeq ($(KALDI_FLAVOR),static)
$(error You cannot build the pybind directory unless the build was dynamic; reconfigure with --shared option.)
endif

PYBIND_INCLUDES=$(shell python3 -m pybind11 --includes)

CXXFLAGS += $(PYBIND_INCLUDES)

PYBIND_EXTENSION=$(shell python3-config --extension-suffix)
LIBFILE=kaldi-pybind
LIBFILE_EXTENSION=$(PYBIND_EXTENSION)

# pybind11 is heavily templated and generates code that is bloated before optimization.
# -flto is link time optimization which apparently is important.
CXXFLAGS += -O3 -flto -I. -I../..
LDFLAGS += -flto
# TODO:

ifeq ($(shell uname),Darwin)
LDFLAGS += -undefined dynamic_lookup
endif

CCFILES = hmm_pybind.cc \
tree-accu_pybind.cc

LIBNAME = kaldi_hmm_pybind


ADDLIBS = ../../util/kaldi-util.a ../../matrix/kaldi-matrix.a ../../hmm/kaldi-hmm.a ../../base/kaldi-base.a

ifeq ($(shell uname), Darwin)
EXTRA_LDLIBS += $(foreach dep,$(ADDLIBS), ../../lib/lib$(notdir $(basename $(dep))).dylib)
else
EXTRA_LDLIBS += $(foreach dep,$(ADDLIBS), $(dir $(dep))lib$(notdir $(basename $(dep))).so)
endif

LIBFILE=$(LIBNAME)$(LIBFILE_EXTENSION)

.PHONY: all clean test

all: $(LIBFILE)

$(LIBFILE): $(ADDLIBS) $(CCFILES)
ifeq ($(shell uname), Darwin)
$(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) $(CCFILES) $(OBJFILES) $(LDLIBS)
install_name_tool -add_rpath ../../lib $(LIBFILE)
else ifeq ($(shell uname), Linux)
# Building shared library from static (static was compiled with -fPIC)
$(CXX) $(CXXFLAGS) -shared -o $@ $(CCFILES) -Wl,--no-whole-archive -Wl,-rpath=$(CURDIR)/../../lib $(LDFLAGS) $(LDLIBS) $(EXTRA_LDLIBS)
else # Platform not supported
$(error Dynamic libraries not supported on this platform. Run configure with --static flag.)
endif
python3 -c 'import kaldi_hmm_pybind' # this line is a test.

clean:
-rm -f *.so
-rm -rf __pycache__
-rm -rf *$(LIBFILE_EXTENSION).dSYM

test: all
python3 tests/test_kaldi_pybind.py
python3 tests/test_matrix.py

# valgrind-python.supp is from http://svn.python.org/projects/python/trunk/Misc/valgrind-python.supp
# since we do not compile Python from souce, we follow the comment in valgrind-python.supp
# to uncomment suppressions for PyObject_Free and PyObject_Realloc.
valgrind:
valgrind --tool=memcheck --suppressions=./valgrind-python.supp \
python3 -E -tt ./tests/test_matrix.py
31 changes: 31 additions & 0 deletions src/pybind/hmm/hmm_pybind.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// hmm_pybind.cc

// Copyright (c) 2019, Johns Hopkins University (Yenda Trmal<jtrmal@gmail.com>)

// 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 <string>
#include "kaldi_pybind.h"
#include "hmm/tree-accu_pybind.h"


PYBIND11_MODULE(kaldi_hmm_pybind, m) {
m.doc() =
"pybind11 binding of some things from kaldi's src/hmm directory."
"Source is in $(KALDI_ROOT)/src/pybind/hmm/hmm_pybind.cc";
pybind_hmm_tree_accu(m);
}

48 changes: 48 additions & 0 deletions src/pybind/hmm/tree-accu_pybind.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// pybind/tree-accu_pybind.cc

// Copyright (c) 2019, Johns Hopkins University (Yenda Trmal<jtrmal@gmail.com>)

// 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 "hmm/tree-accu_pybind.h"
#include "hmm/tree-accu.h"

using namespace kaldi;

void pybind_hmm_tree_accu(py::module& m) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Do you want me to merge this soon or wait?
BTW, it's not clear to me that we'd need this part of the code anytime soon.. I would start with tree, hmm, transition-model, posteriors, and functions that operate on them. Incidentally, in the kaldi10 branch I made some nice simplifications to the tree, HMM, transition-model code; but it never seems to be a good time to merge. (Breaks back compatibility).

py::class_<AccumulateTreeStatsOptions>(m, "AccumulateTreeStatsOptions")
.def(py::init<>())
.def_readwrite("var_floor", &AccumulateTreeStatsOptions::var_floor)
.def_readwrite("ci_phones_str", &AccumulateTreeStatsOptions::ci_phones_str)
.def_readwrite("phone_map_rxfilename", &AccumulateTreeStatsOptions::phone_map_rxfilename)
.def_readwrite("collapse_pdf_classes", &AccumulateTreeStatsOptions::collapse_pdf_classes)
.def_readwrite("context_width", &AccumulateTreeStatsOptions::context_width)
.def_readwrite("central_position", &AccumulateTreeStatsOptions::central_position)
;

py::class_<AccumulateTreeStatsInfo>(m, "AccumulateTreeStatsInfo")
.def(py::init<const AccumulateTreeStatsOptions &>())
.def_readwrite("var_floor", &AccumulateTreeStatsInfo::var_floor)
.def_readwrite("context_width", &AccumulateTreeStatsInfo::context_width)
.def_readwrite("central_position", &AccumulateTreeStatsInfo::central_position)
.def_readwrite("ci_phones", &AccumulateTreeStatsInfo::ci_phones)
.def_readwrite("phone_map", &AccumulateTreeStatsInfo::phone_map)
;

m.def("ReadPhoneMap", &ReadPhoneMap,
"Read a mapping from one phone set to another",
py::arg("phone_map_rxfilename"), py::arg("phone_map"));
}
26 changes: 26 additions & 0 deletions src/pybind/hmm/tree-accu_pybind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// tree-accu_pybind.h

// Copyright (c) 2019, Johns Hopkins University (Yenda Trmal<jtrmal@gmail.com>)

// 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.
#ifndef KALDI_PYBIND_HMM_TREE_ACCU_PYBIND_H_
#define KALDI_PYBIND_HMM_TREE_ACCU_PYBIND_H_

#include "kaldi_pybind.h"

void pybind_hmm_tree_accu(py::module& m);
#endif // KALDI_PYBIND_HMM_TREE_ACCU_PYBIND_H_