File size: 3,220 Bytes
4b0038b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
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.