[PR #344] [MERGED] DeepSeek-V4.1 tensor parallel on 9 GPUs: prompts ~1.9x and answers ~1.55x faster at --tp 8 than main, the same perplexity (all-reduces off the SMs, the prompt skip on several GPUs, the expert cache's recent slots); ./maya.sh --calibr… #477

Closed
opened 2026-10-29 21:25:51 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/344
Author: @needmorevram
Created: 10/21/2026
Status: ✅ Merged
Merged: 10/22/2026
Merged by: @mw00

Base: main ← Head: ds4-perf


📝 Commits (2)

  • 67f69d1 maya-ds4: tensor parallel on several GPUs (ds4-perf, rebased on v1.1.21)
  • 0fe616a maya-ds4: the review's build fixes; one GPU's decode on HIP as main's; --tp 1 keeps prompt chunks

📊 Changes

23 files changed (+3437 additions, -171 deletions)

View changed files

📝 CMakeLists.txt (+3 -2)
📝 docs/SETUP.md (+5 -1)
📝 maya.py (+80 -9)
📝 serve/gpu_map.py (+15 -7)
📝 serve/test_dashboard.py (+24 -0)
📝 serve/web/app.css (+5 -3)
📝 serve/web/app.js (+6 -1)
➕ src/ds4/ds4_host_ar.cpp (+85 -0)
📝 src/ds4/ds4_kernels.cu (+904 -18)
📝 src/ds4/ds4_kernels.cuh (+81 -7)
📝 src/ds4/ds4_main.cpp (+105 -8)
📝 src/ds4/ds4_model.cu (+958 -104)
📝 src/ds4/ds4_model.hpp (+2 -1)
📝 src/kernels/cuda/glm_fast.cu (+5 -2)
➕ tests/ds4_allreduce_dma.cu (+141 -0)
➕ tests/ds4_ar_latency.cu (+75 -0)
➕ tests/ds4_index_score_tc.cu (+143 -0)
➕ tests/ds4_topk_order.cu (+97 -0)
➕ tools/calibrate_ds4_tp.py (+276 -0)
📝 tools/ds4/pack.py (+63 -5)

...and 3 more files

📄 Description

DeepSeek-V4.1-Flash-1017 with tensor parallel on several GPUs. This is the part of #276 that main doesn't have yet, rebased on main v1.1.21 (one commit, plus one for the review's fixes). At --tp 8 (3.5 bits) it reads long prompts about 1.9x faster than main v1.1.20 and answers about 1.55x faster, with the same perplexity.

Results

One PC with 9 GPUs, each on PCIe 3.0 x8, and 2 NUMA nodes. LibertAIDAI's 3.5-bit EXL3 quant at --tp 8 --prefill-chunk 2048. Measured on the v1.1.20 merge, before the rebase; the one-GPU decode path after the review's fixes is in the comments.

The serve bench runs through the server's protocol, sampling at temperature 1.0 with DSpark on. It reads 2 fresh ~16K-token prompts and writes 12 chat answers of 256 tokens. Perplexity uses --ppl over 23 sequences of 2048 tokens (WikiText-2, this repo's code, its docs).

prefill (2 x 16K tokens) decode (12 x 256) perplexity (47,081 tokens)
main v1.1.20 424 / 435 tok/s 42.9 tok/s 3.4114
this branch before the merge 801 / 839 tok/s 66.4 tok/s 3.4133
this PR 785 / 818 tok/s 66.8 tok/s 3.4081

The three perplexities are within 0.15% of each other. Single sequences move by about 0.4% in both directions, mostly from main's F32 router flipping borderline expert picks.

What it adds

maya-ds4: tensor parallel

  • A prompt chunk's all-reduce off the SMs.
    • Three or more members: through the copy engines (allreduce_dma), in pieces with the copies on a stream of their own.
    • Five or more: summed on the host (HostReducer), its rows pinned on each GPU's NUMA node. Settings: DS4_AR_HOST, DS4_AR_HOST_THREADS, DS4_AR_HOST_PIECES; DS4_AR_DMA=0 turns both off.
    • Every variant gives the same FP16 sums as ar_rs16_kernel, bit for bit (tests/ds4_allreduce_dma.cu). The loads are HIP-safe, and the host rows build on Windows too.
  • The attention's all-reduce slab by slab, each slab's sum on its own stream while the next slab computes (DS4_AR_OVERLAP, on).
  • The long-prompt skip on several GPUs.
    • Main's MAYA_DS4_PROMPT_SKIP (layers 20-39 run over the prompt's tail only) required every layer on one GPU.
    • With tensor parallel or several groups, the cut now falls at a chunk's start.
    • A skipped chunk still hands its compressed-KV and index-key rows to the other groups, and the hand-offs skip the chunks that don't run.
    • One GPU keeps main's row-level cut.
  • A tensor-parallel group's Engram rows read once. The group's first member reads each chunk's rows; the other members copy from its pinned slots instead of reading the same rows themselves (8 times at --tp 8). DS4_EG_SHARE=0 gives every GPU its own reads, as main does.
  • Multi-GPU defaults:
    • 4096-token prompt chunks (one GPU keeps main's 2048 or chunk_default).
    • 4 expert-cache threads (one GPU keeps 1).

maya-ds4: the expert cache and placement

  • Recent slots (DS4_RECENT, 32 a layer where prompts stream the experts):
    • VRAM slots for CPU-lane experts a layer used twice in its last 8 ticks.
    • Filled by DMA from a thread of their own, off the windows' path.
    • Tuning: DS4_RECENT_FILLS, DS4_RECENT_NEED, DS4_RECENT_WIN.
    • DS4_CHECK_SWAPS checks each fill against the model file.
  • Cache copies held until a group's turn is over (DS4_COPY_IDLE, on). Over a GPU's PCIe link they slowed a window's all-reduces about 6x.
  • A tick's table writes in one launch a device (tab_set): swaps and recent slots, instead of one 8-byte copy each.
  • Experts placed by the profile.
    • The drafter's: each block's experts that take 99% of its routes, plus more while the lead keeps the next tightest member's room.
    • The trunk's: by each layer's routing share per byte, smoothed a tenth toward even.
    • DS4_PLACE_UNIFORM gives main's even split.
    • Main's Windows commit cap still applies to each member's room.

maya-ds4: decode and kernels

  • The residual stream's mixes (hc_mix / hc_split) on a second stream beside the attention and the MoE. The results are read only where they were before.
  • The indexer's top-k over segments for long rows (TopkScratch): the same picks in the same order, with a long row's passes spread over blocks (164 -> 78 µs at 64K positions). Checked by tests/ds4_topk_order.cu; DS4_TOPK_CHECK checks it live.
  • Speculative rollback: the compressors' state is restored in one launch per device (comp_restore_batch).
  • Smaller kernel fixes:
    • markov_pick reads the members' slots in parallel.
    • moe_combine leaves early for a token with no rows in the set.
    • moe_win_group reads its plan with every thread (GLM's kernel too: the same result).
  • Opt-in: the indexer's scores on the tensor cores (DS4_INDEX_TC=1).
    • The integer products are exact; the FP32 sums run in another order.
    • It reads main's 16-byte-padded key rows. tests/ds4_index_score_tc.cu gives the same masks and the same top 512 as the CUDA cores; DS4_INDEX_CHECK checks it live.

maya-ds4: CLI and diagnostics

  • --ppl FILE: one line is a sequence (BOS + text), scored in one full-sequence forward.
  • --ids @FILE (no argv limit for long prompts).
  • DS4_BENCH_REPEAT (warm passes in one load).
  • DS4_PROFILE / DS4_PROFILE_PROMPT: an nsys capture of the decode, or of the prompt, alone.
  • DS4_DUMP_LAST, DS4_CPU_STATS, DS4_ROUTE_TRACE.
  • A deterministic cuBLAS workspace (CUBLAS_WORKSPACE_CONFIG), so a long prompt's logits are the same from one start to the next.
  • The serve log's CPU-lane share is per request, and DONE reports the decode's VRAM hits and lookups.

Monitor

  • "What fills the GPUs" gets maya-ds4's VRAM lines, including each GPU's prompt buffers and tensor parallel's copies (serve/gpu_map.py, app.js, app.css, test_dashboard.py).

Setup and tools

  • ./maya.sh --calibrate on a DeepSeek config (tools/calibrate_ds4_tp.py):
    • It measures decode with each --tp that groups the GPUs differently, and with fewer CPU threads, one engine start each.
    • A setting is kept only when it's more than 5% faster and still reads a long prompt at 60% of the config's own speed.
    • The result goes into the config and calibration.json for this PC and quant, and a setup again reuses it.
    • It sits beside main's DSpark calibration (tools/calibrate_ds4.py, unchanged).
  • tools/ds4/pack.py:
    • The pack takes the model repo's own chat_template.jinja (a port of DeepSeek's encoding.py); this directory's copy is used only for a repo without one.
    • The repo's exl3_k_map.json is checked against every EXL3 matrix's trellis (codebook mul1, K from its shape).

Where main had its own version

  • Main's code kept: the FP4 KV format, sparse attention, the F32 router, the Engram I/O and row cache, sampled drafts and their verification, the draft-count rule (dspark_cum), the chunk path's checkpoints, and Stop.
  • This branch's behaviour carried over onto main's code: the prompt skip on several GPUs, the shared Engram reads, and the multi-GPU defaults above.
  • Dropped: --prefill-keep, DS4_DRAFT_SAMPLE, DS4_DRAFT_COST, DS4_AR_DEFER, DS4_CHUNK_FIRST and DS4_EG_URING. Main covers each of them, or they weren't wins.

Exactness

  • Everything on by default keeps the arithmetic: the all-reduces sum as ar_rs16 does, the top-k picks the same rows in the same order, the Engram rows are the same bytes, and the prompt skip is main's exact rule.
  • The only opt-in that changes summing order is DS4_INDEX_TC.
  • Measured perplexity is in the table above.

Tests

  • ds4_gguf_test: ALL OK.
  • tests/ds4_topk_order, ds4_allreduce_dma (4 and 8 members) and ds4_index_score_tc: PASS.
  • tools/test_ds4_pack, test_calibrate_ds4_tp and test_setup_ds4 pass.
  • The serve and tools suites fail only where main also fails on this PC: test_responses_schema, test_hip_archs (no cmake on PATH), test_iq_pack.
  • The engine builds for sm_86 and sm_120.
  • The Windows and HIP builds are not tried here. The host all-reduce's Linux-only calls (mmap / mbind / /sys) are guarded.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/344 **Author:** [@needmorevram](https://github.com/needmorevram) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/22/2026 **Merged by:** [@mw00](https://github.com/mw00) **Base:** `main` ← **Head:** `ds4-perf` --- ### 📝 Commits (2) - [`67f69d1`](https://github.com/mw00/project-maya/commit/67f69d1e87608b0457ad1559429851ce4b94c3b2) maya-ds4: tensor parallel on several GPUs (ds4-perf, rebased on v1.1.21) - [`0fe616a`](https://github.com/mw00/project-maya/commit/0fe616adb02cf51cb86a2ec211f7cb1860c716dc) maya-ds4: the review's build fixes; one GPU's decode on HIP as main's; --tp 1 keeps prompt chunks ### 📊 Changes **23 files changed** (+3437 additions, -171 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+3 -2) 📝 `docs/SETUP.md` (+5 -1) 📝 `maya.py` (+80 -9) 📝 `serve/gpu_map.py` (+15 -7) 📝 `serve/test_dashboard.py` (+24 -0) 📝 `serve/web/app.css` (+5 -3) 📝 `serve/web/app.js` (+6 -1) ➕ `src/ds4/ds4_host_ar.cpp` (+85 -0) 📝 `src/ds4/ds4_kernels.cu` (+904 -18) 📝 `src/ds4/ds4_kernels.cuh` (+81 -7) 📝 `src/ds4/ds4_main.cpp` (+105 -8) 📝 `src/ds4/ds4_model.cu` (+958 -104) 📝 `src/ds4/ds4_model.hpp` (+2 -1) 📝 `src/kernels/cuda/glm_fast.cu` (+5 -2) ➕ `tests/ds4_allreduce_dma.cu` (+141 -0) ➕ `tests/ds4_ar_latency.cu` (+75 -0) ➕ `tests/ds4_index_score_tc.cu` (+143 -0) ➕ `tests/ds4_topk_order.cu` (+97 -0) ➕ `tools/calibrate_ds4_tp.py` (+276 -0) 📝 `tools/ds4/pack.py` (+63 -5) _...and 3 more files_ </details> ### 📄 Description DeepSeek-V4.1-Flash-1017 with tensor parallel on several GPUs. This is the part of #276 that main doesn't have yet, rebased on main v1.1.21 (one commit, plus one for the review's fixes). At `--tp 8` (3.5 bits) it reads long prompts about 1.9x faster than main v1.1.20 and answers about 1.55x faster, with the same perplexity. ## Results One PC with 9 GPUs, each on PCIe 3.0 x8, and 2 NUMA nodes. LibertAIDAI's 3.5-bit EXL3 quant at `--tp 8 --prefill-chunk 2048`. Measured on the v1.1.20 merge, before the rebase; the one-GPU decode path after the review's fixes is in the comments. The serve bench runs through the server's protocol, sampling at temperature 1.0 with DSpark on. It reads 2 fresh ~16K-token prompts and writes 12 chat answers of 256 tokens. Perplexity uses `--ppl` over 23 sequences of 2048 tokens (WikiText-2, this repo's code, its docs). | | prefill (2 x 16K tokens) | decode (12 x 256) | perplexity (47,081 tokens) | |---|---|---|---| | main v1.1.20 | 424 / 435 tok/s | 42.9 tok/s | 3.4114 | | this branch before the merge | 801 / 839 tok/s | 66.4 tok/s | 3.4133 | | **this PR** | **785 / 818 tok/s** | **66.8 tok/s** | **3.4081** | The three perplexities are within 0.15% of each other. Single sequences move by about 0.4% in both directions, mostly from main's F32 router flipping borderline expert picks. ## What it adds ### maya-ds4: tensor parallel - **A prompt chunk's all-reduce off the SMs.** - Three or more members: through the copy engines (`allreduce_dma`), in pieces with the copies on a stream of their own. - Five or more: summed on the host (`HostReducer`), its rows pinned on each GPU's NUMA node. Settings: `DS4_AR_HOST`, `DS4_AR_HOST_THREADS`, `DS4_AR_HOST_PIECES`; `DS4_AR_DMA=0` turns both off. - Every variant gives the same FP16 sums as `ar_rs16_kernel`, bit for bit (`tests/ds4_allreduce_dma.cu`). The loads are HIP-safe, and the host rows build on Windows too. - **The attention's all-reduce slab by slab**, each slab's sum on its own stream while the next slab computes (`DS4_AR_OVERLAP`, on). - **The long-prompt skip on several GPUs.** - Main's `MAYA_DS4_PROMPT_SKIP` (layers 20-39 run over the prompt's tail only) required every layer on one GPU. - With tensor parallel or several groups, the cut now falls at a chunk's start. - A skipped chunk still hands its compressed-KV and index-key rows to the other groups, and the hand-offs skip the chunks that don't run. - One GPU keeps main's row-level cut. - **A tensor-parallel group's Engram rows read once.** The group's first member reads each chunk's rows; the other members copy from its pinned slots instead of reading the same rows themselves (8 times at `--tp 8`). `DS4_EG_SHARE=0` gives every GPU its own reads, as main does. - **Multi-GPU defaults:** - 4096-token prompt chunks (one GPU keeps main's 2048 or `chunk_default`). - 4 expert-cache threads (one GPU keeps 1). ### maya-ds4: the expert cache and placement - **Recent slots** (`DS4_RECENT`, 32 a layer where prompts stream the experts): - VRAM slots for CPU-lane experts a layer used twice in its last 8 ticks. - Filled by DMA from a thread of their own, off the windows' path. - Tuning: `DS4_RECENT_FILLS`, `DS4_RECENT_NEED`, `DS4_RECENT_WIN`. - `DS4_CHECK_SWAPS` checks each fill against the model file. - **Cache copies held until a group's turn is over** (`DS4_COPY_IDLE`, on). Over a GPU's PCIe link they slowed a window's all-reduces about 6x. - **A tick's table writes in one launch a device** (`tab_set`): swaps and recent slots, instead of one 8-byte copy each. - **Experts placed by the profile.** - The drafter's: each block's experts that take 99% of its routes, plus more while the lead keeps the next tightest member's room. - The trunk's: by each layer's routing share per byte, smoothed a tenth toward even. - `DS4_PLACE_UNIFORM` gives main's even split. - Main's Windows commit cap still applies to each member's room. ### maya-ds4: decode and kernels - **The residual stream's mixes (hc_mix / hc_split) on a second stream** beside the attention and the MoE. The results are read only where they were before. - **The indexer's top-k over segments** for long rows (`TopkScratch`): the same picks in the same order, with a long row's passes spread over blocks (164 -> 78 µs at 64K positions). Checked by `tests/ds4_topk_order.cu`; `DS4_TOPK_CHECK` checks it live. - **Speculative rollback:** the compressors' state is restored in one launch per device (`comp_restore_batch`). - **Smaller kernel fixes:** - `markov_pick` reads the members' slots in parallel. - `moe_combine` leaves early for a token with no rows in the set. - `moe_win_group` reads its plan with every thread (GLM's kernel too: the same result). - **Opt-in: the indexer's scores on the tensor cores** (`DS4_INDEX_TC=1`). - The integer products are exact; the FP32 sums run in another order. - It reads main's 16-byte-padded key rows. `tests/ds4_index_score_tc.cu` gives the same masks and the same top 512 as the CUDA cores; `DS4_INDEX_CHECK` checks it live. ### maya-ds4: CLI and diagnostics - `--ppl FILE`: one line is a sequence (BOS + text), scored in one full-sequence forward. - `--ids @FILE` (no argv limit for long prompts). - `DS4_BENCH_REPEAT` (warm passes in one load). - `DS4_PROFILE` / `DS4_PROFILE_PROMPT`: an nsys capture of the decode, or of the prompt, alone. - `DS4_DUMP_LAST`, `DS4_CPU_STATS`, `DS4_ROUTE_TRACE`. - A deterministic cuBLAS workspace (`CUBLAS_WORKSPACE_CONFIG`), so a long prompt's logits are the same from one start to the next. - The serve log's CPU-lane share is per request, and DONE reports the decode's VRAM hits and lookups. ### Monitor - "What fills the GPUs" gets maya-ds4's VRAM lines, including each GPU's prompt buffers and tensor parallel's copies (`serve/gpu_map.py`, `app.js`, `app.css`, `test_dashboard.py`). ### Setup and tools - **`./maya.sh --calibrate` on a DeepSeek config** (`tools/calibrate_ds4_tp.py`): - It measures decode with each `--tp` that groups the GPUs differently, and with fewer CPU threads, one engine start each. - A setting is kept only when it's more than 5% faster and still reads a long prompt at 60% of the config's own speed. - The result goes into the config and `calibration.json` for this PC and quant, and a setup again reuses it. - It sits beside main's DSpark calibration (`tools/calibrate_ds4.py`, unchanged). - **`tools/ds4/pack.py`:** - The pack takes the model repo's own `chat_template.jinja` (a port of DeepSeek's `encoding.py`); this directory's copy is used only for a repo without one. - The repo's `exl3_k_map.json` is checked against every EXL3 matrix's trellis (codebook mul1, K from its shape). ## Where main had its own version - **Main's code kept:** the FP4 KV format, sparse attention, the F32 router, the Engram I/O and row cache, sampled drafts and their verification, the draft-count rule (`dspark_cum`), the chunk path's checkpoints, and Stop. - **This branch's behaviour carried over onto main's code:** the prompt skip on several GPUs, the shared Engram reads, and the multi-GPU defaults above. - **Dropped:** `--prefill-keep`, `DS4_DRAFT_SAMPLE`, `DS4_DRAFT_COST`, `DS4_AR_DEFER`, `DS4_CHUNK_FIRST` and `DS4_EG_URING`. Main covers each of them, or they weren't wins. ## Exactness - Everything on by default keeps the arithmetic: the all-reduces sum as `ar_rs16` does, the top-k picks the same rows in the same order, the Engram rows are the same bytes, and the prompt skip is main's exact rule. - The only opt-in that changes summing order is `DS4_INDEX_TC`. - Measured perplexity is in the table above. ## Tests - `ds4_gguf_test`: ALL OK. - `tests/ds4_topk_order`, `ds4_allreduce_dma` (4 and 8 members) and `ds4_index_score_tc`: PASS. - `tools/test_ds4_pack`, `test_calibrate_ds4_tp` and `test_setup_ds4` pass. - The serve and tools suites fail only where main also fails on this PC: `test_responses_schema`, `test_hip_archs` (no `cmake` on PATH), `test_iq_pack`. - The engine builds for sm_86 and sm_120. - The Windows and HIP builds are not tried here. The host all-reduce's Linux-only calls (mmap / mbind / `/sys`) are guarded. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:25:51 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#477
No description provided.