Skip to content

PGuard: a native streaming guardrail that reads the backbone's own layers

THE ARGUMENT

Pushing safety detection inside the protected model's own forward pass can match dedicated external guardrails at roughly one to two percent serving overhead, but only if you self-host, can read intermediate states, and are willing to calibrate thresholds yourself.

On this page

You have 30-plus safety benchmarks to weigh and one procurement decision to make, but the vendor deck shows a single F1 number and no serving cost. The usual advice is to bolt a separate moderation model onto the front door: score the user input once, score the response once, and block whatever crosses a line. That works, and it is what Qwen3Guard-style external guardrails do, at the price of an extra inference pass per request and a judgment that never sees how the protected model itself understood the text. PGuard, from Alibaba AAIG, argues for the opposite arrangement: keep the backbone frozen, read the residual stream it already computes, and score risk token by token while generation runs [1].

The default: bolt a second model onto the front door

External guardrails train a standalone classifier and run it over inputs and completed responses, which buys accuracy at two costs that matter to a buyer. The first is a second inference pass on every request. The second is subtler: an external model sits outside the protected model and cannot see how that model internally represented the text, so two different understandings of the same sentence end up making the safety call [1].

Native guardrails try to use the protected model's own signals instead. The paper names HiddenDetect, which screens input-side risk before generation, and NExT-Guard, which monitors internal features at the token level; a token here is the smallest text fragment the model handles at one step. The authors report that these approaches are limited both by the quality of their safety representations and by the runtime cost of extracting them, and that in their own evaluation existing native methods trail leading external guardrails in aggregate detection while still adding appreciable serving overhead [1]. The default conclusion is therefore blunt: be accurate and pay for an external model, or be cheap and skip moderation.

What it reads: the residual stream, filtered by a layer gate

The backbone stays frozen and PGuard only reads. The read point is the residual stream, the tensor entering each layer, and the capture is read-only: it adds no state to the backbone and does not alter its inference pathway or generation behavior [1]. Because safety-relevant features appear at different depths for different inputs, and even at different positions within one input, PGuard applies dynamic layer gating. Each position-layer pair gets a score, the top-scoring layers are softmax-weighted, and the weighted sum becomes that position's fused feature. During calibration-set pretraining the gate works over a larger candidate layer set and the learned preferences fix the capture layers; during full training and deployment every captured layer participates with position-specific weights, and Gumbel noise is added to the scores before selection during training.

Supervision is segment-level: loss is computed only at annotated positions on tasks that have labels, so nobody has to tag every token. Multimodal inputs need no extra machinery, because image tokens occupy positions in the same sequence as text tokens and the backbone fuses visual and textual evidence before PGuard reads anything.

Three modeling variants, three cost points

The stateless variant classifies each position on its own, keeping no recurrent or cached state in training or inference, which makes prompt positions fully parallel and the per-step decode cost small. The EMA-attention variant keeps causal exponential moving averages at multiple decay rates for each captured layer, so short scales track recent features while long scales retain a slower summary, then attends from the current feature to those concatenated summaries. The GRU variant carries history in a recurrent state that is initialized during prefill by attention pooling over query features, giving the query-side prediction directly, and then updated once per generated token.

VariantMaintains stateCompute profileEnd-to-end overhead reported
StatelessNoPosition-wise, cheapestBelow 1.3%
Sequence (EMA-attention / GRU)One state per requestSequential per tokenBelow 2.4%

The paper's own summary is that sequence modeling improves response-level detection over the stateless variant on both text and multimodal tasks. For a buyer the implication is concrete: the cheapest tier buys you per-position judgments, and risks that accumulate across tokens fall outside what it can represent.

What the 30-plus benchmarks actually report

The evaluation spans more than 30 benchmarks across text safety, multimodal safety, jailbreaks, cyber and CBRN risk, multilingual scenarios, and real-world traffic [1]. Comparisons include the external Qwen3Guard, the native HiddenDetect and NExT-Guard, and Llama Guard 4.

SettingMetricPGuardComparison
Qwen3.5-9B, text queriesAverage F10.836Approaches dedicated external Qwen3Guard
Qwen3.5-9B, multimodal queriesAverage F10.741HiddenDetect 0.638; Llama Guard 4 0.474
Qwen3.5-122B-A10B on SGLangEnd-to-end overhead0.82-2.26%not applicable

Two evaluations are easy to omit from a procurement deck and hard to ignore in production. One is over-refusal on benign traffic, meaning normal content flagged as risky. The other is early interception, which asks whether risk scores stabilize early in generation, what the paper calls prefix stability. Both decide whether the thing can be switched on at all, since a guardrail that misfires gets disabled and one that fires late prevents nothing. Category-specific thresholds and enforcement actions are also configurable at deployment time.

The real cost is synchronization, not arithmetic

Serving engines such as vLLM and SGLang sustain throughput through batching, parallel execution, and careful GPU synchronization. Reading intermediate representations and reporting risk scores inserts extra dependencies into exactly that schedule, and the paper states plainly that a badly placed synchronization can stall an active batch, making the overhead far larger than the guard computation itself [1]. PGuard responds with four techniques: scoring only where needed, parallel prefill execution, non-blocking guard execution, and fused guard computation. The SGLang integration leaves the engine codebase untouched, returns scores through engine metadata, and lets the application apply its own policy when a score crosses a threshold. A two-tier deployment keeps a native screen in front and an external adjudicator behind.

When this approach does not apply

  • It fails when you cannot read intermediate states, for instance a backbone rented purely as an API or an inference stack that will not let you hook in, because the method requires a frozen backbone whose residual stream you can capture plus control over the serving layer.
  • It does not apply when you need the model itself to stop. The design leaves the original inference pathway and generation behavior unchanged, so blocking is the application's job, not the model's.
  • It is out of scope when you have no calibration budget. Thresholds and enforcement actions are configurable per category at deployment, which means there is no universal default you can simply inherit.
  • Coverage follows your label taxonomy, since segment-level supervision only computes loss at annotated positions on labeled tasks; risk categories nobody labeled will not appear on their own.
  • The reported numbers are scoped: 0.82-2.26% was measured on Qwen3.5-122B-A10B served by SGLang, and 0.836 and 0.741 are averages on Qwen3.5-9B. Change the backbone, the engine, or the language mix and these do not transfer.
  • Full integration exists for SGLang today, with a vLLM integration described as in progress.

References

  1. PGuard: A Native, Streaming, and Plug-in Guardrail for Multimodal Large Language Models

Sign in to Padda

A code in your inbox. No password to remember.

Connecting to sign-in…