Skip to content

Commit 82fcf96

Browse files
committed
add command line switch to use f16 instead of f32 for memory k+v
1 parent 80df6cb commit 82fcf96

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

main.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct llama_model {
8686
};
8787

8888
// load the model's weights from a file
89-
bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab & vocab, int n_ctx) {
89+
bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab & vocab, int n_ctx, ggml_type memory_type = GGML_TYPE_F32) {
9090
fprintf(stderr, "%s: loading model from '%s' - please wait ...\n", __func__, fname.c_str());
9191

9292
std::vector<char> f_buf(1024*1024);
@@ -207,8 +207,8 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
207207
ctx_size += n_layer*(n_ff*n_embd*ggml_type_sizef(wtype)); // w2
208208
ctx_size += n_layer*(n_ff*n_embd*ggml_type_sizef(wtype)); // w3
209209

210-
ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F16); // memory_k
211-
ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F16); // memory_v
210+
ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(memory_type); // memory_k
211+
ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(memory_type); // memory_v
212212

213213
ctx_size += (5 + 10*n_layer)*256; // object overhead
214214

@@ -293,8 +293,8 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
293293
const int n_mem = n_layer*n_ctx;
294294
const int n_elements = n_embd*n_mem;
295295

296-
model.memory_k = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, n_elements);
297-
model.memory_v = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, n_elements);
296+
model.memory_k = ggml_new_tensor_1d(ctx, memory_type, n_elements);
297+
model.memory_v = ggml_new_tensor_1d(ctx, memory_type, n_elements);
298298

299299
const size_t memory_size = ggml_nbytes(model.memory_k) + ggml_nbytes(model.memory_v);
300300

@@ -814,8 +814,9 @@ int main(int argc, char ** argv) {
814814

815815
// load the model
816816
{
817+
const ggml_type memory_type = params.memory_f16 ? GGML_TYPE_F16 : GGML_TYPE_F32;
817818
const int64_t t_start_us = ggml_time_us();
818-
if (!llama_model_load(params.model, model, vocab, params.n_ctx)) {
819+
if (!llama_model_load(params.model, model, vocab, params.n_ctx, memory_type)) {
819820
fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, params.model.c_str());
820821
return 1;
821822
}

utils.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
4646
params.top_k = std::stoi(argv[++i]);
4747
} else if (arg == "-c" || arg == "--ctx_size") {
4848
params.n_ctx = std::stoi(argv[++i]);
49+
} else if (arg == "--memory_f16") {
50+
params.memory_f16 = true;
4951
} else if (arg == "--top_p") {
5052
params.top_p = std::stof(argv[++i]);
5153
} else if (arg == "--temp") {
@@ -101,6 +103,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
101103
fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
102104
fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
103105
fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
106+
fprintf(stderr, " --memory_f16 use f16 instead of f32 for memory key+value\n");
104107
fprintf(stderr, " --temp N temperature (default: %.1f)\n", params.temp);
105108
fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
106109
fprintf(stderr, " -m FNAME, --model FNAME\n");

utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct gpt_params {
1818
int32_t n_predict = 128; // new tokens to predict
1919
int32_t repeat_last_n = 64; // last n tokens to penalize
2020
int32_t n_ctx = 512; //context size
21+
bool memory_f16 = false; // use f16 instead of f32 for memory kv
2122

2223
// sampling parameters
2324
int32_t top_k = 40;

0 commit comments

Comments
 (0)