fix(driver): keep Qwen3.5/3.6 linear-attention decay params in f32 so exp() runs on Metal#465
Conversation
… exp() runs on Metal
Running a bf16 Qwen3.6 checkpoint on Metal warned:
48 graph node(s) fell back to CPU (no Metal kernel): UNARYx48
One per linear-attention layer: the gated-delta-rule block computes
`exp(A_log)` directly on the bf16 `linear_attn.A_log` weight, and
ggml-metal has no bf16 unary kernel, so every `exp` node fell back to
CPU and split the graph. `A_log`/`dt_bias` slipped past the load-time
f32 promotion, whose suffix match only covers `*norm.weight`/`*.bias`.
Fix, scoped to PieArch::Qwen3_5:
- model.cpp: promote `linear_attn.{A_log,dt_bias}` to f32 at load
time ([num_v_heads] floats per layer, ~9 KiB total on 27B), and
exempt them from the f32→bf16 downcast.
- graph_qwen3_5.cpp: defensive f32 cast before `exp(A_log)` for
checkpoints that bypass the load-time upcast.
Verified on Qwen3.6-27B (M4 Pro): the warning is gone and the whole
graph schedules onto MTL0. Qwen3-0.6B smoke test unchanged.
WalkthroughQwen3.5 linear-attention decay weights are now retained as F32 during loading, and non-F32 ChangesQwen3.5 precision handling
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
driver/portable/src/graph_qwen3_5.cpp (1)
65-67: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMinor: repeated "cast-to-f32-if-needed" pattern.
dt_biasandA_lognow both use the identical 3-line ternary-cast idiom. Could extract a smallto_f32(ctx, t)helper to avoid duplicating this if a third such param appears later.♻️ Optional helper extraction
+static ggml_tensor* to_f32(ggml_context* ctx, ggml_tensor* t) { + return (t->type == GGML_TYPE_F32) ? t : ggml_cast(ctx, t, GGML_TYPE_F32); +} + ... - auto* dt_bias = (L.lin_dt_bias->type == GGML_TYPE_F32) - ? L.lin_dt_bias - : ggml_cast(ctx, L.lin_dt_bias, GGML_TYPE_F32); + auto* dt_bias = to_f32(ctx, L.lin_dt_bias); ... - auto* A_log = (L.lin_A_log->type == GGML_TYPE_F32) - ? L.lin_A_log - : ggml_cast(ctx, L.lin_A_log, GGML_TYPE_F32); + auto* A_log = to_f32(ctx, L.lin_A_log);Also applies to: 70-75
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@driver/portable/src/graph_qwen3_5.cpp` around lines 65 - 67, Extract the repeated cast-to-F32 ternary used for dt_bias and A_log into a small to_f32(ctx, tensor) helper, then replace both inline expressions with calls to that helper while preserving the existing behavior for tensors already of type GGML_TYPE_F32.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@driver/portable/src/graph_qwen3_5.cpp`:
- Around line 65-67: Extract the repeated cast-to-F32 ternary used for dt_bias
and A_log into a small to_f32(ctx, tensor) helper, then replace both inline
expressions with calls to that helper while preserving the existing behavior for
tensors already of type GGML_TYPE_F32.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b5ebf8c4-597c-447e-a874-5729fc285b82
📒 Files selected for processing (2)
driver/portable/src/graph_qwen3_5.cppdriver/portable/src/model.cpp
Running a bf16 Qwen3.6 checkpoint on Metal warned:
One per linear-attention layer: the gated-delta-rule block computes
exp(A_log)directly on the bf16linear_attn.A_logweight, and ggml-metal has no bf16 unary kernel, so everyexpnode fell back to CPU and split the graph.A_log/dt_biasslipped past the load-time f32 promotion, whose suffix match only covers*norm.weight/*.bias.Fix, scoped to PieArch::Qwen3_5:
linear_attn.{A_log,dt_bias}to f32 at load time ([num_v_heads] floats per layer, ~9 KiB total on 27B), and exempt them from the f32→bf16 downcast.exp(A_log)for checkpoints that bypass the load-time upcast.Verified on Qwen3.6-27B (M4 Pro): the warning is gone and the whole graph schedules onto MTL0. Qwen3-0.6B smoke test unchanged.
Summary by CodeRabbit