Skip to content

gpt-oss-20b trains in-process (MoE backward in the llama.cpp patch); tuned qwen3-14b and gpt-oss-20b profiles - #20

Merged
FMXExpress merged 4 commits into
mainfrom
claude/optimistic-allen-i3bd3d
Nov 10, 2026
Merged

FMXExpress merged 4 commits into
mainfrom
claude/optimistic-allen-i3bd3d

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

memagent's own llama.cpp trainer can now train gpt-oss-20b, and two tuned profiles target a single 16 GB+ GPU. Write-up: bench/README.md, section "Qwen3-14B and gpt-oss-20b training on an RTX 4090".

llama.cpp patch: backward passes for gpt-oss's mixture of experts

The experts stay frozen, so these are gradients into the activations only.

  • GGML_OP_OUT_PROD_ID: the input gradient of MUL_MAT_ID.
    • CPU kernel: dequantizes one expert row at a time.
    • CUDA kernel: reads the routing once. Then, for each expert, it gathers that expert's columns, dequantizes the weights in slices, runs the GEMM, and scatter-adds the result.
  • ADD_ID: the gradient passes through to the activations.
  • SWIGLU_OAI: built from clamp, sigmoid, step and mul, in both the split and fused forms.
  • GET_ROWS_BACK: now batched, on CPU and CUDA. gpt-oss uses it to pick each token's expert weights.
  • packaging/llama-patches/tests/:
    • gradtest.cpp checks each pass against central finite differences, and checks quantized experts against the same experts as f32.
    • cudacmp.cpp compares GPU against CPU.

Profiles (models.json)

profile model training measured on an RTX 4090
qwen3-14b Qwen3-14B Q4_K_M (9 GB) 3 epochs, lr 3e-4 30 facts: 0.967 from the weights, gate recall 1.00, drift 1.14, interference 0.017, 59 s
gpt-oss-20b gpt-oss-20b MXFP4 (12 GB) 4 epochs, lr 6e-4, 32 anchors (the LoRA covers attention only) 10 facts: 0.75 from the weights, gate recall 1.00, drift 1.40, 31 s
  • Qwen3-14B at its old 2 epochs: 0.90 from the weights, recall 0.93, drift 1.22.
  • A 10-fact sweep compared lr and epochs for Qwen3-14B (0.80 to 1.00), and three settings for gpt-oss (0.15 to 0.80).
  • Results are in bench/results/memory-qwen3-14b-4090-*.json and memory-gpt-oss-20b-4090-*.json.

Also

  • bench_llama now resolves [gpu] layout instead of forcing the CPU. BENCH_CPU=1 keeps the old behaviour. Before this, on a GPU it tried to build a 60 GB f32 copy of the 14B.

Not in this PR

  • Qwen3.8-27B training. It needs backward passes for Gated DeltaNet, SSM_CONV, SOLVE_TRI, CUMSUM and more, plus training support for the recurrent cache.

Test plan

  • make test-pascal
  • gradtest.cpp on the CPU: all checks pass
  • cudacmp.cpp on an RTX 4090: f32, Q4_K, MXFP4 and Q8_0 experts match the CPU
  • gpt-oss-20b trains end to end on the 4090, through scripts-style chat, /sleep, /wait and bench_memory.py --trainer llama
  • Qwen3-14B sweep and confirmation run on the 4090
  • The patch applies cleanly to llama.cpp b11985

Generated by Claude Code

… SWIGLU_OAI); qwen3-14b and gpt-oss-20b profiles

GGML_OP_OUT_PROD_ID is the input gradient of MUL_MAT_ID with the experts frozen:
a CPU kernel (one dequantized expert row at a time) and a CUDA kernel (routing
read once, then per expert: gather its columns, dequantize slices, GEMM,
scatter-add). ADD_ID passes its gradient to the activations; SWIGLU_OAI's
backward is built from clamp, sigmoid, step and mul, split and fused.
packaging/llama-patches/tests/gradtest.cpp checks all of them on the CPU
against finite differences (and quantized experts against f32): all pass.
The CUDA kernel is not built or run yet.

models.json: qwen3-14b (Q4_K_M) and gpt-oss-20b (MXFP4), in-process trainer,
training settings still the 1.5B's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
…om the weights, gate recall 1.00, drift 1.14); bench_llama follows [gpu] layout

On bench/facts.json (30 facts, 60 questions), training through the Q4_K_M
weights on the card: 2 epochs (the 1.5B's setting) 0.90 from the weights,
recall 0.93, drift 1.22, 42 s; 3 epochs 0.967, recall 1.00, drift 1.14,
interference 0.017, 59 s. A 10-fact sweep of lr and epochs (0.80 to 1.00)
picked 3 epochs at lr 3e-4.

bench_llama resolved no layout and forced the CPU, so on a card it tried to
make a 60 GB f32 copy of the 14B; it now uses Mem.Layout (BENCH_CPU=1 for the
old behaviour).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
…ft 1.40); batched GET_ROWS_BACK; tuned profiles

The last missing backward for gpt-oss was get_rows over a batch (each token's
expert weights): GET_ROWS_BACK is now batched on the CPU and CUDA, checked
against finite differences in gradtest.cpp. On an RTX 4090 the CUDA
OUT_PROD_ID matches the CPU for f32, Q4_K, MXFP4 and Q8_0 experts, and
gpt-oss-20b trains end to end: 4 epochs, lr 6e-4, 32 anchors gives 0.75
answered from the weights, gate recall 1.00, drift 1.40, 31 s (10 facts).
gpt-oss-20b profile set to that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
@FMXExpress
FMXExpress merged commit 9af951b into main Nov 10, 2026
2 checks passed
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