-
Notifications
You must be signed in to change notification settings - Fork 5.4k
support load kaldi model in python #3976
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
Merged
+574
−0
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a40d73
support load kaldi model in python
fanlu 422d99e
add some component
fanlu c7e9f4e
split one file to multi component wrap files
fanlu 6af774b
fix some bugs and add test mdl
fanlu 3987407
add testmode func in batchnorm pybind
fanlu 946c81f
change StatsSum StatsSumsq to Mean Var
fanlu 11429f2
make const
fanlu 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
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
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
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,40 @@ | ||
| // pybind/nnet3/nnet_component_itf_pybind.cc | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // 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 "nnet3/nnet_component_itf_pybind.h" | ||
|
|
||
| #include "nnet3/nnet-component-itf.h" | ||
|
|
||
| using namespace kaldi::nnet3; | ||
|
|
||
| void pybind_nnet_component_itf(py::module& m) { | ||
| using PyClass = Component; | ||
| py::class_<PyClass>(m, "Component", | ||
| "Abstract base-class for neural-net components.") | ||
| .def("Type", &PyClass::Type, | ||
| "Returns a string such as \"SigmoidComponent\", describing the " | ||
| "type of the object.") | ||
| .def("Info", &PyClass::Info, | ||
| "Returns some text-form information about this component, for " | ||
| "diagnostics. Starts with the type of the component. E.g. " | ||
| "\"SigmoidComponent dim=900\", although most components will have " | ||
| "much more info.") | ||
| .def_static("NewComponentOfType", &PyClass::NewComponentOfType, | ||
| py::return_value_policy::take_ownership); | ||
| } |
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,25 @@ | ||
| // pybind/nnet3/nnet_component_itf_pybind.h | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // See ../../../COPYING for clarification regarding multiple authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // | ||
| // 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_NNET3_NNET_COMPONENT_ITF_PYBIND_H_ | ||
| #define KALDI_PYBIND_NNET3_NNET_COMPONENT_ITF_PYBIND_H_ | ||
|
|
||
| #include "pybind/kaldi_pybind.h" | ||
|
|
||
| void pybind_nnet_component_itf(py::module& m); | ||
|
|
||
| #endif // KALDI_PYBIND_NNET3_NNET_COMPONENT_ITF_PYBIND_H_ |
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,32 @@ | ||
| // pybind/nnet3/nnet_convolutional_component_pybind.cc | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // 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 "nnet3/nnet_convolutional_component_pybind.h" | ||
|
|
||
| #include "nnet3/nnet-convolutional-component.h" | ||
|
|
||
| using namespace kaldi::nnet3; | ||
|
|
||
| void pybind_nnet_convolutional_component(py::module& m) { | ||
| using TC = kaldi::nnet3::TdnnComponent; | ||
| py::class_<TC>(m, "TdnnComponent") | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .def("Type", &TC::Type) | ||
| .def("LinearParams", &TC::LinearParams, py::return_value_policy::reference) | ||
| .def("BiasParams", &TC::BiasParams, py::return_value_policy::reference); | ||
| } | ||
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,25 @@ | ||
| // pybind/nnet3/nnet_convolutional_component_pybind.h | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // See ../../../COPYING for clarification regarding multiple authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // | ||
| // 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_NNET3_NNET_CONVOLUTIONAL_COMPONENT_PYBIND_H_ | ||
| #define KALDI_PYBIND_NNET3_NNET_CONVOLUTIONAL_COMPONENT_PYBIND_H_ | ||
|
|
||
| #include "pybind/kaldi_pybind.h" | ||
|
|
||
| void pybind_nnet_convolutional_component(py::module& m); | ||
|
|
||
| #endif // KALDI_PYBIND_NNET3_NNET_CONVOLUTIONAL_COMPONENT_PYBIND_H_ |
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,52 @@ | ||
| // pybind/nnet3/nnet_nnet_pybind.cc | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // 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 "nnet3/nnet_nnet_pybind.h" | ||
|
|
||
| #include "nnet3/nnet-nnet.h" | ||
|
|
||
| using namespace kaldi; | ||
| using namespace kaldi::nnet3; | ||
|
|
||
| void pybind_nnet_nnet(py::module& m) { | ||
| using PyClass = kaldi::nnet3::Nnet; | ||
| auto nnet = py::class_<PyClass>( | ||
| m, "Nnet", | ||
| "This function can be used either to initialize a new Nnet from a " | ||
| "config file, or to add to an existing Nnet, possibly replacing " | ||
| "certain parts of it. It will die with error if something went wrong. " | ||
| "Also see the function ReadEditConfig() in nnet-utils.h (it's made a " | ||
| "non-member because it doesn't need special access)."); | ||
| nnet.def(py::init<>()) | ||
| .def("Read", &PyClass::Read, py::arg("is"), py::arg("binary")) | ||
| .def("GetComponentNames", &PyClass::GetComponentNames, | ||
| "returns vector of component names (needed by some parsing code, " | ||
| "for instance).", | ||
| py::return_value_policy::reference) | ||
| .def("GetComponentName", &PyClass::GetComponentName, | ||
| py::arg("component_index")) | ||
| .def("Info", &PyClass::Info, | ||
| "returns some human-readable information about the network, " | ||
| "mostly for debugging purposes. Also see function NnetInfo() in " | ||
| "nnet-utils.h, which prints out more extensive infoformation.") | ||
| .def("NumComponents", &PyClass::NumComponents) | ||
| .def("NumNodes", &PyClass::NumNodes) | ||
| .def("GetComponent", (Component * (PyClass::*)(int32)) & PyClass::GetComponent, | ||
| py::arg("c"), py::return_value_policy::reference); | ||
| } |
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,25 @@ | ||
| // pybind/nnet3/nnet_nnet_pybind.h | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // See ../../../COPYING for clarification regarding multiple authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // | ||
| // 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_NNET3_NNET_NNET_PYBIND_H_ | ||
| #define KALDI_PYBIND_NNET3_NNET_NNET_PYBIND_H_ | ||
|
|
||
| #include "pybind/kaldi_pybind.h" | ||
|
|
||
| void pybind_nnet_nnet(py::module& m); | ||
|
|
||
| #endif // KALDI_PYBIND_NNET3_NNET_NNET_PYBIND_H_ |
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,40 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| # Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
| # Apache 2.0 | ||
|
|
||
| import os | ||
| import sys | ||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir)) | ||
|
|
||
| import unittest | ||
|
|
||
| import kaldi | ||
| from kaldi import read_nnet3_model | ||
| from torch.utils.dlpack import from_dlpack | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from torch.utils.dlpack import to_dlpack | ||
|
|
||
| class TestNnetNnet(unittest.TestCase): | ||
|
|
||
| def test_nnet_nnet(self): | ||
| kaldi.SelectGpuId('yes') | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final_mdl = "/mnt/cfs1_alias1/asr/users/fanlu/task/kaldi_recipe/pybind/s10.1/exp/chain_cleaned_1c/tdnn1c_sp/final.mdl" | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| nnet = kaldi.read_nnet3_model(final_mdl) | ||
| for i in range(nnet.NumComponents()): | ||
| component = nnet.GetComponent(i) | ||
| comp_type = component.Type() | ||
| if "Affine" in comp_type or "TdnnComponent" in comp_type: | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| linear_params = from_dlpack(component.LinearParams().to_dlpack()) | ||
| bias_params = from_dlpack(component.BiasParams().to_dlpack()) | ||
| print(linear_params.shape) | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| elif "Batch" in comp_type: | ||
| # stats_sum = from_dlpack(component.StatsSum().to_dlpack()) | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # stats_sumsq = from_dlpack(component.StatsSumsq().to_dlpack()) | ||
| # print(stats_sum.shape) | ||
| pass | ||
| elif "LinearComponent" == comp_type: | ||
| linear_params = from_dlpack(component.LinearParams().to_dlpack()) | ||
| print(linear_params.shape) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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,35 @@ | ||
| // pybind/nnet3/nnet_normalize_component_pybind.cc | ||
|
|
||
| // Copyright 2020 JD AI, Beijing, China (author: Lu Fan) | ||
|
|
||
| // 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 "nnet3/nnet_normalize_component_pybind.h" | ||
|
|
||
| #include "nnet3/nnet-normalize-component.h" | ||
|
|
||
| using namespace kaldi::nnet3; | ||
|
|
||
| void pybind_nnet_normalize_component(py::module& m) { | ||
| using PyClass = kaldi::nnet3::BatchNormComponent; | ||
| py::class_<PyClass>(m, "BatchNormComponent") | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .def("Type", &PyClass::Type) | ||
| .def("StatsSum", &PyClass::StatsSum) | ||
| .def("StatsSumsq", &PyClass::StatsSumsq) | ||
| .def("Count", &PyClass::Count) | ||
| .def("Offset", &PyClass::Offset) | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .def("Scale", overload_cast_<>()(&PyClass::Scale, py::const_)); | ||
fanlu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
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.
Please use
Replace
virtualwithoverride.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.
Why do you make it a
virtualmethod?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.
there is no method of name
StatsSumin base calss, Should I use keyword override?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.
No.
Then please remove
virtual.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.
@csukuangfj
I have a problem here
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.
Do not use double for any matrix types in Kaldi for pybind.
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.
but stats_sum_'s and stats_sumsq_ 's type are CuVector
and offset_ scale_ also give me this bug
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.
You do not need
stats_sum_andstats_sumsq_in inference mode.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.
Have you invoked
SetTestMode(true)beforeprint(from_dlpack(component.Offset().to_dlpack()))?Please re-check your code.
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.
the StatsMean and StatsVar in kaldi's model are equivalent to running_mean, running_var in pytorch, respectively.
but in kaldi's Read and Write function, there are some other operator, like
these operator are not necessary for pytorch's model, just use mean and var rather than offset and scale.