|
Download README.md from jayesh20/swapper: direct link, hf CLI and curl.
- Browser
- Download file 3.22 kB
-
https://huggingface.co/jayesh20/swapper/resolve/main/README.md
- Command line
-
hf download hf://jayesh20/swapper/README.md
-
curl -L -o README.md https://huggingface.co/jayesh20/swapper/resolve/main/README.md
3.22 kB
| language: | |
| - en | |
| license: apache-2.0 | |
| tags: | |
| - qwen | |
| - qwen3.5 | |
| - refusal-ablation | |
| - representation-engineering | |
| - uncensored | |
| - conversational | |
| - text-generation | |
| base_model: Qwen/Qwen3.5-0.8B | |
| pipeline_tag: text-generation | |
| # Swapper: Qwen3.5-0.8B Refusal-Ablated & Orthogonalized | |
| **Swapper** is a decensored version of [Qwen/Qwen3.5-0.8B](https://huggingface.co/Qwen/Qwen3.5-0.8B) created by locating and orthogonalizing the internal refusal direction in the model's residual stream. | |
| Unlike standard supervised fine-tuning (SFT) or DPO, this model modifies the weight matrices that write to the residual stream ($W_E$, attention output projections, and MLP down projections) so the network cannot mathematically represent or write the "refusal feature". | |
| --- | |
| ## Included Files in this Repository | |
| | File | Description | | |
| |------|-------------| | |
| | `model.safetensors` | Orthogonalized model weights (3.2 GB) | | |
| | `config.json`, `*.json` | Architecture and tokenizer configuration | | |
| | `chatbot.py` | Interactive terminal streaming chatbot with conversation history | | |
| | `refusal_demo.ipynb` | Fully executed Jupyter notebook showing the refusal ablation and orthogonalization | | |
| | `heretic_qwen3_5_0_8b_final.ipynb` | Heretic Optuna study notebook for directional ablation | | |
| --- | |
| ## Quickstart: Streaming Chatbot | |
| Clone the repository and run the included terminal chatbot: | |
| ```bash | |
| git clone https://huggingface.co/jayesh20/swapper | |
| cd swapper | |
| pip install -r requirements.txt # or: pip install transformers torch accelerate | |
| python chatbot.py | |
| ``` | |
| Or run directly in Python pointing to this Hugging Face repository: | |
| ```bash | |
| python chatbot.py jayesh20/swapper | |
| ``` | |
| --- | |
| ## Python Usage | |
| ```python | |
| import torch | |
| from transformers import AutoProcessor, AutoModelForMultimodalLM, TextStreamer | |
| model_id = "jayesh20/swapper" | |
| processor = AutoProcessor.from_pretrained(model_id) | |
| model = AutoModelForMultimodalLM.from_pretrained( | |
| model_id, | |
| dtype=torch.float32, | |
| device_map="auto" | |
| ) | |
| streamer = TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True) | |
| messages = [ | |
| {"role": "user", "content": [{"type": "text", "text": "How do hackers test network security vulnerabilities?"}]} | |
| ] | |
| inputs = processor.apply_chat_template( | |
| messages, | |
| add_generation_prompt=True, | |
| tokenize=True, | |
| return_dict=True, | |
| return_tensors="pt" | |
| ).to(model.device) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=1024, | |
| streamer=streamer, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_p=0.9 | |
| ) | |
| ``` | |
| --- | |
| ## Method Details | |
| 1. **Activation Difference**: Activations are cached across intermediate layers (layer 14) for paired harmful instructions (AdvBench) and harmless instructions (Alpaca) at the final prompt token position. | |
| 2. **Refusal Direction ($r$)**: | |
| $$r = \frac{\mu_{\text{harmful}} - \mu_{\text{harmless}}}{\|\mu_{\text{harmful}} - \mu_{\text{harmless}}\|}$$ | |
| 3. **Weight Orthogonalization**: | |
| All projection matrices writing to the residual stream are projected to remove components along $r$: | |
| $$W' \leftarrow (I - r r^T) W$$ | |
| This eliminates the refusal mechanism without degrading general conversational abilities. | |