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 files123---license: apache-2.0---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596---license: mitlibrary_name: peftpipeline_tag: text-generationlanguage:- entags:- lora- peft- adapter- safety- alignment- jailbreak-robustnessbase_model:- meta-llama/Llama-3.1-8B-Instruct- 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><a href="https://github.com/microsoft/HARC"><img src="https://img.shields.io/badge/GitHub-Code-181717.svg?logo=github" alt="GitHub"></a><a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License: MIT"></a></p>---**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 descriptionHARC 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 confinedto a low-dimensional harmfulness–refusal subspace within a small set of selectedlayers, which improves robustness to jailbreak attacks while preserving generalcapability 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 basearchitecture unchanged.This release accompanies the HARC paper. Training and evaluation code: https://github.com/microsoft/HARC## How to useThese are PEFT adapters — load the base model, then attach the adapter with thematching `subfolder`.```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModelbase_id = "Qwen/Qwen2.5-7B-Instruct" # or "meta-llama/Llama-3.1-8B-Instruct"subfolder = "harc_qwen2.5_7b" # or "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)messages = [{"role": "user", "content": "Hello!"}]inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)out = model.generate(inputs, max_new_tokens=256)print(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`.## 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```bibtex@article{chua2026harc,title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},author={Chua, Shei Pern and Wu, Fangzhao},journal={arXiv preprint arXiv:2607.00572},year={2026}}```
README.md+94-1