| 1491 | dump_slot(bb.dump, g, layer, src, off, n, stream); |
| 1492 | } |
| 1493 | bool block_layer_pre(const WeightTable& tables, const ModelGeometry& g, int64_t layer, int64_t pos, int32_t pos_base, const GdnBuffers& gb, const QsaState& qst, const QsaBuffers& qb, const MoEBuffers& mb, int64_t k, const BlockBuffers& bb, void* stream, std::string& err, const Doorbell* db, const PleRun* ple, int half, int stage_prefix) { |
| 1494 | // ================================ THE PLE, AT LAYER 1 ONLY ================================ |
| 1495 | // |
| 1496 | // **THIS IS THE CALL WHOSE ABSENCE FAILED GATE C1.** Layer 1 carries the six `blk.1.ple_*` tensors and no |
| 1497 | // other layer does; every part of the module was built and parity-tested and none of it was reachable from |
| 1498 | // here. The block's output is `hidden + gated + silu(conv)` on the RESIDUAL STACK ITSELF, so it is a |
| 1499 | // residual update applied BEFORE layer 1's own `gr_read` - which is what the architecture means by the |
| 1500 | // gathered rows arriving "before layer 2". |
| 1501 | // |
| 1502 | // **THE HASH AND THE GATHER ARE *NOT* HERE, AND THAT IS THE WHOLE REASON `ple_stage_token` EXISTS.** Both |
| 1503 | // are HOST operations - the hash is 64-bit multiply/xor that `qwen4exp.cpp` says is host-side because "ggml |
| 1504 | // has no int64 and no xor", and the table is a host mapping. This function is CAPTURED, so anything host |
| 1505 | // side in it runs ONCE, at capture time, and the graph then replays that result for every token: the engine |
| 1506 | // would gather the capture-time token's rows forever and produce a perfectly finite, perfectly stable, |
| 1507 | // completely wrong answer. It is the same bug as the doorbell's cloned literal, and it is silent. |
| 1508 | // |
| 1509 | // So the driver calls `ple_stage_token` once per token, BEFORE the graphs, and what is captured here is only |
| 1510 | // the device half: `ple_block` reading `emb_dev`, and the history shift. |
| 1511 | const bool fused = g_fused_gr && stage_prefix == 0 && half == 0 && |
| 1512 | strata::kernels::fused_gr_supported(g.n_embd, g.hc, g.hc_lr); |
| 1513 | // layer-1's FFN write has not been applied to R yet - unless a control vector follows it, which needs R |
| 1514 | bool pending_ffn = fused && layer > 0 && !strata::kernels::cvec().covers(layer - 1); |
| 1515 | if (ple != nullptr && ple->ready() && layer == 1 && (half == 0 || half == 1)) { |
| 1516 | if (pending_ffn) { |
| 1517 | const strata::kernels::GrShapes gs0{g.n_embd, g.hc, g.hc_lr}; |
| 1518 | gr_write(bb.R, bb.block_out, bb.inject2, gs0, bb.R, stream); |
| 1519 | pending_ffn = false; |
| 1520 | } |
| 1521 | const int64_t hcd = strata::kernels::NG_HC_DIM; |
| 1522 | strata::kernels::PleOut po; |
| 1523 | // Exports must not alias the block's internal workspace. The previous diagnostic views used a |
| 1524 | // different layout inside that workspace: exporting gated values overwrote normalized values |
| 1525 | // before the latter were copied to history. Allocate only the one export the recurrence needs. |
| 1526 | po.normalized = (float*) ((uint8_t*) ple->scratch + strata::kernels::ple_block_scratch_bytes()); |
| 1527 | // `result` IS THE RESIDUAL, in place: the block computes `hidden[i] + gated[i] + conv[i]` elementwise, |
| 1528 | // so reading and writing `R` at the same index is well-defined. |
| 1529 | po.result = bb.R; |
| 1530 | try { |
| 1531 | strata::kernels::ple_block(ple->emb_dev, bb.R, ple->hist, ple->w, po, ple->scratch, stream); |
| 1532 | } catch (const std::exception& error) { |
| 1533 | err = std::string("block_layer_pre PLE: ") + error.what(); |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | // ---- AND THE HISTORY ADVANCES, ONCE PER TOKEN, WHICH IS WHAT MAKES IT A CONV HISTORY. |
| 1538 | // |
| 1539 | // `ple_block` deliberately does NOT write back to the history - "the caller keeps ownership of its |
| 1540 | // state and this stays a pure function of its inputs, which is what makes the chunked-versus-single-shot |
| 1541 | // property testable" - so the shift is here. The layout is ROW-FASTEST (`hist[row + NG_HIST*channel]`, |
| 1542 | // from `ggml_reshape_3d(state, d_conv-1, conv_channels, n_seqs)`), so a row shift is a STRIDED copy of |
| 1543 | // NG_HIST-1 elements per channel and an append is a strided copy of one - not a flat memmove, which |
| 1544 | // would be the natural reading and would scramble the channels. |
| 1545 | strata::kernels::ple_history_advance(ple->hist, po.normalized, stream); |
| 1546 | if (cudaPeekAtLastError() != cudaSuccess) { |
| 1547 | err = "block_layer_pre: the PLE history shift failed"; |
| 1548 | return false; |
| 1549 | } |
| 1550 | } |
no test coverage detected