Add static YaRN to Qwen rotary paths with independent CPU and ROCm vectors

This commit is contained in:
hazyumps authored and StillDeadcode committed 2026-10-07 11:09:27 +02:00
1 parent d23c3e0d17
commit 2af7fa4022
22 files changed
+7542 -27

No files matched your search

+2 -2
View File
@@ -371,8 +371,8 @@ that folds dequantisation into its epilogue still receives them.
| `attn_paged` | `q_len`(range) `head_dim` `gqa` `block_size`/der `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out` |
| `attn_dense` | `M`(range) `head_dim` `gqa` `causal` `dtype` `scale`:f64/opt `max_seqlen`/opt | `q`, `k`, `v`, `cu_seqlens` → `out` |
| `kv_store` | `M`(range) `head_dim` `n_head_kv` `block_size` `kv_dtype` `k_scale`:f64/opt `v_scale`:f64/opt | `k`, `v`, `slot_mapping`, `kv_cache`:inout |
| `rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `scale`:f64 `mode` `rotary_dim`/opt `sections`/opt | `qkv`:inout, `positions` |
| `rope_table` | `M`(range) `rot` `theta`:f64 `scale`:f64 `dtype` | `positions` → `cos_sin` |
| `rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `scale`:f64 `mode` `rotary_dim`/opt `sections`/opt `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `qkv`:inout, `positions` |
| `rope_table` | `M`(range) `rot` `theta`:f64 `scale`:f64 `dtype` `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `positions` → `cos_sin` |
| `qk_norm_rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `eps`:f64 `scale`:f64/opt `mode`/opt `rotary_dim`/opt `wadd`:f64/opt | `qkv`:inout, `positions`, `q_w`w, `k_w`w |
`attn_paged` is the ranged op whose band is `q_len`, not `M`: one query row at decode, up to 64 at
+85
View File
@@ -0,0 +1,85 @@
# Static YaRN for Qwen dense FP8 and DFlash2
Increasing `--max-model-len` reserves a larger serving window; it does not change
the model's rotary embeddings. A Qwen checkpoint trained at 262144 positions can
explicitly request static YaRN for a 524288-token deployment. Whether that extension
preserves useful accuracy must be measured on the model and workload.
The Qwen dense FP8 architecture reads the target's `text_config.rope_parameters`,
then `rope_parameters`, then their legacy `rope_scaling` spellings, in that order.
The first object with `rope_type` (or legacy `type`) is authoritative. The DFlash2
drafter reads its own `draft.rope_parameters` / `draft.rope_scaling`; it does not
inherit the target's scaling. The vision tower keeps its original axial rotations.
## Metadata
| Key within the rotary object | Meaning |
| --- | --- |
| `rope_type` | `yarn`; default/absent preserves stock behavior; `linear` divides positions uniformly |
| `factor` | finite number >= 1, required for YaRN |
| `original_max_position_embeddings` | positive integer, required; the **training** length, not the enlarged deployment/table length |
| `beta_fast`, `beta_slow` | defaults 32 and 1; finite, fast >= slow > 0 |
| `attention_factor` | positive finite cos/sin multiplier; default 1 + 0.1 ln(factor) for factor > 1, otherwise 1 |
| `truncate` | 1 (default): floor/ceil correction bounds; 0: retain fractional bounds |
`mscale` and `mscale_all_dim` variants are refused; specify `attention_factor`
explicitly. Other explicit rotary types are refused in this architecture. Rotary
width (including partial rotary), theta and M-RoPE section selection are unchanged.
## Convert a separate container
Use the original target and drafter checkpoints, the same quantization recipe,
and a **new output path**. `--reuse` is valid only for the same source weights and
recipe; it is not a way to mix weights from different model revisions.
```sh
rad-convert /checkpoints/Qwen3.8-27B-FP8 \
--draft-model /checkpoints/Qwen3.8-27B-DFlash2 \
--recipe data/recipes/q38-27b-fp8-df2.recipe \
--reuse /models/qwen3.8-27b-fp8.rad \
--set text_config.rope_parameters.rope_type=yarn \
--set text_config.rope_parameters.factor=2 \
--set text_config.rope_parameters.original_max_position_embeddings=262144 \
--set draft.rope_parameters.rope_type=yarn \
--set draft.rope_parameters.factor=2 \
--set draft.rope_parameters.original_max_position_embeddings=262144 \
--max-ctx 524288 --kv-cache-dtype bf16 \
-o /models/qwen3.8-27b-fp8-yarn2.rad
```
Run with `--plan-only` first. `--max-ctx` is the converter's graph bound, not a
replacement for the explicit YaRN metadata or the server's `--max-model-len`.
The bundled `.rad` by itself is not a converter input; keep the source checkpoints.
Check both target and draft settings with `rad-info --meta` after conversion.
Serve on enough memory for the requested window:
```sh
radiance --model /models/qwen3.8-27b-fp8-yarn2.rad \
--tp 4 --max-model-len 524288 --max-num-seqs 32 --kv-cache-dtype bf16
```
That cache flag controls **both** target and drafter KV. Startup capacity and
successful actual long requests must be checked; the command is not a promise
of concurrency or accuracy. Keep the unmodified model and serving setup until
long-context quality and speed meet the deployment's requirements.
## Kernel and test contract
`rope` and `rope_table` take optional resolved coefficients `yarn_factor`,
`yarn_low`, `yarn_high`, `yarn_magnitude`. Absent factor is the original path.
With YaRN, `scale` must be 1: inverse frequencies are blended rather than all
positions being divided. Axial rotary with YaRN is refused. Rebuild the bundled
plugins together because their shared op schemas must match.
The math follows the static YaRN formulation (Peng et al.) and is checked against
independent vLLM `YaRNScalingRotaryEmbedding` FP32 vectors for each backend. The fixture generator
is `tests/data/generate_yarn_reference.py`; it is not a runtime dependency.
Vectors cover target rotary width 64, draft width 128, an additional width 256,
and positions through 524287.
The CPU fixture uses torch CPU; the HIP fixture uses torch ROCm on gfx1201
(`generate_yarn_reference.py cuda`). CPU and ROCm `pow` differ by a few ULPs;
at 524K positions their cos/sin values can differ by 0.033 even within torch.
Compare each implementation against its backend's independent vectors.
The numerical check allows 0.004 absolute cos/sin error; fused versus unfused HIP rotation is checked
byte-for-byte separately. Existing stock rotary tests remain active.