Skip to content

llama : copy all experts of a host weight shared by several nodes - #32807

Merged
pwilkin merged 2 commits into
masterfrom
pwilkin/inkling2-loop-expert-copy
Nov 22, 2026
Merged

pwilkin merged 2 commits into
masterfrom
pwilkin/inkling2-loop-expert-copy

Conversation

@pwilkin

@pwilkin pwilkin commented Nov 22, 2026

Copy link
Copy Markdown
Member

Overview

Fixes corruption issue when host tensors are copied for reuseable expert tensors.

Additional information

Minimal reproducer:

#!/usr/bin/env bash
# inkling2_loop with --n-cpu-moe: after a batch of 32+ tokens the target emits "!" (token 0) forever
# the n-gram drafts give such batches; --no-op-offload makes it pass
#
# usage: repro-inkling2-loop-expert-copy.sh <llama-server> <Inkling-2-Mini.gguf> [extra server args, e.g. -dev CUDA0]
set -euo pipefail

SERVER=$1
MODEL=$2
shift 2
PORT=${PORT:-18744}
URL=http://127.0.0.1:$PORT

"$SERVER" -m "$MODEL" -ngl 99 --n-cpu-moe 13 -fa on -c 4096 -np 1 --spec-type ngram-simple --port "$PORT" "$@" > repro-server.log 2>&1 &
PID=$!
trap 'kill $PID 2>/dev/null' EXIT

until curl -sf "$URL/health" > /dev/null; do
    kill -0 $PID 2> /dev/null || { echo "server exited, see repro-server.log"; exit 2; }
    sleep 1
done

MSG="Write the sentence 'the cat sat on the mat and looked at the red hat' twenty times, one per line, numbered."
PROMPT=$(curl -s "$URL/apply-template" -d "$(jq -n --arg m "$MSG" '{messages: [{role: "user", content: $m}]}')" | jq -r .prompt)

fail=0
for i in 1 2; do
    OUT=$(curl -s "$URL/completion" -d "$(jq -n --arg p "$PROMPT" '{prompt: $p, n_predict: 200, temperature: 0, top_k: 0}')" | jq -r .content)
    if [[ "$OUT" == *'!!!!!!!!'* ]]; then
        echo "request $i: BROKEN"
        fail=1
    else
        echo "request $i: ok"
    fi
    echo "  ${OUT:0:100}" | tr '\n' ' '
    echo
done

exit $fail

Requirements

The scheduler copies a host weight to the device once per graph, so a
partial expert copy made for the first MUL_MAT_ID is stale for the
others (e.g. the shared layers of inkling2_loop with --n-cpu-moe).

Assisted-by: Claude Opus 5.5
@pwilkin
pwilkin requested a review from a team as a code owner November 22, 2026 13:05

@ServeurpersoCom ServeurpersoCom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The scheduler makes the device copy of a host weight once per graph and only the first split that reads it uploads it, so with the looped layers of inkling2_loop sharing their expert weights, the experts routed only by the later passes were never copied.

Tested with ggml-org/Inkling-2-Mini-GGUF IQ4_NL and --n-cpu-moe 13, with the reproducer from the description and with a llama-perplexity KLD against --no-op-offload logits.

Without this PR, a probe in the scheduler shows almost every later pass of the loop reading experts that its upload did not cover (12 of 61 for a 37 token prompt), the reproducer outputs "!" and the KLD is NaN.

With this PR, the reproducer is fine and the KLD is 0.0013 with over 99% same top token, at both -ub 32 and -ub 512.

@am17an

am17an commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Perhaps #32809 is the more correct fix, but I'm not sure.

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The #32809 makes an assumption that the copy callback does not copy all experts. For llama.cpp this is the case, but in general it is not necessary to be.

So for the short-term I think we should go with the approach in this PR. Probably the loop over the graph nodes can for now be replaced with arch check and add a TODO to improve this logic. Add a references to these PRs to have context later.

@pwilkin
pwilkin merged commit 5bacb7c into master Nov 22, 2026
16 checks passed
Annieren pushed a commit to Annieren/llama.cpp-upstream that referenced this pull request Nov 23, 2026
…ml-org#32807)

* llama : copy all experts of a host weight shared by several nodes

The scheduler copies a host weight to the device once per graph, so a
partial expert copy made for the first MUL_MAT_ID is stale for the
others (e.g. the shared layers of inkling2_loop with --n-cpu-moe).

Assisted-by: Claude Opus 5.5

* llama : copy all experts of a host weight shared by several nodes

Assisted-by: Codex
Kidplayer666 pushed a commit to Kidplayer666/llama.cpp that referenced this pull request Nov 23, 2026
…ml-org#32807)

* llama : copy all experts of a host weight shared by several nodes

The scheduler copies a host weight to the device once per graph, so a
partial expert copy made for the first MUL_MAT_ID is stale for the
others (e.g. the shared layers of inkling2_loop with --n-cpu-moe).

Assisted-by: Claude Opus 5.5

* llama : copy all experts of a host weight shared by several nodes

Assisted-by: Codex
abeloin pushed a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 24, 2026
378 commits amont depuis af6b524 (b12292) : arch inkling2
(5c008e0), fix experts host 5bacb7c (ggml-org#32807), compact bias FA
ggml-org#32740 (tête du tag), le reste de l'écart b12292→b12670.

Conflits résolus (3 fichiers, minimal + documenté) :
- ggml-cuda.cu : union — bloc de fusion NVFP4 amont verbatim +
  crochet mmvq de la couche en sa forme riche
  should_fuse_mul_mat_vec_q(mm_node, out_node, bias) ; la définition
  enrichie de la couche (4 args à défauts, ligne ~2349) est
  rétrocompatible, les appels 1-arg amont compilent tels quels.
- qwen35.cpp : structure amont ssm_beta_alpha_cat prise telle quelle ;
  la couche y perd l'ordre reshape-après-sigmoid (astuce de fusion
  mul_mat+unary des portes GDN, c77181c) — à réappliquer adaptée en
  couche séparée si la build gate manque la perf GDN. Les autres
  changements rdna3 du fichier (l2-norm une-op, états relus en place)
  ont fusionné automatiquement.
- tests/test-backend-ops.cpp : tests 256-experts de la couche
  conservés + porte #if 0 dup-ids conservée ; l'auto-fusion avait
  absorbé le #endif (amont a activé ces tests) — fermeture rétablie
  à l'emplacement du mix pré-merge (équilibre #if/#endif 16/16
  vérifié, sans elle la porte désactivait tous les tests suivants).

Conflit pins unsloth ggml-org#385/ggml-org#386 vs ggml-org#32740 : reporté (MIX.md,
arbitrage propriétaire). Build gate : à suivre avant tout usage.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 24, 2026
…ml-org#32807) (#495)

* llama : copy all experts of a host weight shared by several nodes

The scheduler copies a host weight to the device once per graph, so a
partial expert copy made for the first MUL_MAT_ID is stale for the
others (e.g. the shared layers of inkling2_loop with --n-cpu-moe).

Assisted-by: Claude Opus 5.5

* llama : copy all experts of a host weight shared by several nodes

Assisted-by: Codex
(cherry picked from commit 5bacb7c)

Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com>
Lanilen added a commit to Lanilen/llama.cpp that referenced this pull request Nov 24, 2026
… own

The base is upstream f4f6f2e. Upstream's weight scales (ggml-org#32669), the lightning indexer always fused (ggml-org#32650), SCALE through the strides (ggml-org#32735, the fork's own change line for line) and the whole copy of a looped model's experts (ggml-org#32807, kept to the shared tensors). docs/inkling2-mini.md no longer lists the loop fix and the SCALE as the fork's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 24, 2026
378 commits amont depuis af6b524 (b12292) : arch inkling2
(5c008e0), fix experts host 5bacb7c (ggml-org#32807), compact bias FA
ggml-org#32740 (tête du tag), le reste de l'écart b12292→b12670.

Conflits résolus (3 fichiers, minimal + documenté) :
- ggml-cuda.cu : union — bloc de fusion NVFP4 amont verbatim +
  crochet mmvq de la couche en sa forme riche
  should_fuse_mul_mat_vec_q(mm_node, out_node, bias) ; la définition
  enrichie de la couche (4 args à défauts, ligne ~2349) est
  rétrocompatible, les appels 1-arg amont compilent tels quels.
- qwen35.cpp : structure amont ssm_beta_alpha_cat prise telle quelle ;
  la couche y perd l'ordre reshape-après-sigmoid (astuce de fusion
  mul_mat+unary des portes GDN, c77181c) — à réappliquer adaptée en
  couche séparée si la build gate manque la perf GDN. Les autres
  changements rdna3 du fichier (l2-norm une-op, états relus en place)
  ont fusionné automatiquement.
- tests/test-backend-ops.cpp : tests 256-experts de la couche
  conservés + porte #if 0 dup-ids conservée ; l'auto-fusion avait
  absorbé le #endif (amont a activé ces tests) — fermeture rétablie
  à l'emplacement du mix pré-merge (équilibre #if/#endif 16/16
  vérifié, sans elle la porte désactivait tous les tests suivants).

Conflit pins unsloth ggml-org#385/ggml-org#386 vs ggml-org#32740 : reporté (MIX.md,
arbitrage propriétaire). Build gate : à suivre avant tout usage.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Mr-J-369 pushed a commit to Mr-J-369/llama.cpp-Android that referenced this pull request Nov 25, 2026
…ml-org#32807)

* llama : copy all experts of a host weight shared by several nodes

The scheduler copies a host weight to the device once per graph, so a
partial expert copy made for the first MUL_MAT_ID is stale for the
others (e.g. the shared layers of inkling2_loop with --n-cpu-moe).

Assisted-by: Claude Opus 5.5

* llama : copy all experts of a host weight shared by several nodes

Assisted-by: Codex
(cherry picked from commit 5bacb7c)
KyleAnthonyShepherd pushed a commit to KyleAnthonyShepherd/llama.cpp that referenced this pull request Nov 27, 2026
…ml-org#32807)

* llama : copy all experts of a host weight shared by several nodes

The scheduler copies a host weight to the device once per graph, so a
partial expert copy made for the first MUL_MAT_ID is stale for the
others (e.g. the shared layers of inkling2_loop with --n-cpu-moe).

Assisted-by: Claude Opus 5.5

* llama : copy all experts of a host weight shared by several nodes

Assisted-by: Codex
(cherry picked from commit 5bacb7c)
baraxnaxgaming-commits added a commit to baraxnaxgaming-commits/strix-llama.cpp that referenced this pull request Dec 3, 2026
Upstream: ggml-org/llama.cpp 8d60873..5bacb7c (6 commits)

Conflicts:
- src/llama-context.cpp: llama-context: fork side (whole-file EOL conflict) + manually applied ggml-org#32807 INKLING2_LOOP sched_copy_experts guard (6 lines)
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.

4 participants