sync_llama.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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-darwin_arm64.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. # clip
  41. cp $src_dir/examples/llava/clip.cpp $dst_dir/clip.cpp
  42. cp $src_dir/examples/llava/clip.h $dst_dir/clip.h
  43. cp $src_dir/common/log.h $dst_dir/log.h
  44. cp $src_dir/common/stb_image.h $dst_dir/stb_image.h
  45. # apply patches
  46. for patch in $dst_dir/patches/*.patch; do
  47. git apply "$patch"
  48. done
  49. # add licenses
  50. sha1=$(git -C $src_dir rev-parse @)
  51. TEMP_LICENSE=$(mktemp)
  52. cleanup() {
  53. rm -f $TEMP_LICENSE
  54. }
  55. trap cleanup 0
  56. cat <<EOF | sed 's/ *$//' >$TEMP_LICENSE
  57. /**
  58. * llama.cpp - git $sha1
  59. *
  60. $(sed 's/^/ * /' <$src_dir/LICENSE)
  61. */
  62. EOF
  63. for IN in $dst_dir/*.{c,h,cpp,m,metal,cu}; do
  64. if [[ "$IN" == *"sgemm.cpp" || "$IN" == *"sgemm.h" ]]; then
  65. continue
  66. fi
  67. TMP=$(mktemp)
  68. cat $TEMP_LICENSE $IN >$TMP
  69. mv $TMP $IN
  70. done
  71. # ggml-metal
  72. sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < $dst_dir/ggml-metal.metal > temp.metal
  73. TEMP_ASSEMBLY=$(mktemp)
  74. echo ".section __DATA, __ggml_metallib" > $TEMP_ASSEMBLY
  75. echo ".globl _ggml_metallib_start" >> $TEMP_ASSEMBLY
  76. echo "_ggml_metallib_start:" >> $TEMP_ASSEMBLY
  77. echo ".incbin \"temp.metal\"" >> $TEMP_ASSEMBLY
  78. echo ".globl _ggml_metallib_end" >> $TEMP_ASSEMBLY
  79. echo "_ggml_metallib_end:" >> $TEMP_ASSEMBLY
  80. as -mmacosx-version-min=11.3 $TEMP_ASSEMBLY -o ggml-metal.o
  81. rm -f $TEMP_ASSEMBLY
  82. rm -rf temp.metal