Skip to content

dflash: read model params from GGUF at runtime, support any qwen35 size - #79

Merged
davide221 merged 3 commits into
Luce-Org:mainfrom
howard0su:qwen35
May 3, 2026
Merged

davide221 merged 3 commits into
Luce-Org:mainfrom
howard0su:qwen35

Conversation

@howard0su

@howard0su howard0su commented May 2, 2026 •

Copy link
Copy Markdown
Contributor

Replace all hardcoded Qwen3.5-27B dimensions with runtime values from
TargetWeights (populated by the GGUF loader), enabling support for any
Qwen3.5-dense model size (4B, 9B, 27B).

Add runtime hparams (n_head, n_head_kv, head_dim, n_embd, n_ff, n_layer)
to DraftWeights, populated by both loaders:

Changes:

  • gguf_target_loader.cpp: Remove exact-match validation against 27B dims,
    replace with non-zero + structural invariant checks (key_length==value_length,
    ssm_d_inner%dt_rank==0, n_layer%fai==0). Compute capture_layer_ids from
    n_layer using formula: step=(n_layer-2)/(N-1), ids[k]=1+k*step.
  • qwen35_target_graph.cpp: Delete namespace q35 (22 compile-time constants),
    thread TargetWeights hparams through build_full_attn_block() and
    build_delta_net_block(). Derive n_rot from rope_sections. Replace hardcoded
    CAPTURE_LAYERS with w.capture_layer_ids.
  • internal.h: Add capture_layer_ids[5] to TargetWeights.
  • safetensors_draft.cpp: stores hardcoded values in DraftWeights fields
    (safetensors has no metadata to read from)
  • gguf_draft_loader.cpp: reads dimensions from GGUF metadata instead of
    validating against DFLASH27B_TARGET_* macros
  • qwen3_dflash_graph.cpp: uses w.n_head/n_head_kv/head_dim/n_layer from
    DraftWeights instead of DFLASH27B_TARGET_* macros

This enables loading DFlash draft models for any Qwen3.5 target size
(4B, 9B, 27B) without recompilation.

howard0su and others added 2 commits May 2, 2026 21:28
Replace all hardcoded Qwen3.5-27B dimensions with runtime values from
TargetWeights (populated by the GGUF loader), enabling support for any
Qwen3.5-dense model size (4B, 9B, 27B).

Changes:
- gguf_target_loader.cpp: Remove exact-match validation against 27B dims,
  replace with non-zero + structural invariant checks (key_length==value_length,
  ssm_d_inner%dt_rank==0, n_layer%fai==0). Compute capture_layer_ids from
  n_layer using formula: step=(n_layer-2)/(N-1), ids[k]=1+k*step.
- qwen35_target_graph.cpp: Delete namespace q35 (22 compile-time constants),
  thread TargetWeights hparams through build_full_attn_block() and
  build_delta_net_block(). Derive n_rot from rope_sections. Replace hardcoded
  CAPTURE_LAYERS with w.capture_layer_ids.
- internal.h: Add capture_layer_ids[5] to TargetWeights.

Co-authored-by: Copilot <[email protected]>
Add runtime hparams (n_head, n_head_kv, head_dim, n_embd, n_ff, n_layer)
to DraftWeights, populated by both loaders:

- safetensors_draft.cpp: stores hardcoded values in DraftWeights fields
  (safetensors has no metadata to read from)
- gguf_draft_loader.cpp: reads dimensions from GGUF metadata instead of
  validating against DFLASH27B_TARGET_* macros
- qwen3_dflash_graph.cpp: uses w.n_head/n_head_kv/head_dim/n_layer from
  DraftWeights instead of DFLASH27B_TARGET_* macros

This enables loading DFlash draft models for any Qwen3.5 target size
(4B, 9B, 27B) without recompilation.

Co-authored-by: Copilot <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot 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.

3 issues found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="dflash/src/gguf_draft_loader.cpp">

<violation number="1" location="dflash/src/gguf_draft_loader.cpp:152">
P2: Draft GGUF metadata for `dflash.block_size` / `dflash.n_target_layers` is no longer validated, so incompatible draft files can load despite the graph still assuming a fixed draft block size.</violation>

<violation number="2" location="dflash/src/gguf_draft_loader.cpp:180">
P1: Unbounded `block_count` is trusted for `out.layers` sizing and narrowed to `int` without range checks, so a malformed GGUF can trigger huge allocation/OOM or overflow behavior.</violation>
</file>

<file name="dflash/src/qwen35_target_graph.cpp">

<violation number="1" location="dflash/src/qwen35_target_graph.cpp:414">
P2: Derived M-RoPE width from GGUF metadata is used without validating it against the current head dimension, so malformed rope_sections can produce invalid rotary dimensions and undefined ggml behavior.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread dflash/src/gguf_draft_loader.cpp Resolved
Comment thread dflash/src/gguf_draft_loader.cpp Resolved
// qwen35 that's rope.dimension_count=64 (out of head_dim=256).
int n_rot = 64; // qwen35.rope.dimension_count
// ── M-RoPE (multi-axis rotary). n_rot derived from rope_sections.
const int n_rot = 2 * (w.rope_sections[0] + w.rope_sections[1] +

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.

P2: Derived M-RoPE width from GGUF metadata is used without validating it against the current head dimension, so malformed rope_sections can produce invalid rotary dimensions and undefined ggml behavior.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At dflash/src/qwen35_target_graph.cpp, line 414:

<comment>Derived M-RoPE width from GGUF metadata is used without validating it against the current head dimension, so malformed rope_sections can produce invalid rotary dimensions and undefined ggml behavior.</comment>

<file context>
@@ -400,50 +375,54 @@ static ggml_tensor * build_full_attn_block(
-    //    qwen35 that's rope.dimension_count=64 (out of head_dim=256).
-    int n_rot = 64;  // qwen35.rope.dimension_count
+    // ── M-RoPE (multi-axis rotary). n_rot derived from rope_sections.
+    const int n_rot = 2 * (w.rope_sections[0] + w.rope_sections[1] +
+                           w.rope_sections[2] + w.rope_sections[3]);
     int sections[4];
</file context>
Suggested change
const int n_rot = 2 * (w.rope_sections[0] + w.rope_sections[1] +
const int n_rot = 2 * (w.rope_sections[0] + w.rope_sections[1] +
w.rope_sections[2] + w.rope_sections[3]);
GGML_ASSERT(n_rot > 0 && n_rot <= head_dim);

Address three cubic AI review findings on PR Luce-Org#79:

1. Draft loader: re-validate dflash.block_size and
   dflash.n_target_layers against the compiled constants. The graph
   builder still hardcodes both, so a draft GGUF with mismatched
   metadata would silently mis-shape the graph.

2. Draft loader: bound block_count and other hparams before casting
   to int and sizing out.layers. A malformed GGUF could otherwise
   trigger huge allocations or signed-int overflow. Limits are
   conservative (n_layer<=1024, n_embd<=131072, n_ff<=524288,
   n_head<=1024, head_dim<=1024) and enforce GQA invariants.

3. Target loader: require qwen35.rope.dimension_sections, validate
   non-negative entries, and check 0 < 2*sum(sections) <= head_dim
   so n_rot is always safe at the ggml_rope_multi call site in
   qwen35_target_graph.cpp.

Co-Authored-By: WOZCODE <[email protected]>
@davide221
davide221 merged commit 7f96d4c into Luce-Org:main May 3, 2026
1 check passed
javierpazo added a commit to javierpazo/lucebox-hub that referenced this pull request May 9, 2026
Follow-up to merged Luce-Org#79 ("read model params from GGUF at runtime,
support any qwen35 size"). Luce-Org#79 covers the target loader and the
common drafter fields, but the fallback chain in gguf_draft_loader
still requires the legacy `dflash.n_target_layers` key to be
present.

Drafters published with the new metadata key naming
(`dflash-draft.dflash.target_layer_ids` plus
`n_target_features`) hit the path where the legacy key is missing
and the loader fails. Concrete case: the published Q8 GGUF drafter
for Qwen3.6-27B-DFlash.

This change derives `n_target_layers` in two steps:

  1. If `target_layer_ids` is present, use its length.
  2. Otherwise, if `n_target_features` and `n_embd` are both
     present, use `n_target_features / n_embd` (with a sanity
     check that the division is exact).

If neither is available, the loader still fails with the same
honest error as before. The legacy key path is untouched.

Validation (RTX 6000 Ada sm_89, Qwen3.6-27B Heretic Q4_K_M target,
Q8 GGUF drafter via the new metadata):

  Loaded `SWA layers: 4/5`, decode 21.06 tok/s, no fallback chain
  errors during init.

Verification vs existing community PRs:

  COMP-COMPL with Luce-Org#79 (merged 2026-05-03). Luce-Org#79 covered target
  loader and drafter fields generically. This PR is a small
  follow-up for the case where only the new metadata is present
  on the drafter side.

Author: Javier Pazo <[email protected]>
@howard0su
howard0su deleted the qwen35 branch May 22, 2026 04:10
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
Address three cubic AI review findings on PR Luce-Org#79:

1. Draft loader: re-validate dflash.block_size and
   dflash.n_target_layers against the compiled constants. The graph
   builder still hardcodes both, so a draft GGUF with mismatched
   metadata would silently mis-shape the graph.

2. Draft loader: bound block_count and other hparams before casting
   to int and sizing out.layers. A malformed GGUF could otherwise
   trigger huge allocations or signed-int overflow. Limits are
   conservative (n_layer<=1024, n_embd<=131072, n_ff<=524288,
   n_head<=1024, head_dim<=1024) and enforce GQA invariants.

3. Target loader: require qwen35.rope.dimension_sections, validate
   non-negative entries, and check 0 < 2*sum(sections) <= head_dim
   so n_rot is always safe at the ggml_rope_multi call site in
   qwen35_target_graph.cpp.

Co-Authored-By: WOZCODE <[email protected]>
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
dflash: read model params from GGUF at runtime, support any qwen35 size
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
Follow-up to merged Luce-Org#79 ("read model params from GGUF at runtime,
support any qwen35 size"). Luce-Org#79 covers the target loader and the
common drafter fields, but the fallback chain in gguf_draft_loader
still requires the legacy `dflash.n_target_layers` key to be
present.

Drafters published with the new metadata key naming
(`dflash-draft.dflash.target_layer_ids` plus
`n_target_features`) hit the path where the legacy key is missing
and the loader fails. Concrete case: the published Q8 GGUF drafter
for Qwen3.6-27B-DFlash.

This change derives `n_target_layers` in two steps:

  1. If `target_layer_ids` is present, use its length.
  2. Otherwise, if `n_target_features` and `n_embd` are both
     present, use `n_target_features / n_embd` (with a sanity
     check that the division is exact).

If neither is available, the loader still fails with the same
honest error as before. The legacy key path is untouched.

Validation (RTX 6000 Ada sm_89, Qwen3.6-27B Heretic Q4_K_M target,
Q8 GGUF drafter via the new metadata):

  Loaded `SWA layers: 4/5`, decode 21.06 tok/s, no fallback chain
  errors during init.

Verification vs existing community PRs:

  COMP-COMPL with Luce-Org#79 (merged 2026-05-03). Luce-Org#79 covered target
  loader and drafter fields generically. This PR is a small
  follow-up for the case where only the new metadata is present
  on the drafter side.

Author: Javier Pazo <[email protected]>
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