[PR #5387] [Engine] Metal: fix garbage output with Unsloth low-bit files (BF16 ple_conv1d read as F16) #5412

Open
opened 2026-11-07 16:35:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5387
Author: @qChauncey
Created: 11/6/2026
Status: 🔄 Open

Base: main ← Head: metal-ple-conv1d-bf16


📝 Commits (1)

  • dc1cf66 [Engine] Metal: narrow a BF16 blk.1.ple_conv1d to F16 at load

📊 Changes

1 file changed (+13 additions, -4 deletions)

View changed files

📝 metal/src/engine/window.cpp (+13 -4)

📄 Description

Problem and result

On the Metal engine, Unsloth's UD-IQ2_XS (Dynamic v3.1, uploaded 2026-11-04) answers with garbage from the first token. Logits are finite (STRATA_METAL_NFCHECK: largest |logit| 19.6); DFlash2 accepts ~0 drafts. With this change the same file gives coherent answers.

Cause: ple_conv_add reads blk.1.ple_conv1d.weight as F16. Window::init (metal/src/engine/window.cpp) converts it only when the GGUF stores it as F32 and otherwise passes the file's bytes through. In these files it is BF16 - the same size as F16 - so the BF16 words are read as F16, the layer-1 PLE conv is wrong, and every later layer follows.

Which files store it how (headers read with tools/quantscope.py; not all of them were run):

  • BF16: unsloth UD-IQ1_S, UD-IQ1_M, UD-Q2_0, UD-IQ2_XS, UD-Q2_K_XL, BF16
  • F32: unsloth UD-IQ3_XXS, UD-IQ4_XS, UD-Q3_K_XL, UD-Q4_K_XL, UD-Q5_K_XL, UD-Q6_K_XL, Q8_0; Gyro-M, Gyro-L
  • F16: every ISTA-DASLab GSQ-RCO file, Gyro-S

Of the sets the 0.2.11 Mac app offers, UD-IQ1_M, UD-Q2_0, UD-IQ2_XS and UD-Q2_K_XL are affected.

none (focused fix). #4711 (UD-Q4_K_XL) stores ple_conv1d as F32, so it is not this bug.

Approach

Convert BF16 like F32 (bf16 -> f32 -> ref::f32_to_f16, round to nearest even - the same rule tools/iq_pack.py uses when it stores this tensor as F16 in the pack), and refuse the start with a message for any other non-F16 type instead of computing with it.

Compatibility: F16 files take the same path as before; F32 files get the same arithmetic as before. Every Metal reader of this tensor (decode, verify windows, prompt chain) goes through kw_arg() / kw_host(), so all of them see the converted copy. Metal only: the CUDA / HIP / SYCL engines read this tensor from the pack, where iq_pack --compat-bf16 already converts it (conversions.json: bf16->f16).

Cost: load time only - 40,960 values once and an 80 KB buffer; the decode and prompt kernels are unchanged.

Validation

Environment: Mac Studio M4 Max 36 GB, macOS 27.2; main at 9f7d977d + this patch; engine built with cmake --build build-metal --target strata; served by the Strata app 0.2.11 with the run config's exe pointed at the built engine; experts streamed from the SSD (STRATA_METAL_STREAM=1). UD-IQ2_XS freshly prepared from the original Unsloth shards (metal/tools/metal_repack.py --verify, metal/tools/stream_experts.py --verify, tools/iq_pack.py --compat-bf16).

Passed:

  • Released engine 0.1.57 (app 0.2.11) on this file: garbage from the first token - the problem reproduced.
  • Patched engine on the same file: coherent answers to a greeting and to a short reasoning question.
  • Isolation before the patch, same file, released engine: converting only blk.1.ple_conv1d to F16 inside the GGUF fixes the output; putting the original BF16 bytes back (SHA-256 06a84201...) brings the garbage back.
  • Metal parity programs at this revision (gemv, mma, dense_mm, pair, gr, gr2, route, gdn, fuse, qsa, expert, sample): all PASS. They do not cover this load path.

Failed:
none observed

Not run:

  • UD-IQ1_S / IQ1_M / Q2_0 / Q2_K_XL and the BF16 file: not downloaded; affected by their headers only.
  • An F32 file on the patched engine: not downloaded; that branch keeps the previous arithmetic.
  • Other Macs and the resident (non-streaming) mode: only one 36 GB machine available.
  • CUDA / HIP / SYCL builds: no hardware; the change is in Metal-only code.

Limits

  • No automated regression test: the load path needs a model file. Happy to add one where you prefer.
  • Related, not in this PR (unconfirmed static analysis): in the same low-bit files the hyper-connection inject matrices are BF16 while down / up are Q8_0. With the default Q8 HC read they get Q8_0 copies and are fine; with STRATA_HC_Q8=0, gr_read picks gr_down_q8_* (inject read as Q8_0) because injf only covers F32 injects, so a BF16 inject would be read as Q8_0. I'll report it in a separate issue.
  • Design note: this is the second Metal-only format special case for a small tensor (with the F32 branch above), and the STRATA_HC_Q8=0 inject case looks like a third. The pack path normalizes these tensors through iq_pack.py's FORM table; the Metal window has its own per-tensor assumptions. Applying the same FORM table at Metal load (convert or refuse, in one place) would close this class of bug. Happy to help if you want that direction.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5387 **Author:** [@qChauncey](https://github.com/qChauncey) **Created:** 11/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `metal-ple-conv1d-bf16` --- ### 📝 Commits (1) - [`dc1cf66`](https://github.com/Niko1221/Strata/commit/dc1cf66a9be6898ae392573d0b063a324e622069) [Engine] Metal: narrow a BF16 blk.1.ple_conv1d to F16 at load ### 📊 Changes **1 file changed** (+13 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `metal/src/engine/window.cpp` (+13 -4) </details> ### 📄 Description ## Problem and result On the Metal engine, Unsloth's UD-IQ2_XS (Dynamic v3.1, uploaded 2026-11-04) answers with garbage from the first token. Logits are finite (STRATA_METAL_NFCHECK: largest |logit| 19.6); DFlash2 accepts ~0 drafts. With this change the same file gives coherent answers. Cause: ple_conv_add reads blk.1.ple_conv1d.weight as F16. Window::init (metal/src/engine/window.cpp) converts it only when the GGUF stores it as F32 and otherwise passes the file's bytes through. In these files it is BF16 - the same size as F16 - so the BF16 words are read as F16, the layer-1 PLE conv is wrong, and every later layer follows. Which files store it how (headers read with tools/quantscope.py; not all of them were run): - BF16: unsloth UD-IQ1_S, UD-IQ1_M, UD-Q2_0, UD-IQ2_XS, UD-Q2_K_XL, BF16 - F32: unsloth UD-IQ3_XXS, UD-IQ4_XS, UD-Q3_K_XL, UD-Q4_K_XL, UD-Q5_K_XL, UD-Q6_K_XL, Q8_0; Gyro-M, Gyro-L - F16: every ISTA-DASLab GSQ-RCO file, Gyro-S Of the sets the 0.2.11 Mac app offers, UD-IQ1_M, UD-Q2_0, UD-IQ2_XS and UD-Q2_K_XL are affected. ## Related discussion none (focused fix). #4711 (UD-Q4_K_XL) stores ple_conv1d as F32, so it is not this bug. ## Approach Convert BF16 like F32 (bf16 -> f32 -> ref::f32_to_f16, round to nearest even - the same rule tools/iq_pack.py uses when it stores this tensor as F16 in the pack), and refuse the start with a message for any other non-F16 type instead of computing with it. Compatibility: F16 files take the same path as before; F32 files get the same arithmetic as before. Every Metal reader of this tensor (decode, verify windows, prompt chain) goes through kw_arg() / kw_host(), so all of them see the converted copy. Metal only: the CUDA / HIP / SYCL engines read this tensor from the pack, where iq_pack --compat-bf16 already converts it (conversions.json: bf16->f16). Cost: load time only - 40,960 values once and an 80 KB buffer; the decode and prompt kernels are unchanged. ## Validation Environment: Mac Studio M4 Max 36 GB, macOS 27.2; main at 9f7d977d + this patch; engine built with `cmake --build build-metal --target strata`; served by the Strata app 0.2.11 with the run config's exe pointed at the built engine; experts streamed from the SSD (STRATA_METAL_STREAM=1). UD-IQ2_XS freshly prepared from the original Unsloth shards (metal/tools/metal_repack.py --verify, metal/tools/stream_experts.py --verify, tools/iq_pack.py --compat-bf16). **Passed:** - Released engine 0.1.57 (app 0.2.11) on this file: garbage from the first token - the problem reproduced. - Patched engine on the same file: coherent answers to a greeting and to a short reasoning question. - Isolation before the patch, same file, released engine: converting only blk.1.ple_conv1d to F16 inside the GGUF fixes the output; putting the original BF16 bytes back (SHA-256 06a84201...) brings the garbage back. - Metal parity programs at this revision (gemv, mma, dense_mm, pair, gr, gr2, route, gdn, fuse, qsa, expert, sample): all PASS. They do not cover this load path. **Failed:** none observed **Not run:** - UD-IQ1_S / IQ1_M / Q2_0 / Q2_K_XL and the BF16 file: not downloaded; affected by their headers only. - An F32 file on the patched engine: not downloaded; that branch keeps the previous arithmetic. - Other Macs and the resident (non-streaming) mode: only one 36 GB machine available. - CUDA / HIP / SYCL builds: no hardware; the change is in Metal-only code. ## Limits - No automated regression test: the load path needs a model file. Happy to add one where you prefer. - Related, not in this PR (unconfirmed static analysis): in the same low-bit files the hyper-connection inject matrices are BF16 while down / up are Q8_0. With the default Q8 HC read they get Q8_0 copies and are fine; with STRATA_HC_Q8=0, gr_read picks gr_down_q8_* (inject read as Q8_0) because injf only covers F32 injects, so a BF16 inject would be read as Q8_0. I'll report it in a separate issue. - Design note: this is the second Metal-only format special case for a small tensor (with the F32 branch above), and the STRATA_HC_Q8=0 inject case looks like a third. The pack path normalizes these tensors through iq_pack.py's FORM table; the Metal window has its own per-tensor assumptions. Applying the same FORM table at Metal load (convert or refuse, in one place) would close this class of bug. Happy to help if you want that direction. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5412
No description provided.