vincchua commited on
Commit
ecd7085
·
verified ·
1 Parent(s): 480d5a0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +94 -1
README.md CHANGED
@@ -1,3 +1,96 @@
1
  ---
2
- license: apache-2.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ license: mit
3
+ library_name: peft
4
+ pipeline_tag: text-generation
5
+ language:
6
+ - en
7
+ tags:
8
+ - lora
9
+ - peft
10
+ - adapter
11
+ - safety
12
+ - alignment
13
+ - jailbreak-robustness
14
+ base_model:
15
+ - meta-llama/Llama-3.1-8B-Instruct
16
+ - Qwen/Qwen2.5-7B-Instruct
17
+ base_model_relation: adapter
18
  ---
19
+
20
+ <h1 align="center">HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment</h1>
21
+
22
+ <p align="center">
23
+ <a href="https://arxiv.org/abs/2607.00572"><img src="https://img.shields.io/badge/Paper-arXiv-b31b1b.svg?logo=arxiv&logoColor=white" alt="Paper"></a>
24
+ <a href="https://huggingface.co/microsoft/HARC"><img src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Models-yellow.svg" alt="Hugging Face"></a>
25
+ <a href="https://github.com/microsoft/HARC"><img src="https://img.shields.io/badge/GitHub-Code-181717.svg?logo=github" alt="GitHub"></a>
26
+ <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License: MIT"></a>
27
+ </p>
28
+
29
+ ---
30
+
31
+ **HARC (Harmfulness-And-Refusal Coupling)** is a representation-level safety-alignment method that binds a model's internal harmfulness and refusal directions so that detecting harm reliably triggers refusal at both prompt and response positions in the residual stream. By confining the intervention to this low-dimensional harmfulness–refusal subspace, HARC strengthens robustness to jailbreak attacks while leaving general capability and over-refusal behavior largely intact. This repo provides the official implementation of our paper *HARC: Coupling Harmfulness And Refusal Directions for Robust Safety Alignment*.
32
+
33
+
34
+ ## Model description
35
+
36
+ HARC couples a model's internal *harmfulness* and *refusal* directions at both
37
+ prompt-side and response-side token positions, using an additive margin-hinge
38
+ loss on cosine projections of the residual stream. The intervention is confined
39
+ to a low-dimensional harmfulness–refusal subspace within a small set of selected
40
+ layers, which improves robustness to jailbreak attacks while preserving general
41
+ capability and avoiding the over-refusal regression typical of broader safety
42
+ tuning.
43
+
44
+ The LoRA adapter (rank 32, alpha 64) is applied to attention and MLP projections
45
+ and trained for up to 4,000 steps with a composite objective: (i) the
46
+ margin-hinge coupling loss, (ii) a KL-divergence retention term anchoring benign
47
+ outputs to the base model, and (iii) a cross-entropy term supervising refusal
48
+ text on harmful prompts. Training directions are extracted via
49
+ difference-of-means on contrastive prompt sets and periodically recomputed with
50
+ EMA blending. The adapter adds ~1% trainable parameters and leaves the base
51
+ architecture unchanged.
52
+
53
+ This release accompanies the HARC paper. Training and evaluation code: https://github.com/microsoft/HARC
54
+
55
+ ## How to use
56
+
57
+ These are PEFT adapters — load the base model, then attach the adapter with the
58
+ matching `subfolder`.
59
+
60
+ ```python
61
+ from transformers import AutoModelForCausalLM, AutoTokenizer
62
+ from peft import PeftModel
63
+
64
+ base_id = "Qwen/Qwen2.5-7B-Instruct" # or "meta-llama/Llama-3.1-8B-Instruct"
65
+ subfolder = "harc_qwen2.5_7b" # or "harc_llama3.1_8b"
66
+
67
+ tokenizer = AutoTokenizer.from_pretrained(base_id)
68
+ base = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype="auto", device_map="auto")
69
+ model = PeftModel.from_pretrained(base, "microsoft/HARC", subfolder=subfolder)
70
+
71
+ messages = [{"role": "user", "content": "Hello!"}]
72
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
73
+ out = model.generate(inputs, max_new_tokens=256)
74
+ print(tokenizer.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True))
75
+ ```
76
+
77
+ Use the base model's standard chat template. Inference hardware requirements
78
+ match the base model (a 7–8B model in bf16/fp16 fits on a 24GB GPU). Requires
79
+ `torch >= 2.1`, `transformers`, and `peft`.
80
+
81
+ ## Results
82
+
83
+ <p align="center">
84
+ <img src="assets/HARC-res.png" alt="HARC main results on Llama-3.1-8B and Qwen-2.5-7B" width="60%">
85
+ </p>
86
+
87
+ ## Citation
88
+
89
+ ```bibtex
90
+ @article{chua2026harc,
91
+ title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},
92
+ author={Chua, Shei Pern and Wu, Fangzhao},
93
+ journal={arXiv preprint arXiv:2607.00572},
94
+ year={2026}
95
+ }
96
+ ```