Privacy-preserving early toxicity detection for mobile keyboards using personalized federated learning and LoRA-adapted lightweight transformers.
FedPref is a personalized federated learning framework for while-typing toxicity detection:
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Client 1 │ │ Client 2 │ ... │ Client N │
│ LoRA_A │ │ LoRA_B │ │ LoRA_N │
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ LoRA weights │ │
└──────────────┴──────────┬─────────┘
▼
┌─────────────────┐
│ Flower Server │
│ FedAvg LoRA │
└─────────────────┘
Training:
Full comment → Prefix sampler [8, 16, 32, 64, 128]
│
Shared encoder (frozen on-device)
│
┌───────┴───────┐
│ LoRA adapters│ ← Per-client, FL-aggregated
└───────┬───────┘
│
[CLS] → Classification head
│
▼
Toxicity prob at EACH prefix length
uv sync
# Module 1: Data preparation (cleaning)
uv run python module_01_data_preparation.py
# Module 2: Tokenization
uv run python module_02_tokenization.py
# Module 3: Centralized DistilBERT baseline
uv run python module_03_training.py
# Module 4: Model definitions + LoRA (verification)
uv run python -c "from models.registry import create_model; m = create_model(config); print(m.get_num_parameters())"
# Module 5: Federated learning (FedPref)
uv run python module_05_federated.py
# Module 6: Prefix-based early detection training
uv run python module_06_prefix.py
# Module 7: Model optimization (quantization, distillation, pruning)
uv run python module_07_optimization.py
# Module 8: Interactive simulation
streamlit run simulation/app.py
| Metric | Value |
|---|---|
| Centralized F1-macro (DistilBERT, full text) | 0.698 |
| Federated F1-macro (FedPref, LoRA r=8) | 0.695 |
| Prefix detection point | ~35 tokens |
| Characters saved per toxic comment | ~85 chars |
| Communication savings (LoRA vs full model) | 99.96% |
| Deployed model size (TinyBERT + INT8) | 10 MB |
| On-device latency | 45 ms |
The ATAM-FET experimental workflow evaluates the Adaptive Toxic Attention Module (ATAM) with DistilBERT and extends the evaluation to federated learning settings.
ATAM was integrated with DistilBERT for multi-label toxicity classification and evaluated on the Jigsaw Toxic Comment dataset.
| Metric | Value |
|---|---|
| Macro-F1 | 0.6818 |
| Micro-F1 | 0.7713 |
| ROC-AUC | 0.9878 |
The evaluation also includes per-label performance analysis, training curves, confusion matrices, and attention-based analysis.
Federated experiments were conducted under both IID and non-IID data distributions.
The completed non-IID experiment used Dirichlet partitioning (α=0.5) across 10 simulated clients.
| Metric | Value |
|---|---|
| Macro-F1 | 0.4721 |
| Micro-F1 | 0.6084 |
| ROC-AUC | 0.9822 |
| Mean Client F1 | 0.4530 ± 0.0244 |
The experimental notebooks and saved outputs are available in the updates branch.
Two strategies for heterogeneous client simulation:
Both are configurable via configs/config.yaml.
├── configs/config.yaml ← All hyperparameters
├── data/ ← Raw + processed + partitions
├── preprocessing/ ← Cleaning (M1) + Tokenization (M2)
├── training/ ← Centralized (M3) + Prefix (M6)
├── models/ ← DistilBERT + TinyBERT + MobileBERT + LoRA (M4)
├── federated/ ← Flower clients, server, strategies (M5)
├── evaluation/ ← Metrics + prefix eval + ablation (M9)
├── optimization/ ← Quantization, distillation, pruning (M7)
├── simulation/ ← Streamlit interactive demo (M8)
├── results/ ← Checkpoints, logs, plots, reports (M10)
├── utils/ ← Config loader, helpers
├── module_0*_*.py ← Entry points for each module
└── PROJECT_STATUS.md ← Current state and progress
Jigsaw Toxicity Dataset by Google https://huggingface.co/datasets/google/jigsaw_toxicity_pred
If you use this work in your research, please cite:
@software{fedpref2025,
title = {FedPref: Federated Prefix-Based Early Toxicity Detection},
author = {Your Name},
year = {2025},
}
Research purpose.
Privacy-preserving early toxicity detection for mobile keyboards using personalized federated learning and LoRA-adapted lightweight transformers.
FedPref is a personalized federated learning framework for while-typing toxicity detection:
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Client 1 │ │ Client 2 │ ... │ Client N │
│ LoRA_A │ │ LoRA_B │ │ LoRA_N │
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ LoRA weights │ │
└──────────────┴──────────┬─────────┘
▼
┌─────────────────┐
│ Flower Server │
│ FedAvg LoRA │
└─────────────────┘
Training:
Full comment → Prefix sampler [8, 16, 32, 64, 128]
│
Shared encoder (frozen on-device)
│
┌───────┴───────┐
│ LoRA adapters│ ← Per-client, FL-aggregated
└───────┬───────┘
│
[CLS] → Classification head
│
▼
Toxicity prob at EACH prefix length
uv sync
# Module 1: Data preparation (cleaning)
uv run python module_01_data_preparation.py
# Module 2: Tokenization
uv run python module_02_tokenization.py
# Module 3: Centralized DistilBERT baseline
uv run python module_03_training.py
# Module 4: Model definitions + LoRA (verification)
uv run python -c "from models.registry import create_model; m = create_model(config); print(m.get_num_parameters())"
# Module 5: Federated learning (FedPref)
uv run python module_05_federated.py
# Module 6: Prefix-based early detection training
uv run python module_06_prefix.py
# Module 7: Model optimization (quantization, distillation, pruning)
uv run python module_07_optimization.py
# Module 8: Interactive simulation
streamlit run simulation/app.py
| Metric | Value |
|---|---|
| Centralized F1-macro (DistilBERT, full text) | 0.698 |
| Federated F1-macro (FedPref, LoRA r=8) | 0.695 |
| Prefix detection point | ~35 tokens |
| Characters saved per toxic comment | ~85 chars |
| Communication savings (LoRA vs full model) | 99.96% |
| Deployed model size (TinyBERT + INT8) | 10 MB |
| On-device latency | 45 ms |
The ATAM-FET experimental workflow evaluates the Adaptive Toxic Attention Module (ATAM) with DistilBERT and extends the evaluation to federated learning settings.
ATAM was integrated with DistilBERT for multi-label toxicity classification and evaluated on the Jigsaw Toxic Comment dataset.
| Metric | Value |
|---|---|
| Macro-F1 | 0.6818 |
| Micro-F1 | 0.7713 |
| ROC-AUC | 0.9878 |
The evaluation also includes per-label performance analysis, training curves, confusion matrices, and attention-based analysis.
Federated experiments were conducted under both IID and non-IID data distributions.
The completed non-IID experiment used Dirichlet partitioning (α=0.5) across 10 simulated clients.
| Metric | Value |
|---|---|
| Macro-F1 | 0.4721 |
| Micro-F1 | 0.6084 |
| ROC-AUC | 0.9822 |
| Mean Client F1 | 0.4530 ± 0.0244 |
The experimental notebooks and saved outputs are available in the updates branch.
Two strategies for heterogeneous client simulation:
Both are configurable via configs/config.yaml.
├── configs/config.yaml ← All hyperparameters
├── data/ ← Raw + processed + partitions
├── preprocessing/ ← Cleaning (M1) + Tokenization (M2)
├── training/ ← Centralized (M3) + Prefix (M6)
├── models/ ← DistilBERT + TinyBERT + MobileBERT + LoRA (M4)
├── federated/ ← Flower clients, server, strategies (M5)
├── evaluation/ ← Metrics + prefix eval + ablation (M9)
├── optimization/ ← Quantization, distillation, pruning (M7)
├── simulation/ ← Streamlit interactive demo (M8)
├── results/ ← Checkpoints, logs, plots, reports (M10)
├── utils/ ← Config loader, helpers
├── module_0*_*.py ← Entry points for each module
└── PROJECT_STATUS.md ← Current state and progress
Jigsaw Toxicity Dataset by Google https://huggingface.co/datasets/google/jigsaw_toxicity_pred
If you use this work in your research, please cite:
@software{fedpref2025,
title = {FedPref: Federated Prefix-Based Early Toxicity Detection},
author = {Your Name},
year = {2025},
}
Research purpose.