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 files4 unmodified lines567891011122 unmodified lines15161718192021222324252627282 unmodified lines31323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576773 unmodified lines81828384858611 unmodified lines98991001011021031041051061071081091101114 unmodified lineslanguage:- entags:- safety- alignmentbase_model:- meta-llama/Llama-3.1-8B-Instruct- Qwen/Qwen2.5-7B-Instruct2 unmodified lines<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>---## 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 confined2 unmodified linescapability and avoiding the over-refusal regression typical of broader safetytuning.We 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.- **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/β βββ harc_qwen2.5_7b/βββ models/βββ harc_llama3.1_8b/βββ harc_qwen2.5_7b/```## How to useUse the base model's standard chat template in both cases.### Option A β pre-merged full model (simplest)```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerrepo = "microsoft/HARC"subfolder = "models/harc_qwen2.5_7b" # or "models/harc_llama3.1_8b"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)3 unmodified lines### Option B β base model + LoRA adapter (via PEFT)```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModel11 unmodified lines24GB 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>## ResultsThis project is licensed under the MIT License.## Citation```bibtex@article{chua2026harc,title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},4 unmodified lines56789101112131415162 unmodified lines1920212223242526272829303132332 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788893 unmodified lines9394959697989910011 unmodified lines1121131141151161171181191201211221231241251261271281294 unmodified lineslanguage:- entags:- lora- peft- adapter- safety- alignment- jailbreak-robustnessbase_model:- meta-llama/Llama-3.1-8B-Instruct- Qwen/Qwen2.5-7B-Instruct2 unmodified lines<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/collections/microsoft/harc"><img src="https://img.shields.io/badge/%F0%9F%A4%97%20Collection-HARC-ff9d00.svg" alt="Collection"></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="https://huggingface.co/microsoft/HARC/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License: MIT"></a></p>---## 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 confined2 unmodified linescapability and avoiding the over-refusal regression typical of broader safetytuning.**This repository contains the HARC LoRA adapters.** 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.- **Backbone models:** Llama-3.1-8B-Instruct, Qwen-2.5-7B-Instruct- **Collection:** [HARC Collection](https://huggingface.co/collections/microsoft/harc)- **Paper:** [arXiv:2607.00572](https://arxiv.org/abs/2607.00572)- **Code:** [github.com/microsoft/HARC](https://github.com/microsoft/HARC)## The HARC collection| Repo | Contents | License ||---|---|---|| **microsoft/HARC** (this repo) | LoRA adapters for both backbones | MIT || [microsoft/HARC-Llama-3.1-8B-Instruct](https://huggingface.co/microsoft/HARC-Llama-3.1-8B-Instruct) | Merged full model | Llama 3.1 Community License || [microsoft/HARC-Qwen2.5-7B-Instruct](https://huggingface.co/microsoft/HARC-Qwen2.5-7B-Instruct) | Merged full model | Apache-2.0 |Use this repo if you want the lightweight adapters to load on top of your owncopy of the base model; use the merged-model repos if you want a singleready-to-run checkpoint.## Repository structure```microsoft/HARC/βββ adapters/βββ harc_llama3.1_8b/ # base = Llama-3.1-8B-Instructβββ harc_qwen2.5_7b/ # base = Qwen2.5-7B-Instruct```## How to useUse the base model's standard chat template in both cases.### Option A β pre-merged full model (simplest)Loads directly from the merged-model repo; no base download or PEFT required.```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizer# pick the merged model you wantrepo = "microsoft/HARC-Qwen2.5-7B-Instruct" # or "microsoft/HARC-Llama-3.1-8B-Instruct"tokenizer = AutoTokenizer.from_pretrained(repo)model = AutoModelForCausalLM.from_pretrained(repo, 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)3 unmodified lines### Option B β base model + LoRA adapter (via PEFT)Load the base model, then attach the adapter from this repo with the matching `subfolder`.```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModel11 unmodified lines24GB GPU).## Results## LicenseThe LoRA adapters in this repository are released under the MIT License. Themerged full models are distributed in separate repositories under their basemodel's license: the Llama variant under the Meta Llama 3.1 Community License,and the Qwen variant under Apache-2.0.## Citation```bibtex@article{chua2026harc,title={HARC: Coupling Harmfulness and Refusal Directions for Robust Safety Alignment},
README.md+43-25