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

Commit

Permalink
PyTorch Integration with Tensor Comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
prigoyal committed Feb 26, 2018
1 parent c9b372f commit e908c24
Show file tree
Hide file tree
Showing 91 changed files with 5,372 additions and 148 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -13,5 +13,6 @@ conda
*/.nfs*
tensor_comprehensions.egg-info/
tensor_comprehensions/version.py
tensor_comprehensions/*.proto
slurm-*
examples/results*
examples/results*
9 changes: 4 additions & 5 deletions CMakeLists.txt
Expand Up @@ -174,8 +174,6 @@ endif()

################################################################################
# ATen

# ATen - if someone ships libATen.so, we try to use that if available
# first find python path
execute_process(COMMAND which python OUTPUT_VARIABLE __python OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "PYTHON output: \n${__python}")
Expand All @@ -184,14 +182,15 @@ message(STATUS "PYTHON output: \n${__python}")
execute_process(COMMAND "${__python}" "-c" "import torch;" RESULT_VARIABLE __torch_install OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "IMPORTING TORCH: \n${__torch_install}")

# also get the site-packages path where conda installs things
# also get the site-packages path where conda installs pytorch
execute_process(COMMAND "python" "-c"
"from distutils.sysconfig import get_python_lib; print(get_python_lib())"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "PYTHON site packages: \n${PYTHON_SITE_PACKAGES}")

# if PyTorch is installed, we get libATen.so.1 from there, otherwise build it
if (__torch_install EQUAL 0)
message(STATUS "TORCH INSTALLED, linking to ATen")
message(STATUS "TORCH INSTALLED, linking to ATen from PyTorch")
set(ATEN_INCLUDE_DIR "${PYTHON_SITE_PACKAGES}/torch/lib/include")
include_directories(${ATEN_INCLUDE_DIR})
find_library(ATEN_LIBRARIES NAMES libATen.so.1 PATHS ${PYTHON_SITE_PACKAGES}/torch/lib)
Expand Down Expand Up @@ -238,7 +237,7 @@ message(STATUS "Found glog: ${GLOG_LIBRARIES}")

add_subdirectory(src)
enable_testing()
add_subdirectory(pybinds)
add_subdirectory(tensor_comprehensions/pybinds)
add_subdirectory(test)

if (WITH_CAFFE2)
Expand Down
67 changes: 41 additions & 26 deletions CodeOwners.md
@@ -1,16 +1,52 @@
This file lists the "owners" for each part of Tensor Comprehensions.
These people are major contributors of a specific part, they ensure issues in that part get addressed and pull requests get reviewed.
If you want to contribute to Tensor Comprehensions, make sure to include these people in your review requests.
These people are major contributors of a specific part, they ensure issues in
that part get addressed and pull requests get reviewed. If you want to contribute
to Tensor Comprehensions, make sure to include these people in your review requests.

### Build system and Docker
### Documentation
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
* **Albert Cohen** [@albertcohen](https://github.com/albertcohen)
```
docs/*
*.md
```

### Build system
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
* **Nicolas Vasilache** [@nicolasvasilache](https://github.com/nicolasvasilache)
```
docker/*
.circleci/*
*/CMakeLists.txt
```

### Tensor Comprehensions language and frontend
### Docker
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
```
docker/*
```

### Conda Packaging
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
```
conda_recipes/*
```

### Python bindings
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
```
tensor_comprehensions/pybinds/*
test_python/*
```

### Framework integration (PyTorch, Caffe2, etc.)
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
```
tensor_comprehensions/*
src/c2/*
src/aten/*
```

### Tensor Comprehensions language and frontend
* **Zachary DeVito** [@zdevito](https://github.com/zdevito)
```
src/lang/*
Expand All @@ -37,27 +73,6 @@ src/core/polyhedral/*
src/autotuner/*
```

### Framework integration (ATen, Caffe2, etc.)
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
```
src/c2/*
src/aten/*
```

### Documentation
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
* **Albert Cohen** [@albertcohen](https://github.com/albertcohen)
```
docs/*
*.md
```

### Python bindings
* **Priya Goyal** [@prigoyal](https://github.com/prigoyal)
```
pybind/*
```

### Protocol buffers
* **Oleksandr Zinenko** [@ftynse](https://github.com/ftynse)
* **Theodoros Theodoridis** [@ttheodor](https://github.com/ttheodor)
Expand Down
33 changes: 29 additions & 4 deletions build.sh
Expand Up @@ -245,9 +245,9 @@ function install_caffe2() {
rm -rf * || exit 1

if ! test ${USE_CONTBUILD_CACHE}; then
${CMAKE_VERSION} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DPYTHON_EXECUTABLE=${PYTHON} -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_PYTHON=${WITH_PYTHON_C2} -DUSE_GLOG=OFF -DUSE_GFLAGS=OFF -DUSE_NNPACK=${WITH_NNPACK} -DUSE_GLOO=OFF -DUSE_NCCL=OFF -DUSE_LMDB=OFF -DUSE_LEVELDB=OFF -DBUILD_TEST=OFF -DUSE_OPENCV=OFF -DUSE_OPENMP=OFF -DCMAKE_INSTALL_MESSAGE=NEVER -DCMAKE_CXX_FLAGS="-fno-var-tracking-assignments" -DPROTOBUF_PROTOC_EXECUTABLE=${PROTOC} -DCUDNN_ROOT_DIR=${CUDNN_ROOT_DIR} -DCUB_INCLUDE_DIR=${CUB_INCLUDE_DIR} -DCMAKE_C_COMPILER=${CC} -DCMAKE_CXX_COMPILER=${CXX} .. || exit
${CMAKE_VERSION} -DBUILD_BINARY=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_PYTHON=${WITH_PYTHON_C2} -DUSE_GLOG=OFF -DUSE_GFLAGS=OFF -DUSE_NNPACK=${WITH_NNPACK} -DGLOG_ROOT_DIR=${INSTALL_PREFIX} -DUSE_GLOO=OFF -DUSE_NCCL=OFF -DUSE_LMDB=OFF -DUSE_LEVELDB=OFF -DBUILD_TEST=OFF -DUSE_OPENCV=OFF -DUSE_OPENMP=OFF -DCMAKE_INSTALL_MESSAGE=NEVER -DCMAKE_CXX_FLAGS="-fno-var-tracking-assignments" -DPROTOBUF_PROTOC_EXECUTABLE=${PROTOC} -DCUDNN_ROOT_DIR=${CUDNN_ROOT_DIR} -DCUB_INCLUDE_DIR=${CUB_INCLUDE_DIR} -DCMAKE_C_COMPILER=${CC} -DCMAKE_CXX_COMPILER=${CXX} .. || exit
else
${CMAKE_VERSION} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DPYTHON_EXECUTABLE=${PYTHON} -DCUDA_ARCH_NAME="Maxwell" -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_PYTHON=${WITH_PYTHON_C2} -DUSE_GLOG=OFF -DUSE_GLOO=OFF -DUSE_NNPACK=${WITH_NNPACK} -DUSE_NCCL=OFF -DUSE_GFLAGS=OFF -DUSE_LMDB=OFF -DUSE_LEVELDB=OFF -DBUILD_TEST=OFF -DUSE_OPENCV=OFF -DUSE_OPENMP=OFF -DCMAKE_INSTALL_MESSAGE=NEVER -DCMAKE_CXX_FLAGS="-fno-var-tracking-assignments" -DPROTOBUF_PROTOC_EXECUTABLE=${PROTOC} -DCMAKE_C_COMPILER=${CC} -DCMAKE_CXX_COMPILER=${CXX} .. || exit
${CMAKE_VERSION} -DBUILD_BINARY=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCUDA_ARCH_NAME="Maxwell" -DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_PYTHON=${WITH_PYTHON_C2} -DUSE_GLOG=OFF -DUSE_GFLAGS=OFF -DGLOG_ROOT_DIR=${INSTALL_PREFIX} -DUSE_GLOO=OFF -DUSE_NNPACK=${WITH_NNPACK} -DUSE_NCCL=OFF -DUSE_LMDB=OFF -DUSE_LEVELDB=OFF -DBUILD_TEST=OFF -DUSE_OPENCV=OFF -DUSE_OPENMP=OFF -DCMAKE_INSTALL_MESSAGE=NEVER -DCMAKE_CXX_FLAGS="-fno-var-tracking-assignments" -DPROTOBUF_PROTOC_EXECUTABLE=${PROTOC} -DCUB_INCLUDE_DIR=${CUB_INCLUDE_DIR} -DCMAKE_C_COMPILER=${CC} -DCMAKE_CXX_COMPILER=${CXX} .. || exit
fi
fi
VERBOSE=${VERBOSE} make -j $CORES install -s || exit 1
Expand Down Expand Up @@ -320,8 +320,33 @@ function install_cub() {

function install_tc_python() {
echo "Setting up python now"
export PYTHONPATH=${TC_DIR}/build/pybinds:${PYTHONPATH}
echo "PYTHONPATH: ${PYTHONPATH}"
echo "USE_CONTBUILD_CACHE: ${USE_CONTBUILD_CACHE}"

if [ "$USE_CONTBUILD_CACHE" == "1" ]; then
echo "Running on CI, setting PYTHONPATH only"
export PYTHONPATH=${TC_DIR}/build/tensor_comprehensions/pybinds:${PYTHONPATH}
echo "PYTHONPATH: ${PYTHONPATH}"
else
if which conda &> /dev/null; then
echo "Found conda, going to install Python packages"
cd ${TC_DIR}
export CONDA_PYTHON=$(which python3)
echo "CONDA_PYTHON: ${CONDA_PYTHON}"
if [ "$BUILD_TYPE" == "Release" ]; then
echo "Install mode setup for python"
${CONDA_PYTHON} setup.py install
else
echo "Develop mode setup for python"
${CONDA_PYTHON} setup.py develop
fi
else
echo "Conda not found, setting PYTHONPATH instead"
echo "Setting PYTHONPATH now"
export PYTHONPATH=${TC_DIR}/tensor_comprehensions:$PYTHONPATH
echo "PYTHONPATH: ${PYTHONPATH}"
fi
fi
echo "python all set now"
}

function install_tc() {
Expand Down
68 changes: 68 additions & 0 deletions conda_recipes/Dockerfile
@@ -0,0 +1,68 @@
FROM nvidia/cuda:8.0-cudnn6-devel-ubuntu14.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update

RUN apt-get install -y --no-install-recommends make git ssh realpath wget unzip cmake3 vim
RUN apt-get install -y --no-install-recommends libgoogle-glog-dev libyaml-dev
RUN apt-get install -y --no-install-recommends libgtest-dev libz-dev libgmp3-dev
RUN apt-get install -y --no-install-recommends automake libtool valgrind subversion
RUN apt-get install -y --no-install-recommends ca-certificates software-properties-common

RUN cmake --version

# GCC 4.8.*
RUN add-apt-repository ppa:ubuntu-toolchain-r/test
RUN apt-get update
RUN apt-get install -y --no-install-recommends libcilkrts5 gcc-4.8 g++-4.8
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
RUN update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50

ENV CC /usr/bin/gcc
ENV CXX /usr/bin/g++

# LLVM+Clang
ENV LLVM_SOURCES /tmp/llvm_sources-tapir5.0
WORKDIR $LLVM_SOURCES

ENV CLANG_PREFIX /usr/local/clang+llvm-tapir5.0
ENV CMAKE_VERSION cmake

RUN git clone --recursive https://github.com/wsmoses/Tapir-LLVM llvm && \
mkdir -p ${LLVM_SOURCES}/llvm_build && cd ${LLVM_SOURCES}/llvm_build && \
${CMAKE_VERSION} -DLLVM_ENABLE_EH=ON -DLLVM_ENABLE_OCAMLDOC=OFF -DLLVM_INSTALL_OCAMLDOC_HTML_DIR=/tmp -DLLVM_OCAML_INSTALL_PATH=/tmp -DCMAKE_INSTALL_PREFIX=${CLANG_PREFIX} -DLLVM_TARGETS_TO_BUILD=X86 -DCOMPILER_RT_BUILD_CILKTOOLS=OFF -DLLVM_ENABLE_CXX1Y=ON -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_BUILD_TESTS=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_ENABLE_RTTI=ON ../llvm/ && \
make -j"$(nproc)" -s && make install -j"$(nproc)" -s

RUN rm -Rf ${LLVM_SOURCES}

# Anaconda3
ENV LD_LIBRARY_PATH /usr/local/cuda/lib64:/usr/local/cuda/targets/x86_64-linux/lib/stubs/:$LD_LIBRARY_PATH
ENV PATH /usr/local/bin:/usr/local/cuda/bin:$PATH

WORKDIR /conda-install
RUN echo 'export PATH=/opt/conda/bin:$PATH' > /etc/profile.d/conda.sh &&\
wget --quiet https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh -O anaconda.sh && \
chmod +x anaconda.sh && \
./anaconda.sh -b -p /opt/conda && \
rm anaconda.sh

ENV PATH /opt/conda/bin:$PATH

RUN conda install numpy decorator six future cmake pyyaml

# Protobuf 3.4*
WORKDIR /proto-install
RUN wget --quiet https://github.com/google/protobuf/archive/v3.4.0.zip -O proto.zip && unzip -qq proto.zip -d /

RUN cd /protobuf-3.4.0 && ./autogen.sh && ./configure && make -j 8
RUN cd /protobuf-3.4.0 && make install && ldconfig

RUN which conda
RUN conda --version
RUN which protoc
RUN protoc --version
RUN which python
RUN python --version

CMD ["bash"]
144 changes: 144 additions & 0 deletions conda_recipes/README.md
@@ -0,0 +1,144 @@
# How to build conda package for Tensor Comprehensions and its dependencies

## Building Conda Packages inside TC nvidia-docker image

We have a runtime nvidia docker image for TC to build conda packages and run all
tests including gpu ones. To build the packages, follow the below commands:

```Shell
# Build a new nvidia docker image
cd TensorComprehensions/conda_recipes
docker build -t trusty-gcc4.8-py3-conda-cuda:2 .
```

Now, we have the docker image built, we will run it by following command:

```Shell
nvidia-docker run --rm -i -t trusty-gcc4.8-py3-conda-cuda:2
```

Once you are inside the docker image, run `nvidia-smi` and verify that you have
gpus access. Now, you are good to go and build conda packages. For that run the
following command:

```Shell
cd TensorComprehensions/conda_recipes
./conda_build_tc.sh
```

Sit back, relax and let the packages build. If you see any errors in packages, fix them.

## Building conda packages outside docker image

Steps:
1. You need to have conda installed. Follow the instructions in [dockerfile](https://github.com/facebookresearch/TensorComprehensions/tree/master/docker/linux-trusty-gcc4.8-cuda8-cudnn6-py3-conda) for how to install conda

*NOTE*: Make sure that conda get download things from web and you might need some
proxy-setup. You can add following lines to your `~/.condarc` file

```
proxy_servers:
http: <http_proxy_to_use>
https: <https_proxy_to_use>
```

2. Next install conda build

```Shell
conda install conda-build
```

This installs a bunch of dependencies like patchelf, glob, pkginfo, conda-verify.
When prompted, say yes to install these dependencies.

3. While building conda packaged, we might have some third-party dependencies for which we want to install the conda package available on [conda-forge](https://github.com/conda-forge/feedstocks). In order to install packages from conda-forge, we need to add conda-forge to the conda channels by running the following command

We will add `soumith` channel since we will get pytorch from there. Channels are prioritized in the order they were added.

```Shell
conda config --add channels conda-forge
conda config --add channels pytorch
```

For example: this is required for the libgmp dependency of ISL and we can get
conda package of libgmp from conda-forge

4. Now create a Python 3.6 conda environment for packaging TC by following below
instructions:

```Shell
conda create --name tc_build python=3.6
source activate tc_build
```

`tc_build` is the name of environment. You may chose any name you want.

**NOTE**: If you want to exit the conda environment, you can run

```Shell
source deactivate
```

5. Now, we have conda setup, we are ready to build conda package for TC. For this,
since TC has its dependencies that are linked dynamically, we will build conda
packages for them separately. In short, we need to build packages for `clang+llvm-tapir5.0`, `gflags`, `glog`, `Halide`, TC version of `isl`, TC version of `protobuf`, and finally `Tensor Comprehensions`.

For building each package, we need to specify a `build version`, `build number` and
`git hash`. This information is used to build each package.

If you ever want to build new packages corresponding to new git hashes for example
when submodules are bumped, you will need to build conda package for submodule
with the new git hash. For that, all you need to do is change the build version,
number and git hash.

Now, we will go ahead and build the conda package of TC and all of its dependencies. For that, run the command below:

```Shell
cd $TC_DIR/conda_recipes
./conda_build_tc.sh
```

You will see that you have conda packages in `<anaconda_root>/conda-bld/linux-64`

**NOTE**: If some of the conda packaging fails, you can clean them up by following
command:

```Shell
conda build purge
```

Now, let's install the tensor Comprehensions package. By default, the package
will get installed to `<anaconda_root>/lib/python3.6/site-packages`

```Shell
conda install --use-local tensor_comprehensions
```

**NOTE**: `--use-local` means that we are going to install locally built packages.

6. In order to uninstall a conda package, you can run

```Shell
conda uninstall <package_name>
```

If you want to uninstall a python version of package like `tensor_comprehensions`,
run the following command **twice**

```Shell
pip uninstall tensor_comprehensions
```

# A few helpful things
1. When building a private repo, git clone might fail with authentication errors
even when you pass the https_proxy (especially for private repo). In that case,
generate an access token from github https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
and use that as your password when prompted for authentication.

2. Once the build finished, the packaging and un-packaging step takes long
and might seem like it's stuck but it's not.

3. Look at the description on top in `build.sh` script of conda-recipes for how
conda builds packages and tests the builds.

4. Last, [here](https://conda.io/docs/user-guide/tasks/index.html) is a very useful reference on conda packaging.

0 comments on commit e908c24

Please sign in to comment.