[buildout]
extends =
  ../../stack/slapos.cfg
  ../gcc/buildout.cfg
  ../openblas/buildout.cfg
  ../cmake/buildout.cfg
  ../python-cffi/buildout.cfg
  ../python-PyYAML/buildout.cfg
  ../python-cocoapi/buildout.cfg
  ../pillow/buildout.cfg
  ../scipy/buildout.cfg
  ../matplotlib/buildout.cfg
  ../unzip/buildout.cfg
parts =
  pytorch-egg

[pytorch-repository]
recipe = plone.recipe.command
stop-on-error = true
repository = https://github.com/pytorch/pytorch
tag = master
commit = 4af66c43045a317b477918c503d105f565b4a66b
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 --recursive --quiet -b ${:tag} ${:repository} ${:location}; cd ${:location}; ${:git-binary} checkout ${:commit}; ${:patch-binary} -p1 -d . < ${:_profile_base_location_}/setup.py.4af66c4.patch ) || (rm -fr ${:location}; exit 1)

[pytorch-build-interpreter]
recipe = zc.recipe.egg
initialization =
  import scipy.spatial.ckdtree # load our own libstdc++ explicitly at the very beginning
  import sys
  sys.path.append('.')
  sys.path.append('${pytorch-repository:location}/torch/lib/ATen')
eggs =
  setuptools
  ${scipy:egg}
  ${numpy:egg}
  ${python-cffi:egg}
  ${python-PyYAML:egg}
interpreter = pytorch-build-interpreter
scripts = pytorch-build-interpreter
location = ${buildout:parts-directory}/${:_buildout_section_name_}

[pytorch-build]
recipe = slapos.recipe.build
location = ${buildout:parts-directory}/${:_buildout_section_name_}
workdir = ${pytorch-repository:location}
python-bin = ${buildout:bin-directory}/${pytorch-build-interpreter:interpreter}
no-cuda = 0
cmake-bin = ${cmake:location}/bin
git-bin = ${git:location}/bin
binutils-location = ${binutils:location}
gcc-location = ${gcc:location}
openblas-location = ${openblas:location}
script =
  os.makedirs(location)
  workdir = self.options['workdir']
  python_bin = self.options['python-bin']
  binutils_location = self.options['binutils-location']
  gcc_location = self.options['gcc-location']
  openblas_location = self.options['openblas-location']
  env = {'PYTHONPATH':workdir,
         'PATH':':'.join([self.options['cmake-bin'],
                          self.options['git-bin'],
                          binutils_location+'/bin',
                          gcc_location+'/bin',
                          os.environ['PATH']]),
         'CMAKE_INCLUDE_PATH':':'.join([gcc_location+'/include',
                                        binutils_location+'/include',
                                        openblas_location+'/include',
                                        ]),
         'CMAKE_LIBRARY_PATH':':'.join([gcc_location+'/lib',
                                        gcc_location+'/lib64',
                                        binutils_location+'/lib',
                                        openblas_location+'/lib',
                                        ]),
         'CC':gcc_location+'/bin/gcc',
         'CXX':gcc_location+'/bin/g++',
         'PYTORCH_PYTHON':python_bin,
         }
  if self.options.get('no-cuda') == '1':
    env['NO_CUDA'] = '1'
  import os.path
  call([python_bin, 'setup.py', 'build'], cwd=workdir, env=env)
  call([python_bin, 'setup.py', 'bdist_egg'], cwd=workdir, env=env)

[pytorch-build-install-egg]
recipe = slapos.recipe.build
unzip-binary = ${unzip:location}/bin/unzip
pytorch-repository-path = ${pytorch-repository:location}
location = ${buildout:parts-directory}/${:_buildout_section_name_}
need-pytorch-build = ${pytorch-build:location}
egg = torch
script =
  os.makedirs(location)
  workdir = self.options['pytorch-repository-path']
  egg_name = 'torch-0.2.0+4af66c4-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([self.options['unzip-binary'], '-o', os.path.join(dist_dir, egg_name), '-d', dest_dir])

[pytorch-egg]
recipe = zc.recipe.egg
initialization =
  import scipy.spatial.ckdtree # load our own libstdc++ explicitly at the very beginning
eggs = 
  ${scipy:egg}
  ${numpy:egg}
  ${python-PyYAML:egg}
  ${pytorch-build-install-egg:egg}
  ${pillow-python:egg}
  ${python-cocoapi-build-install-egg:egg}
  ${matplotlib:egg}
  six
  torchvision
interpreter = pytorch-python
scripts = pytorch-python

[versions]
torchvision = 0.1.6