Skip to content

Commit

Permalink
Fix for OSX (#49)
Browse files Browse the repository at this point in the history
* g++ fix for pybind11 OSX

* Make compiler flag platform dependent

* Try referring to Python executable direcly

* Make CPP BDT class module local in Python bindings

* Allow different threshold and score precison in cpp backend
  • Loading branch information
thesps authored Jul 31, 2023
1 parent 086c82e commit b51258a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 2 additions & 4 deletions conifer/backends/cpp/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
from shutil import copyfile
import copy
from conifer.utils import _ap_include, _json_include, copydocstring
from conifer.utils import _ap_include, _json_include, _gcc_opts, _py_executable, copydocstring
from conifer.model import ModelBase
from conifer.backends.common import MultiPrecisionConfig
import logging
Expand All @@ -19,8 +19,6 @@ def _extra_validate(self):
# TODO: proagate different precisions properly through backend
# for now enforce that all the precisions are equal
assert self.input_precision == self.threshold_precision, f'input & threshold precision must be equal, got: {self.input_precision} & {self.threshold_precision}'
assert self.threshold_precision == self.score_precision, f'threshold & score precision must be equal, got: {self.threshold_precision} & {self.score_precision}'


class CPPModel(ModelBase):
def __init__(self, ensembleDict, config, metadata=None):
Expand Down Expand Up @@ -89,7 +87,7 @@ def compile(self):
conifer_include = f'-I{filedir}/include/'

# Do the compile
cmd = f"g++ -O3 -shared -std=c++14 -fPIC $(python3 -m pybind11 --includes) {ap_include} {json_include} {conifer_include} bridge.cpp -o conifer_bridge_{self._stamp}.so"
cmd = f"g++ -O3 -shared -std=c++14 -fPIC $({_py_executable()} -m pybind11 --includes) {ap_include} {json_include} {conifer_include} {_gcc_opts()} bridge.cpp -o conifer_bridge_{self._stamp}.so"
logger.debug(f'Compiling with command {cmd}')
try:
ret_val = os.system(cmd)
Expand Down
4 changes: 2 additions & 2 deletions conifer/backends/xilinxhls/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings
import numpy as np
import copy
from conifer.utils import _ap_include, copydocstring
from conifer.utils import _ap_include, _gcc_opts, _py_executable, copydocstring
from conifer.backends.common import BottomUpDecisionTree, MultiPrecisionConfig, read_hls_report
from conifer.model import ModelBase
import datetime
Expand Down Expand Up @@ -452,7 +452,7 @@ def compile(self):
if ap_include is None:
os.chdir(curr_dir)
raise Exception("Couldn't find Xilinx ap_ headers. Source the Vivado/Vitis HLS toolchain, or set XILINX_AP_INCLUDE environment variable.")
cmd = f"g++ -O3 -shared -std=c++14 -fPIC $(python3 -m pybind11 --includes) {ap_include} bridge.cpp firmware/BDT.cpp firmware/{cfg.project_name}.cpp -o conifer_bridge_{self._stamp}.so"
cmd = f"g++ -O3 -shared -std=c++14 -fPIC $({_py_executable()} -m pybind11 --includes) {ap_include} {_gcc_opts()} bridge.cpp firmware/BDT.cpp firmware/{cfg.project_name}.cpp -o conifer_bridge_{self._stamp}.so"
logger.debug(f'Compiling with command {cmd}')
try:
ret_val = os.system(cmd)
Expand Down
2 changes: 1 addition & 1 deletion conifer/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .fixed_point import FixedPointConverter
from conifer.utils.misc import _ap_include, _json_include, copydocstring
from conifer.utils.misc import _ap_include, _json_include, _gcc_opts, _py_executable, copydocstring
16 changes: 16 additions & 0 deletions conifer/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import logging
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,6 +33,21 @@ def _json_include():
logger.warn('Could not find JSON headers. JSON_ROOT not defined')
return ret

def _gcc_opts():
'''
Get extra platform specific g++ compile options
'''
if sys.platform == 'darwin':
return '-undefined dynamic_lookup'
else:
return ''

def _py_executable():
'''
Get the python executable
'''
return sys.executable

def copydocstring(fromfunc, sep="\n"):
"""
Decorator: Copy the docstring of `fromfunc`
Expand Down

0 comments on commit b51258a

Please sign in to comment.