[buildout]
extends =
  ../../stack/slapos.cfg
  ../scipy/buildout.cfg
  ../zip/buildout.cfg
  ../bazel/buildout.cfg
  ../protobuf-python/buildout.cfg
  ../tensorboard/buildout.cfg
parts =
  slapos-cookbook-develop
  slapos-cookbook
  tensorflow-build-install-egg

[numpy-egg]
recipe = zc.recipe.egg
initialization =
  import scipy.spatial.ckdtree # load our own libstdc++ explicitly at the very beginning
eggs =
  setuptools
  ${scipy:egg}
  ${numpy:egg}
interpreter = numpy-python
scripts = numpy-python
location = ${buildout:parts-directory}/${:_buildout_section_name_}

[tensorflow-repository]
recipe = plone.recipe.command
stop-on-error = true
repository = https://github.com/tensorflow/tensorflow
tag = v1.4.0
git-binary = ${git:location}/bin/git
patch-binary = ${patch:location}/bin/patch
location = ${buildout:parts-directory}/${:_buildout_section_name_}
command = export HOME=${:location}; (${:git-binary} clone --recurse-submodules --quiet -b ${:tag} ${:repository} ${:location}; cd ${buildout:parts-directory} ; ${:patch-binary} -p1 -d ${:_buildout_section_name_} < ${:_profile_base_location_}/tensorflow-r1.4.patch ) || (rm -fr ${:location}; exit 1)

[cuda]
tf_need_cuda = 1
tf_cuda_version = 8.0
tf_cudnn_version = 5.1.10
tf_cuda_compute_capabilities = 5.2,6.1
cuda_toolkit_path = /usr/local/cuda
cudnn_install_path = /usr/local/cuda

[tensorflow-build]
recipe = slapos.recipe.build
location = ${buildout:parts-directory}/${:_buildout_section_name_}
workdir = ${tensorflow-repository:location}
gcc-bin = ${gcc:location}/bin
gcc-lib = ${gcc:location}/lib
gcc-lib64 = ${gcc:location}/lib64
numpy-python-command = ${buildout:bin-directory}/${numpy-egg:interpreter}
python27-lib = ${python2.7:location}/lib
java_home_bin = ${bazel:java_home}/bin
bazel_bin = ${bazel:location}/bin
need_cuda = ${cuda:cuda_toolkit_path}
script =
  os.makedirs(location)
  workdir = self.options['workdir']
  env = {'PATH':':'.join([self.options['gcc-bin'],
                          self.options['java_home_bin'],
                          self.options['bazel_bin'],
                          os.environ['PATH']]),
         'COMPILER_PATH':':'.join([self.options['gcc-bin'],
                                   os.environ.get('COMPILER_PATH') or '']),
         'LIBRARY_PATH':':'.join([self.options['gcc-lib'],
                                  self.options['gcc-lib64'],
                                  os.environ.get('LIBRARY_PATH') or '']),
         'PYTHON_BIN_PATH':self.options['numpy-python-command'],
         'PYTHON_LIB_PATH':self.options['python27-lib'],
         'CC_OPT_FLAGS':'-march=native',
         'TF_NEED_JEMALLOC':'1',
         'TF_ENABLE_XLA':'0',
         'TF_NEED_GCP':'0',
         'TF_NEED_HDFS':'0',
         'TF_NEED_OPENCL':'0',
         'TF_NEED_MKL':'0',
         'TF_NEED_VERBS':'0',
         'TF_CUDA_CLANG':'0',
         'TF_NEED_MPI':'0',
         'TF_NEED_CUDA':self.buildout['cuda']['tf_need_cuda'],
         ##### FOR CUDA #####
         'GCC_HOST_COMPILER_PATH':os.path.join(self.options['gcc-bin'], 'gcc'),
         'TF_CUDA_VERSION':self.buildout['cuda']['tf_cuda_version'],
         'CUDA_TOOLKIT_PATH':self.buildout['cuda']['cuda_toolkit_path'],
         'TF_CUDNN_VERSION':self.buildout['cuda']['tf_cudnn_version'],
         'CUDNN_INSTALL_PATH':self.buildout['cuda']['cudnn_install_path'],
         'TF_CUDA_COMPUTE_CAPABILITIES':self.buildout['cuda']['tf_cuda_compute_capabilities'],
         ####################
         }
  import os.path
  if not os.path.exists(env['CUDA_TOOLKIT_PATH']): env['TF_NEED_CUDA'] = '0'
  env['LD_LIBRARY_PATH'] = env['LIBRARY_PATH']
  env['LDFLAGS'] = '-lW,-rpath='+self.options['gcc-lib64']
  call(['./configure'], cwd=workdir, env=env)
  bazel_command = (env['TF_NEED_CUDA'] == '1' and
    ['bazel', 'build', '-c', 'opt', '--copt', '-march=native', '--config', 'cuda', '-s', '--verbose_failures', '//tensorflow/tools/pip_package:build_pip_package']
    or
    ['bazel', 'build', '-c', 'opt', '--copt', '-march=native', '-s', '--verbose_failures', '//tensorflow/tools/pip_package:build_pip_package'])
  call(bazel_command, cwd=workdir, env=env)

[tensorflow-build-install-egg]
recipe = slapos.recipe.build
unzip-binary = ${unzip:location}/bin/unzip
tensorflow-repository-path = ${tensorflow-repository:location}
location = ${buildout:parts-directory}/${:_buildout_section_name_}
need-tensorboard-build = ${tensorboard-build:location}
need-tensorflow-build = ${tensorflow-build:location}
need-protobuf-python = ${protobuf-python:egg}
egg = tensorflow
script =
  os.makedirs(location)
  workdir = self.options['tensorflow-repository-path']
  egg_name = 'tensorflow-1.4.0-py2.7-linux-x86_64.egg'
  dist_dir = os.path.join(workdir, 'dist')
  dest_dir = os.path.join(self.buildout['buildout']['eggs-directory'], egg_name)
  call(['bazel-bin/tensorflow/tools/pip_package/build_pip_package', dist_dir], cwd=workdir)
  call([self.options['unzip-binary'], '-o', os.path.join(dist_dir, egg_name), '-d', dest_dir])

[versions]
