Skip to content

metal: add opt-in slab residency for DeepSeek V4.1 Flash (M2 192GB 0.25tk/s -> 13tk/s) - #1033

Open
Dango233 wants to merge 5 commits into
antirez:mainfrom
Dango233:perf/metal-streaming-slab-residency
Open

Dango233 wants to merge 5 commits into
antirez:mainfrom
Dango233:perf/metal-streaming-slab-residency

Conversation

@Dango233

@Dango233 Dango233 commented Sep 13, 2026

Copy link
Copy Markdown

This attaches owned SSD expert-cache slabs to a queue residency set on macOS 15+. The switch is off by default. Memory-pressure relief removes the set until the cache is rebuilt. This addresses the multi-second command submission delay reproduced with large slabs on this M2 Ultra.

Enable with DS4_METAL_STREAMING_SLAB_RESIDENCY=1.

Revalidation against current upstream

Measured on 2026-09-14 after merging a04f46fa423e45712c8c7e430eff422479f314a3 (DeepSeek V4.1 CUDA support). M2 Ultra, 192 GiB, macOS 15.7.4, Metal; calibrated V4.1 Flash IQ2_XXS/Q2_K. Same binary per comparison, 2,048-token prompt, 8,257 allocated context, automatic expert cache, fresh engine per run; OS/file cache was not flushed.

Mean 8-token decode: 0.240 → 9.310 tokens/s (38.8×).

Run Switch Prefill t/s Decode t/s
1 off 72.13 0.24
2 on 67.99 9.35
3 on 67.81 9.27
4 off 61.97 0.24

All four decoded outputs match. These are two observations per variant on one host. The short decode includes the first-token cost; candidate steady decode is 13.41, 13.31 tokens/s. Original 2026-09-13 measurements remain in the report.

Validation and limits

Slab lifecycle and pressure-relief tests, SSD expert kernels, MoE prefill, admission/address checks, and the real-model session fixture pass under Metal API validation.

Clean Metal build, CPU compilation, frontend/Engram/GGUF/quality-tool unit tests and seven targeted Metal kernel subtests pass. The full Metal kernel suite is not green: the new router check fails at tests/test_deepseek41_metal.c:105; pristine upstream reproduces the exact same actual/reference values. No kernel or tolerance was changed to bypass it.

The two existing SDK 15 unused Metal 4 symbol warnings remain. Full legacy make test and CUDA/ROCm hardware validation were not run.

Commands, model hash, raw CSVs and full results.

@Dango233 Dango233 changed the title DeepSeek V4.1 Flash: opt-in Metal streaming slab residency metal: add opt-in slab residency for DeepSeek V4.1 Flash Sep 13, 2026
@Dango233 Dango233 changed the title metal: add opt-in slab residency for DeepSeek V4.1 Flash metal: add opt-in slab residency for DeepSeek V4.1 Flash (M2 192GB 0.25tk/s -> 13tk/s) Sep 13, 2026

@emilianbold emilianbold left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how small the actual change is. Some minor comments below.

Comment thread ds4_metal.m
!g_stream_expert_cache_mlock_relief_applied) {
#if TARGET_OS_OSX
if (@available(macOS 15.0, *)) {
const BOOL fresh = g_stream_slab_residency_set == nil;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the fresh bool logic hard to read. It's basically an if/else on g_stream_slab_residency_set == nil with the else just adding the new buffer to the existing residency set.

Comment thread ds4_metal.m
if (!g_stream_slab_residency_set) {
fprintf(stderr, "ds4: streaming slab residency set failed: %s\n",
[[error localizedDescription] UTF8String]);
return nil;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat debatable to me why can't we just return the usable buffer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants