sync_llama.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. # llava
  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/examples/llava/llava.cpp $dst_dir/llava.cpp
  44. cp $src_dir/examples/llava/llava.h $dst_dir/llava.h
  45. cp $src_dir/common/log.h $dst_dir/log.h
  46. cp $src_dir/common/stb_image.h $dst_dir/stb_image.h
  47. # These files are mostly used by the llava code
  48. # and shouldn't be necessary once we use clip.cpp directly
  49. cp $src_dir/common/common.cpp $dst_dir/common.cpp
  50. cp $src_dir/common/common.h $dst_dir/common.h
  51. cp $src_dir/common/sampling.cpp $dst_dir/sampling.cpp
  52. cp $src_dir/common/sampling.h $dst_dir/sampling.h
  53. cp $src_dir/common/grammar-parser.cpp $dst_dir/grammar-parser.cpp
  54. cp $src_dir/common/grammar-parser.h $dst_dir/grammar-parser.h
  55. cp $src_dir/common/json.hpp $dst_dir/json.hpp
  56. cp $src_dir/common/json-schema-to-grammar.cpp $dst_dir/json-schema-to-grammar.cpp
  57. cp $src_dir/common/json-schema-to-grammar.h $dst_dir/json-schema-to-grammar.h
  58. cp $src_dir/common/base64.hpp $dst_dir/base64.hpp
  59. cat <<EOF > $dst_dir/build-info.cpp
  60. int LLAMA_BUILD_NUMBER = 0;
  61. char const *LLAMA_COMMIT = "$sha1";
  62. char const *LLAMA_COMPILER = "";
  63. char const *LLAMA_BUILD_TARGET = "";
  64. EOF
  65. # apply patches
  66. for patch in $dst_dir/patches/*.patch; do
  67. git apply "$patch"
  68. done
  69. # add licenses
  70. sha1=$(git -C $src_dir rev-parse @)
  71. TEMP_LICENSE=$(mktemp)
  72. cleanup() {
  73. rm -f $TEMP_LICENSE
  74. }
  75. trap cleanup 0
  76. cat <<EOF | sed 's/ *$//' >$TEMP_LICENSE
  77. /**
  78. * llama.cpp - git $sha1
  79. *
  80. $(sed 's/^/ * /' <$src_dir/LICENSE)
  81. */
  82. EOF
  83. for IN in $dst_dir/*.{c,h,cpp,m,metal,cu}; do
  84. if [[ "$IN" == *"sgemm.cpp" || "$IN" == *"sgemm.h" ]]; then
  85. continue
  86. fi
  87. TMP=$(mktemp)
  88. cat $TEMP_LICENSE $IN >$TMP
  89. mv $TMP $IN
  90. done
  91. # ggml-metal
  92. sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < $dst_dir/ggml-metal.metal > temp.metal
  93. TEMP_ASSEMBLY=$(mktemp)
  94. echo ".section __DATA, __ggml_metallib" > $TEMP_ASSEMBLY
  95. echo ".globl _ggml_metallib_start" >> $TEMP_ASSEMBLY
  96. echo "_ggml_metallib_start:" >> $TEMP_ASSEMBLY
  97. echo ".incbin \"temp.metal\"" >> $TEMP_ASSEMBLY
  98. echo ".globl _ggml_metallib_end" >> $TEMP_ASSEMBLY
  99. echo "_ggml_metallib_end:" >> $TEMP_ASSEMBLY
  100. as -mmacosx-version-min=11.3 $TEMP_ASSEMBLY -o ggml-metal.o
  101. rm -f $TEMP_ASSEMBLY
  102. rm -rf temp.metal