Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit eaeb52f

Browse files
authored
Merge pull request #67 from ravenscroftj/fix/mac-memory-usage
Temporary fix for stablecode and starcoder on mac
2 parents 8fd357e + 83cb7c0 commit eaeb52f

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

src/gptneox.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,21 @@ bool gpt_neox_eval(
8484
const int n_vocab = hparams.n_vocab;
8585
const int n_rot = hparams.n_rot;
8686

87-
static size_t buf_size = 256u*1024*1024;
87+
static size_t buf_size = 512u*1024*1024;
8888
static void * buf = malloc(buf_size);
8989

9090
// use 2 scratch buffers
9191
// TODO: very hacky solution - reimplement in a more elegant way
92-
static size_t scr0_size = 256u*1024*1024;
92+
static size_t scr0_size = 512*1024*1024;
9393
static void * scr0 = malloc(scr0_size);
9494

95-
static size_t scr1_size = 256u*1024*1024;
95+
static size_t scr1_size = 512*1024*1024;
9696
static void * scr1 = malloc(scr1_size);
97+
9798

98-
if (mem_per_token > 0 && mem_per_token*N > buf_size) {
99+
if (mem_per_token > 0 && (mem_per_token*N) > buf_size) {
99100
const size_t buf_size_new = 1.1*(mem_per_token*N); // add 10% to account for ggml object overhead
100-
//printf("\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new);
101+
spdlog::debug("{}: reallocating buffer from {} to {} bytes\n", __func__, buf_size, buf_size_new);
101102

102103

103104
// reallocate
@@ -107,8 +108,6 @@ bool gpt_neox_eval(
107108
fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size);
108109
return false;
109110
}
110-
111-
spdlog::debug("{}: reallocating context buffer {} -> now {} bytes of tokens in prompt = {}", __func__, buf_size, buf_size_new);
112111
}
113112

114113
struct ggml_init_params params = {
@@ -303,12 +302,14 @@ bool gpt_neox_eval(
303302
embd_w.resize(n_vocab);
304303
memcpy(embd_w.data(), (float *) ggml_get_data(inpL) + (n_vocab*(N-1)), sizeof(float)*n_vocab);
305304

305+
306+
spdlog::debug("used_mem = {}\n", ggml_used_mem(ctx0));
307+
306308
if (mem_per_token == 0) {
307-
mem_per_token = ggml_used_mem(ctx0)/N;
309+
mem_per_token = ggml_used_mem(ctx0) / N; //* 4;
310+
spdlog::debug("Set mem_per_token={} / {} * {} = {}", ggml_used_mem(ctx0), N, 4, mem_per_token);
308311

309312
}
310-
spdlog::debug("used_mem = {}\n", ggml_used_mem(ctx0));
311-
//printf("used_mem = %zu\n", ggml_used_mem(ctx0));
312313

313314
ggml_free(ctx0);
314315

@@ -685,7 +686,13 @@ std::stringstream GPTNEOXModel::predict_impl(std::string prompt, int max_length,
685686

686687
std::vector<float> logits;
687688

688-
gpt_neox_eval((*model), config.n_threads, 0, { 0, 1, 2, 3 }, logits, mem_per_token);
689+
std::vector<gpt_vocab::id> test = {};
690+
691+
for(int i=0;i<64;i++){
692+
test.push_back(i);
693+
}
694+
695+
gpt_neox_eval((*model), config.n_threads, 0, test, logits, mem_per_token);
689696

690697
const int64_t t_start_us = ggml_time_us();
691698

src/starcoder.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bool starcoder_eval(
3737
const int n_head = hparams.n_head;
3838
const int n_vocab = hparams.n_vocab;
3939

40-
static size_t buf_size = 256u*1024*1024;
40+
static size_t buf_size = 512u*1024*1024;
4141
static void * buf = malloc(buf_size);
4242

4343
// use 2 scratch buffers
@@ -48,17 +48,21 @@ bool starcoder_eval(
4848
static size_t scr1_size = 512u*1024*1024;
4949
static void * scr1 = malloc(scr1_size);
5050

51-
if (mem_per_token > 0 && mem_per_token*N > buf_size) {
52-
const size_t buf_size_new = 1.1*(mem_per_token*N); // add 10% to account for ggml object overhead
53-
spdlog::debug("{}: reallocating buffer from {} to {} bytes\n", __func__, buf_size, buf_size_new);
51+
if (mem_per_token > 0 && 2*mem_per_token*N > buf_size) {
52+
const size_t buf_size_new = 2*(mem_per_token*N); // add 10% to account for ggml object overhead
5453

55-
// reallocate
56-
buf_size = buf_size_new;
57-
buf = realloc(buf, buf_size);
58-
if (buf == nullptr) {
59-
spdlog::error("{}: failed to allocate {} bytes\n", __func__, buf_size);
60-
return false;
54+
if(buf_size_new > buf_size){
55+
spdlog::debug("{}: reallocating buffer from {} to {} bytes\n", __func__, buf_size, buf_size_new);
56+
57+
// reallocate
58+
buf_size = buf_size_new;
59+
buf = realloc(buf, buf_size);
60+
if (buf == nullptr) {
61+
spdlog::error("{}: failed to allocate {} bytes\n", __func__, buf_size);
62+
return false;
63+
}
6164
}
65+
6266
}
6367

6468
struct ggml_init_params params = {
@@ -67,6 +71,7 @@ bool starcoder_eval(
6771
/*.no_alloc =*/ false,
6872
};
6973

74+
7075
struct ggml_context * ctx0 = ggml_init(params);
7176
struct ggml_cgraph gf = {};
7277

@@ -338,7 +343,9 @@ bool starcoder_eval(
338343
if (mem_per_token == 0) {
339344
mem_per_token = ggml_used_mem(ctx0)/N;
340345
}
341-
//printf("used_mem = %zu MB\n", ggml_used_mem(ctx0)/(1024*1024));
346+
347+
spdlog::debug("{}: used mem buf={} bytes", __func__, ggml_used_mem(ctx0));
348+
342349

343350
ggml_free(ctx0);
344351

@@ -743,11 +750,22 @@ std::stringstream StarcoderModel::predict_impl(std::string prompt, int max_lengt
743750
size_t mem_per_token = 0;
744751

745752
std::vector<float> logits;
753+
std::vector<gpt_vocab::id> test = {};
754+
755+
for(int i=0;i<64;i++){
756+
test.push_back(i);
757+
}
758+
759+
spdlog::debug("{}: calculate required memory per token", __func__);
760+
starcoder_eval((*model), config.n_threads, 0, test, logits, mem_per_token);
761+
spdlog::debug("{}: mem_per_token={}", __func__, mem_per_token);
762+
spdlog::debug("{}: total mem needed for prompt = {}*{}={}", __func__, embd_inp.size(), mem_per_token, embd_inp.size()*mem_per_token);
746763

747-
starcoder_eval((*model), config.n_threads, 0, { 0, 1, 2, 3 }, logits, mem_per_token);
748764

749765
for (int i = embd.size(); i < embd_inp.size() + n_predict; i++) {
750766
// predict
767+
spdlog::debug("{}: process token #{}: ", __func__, i);
768+
751769
if (embd.size() > 0) {
752770
const int64_t t_start_us = ggml_time_us();
753771

0 commit comments

Comments
 (0)