Phase 2: flash_attn_ext + thread binding for AR engine

- Replace manual attention (permute + mul_mat + soft_max_ext + mul_mat)
  with ggml_flash_attn_ext (fused kernel, online softmax)
- Flash attention handles GQA natively (n_head_kv → n_head broadcast)
  eliminating the ggml_repeat GQA expansion (~5x less KV memory)
- Mask changed from F32 to F16 (required by flash_attn kernel)
- Reduced compute buffer sizing (no GQA expansion memory)
- Added --bind-cores flag for CPU thread affinity (OMP_PROC_BIND)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Carmen EstebanandClaude Opus 4.6 committed 2026-04-05 18:08:27 +02:00
1 parent fb8765fdc7
commit d764edf046
2 files changed
+57 -45

No files matched your search

+20
View File
@@ -6,6 +6,10 @@
#include <string>
#include <vector>
#ifdef _OPENMP
#include <omp.h>
#endif
static void print_usage(const char * prog) {
fprintf(stderr, "Usage: %s [options]\n", prog);
fprintf(stderr, "\ndiffuse-cpp autoregressive inference engine\n");
@@ -21,6 +25,7 @@ static void print_usage(const char * prog) {
fprintf(stderr, " --repeat-last-n INT Repeat penalty window (default: 64)\n");
fprintf(stderr, " --seed INT Random seed (default: 42)\n");
fprintf(stderr, " --tokens IDs Comma-separated pre-tokenized input\n");
fprintf(stderr, " --bind-cores Bind threads to CPU cores (reduces jitter)\n");
fprintf(stderr, "\nExample:\n");
fprintf(stderr, " %s -m thrombia-32b-q4.gguf --tokens 151644,8948,198,... -n 512 -t 12\n", prog);
}
@@ -48,6 +53,7 @@ int main(int argc, char ** argv) {
float repeat_penalty = 1.1f;
int repeat_last_n = 64;
uint32_t seed = 42;
bool bind_cores = false;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-m") == 0 && i + 1 < argc) {
@@ -72,6 +78,8 @@ int main(int argc, char ** argv) {
seed = atoi(argv[++i]);
} else if (strcmp(argv[i], "--tokens") == 0 && i + 1 < argc) {
input_tokens = parse_tokens(argv[++i]);
} else if (strcmp(argv[i], "--bind-cores") == 0) {
bind_cores = true;
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
print_usage(argv[0]);
return 0;
@@ -105,6 +113,18 @@ int main(int argc, char ** argv) {
return 1;
}
// Thread binding for reduced scheduling jitter
if (bind_cores) {
#ifdef _OPENMP
omp_set_num_threads(n_threads);
#endif
// Set environment for child threads (GGML uses pthreads internally)
setenv("OMP_PROC_BIND", "close", 0); // don't override if already set
setenv("OMP_PLACES", "cores", 0);
setenv("GOMP_CPU_AFFINITY", "0-1023", 0);
fprintf(stderr, "Thread binding: enabled (%d threads, close/cores)\n", n_threads);
}
// Load model
fprintf(stderr, "Loading model...\n");
diffuse_model * model = diffuse_model_load(model_path, n_threads);