messing around with memory allocation to bandaid the random ooms with various gpt2 and gptj models

This commit is contained in:
Concedo 2023-04-19 20:18:55 +08:00
parent f662a9a230
commit cc407f283a
2 changed files with 27 additions and 19 deletions

View file

@ -138,8 +138,8 @@ ModelLoadResult gpt2_model_load(const std::string & fname, gpt2_model & model, g
ctx_size += n_layer*(4*n_embd*n_embd*ggml_type_sizef(wtype)); // c_mlp_proj_w ctx_size += n_layer*(4*n_embd*n_embd*ggml_type_sizef(wtype)); // c_mlp_proj_w
ctx_size += n_layer*( n_embd*ggml_type_sizef(GGML_TYPE_F32)); // c_mlp_proj_b ctx_size += n_layer*( n_embd*ggml_type_sizef(GGML_TYPE_F32)); // c_mlp_proj_b
ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32); // memory_k ctx_size += 1.5*(n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32)); // memory_k
ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32); // memory_v ctx_size += 1.5*(n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32)); // memory_v
ctx_size += (6 + 12*n_layer)*256; // object overhead ctx_size += (6 + 12*n_layer)*256; // object overhead
@ -241,8 +241,8 @@ ModelLoadResult gpt2_model_load(const std::string & fname, gpt2_model & model, g
const int n_mem = n_layer*n_ctx; const int n_mem = n_layer*n_ctx;
const int n_elements = n_embd*n_mem; const int n_elements = n_embd*n_mem;
model.memory_k = ggml_new_tensor_1d(ctx, memory_type, n_elements); model.memory_k = ggml_new_tensor_1d(ctx, memory_type, n_elements*1.5);
model.memory_v = ggml_new_tensor_1d(ctx, memory_type, n_elements); model.memory_v = ggml_new_tensor_1d(ctx, memory_type, n_elements*1.5);
const size_t memory_size = ggml_nbytes(model.memory_k) + ggml_nbytes(model.memory_v); const size_t memory_size = ggml_nbytes(model.memory_k) + ggml_nbytes(model.memory_v);
@ -371,19 +371,23 @@ bool gpt2_eval(
const int n_vocab = hparams.n_vocab; const int n_vocab = hparams.n_vocab;
//todo: there is a bug that causes the buffer to oom and I cannot figure it out, hack to increase size for now //todo: there is a bug that causes the buffer to oom and I cannot figure it out, hack to increase size for now
static size_t buf_size = 1280u*1024*1024; static size_t buf_size = 1600u*1024*1024;
static void * buf = malloc(buf_size); static void * buf = malloc(buf_size);
if (mem_per_token > 0 && mem_per_token*N > buf_size) { if (mem_per_token > 0 && mem_per_token*N*1.6 > buf_size) {
const size_t buf_size_new = 2*(mem_per_token*N); // add 10% to account for ggml object overhead const size_t buf_size_new = 2*(mem_per_token*N); // add 10% to account for ggml object overhead
//printf("\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new); //printf("\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new);
// reallocate // reallocate
buf_size = buf_size_new; if (buf_size_new > buf_size)
buf = realloc(buf, buf_size); {
if (buf == nullptr) { buf_size = buf_size_new;
fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size); buf = realloc(buf, buf_size);
return false; if (buf == nullptr)
{
fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size);
return false;
}
} }
} }

View file

@ -382,19 +382,23 @@ bool gptj_eval(
const int d_key = n_embd/n_head; const int d_key = n_embd/n_head;
//todo: there is a bug that causes the buffer to oom and I cannot figure it out, hack to increase size for now //todo: there is a bug that causes the buffer to oom and I cannot figure it out, hack to increase size for now
static size_t buf_size = 1280u*1024*1024; static size_t buf_size = 1600u*1024*1024;
static void * buf = malloc(buf_size); static void * buf = malloc(buf_size);
if (mem_per_token > 0 && mem_per_token*N > buf_size) { if (mem_per_token > 0 && mem_per_token*N*1.4 > buf_size) {
const size_t buf_size_new = 1.5*(mem_per_token*N); // add 10% to account for ggml object overhead const size_t buf_size_new = 1.6*(mem_per_token*N); // add 10% to account for ggml object overhead
//printf("\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new); //printf("\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new);
// reallocate // reallocate
buf_size = buf_size_new; if (buf_size_new > buf_size)
buf = realloc(buf, buf_size); {
if (buf == nullptr) { buf_size = buf_size_new;
fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size); buf = realloc(buf, buf_size);
return false; if (buf == nullptr)
{
fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size);
return false;
}
} }
} }