sync_llama.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. set -e
  3. # Set the source directory
  4. src_dir=$1
  5. if [ -z "$src_dir" ]; then
  6. echo "Usage: $0 LLAMA_CPP_DIR"
  7. exit 1
  8. fi
  9. # Set the destination directory
  10. dst_dir=./llama
  11. # llama.cpp
  12. cp $src_dir/unicode.cpp $dst_dir/unicode.cpp
  13. cp $src_dir/unicode.h $dst_dir/unicode.h
  14. cp $src_dir/unicode-data.cpp $dst_dir/unicode-data.cpp
  15. cp $src_dir/unicode-data.h $dst_dir/unicode-data.h
  16. cp $src_dir/llama.cpp $dst_dir/llama.cpp
  17. cp $src_dir/llama.h $dst_dir/llama.h
  18. cp $src_dir/sgemm.cpp $dst_dir/sgemm.cpp
  19. cp $src_dir/sgemm.h $dst_dir/sgemm.h
  20. # ggml
  21. cp $src_dir/ggml.c $dst_dir/ggml.c
  22. cp $src_dir/ggml.h $dst_dir/ggml.h
  23. cp $src_dir/ggml-quants.c $dst_dir/ggml-quants.c
  24. cp $src_dir/ggml-quants.h $dst_dir/ggml-quants.h
  25. cp $src_dir/ggml-metal.metal $dst_dir/ggml-metal.metal
  26. cp $src_dir/ggml-metal.h $dst_dir/ggml-metal.h
  27. cp $src_dir/ggml-metal.m $dst_dir/ggml-metal-darwin_arm64.m
  28. cp $src_dir/ggml-impl.h $dst_dir/ggml-impl.h
  29. cp $src_dir/ggml-cuda.h $dst_dir/ggml-cuda.h
  30. cp $src_dir/ggml-cuda.cu $dst_dir/ggml-cuda.cu
  31. cp $src_dir/ggml-common.h $dst_dir/ggml-common.h
  32. cp $src_dir/ggml-backend.h $dst_dir/ggml-backend.h
  33. cp $src_dir/ggml-backend.c $dst_dir/ggml-backend.c
  34. cp $src_dir/ggml-backend-impl.h $dst_dir/ggml-backend-impl.h
  35. cp $src_dir/ggml-alloc.h $dst_dir/ggml-alloc.h
  36. cp $src_dir/ggml-alloc.c $dst_dir/ggml-alloc.c
  37. # ggml-cuda
  38. mkdir -p $dst_dir/ggml-cuda
  39. cp $src_dir/ggml-cuda/*.cu $dst_dir/ggml-cuda/
  40. cp $src_dir/ggml-cuda/*.cuh $dst_dir/ggml-cuda/
  41. # llava
  42. cp $src_dir/examples/llava/clip.cpp $dst_dir/clip.cpp
  43. cp $src_dir/examples/llava/clip.h $dst_dir/clip.h
  44. cp $src_dir/examples/llava/llava.cpp $dst_dir/llava.cpp
  45. cp $src_dir/examples/llava/llava.h $dst_dir/llava.h
  46. cp $src_dir/common/log.h $dst_dir/log.h
  47. cp $src_dir/common/stb_image.h $dst_dir/stb_image.h
  48. # These files are mostly used by the llava code
  49. # and shouldn't be necessary once we use clip.cpp directly
  50. cp $src_dir/common/common.cpp $dst_dir/common.cpp
  51. cp $src_dir/common/common.h $dst_dir/common.h
  52. cp $src_dir/common/sampling.cpp $dst_dir/sampling.cpp
  53. cp $src_dir/common/sampling.h $dst_dir/sampling.h
  54. cp $src_dir/common/grammar-parser.cpp $dst_dir/grammar-parser.cpp
  55. cp $src_dir/common/grammar-parser.h $dst_dir/grammar-parser.h
  56. cp $src_dir/common/json.hpp $dst_dir/json.hpp
  57. cp $src_dir/common/json-schema-to-grammar.cpp $dst_dir/json-schema-to-grammar.cpp
  58. cp $src_dir/common/json-schema-to-grammar.h $dst_dir/json-schema-to-grammar.h
  59. cp $src_dir/common/base64.hpp $dst_dir/base64.hpp
  60. cat <<EOF > $dst_dir/build-info.cpp
  61. int LLAMA_BUILD_NUMBER = 0;
  62. char const *LLAMA_COMMIT = "$sha1";
  63. char const *LLAMA_COMPILER = "";
  64. char const *LLAMA_BUILD_TARGET = "";
  65. EOF
  66. # apply patches
  67. for patch in $dst_dir/patches/*.diff; do
  68. git apply "$patch"
  69. done
  70. # add licenses
  71. sha1=$(git -C $src_dir rev-parse @)
  72. TEMP_LICENSE=$(mktemp)
  73. cleanup() {
  74. rm -f $TEMP_LICENSE
  75. }
  76. trap cleanup 0
  77. cat <<EOF | sed 's/ *$//' >$TEMP_LICENSE
  78. /**
  79. * llama.cpp - git $sha1
  80. *
  81. $(sed 's/^/ * /' <$src_dir/LICENSE)
  82. */
  83. EOF
  84. for IN in $dst_dir/*.{c,h,cpp,m,metal,cu}; do
  85. if [[ "$IN" == *"sgemm.cpp" || "$IN" == *"sgemm.h" ]]; then
  86. continue
  87. fi
  88. TMP=$(mktemp)
  89. cat $TEMP_LICENSE $IN >$TMP
  90. mv $TMP $IN
  91. done
  92. # ggml-metal
  93. sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < $dst_dir/ggml-metal.metal > temp.metal
  94. TEMP_ASSEMBLY=$(mktemp)
  95. echo ".section __DATA, __ggml_metallib" > $TEMP_ASSEMBLY
  96. echo ".globl _ggml_metallib_start" >> $TEMP_ASSEMBLY
  97. echo "_ggml_metallib_start:" >> $TEMP_ASSEMBLY
  98. echo ".incbin \"temp.metal\"" >> $TEMP_ASSEMBLY
  99. echo ".globl _ggml_metallib_end" >> $TEMP_ASSEMBLY
  100. echo "_ggml_metallib_end:" >> $TEMP_ASSEMBLY
  101. as -mmacosx-version-min=11.3 $TEMP_ASSEMBLY -o $dst_dir/ggml-metal.o
  102. rm -f $TEMP_ASSEMBLY
  103. rm -rf temp.metal