Skip to content

Commit 81f44e3

Browse files
committed
add build_tf_extension in setup.py
1 parent c3d1362 commit 81f44e3

5 files changed

Lines changed: 105 additions & 5 deletions

File tree

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ contextmanager-decorators=contextlib.contextmanager
236236
# List of members which are set dynamically and missed by pylint inference
237237
# system, and so shouldn't trigger E1101 when accessed. Python regular
238238
# expressions are accepted.
239-
generated-members=numpy.*,torch.*,mpi4py.MPI.*,bluefog.torch.mpi_lib.*
239+
generated-members=numpy.*,torch.*,mpi4py.MPI.*,bluefog.torch.mpi_lib.*,tensorflow.*
240240

241241
# Tells whether missing members accessed in mixin class should be ignored. A
242242
# mixin class is detected if its name ends with "mixin" (case insensitive).

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ clean_build:
1313
rm -R build
1414

1515
clean_so:
16-
rm ./bluefog/torch/mpi_lib.*.so
16+
rm ./bluefog/torch/mpi_lib.*.so; rm ./bluefog/tensorflow/mpi_lib.*.so
1717

1818
clean_all: clean_build clean_so

bluefog/tensorflow/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
raise NotImplementedError("Will be implemented in the future.")
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
import collections
6+
import os
7+
import torch
8+
from bluefog.common.util import check_extension
9+
10+
check_extension('bluefog.tensorflow', __file__, 'mpi_lib')
11+
12+
# pylint: disable = wrong-import-position
13+
from bluefog.tensorflow.mpi_ops import init, shutdown
14+
from bluefog.tensorflow.mpi_ops import size, local_size, rank, local_rank
15+
from bluefog.tensorflow.mpi_ops import load_topology, set_topology
16+
from bluefog.tensorflow.mpi_ops import mpi_threads_supported

bluefog/tensorflow/mpi_ops.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
from bluefog.common.basics_c import BlueFogBasics
6+
7+
_basics = BlueFogBasics(__file__, 'mpi_lib')
8+
9+
# import basic methods
10+
init = _basics.init
11+
shutdown = _basics.shutdown
12+
size = _basics.size
13+
local_size = _basics.local_size
14+
rank = _basics.rank
15+
local_rank = _basics.local_rank
16+
mpi_threads_supported = _basics.mpi_threads_supported
17+
load_topology = _basics.load_topology
18+
set_topology = _basics.set_topology

setup.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
reqs = list(f.read().strip().split('\n'))
3737

3838

39+
bluefog_tensorflow_mpi_lib = Extension('bluefog.tensorflow.mpi_lib', [])
3940
bluefog_torch_mpi_lib = Extension('bluefog.torch.mpi_lib', [])
4041

4142

@@ -220,14 +221,65 @@ def get_common_options(build_ext):
220221
LINK_FLAGS = link_flags + shlex.split(mpi_flags)
221222
LIBRARY_DIRS = []
222223
LIBRARIES = []
224+
EXTRA_OBJECTS = []
223225

224226
return dict(MACROS=MACROS,
225227
INCLUDES=INCLUDES,
226228
SOURCES=SOURCES,
227229
COMPILE_FLAGS=COMPILE_FLAGS,
228230
LINK_FLAGS=LINK_FLAGS,
229231
LIBRARY_DIRS=LIBRARY_DIRS,
230-
LIBRARIES=LIBRARIES)
232+
LIBRARIES=LIBRARIES,
233+
EXTRA_OBJECTS=EXTRA_OBJECTS)
234+
235+
236+
def check_tf_version():
237+
try:
238+
import tensorflow
239+
if LooseVersion(tensorflow.__version__) < LooseVersion('1.1.0'):
240+
raise DistutilsPlatformError(
241+
'Your TensorFlow version %s is outdated. '
242+
'Bluefog requires tensorflow>=1.1.0' % tensorflow.__version__)
243+
except ImportError:
244+
raise DistutilsPlatformError(
245+
'import tensorflow failed, is it installed?\n\n%s' % traceback.format_exc())
246+
247+
248+
def build_tf_extension(build_ext, global_options):
249+
# Backup the options, preventing other plugins access libs that
250+
# compiled with compiler of this plugin
251+
options = copy.deepcopy(global_options)
252+
import tensorflow as tf
253+
254+
tf_compile_flags = tf.sysconfig.get_compile_flags()
255+
tf_link_flags = tf.sysconfig.get_link_flags()
256+
have_cuda = tf.test.is_built_with_cuda()
257+
updated_macros = set_macro(
258+
options['MACROS'], 'HAVE_CUDA', str(int(have_cuda)))
259+
print(tf_compile_flags, tf_link_flags, have_cuda)
260+
261+
if have_cuda:
262+
cuda_include_dirs, cuda_lib_dirs = get_cuda_dirs(
263+
build_ext, options['COMPILE_FLAGS'])
264+
options['INCLUDES'] += cuda_include_dirs
265+
options['LIBRARY_DIRS'] += cuda_lib_dirs
266+
options['LIBRARIES'] += ['cudart']
267+
print('INFO: Try Tensorflow extension with CUDA.')
268+
269+
bluefog_tensorflow_mpi_lib.define_macros = updated_macros
270+
bluefog_tensorflow_mpi_lib.include_dirs = options['INCLUDES']
271+
bluefog_tensorflow_mpi_lib.sources = options['SOURCES'] + [
272+
"bluefog/tensorflow/adapter.cc"
273+
]
274+
bluefog_tensorflow_mpi_lib.extra_compile_args = (
275+
options['COMPILE_FLAGS'] + tf_compile_flags)
276+
bluefog_tensorflow_mpi_lib.extra_link_args = (
277+
options['LINK_FLAGS'] + tf_link_flags)
278+
bluefog_tensorflow_mpi_lib.library_dirs = options['LIBRARY_DIRS']
279+
bluefog_tensorflow_mpi_lib.libraries = options['LIBRARIES']
280+
bluefog_tensorflow_mpi_lib.extra_objects = options['EXTRA_OBJECTS']
281+
282+
build_ext.build_extension(bluefog_tensorflow_mpi_lib)
231283

232284

233285
def dummy_import_torch():
@@ -332,6 +384,21 @@ def build_extensions(self):
332384
if not os.environ.get('BLUEFOG_WITHOUT_PYTORCH'):
333385
dummy_import_torch()
334386

387+
if not os.environ.get('BLUEFOG_WITHOUT_TENSORFLOW'):
388+
try:
389+
check_tf_version()
390+
build_tf_extension(self, options)
391+
built_plugins.append(True)
392+
print('INFO: Tensorflow extension is built successfully.')
393+
except: # pylint: disable=bare-except
394+
if not os.environ.get('BLUEFOG_WITHOUT_TENSORFLOW'):
395+
print(
396+
'INFO: Unable to build TensorFlow plugin, will skip it.\n\n'
397+
'%s' % traceback.format_exc(), file=sys.stderr)
398+
built_plugins.append(False)
399+
else:
400+
raise
401+
335402
if not os.environ.get('BLUEFOG_WITHOUT_PYTORCH'):
336403
try:
337404
check_torch_version()
@@ -366,7 +433,7 @@ def build_extensions(self):
366433
"Programming Language :: Python :: 3.7",
367434
"Programming Language :: Python :: Implementation :: CPython",
368435
],
369-
ext_modules=[bluefog_torch_mpi_lib],
436+
ext_modules=[bluefog_torch_mpi_lib, bluefog_tensorflow_mpi_lib],
370437
cmdclass={"build_ext": custom_build_ext},
371438
install_requires=reqs,
372439
extras_require=EXTRAS,

0 commit comments

Comments
 (0)