Skip to content

Commit b19edd5

Browse files
authored
Adding support for llama2.c models (#2559)
1 parent 53dc399 commit b19edd5

File tree

6 files changed

+864
-2
lines changed

6 files changed

+864
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.o
22
*.a
33
*.so
4+
*.bin
45
.DS_Store
56
.build/
67
.cache/
@@ -39,6 +40,7 @@ models-mnt
3940
/perplexity
4041
/embedding
4142
/train-text-from-scratch
43+
/convert-llama2c-to-ggml
4244
/simple
4345
/benchmark-matmult
4446
/vdot

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Define the default target now so that it is always the first target
2-
BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot train-text-from-scratch simple server embd-input-test
2+
BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot train-text-from-scratch convert-llama2c-to-ggml simple server embd-input-test
33

44
# Binaries only useful for tests
55
TEST_TARGETS = tests/test-double-float tests/test-grad0 tests/test-opt tests/test-quantize-fns tests/test-quantize-perf tests/test-sampling tests/test-tokenizer-0
@@ -345,7 +345,7 @@ libllama.so: llama.o ggml.o $(OBJS)
345345
$(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
346346

347347
clean:
348-
rm -vf *.o *.so *.dll main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state server simple vdot train-text-from-scratch embd-input-test build-info.h $(TEST_TARGETS)
348+
rm -vf *.o *.so *.dll main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state server simple vdot train-text-from-scratch convert-llama2c-to-ggml embd-input-test build-info.h $(TEST_TARGETS)
349349

350350
#
351351
# Examples
@@ -388,6 +388,9 @@ embd-input-test: $(LIB_PRE)embdinput$(DSO_EXT) examples/embd-input/embd-input-te
388388
train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp build-info.h ggml.o llama.o $(OBJS)
389389
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
390390

391+
convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp build-info.h ggml.o llama.o $(OBJS)
392+
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
393+
391394
build-info.h: $(wildcard .git/index) scripts/build-info.sh
392395
@sh scripts/build-info.sh > $@.tmp
393396
@if ! cmp -s $@.tmp $@; then \

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ else()
4242
add_subdirectory(benchmark)
4343
add_subdirectory(baby-llama)
4444
add_subdirectory(train-text-from-scratch)
45+
add_subdirectory(convert-llama2c-to-ggml)
4546
add_subdirectory(simple)
4647
add_subdirectory(embd-input)
4748
if (LLAMA_METAL)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(TARGET convert-llama2c-to-ggml)
2+
add_executable(${TARGET} convert-llama2c-to-ggml.cpp)
3+
install(TARGETS ${TARGET} RUNTIME)
4+
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
5+
target_compile_features(${TARGET} PRIVATE cxx_std_11)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Convert llama2.c model to ggml
2+
3+
This example reads weights from project [llama2.c](https://github.com/karpathy/llama2.c) and saves them in ggml compatible format. The vocab that is available in `models/ggml-vocab.bin` is used by default.
4+
5+
To convert the model first download the models from the [llma2.c](https://github.com/karpathy/llama2.c) repository:
6+
7+
`$ make -j`
8+
9+
After successful compilation, following usage options are available:
10+
```
11+
usage: ./convert-llama2c-to-ggml [options]
12+
13+
options:
14+
-h, --help show this help message and exit
15+
--copy-vocab-from-model FNAME model path from which to copy vocab (default 'models/ggml-vocab.bin')
16+
--llama2c-model FNAME [REQUIRED] model path from which to load Karpathy's llama2.c model
17+
--llama2c-output-model FNAME model path to save the converted llama2.c model (default ak_llama_model.bin')
18+
```
19+
20+
An example command is as follows:
21+
22+
`$ ./convert-llama2c-to-ggml --copy-vocab-from-model <ggml-vocab.bin> --llama2c-model <llama2.c model path> --llama2c-output-model <ggml output model path>`
23+
24+
Now you can use the model with command like:
25+
26+
`$ ./main -m <ggml output model path> -p "One day, Lily met a Shoggoth" -n 500 -c 256 -eps 1e-5`

0 commit comments

Comments
 (0)