Run this model with HF Jobs
Run microsoft/HARC on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/microsoft/HARC. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/microsoft/HARC on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/microsoft/HARC. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/microsoft/HARC. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
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 files12234510 unmodified lines1617181919212021223 unmodified lines26272831323329353031322 unmodified lines3536374445464748495038394041424344454653474849505152535455565758596061626364656667685758697071726273646574757667686977787980811 unmodified line83848577787986878188899091929394959697989910010110210310410510610710887109110111112113114115---license: mitlicense: apache-2.0library_name: peftpipeline_tag: text-generationlanguage:10 unmodified lines - Qwen/Qwen2.5-7B-Instructbase_model_relation: adapter---
<h1 align="center">HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment</h1>
<p align="center"> <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> <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>3 unmodified lines
---
**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*.
## Model description
HARC couples a model's internal *harmfulness* and *refusal* directions at bothprompt-side and response-side token positions, using an additive margin-hingeloss on cosine projections of the residual stream. The intervention is confined2 unmodified linescapability and avoiding the over-refusal regression typical of broader safetytuning.
The LoRA adapter (rank 32, alpha 64) is applied to attention and MLP projectionsand trained for up to 4,000 steps with a composite objective: (i) themargin-hinge coupling loss, (ii) a KL-divergence retention term anchoring benignoutputs to the base model, and (iii) a cross-entropy term supervising refusaltext on harmful prompts. Training directions are extracted viadifference-of-means on contrastive prompt sets and periodically recomputed withEMA blending. The adapter adds ~1% trainable parameters and leaves the baseWe provide HARC adapters in this repository. The adapter is applied to attention and MLP projectionsand trained with a composite objective:**(i)** the margin-hinge coupling loss**(ii)** a KL-divergence retention term anchoring benign outputs to the base model**(iii)** a cross-entropy term supervising refusal text on harmful prompts.Training directions are extracted via difference-of-means on contrastive prompt setsand periodically recomputed with EMA blending. The adapter adds ~1% trainable parameters and leaves the basearchitecture unchanged.
This release accompanies the HARC paper. Training and evaluation code: https://github.com/microsoft/HARC
- **Backbone models:** Llama-3.1-8B-Instruct, Qwen-2.5-7B-Instruct- **Paper:** [arXiv:2607.00572](https://arxiv.org/abs/2607.00572)- **Code:** [github.com/microsoft/HARC](https://github.com/microsoft/HARC)
## Repository structureEach model is provided in two forms: the standalone LoRA adapter and a pre-mergedfull model (adapter fused into the base weights).
```microsoft/HARC/├── adapters/│ ├── harc_llama3.1_8b/ LoRA adapter, base = Llama-3.1-8B-Instruct (~0.34 GB)│ └── harc_qwen2.5_7b/ LoRA adapter, base = Qwen2.5-7B-Instruct (~0.32 GB)└── models/ ├── harc_llama3.1_8b/ merged full model (~16 GB) └── harc_qwen2.5_7b/ merged full model (~15 GB)```
## How to useUse the base model's standard chat template in both cases.
These are PEFT adapters — load the base model, then attach the adapter with thematching `subfolder`.### Option A — pre-merged full model (simplest)
```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModel
base_id = "Qwen/Qwen2.5-7B-Instruct" # or "meta-llama/Llama-3.1-8B-Instruct"subfolder = "harc_qwen2.5_7b" # or "harc_llama3.1_8b"repo = "microsoft/HARC"subfolder = "models/harc_qwen2.5_7b" # or "models/harc_llama3.1_8b"
tokenizer = AutoTokenizer.from_pretrained(base_id)base = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype="auto", device_map="auto")model = PeftModel.from_pretrained(base, "microsoft/HARC", subfolder=subfolder)tokenizer = AutoTokenizer.from_pretrained(repo, subfolder=subfolder)model = AutoModelForCausalLM.from_pretrained(repo, subfolder=subfolder, torch_dtype="auto", device_map="auto")
messages = [{"role": "user", "content": "Hello!"}]inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)1 unmodified lineprint(tokenizer.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True))```
Use the base model's standard chat template. Inference hardware requirementsmatch the base model (a 7–8B model in bf16/fp16 fits on a 24GB GPU). Requires`torch >= 2.1`, `transformers`, and `peft`.### Option B — base model + LoRA adapter (via PEFT)
## Results```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModel
base_id = "Qwen/Qwen2.5-7B-Instruct" # or "meta-llama/Llama-3.1-8B-Instruct"subfolder = "adapters/harc_qwen2.5_7b" # or "adapters/harc_llama3.1_8b"
tokenizer = AutoTokenizer.from_pretrained(base_id)base = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype="auto", device_map="auto")model = PeftModel.from_pretrained(base, "microsoft/HARC", subfolder=subfolder)```
Requires `torch >= 2.1`, `transformers`, and (for Option B) `peft`. Inferencehardware requirements match the base model (a 7–8B model in bf16/fp16 fits on a24GB GPU).
## Results<p align="center"> <img src="assets/HARC-res.png" alt="HARC main results on Llama-3.1-8B and Qwen-2.5-7B" width="60%"></p>
## Citation## ResultsThis project is licensed under the MIT License.
## Citation```bibtex@article{chua2026harc, title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},README.md+52-28