メインコンテンツまでスキップ

Per-Iteration Safety Check

The Problem

Language models can produce degenerate hidden states — exploding norms, NaN propagation, adversarial-triggered instability. In a standard transformer, you can only check after the full forward pass completes (all 24 layers). By the time you detect the problem, you've already burned the full compute.

In a looped transformer, the same block runs L times. If the hidden state explodes at iteration 3, iterations 4–8 are wasted compute that also produces garbage.

The Solution

Check the residual L2 norm after every iteration. Abort the moment it exceeds a threshold. This gives L checkpoints per token instead of 1.

Cost Analysis

OperationFLOPs (D=2048)Relative cost
L2 norm computation~2K
Attention (T=128)~4M2000×
Full block (QKV+attn+MLP)~34M17,000×

The check reads buf.main (FP32, already in L2 cache from the block that just wrote it) and computes a single scalar. It's essentially free.

Implementation

The kernel f32_l2_norm computes √(Σ x²) over the D-dimensional residual vector. One thread-block reduction, ~2 μs on H100.

Measured Results

On H100 / 2B INT4 model:

ThresholdBehaviorIter triggeredNorm value
10.0Aborts earlyiter 030.7
50.0Catches divergenceiter 262.4
1000.0Normal generationmax 45.2
100000.0Never triggers

Usage

CLI

tinyloop model.tinyloop generate \
--prompt "Hello" --loops 8 \
--safety-check --safety-threshold 50.0

Python

tokens = model.generate(
prompt, loops=8,
safety_check=True,
safety_norm_threshold=50.0
)

Server (native endpoint)

{
"prompt": "Hello world",
"loops": 8,
"safety_check": true,
"safety_norm_threshold": 50.0
}

Why This Is Architecture-Exclusive

In a standard transformer, layer 1's hidden state lives in a different representational space than layer 24's. A norm threshold that works for layer 1 would be meaningless for layer 24. In a looped transformer, every iteration operates in the same space with the same weights, so a single threshold applies uniformly.

Integration with Other Safety Features

FeatureScopeCostWhen it fires
Per-iteration norm checkPer token, per iteration0.05% of blockHidden state divergence
β monitoringPer generation call~0%Draft/verify agreement drops
Token-stable early exitPer token1 logit peekOutput stabilizes early
Constrained decodingPer tokenGrammar NFA stepInvalid output

The norm check is the earliest and cheapest safety gate — it fires before the model even produces a logit.