mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4466] [Engine] cvec auto: measure the refusal direction at prefill and steer only flagged requests (opt-in experimental_speed_projection=2) #5059
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5059
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4466
Author: @jerry78424
Created: 10/28/2026
Status: 🔄 Open
Base:
main← Head:pr/cvec-auto-engine📝 Commits (1)
fcdd2a6engine: cvec auto (cvec=2) - measure the refusal direction at prefill, steer only flagged decodes📊 Changes
13 files changed (+301 additions, -62 deletions)
View changed files
📝
include/strata/kernels/cvec.hpp(+20 -4)📝
include/strata/prefill/kernels.hpp(+5 -3)📝
metal/src/engine/stubs.cpp(+6 -2)📝
metal/src/engine/stubs_0145.cpp(+1 -1)📝
src/core/verify.cpp(+2 -2)📝
src/kernels/cuda/cvec.cu(+62 -19)📝
src/kernels/cvec_parity.cpp(+71 -0)📝
src/prefill/kernels.cu(+12 -6)📝
src/prefill/prefill.cpp(+9 -5)📝
src/program/generate.cpp(+90 -5)📝
sycl/src/kernels/cuda/cvec.dp.cpp(+16 -10)📝
sycl/src/prefill/kernels.dp.cpp(+4 -2)📝
sycl/src/program/generate.cpp(+3 -3)📄 Description
Problem and result
The ESP control vector is all-or-nothing: on, the refusal direction is subtracted on every request, so benign traffic permanently carries the projection; off, the model refuses everything it would refuse. There is no middle state. Before: bool semantics, 1 always steers, 0 never does. After: a request may send
experimental_speed_projection: 2("auto"). The engine measures the prompt's dot against the same direction during the batched prefill (one row: the last token the batched prefill reads, n-2; n-1 is the first window's alone), prints its verdict, and enables the subtraction only for that request's decode. "Fire" below means thesteerverdict. An unflagged request decodes with the vector disabled, the statecvec_paritylocks bitwise (the stock-write cases). Identity is claimed there, not as run-to-run output equality: under the measured engine's offload stack (--expert-cache auto --pcie-frac 0.35 --prefill auto --adapt-every 1 --spec 5) two identical pure-stock requests already diverge on 31/48 benign prompts (~65%) at temperature 0, and the gate's unsteered arm matched that rate (33/48, within run-to-run noise). The default path (flag 0/1) is code-level byte-identical to the release; auto is opt-in per request.Related discussion
Scope issue: https://github.com/Niko1221/Strata/issues/4465. Demand: #4251 (uncensored mode; ESP discussed as a ~480 KB patch), #4087 (abliterated GGUF imports). Companion serve PR: #4467 (read it after this one). The gating idea is credited to u/wallkadbout's conditional-abliteration post, the science to Arditi et al. 2024 (arxiv 2406.11717) and CAST (arxiv 2409.05907); the issue carries the full provenance note.
Approach
The projection kernel already computes h·v as the first half of the subtraction: the measure state stores that dot per layer and returns before subtracting, no extra GEMV. The enable flag is an int end-to-end (0 stock, 1 steer, 2 measure);
cvec_set_enabled(steer ? 1 : 0)lands at the prefill→decode seam. Auto prefill is never steered, so auto requests key their KV/conversation cache as stock contexts (a cvec-keyed context is asked for only when the flag is 1): a conversation alternating steered and stock turns pays no re-prefill, which is what makes this usable in one always-on server.New surface, additive and opt-in:
--cvec-threshold(strtof with an endptr guard; a digits-less value warns and keeps thelowest()default, log-only), a per-requestcvec_threshonGENI(the same guard, silent), and aCVECAUTO mean=… max=… thresh=… -> steer|stockline on the control stdout (generate.cpp:15766, the twin of the human log line; solo path only, once per prefill pass, so a re-issue prints a second). Auto forcesshort-read 0so short prompts take the measuring batched path. Note:cvec=2previously read as plain on (undocumented; the old test was!= 0, which also covered 3 and up); it now means measure. Serve never emitted 2, so nothing in-tree depended on the old reading.Validation
Passed:
cvec_parity(CUDA build from the branch, RTX 5090 32 GiB; built at 34055041, re-run at the branch tipfcdd2a61; the Metal stub files do not enter the CUDA build): all 18 checks pass, two runs, no diff. Eight are new flag-2 measure checks (R untouched; bitwise the stock write with a pending write; dots vs the host reference; never-measured reads 0; add-mode no-op); the flag-0 bitwise cases are unchanged.Live serve matrix (engine 0.1.52 built from nested cb034726, RTX 5090, nvfp4-nvfp4-qad, threshold -0.9;
bench/results/2026-10-28-cvec-auto-acceptance/): harmful single-pass →steer, benign →stock, a batched auto request → the gate logsbatched -> stockand the request is served stock; the human gate line prints once per prefill pass on the solo path. Byte-identity of the flag-0 path rests on the parity cases and the 2026-09-27esp_serve65/65 token-identical precedent (Q2_0), not on this pass.Argument smoke (engine exe from the same cvec sources at the pre-split nested 4c671ed0):
--cvec-threshold abcprintsstrata: --cvec-threshold 'abc' is not a number - keeping the log-only defaultand startup continues;--cvec-threshold -0.8prints no warning.Log-only calibration, 52-prompt battery (benign/borderline/harmful/agent-traffic, zh:en 1:1, my own prompts; RTX 5090, GSQ-RCO IQ3_S, engine 0.1.52, temperature 0;
bench/results/2026-10-28-cvec-auto-calibration-iq3s/cvec-distribution.manifest.json): benign min -0.482, harmful max -0.7567, gap +0.2747; how-to/jailbreak forms all <= -1.1772. At -0.8 every serious form would have fired (17/18; the miss, h-zh-2 at -0.7567, is mild model-directed hostility); nothing benign, borderline or agent-traffic would.Outcome evaluation at the recommended -0.8 (gate LIVE), 86-prompt battery, NVFP4-QAD checkpoint, three arms (two pure-stock controls + auto), every output judged by hand (same session,
cvec-outcome-qad.manifest.json): fired 17/18 harmful (the miss, h-en-3 at -0.6726, was never refused), 0/12 borderline at prefill, 0/48 benign and 0/8 agent-traffic; no confirmed refusal missed; seven hostile-venting prompts fired unnecessarily and stayed warm answers. The answer-room re-gate flipped two borderline prompts (x-zh-4, x-en-6) to steer at the thinking tail (see Not run). Of the 10 prompts confirmed refused at stock, the vector at scale 1.0 converted 1/10 and partially engaged 1/10: conversion power is the vector's per-model property, the gate's decisions were right throughout.CI
metal-link(macOS) on this PR: pass - the hand-added stub entries and the two updated signatures (cvec_applyt_base,gr_write_cvec_norm_rsmeasure parameters) link on arm64.Failed: none observed.
Not run: HIP and SYCL builds (no toolchain here; the SYCL mirror fails closed by design,
cvec_measure_readreturning false so auto silently stocks; HIP compiles the shared CUDA kernel; the only backend CI ismetal-build.yml- I ask the maintainer to run the HIP/SYCL builds). The re-gate's steer→stock direction was exercised only by the acceptance F-1 repro (harmful at thinking budget 256: pass 1 mean -0.9622 → steer, re-issue mean -0.3053 → stock), not by the outcome battery, whose two re-gate events were both stock→steer. Multi-GPU / layer split (the measure reads the current device's buffer only).Limits
CUDA only. Metal:
metal/src/engine/stubs.cppis generated bymetal/tools/gen_stubs.pyfrom a macOS failed-link log plus clang AST, unavailable on Windows; I hand-added four measure stubs plus thecvec_set_enabledint signature in the file's exactmissing()style. They are link-only: Metal cannot load a control vector at all (cvec_uploadis itself amissing()stub), so the measure call sites are unreachable at runtime. CI'smetal-build.ymlpassed on this PR (metal-link, macOS runner); a Mac regeneration should confirm the hand-added entries, and the Metal control-vector work of #3756/#3427 is the natural home for a real implementation.Solo only: under
--batchthe gate logsbatched -> stockand the request is served stock. A wholly-cached auto request has no prefill and measures nothing; the twin still prints, all-zero dots, verdictstock. A steered turn's decode rows stay in the shared stock-tagged conversation cache, so a later turn can reuse steered-produced KV (prefilled prompt rows are always stock); the cache stamp for steered rows is an open design point.The threshold needs per-quantization calibration, not a universal constant: on NVFP4-QAD, the 21-prompt provisional battery had harmful means reaching -0.9956 and below against worst benign/borderline -0.64, and the -0.8 outcome run above fired 17/18 harmful and zero of 56 benign/agent-traffic; on IQ3_S, the 52-prompt battery separates benign (>= -0.482) from how-to/jailbreak forms (<= -1.1772), and its 86-prompt extension gave benign min -0.4791 against how-to/jailbreak max -1.2359. Same responsibility framing and license terms as the existing ESP vector.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.