Skip to content

Commit

Permalink
Removing Manual Hipify Build Step (facebookresearch#3962)
Browse files Browse the repository at this point in the history
Summary:
- Called the hipify script at CMAKE configure time removing the need for the user to run it.
- Now removes any .hip files left over when running the hipify script.
- Cleaned up the hipify script to remove redundancy.

Pull Request resolved: facebookresearch#3962

Reviewed By: asadoughi, ramilbakhshyiev

Differential Revision: D64495550

Pulled By: mnorris11

fbshipit-source-id: 5547712a4e46fc18cf62346adb0395d0e5626399
  • Loading branch information
ItsPitt authored and facebook-github-bot committed Oct 18, 2024
1 parent 6617b13 commit f9a01c6
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 180 deletions.
4 changes: 0 additions & 4 deletions .github/actions/build_cmake/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ runs:
sudo apt-get -qq autoclean >/dev/null
sudo apt-get -qq clean >/dev/null
sudo rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
- name: ROCm - Hipify
if: inputs.rocm == 'ON'
shell: bash
run: ./faiss/gpu/hipify.sh
- name: Symblink system dependencies
if: inputs.raft == 'ON' || inputs.rocm == 'ON'
shell: bash
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ if(FAISS_ENABLE_GPU)
find_package(HIP REQUIRED)
find_package(hipBLAS REQUIRED)
set(GPU_EXT_PREFIX "hip")
execute_process(COMMAND ${PROJECT_SOURCE_DIR}/faiss/gpu/hipify.sh)
else ()
set(CMAKE_CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
enable_language(CUDA)
Expand Down
4 changes: 1 addition & 3 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ Several options can be passed to CMake, among which:
to build against (see [CUDA docs](https://developer.nvidia.com/cuda-gpus) to
determine which architecture(s) you should pick),
- `-DFAISS_ENABLE_ROCM=ON` in order to enable building GPU indices for AMD GPUs.
The hipify script must be executed before using this option.
Invoke `./faiss/gpu/hipify.sh` to execute. `-DFAISS_ENABLE_GPU` must be `ON`
when using this option. (possible values are `ON` and `OFF`),
`-DFAISS_ENABLE_GPU` must be `ON` when using this option. (possible values are `ON` and `OFF`),
- python-related options:
- `-DPython_EXECUTABLE=/path/to/python3.7` in order to build a python
interface for a different python than the default one (see
Expand Down
266 changes: 93 additions & 173 deletions faiss/gpu/hipify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,87 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# go one level up from faiss/gpu
top=$(dirname "${BASH_SOURCE[0]}")/..
echo "top=$top"
cd "$top" || exit
echo "pwd=$(pwd)"
function hipify_dir()
{
# print dir name
cd "$1" || exit
echo "Hipifying $(pwd)"

# create all destination directories for hipified files into sibling 'gpu-rocm' directory
while IFS= read -r -d '' src
do
dst="${src//gpu/gpu-rocm}"
echo "Creating $dst"
mkdir -p "$dst"
done < <(find ./gpu -type d -print0)

# run hipify-perl against all *.cu *.cuh *.h *.cpp files, no renaming
# run all files in parallel to speed up
for ext in cu cuh h cpp
do
# create all destination directories for hipified files into sibling 'gpu-rocm' directory
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
hipify-perl -o="$dst.tmp" "$src" &
done < <(find ./gpu -name "*.$ext" -print0)
done
wait
dst="${src//gpu/gpu-rocm}"

# rename all hipified *.cu files to *.hip
while IFS= read -r -d '' src
do
dst=${src%.cu.tmp}.hip.tmp
mv "$src" "$dst"
done < <(find ./gpu-rocm -name "*.cu.tmp" -print0)
if [ -d $dst ]; then
#Clearing out any leftover files and directories
echo "Removing old $dst"
rm -rf "$dst"
fi

# replace header include statements "<faiss/gpu/" with "<faiss/gpu-rocm"
# replace thrust::cuda::par with thrust::hip::par
# adjust header path location for hipblas.h to avoid unnecessary deprecation warnings
# adjust header path location for hiprand_kernel.h to avoid unnecessary deprecation warnings
for ext in hip cuh h cpp
do
while IFS= read -r -d '' src
do
sed -i 's@#include <faiss/gpu/@#include <faiss/gpu-rocm/@' "$src"
sed -i 's@thrust::cuda::par@thrust::hip::par@' "$src"
sed -i 's@#include <hipblas.h>@#include <hipblas/hipblas.h>@' "$src"
sed -i 's@#include <hiprand_kernel.h>@#include <hiprand/hiprand_kernel.h>@' "$src"
done < <(find ./gpu-rocm -name "*.$ext.tmp" -print0)
done
#Making directories
echo "Creating $dst"
mkdir -p "$dst"
done < <(find ./gpu -type d -print0)

# hipify was run in parallel above
# don't copy the tmp file if it is unchanged
for ext in hip cuh h cpp
do
# run hipify-perl against all *.cu *.cuh *.h *.cpp files, no renaming
# run all files in parallel to speed up
for ext in cu cuh h cpp c
do
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
hipify-perl -o="$dst.tmp" "$src" &
done < <(find ./gpu -name "*.$ext" -print0)
done
wait

# rename all hipified *.cu files to *.hip
while IFS= read -r -d '' src
do
dst=${src%.tmp}
if test -f "$dst"
then
if diff -q "$src" "$dst" >& /dev/null
dst=${src%.cu.tmp}.hip.tmp
mv "$src" "$dst"
done < <(find ./gpu-rocm -name "*.cu.tmp" -print0)

# replace header include statements "<faiss/gpu/" with "<faiss/gpu-rocm"
# replace thrust::cuda::par with thrust::hip::par
# adjust header path location for hipblas.h to avoid unnecessary deprecation warnings
# adjust header path location for hiprand_kernel.h to avoid unnecessary deprecation warnings
for ext in hip cuh h cpp c
do
while IFS= read -r -d '' src
do
sed -i 's@#include <faiss/gpu/@#include <faiss/gpu-rocm/@' "$src"
sed -i 's@thrust::cuda::par@thrust::hip::par@' "$src"
sed -i 's@#include <hipblas.h>@#include <hipblas/hipblas.h>@' "$src"
sed -i 's@#include <hiprand_kernel.h>@#include <hiprand/hiprand_kernel.h>@' "$src"
done < <(find ./gpu-rocm -name "*.$ext.tmp" -print0)
done

# hipify was run in parallel above
# don't copy the tmp file if it is unchanged
for ext in hip cuh h cpp c
do
while IFS= read -r -d '' src
do
dst=${src%.tmp}
if test -f "$dst"
then
echo "$dst [unchanged]"
rm "$src"
if diff -q "$src" "$dst" >& /dev/null
then
echo "$dst [unchanged]"
rm "$src"
else
echo "$dst"
mv "$src" "$dst"
fi
else
echo "$dst"
mv "$src" "$dst"
fi
else
echo "$dst"
mv "$src" "$dst"
fi
done < <(find ./gpu-rocm -name "*.$ext.tmp" -print0)
done

# copy over CMakeLists.txt
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
if test -f "$dst"
then
if diff -q "$src" "$dst" >& /dev/null
then
echo "$dst [unchanged]"
else
echo "$dst"
cp "$src" "$dst"
fi
else
echo "$dst"
cp "$src" "$dst"
fi
done < <(find ./gpu -name "CMakeLists.txt" -print0)
done < <(find ./gpu-rocm -name "*.$ext.tmp" -print0)
done

# Copy over other files
other_exts="py"
for ext in $other_exts
do
# copy over CMakeLists.txt
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
Expand All @@ -115,102 +101,36 @@ do
echo "$dst"
cp "$src" "$dst"
fi
done < <(find ./gpu -name "*.$ext" -print0)
done

###################################################################################
# C_API Support
###################################################################################

# Now get the c_api dir
# This points to the faiss/c_api dir
top_c_api=$(dirname "${BASH_SOURCE[0]}")/../../c_api
echo "top=$top_c_api"
cd "../$top_c_api" || exit
echo "pwd=$(pwd)"


# create all destination directories for hipified files into sibling 'gpu-rocm' directory
while IFS= read -r -d '' src
do
dst="${src//gpu/gpu-rocm}"
echo "Creating $dst"
mkdir -p "$dst"
done < <(find ./gpu -type d -print0)
done < <(find ./gpu -name "CMakeLists.txt" -print0)

# run hipify-perl against all *.cu *.cuh *.h *.cpp files, no renaming
# run all files in parallel to speed up
for ext in cu cuh h cpp c
do
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
hipify-perl -o="$dst.tmp" "$src" &
done < <(find ./gpu -name "*.$ext" -print0)
done
wait

# rename all hipified *.cu files to *.hip
while IFS= read -r -d '' src
do
dst=${src%.cu.tmp}.hip.tmp
mv "$src" "$dst"
done < <(find ./gpu-rocm -name "*.cu.tmp" -print0)

# replace header include statements "<faiss/gpu/" with "<faiss/gpu-rocm"
# replace thrust::cuda::par with thrust::hip::par
# adjust header path location for hipblas.h to avoid unnecessary deprecation warnings
# adjust header path location for hiprand_kernel.h to avoid unnecessary deprecation warnings
for ext in hip cuh h cpp c
do
while IFS= read -r -d '' src
# Copy over other files
other_exts="py"
for ext in $other_exts
do
sed -i 's@#include <faiss/gpu/@#include <faiss/gpu-rocm/@' "$src"
sed -i 's@thrust::cuda::par@thrust::hip::par@' "$src"
sed -i 's@#include <hipblas.h>@#include <hipblas/hipblas.h>@' "$src"
sed -i 's@#include <hiprand_kernel.h>@#include <hiprand/hiprand_kernel.h>@' "$src"
done < <(find ./gpu-rocm -name "*.$ext.tmp" -print0)
done

# hipify was run in parallel above
# don't copy the tmp file if it is unchanged
for ext in hip cuh h cpp c
do
while IFS= read -r -d '' src
do
dst=${src%.tmp}
if test -f "$dst"
then
if diff -q "$src" "$dst" >& /dev/null
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
if test -f "$dst"
then
echo "$dst [unchanged]"
rm "$src"
if diff -q "$src" "$dst" >& /dev/null
then
echo "$dst [unchanged]"
else
echo "$dst"
cp "$src" "$dst"
fi
else
echo "$dst"
mv "$src" "$dst"
cp "$src" "$dst"
fi
else
echo "$dst"
mv "$src" "$dst"
fi
done < <(find ./gpu-rocm -name "*.$ext.tmp" -print0)
done
done < <(find ./gpu -name "*.$ext" -print0)
done
}

# copy over CMakeLists.txt
while IFS= read -r -d '' src
do
dst="${src//\.\/gpu/\.\/gpu-rocm}"
if test -f "$dst"
then
if diff -q "$src" "$dst" >& /dev/null
then
echo "$dst [unchanged]"
else
echo "$dst"
cp "$src" "$dst"
fi
else
echo "$dst"
cp "$src" "$dst"
fi
done < <(find ./gpu -name "CMakeLists.txt" -print0)
# Convert the faiss/gpu dir
dir_name=$(dirname "${BASH_SOURCE[0]}")/..
hipify_dir $dir_name

# Convert the faiss/c_api dir
dir_name=$(dirname "${BASH_SOURCE[0]}")/../../c_api
hipify_dir $dir_name

0 comments on commit f9a01c6

Please sign in to comment.