-
Notifications
You must be signed in to change notification settings - Fork 26
Using Tensorflow as a back end
As of 2017-09-12, Nimble has experimental support for Tensorflow as a back-end.
Tensorflow is compute framework for medium sized tensor operations. Tensorflow is usually used for deep neural networks, but Nimble uses it only to accelerate tensor computations. In this sense, Nimble uses Tensorflow roughly as a GPU-compatible replacement for Eigen.
To use the Tensorflow backend for Nimble, you'll need to install RStudio's R wrapper for Tensorflow, and then use the R wrapper to install tensorflow
install.packages('tensorflow') # Installs RStudio's R wrapper library.
library('tensorflow')
tensorflow::install_tensorflow() # Installs Google's Tensorflow library.The above lines install the latest stable CPU-only version of Tensorflow in a python virtualenv named r-tensorflow. You can replace this version with a GPU-compatible version by manually installing the Python package:
$ workon r-tensorflow # You may need to install virtualenv before doing this.
$ pip install --ignore-installed tensorflow-gpuTo run on GPUs, you'll also need to install various CUDA libraries on your system. See the Installing Tensorflow docs for details.
For best performance, you may want to compile Tensorflow from source. This arduous process is well documemnted; we recommend performing it only if you're waiting on a very slow computation.
Nimble can use Tensorflow to accelerate nimbleFunctions.
Each nimbleFunction can currently use either Tensorflow or Eigen for vectorizable math, and this decision is made on a per-function basis.
To enable Tensorflow, set the experimentalUseTensorflow option when compiling that function:
nimbleOptions(experimentalUseTensorflow = TRUE)
tf_fun <- compileNimble(fun)It is generally safer to set this global variable temporarily using withNimbleOptions:
tf_fun <- withNimbleOptions(list(experimentalUseTensorflow = TRUE),
compileNimble(fun))- Only very simple functions can be compiled using tensorflow.
- Tensorflow graphs are currently created for each statement (line of code).
- Tensorflow is slower on small arrays due copy overhead.
- Tensorflow is only faster on very large arrays (like 1000 x 1000).
- To see benefit from CPUs, you'll probably need to compile tensorflow on your machine.
- To get GPU support you'll need to install a version of tensorflow with gpu support.
- Tensorflow does not support GPUs on OS X (but does parallelize over cores).
- Nimble can currently only use tensorflow on Linux:
- OSX support is waiting on an upstream fix;
- Windows support has not been tested and certainly will need fixes.
- Despite testing, some functions may give incorrect results.
For known failures, search
tests/mathTestLists.Rfor 'tensorflow'. - The current implementation supports only double precision floating point arithmetic.