From 44c9414a22b309ecc199ce30eb8cfb0a98ee9d2a Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Wed, 11 Oct 2017 14:37:32 +0900
Subject: [PATCH] component/pytorch: Add pytorch.

---
 component/pytorch/buildout.cfg           | 119 +++++++++++++++++++++++
 component/pytorch/setup.py.4af66c4.patch |  13 +++
 2 files changed, 132 insertions(+)
 create mode 100644 component/pytorch/buildout.cfg
 create mode 100644 component/pytorch/setup.py.4af66c4.patch

diff --git a/component/pytorch/buildout.cfg b/component/pytorch/buildout.cfg
new file mode 100644
index 000000000..f661049b7
--- /dev/null
+++ b/component/pytorch/buildout.cfg
@@ -0,0 +1,119 @@
+[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
+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'],
+                          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
diff --git a/component/pytorch/setup.py.4af66c4.patch b/component/pytorch/setup.py.4af66c4.patch
new file mode 100644
index 000000000..041ef0b79
--- /dev/null
+++ b/component/pytorch/setup.py.4af66c4.patch
@@ -0,0 +1,13 @@
+diff --git a/setup.py b/setup.py
+index 1d9a765..a50e9cb 100644
+--- a/setup.py
++++ b/setup.py
+@@ -90,7 +90,7 @@ def build_libs(libs):
+         assert lib in dep_libs, 'invalid lib: {}'.format(lib)
+     build_libs_cmd = ['bash', 'torch/lib/build_libs.sh']
+     my_env = os.environ.copy()
+-    my_env["PYTORCH_PYTHON"] = sys.executable
++    #my_env["PYTORCH_PYTHON"] = sys.executable
+     if WITH_SYSTEM_NCCL:
+         my_env["NCCL_ROOT_DIR"] = NCCL_ROOT_DIR
+     if WITH_CUDA:
-- 
2.30.9