Skip to content

Commit

Permalink
Do not quantize activations if not necessary (#79)
Browse files Browse the repository at this point in the history
* Do not quantize activations if not necessary

* Do not quantize activations if not necessary also for MoE models

---------

Co-authored-by: Iwan Kawrakow <[email protected]>
  • Loading branch information
2 people authored and Nexesenex committed Oct 22, 2024
1 parent c394889 commit f270120
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions ggml/include/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ extern "C" {
// since https://github.com/ggerganov/ggml/issues/287
struct ggml_cplan {
size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
size_t q_size;
uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`

int n_threads;
Expand Down
59 changes: 46 additions & 13 deletions ggml/src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,7 @@ struct ggml_compute_params {

// work buffer for all threads
size_t wsize;
size_t qsize;
void * wdata;

struct ggml_threadpool * threadpool;
Expand Down Expand Up @@ -12768,13 +12769,18 @@ UseGgmlGemm1:;
#endif

if (src1->type != vec_dot_type) {
char * wdata = params->wdata;
char * wdata = (char *)params->wdata + params->wsize - params->qsize;

if (strncmp(src1->name, wdata - GGML_MAX_NAME, GGML_MAX_NAME) == 0) {
goto AlreadyQunatized;
}
wdata += GGML_MAX_NAME;

const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
const size_t nbw2 = nbw1*ne11;
const size_t nbw3 = nbw2*ne12;

assert(params->wsize >= ne13*nbw3);
assert(params->qsize >= ne13*nbw3);
GGML_ASSERT(src1->type == GGML_TYPE_F32);

for (int64_t i13 = 0; i13 < ne13; ++i13) {
Expand All @@ -12798,11 +12804,18 @@ UseGgmlGemm1:;
}

if (ith == 0) {
wdata -= GGML_MAX_NAME;
memcpy(wdata, src1->name, GGML_MAX_NAME);
// Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start.
atomic_store_explicit(&params->threadpool->current_chunk, nth, memory_order_relaxed);
}

AlreadyQunatized:;
ggml_barrier(params->threadpool);
}

const void * wdata = (src1->type == vec_dot_type) ? src1->data
: (const void *)((const char *)params->wdata + params->wsize - params->qsize + GGML_MAX_NAME);

#if GGML_USE_LLAMAFILE
if (src1->type != vec_dot_type) {
Expand Down Expand Up @@ -12953,9 +12966,10 @@ static void ggml_compute_forward_mul_mat_id(
const int n_ids = ids->ne[0]; // n_expert_used
const int n_as = ne02; // n_expert

char * wdata_src1_end = (src1->type == vec_dot_type) ?
(char *) params->wdata :
(char *) params->wdata + GGML_PAD(ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));
char * qdata = (char *)params->wdata + params->wsize - params->qsize;

char * wdata_src1_end = (src1->type == vec_dot_type) ? qdata :
qdata + GGML_PAD(GGML_MAX_NAME + ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));

struct mmid_row_mapping {
int32_t i1;
Expand All @@ -12965,14 +12979,19 @@ static void ggml_compute_forward_mul_mat_id(
int64_t * matrix_row_counts = (int64_t *) (wdata_src1_end); // [n_as]
struct mmid_row_mapping * matrix_rows = (struct mmid_row_mapping *)(matrix_row_counts + n_as); // [n_as][ne11]

bool store_name = false;
if (src1->type != vec_dot_type) {
char * wdata = params->wdata;
if (strncmp(src1->name, qdata, GGML_MAX_NAME) == 0) {
goto QuantizationAlreadyDone;
}
store_name = true;
char * wdata = qdata + GGML_MAX_NAME;

const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
const size_t nbw2 = nbw1*ne11;
const size_t nbw3 = nbw2*ne12;

assert(params->wsize >= ne13*nbw3);
assert(params->qsize >= ne13*nbw3);
GGML_ASSERT(src1->type == GGML_TYPE_F32);

for (int64_t i13 = 0; i13 < ne13; ++i13) {
Expand All @@ -12988,7 +13007,12 @@ static void ggml_compute_forward_mul_mat_id(

#define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ne12 + (i1)]

QuantizationAlreadyDone:;
if (ith == 0) {
if (store_name) {
memcpy(qdata, src1->name, GGML_MAX_NAME);
}

// initialize matrix_row_counts
memset(matrix_row_counts, 0, n_as*sizeof(int64_t));

Expand Down Expand Up @@ -13017,7 +13041,7 @@ static void ggml_compute_forward_mul_mat_id(

const char * src0_cur = (const char *) src0->data + cur_a*nb02;

const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
const void * wdata = (src1->type == vec_dot_type) ? src1->data : qdata + GGML_MAX_NAME;
const size_t row_size = ggml_row_size(vec_dot_type, ne10);

const int64_t nr0 = ne01; // src0 rows
Expand Down Expand Up @@ -19963,6 +19987,7 @@ struct ggml_cplan ggml_graph_plan(
}

size_t work_size = 0;
size_t q_size = 0;

struct ggml_cplan cplan;
memset(&cplan, 0, sizeof(struct ggml_cplan));
Expand All @@ -19978,6 +20003,7 @@ struct ggml_cplan ggml_graph_plan(
max_tasks = MAX(max_tasks, n_tasks);

size_t cur = 0;
size_t cur_q = 0;

switch (node->op) {
case GGML_OP_CPY:
Expand Down Expand Up @@ -20017,7 +20043,7 @@ struct ggml_cplan ggml_graph_plan(
} else
#endif
if (node->src[1]->type != vec_dot_type) {
cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
cur_q = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
}
} break;
case GGML_OP_MUL_MAT_ID:
Expand All @@ -20027,12 +20053,12 @@ struct ggml_cplan ggml_graph_plan(
const struct ggml_tensor * src1 = node->src[1];
const enum ggml_type vec_dot_type = type_traits[src0->type].vec_dot_type;
if (src1->type != vec_dot_type) {
cur += ggml_row_size(vec_dot_type, ggml_nelements(src1));
cur_q += ggml_row_size(vec_dot_type, ggml_nelements(src1));
}
const int n_as = src0->ne[2];
cur += GGML_PAD(cur, sizeof(int64_t)); // align
cur += n_as * sizeof(int64_t); // matrix_row_counts
cur += n_as * src1->ne[2] * sizeof(int64_t); // matrix_rows
cur_q += GGML_PAD(cur, sizeof(int64_t)); // align
cur_q += n_as * sizeof(int64_t); // matrix_row_counts
cur_q += n_as * src1->ne[2] * sizeof(int64_t); // matrix_rows
} break;
case GGML_OP_OUT_PROD:
{
Expand Down Expand Up @@ -20121,15 +20147,21 @@ struct ggml_cplan ggml_graph_plan(
}

work_size = MAX(work_size, cur);
q_size = MAX(q_size, cur_q);
}

if (work_size > 0) {
work_size += CACHE_LINE_SIZE*(n_threads);
}
if (q_size > 0) {
q_size += GGML_MAX_NAME;
}
work_size += q_size;

cplan.threadpool = threadpool;
cplan.n_threads = MIN(max_tasks, n_threads);
cplan.work_size = work_size;
cplan.q_size = q_size;
cplan.work_data = NULL;

return cplan;
Expand All @@ -20148,6 +20180,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
/*.ith =*/ state->ith,
/*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
/*.wsize =*/ cplan->work_size,
/*.qsize =*/ cplan->q_size,
/*.wdata =*/ cplan->work_data,
/*.threadpool=*/ tp,
};
Expand Down

0 comments on commit f270120

Please sign in to comment.