This repository contains the code and running instructions for the ACL paper "SLIM: Stealthy Low-Coverage Black-Box Watermarking via Latent-Space Confusion Zones". The project studies a stealthy black-box watermarking pipeline that identifies low-coverage candidate texts, constructs watermarked training data, trains a target language model, and verifies the watermark through t-statistic-based analysis.
python -m venv .venv
source .venv/bin/activateOn Windows PowerShell:
python -m venv .venv
.venv\Scripts\Activate.ps1Install the Python packages required by the scripts:
pip install torch transformers datasets huggingface_hub openai pandas matplotlib scipy sentence-transformers scikit-learn evaluate bert-scoreIf you prefer to use requirements.txt, you can use:
pip install -r requirements.txtThis project requires both Hugging Face and OpenAI access.
Set your Hugging Face token:
export HF_TOKEN=your_huggingface_tokenOn Windows PowerShell:
$env:HF_TOKEN="your_huggingface_token"Set your OpenAI API key:
export OPENAI_API_KEY=your_openai_api_keyOn Windows PowerShell:
$env:OPENAI_API_KEY="your_openai_api_key"CandidatePrepare.py, ModelFineTune.py, and ModelPreTrain.py load large language models. A CUDA GPU with enough VRAM is strongly recommended for practical runs.
The full workflow is:
- Prepare candidate sequences.
- Generate the watermarked dataset.
- Fine-tune or pre-train a model on the watermarked dataset.
- Compute the t-statistic for verification.
Run:
python CandidatePrepare.pyDefault behavior:
- Loads the base selection model and source dataset.
- Computes perplexity-based filtering.
- Saves the selected candidate sequence to
Final_Candidate.jsonl.
Example with explicit arguments:
python CandidatePrepare.py \
--seed 42 \
--model-name EleutherAI/pythia-12b-deduped \
--ds-name gfissore/arxiv-abstracts-2021 \
--dynamic-threshold 20 \
--min-tokens 180 \
--save-path Final_Candidate.jsonlRun:
python WatermarkDataset.pyDefault behavior:
- Reads
Final_Candidate.jsonl. - Splits the selected candidate into prefix and continuation.
- Uses the OpenAI API to generate paraphrased prefixes and new continuations.
- Writes the final watermarked training data to
Watermark_dataset_final.csv.
Example with explicit arguments:
python WatermarkDataset.py \
--seed 42 \
--ds-name gfissore/arxiv-abstracts-2021 \
--split-ratio 0.2 \
--ver-num 16 \
--input-file Final_Candidate.jsonl \
--out-csv Watermark_dataset_final.csvYou can choose either fine-tuning or pre-training.
python ModelFineTune.pyExample:
python ModelFineTune.py \
--seed 42 \
--model-name google/gemma-3-4b-pt \
--csv-path Watermark_dataset_final.csv \
--ver-num 16 \
--max-len 1024 \
--min-tokens 50 \
--output-dir ./gemma-3-4b-pt_WM_FTpython ModelPreTrain.pyExample:
python ModelPreTrain.py \
--seed 42 \
--model-name EleutherAI/pythia-1.4b \
--csv-path Watermark_dataset_final.csv \
--ver-num 16 \
--max-len 512 \
--min-tokens 50 \
--output-dir ./pythia-1.4b_WM_PTRun:
python TStatistixCalculation.pyDefault behavior:
- Reads
Final_Candidate.jsonl. - Generates continuations from the verification model.
- Computes BERTScore-based statistics and reports the t-statistic.
Example:
python TStatistixCalculation.py \
--inp-file Final_Candidate.jsonl \
--n-words 3 \
--model-dir ./gemma-3-4b-pt_WM_FT- The scripts are written as standalone Python CLI programs.
- Default argument values are already defined inside each file.