CRSE / FBA Encoder is a robust semantic encoder designed for Byzantine-resilient multi-agent agreement. It aims to map semantically consistent honest proposals close to each other while separating adversarial or semantically poisoned proposals in the embedding space.
This repository supports my broader research on trustworthy multi-agent AI systems, semantic commitment, and Byzantine-resilient collaboration, where multiple agents may submit natural-language or structured proposals under disagreement, uncertainty, or adversarial manipulation.
Modern multi-agent AI systems increasingly rely on natural-language proposals, explanations, and evidence-grounded judgments. However, standard semantic encoders may fail to distinguish between honest semantic variations and Byzantine semantic attacks, such as polarity flips, misleading evidence use, hallucinated causal relations, or adversarially shifted claims.
This project explores contrastive training strategies for improving semantic separation between honest and Byzantine proposals, with the goal of supporting downstream mechanisms such as fuzzy Byzantine agreement, certified semantic commitment, and selective commitment in LLM-agent collaboration.
- Transformer-based semantic encoding for proposal-level representation.
- Contrastive learning for honest-honest alignment and honest-Byzantine separation.
- Support for semantic attack patterns such as polarity flip, semantic shift, and misleading claim transformation.
- Designed as a research module for Byzantine-resilient semantic agreement and trustworthy multi-agent AI systems.
This repository is part of a broader research agenda on protocol-level trust mechanisms for multi-agent AI collaboration, including:
- Byzantine-resilient semantic agreement;
- semantic robustness under adversarial disagreement;
- evidence-aware and certificate-based commitment;
- selective commitment and safe-abort mechanisms for LLM-agent systems.
# Clone the repository
git clone https://github.com/HrxuAlbert/FBA_ENCODER.git
cd FBA_ENCODER
# Install dependencies
pip install -r requirements.txtRequirements:
- Python 3.8+
- PyTorch 2.0+
- Transformers 4.30+
CRSE training requires a JSONL dataset where each line is an "anchor group":
{
"anchor": {"text": "Original paragraph...", "source_id": "id1"},
"positives": [
{"text": "Faithful paraphrase...", "id": "id1_P1"}
],
"byzantine": [
{"text": "Semantic attack...", "id": "id1_B1", "attack_type": "B1_POLARITY_FLIP", "budget": "mild"}
],
"stats": {"n_positives": 1, "n_byzantine": 12}
}Use data_preparation/build_dataset.py to construct this from separate files:
python3 data_preparation/build_dataset.py \
--anchors path/to/source.jsonl \
--paraphrases path/to/paraphrases.jsonl \
--attacks path/to/attacks.jsonl \
--output data/crse/dataset.jsonlpython3 training/train_crse.py \
--profile local_mini \
--data data/crse/dataset.jsonl \
--output-dir checkpoints/crse_localpython3 training/train_crse.py \
--profile gpu_full \
--data data/crse/dataset.jsonl \
--output-dir checkpoints/crse_gpu \
--batch-groups 32 \
--epochs 10import torch
from crse.model import CRSEModel
# Load model
model = CRSEModel(projection_dim=512)
model.load_state_dict(torch.load("checkpoints/crse_gpu/best_model.pt"))
model.eval()
# Encode texts
texts = ["This is a test.", "Another sentence."]
embeddings = model.encode(texts, normalize=True)
print(embeddings.shape) # (2, 512)FBA_ENCODER/
βββ crse/ # CRSE model core
β βββ __init__.py
β βββ model.py # CRSEModel definition
β βββ config.py # Training configurations
β
βββ training/ # Training scripts
β βββ train_crse.py # Main training script
β βββ dataset.py # Dataset loader
β βββ loss.py # Contrastive loss
β βββ evaluation.py # Geometry evaluation
β
βββ data_preparation/ # Data preprocessing tools
β βββ build_dataset.py # Build CRSE dataset
β
βββ scripts/ # Helper scripts
β βββ train_local.sh # Local training script
β βββ train_gpu.sh # GPU training script
β
βββ docs/ # Documentation
β βββ TRAINING_GUIDE.md # Detailed training guide
β βββ DATA_PREPARATION.md # Data preparation guide
β βββ QUICKSTART.md # Quick start tutorial
β
βββ README.md # This file
βββ requirements.txt # Python dependencies
βββ LICENSE # MIT License
βββ .gitignore # Git ignore rules
CRSE uses anchor-centered contrastive learning:
-
Anchor groups: Each training sample consists of:
- 1 anchor text (original)
- N positives (faithful paraphrases)
- M negatives (Byzantine attacks)
-
Contrastive loss: InfoNCE loss that:
- Pulls anchor and honest paraphrases together (high cosine similarity)
- Pushes anchor and Byzantine attacks apart (low cosine similarity)
-
Byzantine attacks: Four attack types with three intensity levels:
B1_POLARITY_FLIP: Reverse factual claimsB2_EVIDENCE_OMISSION: Remove caveats/limitationsB3_FAKE_CAUSALITY: Introduce false causal linksB4_ON_TOPIC_HALLUCINATION: Add plausible but false details- Budgets:
mild,medium,strong
-
Evaluation metric: Separation = cos(anchor, para) - cos(anchor, byz)
| Profile | Device | Batch Groups | Epochs | Projection Dim | Use Case |
|---|---|---|---|---|---|
local_mini |
MPS/CPU | 8 | 5 | 256 | Quick local testing |
gpu_full |
CUDA | 32 | 10 | 512 | Full GPU training |
Customize with command-line arguments:
python3 training/train_crse.py \
--profile gpu_full \
--data data/crse/dataset.jsonl \
--batch-groups 64 \
--epochs 15 \
--lr 1e-5- Quick Start Tutorial: Step-by-step beginner's guide
- Training Guide: Detailed training instructions
- Data Preparation Guide: How to prepare training data
- Dataset Specification: Complete dataset format and requirements
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Author: Haoran Xu
Email: 2614067X@student.gla.ac.uk
Institution: University of Glasgow
GitHub: @HrxuAlbert
- RAP-BFT: Byzantine Fault-Tolerant consensus using CRSE
https://github.com/HrxuAlbert/RAP-BFT
If you use CRSE in your research, please cite:
@misc{crse2025,
author = {Xu, Haoran},
title = {CRSE: Certified Robust Semantic Encoder},
year = {2025},
publisher = {GitHub},
url = {https://github.com/HrxuAlbert/FBA_ENCODER}
}- Built on E5-base-v2
- Inspired by contrastive learning literature in NLP
- Byzantine attack taxonomy designed for distributed consensus systems