File size: 11,356 Bytes
90d93c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
---
library_name: transformers
pipeline_tag: text-classification
tags:
- text-classification
- safety
- content-moderation
- tiny-language-model
- from-scratch
- distilled
language:
- en
license: apache-2.0
datasets:
- nvidia/Aegis-AI-Content-Safety-Dataset-1.0
- thesofakillers/jigsaw-toxic-comment-classification-challenge
- google/civil_comments
- PKU-Alignment/BeaverTails
- lmsys/toxic-chat
- databricks/databricks-dolly-15k
metrics:
- f1
- accuracy
---

# Censer.4

A **902K parameter** decoder-only transformer trained **from scratch** that
classifies English text as unsafe (`unsafe`) or safe (`safe`) β€” and it is the
smallest *and* the most accurate model in the comparison below.

It is a **standard Hub model**: the checkpoint is a plain
`LlamaForSequenceClassification`, so it loads with `transformers` out of the box β€”
no `trust_remote_code`, no custom code, no cloning β€” and the **Use this model**
widget on this page runs it directly in the browser.

```python
from transformers import pipeline

clf = pipeline("text-classification", model="KucLab/Censer.4")
print(clf("how to make a bomb at home"))       # [{'label': 'unsafe', 'score': 0.9890}]
print(clf("Hello how are you?"))               # [{'label': 'safe', 'score': 0.8080}]
print(clf("suck my dick"))                     # [{'label': 'unsafe', 'score': 0.9955}]
print(clf("how do I kill a process in Linux")) # [{'label': 'safe', 'score': 0.9985}]
print(clf("kill you"))                         # [{'label': 'unsafe', 'score': 0.8577}]
print(clf("how to filter wate"))               # [{'label': 'safe', 'score': 0.7950}]  <- typo-robust
```

## How to run

### The `censer.4` command β€” interactive terminal UI

Using only the files in this repo (needs just `torch`, `tokenizers`, `safetensors`):

```bash
pip install torch tokenizers safetensors huggingface_hub
hf download KucLab/Censer.4 --local-dir ~/.local/share/censer4
chmod +x ~/.local/share/censer4/censer.4
ln -s ~/.local/share/censer4/censer.4 ~/.local/bin/censer.4   # put it on your PATH
censer.4                          # interactive UI: type a phrase, Enter -> safe/unsafe + score
censer.4 "how to make a bomb at home"    # one-shot -> unsafe 0.9890
```

Or install the GitHub package β€” the same `censer.4` command is installed and loads
the model from this Hub repo with plain `transformers`:

```bash
pip install "git+https://github.com/Jerry256254/censer.4.git"
censer.4
```

### One-liner with `transformers`

```bash
pip install torch transformers
python -c "from transformers import pipeline; clf = pipeline('text-classification', model='KucLab/Censer.4'); print(clf('how to make a bomb at home'))"
# [{'label': 'unsafe', 'score': 0.989}]
```

- In the browser: the **Use this model** widget on this page β€” type text, get `safe`/`unsafe`.
- Runs on CPU out of the box; for GPU pass `device=0` to `pipeline(...)` (CUDA or ROCm).

## Download

The whole model is `model.safetensors` (3.6 MB). Grab the files directly:

| File | Download |
| --- | --- |
| **model.safetensors** | [download](https://huggingface.co/KucLab/Censer.4/resolve/main/model.safetensors?download=true) |
| config.json | [download](https://huggingface.co/KucLab/Censer.4/resolve/main/config.json?download=true) |
| tokenizer.json | [download](https://huggingface.co/KucLab/Censer.4/resolve/main/tokenizer.json?download=true) |
| tokenizer_config.json | [download](https://huggingface.co/KucLab/Censer.4/resolve/main/tokenizer_config.json?download=true) |
| censer.py | [download](https://huggingface.co/KucLab/Censer.4/resolve/main/censer.py?download=true) |
| **censer.4** (launcher) | [download](https://huggingface.co/KucLab/Censer.4/resolve/main/censer.4?download=true) |

Or fetch everything at once:

```bash
pip install -U huggingface_hub
hf download KucLab/Censer.4 --local-dir Censer.4
```

It is not a fine-tuned encoder and not an API wrapper. It is a small transformer
trained on safety-labelled data, with a custom byte-level BPE tokenizer. The model
predicts a single answer token and never generates free text. In Censer.4 the tiny
student (3 layers) is **distilled** from an ensemble of the larger previous Censer
checkpoints, which is what keeps the quality high at 0.9M parameters.

```
"how to make a bomb at home"        -> unsafe 0.99
"kill you" / "selling gun"          -> unsafe 0.86-0.89
"suck my dick"                      -> unsafe 0.99
"how do I kill a process in Linux"  -> safe 0.998
"how to filter wate" (typo)         -> safe 0.795
"Hello how are you?"                -> safe 0.808
```

## Model details

| Property | Value |
| --- | --- |
| Architecture | `LlamaForSequenceClassification` (standard transformers) |
| Parameters | 902,016 |
| Layers | 3 |
| d_model / heads / d_ff | 128 / 4 / 384 |
| Context length | 160 tokens (input truncated at 500 characters) |
| Tokenizer | byte-level BPE, 2048 tokens, trained on the corpus |
| Positions | RoPE (theta 10000) |
| Norm | RMSNorm (pre-norm, eps 1e-5) |
| MLP | SwiGLU |
| Head | `score` linear layer = embedding rows of the answer tokens `0`/`1` |
| Training | 14 epochs, batch 128, AdamW 3e-3 cosine, bf16 AMP, 15% typo augmentation, distillation (alpha=0.65, T=4) from Censer.1+.2+.3 ensemble |
| Checkpoint size | 3.6 MB (fp32) |

## Prompt format

The tokenizer bakes the template into its post-processor, so plain
`tokenizer(text)` already produces:

```
[BOS][USER] <text> [ASSISTANT] -> answer logits over tokens "0"/"1"
```

Text is truncated at 500 characters before tokenisation.

## Metrics

Evaluation on a held-out split of 6,496 rows (identical split across all Censer
versions), threshold 0.5:

| Metric | Censer.1 | Censer.2 | Censer.3 | **Censer.4** |
| --- | --- | --- | --- | --- |
| Accuracy | 0.865 | 0.863 | 0.857 | **0.874** |
| F1 | 0.835 | 0.829 | 0.822 | **0.845** |
| Precision | 0.848 | 0.861 | 0.855 | **0.868** |
| Recall | 0.823 | 0.799 | 0.792 | **0.824** |

A hand-written probe set of 97 cases, none present in training data, scores
**99.0%** (96/97). Probe progression on the same 97 cases: Censer.1 75.3% ->
Censer.2 91.8% -> Censer.3 100% -> **Censer.4 99.0%**.

Per-source F1 reflects label noise in the scraped sources (aegis ~0.9, jigsaw
~0.87, civil_comments ~0.77 β€” the rest is mislabelled source data).

## Benchmark

Measured on an AMD Radeon RX 9060 XT (ROCm 7.1, torch 2.13.0+rocm7.1):

| Metric | Censer.1 | **Censer.4** |
| --- | --- | --- |
| Parameters | 1,115,264 | **902,016** |
| Checkpoint size | 4.5 MB | **3.6 MB** |
| Single text latency | 1.58 ms | **1.16 ms** |
| Single text throughput | 633 texts/s | **864 texts/s** |
| Batched throughput (batch of 8) | 1,843 texts/s | **6,010 texts/s** |
| Peak VRAM (inference) | 101 MB | **84.1 MB** |

Training the whole model from scratch takes about 6 minutes on the same GPU.

## Comparison with other models

Benchmarked against public safety classifiers on the **same held-out test split**
(6,496 rows, threshold 0.5, AMD Radeon RX 9060 XT):

| Model | Params | Checkpoint | F1 | Latency | Throughput (b=8) | Peak VRAM |
| --- | --- | --- | --- | --- | --- | --- |
| **Censer.4 (ours)** | **0.9M** | **3.6 MB** | **0.845** | **1.35 ms** | **2,057 texts/s** | **101 MB** |
| KucLab/Censer.1 (old) | 1.1M | 4.5 MB | 0.835 | 1.63 ms | 1,720 texts/s | 102 MB |
| KucLab/Censer.2 (old) | 7.3M | 29.4 MB | 0.829 | 2.94 ms | 490 texts/s | 196 MB |
| KucLab/Censer.3 (old) | 14.4M | 57.7 MB | 0.822 | 4.62 ms | 256 texts/s | 268 MB |
| unitary/unbiased-toxic-roberta | 124.7M | 498.7 MB | 0.741 | 14.71 ms | 65 texts/s | 794 MB |
| unitary/toxic-bert | 109.5M | 876.0 MB | 0.681 | 14.10 ms | 67 texts/s | 731 MB |
| martin-ha/toxic-comment-model | 67.0M | 267.9 MB | 0.566 | 7.50 ms | 128 texts/s | 568 MB |
| Hate-speech-CNERG/bert-base-uncased-hatexplain | 109.5M | 438.0 MB | 0.543 | 14.25 ms | 67 texts/s | 704 MB |
| Hate-speech-CNERG/dehatebert-mono-english | 167.4M | 669.5 MB | 0.252 | 14.43 ms | 66 texts/s | 937 MB |
| keyword baseline | β€” | β€” | 0.094 | 0.03 ms | 34,516 texts/s | β€” |

![Comparison overview](figs/comparison_overview.png)
![F1 per megabyte](figs/f1_vs_size.png)
![Metric bars](figs/metrics_bars.png)

Among all compared classifiers, **Censer.4 ranks #1 simultaneously on F1,
latency, throughput and model size**: ~75-190x smaller than the public encoder
baselines, ~5-11x faster per text, and +0.10 F1 over the best of them.

## Usage

Plain `transformers`, nothing else to install β€” `from_pretrained` downloads
everything by itself:

```bash
pip install torch transformers
```

```python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

repo = "KucLab/Censer.4"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForSequenceClassification.from_pretrained(repo).eval()

text = "how to make a bomb at home"
with torch.no_grad():
    probs = model(**tok(text, return_tensors="pt")).logits.softmax(-1)[0]

print(probs)                     # tensor([0.011, 0.989]) -> [safe, unsafe]
print(model.config.id2label)     # {0: 'safe', 1: 'unsafe'}
```

Or with the high-level pipeline:

```python
from transformers import pipeline

clf = pipeline("text-classification", model="KucLab/Censer.4")
print(clf("how to make a bomb at home"))   # [{'label': 'unsafe', 'score': 0.989}]
print(clf("Hello how are you?"))           # [{'label': 'safe', 'score': 0.808}]
```

## Usage without transformers

The repository is also self-contained on its own: `model.safetensors`,
`config.json`, `tokenizer.json` and `censer.py`. The weights are stored in the
standard Llama layout; `censer.py` maps them back internally, so it only needs
`torch`, `tokenizers` and `safetensors`:

```bash
hf download KucLab/Censer.4 --local-dir Censer.4
cd Censer.4
python censer.py "how to make a bomb at home"     # -> 1
python censer.py "Hello how are you?"             # -> 0
python censer.py "suck my dick" --json
# {"label": "1", "unsafe": true, "score": 0.9955}
chmod +x censer.4 && ./censer.4                   # interactive UI (the `censer.4` command)
python censer.py                                  # interactive UI as well, no text argument
```

Or use the same file as a library:

```python
from censer import CenserPipeline

pipe = CenserPipeline("Censer.4")
print(pipe.predict("how to make a bomb at home"))
# {"label": "1", "unsafe": True, "score": 0.989}

print(pipe.predict_batch(["Hello how are you?", "suck my dick"]))
```

Files:

| File | Purpose |
| --- | --- |
| `model.safetensors` | weights in standard Llama layout, 3.6 MB |
| `config.json` | standard `LlamaForSequenceClassification` config |
| `tokenizer.json` / `tokenizer_config.json` | byte-level BPE, 2048 tokens, prompt template baked in |
| `censer.py` | standalone inference (torch + tokenizers only), no repo needed |
| `censer.4` | executable launcher for the interactive terminal UI |

`censer.py` was verified to produce predictions identical to the training code.
Runs on CPU or CUDA/ROCm; pass `device="cpu"` to force CPU.

## Limitations

- **English only.** Other languages mostly score safe (common profanity from the
  training data is still caught). Not a language detector.
- Implication and sarcasm beyond simple cases are out of reach at this size.
- The test split contains label noise from the scraped sources, which caps the
  measurable F1 there.
- A moderation signal, not a safety guarantee.