Skip to content

llama : looped layers, device placement by weight layers and host weight views - #396

Open
danielhanchen wants to merge 7 commits into
base/upstream-87f816e87from
inkling2-loop-offload
Open

danielhanchen wants to merge 7 commits into
base/upstream-87f816e87from
inkling2-loop-offload

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes partial offload and multi-GPU placement for models whose loop layers are cache slots sharing the weights of physical layers (inkling2_loop).

  • Host-resident weights: later visits get their own views of the shared weights, so --cpu-moe, -ngl 12 and -ngl 0 no longer produce NaN logits.
  • Device placement counts the layers that hold weights, so each slot sits on the device of its weights. -ngl and -ts count physical layers (24 on Inkling-2-Mini, not 48 slots); llama_model_n_layer_weights() gives common/fit the same unit.
  • The Hadamard and precision state keyed by tensor is carried over to the views, and the saver writes each weight once.
  • Models without shared layer weights are unchanged.

Tests (b12600 + ggml-org#32704 + ggml-org#32218 + this), wikitext-2, 20 chunks of 512, against full single-GPU:

  • --cpu-moe and -ngl 12: max KLD at the run-to-run floor (0.000053), top-1 100%. Before: NaN.
  • -ts 1,1 on 2 GPUs: no slot placed off its weights' device, scheduler splits 419 -> 17.
  • test-llama-archs: all configs pass on CPU and CUDA.

… give later visits their own host weight views

Device placement: n_gpu_layers and the tensor splits counted every cache slot,
while the weights exist once per physical layer. On Inkling-2-Mini (48 slots,
24 physical layers) `-ts 1,1` put every weight on the first GPU and ran 23 later
visits on the second, and `-ngl N` offloaded slots instead of layers. A model
now reports the layer whose weights each layer uses (layer_weights), the splits
count the layers that hold weights, and every slot runs on the device of its
weights. llama_model_n_layer_weights() gives common/fit the same unit.

Host weight views: the backend scheduler keys the device copy of a host weight
(op offload) by tensor, so with experts or layers on the CPU every later visit
of a shared weight read the copy of the first visit and the output was NaN. The
later users of a shared host weight now get views of it, each with its own copy.
Views keep the name and the buffer, the Hadamard and precision state keyed by
tensor is given to them, and the saver writes each weight once.

Models without shared layer weights are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T07:12:11.606601Z bff7247 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d96a8345a8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/llama-model.cpp Outdated
Comment on lines +1723 to +1724
// The backend scheduler keys the device copy of a host weight (op offload) by tensor, so with one tensor for every
// visit the later visits read the copy of the first one, which is wrong once its memory is reused, and the copies of

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove mid-sentence wrapping from the shared-weight comment

The new explanation hard-wraps several sentences across comment lines, contrary to the explicit rule in AGENTS.md prohibiting mid-sentence line breaks. Shorten the explanation where possible and keep each sentence on an unsplit line.

AGENTS.md reference: AGENTS.md:L84-L84

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in bff7247. The comment is shorter now, with each sentence on its own line. Only the comment changed.

Since ggml-org#32669 build_lora_mm and build_lora_mm_id look the .scale of a
weight up by tensor (llama_model::w_scales) instead of taking the layer's
*_s tensor. The per-visit views of shared host weights are new tensors, so
the later visits of a looped layer lost their NVFP4 scales. Map each view to
the view of its scale in the same layer, as the *_s fields were before.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: bff7247941

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen changed the base branch from base/upstream-19bc41dbb to base/upstream-87f816e87 November 24, 2026 09:38
@danielhanchen

Copy link
Copy Markdown
Member Author

Repinned to 7b2356da0, a fast-forward of bff724794 (no force push). Three commits on top:

The PR base moves to base/upstream-87f816e87 (b12668).

Evidence, B200:

  • Inkling-2-Mini with CPU-offloaded experts: pp512 802.8 to 975.6 t/s (+21.5%), pp2048 753.6 to 1031.2 t/s (+37%). PPL equal and KLD 0 on 3 placements, so the selected-expert copy gives the same logits as the full copy.
  • Quantized KV gate (UD-IQ3_XXS, q8_0, 128K): memory identical to the old pin, PPL string-equal, KLD 0.
  • Numeric gate on the merged release tree (Inkling-2-Mini UD-IQ1_S, wikitext-2, -c 512 --chunks 20, all layers on GPU):
arm tree PPL mean KLD vs A top-1
A current pr-set.json on b12667 (a658404ac) 8.4647 - -
A again same 8.464749 -0.000000 100%
B + #396 77941e048, #413 627b5434a, both mirrors, on b12667 (b15c4ae7e) 8.464749 -0.000000 100%
D new pin set on b12671 (4d5f65714) 8.464749 -0.000000 100%
C D + ggml-org master (afbc31e0f) 8.464749 -0.000000 100%

All arms give the same PPL, mean KLD 0 (printed as -0.000000) and top-1 100%, so the gate passes. (llama_pinset.py gate prints FAIL here only because its KLD regex does not accept the minus sign. The verdict was read from the logs.)

This PR on its own cannot load the published Inkling-2-Mini GGUFs (they need #395 for the looped layout), so the gate runs on the merged pin set. A = current pr-set.json on b12667, B = the same with this repin, #413's repin and the two new mirrors on b12667, D = the new pin set on b12671, C = D merged with ggml-org master.

  • The pin set with this commit merges, passes the contract and merge checks on b12670 and b12671, and passes verify --cuda on both (all 31 features on GPU, including inkling2-loop).

This branch has not been deployed

No deployments
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.

1 participant