Skip to content

Commit e02c45c

Browse files
committed
examples: add compression example
1 parent 70392f1 commit e02c45c

File tree

6 files changed

+675
-1
lines changed

6 files changed

+675
-1
lines changed

common/arg.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
16331633
params.cvector_outfile = value;
16341634
params.lora_outfile = value;
16351635
}
1636-
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA}));
1636+
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA, LLAMA_EXAMPLE_COMPRESS}));
16371637
add_opt(llama_arg(
16381638
{"-ofreq", "--output-frequency"}, "N",
16391639
format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
@@ -1938,6 +1938,24 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
19381938
else { std::invalid_argument("invalid value"); }
19391939
}
19401940
).set_examples({LLAMA_EXAMPLE_BENCH}));
1941+
add_opt(llama_arg(
1942+
{"--compression_header_size"}, "N",
1943+
"Number of tokens to keep in header (default: 1)",
1944+
[](gpt_params & params, int value){
1945+
params.num_tokens_header = value;
1946+
}).set_examples({LLAMA_EXAMPLE_COMPRESS}));
1947+
add_opt(llama_arg(
1948+
{"--mode"}, "{compress,expand,test}",
1949+
"What task to run (default: test)",
1950+
[](gpt_params & params, const std::string & value){
1951+
if (value == "test"){
1952+
return; }
1953+
else if (value == "compress"){
1954+
params.compress_mode = 1; }
1955+
else if (value == "expand"){
1956+
params.compress_mode = 2; }
1957+
else { std::invalid_argument("invalid value"); }
1958+
}).set_examples({LLAMA_EXAMPLE_COMPRESS}));
19411959
add_opt(llama_arg(
19421960
{"--log-disable"},
19431961
"Log disable",

common/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ enum llama_example {
8080
LLAMA_EXAMPLE_PARALLEL,
8181

8282
LLAMA_EXAMPLE_COUNT,
83+
LLAMA_EXAMPLE_COMPRESS
8384
};
8485

8586
enum gpt_sampler_type {
@@ -340,6 +341,9 @@ struct gpt_params {
340341

341342
// batched-bench params
342343
bool batched_bench_output_jsonl = false;
344+
345+
int num_tokens_header = 1;
346+
int compress_mode = 0;
343347
};
344348

345349
// call once at the start of a program if it uses libcommon

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ else()
1717
add_subdirectory(batched-bench)
1818
add_subdirectory(batched)
1919
add_subdirectory(benchmark)
20+
add_subdirectory(compress)
2021
add_subdirectory(convert-llama2c-to-ggml)
2122
add_subdirectory(embedding)
2223
add_subdirectory(eval-callback)

examples/compress/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(TARGET llama-compress)
2+
add_executable(${TARGET} compress.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)

examples/compress/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# llama.cpp/examples/compress
2+
3+
Demonstration of LLM-based natural language compression.

0 commit comments

Comments
 (0)