|
36 | 36 | reqs = list(f.read().strip().split('\n')) |
37 | 37 |
|
38 | 38 |
|
| 39 | +bluefog_tensorflow_mpi_lib = Extension('bluefog.tensorflow.mpi_lib', []) |
39 | 40 | bluefog_torch_mpi_lib = Extension('bluefog.torch.mpi_lib', []) |
40 | 41 |
|
41 | 42 |
|
@@ -220,14 +221,65 @@ def get_common_options(build_ext): |
220 | 221 | LINK_FLAGS = link_flags + shlex.split(mpi_flags) |
221 | 222 | LIBRARY_DIRS = [] |
222 | 223 | LIBRARIES = [] |
| 224 | + EXTRA_OBJECTS = [] |
223 | 225 |
|
224 | 226 | return dict(MACROS=MACROS, |
225 | 227 | INCLUDES=INCLUDES, |
226 | 228 | SOURCES=SOURCES, |
227 | 229 | COMPILE_FLAGS=COMPILE_FLAGS, |
228 | 230 | LINK_FLAGS=LINK_FLAGS, |
229 | 231 | 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) |
231 | 283 |
|
232 | 284 |
|
233 | 285 | def dummy_import_torch(): |
@@ -332,6 +384,21 @@ def build_extensions(self): |
332 | 384 | if not os.environ.get('BLUEFOG_WITHOUT_PYTORCH'): |
333 | 385 | dummy_import_torch() |
334 | 386 |
|
| 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 | + |
335 | 402 | if not os.environ.get('BLUEFOG_WITHOUT_PYTORCH'): |
336 | 403 | try: |
337 | 404 | check_torch_version() |
@@ -366,7 +433,7 @@ def build_extensions(self): |
366 | 433 | "Programming Language :: Python :: 3.7", |
367 | 434 | "Programming Language :: Python :: Implementation :: CPython", |
368 | 435 | ], |
369 | | - ext_modules=[bluefog_torch_mpi_lib], |
| 436 | + ext_modules=[bluefog_torch_mpi_lib, bluefog_tensorflow_mpi_lib], |
370 | 437 | cmdclass={"build_ext": custom_build_ext}, |
371 | 438 | install_requires=reqs, |
372 | 439 | extras_require=EXTRAS, |
|
0 commit comments