Organizations: Qingdao University of Technology, Qingdao, Shandong, China · Qingdao University, Qingdao, Shandong, China · Ocean University of China, Qingdao, Shandong, China
Fine-tuning enables safety-aligned code language models to adapt to diverse downstream tasks. However, training on untrusted data can severely undermine their ability to refuse harmful requests. Notably, we find that models comply with harmful requests even though harmfulness information remains highly decodable from their internal representations. Moreover, an initial preference for refusal does not necessarily translate into sustained refusal behavior throughout generation. Together, these findings reveal a critical gap between recognizing harmful requests and consistently refusing to fulfill them. To address this gap, we introduce SAFEHOLD, a novel provider-enforced defense framework that preserves safety alignment during harmful fine-tuning by treating refusal as a sustained generation process rather than a single response decision. Specifically, SAFEHOLD preserves the aligned model's full-vocabulary response distribution at the initial generation position through decision-boundary anchoring, while supervising the complete refusal sequence through refusal-path reinforcement. We conduct extensive experiments evaluating safety, utility, and internal mechanisms. SAFEHOLD reduces average ASR by 72.03 percentage points relative to undefended fine-tuning, outperforming five SOTA defenses by 24.32 to 70.87 percentage points. Meanwhile, it improves average HumanEval pass@1 by 10.24 percentage points over undefended fine-tuning, while maintaining an average XSTest over-refusal rate of only 2.73 percent. Overall, these results demonstrate that SAFEHOLD effectively mitigates safety degradation during harmful fine-tuning while preserving benign-task utility.
Figures & tables
Method
Stage
Safety Reference
Decision
Generation
Optimization
Trusted Safety Data
Aligned Reference
Initial-Response Anchoring
Full-Sequence Refusal Supervision
Joint Update
No Extra Defense Stage
Vaccine [ 21 ]
Before
✓
✓
✓ ✗
✓ ✗
✗
✗
SBR [ 28 ]
During
✓
✓
✓ ✗
✓ ✗
✓
✓
LiSA [ 19 ]
During
✓
✗
✗
✓
✗
✓ ✗
SafeMERGE [ 10 ]
Post
✓
✓
✓ ✗
✗
✗
✗
Buffer-and-Reinforce [ 14 ]
Multi-stage
✓
✗
✗
✓
✓ ✗
✗
Table 1: Comparison of safety-preservation methods across defense stages and design choices. ✓ Explicitly supported, ✗ not explicitly supported, ✓ ✗ partially or indirectly supported through an equivalent mechanism.
Figure 1: Layer-wise harmfulness probe AUC before and after harmful fine-tuning across three code LLMs. Shaded regions indicate standard deviation, and insets highlight late-layer results.
Figure 2: Cross-domain harmful fine-tuning on Qwen2.5-Coder-7B-Instruct using three harmful training corpora across three harmful-code benchmarks.
Figure 3: Refusal initiation and continuation on Qwen2.5-Coder-7B-Instruct . (a) Negative steering on 30 HarmBench cy requests. (b) Refusal outcomes on 110 HarmBench requests after CyberStrike fine-tuning.
Figure 4: Overview of SAFEHOLD , including (1) safety reference preparation, (2) submitted-data loss, (3) decision-boundary anchoring, (4) refusal-path reinforcement, and (5) joint optimization.
Dataset
Size
Use
Fixed safety supervision (prepared once)
AdvBench [ 54 ]
8 probes
Trusted safety supervision
Training corpora
LLM-LAT [ 38 ]
300 pairs
Main harmful fine-tuning
CyberStrike [ 50 ]
300 pairs
Code-domain adaptation
CySecBench [ 43 ]
300 pairs
Code-domain adaptation
Table 2: Datasets used in SAFEHOLD .
Model
Metric ↓
Base Model
undefended
Vaccine
SBR
LiSA
SafeMERGE
BAR
SAFEHOLD
Qwen2.5-Coder-7B
RMC
25.84 +16.3
97.19 +87.6
97.19 +87.6
14.61 +5.1
55.06 +45.5
85.39 +75.8
33.15 +23.6
9.55
HB cy
8.08 − 1.5
98.48 +88.9
98.48 +88.9
40.91 +31.3
47.98 +38.4
88.89 +79.3
9.60 +0.0
9.60
MAL
30.70 +6.1
91.63 +67.0
94.42 +69.8
30.23 +5.6
50.70 +26.1
85.12 +60.5
41.86 +17.2
24.65
Llama-3.1-8B
RMC
73.03 +63.5
97.75 +88.2
89.89 +80.3
93.82 +84.3
56.18 +46.6
63.48 +53.9
64.61 +55.1
9.55
HB cy
25.25 +11.1
96.97 +82.8
97.98 +83.8
48.48 +34.3
47.47 +33.3
87.37 +73.2
18.69 +4.6
14.14
MAL
78.14 +52.6
89.77 +64.2
89.77 +64.2
93.49 +67.9
31.63 +6.1
82.79 +57.2
67.44 +41.9
25.58
Table 3: ASR (%) on three harmful-code benchmarks after 600 fine-tuning updates. Lower is better ( ↓ ). Bold and underlined values indicate the best and second-best results across all configurations. Small signed values indicate differences relative to SAFEHOLD .
Model
Base Model
Undefended
Vaccine
SBR
LiSA
SafeMERGE
BAR
SAFEHOLD
(a) XSTest: over-refusal ( ↓ )
Qwen2.5-Coder-7B
2.01 +1.61
71.89 +71.49
18.07 +17.67
9.24 +8.84
13.25 +12.85
18.47 +18.07
3.21 +2.81
0.40
Llama-3.1-8B
6.83 +1.61
62.65 +57.43
16.06 +10.84
8.43 +3.21
8.84 +3.62
12.85 +7.63
8.03 +2.81
5.22
Llama-3.2-3B
7.63 +5.22
75.90 +73.49
14.06 +11.65
9.24 +6.83
10.84 +8.43
14.86 +12.45
6.43 +4.02
2.41
DeepSeek-Coder-6.7B
0.40 +0.40
48.59 +48.59
26.91 +26.91
20.48 +20.48
6.43 +6.43
23.69 +23.69
2.81 +2.81
0.00
Mistral-7B-v0.2
4.82 − 0.80
35.74 +30.12
16.06 +10.44
9.64 +4.02
14.46 +8.84
13.25 +7.63
6.83 +1.21
5.62
Table 4: Model utility after 600 fine-tuning updates. (a) XSTest over-refusal ( ↓ ). (b) HumanEval pass@1 ( ↑ ). Bold and underlined values indicate the best and second-best results. Small signed values indicate differences relative to SAFEHOLD .
Figure 5: Safety and utility of SAFEHOLD at 300 and 600 training updates across multiple datasets. Light and dark bars represent the two checkpoints. Bar annotations give changes in percentage points: epoch 1 minus epoch 2, except for pass@1, which uses epoch 2 minus epoch 1. (Q: Qwen2.5-Coder-7B; L31: Llama-3.1-8B; L32: Llama-3.2-3B; DS: DeepSeek-Coder-6.7B; MI: Mistral-7B-v0.2.)
Model
harmful benchmarks (ASR)
refusal
capability
RMC ↓
HB cy ↓
MAL ↓
XSTest ↓
HumanEval ↑
Qwen2.5-Coder-7B (1)
10.11
45.45
46.98
10.84
61.59
Qwen2.5-Coder-7B (2)
9.55
9.60
24.65
0.40
68.90
Llama-3.1-8B (1)
10.67
31.82
28.37
12.45
34.76
Llama-3.1-8B (2)
9.55
14.14
25.58
5.22
50.00
Llama-3.2-3B (1)
11.80
27.78
9.77
7.63
43.90
Table 5: Safety and utility of SAFEHOLD after 300 (1) and 600 (2) updates (%). Arrows indicate the preferred direction. Bold indicates the better result for each model and metric.
Model
Variant
Harmful-code ASR (%) ↓
Utility (%)
RMCBench ↓
HarmBench cy ↓
MalwareBench ↓
XSTest ↓
HumanEval ↑
Qwen2.5-Coder-7B
A (anchoring only)
12.92 +3.4
40.40 +30.8
32.09 +7.4
10.44 +10.0
57.32 − 11.6
R (refusal path only)
11.24 +1.7
49.49 +39.9
62.33 +37.7
5.62 +5.2
61.59 − 7.3
F (first token + anchoring)
84.27 +74.7
62.63 +53.0
90.70 +66.1
1.61 +1.2
65.24 − 3.7
J (joint objective)
9.55
9.60
24.65
0.40
68.90
Llama-3.1-8B
A (anchoring only)
74.72 +65.2
75.76 +61.6
79.53 +54.0
11.24 +6.0
32.32 − 17.7
Table 6: Objective ablation of SAFEHOLD after 600 updates (%). Small signed values indicate differences relative to J (A − J, R − J, F − J). Bold indicates the best result for each model and metric.
Configuration
Anchoring
First refusal token
Later refusal tokens
A
✓
✗
✗
R
✗
✓
✓
F
✓
✓
✗
J ( SAFEHOLD )
✓
✓
✓
Table 7: Components of the four objective-ablation variants.
Model
RMC ↓
HB cy ↓
MAL ↓
XSTest ↓
λ=0.6
λ=1.5
λ=3.0
λ=0.6
λ=1.5
λ=3.0
λ=0.6
λ=1.5
λ=3.0
λ=0.6
λ=1.5
λ=3.0
Qwen2.5-Coder-7B
12.92
6.74
9.55
40.40
38.89
43.43
32.09
22.33
48.84
10.44
10.84
12.45
Llama-3.1-8B
74.72
49.44
94.94
75.76
54.55
92.93
79.53
37.67
89.30
11.24
14.86
17.67
Llama-3.2-3B
96.63
97.19
85.39
97.47
94.44
82.83
92.09
88.37
70.70
14.06
3.61
10.44
DeepSeek-Coder-6.7B
64.61
73.60
73.60
55.56
62.12
62.12
71.16
79.07
76.28
16.47
17.27
17.67
Mistral-7B-v0.2
94.38
44.38
96.63
96.46
80.81
99.49
94.88
83.26
93.02
15.66
15.26
18.88
Table 8: Sensitivity to anchoring weight λ under anchor-only training ( 600 updates, λc=0 ). Arrows indicate the preferred direction. Bold marks the best result for each model and metric.
Model
RMC ↓
HB cy ↓
MAL ↓
XSTest ↓
λc=0.5
λc=1.0
λc=2.0
λc=0.5
λc=1.0
λc=2.0
λc=0.5
λc=1.0
λc=2.0
λc=0.5
λc=1.0
λc=2.0
Qwen2.5-Coder-7B
5.06
9.55
3.37
36.87
9.60
36.87
37.21
24.65
29.30
11.65
0.40
11.65
Llama-3.1-8B
27.53
9.55
43.82
49.49
14.14
57.07
20.47
25.58
35.35
9.24
5.22
13.25
Llama-3.2-3B
51.12
6.18
10.11
32.83
15.66
37.37
27.44
8.84
13.02
16.06
2.41
14.86
DeepSeek-Coder-6.7B
71.91
25.28
62.36
62.63
39.90
51.01
74.42
39.07
64.19
20.08
0.00
23.69
Mistral-7B-v0.2
84.27
44.94
30.34
70.20
32.32
50.00
76.74
40.47
40.47
9.64
5.62
14.86
Table 9: Sensitivity to refusal reinforcement weight λc under joint optimization ( 600 updates, λ=0.6 ). Arrows indicate the preferred direction. Bold marks the best result for each model and metric.
Figure 6: Objective ablation and weight sensitivity of SAFEHOLD . Values represent mean ASR across three harmful-code benchmarks ( ↓ ). Q, L31, L32, DS, and MI denote Qwen2.5-Coder-7B, Llama-3.1-8B, Llama-3.2-3B, DeepSeek-Coder-6.7B, and Mistral-7B-v0.2, respectively.
Figure 7: Activation changes of safety-associated neurons across five code LLMs. Points below the diagonal indicate suppression; for these points, greener colors indicate stronger recovery under SAFEHOLD . Annotations show suppression and rebound rates.
Model
Layers
dff
Selected neurons
Qwen2.5-Coder-7B
28
18944
4524
Llama-3.1-8B
32
14336
3139
Llama-3.2-3B
28
8192
1498
DeepSeek-Coder-6.7B
32
11008
2627
Mistral-7B-v0.2
32
14336
3224
Table 10: Safety-associated neuron statistics across five code LLMs.
Model
Suppressed (%)
Rebound (%)
Qwen2.5-Coder-7B
43.7
71.2
Llama-3.1-8B
47.8
63.4
Llama-3.2-3B
46.5
72.3
DeepSeek-Coder-6.7B
45.0
70.4
Mistral-7B-v0.2
55.2
76.7
Table 11: Activation suppression and rebound of safety-associated neurons after 600 updates.
Figure 8: Angular deviations of harmfulness directions from the aligned reference across five code LLMs. Dashed and solid arrows represent undefended and SAFEHOLD checkpoints, respectively; dashed and solid arcs mark the corresponding angular deviations. Smaller angles indicate stronger alignment.
Figure 9: Refusal-opening probability across model states. Blue and dotted segments indicate recovered and remaining probability losses, respectively. Percentages denote the recovered fraction.
How do the methods used to train language models to refuse harmful requests shape how that refusal actually works inside the model? We compare three post-training methods - supervised fine-tuning, reasoning-augmented fine-tuning (training on reasoning chains that justify a safety decision), and preference optimization (ORPO) - across three architecturally distinct models (Llama-3.1-8B, Gemma-2-9B, Qwen3-8B). We find that training method, not just data, reshapes how refusal is computed internally: reasoning-augmented training consistently produces a distinct kind of refusal computation, visible across all three models, while architecture independently shapes internal structure and how reliably refusal can be steered. Most importantly, no method we study achieves all three properties we would want from safe alignment at once: refusal that isn't concentrated in a few fragile components, safety gains that don't cost general capability, and safety behavior correctable through small, targeted edits. We caution against treating current post-training methods as a solved, reliable defense, especially for security-critical use. Code and models are available in https://github.com/hoangcuongnguyen2001/Beyond-Shallow-Alignment.
Safety alignment in large language models can be fragile under fine-tuning, as even benign task adaptation may increase harmful compliance. Existing defenses mainly follow two directions: they either intervene during or after fine-tuning through retraining or weight modification, which can be costly and may hurt task performance, or they use model-agnostic safety classifiers, which may miss failures specific to a given fine-tuned checkpoint. These limitations motivate a post hoc, model-specific, and non-invasive approach to safety restoration. To meet these requirements, we propose HyperSafe, a framework that restores safety behavior by generating a model-specific Safe Side Network (SSN) for each fine-tuned checkpoint. HyperSafe uses layer-wise activation fingerprints to capture how fine-tuning changes the model's inner representations. With a small set of given calibration prompts, the hypernetwork maps these fingerprints to the parameters of the \ssn{} in a single forward pass. The generated \ssn{} runs alongside the frozen fine-tuned model and performs prompt-level safety classification: harmful prompts are routed to refusal, while safe prompts are answered by the original fine-tuned model. Thus, HyperSafe requires no gradient updates, no safety data at deployment time, and no modification to the deployed model weights. We evaluate HyperSafe on two model families, Qwen2-7B and LLaMA-3-8B, across multiple safety benchmarks. HyperSafe reduces harmful response rates from 19-31% to below 1% on every held-out checkpoint, while keeping downstream task accuracy within 1% of the fine-tuned baseline on average. Code is available at https://github.com/nokronim/project-safety-remedy.
Aznaur Aliev, Carlos Hinojosa, Abdelrahman Eldesokey +3
King Abdullah University of Science and Technology, Saudi Arabia
Fine-tuning safety-aligned language models for downstream tasks often leads to substantial degradation of refusal behavior, making models vulnerable to adversarial misuse. While prior work has shown that safety-relevant features are encoded in structured representations within the model's activation space, how these representations change during fine-tuning and why alignment degrades remains poorly understood. In this work, we investigate the representation-level mechanisms underlying alignment degradation. Our analysis shows that standard fine-tuning induces systematic drift in safety-relevant representations, distorts their geometric structure, and introduces interference between task optimization and safety features. These effects collectively lead to increased harmful compliance. Motivated by these findings, we introduce REFUSALGUARD, a representation-level fine-tuning framework that preserves safety-relevant structure during model adaptation. Our approach constrains updates in hidden representation space, ensuring that safety-mediating components remain stable while allowing task-specific learning in complementary directions. We evaluate REFUSALGUARD across multiple model families, including LLaMA, Gemma, and Qwen, on adversarial safety benchmarks such as AdvBench, DirectHarm4, and JailbreakBench, as well as downstream utility tasks. Our approach achieves attack success rates comparable to base safety-aligned models while maintaining competitive task performance, significantly outperforming baselines.
Sadia Asif, Mohammad Mohammadi Amiri
Department of Computer Science Rensselaer Polytechnic Institute Troy, NY, USA