ds4
ds4 copied to clipboard
engram speedup patch achieved 28 tok sec deepseek 4.1 flash on m3 ultra 512 gb
achieved 28 tok sec deepseek 4.1 flash on m3 ultra 512 gb
ds4-engram-batch-read.patch diff --git a/ds4.c b/ds4.c index aafcb44..0d78477 100644 --- a/ds4.c +++ b/ds4.c @@ -42967,21 +42967,69 @@ static DS4_MAYBE_UNUSED bool ds41_graph_step_batch(ds41_gpu_graph *const *graphs ok = queries[i] && heads[i]; } const float initial_pre[] = {1, 0, 0, 0};
- /* Hash every row first, then fetch all Engram rows for the step in one
-
* parallel batch. The previous per-row loop issued 48 serial 264-byte -
* preads per token (288 for a six-row DSpark verify), which showed up as -
* roughly a quarter of decode wall time. */ - uint32_t (*engram_ids)[2][DS4_ENGRAM_COLS] = NULL;
- float *engram_batch_out[2] = {NULL, NULL};
- if (ok) {
-
engram_ids = malloc((size_t)count * sizeof(*engram_ids)); -
if (!engram_ids) { -
ok = false; -
} else if (count > 1) { -
for (unsigned table = 0; table < 2; table++) -
engram_batch_out[table] = malloc((size_t)count * DS4_ENGRAM_COLS * -
DS4_ENGRAM_DIM * sizeof(float)); -
if (!engram_batch_out[0] || !engram_batch_out[1]) ok = false; -
} - } for (int i = 0; ok && i < count; i++) { ds41_gpu_graph *s = graphs[i];
-
uint32_t ids[2][DS4_ENGRAM_COLS]; const bool continued = i > 0 && (uint32_t)i < prefill_rows; positions[i] = s->pos + (continued ? (uint32_t)i : 0u); history[i] = continued ? history[i - 1] : s->history; -
ok = ds4_engram_hash(&s->engram, &history[i], &tokens[i], NULL, 1, &ids[0][0]);
-
ok = ds4_engram_hash(&s->engram, &history[i], &tokens[i], NULL, 1, -
&engram_ids[i][0][0]); - }
- if (ok && count > 1) {
-
uint32_t *flat = malloc((size_t)count * DS4_ENGRAM_COLS * sizeof(*flat)); -
if (!flat) { -
ok = false; -
} else { -
for (unsigned table = 0; ok && table < 2; table++) { -
for (int i = 0; i < count; i++) -
memcpy(flat + (size_t)i * DS4_ENGRAM_COLS, -
engram_ids[i][table], -
DS4_ENGRAM_COLS * sizeof(*flat)); -
ok = ds4_engram_read_batch(&graphs[0]->table[table], flat, -
(size_t)count, DS4_ENGRAM_COLS, engram_batch_out[table]); -
} -
free(flat); -
} - }
- for (int i = 0; ok && i < count; i++) {
-
ds41_gpu_graph *s = graphs[i]; float (*disk_rows)[DS4_ENGRAM_COLS * DS4_ENGRAM_DIM] = engram && (uint32_t)i < prefill_rows ? engram[i] : s->rows;
-
for (unsigned table = 0; ok && table < 2; table++) -
ok = ds4_engram_read(&s->table[table], ids[table], DS4_ENGRAM_COLS, disk_rows[table]);
-
if (count > 1) { -
for (unsigned table = 0; table < 2; table++) -
memcpy(disk_rows[table], -
engram_batch_out[table] + -
(size_t)i * DS4_ENGRAM_COLS * DS4_ENGRAM_DIM, -
DS4_ENGRAM_COLS * DS4_ENGRAM_DIM * sizeof(float)); -
} else { -
for (unsigned table = 0; ok && table < 2; table++) -
ok = ds4_engram_read(&s->table[table], engram_ids[i][table], -
DS4_ENGRAM_COLS, disk_rows[table]); -
}} if (ok) ok = ds4_gpu_tensor_write(g->rows_view[i].pre, 0, initial_pre, sizeof(initial_pre)) && ds4_gpu_embed_token_hc_tensor(g->rows_view[i].residual, model->map, model->size, weights->token_embd->abs_offset, DS4_N_VOCAB, (uint32_t)tokens[i], DS4_N_EMBD, DS4_N_HC); - free(engram_ids);
- free(engram_batch_out[0]);
- free(engram_batch_out[1]); if (ok) ok = ds4_gpu_tensor_write(g->prefill_tokens, 0, tokens, rows * sizeof(int)); uint32_t il = 0; for (; ok && il < DS4_N_LAYER; il++) {
mac studio still have 150gb memory left.is there a way to make better dspark that is bigger to increase token generation?
check this fork,https://github.com/Chida82/sf-ds4-1flash