Transformer-based NLP models remain vulnerable to adversarial perturbations, yet existing repair methods face a fundamental trade-off: gradient-based approaches offer flexibility but lack verifiability and often overfit; methods that do provide repair guarantees are restricted to the final layer or small networks, significantly limiting the parameter search space available for repair. We present WARP (Weight-Adjusted Repair with Provability), a constraint-based repair framework that extends repair beyond the last layer of Transformer models. WARP formulates repair as a convex quadratic program derived from a first-order linearization of the logit gap, enabling tractable optimization over a high-dimensional parameter space. Under the condition that the first-order approximation holds, this formulation induces three per-sample guarantees: (i) a positive margin constraint ensuring correct classification on repaired inputs, (ii) preservation constraints over a designated remain set, and (iii) a certified robustness radius derived from Lipschitz continuity. To ensure feasibility across varying model architectures, we introduce a sensitivity-based preprocessing step that conditions the optimization landscape accordingly. We further show that the iterative optimization procedure converges to solutions satisfying all repair constraints under mild assumptions. Empirical evaluation on encoder-only Transformers with varying layer architectures validates that these guarantees hold in practice while improving robustness to adversarial inputs. Our results demonstrate that guaranteed, generalizable Transformer repair is achievable through principled constraint-based optimization.
Figures & tables
Symbol
Shape
Description
Symbol
Shape
Description
v(x)
Rdin
Input embedding to repair layer
U(t)
Rdout×r
Top- r left singular vectors of W(t)
W
Rdout×din
Repair-layer weight (only modified param.)
B
Rr×din
Coefficient matrix for low-rank update
b
Rdout
Repair-layer bias (fixed)
β
Rrdin
Vectorisation of B ; QP decision variable
Wc
RC×dout
Classification head (fixed)
κ(v)
scalar
Gap Sensitivity Norm (GSN)
bc
RC
Classification bias (fixed)
γs,γh
scalar
Repair / remain gap margins
W
Rdout×din
Final converged repair-layer weight
ρ,λ
scalar
Regularisation / slack-penalty coeff.
Table 1: Principal notation used in § 3 .
Task
Model
∣S∣
∣P∣
∣Gen∣
∣AtkGen∣
SST-2
DistilBERT
108
800 †
2,000 †
1,207
BERT
105
800 †
2,000 †
1,207
RTE
DistilBERT
47
200 ‡
500 ‡
222
BERT
50
200 ‡
500 ‡
222
Table 2: Dataset configurations. ∣S∣ : repair set; ∣P∣ : remain set; ∣Gen∣ : general accuracy evaluation set; ∣AtkGen∣ : attack generalization set. † Yelp Polarity test split ( Zhang et al., 2015 ) . ‡ RTE training split.
Task
Model
Repair Layer
Activation Function
Type
Init. κ
SST-2
DistilBERT
pre_classifier ( 768×768 )
ReLU
A
5.43
BERT
bert.pooler.dense ( 768×768 )
tanh
B
0.58
RTE
DistilBERT
pre_classifier ( 768×768 )
ReLU
A
4.79
BERT
bert.pooler.dense ( 768×768 )
tanh
B
0.52
Table 3: Model configurations. GSN κ is measured at initialisation (pre-GSN-FT). Type A repair layers are fully updated during downstream fine-tuning (high initial κ ); Type B receive minimal task-specific gradient updates (low initial κ ).
Model
Method
Repair Acc ↑
Remain Acc ↑
General Acc ↑
Atk. Gen ↑
SST-2 (sentiment classification)
DistilBERT
Original
5.6%
98.1%
98.2%
22.3%
Same-layer LoRA ( r=2 )
75.9%
99.0%
97.5%
56.7%
All-layers Full FT
100.0%
99.5%
93.6%
50.2%
WARP (ours)
100.0%
100.0%
91.4%
69.0%
BERT
Original
1.0%
98.2%
98.5%
23.2%
Table 4: Main experimental results on SST-2 and RTE. “Repair Acc” is the accuracy on the repair set S ; “Remain Acc” on the remain set P ; “General Acc” on the held-out evaluation set; “Atk. Gen” is the mean accuracy across six adversarial attack methods (higher is better for all metrics). All sets are defined with respect to the post-GSN-FT weights; see § 4.1 .
SST-2
RTE
DistilBERT
BERT
DistilBERT
BERT
Cert. 1
Samples with gap ≥γs
108/108
105/105
47/47
50/50
gap min / mean / max
1.000/1.291/2.360
1.000/1.431/2.814
1.000/1.244/2.662
1.000/1.866/4.843
Cert. 2
Samples with remain gap ≥γh
798/800
786/800
200/200
200/200
remain gap min / mean
0.300/1.059
0.300/1.915
0.300/0.946
0.300/0.951
Cert. 3
ε∗ min/median/mean/max
0.172/0.211/0.222/0.405
0.014/0.016/0.020/0.039
0.283/0.305/0.352/0.753
0.022/0.028/0.041/0.106
Table 5: Repair certificate verification (WARP, after QP repair).
SST-2
RTE
DistilBERT
BERT
DistilBERT
BERT
Pre
Post
Pre
Post
Pre
Post
Pre
Post
GSN κ
5.43
7.43
0.58
1.31
4.79
8.48
0.52
1.60
Atk. Gen
22.3%
20.9%
23.2%
22.5%
37.4%
37.7%
39.3%
42.8%
Best step
—
4
—
20
—
23
—
27
∥W∥2
1.506
2.238
13.408
13.423
1.140
2.949
13.413
13.558
Table 6: Effect of GSN-FT. Attack generalization is reported before and after GSN-FT to confirm that generalization gains originate from the QP step rather than preprocessing.
Appendix figures & tables8 assets
Supplementary material from the paper’s appendix.
Appendix
Symbol
Description
Value
r
Rank of weight perturbation
2
γs
Repair set gap margin target
1.0
γh
Remain set gap margin lower bound
0.3
λ
Slack penalty coefficient
50.0
ρ
regularization coefficient
2.0
T
Maximum iterations
300
Appendix
Table 7: QP repair hyperparameters.
Parameter
Value
Max steps TFT
30
Learning rate
1×10−3
Batch size
32
Auxiliary set size Naux
8
Trainable parameters
repair layer W + classification head Wc (592,130 total)
Checkpoint selection criterion
step with maximum GSN κ
Appendix
Table 8: GSN-FT hyperparameters.
SST-2
RTE
DistilBERT
BERT
DistilBERT
BERT
Multiplier k
Flipped
Cum.%
Flipped
Cum.%
Flipped
Cum.%
Flipped
Cum.%
≤1×
0/108
0.0%
0/105
0.0%
0/47
0.0%
0/50
0.0%
≤20×
37/108
34.3%
0/105
0.0%
13/47
27.7%
0/50
0.0%
≤25×
78/108
72.2%
0/105
0.0%
34/47
72.3%
0/50
0.0%
≤35×
107/108
99.1%
0/105
0.0%
47/47
100.0%
0/50
0.0%
Appendix
Table 9: Expanding-radius Monte Carlo stress test (tightness ratio: smallest multiplier k such that a flip is observed within k×ε∗ ).
Distance band ( d/εnearest∗ )
Samples
Accuracy
≤20×
5
100.0%
20× – 25×
49
87.8%
25× – 30×
180
82.2%
30× – 35×
267
74.5%
35× – 40×
271
68.6%
40× – 50×
324
60.5%
Appendix
Table 10: Accuracy of the repaired DistilBERT model (SST-2) on adversarial samples grouped by distance to the nearest repaired embedding as a multiple of ε∗ .
Adversarial robustness of language models studies how to keep their outputs and decisions reliable under manipulative prompts, input changes, and malicious fine-tuning.
We introduce a constrained optimization framework for training transformers that behave like optimization descent algorithms. Specifically, we enforce layerwise descent constraints on the objective function and replace standard empirical risk minimization (ERM) with a primal-dual training scheme. This approach yields models whose intermediate representations decrease the loss monotonically in expectation across layers. We apply our method to both unrolled transformer architectures and conventional pretrained transformers on tasks of video denoising and text classification. Across these settings, we observe constrained transformers achieve stronger robustness to perturbations and maintain higher out-of-distribution generalization, while preserving in-distribution performance.
Javier Porras-Valenzuela, Samar Hadou, Alejandro Ribeiro
Department of Electrical and Systems Engineering, University of Pennsylvania, Philadelphia, PA, United States of America.
To ensure the overall quality of AI-enabled software, not only traditional software components but also AI components need to be tested and repaired. Among AI components, Transformer models are increasingly integrated into software systems, which makes their misbehaviors critical. Although prior work in the software engineering community has proposed deep neural network (DNN) repair methods, most overlook Transformer-specific structures. We propose RepTran, a search-based repair method for Transformer models. It targets their feed-forward networks (FFNs), which play a central role in the architecture. RepTran identifies suspicious weights by combining two types of scores: a variance-based neuron score and an existing bidirectional score. It then iteratively optimizes these weights using differential evolution. Our evaluation includes 18 fault benchmarks constructed from CIFAR-100 and Tiny-ImageNet. We compare RepTran against three baselines: random weight selection, Arachne (a state-of-the-art DNN repair method), and ArachneW, which enables Arachne to control the number of selected weights. RepTran achieved an average repair rate of 74.7%, statistically outperforming random selection and Arachne across all benchmarks. Effect size analysis revealed that RepTran achieved higher repair rates than ArachneW regardless of the number of selected weights. These results suggest that RepTran is effective for enhancing the reliability of AI-enabled software.
Yuta Ishimoto, Paolo Arcaini, Fuyuki Ishikawa +3
The University of Osaka, Japan · National Institute of Informatics, Japan · Kyushu University, Japan +1
Pre-trained transformers have demonstrated remarkable generalization abilities, at times extending beyond the scope of their training data. Yet, real-world deployments often face unexpected or adversarial data that diverges from training data distributions. Without explicit mechanisms for handling such shifts, model reliability and safety degrade, urging more disciplined study of out-of-distribution (OOD) settings for transformers. By systematic experiments, we present a mechanistic framework for delineating the precise contours of transformer model robustness. We find that OOD inputs, including subtle typos and jailbreak prompts, drive language models to operate on an increased number of fallacious concepts in their internals. We leverage this device to quantify and understand the degree of distributional shift in prompts, enabling a mechanistically grounded fine-tuning strategy to robustify LLMs. Expanding the very notion of OOD from input data to a model's private computational processes, a new transformer diagnostic at inference time is a critical step toward making AI systems safe for deployment across science, business, and government.
Praneet Suresh, Jack Stanley, Sonia Joseph +2
Mila Quebec AI Institute · Meta AI · Sapient Intelligence