Instructions to use microsoft/HARC with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use microsoft/HARC with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
license:
|
| 3 |
library_name: peft
|
| 4 |
pipeline_tag: text-generation
|
| 5 |
language:
|
|
@@ -16,9 +16,7 @@ base_model:
|
|
| 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>
|
|
@@ -28,11 +26,7 @@ base_model_relation: adapter
|
|
| 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
|
|
@@ -41,32 +35,47 @@ 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 |
-
|
| 45 |
-
and trained
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
difference-of-means on contrastive prompt sets
|
| 50 |
-
EMA blending. The adapter adds ~1% trainable parameters and leaves the base
|
| 51 |
architecture unchanged.
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
## How to use
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
matching `subfolder`.
|
| 59 |
|
| 60 |
```python
|
| 61 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 62 |
-
from peft import PeftModel
|
| 63 |
|
| 64 |
-
|
| 65 |
-
subfolder = "harc_qwen2.5_7b"
|
| 66 |
|
| 67 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 68 |
-
|
| 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)
|
|
@@ -74,18 +83,33 @@ out = model.generate(inputs, max_new_tokens=256)
|
|
| 74 |
print(tokenizer.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True))
|
| 75 |
```
|
| 76 |
|
| 77 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
##
|
|
|
|
| 88 |
|
|
|
|
| 89 |
```bibtex
|
| 90 |
@article{chua2026harc,
|
| 91 |
title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
library_name: peft
|
| 4 |
pipeline_tag: text-generation
|
| 5 |
language:
|
|
|
|
| 16 |
- Qwen/Qwen2.5-7B-Instruct
|
| 17 |
base_model_relation: adapter
|
| 18 |
---
|
|
|
|
| 19 |
<h1 align="center">HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment</h1>
|
|
|
|
| 20 |
<p align="center">
|
| 21 |
<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>
|
| 22 |
<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>
|
|
|
|
| 26 |
|
| 27 |
---
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
## Model description
|
|
|
|
| 30 |
HARC couples a model's internal *harmfulness* and *refusal* directions at both
|
| 31 |
prompt-side and response-side token positions, using an additive margin-hinge
|
| 32 |
loss on cosine projections of the residual stream. The intervention is confined
|
|
|
|
| 35 |
capability and avoiding the over-refusal regression typical of broader safety
|
| 36 |
tuning.
|
| 37 |
|
| 38 |
+
We provide HARC adapters in this repository. The adapter is applied to attention and MLP projections
|
| 39 |
+
and trained with a composite objective:
|
| 40 |
+
**(i)** the margin-hinge coupling loss
|
| 41 |
+
**(ii)** a KL-divergence retention term anchoring benign outputs to the base model
|
| 42 |
+
**(iii)** a cross-entropy term supervising refusal text on harmful prompts.
|
| 43 |
+
Training directions are extracted via difference-of-means on contrastive prompt sets
|
| 44 |
+
and periodically recomputed with EMA blending. The adapter adds ~1% trainable parameters and leaves the base
|
| 45 |
architecture unchanged.
|
| 46 |
|
| 47 |
+
|
| 48 |
+
- **Backbone models:** Llama-3.1-8B-Instruct, Qwen-2.5-7B-Instruct
|
| 49 |
+
- **Paper:** [arXiv:2607.00572](https://arxiv.org/abs/2607.00572)
|
| 50 |
+
- **Code:** [github.com/microsoft/HARC](https://github.com/microsoft/HARC)
|
| 51 |
+
|
| 52 |
+
## Repository structure
|
| 53 |
+
Each model is provided in two forms: the standalone LoRA adapter and a pre-merged
|
| 54 |
+
full model (adapter fused into the base weights).
|
| 55 |
+
|
| 56 |
+
```
|
| 57 |
+
microsoft/HARC/
|
| 58 |
+
βββ adapters/
|
| 59 |
+
β βββ harc_llama3.1_8b/ LoRA adapter, base = Llama-3.1-8B-Instruct (~0.34 GB)
|
| 60 |
+
β βββ harc_qwen2.5_7b/ LoRA adapter, base = Qwen2.5-7B-Instruct (~0.32 GB)
|
| 61 |
+
βββ models/
|
| 62 |
+
βββ harc_llama3.1_8b/ merged full model (~16 GB)
|
| 63 |
+
βββ harc_qwen2.5_7b/ merged full model (~15 GB)
|
| 64 |
+
```
|
| 65 |
|
| 66 |
## How to use
|
| 67 |
+
Use the base model's standard chat template in both cases.
|
| 68 |
|
| 69 |
+
### Option A β pre-merged full model (simplest)
|
|
|
|
| 70 |
|
| 71 |
```python
|
| 72 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 73 |
|
| 74 |
+
repo = "microsoft/HARC"
|
| 75 |
+
subfolder = "models/harc_qwen2.5_7b" # or "models/harc_llama3.1_8b"
|
| 76 |
|
| 77 |
+
tokenizer = AutoTokenizer.from_pretrained(repo, subfolder=subfolder)
|
| 78 |
+
model = AutoModelForCausalLM.from_pretrained(repo, subfolder=subfolder, torch_dtype="auto", device_map="auto")
|
|
|
|
| 79 |
|
| 80 |
messages = [{"role": "user", "content": "Hello!"}]
|
| 81 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
|
|
|
| 83 |
print(tokenizer.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True))
|
| 84 |
```
|
| 85 |
|
| 86 |
+
### Option B β base model + LoRA adapter (via PEFT)
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
```python
|
| 89 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 90 |
+
from peft import PeftModel
|
| 91 |
+
|
| 92 |
+
base_id = "Qwen/Qwen2.5-7B-Instruct" # or "meta-llama/Llama-3.1-8B-Instruct"
|
| 93 |
+
subfolder = "adapters/harc_qwen2.5_7b" # or "adapters/harc_llama3.1_8b"
|
| 94 |
|
| 95 |
+
tokenizer = AutoTokenizer.from_pretrained(base_id)
|
| 96 |
+
base = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype="auto", device_map="auto")
|
| 97 |
+
model = PeftModel.from_pretrained(base, "microsoft/HARC", subfolder=subfolder)
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
Requires `torch >= 2.1`, `transformers`, and (for Option B) `peft`. Inference
|
| 101 |
+
hardware requirements match the base model (a 7β8B model in bf16/fp16 fits on a
|
| 102 |
+
24GB GPU).
|
| 103 |
+
|
| 104 |
+
## Results
|
| 105 |
<p align="center">
|
| 106 |
<img src="assets/HARC-res.png" alt="HARC main results on Llama-3.1-8B and Qwen-2.5-7B" width="60%">
|
| 107 |
</p>
|
| 108 |
|
| 109 |
+
## Results
|
| 110 |
+
This project is licensed under the MIT License.
|
| 111 |
|
| 112 |
+
## Citation
|
| 113 |
```bibtex
|
| 114 |
@article{chua2026harc,
|
| 115 |
title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},
|