Skip to content

Commit f4ed97e

Browse files
DanEnergeticsDaniel Manncurufinwe
authored
Add python bindings (#69)
Co-authored-by: Daniel Mann <[email protected]> Co-authored-by: Eugen Beck <[email protected]>
1 parent a942e39 commit f4ed97e

File tree

21 files changed

+381
-23
lines changed

21 files changed

+381
-23
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*.pyc
2323
*.o
2424
*.so
25+
/RASR.egg-info
2526

2627
# For exec-files, the postfix is ".$(OS)-$(PROC)-$(COMPILE)[-$(SUFFIX)]".
2728
*.*-*-*
@@ -53,6 +54,9 @@
5354
.history*
5455
*.autosave
5556

57+
# Singularity/Apptainer images
58+
*.sif
59+
5660
# Broken NFS.
5761
.nfs*
5862

Modules.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ TOOLS += CorpusStatistics
123123
TOOLS += FeatureExtraction
124124
TOOLS += FeatureStatistics
125125
TOOLS += Fsa
126+
TOOLS += LibRASR
126127
TOOLS += Lm
127128
TOOLS += SpeechRecognizer
128129
TOOLS += Xml

apptainer/2023-11-08_tensorflow-2.14_v1/image.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Stage: build
99
DEBIAN_FRONTEND=noninteractive apt install -y wget git unzip gzip libssl-dev lsb-release zsh \
1010
bison libxml2-dev libopenblas-dev libsndfile1-dev libcrypto++-dev libcppunit-dev \
1111
parallel xmlstarlet python3-lxml htop strace gdb sox python3-pip cmake ffmpeg vim
12+
13+
pip3 install pybind11==2.11.1
1214

1315
# download the cache manager and place in /usr/local
1416
cd /usr/local

apptainer/2023-11-08_tensorflow-2.14_v1/makefiles/Modules.make

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ MODULES += MODULE_TEST
110110
# Tensorflow integration
111111
MODULES += MODULE_TENSORFLOW
112112

113+
# ONNX integration
114+
# MODULES += MODULE_ONNX
115+
113116
# define variables for the makefiles
114117
$(foreach module, $(MODULES), $(eval $(module) = 1))
115118

@@ -120,6 +123,8 @@ TOOLS += CorpusStatistics
120123
TOOLS += FeatureExtraction
121124
TOOLS += FeatureStatistics
122125
TOOLS += Fsa
126+
TOOLS += LibRASR
127+
TOOLS += Lm
123128
TOOLS += SpeechRecognizer
124129
TOOLS += Xml
125130

apptainer/2023-11-08_tensorflow-2.14_v1/makefiles/config/cc-gcc.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CXX_MINOR = $(shell $(CXX) --version | head -n 1 | sed -e 's/.*[ \t]\([0-9]\)\.\
2222
# -----------------------------------------------------------------------------
2323
# compiler options
2424
DEFINES += -D_GNU_SOURCE
25+
CCFLAGS += -fPIC
2526
CCFLAGS += -pipe
2627
CCFLAGS += -funsigned-char
2728
CCFLAGS += -fno-exceptions

apptainer/2023-11-08_tensorflow-2.14_v1/makefiles/config/os-linux.make

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ LDFLAGS += `${PYTHON_PATH}/bin/python3.11-config --ldflags --embed 2>/dev/nu
144144
LDFLAGS += -Wl,-rpath -Wl,${PYTHON_PATH}/lib
145145
else
146146
INCLUDES += `python3.11-config --includes 2>/dev/null`
147+
INCLUDES += -I$(shell python3.11 -c 'import numpy as np; print(np.get_include())')
148+
INCLUDES += `python3.11 -m pybind11 --includes 2>/dev/null`
147149
LDFLAGS += `python3.11-config --ldflags --embed 2>/dev/null`
148150
# IF you want to use Python2 for whatever reason:
149151
# INCLUDES += `pkg-config --cflags python`

config/cc-gcc.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CXX_MINOR = $(shell $(CXX) --version | head -n 1 | sed -e 's/.*[ \t]\([0-9]\)\.\
2222
# -----------------------------------------------------------------------------
2323
# compiler options
2424
DEFINES += -D_GNU_SOURCE
25+
CCFLAGS += -fPIC
2526
CCFLAGS += -pipe
2627
CCFLAGS += -funsigned-char
2728
CCFLAGS += -fno-exceptions

config/os-linux.make

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ LDFLAGS += `${PYTHON_PATH}/bin/python3-config --ldflags 2>/dev/null`
152152
LDFLAGS += -Wl,-rpath -Wl,${PYTHON_PATH}/lib
153153
else
154154
INCLUDES += `python3-config --includes 2>/dev/null`
155-
LDFLAGS += `python3-config --ldflags 2>/dev/null`
155+
INCLUDES += -I$(shell python3 -c 'import numpy as np; print(np.get_include())')
156+
INCLUDES += `python3 -m pybind11 --includes 2>/dev/null`
157+
LDFLAGS += `python3-config --ldflags --embed 2>/dev/null`
156158
# IF you want to use Python2 for whatever reason:
157159
# INCLUDES += `pkg-config --cflags python`
158160
# LDFLAGS += `pkg-config --libs python`

setup.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Setup.py inspired by https://thomastrapp.com/posts/building-a-pypi-package-for-a-modern-cpp-project/"""
2+
import os
3+
from setuptools import setup, dist
4+
5+
PACKAGE_NAME = "librasr" # keyword to import
6+
LIBRASR_SUBDIR = os.path.join("src", "Tools", "LibRASR")
7+
LIBRASR_SO = "librasr.so"
8+
9+
assert os.path.exists(os.path.join(LIBRASR_SUBDIR, LIBRASR_SO)), \
10+
"Could not find librasr shared library. Did you compile it correctly?"
11+
class BinaryDistribution(dist.Distribution):
12+
"""Helper class: We assume that librasr.so has already been compiled."""
13+
def has_ext_modules(self) -> bool:
14+
return True
15+
16+
# get README content for long description
17+
this_directory = os.path.abspath(os.path.dirname(__file__))
18+
with open(os.path.join(this_directory, 'README')) as f:
19+
long_description = f.read()
20+
21+
setup(
22+
name='RASR',
23+
24+
# include shared library and link to lower case package name
25+
packages=[PACKAGE_NAME],
26+
package_dir={PACKAGE_NAME: LIBRASR_SUBDIR},
27+
package_data={PACKAGE_NAME: [LIBRASR_SO]},
28+
include_package_data=True,
29+
30+
description="RASR as a python module.",
31+
long_description=long_description,
32+
long_description_content_type="text/markdown",
33+
34+
# use custom distribution class to export librasr.so
35+
distclass=BinaryDistribution,
36+
37+
version='0.0.1',
38+
url='https://github.com/rwth-i6/rasr',
39+
author_email='[email protected]',
40+
)

src/Core/Configuration.cc

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void Configuration::Resource::writeUsage(XmlWriter& os) const {
169169
* Central storage place for all resources.
170170
*/
171171

172-
class Configuration::ResourceDataBase {
172+
class Configuration::ResourceDataBase : public ReferenceCounted {
173173
private:
174174
std::set<Resource> resources;
175175
Resource noResource_;
@@ -401,24 +401,19 @@ bool Configuration::isWellFormedParameterName(const std::string& s) {
401401
}
402402

403403
Configuration::Configuration()
404-
: db_(0),
405-
isDataBaseOwner_(true),
404+
: db_(new ResourceDataBase),
406405
selection_("UNNAMED"),
407406
name_("UNNAMED") {
408-
// define new root configuration
409-
db_ = new ResourceDataBase;
410407
}
411408

412409
Configuration::Configuration(const Configuration& c)
413410
: db_(c.db_),
414-
isDataBaseOwner_(false),
415411
selection_(c.selection_),
416412
name_(c.name_) {
417413
}
418414

419415
Configuration::Configuration(const Configuration& c, const std::string& add_selection)
420-
: db_(c.db_),
421-
isDataBaseOwner_(false) {
416+
: db_(c.db_) {
422417
require(add_selection.length());
423418
require(add_selection.find(resource_separation_char) == std::string::npos);
424419
require(add_selection.find(resource_wildcard_char) == std::string::npos);
@@ -428,16 +423,9 @@ Configuration::Configuration(const Configuration& c, const std::string& add_sele
428423
ensure(isWellFormedParameterName(selection_));
429424
}
430425

431-
Configuration::~Configuration() {
432-
if (isDataBaseOwner_)
433-
delete db_;
434-
}
426+
Configuration::~Configuration() {}
435427

436428
Configuration& Configuration::operator=(const Configuration& c) {
437-
if (isDataBaseOwner_) {
438-
delete db_;
439-
isDataBaseOwner_ = false;
440-
}
441429
db_ = c.db_;
442430
selection_ = c.selection_;
443431
name_ = c.name_;

0 commit comments

Comments
 (0)