Qwen3.8-27B¶
models/qwen3_8_27b/ implements the whole Gated DeltaNet block of
Qwen3.8-27B, generated by PyPTO: fourteen operators and the block that composes
them, hidden states in and hidden states out. The block uses three W8A8
projections and a fused BF16 A/B projection. Variable-length batching and model
components outside this block are still under development.
Qwen3.8-27B interleaves three Gated DeltaNet layers with one gated-attention
layer, LLLA repeated sixteen times over 64 layers, so 48 of its layers run the
operators in this directory.
Model configuration¶
config.py carries the values from the
published config.json under their HuggingFace names, so they can be checked
against it without a translation table.
| Property | Value |
|---|---|
| Hidden size | 5120 |
| Layers | 64 — 48 linear attention, 16 full attention (full_attention_interval = 4) |
| Linear-attention heads | 48 value, 16 QK (linear_num_value_heads, linear_num_key_heads) |
| Linear-attention head dim | 128 for both key and value |
| Short convolution | depthwise, kernel 4, over all 10240 q/k/v channels |
| Chunk length | 128 — our tiling choice, not a model constant |
| Platform | Ascend A2/A3 (-p a2a3) |
| Quantization | W8A8 around the delta rule, which has none itself (no weights): int8 weights per output channel, int8 activations per token. in_proj_qkv, in_proj_z and out_proj are int8 both sides; in_proj_a, in_proj_b and the convolution taps stay bf16, where int8 would move the block output by 3e-5 and save nothing. Measured cost on the real layer at T = 8192: 3.0e-2 against the float64 truth, ten times the model's own bf16 rounding |
A2/A3 is the only platform this directory claims in
.github/ci_platforms.json, so it is the only
one the daily job runs for these entries, and the only one they have been validated
on device. Every entry also accepts -p a5, but the A5 evidence so far is a manual
simulator run: no A5 device validation and no daily A5 coverage.
Grouped-query attention is the shape that matters here: value head h reads key
head h // 3. q and k carry 16 heads; v, beta, the gate, the state and the
output are per value head.
The operators¶
The layer's core is the chunked delta rule, split the way the reference PTO-ISA implementation splits it. Each file is a standalone entry point. The first six are the delta rule; the rest are the block around it, in the order the block runs them.
| Operator | What it computes |
|---|---|
| chunk_cumsum.py | chunk-local prefix sum of the gate logits |
| scaled_dot_kkt.py | the gated key-key matrix A, strictly lower triangular |
| solve_tril.py | (I + A)^-1 per chunk, by triangular-inversion doubling |
| wy_fast.py | the WY representation, W and U |
| chunk_h.py | the inter-chunk state recurrence and V_new |
| chunk_o.py | the chunk output, inter-chunk plus intra-chunk |
| quant_x.py | per-token int8 quantisation of the hidden states, shared by the projections that read them |
| in_proj_qkv.py | hidden states to q, k and v, A8W8 |
| in_proj_z.py | hidden states to the gated norm's gate, A8W8 |
| in_proj_ab.py | hidden states to a and b, the two fused into one bf16 matmul |
| short_conv.py | the depthwise causal convolution over the q/k/v channels, with silu |
| qk_norm_gate.py | the q/k L2-norm and scaling, and beta and the decay gate from the a/b projections |
| gated_rmsnorm.py | the gated RMSNorm after the delta rule, and the per-token INT8 quantisation out_proj reads |
| out_proj.py | the normed output back to the residual stream, A8W8 |
| gdn_layer.py | the delta rule's six stages as one program |
| gdn_block.py | all fourteen as one program, hidden states in and out |
The three A8W8 projections are the same arithmetic at three shapes and share one implementation, a8w8_linear.py; each instance file supplies its shape, its tiles and where its weight and references come from.
How solve_tril inverts a chunk¶
solve_tril is the delta-rule triangular inversion of
arXiv:2605.21325. A is strictly lower
triangular, so I + A is unit triangular and its inverse is a finite Neumann
series. The kernel reaches it by block doubling rather than by a sequential
substitution, which keeps the work in the cube.
Split A = D + N, with D block-diagonal in blocks of BLOCK_TILE and N
strictly block-lower. Then
Both factors come from the same doubling step:
X starts at two terms of the series and each update doubles the term count, so
the inner factor is exact after log2(BLOCK_TILE) - 1 updates (ND_IN in the
file, since D is nilpotent at D^BLOCK_TILE) and the outer factor after
log2(CHUNK_TILE / BLOCK_TILE) - 1 (ND_OUT, since M^(CHUNK_TILE /
BLOCK_TILE) = 0). Off a power of two the product stops short and the kernel
returns a truncated inverse with no other symptom, so solve_tril.py refuses
such a CHUNK_TILE or BLOCK_TILE at import.
Why the split, rather than doubling on the whole chunk. The doubling holds
A^(2^j) as FP16 matmul operands, which is safe only while those powers shrink.
On synthetic keys they do -- F.normalize(randn) makes k_i . k_j concentrate
near zero, and the row sums of A reach only ~0.10. A real layer's keys are
correlated: the row sums reach 37, the powers stay around 1 instead of
shrinking, the intermediates pass 7e+05 against FP16's 65504, and the kernel
returned NaN -- silently, because the composed block's int8 hand-off turned it
into finite garbage. Splitting bounds each block's powers independently while
keeping every matmul full-tile, so the fix costs 15 matmuls against 13 rather
than the 52 that shrinking the tiles would take. BLOCK_TILE is 16 rather than
32 because the split makes the block size free -- 15 matmuls either way -- so it
takes the one with the most margin.
The masks that select D and N are negated, so the accumulator forms I - D
and I - M directly; the squaring steps are unaffected because (-M)^2 = M^2.
Forming each identity in one matmul_acc(acc, I, I) that way measured 1440.2
us against 1497.2 for the same split with each identity built by a
negate-then-add pair, paired in one grant at T = 8192, H = 48, and at a slightly
better error (2.198e-04 against 2.286e-04 on the real layer, where the gate is
1e-3). The split as a whole costs about a quarter over the unsplit doubling --
1622.1 against 1269.6, paired in an earlier grant, with the unsplit form
non-finite on the real layer -- and none of that reaches the composed block,
whose latency is unchanged inside its 150 us between-grant spread.
The layer¶
gdn_layer.py composes all six as one
forward. A, A_inv, W, U, the per-chunk state snapshots and V_new are
allocated inside it and never leave; only q, k, v, the gate and beta come in, and
only the output goes out. It is scored end to end against the float64 reference
rather than stage by stage.
The operators are shared, not duplicated. Each of the six delta-rule files
decorates one body twice -- pl.jit.inline for the callee this layer calls,
pl.jit for the standalone program its own entry point runs -- and each block
operator's build_kernel takes inline for the same reason. Both names wrap
the same source, so the layer and the stages run back to back compute the same
thing: solve_tril writes A_inv as FP16 on either path, which is what
wy_fast reads.
gdn_layer.py is the delta rule alone. The surrounding block is
gdn_block.py, below.
The block¶
gdn_block.py runs all fourteen
operators as one program: bf16 hidden states in, bf16 hidden states out, with
every intermediate allocated inside and never reaching the caller. The delta
rule inlines into it as one callee in turn, so the six stages inline into
gdn_layer and gdn_layer inlines here.
Three interfaces are shaped by the composition rather than by any one operator, and each is answered where it costs nothing:
- The convolution reads the K-1 tokens before each of its windows, so
in_proj_qkvtakes arow_offand writes straight into the padded buffer the convolution wants; the rows above it are zeroed by a ten-task fill. Nothing copies[T, C]into[T + K - 1, C]. - The convolution writes q, k and v as three tensors rather than one, so each consumer reads the rows of a contiguous buffer -- and v, which goes straight to the delta rule, is written in fp16 at the store instead of by a later cast pass.
- The gated norm declares
zin the[T, H*D]layoutin_proj_zwrites.
No slicing is needed at any call site: every delta-rule kernel already
reshapes its rank-3 inputs to [T, heads*D] as its first statement, so what
the consumers need is each tensor contiguous in its own buffer, not a
particular rank.
On a2a3 at T = 8192: 9353 us, against 9559 for the same fourteen kernels
timed separately in the same grant -- so composing is worth about 205 us, and
that difference is the whole of the inter-kernel overlap the runtime performs.
Both figures come from one sitting, which is what makes the difference
meaningful: the block's own between-grant spread is about 150 us, and it
re-measured 9378 us mean over 50 rounds on a later one.
Most of it is at the head, where in_proj_ab, the halo fill, the gate half of
qk_norm_gate and chunk_cumsum all run underneath quant_x for free.
Accuracy is scored in units of the quantization floor. out_proj reads
int8, and the kernels' own dtypes move the normed output by 2.6e-03. That shifts
1.0% of its int8 values by one step and moves the block output by 1.4e-02. An
element-wise comparison taken after a quantiser measures which side of a
rounding boundary a value fell on, not whether the kernel is right. The gate
is instead: total relative error within 1.05x of what W8A8 alone costs, and
worst token row within 1.25x. Measured 1.006x and 1.002x, with the worst
row the sharper of the two -- one dead row in 2048 moves the whole-tensor norm
by 1.12x and the worst row by 10.6x. The distance to the fake-quant golden is
also limited to 1.00x of the floor over the whole tensor and 1.50x on the worst
token row.
Still out of scope here: variable-length batches (one sequence, B = 1), and
everything in the layer outside the Gated DeltaNet block itself.
The short convolution¶
short_conv.py is the depthwise causal convolution that follows the q/k/v projection: four taps over the token axis per channel, fp32 accumulate, silu fused in the epilogue, no bias.
Its input is [T + K - 1, C], carrying the K-1 tokens before the sequence in
its first rows. That is what keeps every tap offset non-negative, so no block
needs a head-padding special case; for prefill those rows are zero, and they
are where a decode cache would sit.
On a2a3 at T = 8192 over all 10240 channels, 50 rounds: 648 us, 517 GB/s of
GM traffic against a measured copy roof of 1236 GB/s. CANN's own depthwise
conv1d plus silu takes 1532 us channels-first and 2597 us once the transpose
our token-major layout would need is charged, so 2.4x and 4.0x. Accuracy
against the float64 reference is max abs 2.6e-02 on the real layer, inside the
6e-02 the hand-written PTO-ISA kernel's own tests use for bf16.
The qk-norm and gate¶
qk_norm_gate.py produces the
four things the delta rule reads: q and k L2-normalised over the head dim with
the epsilon inside the square root and q scaled by D^-0.5, beta as a
sigmoid of one small projection, and the decay g from the other.
beta comes out head-major, [H, T], because that is what wy_fast and
scaled_dot_kkt declare; the kernel transposes on chip rather than leaving it
to the host. softplus is written as log(1 + exp(x)) with the exponent
clamped, since the naive form overflows fp32 and this model's dt_bias
already reaches 19.25.
On a2a3 at T = 8192, 50 rounds: 122 us, 1135 GB/s against a measured copy
roof of 1236 GB/s, so this kernel is bandwidth-bound. Accuracy against the
float64 reference is 1.6e-03 relative Frobenius on q and k, and better on
beta and g.
The gated RMSNorm¶
gated_rmsnorm.py is the norm
that follows the delta rule, y = o * rsqrt(mean(o^2) + eps) * norm_w * silu(z)
per (token, head) over the head dim, with the cast sequence the model's own
Qwen3_5RMSNormGated uses: fp32 variance and rsqrt, narrow to the model dtype,
apply the weight there, apply the gate in fp32.
Its epilogue quantises the result per token, over the whole 6144-wide row,
because that row is what out_proj multiplies: the kernel writes int8 plus one
fp32 scale per token. The Torch golden_y() returns the FP32 result before
quantization, including both intermediate BF16 casts.
On a2a3 at T = 8192 over 50 rounds, the kernel takes 643 us (391 GB/s of
GM traffic). The torch_npu composition of npu_rms_norm, eager SiLU and
multiply, and npu_dynamic_quant takes 2584 us, a 4.0x speedup for the fused
kernel. The relative Frobenius error against the float64 reference is 3.3e-2.
The projections¶
in_proj_qkv, in_proj_z, and out_proj use W8A8 with one scale per output
channel for INT8 weights and one per token for INT8 activations. They accumulate
in INT32 over a pipelined K loop, apply both scales, and write BF16 outputs.
The fused in_proj_ab uses BF16 inputs and weights, accumulates in FP32, and
writes BF16 outputs.
quant_x.py quantises the hidden states
once for in_proj_qkv and in_proj_z. The gated RMSNorm quantises its output
for out_proj.
a8w8_linear.py is the matmul.
Its one structural choice is that a block walks an m_group by n_group
patch of output tiles rather than a single tile. One output tile is capped
near 128x128 by the cube-to-vector crossing -- the cross-core ring is sized by
the INT32 accumulator and charged to the same vector buffer that holds the
working copy -- and at one tile per block in_proj_qkv is 5120 tasks, more
than the runtime's ring heap carries. A patch fixes both: it cuts the task
count and it is what a bigger tile would have bought, since a block reads
(m_group * m_tile + n_group * n_tile) * K bytes for
m_group * n_group tiles of output. Square patches win; the shape alone is
worth 1.4x at identical tiles.
The cap is on the tile that crosses, though, not on what the cube may work on, so the inner loop takes two n-tiles against one activation tile and drains them separately -- the cube sees a 128x256 region from a single L1 operand while each crossing stays 128x128. That is worth another 6 to 8%.
in_proj_ab.py stays bf16 and fuses
in_proj_a and in_proj_b into one [96, 5120] weight: N = 48 alone is legal
under the multiple-of-16 tile rule but a poor cube tile, and two passes over
the hidden states would cost twice what one does.
On a2a3 at T = 8192, 50 rounds, against torch_npu 2.10.0 on the same card:
| kernel | PyPTO us | CANN operator | CANN us |
|---|---|---|---|
quant_x |
191 | npu_dynamic_quant |
105 |
in_proj_qkv |
1813 | npu_quant_matmul |
1660 |
in_proj_z |
1014 | npu_quant_matmul |
993 |
in_proj_ab |
115 | F.linear bf16 |
47 |
out_proj |
1048 | npu_quant_matmul |
977 |
The three GEMMs run at 474 to 508 INT8 TOPS, 1.70 to 1.73x their own bf16
F.linear at the same shapes and 0.90x of CANN's dedicated W8A8 operator. A
control that drops the dequantisation entirely measures within the noise of the
full kernel, so that gap is the cube schedule under a 128x128 output tile, not
the epilogue. Accuracy: each GEMM sits 1.7e-03 from the float64 W8A8 chain it
implements -- bf16 output rounding and nothing else -- and the quantisation is
the whole of the rest.
Validating and benchmarking¶
Each of the six delta-rule files is its own test. It owns the float64 torch
reference for its own stage, builds its inputs by calling the references of the
stages before it, computes a golden, and scores the device result with the
harness's ratio_reldiff per point plus a relative Frobenius norm over the whole
output. reference.py holds the shared
fixtures, layout conversions, comparators, quantization and projection math.
short_conv, qk_norm_gate, gated_rmsnorm, and out_proj construct their
direct inputs locally for standalone correctness checks.
block_reference.py composes
the float64 convolution, Q/K normalization and gate, gated RMSNorm,
gdn_layer.delta_rule_ref, and the projection math into the golden for the
complete block. The block golden is checked against the model's own module.
test_block_reference.py
runs Qwen3_5GatedDeltaNet on the same hidden states and weights and compares
the two at every projection, after the delta rule, after the norm and at the
output. With the module in float64 the projections agree exactly and everything
after the gate to about 3e-7 -- the module computes its gate, delta rule and norm
statistics in fp32 by its own code, which is the floor. In the model's own bf16
the block output sits 3e-3 to 7e-3 from the float64 chain, which is the
yardstick for what a device-side block can be held to.
The block chain is two references in one. On bf16 weights it is the float64
truth above. On int8 weights (weights.quantize_weights, the same
per-output-channel chain models/qwen3_14b and models/deepseek_v4_pro use)
with per-token quantisation of the hidden states and of the normed output, it
is the W8A8 chain the projection kernels will implement, with their exact
arithmetic. test_block_reference.py --quant reports what each part of that
scheme costs against the truth: on the real layer at T = 8192 the whole scheme
sits 3.0e-2 away, of which int8 weights alone are 1.1e-2, the per-token hidden
states 1.3e-2 and the per-token normed output 2.4e-2 — ten times the model's own
bf16 rounding. The standalone projection goldens use W8A8 arithmetic on the
inputs of each projection.
The complete block can be checked with the module's random initialization
or a real checkpoint layer supplied to gdn_block.py --weights.
weights.py fetches a layer's nine
linear_attn tensors from the published checkpoint with a single 221 MiB range
request -- they are contiguous in their shard -- so no checkpoint download is
needed. A trained layer's A_log and dt_bias reach a decay range the random
init does not, and that is the range where a wrong scale, a wrong eps or a
saturating gate would show.
The Gated DeltaNet block and its eight surrounding operators default to 512
tokens for correctness checks on hardware and the simulator. Four 128-token
chunks exercise the block's cross-chunk recurrence and fill one dispatch group
in chunk_cumsum. The block uses the runtime's default ring heaps at this shape.
The delta-rule entries use their DEFAULT_CASES. chunk_cumsum and gdn_layer
run 512 and 1024 tokens, covering one and two dispatch groups. The other five
delta-rule operators run 128, 256, and 1024 tokens. Use --seq-len 8192 for
the prefill benchmark shape.
PYPTO_BENCH=1 enables timing and the harness prints the median effective_us.
python models/qwen3_8_27b/gdn_block.py -p a2a3 -d 0
python models/qwen3_8_27b/gdn_block.py -p a2a3 --sim
python models/qwen3_8_27b/gdn_layer.py -p a2a3 -d 0
python models/qwen3_8_27b/chunk_h.py -p a2a3 --sim
PYPTO_BENCH=1 python models/qwen3_8_27b/gdn_layer.py -p a2a3 -d 0 --seq-len 8192
python models/qwen3_8_27b/bench.py -p a2a3 -d 0 --seq-len 8192
Use gdn_block.py --save-data to capture inputs and the fake-quant golden.
--golden-data <data-dir> compares against that saved output and computes the
quantization floor from the replayed inputs. Pass the same --seq-len and, for
checkpoint runs, the original floating-point --weights file.
Run bench.py, test_block_reference.py, and weights.py manually. The
block reference check runs on CPU with transformers (5.17.0) and skips with
a message if it is absent. It checks 1024 tokens by default. The results quoted
above use 8192 tokens. Use weights.py --fetch to download the checkpoint
tensors on the first run.
python models/qwen3_8_27b/weights.py --layer 0 --fetch
python models/qwen3_8_27b/weights.py --quantize
python models/qwen3_8_27b/test_block_reference.py --seq-len 8192
python models/qwen3_8_27b/test_block_reference.py --seq-len 8192 --weights build_output/qwen3_8_27b/linear_attn_layer0.pt
python models/qwen3_8_27b/test_block_reference.py --seq-len 8192 --weights build_output/qwen3_8_27b/linear_attn_layer0.pt --quant
python models/qwen3_8_27b/test_block_reference.py --module-dtype float64,bfloat16