Skip to content

[Caffe2] Amd build and docs #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 9, 2018
Merged
16 changes: 16 additions & 0 deletions .jenkins/caffe2/amd/binary_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -ex

LOCAL_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_DIR=$(cd "$LOCAL_DIR"/../../../ && pwd)

cd "$ROOT_DIR"

echo "Running C++ tests.."

for file in $(find "${ROOT_DIR}/build_caffe2/bin" -executable -type f); do
if [[ "$file" =~ "test" ]]; then
$file
fi
done
71 changes: 71 additions & 0 deletions .jenkins/caffe2/amd/build_amd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

set -ex

# The INSTALL_PREFIX here must match up with test.sh
INSTALL_PREFIX="/usr/local/caffe2"
LOCAL_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_DIR=$(cd "$LOCAL_DIR"/../../.. && pwd)
CMAKE_ARGS=()

##############################################################################
# Explicitly set Python executable.
###############################################################################
# On Ubuntu 16.04 the default Python is still 2.7.
PYTHON="$(which python)"

###############################################################################
# Set cmake args
###############################################################################
CMAKE_ARGS+=("-DBUILD_BINARY=ON")
CMAKE_ARGS+=("-DBUILD_TEST=ON")
CMAKE_ARGS+=("-DINSTALL_TEST=ON")
CMAKE_ARGS+=("-DUSE_OBSERVERS=ON")
CMAKE_ARGS+=("-DUSE_ZSTD=ON")
CMAKE_ARGS+=("-DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}")

# TODO: This is patching the official FindHip to properly handly
# cmake generator expression. A PR is opened in the upstream repo here:
# https://github.com/ROCm-Developer-Tools/HIP/pull/516
# remove this hack once it's merged.
if [[ -f /opt/rocm/hip/cmake/FindHIP.cmake ]]; then
sudo sed -i 's/\ -I${dir}/\ $<$<BOOL:${dir}>:-I${dir}>/' /opt/rocm/hip/cmake/FindHIP.cmake
fi

export LANG=C.UTF-8
export LC_ALL=C.UTF-8
export HCC_AMDGPU_TARGET=gfx900

########## HIPIFY Caffe2 operators
${PYTHON} "${ROOT_DIR}/tools/amd_build/build_caffe2_amd.py"

MAX_JOBS=$(nproc)

###############################################################################
# Configure and make
###############################################################################
# Run cmake from ./build_caffe2 directory so it doesn't conflict with
# standard PyTorch build directory. Eventually these won't need to
# be separate.
rm -rf build_caffe2
mkdir build_caffe2
cd ./build_caffe2

# Configure
cmake "${ROOT_DIR}" ${CMAKE_ARGS[*]} "$@"

# Build
if [ "$(uname)" == "Linux" ]; then
make "-j${MAX_JOBS}" install
else
echo "Don't know how to build on $(uname)"
exit 1
fi

###############################################################################
# Install ONNX
###############################################################################

# Install ONNX into a local directory
pip install --user -b /tmp/pip_install_onnx "file://${ROOT_DIR}/third_party/onnx#egg=onnx"

87 changes: 87 additions & 0 deletions .jenkins/caffe2/amd/install_dependencies_amd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash

set -ex

apt-get update
apt-get install -y --no-install-recommends \
autoconf \
build-essential \
ca-certificates \
cmake \
curl \
git \
libgoogle-glog-dev \
libhiredis-dev \
libiomp-dev \
libleveldb-dev \
liblmdb-dev \
libopencv-dev \
libpthread-stubs0-dev \
libsnappy-dev \
libprotobuf-dev \
protobuf-compiler \
python-dev \
python-setuptools \
python-pip

pip install networkx==2.0

pip install --user \
click \
future \
hypothesis \
jupyter \
numpy \
protobuf \
pytest \
pyyaml \
scipy==0.19.1 \
scikit-image \
tabulate \
virtualenv \
mock \
typing \
typing-extensions \
pyyaml

CLANG_VERSION=3.8

apt-get update
apt-get install -y --no-install-recommends clang-"$CLANG_VERSION"
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Use update-alternatives to make this version the default
update-alternatives --install /usr/bin/gcc gcc /usr/bin/clang-"$CLANG_VERSION" 50
update-alternatives --install /usr/bin/g++ g++ /usr/bin/clang++-"$CLANG_VERSION" 50


apt-get install -y --no-install-recommends \
rocm-dev \
rocm-utils \
rocm-libs \
rocfft \
miopen-hip \
miopengemm \
rocblas \
rocrand


if [[ -f /opt/rocm/hip/cmake/FindHIP.cmake ]]; then
sudo sed -i 's/\ -I${dir}/\ $<$<BOOL:${dir}>:-I${dir}>/' /opt/rocm/hip/cmake/FindHIP.cmake
fi

if [[ -f /opt/rocm/hip/cmake/FindHIP.cmake ]]; then
sudo sed -i 's/set(_hip_build_configuration "${CMAKE_BUILD_TYPE}")/string(TOUPPER _hip_build_configuration "${CMAKE_BUILD_TYPE}")/' /opt/rocm/hip/cmake/FindHIP.cmake
fi

git clone --recursive https://github.com/ROCmSoftwarePlatform/Thrust.git /data/Thrust
rm -rf /data/Thrust/thrust/system/cuda/detail/cub-hip
git clone --recursive https://github.com/ROCmSoftwarePlatform/cub-hip.git /data/Thrust/thrust/system/cuda/detail/cub-hip


export PATH=/opt/rocm/bin:$PATH
export PATH=/opt/rocm/hcc/bin:$PATH
export PATH=/opt/rocm/hip/bin:$PATH
export PATH=/opt/rocm/opencl/bin:$PATH
export THRUST_ROOT=/data/Thrust
export HIP_PLATFORM=hcc
40 changes: 40 additions & 0 deletions .jenkins/caffe2/amd/python_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

set -ex

LOCAL_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_DIR=$(cd "$LOCAL_DIR"/../../../ && pwd)

cd "$ROOT_DIR"

# Get the relative path to where the caffe2 python module was installed
CAFFE2_PYPATH="$ROOT_DIR/build_caffe2/caffe2"

rocm_ignore_test=()
# need to debug
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/operator_test/arg_ops_test.py")
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/operator_test/piecewise_linear_transform_test.py")
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/operator_test/softmax_ops_test.py")
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/operator_test/unique_ops_test.py")
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/model_device_test.py")
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/data_parallel_model_test.py")

# Need to go through roi ops to replace max(...) with fmaxf(...)
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/operator_test/roi_align_rotated_op_test.py")

# cuda top_k op has some asm code, the hipified version doesn't
# compile yet, so we don't have top_k operator for now
rocm_ignore_test+=("--ignore $CAFFE2_PYPATH/python/operator_test/top_k_test.py")


# Python tests
echo "Running Python tests.."
python \
-m pytest \
-v \
--ignore "$CAFFE2_PYPATH/python/test/executor_test.py" \
--ignore "$CAFFE2_PYPATH/python/operator_test/matmul_op_test.py" \
--ignore "$CAFFE2_PYPATH/python/operator_test/pack_ops_test.py" \
--ignore "$CAFFE2_PYPATH/python/mkl/mkl_sbn_speed_test.py" \
${rocm_ignore_test[@]} \
"$CAFFE2_PYPATH/python"
25 changes: 15 additions & 10 deletions rocm-docs/caffe2-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ Once the machine is ready with ROCm stack, there are two ways to use caffe2

## Launch docker container with caffe2 pre-installed
```
docker run -it --network=host --device=/dev/kfd --device=/dev/dri --group-add video rocm/caffe2:rocm1.8.2
docker run -it --network=host --device=/dev/kfd --device=/dev/dri --group-add video rocm/caffe2:rocm1.8.2-v2
```

To run benchmarks, skip directly to benchmarks section of the document.

## Build Caffe2 from source
### Pull the docker image
```
docker pull rocm/caffe2:unbuilt-rocm1.8.2
docker pull rocm/caffe2:unbuilt-rocm1.8.2-v2
```
This docker image has all the dependencies for caffe2 pre-installed.

### Pull the latest caffe2 source:

Checkout the `caffe2_specific` branch of this repository.
* Using https
```
git clone --recurse-submodules https://github.com/ROCmSoftwarePlatform/pytorch.git
git clone --recurse-submodules -b caffe2_specific https://github.com/ROCmSoftwarePlatform/pytorch.git
```
* Using ssh
```
git clone --recurse-submodules [email protected]:ROCmSoftwarePlatform/pytorch.git
git clone --recurse-submodules -b caffe2_specific [email protected]:ROCmSoftwarePlatform/pytorch.git
```
Navigate to repo directory
```
Expand All @@ -47,26 +49,29 @@ cd pytorch

### Launch the docker container
```
docker run -it --network=host --device=/dev/kfd --device=/dev/dri --group-add video -v $PWD:/pytorch rocm/caffe2:unbuilt-rocm1.8.2
docker run -it --network=host --device=/dev/kfd --device=/dev/dri --group-add video -v $PWD:/pytorch rocm/caffe2:unbuilt-rocm1.8.2-v2
```
Navigate to pytorch directory `cd /pytorch` inside the container.

### Build caffe2 Project from source

* Run the command

`.jenkins/caffe2/build.sh`
`.jenkins/caffe2/amd/amd_build.sh`


* Test the rocm-caffe2 Installation

Before running the tests, make sure that the required environment variables are set:
```
export LD_LIBRARY_PATH=/usr/local/caffe2/lib:$LD_LIBRARY_PATH
export PYTHONPATH=/usr/local/caffe2/lib/python2.7/dist-packages:$PYTHONPATH
```
cd build_caffe2 && python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"
```
Make sure the the following environment variables are set.

Run the binaries under `/pytorch/build_caffe2/bin`
```
LD_lIBRARY_PATH=/usr/local/caffe2/lib
PYTHONPATH=/usr/local/caffe2/lib/python2.7/dist-packages
```

## Run benchmarks

Expand Down