sync_llama.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. # Set the source directory
  3. src_dir=$1
  4. if [ -z "$src_dir" ]; then
  5. echo "Usage: $0 LLAMA_CPP_DIR"
  6. exit 1
  7. fi
  8. # Set the destination directory (current directory)
  9. dst_dir=./llama
  10. # llama.cpp
  11. cp $src_dir/unicode.cpp $dst_dir/unicode.cpp
  12. cp $src_dir/unicode.h $dst_dir/unicode.h
  13. cp $src_dir/unicode-data.cpp $dst_dir/unicode-data.cpp
  14. cp $src_dir/unicode-data.h $dst_dir/unicode-data.h
  15. cp $src_dir/llama.cpp $dst_dir/llama.cpp
  16. cp $src_dir/llama.h $dst_dir/llama.h
  17. cp $src_dir/sgemm.cpp $dst_dir/sgemm.cpp
  18. cp $src_dir/sgemm.h $dst_dir/sgemm.h
  19. # ggml
  20. cp $src_dir/ggml.c $dst_dir/ggml.c
  21. cp $src_dir/ggml.h $dst_dir/ggml.h
  22. cp $src_dir/ggml-quants.c $dst_dir/ggml-quants.c
  23. cp $src_dir/ggml-quants.h $dst_dir/ggml-quants.h
  24. cp $src_dir/ggml-metal.metal $dst_dir/ggml-metal.metal
  25. cp $src_dir/ggml-metal.h $dst_dir/ggml-metal.h
  26. cp $src_dir/ggml-metal.m $dst_dir/ggml-metal.m
  27. cp $src_dir/ggml-impl.h $dst_dir/ggml-impl.h
  28. cp $src_dir/ggml-cuda.h $dst_dir/ggml-cuda.h
  29. cp $src_dir/ggml-cuda.cu $dst_dir/ggml-cuda.cu
  30. cp $src_dir/ggml-common.h $dst_dir/ggml-common.h
  31. cp $src_dir/ggml-backend.h $dst_dir/ggml-backend.h
  32. cp $src_dir/ggml-backend.c $dst_dir/ggml-backend.c
  33. cp $src_dir/ggml-backend-impl.h $dst_dir/ggml-backend-impl.h
  34. cp $src_dir/ggml-alloc.h $dst_dir/ggml-alloc.h
  35. cp $src_dir/ggml-alloc.c $dst_dir/ggml-alloc.c
  36. # ggml-cuda
  37. mkdir -p $dst_dir/ggml-cuda
  38. cp $src_dir/ggml-cuda/*.cu $dst_dir/ggml-cuda/
  39. cp $src_dir/ggml-cuda/*.cuh $dst_dir/ggml-cuda/
  40. # apply patches
  41. for patch in $dst_dir/patches/*.patch; do
  42. git apply "$patch"
  43. done
  44. # add license
  45. sha1=$(git -C $src_dir rev-parse @)
  46. TEMP_LICENSE=$(mktemp)
  47. cleanup() {
  48. rm -f $TEMP_LICENSE
  49. }
  50. trap cleanup 0
  51. cat <<EOF | sed 's/ *$//' >$TEMP_LICENSE
  52. /**
  53. * llama.cpp - git $sha1
  54. *
  55. $(sed 's/^/ * /' <$src_dir/LICENSE)
  56. */
  57. EOF
  58. for IN in $dst_dir/*.{c,h,cpp,m,metal,cu}; do
  59. if [[ "$IN" == *"sgemm.cpp" || "$IN" == *"sgemm.h" ]]; then
  60. continue
  61. fi
  62. TMP=$(mktemp)
  63. cat $TEMP_LICENSE $IN >$TMP
  64. mv $TMP $IN
  65. done
  66. # ggml-metal
  67. sed -i '' '1s;^;// go:build darwin,arm64\n\n;' $dst_dir/ggml-metal.m
  68. sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < $dst_dir/ggml-metal.metal > temp.metal
  69. TEMP_ASSEMBLY=$(mktemp)
  70. echo ".section __DATA, __ggml_metallib" > $TEMP_ASSEMBLY
  71. echo ".globl _ggml_metallib_start" >> $TEMP_ASSEMBLY
  72. echo "_ggml_metallib_start:" >> $TEMP_ASSEMBLY
  73. echo ".incbin \"temp.metal\"" >> $TEMP_ASSEMBLY
  74. echo ".globl _ggml_metallib_end" >> $TEMP_ASSEMBLY
  75. echo "_ggml_metallib_end:" >> $TEMP_ASSEMBLY
  76. as -mmacosx-version-min=11.3 $TEMP_ASSEMBLY -o ggml-metal.o
  77. rm -f $TEMP_ASSEMBLY
  78. rm -rf temp.metal