engram speedup patch achieved 28 tok sec deepseek 4.1 flash on m3 ultra 512 gb

Aperta
#1,240 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c

Direzione di ricerca

The report includes a proposed change in ds4.c, inside ds41_graph_step_batch, that batches Engram row reads. Start by reviewing this function and the existing ds4_engram_read_batch implementation, then check the two comments for discussion or concerns. Done means the speedup is verified and the batch path works correctly across the relevant decode and prefill cases.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

achieved 28 tok sec deepseek 4.1 flash on m3 ultra 512 gb

Image

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++) {
Lingua principale
C
Stelle
24.4k
Fork
2.4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di antirez/ds4

Tutte le issue di antirez/ds4

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.