This repository contains the implementation of Shielded RecRL, a novel method for adding chat-style explanations to recommender systems without affecting the underlying ranking model's performance. The key innovation is a gradient projection technique that prevents the explanation model from interfering with the ranking model.
Shielded RecRL uses a two-tower architecture:
- Frozen Ranking Model: SASRec (collaborative filtering) that remains unchanged
- Trainable Language Model: TinyLlama-1.1B with LoRA adapters that generates explanations
The gradient projection technique ensures that the explanation model can learn to generate helpful explanations without degrading the ranking model's recommendation quality.
- Python 3.8+
- CUDA-compatible GPU (8GB+ VRAM for local testing, 24GB+ for full experiments)
- Git
- Conda (recommended for environment management)
-
Clone the repository:
git clone https://github.com/your-username/shielded-recrl.git cd shielded-recrl -
Set up the environment:
# Create conda environment conda create -n rec python=3.10 conda activate rec # Install PyTorch with CUDA support conda install pytorch=2.3 torchvision=0.18 torchaudio=2.3 pytorch-cuda=12.2 -c pytorch -c nvidia # Install other dependencies pip install -r requirements.txt # Fix bitsandbytes for CUDA 12.2 pip install bitsandbytes-cuda122==0.43.0
-
Verify installation:
python gpu_test.py bash environment_check.sh
For Windows users, see the detailed guide in WINDOWS_SETUP.md or run:
# Run PowerShell as Administrator
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
.\setup_windows.ps1The project supports three datasets:
- Amazon Books (recommended for testing)
- MovieLens-25M
- Steam-200K
For a quick test on a 6GB GPU:
# Run the complete pipeline with minimal settings
bash scripts/run_local_smoke.shThis script will:
- Download and preprocess the Books dataset
- Train a SASRec ranking model
- Compute gradient projection basis
- Initialize LoRA adapters with TinyLlama
- Run a small Shielded RecRL training session
For full experiments, we recommend using RunPod with an A100 80GB GPU:
-
Launch RunPod instance:
- Runtime: PyTorch 2.3 | Python 3.10 | CUDA 12.2
- GPU: NVIDIA A100 80GB or 2Γ RTX 4090 24GB
- Volume: β₯ 400GB
-
SSH into your instance:
ssh -p YOUR_PORT runpod@YOUR_POD_ID.connect.runpod.io
-
Set up the environment:
# Clone your repository cd /workspace git clone https://github.com/your-username/shielded-recrl.git cd shielded-recrl # Run setup script bash setup_runpod.sh
-
Run full experiments:
bash scripts/run_runpod_full.sh
If you prefer to run each step manually:
# Download and preprocess all datasets
bash code/dataset/run_preprocessing.sh# Train SASRec on all datasets
cd code/ranker
bash run_training.sh
cd ../..# Compute gradient projection basis for each dataset
cd code/projection
python run_basis.py --dataset books --proj_dir ../..
python run_basis.py --dataset ml25m --proj_dir ../..
python run_basis.py --dataset steam --proj_dir ../..
cd ../..# Initialize LoRA adapters for all datasets
cd code/explainer
bash run_lora_init.sh --int8
cd ../..# Main experiment with gradient projection
cd code/trainer
python run_recrl_cli.py \
--dataset books \
--ranker_ckpt ../../checkpoints/sasrec_books.pt \
--projection_basis ../../checkpoints/basis_books.pt \
--lora_rank 16 \
--kl_beta 0.05 \
--micro_batch_size 4 \
--grad_accum 2 \
--max_seq_len_explainer 384 \
--explanation_max_len 160 \
--max_steps 20000 \
--tag shielded
# Ablation: No projection
python run_recrl_cli.py \
--dataset books \
--ranker_ckpt ../../checkpoints/sasrec_books.pt \
--no_projection \
--lora_rank 16 \
--kl_beta 0.05 \
--max_steps 10000 \
--tag no_proj
# Ablation: KL=0
python run_recrl_cli.py \
--dataset books \
--ranker_ckpt ../../checkpoints/sasrec_books.pt \
--projection_basis ../../checkpoints/basis_books.pt \
--lora_rank 16 \
--kl_beta 0.0 \
--max_steps 10000 \
--tag kl0
cd ../..# Run toxicity, bias, and privacy audits
cd code/audit
python run_audit.py --model_ckpt ../../checkpoints/recrl/books/latest
cd ../..# Generate final results and plots
cd code/eval
python aggregate_main.py \
--runs_dir ../../logs \
--out_csv ../../experiments/aggregate_results.csv \
--out_dir ../../experiments/figs
cd ../..shielded-recrl/
βββ code/
β βββ dataset/ # Dataset preprocessing and download
β βββ ranker/ # SASRec ranking model implementation
β βββ explainer/ # LLM with LoRA adapters
β βββ projection/ # Gradient projection implementation
β βββ trainer/ # Shielded PPO training
β βββ eval/ # Evaluation and aggregation
β βββ audit/ # Toxicity, bias, privacy audits
βββ data/
β βββ raw/ # Raw datasets
β βββ proc/ # Processed datasets
β βββ _checksums/ # Dataset integrity checks
βββ checkpoints/ # Model checkpoints
βββ logs/ # Training logs
βββ experiments/ # Configuration files
βββ docs/ # Documentation
βββ scripts/ # Automation scripts
βββ docker/ # Docker configuration
- Computes orthogonal basis for the ranking model's parameter space
- Projects gradients to prevent interference with ranking performance
- Implements PPO with gradient projection
- Maintains ranking model performance while training explanations
- Amazon Books: E-commerce recommendations
- MovieLens-25M: Movie recommendations
- Steam-200K: Game recommendations
The experiments evaluate:
- Ranking Performance: NDCG@10, HR@10 (should remain stable)
- Explanation Quality: BLEU, ROUGE, human evaluation
- Safety: Toxicity, bias, privacy audits
Modify experiment settings in experiments/recrl_default.yaml:
books:
epochs: 8
ppo_batch: 256
sim_batch: 32
kl_beta: 0.05
int8: true
lr: 3e-5
max_new_tokens: 40
temperature: 0.7
top_p: 0.9
seed: 42- CUDA out of memory: Use
--int8flag or reduce batch sizes - Import errors: Ensure conda environment is activated (
conda activate rec) - Dataset download fails: Check internet connection and disk space
- Git LFS issues: Install Git LFS:
sudo apt-get install git-lfs && git lfs install
- Local testing: 6GB GPU (use TinyLlama + 4-bit quantization)
- Full experiments: 24GB+ GPU (A100 80GB recommended)
- CPU fallback: Available but very slow
- Use
--int8for 8-bit quantization to reduce memory usage - Adjust
micro_batch_sizeandgrad_accumbased on your GPU - For local testing, use the Books dataset only
If you use this code in your research, please cite:
@article{shielded-recrl,
title={Shielded RecRL: Gradient-Shielded Recommender Systems with Explanations},
author={Your Name},
journal={arXiv preprint},
year={2024}
}- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request