Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Old style "Option 2": Build nGraph bridge from source using TensorFlow source

Sayantan Sarkar edited this page Dec 7, 2018 · 1 revision

Recommended for developers only. If you don't need to do this, use pip install ngraph-tensorflow-bridge (see main README). Note this option is slowly being phased out and being replaced by "build_ngtf.py" style build

To run unit tests, or if you are planning to contribute, install the nGraph bridge using the TensorFlow source tree as follows:

Prepare the build environment

The installation prerequisites are the same as described in the TensorFlow [prepare environment] for linux.

  1. We use the standard build process which is a system called "bazel". These instructions were tested with [bazel version 0.16.0].

     wget https://github.com/bazelbuild/bazel/releases/download/0.16.0/bazel-0.16.0-installer-linux-x86_64.sh      
     chmod +x bazel-0.16.0-installer-linux-x86_64.sh
     ./bazel-0.16.0-installer-linux-x86_64.sh --user
    
  2. Add and source the bin path to your ~/.bashrc file in order to be able to call bazel from the user's installation we set up:

     export PATH=$PATH:~/bin
     source ~/.bashrc   
    

Installation

  1. Once TensorFlow's dependencies are installed, clone the source of the [tensorflow] repo to your machine.

    ⚠️ You need the following version of TensorFlow: v1.12.0-rc2

     git clone https://github.com/tensorflow/tensorflow.git
     cd tensorflow
     git checkout v1.12.0-rc2
     git status
     HEAD detached at v1.12.0-rc2
    
  2. You must instantiate a specific kind of virtualenv to be able to proceed with the ngraph-tf bridge installation. For systems with Python 3.n or Python 2.7, these commands are

     virtualenv --system-site-packages -p python3 your_virtualenv 
     virtualenv --system-site-packages -p /usr/bin/python2 your_virtualenv  
     source your_virtualenv/bin/activate # bash, sh, ksh, or zsh
    

    Note: Depending on specific version of the Python and components already installed on your system - the list of dependent Python components vary. You may need to install one or more of the following Python packages: numpy mock keras keras_applications protobuf keras_preprocessing.

  3. Now run ./configure and choose no for the following when prompted to build TensorFlow.

    XLA support:

     Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
     No XLA JIT support will be enabled for TensorFlow.
    

    CUDA support:

     Do you wish to build TensorFlow with CUDA support? [y/N]: N
     No CUDA support will be enabled for TensorFlow.
    

    Note that if you are running TensorFlow on a Skylake family processor then select -march=broadwell when prompted to specify the optimization flags:

     Please specify optimization flags to use during compilation 
     when bazel option "--config=opt" is specified 
     [Default is -march=native]: -march=broadwell
    

    This is due to an issue in TensorFlow tracked in this issue: https://github.com/tensorflow/tensorflow/issues/17273

  4. Prepare the pip package and the TensorFlow C++ library:

     bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
     bazel-bin/tensorflow/tools/pip_package/build_pip_package ./
    
  5. Install the pip package, replacing the tensorflow-1.* with your version of TensorFlow:

     pip install -U ./tensorflow-1.*whl
    
  6. Now clone the ngraph-tf repo one level above -- in the parent directory of the tensorflow repo cloned in step 1:

     cd ..
     git clone https://github.com/NervanaSystems/ngraph-tf.git
     cd ngraph-tf
     git checkout v0.7.0
    
  7. Next, build and install nGraph bridge. :warning: Run the ngraph-tf build from within the virtualenv.

     mkdir build
     cd build
     cmake -DUNIT_TEST_ENABLE=TRUE -DTF_SRC_DIR=<path to TensorFlow source directory> ..
     make -j <your_processor_cores>
     make install 
     pip install -U python/dist/<ngraph-0.7.0-py2.py3-none-linux_x86_64.whl>
    

This final step automatically downloads the necessary version of ngraph and the dependencies.

  1. Test the installation by running the following command:

     python -c "import tensorflow as tf; print('TensorFlow version: r',tf.__version__);import ngraph_config; print(ngraph_config.__version__)"
    

Once the build and installation steps are complete, you can start using TensorFlow with nGraph backends.

Please add the following line to enable nGraph: import ngraph_config

Note: The actual filename for the pip package may be different as it's version dependent. Please check the build/python/dist directory for the actual pip wheel.