forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 14
WIP: First src/hmm wraps + module + Darwin compilation #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jtrmal
wants to merge
1
commit into
danpovey:pybind11
Choose a base branch
from
jtrmal:pybind11
base: pybind11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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")); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_ | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).