Metadata-Version: 2.4
Name: heads-of-security
Version: 0.1.0
Summary: Heads of Security: trained per-head trust-channel gates on attention, for gpt-oss and Llama (HuggingFace + vLLM)
License: MIT License
        
        Copyright (c) 2026 Authors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.9.1
Requires-Dist: transformers==5.9.0
Requires-Dist: safetensors>=0.5
Requires-Dist: huggingface-hub>=0.36.0
Requires-Dist: accelerate>=1.12.0
Requires-Dist: openai-harmony>=0.0.8
Requires-Dist: numpy>=2.2.0
Provides-Extra: vllm
Requires-Dist: vllm==0.27.1; extra == "vllm"
Requires-Dist: fastapi>=0.115; extra == "vllm"
Requires-Dist: uvicorn>=0.30; extra == "vllm"
Requires-Dist: pydantic>=2; extra == "vllm"
Provides-Extra: demos
Requires-Dist: matplotlib>=3.10; extra == "demos"
Requires-Dist: jupyter>=1.1; extra == "demos"
Provides-Extra: train
Requires-Dist: torchtune==0.6.1; extra == "train"
Requires-Dist: torchao==0.13.0; extra == "train"
Requires-Dist: datasets>=4.4; extra == "train"
Requires-Dist: wandb>=0.23; extra == "train"
Requires-Dist: tqdm>=4.66; extra == "train"
Requires-Dist: omegaconf>=2.3; extra == "train"
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: ruff>=0.8; extra == "dev"
Dynamic: license-file

# Heads of Security

## Summary

This repository provides the code and trained gates for **Heads of Security**, a method for enforcing instruction–data separation through learned per-head attention gates. The gates help the model use untrusted data, such as tool results, without following instructions embedded in it.

The code supports GPT-OSS-20B and Llama-3.3-70B-Instruct with Hugging Face Transformers and vLLM.

## Quick start

Install from the repository root with Python 3.12 or later and `uv`:

```bash
uv sync
```

Load a model with its trained gates:

```python
from heads_of_security import load_gated_model

model, tokenizer = load_gated_model("imperial-cpg/hos-gpt-oss-20b", device="cuda:0")
```

The loader downloads the gates and the corresponding base model. Use `device="auto"` to distribute a model across GPUs.

Available models:

| Model ID | Base model |
| --- | --- |
| `imperial-cpg/hos-gpt-oss-20b` | GPT-OSS-20B |
| `imperial-cpg/hos-llama-3.3-70b-instruct` | Llama-3.3-70B-Instruct |

See the [introduction notebook](demos/introduction.ipynb) for a complete example of preparing messages, assigning trust channels, and generating responses. To install the notebook dependencies:

```bash
uv sync --extra demos
```

## vLLM example

Install vLLM and run the demo on a GPU machine:

```bash
uv sync --extra vllm
bash demos/build_and_serve_vllm.sh gptoss
```

For Llama, replace `gptoss` with `llama` (uses four GPUs by default).

The [demo script](demos/build_and_serve_vllm.sh) builds the gated model and starts an OpenAI-compatible API on port 8000. It includes a sample request. The server assigns trust channels automatically, treating tool results as untrusted data.

## Training your own gates

Training uses Direct Preference Optimization (DPO) to favor responses that follow the user's task over injected instructions. Only the gates are trained; the base model stays frozen.

The dataset is on Hugging Face `imperial-cpg/heads-of-security-dpo`: it's SecAlign's CleanAlpaca data, augmented with
reasoning traces from GPT-OSS-120B.

Install the training dependencies:

```bash
uv sync --extra train
```

Run the [training script](demos/train_gates.sh) from the repository root:

```bash
bash demos/train_gates.sh gptoss 0.1     # eight 80 GB GPUs, about 4 hours
bash demos/train_gates.sh llama 0.008   # eight 80 GB GPUs, about 2 hours
```

The second argument sets the learning rate. Other settings are in [the training configs](configs/training/).

GPT-OSS-20B can also fit on a single GPU, but keep the effective batch at 64 to get the same training:

```bash
NPROC=1 bash demos/train_gates.sh gptoss 0.1 gradient_accumulation_steps=64
```

To override any other setting, add it to the command:

```bash
bash demos/train_gates.sh gptoss 0.1 max_steps_per_epoch=50
```

Each run saves its gates to `runs/<name>/gates/epoch_0.safetensors`. Load them with `load_gated_model_local` as shown in [the gate documentation](gates/README.md#load-local-gates).

## Repository structure and tests

```text
src/heads_of_security/
  attention_gating/   Transformers models, tokenizers, and gate loading
  vllm_gates/         vLLM integration and server
  serve/              Message formatting and tool-call parsing
  training/           DPO recipe for training new gates (see above)
configs/training/   Training configs for the two released models
gates/              Trained gate files
demos/              Notebooks, serving and training launchers
tests/              Tests for gating, tokenizers, loading, generation, and training data
```

Run the tests:

```bash
uv sync --extra dev
uv run pytest
```

Tests use small models on CPU. Tokenizer tests download the base model tokenizers.

Training tests are optional and skip automatically when training dependencies are missing. To run the full suite, including training tests:

```bash
uv sync --extra dev --extra train
uv run pytest
```

## Method summary

![Heads of Security method diagram](heads_of_security_diagram.png)

Each token is assigned to one of three channels: trusted instructions and conversation, untrusted tool results, or chat-template formatting. Learned gates in each attention head control how much tokens in one channel attend to tokens in another.

Only the gates are trained; the base model stays frozen. Training favors responses that follow the user's task over responses that follow injected instructions. This teaches the gates to reduce the influence of malicious instructions in untrusted data.

See [the gate documentation](gates/README.md) for training and file details.
