M4: Qwen3-4B fine-tuned as a toxicity moderator
M4 of the CS 769 (UW–Madison) project Auditing Social Bias in Language Models: Metrics & a
Category-Aware Framework (Natarajan, Ethiraj, Venkatesan). It is Qwen/Qwen3-4B (M0) after
full fine-tuning with a counterfactual loss on cleaned ETHOS and ToxicCommons posts, trained to answer whether a post is toxic. The project
compares the base model (M0), plain fine-tuning (M2) and fine-tuning with a counterfactual loss (M4), with
and without few-shot prompting, using a counterfactual bias test:
how much the answer moves when only the gender or disability a post refers to is swapped, beyond how
much it moves under matched neutral rewordings (Yang et al., 2026).
How to use
The model is read as a classifier: the probability of the first answer token after the prompt it was trained on.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = "gayathri-er/kqv-m4-qwen3-4b-v1"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, torch_dtype="auto", device_map="auto")
def p_toxic(post):
messages = [
{"role": "system", "content": 'You are a content moderator.'},
{"role": "user", "content": 'Is the following social media post toxic?' + "\n\nPost: " + post
+ "\n\nAnswer with exactly one of: No, Yes."},
]
text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False)
ids = tok(text, return_tensors="pt", add_special_tokens=False).to(model.device)
logits = model(**ids).logits[0, -1]
options = [tok('No', add_special_tokens=False).input_ids[0], tok('Yes', add_special_tokens=False).input_ids[0]]
return torch.softmax(logits[options].float(), dim=-1)[1].item()
print(p_toxic("My deaf cousin keeps texting me about the game tonight."))
In the project's bias framework (bias-compare-poc) it is an hf-causal-lm model; m2_prompt.yaml in
this repo is the prompt to score it with.
Training
| Base model (M0) | Qwen/Qwen3-4B |
| Method | full fine-tuning, lr 1e-05, adamw_bnb_8bit, bfloat16 |
| Objective | cross-entropy on the first token of the correct answer ('Yes' = toxic, 'No' = not toxic) after the prompt above. Plus the counterfactual loss: each training post is paired with a version where only the gender (lexicon swap) or a disability mention (adjective added or removed) changes; loss = ce + 1.0 x ce(counterfactual, same label) + 0.5 x (log-odds original - log-odds counterfactual)^2 (Garg et al., 2019). Posts whose hate targets the swapped group get no pair. |
| Data | 20,000 training rows from the project's train split (Civil Comments + ETHOS, English, deduplicated), oversampled: disability=0.1 |
| Epochs / effective batch | 1.0 / 32 |
| Max prompt length | 1024 tokens (cut from the left) |
| Selection | none: the weights after the last step (no early stopping); validation loss logged every 100 steps |
| Steps / time | 625 steps, 58.5 min on NVIDIA A100-SXM4-80GB |
Evaluation (M0 = base model, M4 = this model)
Scored with the bias framework's LLM adapter on held-out posts. FPR is the share of benign posts flagged toxic; "mentions gender/disability" counts every post that mentions the category.
| split | group | acc M0 | acc M4 | macro-F1 M0 | macro-F1 M4 | AUROC M0 | AUROC M4 | FPR M0 | FPR M4 | FNR M0 | FNR M4 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| val | overall | 0.533 | 0.923 | 0.428 | 0.923 | 0.617 | 0.978 | 0.899 | 0.087 | 0.010 | 0.067 |
| val | mentions gender | 0.568 | 0.902 | 0.427 | 0.901 | 0.588 | 0.968 | 0.921 | 0.131 | 0.017 | 0.069 |
| val | mentions disability | 0.522 | 0.898 | 0.412 | 0.898 | 0.603 | 0.962 | 0.913 | 0.140 | 0.004 | 0.062 |
| val | mentions none | 0.510 | 0.938 | 0.424 | 0.938 | 0.634 | 0.986 | 0.888 | 0.060 | 0.005 | 0.064 |
| test | overall | 0.546 | 0.933 | 0.455 | 0.933 | 0.627 | 0.980 | 0.868 | 0.075 | 0.009 | 0.059 |
| test | mentions gender | 0.580 | 0.932 | 0.464 | 0.931 | 0.620 | 0.984 | 0.878 | 0.086 | 0.013 | 0.053 |
| test | mentions disability | 0.546 | 0.926 | 0.457 | 0.926 | 0.621 | 0.979 | 0.864 | 0.092 | 0.006 | 0.053 |
| test | mentions none | 0.514 | 0.939 | 0.437 | 0.938 | 0.636 | 0.980 | 0.870 | 0.058 | 0.007 | 0.065 |
These are error rates on labelled data.
Counterfactual gap on lexicon/adjective swaps of held-out posts: mean |P(toxic) original − P(toxic) swapped| (also on benign posts only), direction = the women's / disabled version minus the other (positive = scored as more toxic), and the share of answers that flip. This is the raw gap, without the paraphrase baseline. The project's bias result (Δ, which subtracts that baseline) is reported separately.
| split | axis | pairs | gap M0 | gap M4 | gap benign M0 | gap benign M4 | direction M0 | direction M4 | flips M0 | flips M4 |
|---|---|---|---|---|---|---|---|---|---|---|
| val | gender | 300 | 0.009 | 0.002 | 0.006 | 0.003 | 0.003 | 0.001 | 0.007 | 0.000 |
| val | disability | 142 | 0.013 | 0.006 | 0.019 | 0.007 | -0.005 | -0.001 | 0.021 | 0.007 |
| test | gender | 300 | 0.009 | 0.002 | 0.019 | 0.003 | 0.008 | 0.000 | 0.010 | 0.000 |
| test | disability | 210 | 0.024 | 0.005 | 0.045 | 0.006 | 0.024 | -0.001 | 0.024 | 0.005 |
Data and limitations
- ETHOS (Mollas et al., 2020): YouTube and Reddit comments with crowd-sourced hate labels; GPL-3.0 licence.
- ToxicCommons (Arnett et al., 2024), via
tcapelle/ToxicCommons-balanced(no licence stated on the dataset card): mostly OCR'd public-domain books and newspapers, labelled by Llama-3.1-8B, so the domain is not social media and the labels are model-made. - English only. A research artefact for studying moderation bias, not a production moderator.
- Check the data licences before redistributing this model; the licence field above is the base model's.