Skip to content

deepseek41 : add core DeepSeek V4.1 runtime (arch, loader, Engram, expert store, memory admission, watchdog) - #68

Closed
ajaxdude wants to merge 7 commits into
masterfrom
jeromecoste-microsoft-deepseek-v41-core-runtime
Closed

ajaxdude wants to merge 7 commits into
masterfrom
jeromecoste-microsoft-deepseek-v41-core-runtime

Conversation

@ajaxdude

Copy link
Copy Markdown
Member

Overview

This is Layer 1 (PR A) of a two-PR stack reviving closed PR #59 ("trace : recover DeepSeek V4.1 correctness harness"), which was closed alongside #49 and #60 for two reasons: (1) all three PRs targeted master directly instead of stacking, so each diff showed the combined content of its unmerged predecessors; (2) leaked personal paths. This PR succeeds #49 with the same substantive content, properly restacked with clean history and no personal paths.

This layer adds the core DeepSeek V4.1 runtime, scoped as the model-execution half of the correctness-harness effort tracked in #48:

  • LLM_ARCH_DEEPSEEK41 registration and GGUF schema (gguf-py/gguf/constants.py, src/llama-arch.{cpp,h})
  • Model graph and loader wiring (src/models/deepseek41.cpp, src/llama-model.cpp, src/llama-model-loader.cpp, src/llama-graph.cpp, src/llama-context.cpp, include/llama.h, common/arg.cpp)
  • Engram state and expert store (src/llama-engram.*, src/llama-expert-store.*, src/llama-dsv41-engram.*, src/llama-dsv41-expert.*, src/llama-dsv41.*)
  • On-disk expert memory admission (src/llama-memory-dsv41.*, src/llama-bounded-file.*, src/llama-ple-disk.cpp integration)
  • A standalone host-memory watchdog script and docs (scripts/strix_memory_watchdog.py, docs/strix-memory-watchdog.md)
  • Unit tests for all of the above

PR B (the correctness trace harness under tools/deepseek-v41-trace/) will be created afterward, stacked on top of this branch. This layer deliberately does not include tools/deepseek-v41-trace/, tools/server/ integration, or the PR60 host-memory-guard-in-loader scope; those are out of scope here.

AI usage disclosure: AGENT-AUTHORED. I (Claude Sonnet 5, running as GitHub Copilot) rebuilt this layer from scratch: reconciled the 12 files that drifted between the old closed branch and current master (see below), re-applied the validated DeepSeek V4.1 content on top of current master, and wrote fresh commits.

Twelve-file reconciliation

Twelve files changed on current master since the old branch's merge-base and had to be reconciled (3-way merge of base/master/old-branch content) rather than blindly overwritten:

  • gguf-py/gguf/constants.py, src/llama-arch.cpp, src/llama-arch.h, common/arg.cpp, include/llama.h, src/llama-context.cpp, src/llama-graph.cpp, src/llama-model-loader.cpp, src/llama-model.h, tests/CMakeLists.txt -- merged with zero conflicts (purely additive on both sides).
  • src/llama-model.cpp -- one conflict: both master (LLAMA_LAZY_MODE_DIRECT skip) and this branch (!ml.external.any() skip) touched the same ml.init_mappings(...) call; resolved by combining both conditions.
  • src/llama-ple-disk.cpp -- the significant one: master had independently reworked the row-gather algorithm (old uniq-sorted scheme -> new order/runs/job_kind scheme with calling-thread work-stealing) since the merge-base. This branch's llama_bounded_file abstraction (exception-based I/O error handling, replacing raw pread/O_DIRECT) was hand-integrated onto master's newer scheduling algorithm, including extending exception propagation to cover the calling thread's own work-stealing pass, and keeping a dedicated raw fd for prefetch()'s posix_fadvise hint (a master-only addition postdating the merge-base that llama_bounded_file doesn't expose).

Scope decision made during implementation

tests/test-deepseek41-runtime.cpp (checked out into this layer, per plan) includes tools/deepseek-v41-trace/trace-components.h to validate the trace-tensor naming contract round-trip. Since that header is explicitly PR B's content, its CTest registration is deferred to that layer (the file exists in this tree but is not yet wired into tests/CMakeLists.txt; PR B will add that registration when it adds the header).

Measurements

Not applicable. This layer only adds new architecture registration, loader plumbing, and CPU-side runtime infrastructure; nothing in this PR is reachable without a DeepSeek V4.1 GGUF, and no such model was loaded or run as part of this work (standing constraint: no inference, no GGUF loaded). There is no throughput or latency claim to make, and no baseline to compare against, until PR B's trace harness can actually drive a model through this code. This section will be populated as needed by future performance work once end-to-end correctness is established.

Correctness:

Model-free build and targeted CTest, run on this development machine (macOS/arm64, not Strix hardware):

cmake -S . -B build -DLLAMA_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)
ctest -R 'deepseek41|test-engram|test-expert-store|test-llama-archs' --output-on-failure
100% tests passed, 0 tests failed out of 7
    test-deepseek41-schema    Passed
    test-deepseek41-engram    Passed
    test-deepseek41-expert    Passed
    test-expert-store         Passed
    test-deepseek41-memory    Passed
    test-engram               Passed
    test-deepseek41-no-alloc  Passed

Also ran directly (not wired into CTest on non-Linux): gguf-py/tests/test_deepseek41_schema.py (4 passed) and tests/test_strix_memory_watchdog.py (36 passed, 8 skipped - Linux-only process-group cases).

Note: building in this macOS environment required a local, uncommitted one-line guard around a pre-existing posix_fadvise/POSIX_FADV_WILLNEED call in llama-ple-disk.cpp that is unrelated to this PR (it predates this branch, landed at the merge-base commit, and is a known, currently-failing CI (apple) issue on master itself -- POSIX_FADV_WILLNEED does not exist on macOS). That local shim was reverted before committing; it is not part of this PR's diff.

Additional information

Related: #48 (tracking issue), #49 (predecessor, closed for stacking/leak hygiene), #59 (predecessor, closed for the same reason), #60 (companion PR covering the host-memory-guard-in-loader integration, deliberately excluded from this layer's scope).

Requirements

  • I have read and agree with the contributing guidelines
  • This change is Strix Halo specific: DeepSeek V4.1's Engram/expert-store design targets Strix Halo's unified-memory constraints (bounded on-disk expert paging, host-memory watchdog for degraded-swap conditions). No performance claim is made in this layer; that is PR B's job once correctness is established end-to-end.
  • AI usage disclosure: AGENT-AUTHORED (Claude Sonnet 5 / GitHub Copilot). See above for what was reconciled vs. directly reused.
  • What was NOT verified: no GGUF loaded, no inference run, no ROCm/Vulkan backend exercised, no Strix hardware run. Cross-runtime and hardware-backed correctness are explicitly out of scope for this layer and are PR B's responsibility.

Jerome Coste and others added 6 commits September 16, 2026 13:37
Add the DEEPSEEK41 model architecture enum, its GGUF metadata keys
(hidden size, MoE routing, index attention, Engram config) and tensor
names, plus the DeepSeek-V4.1-specific hparams fields (kv/index source
layer maps, engram layer bitset). Reconciled onto the current gguf-py
constants and llama-arch content.

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… infra

Add llama_bounded_file, a direct-IO-aware file reader abstraction with
exception-based error handling, and integrate it into llama_ple_disk's
row-gather path (replacing the old raw pread/O_DIRECT logic) while
keeping master's current gather/dequant job scheduling and read-size
accounting intact.

Add llama_engram (fixed-capacity KV/index recall state) and
llama_expert_store (host-resident MoE expert weight cache) as new
standalone modules, plus their unit tests. Extend llama_mmap,
llama_kv_cache and llama_memory with the small hooks these modules
need.

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add llama-dsv41, the DeepSeek V4.1-specific glue that binds Engram
recall state and the expert store to the model runtime hparams, and
llama-memory-dsv41, the llama_memory implementation that admits and
schedules Engram/expert state per sequence. Add unit tests for the
engram runtime, expert runtime, and memory admission paths.

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add llama_model_deepseek41's arch graph, hparam and tensor loading in
src/models/deepseek41.cpp, and register it in the model factory and
llama_model::create_memory. Reconcile llama-model.cpp, llama-model.h,
llama-model-loader.cpp, llama-context.cpp and llama-graph.cpp against
master's own drift since the merge-base this content is sourced from:

- llama-model-loader gains register_external_tensor and an
  external_read tracker so expert-store tensors can be read straight
  from disk instead of being mapped or fully loaded; load_all_data and
  init_mappings take explicit load_from_mmap/discard_file_cache
  parameters instead of assuming the loader-wide mmap setting.
- llama-model's load_tensors keeps mmap unsafe for any context that
  overlaps an external tensor range, still applying master's own
  ple_on_disk/lazy-mode-direct mapping skip alongside it.
- llama-context adds a memory-context commit/rollback transaction
  guard around graph compute, and model runtime acquire/release hooks
  used by the dsv41 engram/expert runtime.
- llama-graph's build_moe_ffn takes a separate expert lookup tensor
  from the routing tensor, needed because dsv41's expert store lets
  the physical MoE weight slot differ from the logical expert id.

Add --expert-cache-slots/--expert-cache-mib CLI options and extend
test-llama-archs.cpp with a no-allocation DeepSeek V4.1 graph-shape
smoke test (its full published geometry is too large for the compact
generated-model fixture used by the rest of that test).

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add scripts/strix_memory_watchdog.py, a standalone process-group
supervisor that requires zero active swap and stops a wrapped command
before host-wide memory reaches a validation ceiling. This is a plain
external monitoring script: it does not touch or link against llama.cpp
runtime code, and is unrelated to the (deliberately excluded from this
layer) in-process host-memory-guard integration.

Add its unit tests, usage docs, and a README table entry. Use a full
git commit hash instead of the short form for GGML_BUILD_COMMIT so a
build can be traced back unambiguously during a watchdog-flagged run.

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Register the new DeepSeek V4.1 unit tests (schema, engram, expert
store, memory admission, runtime) and the test-deepseek41-no-alloc
allocation-guard case for the deepseek41 arch. Register the Python
strix-memory-watchdog unit test alongside the other python-labeled
tests.

This intentionally omits the trace-harness and native-containment
test registrations (test-server-child-protocol.cpp, test-deepseek41-
trace.py, LLAMA_DEEPSEEK_V41_NATIVE_CONTAINMENT_TESTS); those depend
on tools/deepseek-v41-trace/ and tools/server/ content that is out of
scope for this layer and will be added by the next layer.

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
POSIX_FADV_WILLNEED is not declared on macOS, so the unconditional
posix_fadvise() call in prefetch() failed to compile there. Guard it
the same way discard_cache() already guards POSIX_FADV_DONTNEED in
llama-mmap.cpp: skip the readahead hint when the macro is unavailable,
Linux behaviour is unchanged.

Assisted-by: Claude Sonnet 5
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@ajaxdude

Copy link
Copy Markdown
Member Author

@dzannotti Reopening this as a clean revival of #49 (and its siblings #59/#60), which you closed citing two specific issues: PRs "poorly stacked" against master instead of each other, and leaking "an incredible amount of personal configuration and pathing."

Both are fixed here, concretely:

Both PR descriptions include a full scope accounting, what was/wasn't verified, and an AI-authorship disclosure.

What I'm asking for: a re-review of this PR and #67 with the above in mind. If either issue resurfaces, or anything else needs attention, please say so directly and I'll fix it before requesting merge.

@ajaxdude

Copy link
Copy Markdown
Member Author

Closing and superseding with #70. Same content, same 7 commits, byte-identical diff -- but this PR's commits carried a real name + machine hostname as git author/committer (Jerome Coste <jeromecoste@Jeromes-MacBook-Pro.local>), and the branch name embedded a corporate username (jeromecoste-microsoft-...). Same category of issue that got the original #49/#59/#60 closed (leaked personal configuration/pathing), just at the commit-metadata/branch-name level instead of file content. Fixed via authorship rebase (git commit --amend --reset-author, verified zero content diff) and a fresh push under a leak-free branch name. Note: GitHub retains this PR's old branch name in this page's own history regardless of what happens to the branch itself -- that's an inherent platform limitation of closing/re-opening rather than something further action here can fix; #70 is clean going forward.

@ajaxdude ajaxdude closed this Sep 17, 2026
@ajaxdude
ajaxdude deleted the jeromecoste-microsoft-deepseek-v41-core-runtime branch September 17, 2026 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build conversion documentation Improvements or additions to documentation ggml model testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant