Skip to content

metal: MUL_MAT_ID_BACK kernel, so a MoE LoRA's backward runs on the GPU (stacked on #82; continuum card 14b64aa9) - #83

Merged
joelteply merged 1 commit into
889580fc/seq-state-across-stream-countsfrom
0a3a5106/metal-mul-mat-id-back-kernel-so-a-moe-lo
Oct 31, 2026
Merged

joelteply merged 1 commit into
889580fc/seq-state-across-stream-countsfrom
0a3a5106/metal-mul-mat-id-back-kernel-so-a-moe-lo

Conversation

@joelteply

Copy link
Copy Markdown

#82 adds GGML_OP_MUL_MAT_ID_BACK with a CPU kernel; on Metal the scheduler would run that one
op on the CPU inside a GPU graph. This is the Metal kernel.

  • kernels/out_prod.metal: kernel_mul_mat_id_back, in kernel_out_prod's template shape
    <T0, block_q, nl, dequantize_func>. One threadgroup per routed slot (i, t); each thread owns
    columns of that slot's row and walks the expert's rows, so every dst element has one writer
    and no expert is copied or transposed in device memory. nl == 0 reads F32/F16/BF16 element by
    element; nl > 0 dequantizes 16 consecutive elements of a row in registers (ne00 % 16 == 0).
    Instantiated for f32, f16, bf16 (GGML_METAL_HAS_BF16), q4_0, q4_1, q5_0, q5_1, q8_0 and
    q2_K-q6_K: out_prod's set.
  • ggml-metal-ops.cpp:
    • ggml_metal_op_mul_mat_id_back dispatches (n_used, n_tokens) threadgroups;
    • ggml_metal_op_mul_mat_id_back_supported refuses the pointer-table gather (src[3]);
    • out_prod's weight-type switch is now one predicate, ggml_metal_frozen_weight_readable,
      shared by both training kernels.
  • ggml-metal-device.*:
    • the pipeline getter (kernel_mul_mat_id_back_);
    • supports_op, with bf16 requiring has_bfloat.

Not run here: enrico's Metal driver is wedged, and the Metal toolchain could not be installed
to compile-check the shader. The C++ side compiles (clang -fsyntax-only on ggml-metal-ops.cpp).
Proof is on the M5:

  • test-backend-ops test -o MUL_MAT_ID_BACK -b Metal against CPU, for every expert type in
    test_mul_mat_id_back;
  • test-backend-ops grad -o MUL_MAT_ID -b Metal.

Co-Authored-By: Claude Opus 5.5 (1M context) noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply
joelteply changed the base branch from master to cormac/mul-mat-id-backward October 31, 2026 21:20
@joelteply
joelteply changed the base branch from cormac/mul-mat-id-backward to 889580fc/seq-state-across-stream-counts October 31, 2026 22:48
…PU (stacked on #82; continuum card 14b64aa9)

#82 adds GGML_OP_MUL_MAT_ID_BACK with a CPU kernel; on Metal the scheduler would run that one
op on the CPU inside a GPU graph. This is the Metal kernel.

- kernels/out_prod.metal: kernel_mul_mat_id_back, in kernel_out_prod's template shape
  <T0, block_q, nl, dequantize_func>. One threadgroup per routed slot (i, t); each thread owns
  columns of that slot's row and walks the expert's rows, so every dst element has one writer
  and no expert is copied or transposed in device memory. nl == 0 reads F32/F16/BF16 element by
  element; nl > 0 dequantizes 16 consecutive elements of a row in registers (ne00 % 16 == 0).
  Instantiated for f32, f16, bf16 (GGML_METAL_HAS_BF16), q4_0, q4_1, q5_0, q5_1, q8_0 and
  q2_K-q6_K: out_prod's set.
- ggml-metal-ops.cpp:
  - ggml_metal_op_mul_mat_id_back dispatches (n_used, n_tokens) threadgroups;
  - ggml_metal_op_mul_mat_id_back_supported refuses the pointer-table gather (src[3]);
  - out_prod's weight-type switch is now one predicate, ggml_metal_frozen_weight_readable,
    shared by both training kernels.
- ggml-metal-device.*:
  - the pipeline getter (kernel_mul_mat_id_back_<type>);
  - supports_op, with bf16 requiring has_bfloat.

Not run here: enrico's Metal driver is wedged, and the Metal toolchain could not be installed
to compile-check the shader. The C++ side compiles (clang -fsyntax-only on ggml-metal-ops.cpp).
Proof is on the M5:
- `test-backend-ops test -o MUL_MAT_ID_BACK -b Metal` against CPU, for every expert type in
  test_mul_mat_id_back;
- `test-backend-ops grad -o MUL_MAT_ID -b Metal`.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply
joelteply force-pushed the 0a3a5106/metal-mul-mat-id-back-kernel-so-a-moe-lo branch from a7b1ab7 to a726c39 Compare October 31, 2026 22:48
@joelteply

Copy link
Copy Markdown
Author

PASS at a726c39 (Fable). Verified on the M5 (Apple M5 Max, Metal, serving down), built from this head: test-backend-ops test -o MUL_MAT_ID_BACK -b MTL0 12/12; test-backend-ops grad -o MUL_MAT_ID -b MTL0 19838/19838 (877 MUL_MAT_ID grad cases OK on MTL0). The same counts as my run at a7b1ab7. Logs: ~/.continuum/evidence/2026-10-31-fork83-metal/a726c39d7-{test,grad}.log on the M5.

@joelteply
joelteply merged commit 6606d1a into 889580fc/seq-state-across-stream-counts Oct 31, 2026
18 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant