Skip to main content

Post-hoc LLM watermarking toolkit

Project description

Text Seal

Meta Text Seal is a comprehensive toolkit for LLM generation-time watermarking, post-hoc text watermarking through LLM rephrasing, and contamination detection through watermark radioactivity. It is part of the Meta Seal family of watermarking technologies.

[post-hoc paper] [contamination paper] [meta seal] [colab]

Features

  • 🔏 Post-hoc Watermarking: Rephrase text with an LLM while inserting a watermark using generation-time scheme (Green-list/Red-list, Gumbel-max, DipMark, SynthID, MorphMark, WaterMax, etc.).
  • 🧪 Contamination Detection: Detect watermarked dataset membership inference through radioactivity.
  • 🚀 Training Infrastructure: Distributed pretraining and SFT with contamination injection support for research purposes.

Papers

This codebase implements methods from:

Quick Start

Installation

Option 1: pip install (fastest)

pip install textseal

Option 2: Install from source

git clone https://github.com/facebookresearch/textseal.git
cd textseal
pip install -e .

Python API

Watermark text using the Python API:

from textseal import PostHocWatermarker, WatermarkConfig, ModelConfig, ProcessingConfig

# Basic usage with defaults
watermarker = PostHocWatermarker()
result = watermarker.process_text("Your text here")
print(result["wm_text"])  # Watermarked text
print(result["wm_eval"]["p_value"])  # Detection p-value

# Custom configuration
watermarker = PostHocWatermarker(
    watermark_config=WatermarkConfig(watermark_type="gumbelmax"),
    model_config=ModelConfig(model_name="meta-llama/Llama-3.2-3B-Instruct"),
    processing_config=ProcessingConfig(temperature=0.8, top_p=0.95),
)
result = watermarker.process_text("Text to watermark")

💡 Tip: Increasing watermark strength. For Gumbel-max watermarking, increase temperature for stronger watermarks (e.g., temperature=1.2 in ProcessingConfig). For Greenlist watermarking, increase delta (e.g., delta=3.0 in WatermarkConfig). See Watermark Configuration Guide for details.

See docs/README_posthoc.md for detailed documentation on the configurations and usage.

Common Use Cases:

  • Watermarking + Detection: Use process_text() to watermark text and get detection metrics (p-value, score) in one call.
  • Watermarking Only: Use rephrase_with_watermark() to get just the watermarked text without evaluation.
  • Detection Only: Set enable_detection_only=True and use evaluate_watermark() to check existing text for watermarks without loading the LLM.

See docs/README_posthoc_api.md for complete API usage examples

Command Line Interface

After installing textseal, you get the textseal-watermark CLI command:

# Get help
textseal-watermark --help

# Watermark a file
textseal-watermark --input_path document.txt --dump_dir output/

# Detection-only mode
textseal-watermark --input_path text_to_check.txt --evaluation.enable_detection_only true

Using the Repository

Installation

Option 3: Development setup

# Clone the repository
git clone https://github.com/facebookresearch/textseal.git
cd textseal

# Create environment and install dependencies
conda create -n text_seal python=3.11.13
conda activate text_seal
pip install -r requirements.txt

💡 For contamination detection experiments (training with contamination injection), you need additional setup. First follow the Meta Lingua installation instructions, then install the requirements above. See Environment Setup for details.

Post-hoc Watermarking

For batch processing or command-line workflows, use the CLI:

python -m textseal.posthoc.main \
  --input_path assets/sample_document.txt \
  --dump_dir output/ \
  --watermark.watermark_type gumbelmax \
  --model.model_name meta-llama/Llama-3.2-3B-Instruct \
  --processing.temperature 1.0 \
  --processing.top_p 0.95

Results are saved in output/ directory as a JSONL file containing original, watermarked text and statistics.

Contamination Detection

Inject watermarked benchmarks during training and detect memorization through watermark radioactivity.

Download DCLM training data and benchmark datasets (ARC-Easy, ARC-Challenge, MMLU). See Data Preparation in the contamination docs.

The contamination detection workflow consists of three steps, each with its own experiment configuration file:

# Step 1: Watermark benchmarks with different secret keys
python -m textseal.posthoc.main --config configs/watermark_benchmarks.yaml

# Step 2: Train model with contaminated watermarked data
python -m textseal.common.stool script=textseal.wmtraining.train \
  config=configs/train_with_contamination.yaml \
  nodes=4 ngpu=8 partition=learn qos=high time=4320

# Step 3: Detect contamination via watermark evaluation
python -m textseal.wmtraining.eval_wm --config configs/eval_contamination.yaml

Configuration files:

See docs/README_contamination.md for detailed documentation.

Documentation

Repository Structure

textseal/
├── textseal/
│   ├── posthoc/          # Post-hoc watermarking
│   ├── wmtraining/       # Training and evaluation
│   ├── analysis/         # Analysis tools
│   └── common/           # Shared utilities (LLM, watermark, config)
├── docs/                 # Detailed documentation
├── configs/              # Example configurations for watermarking and training
├── assets/               # Sample texts
├── setup/                # Setup scripts and data processing

Use Cases

1. Content Authentication

Watermark text to enable verification and provenance tracking.

2. Dataset Contamination Detection

Detect if evaluation benchmarks were included in training data by injecting watermarked versions and checking for "radioactivity."

3. Research on Watermarking

Experiment with different watermarking algorithms and detection methods on your own models and datasets.

License

Meta Text Seal is released under the MIT License.

It relies on code and models from other repositories. The contamination detection app builds on Meta Lingua for training, which has a BSD 3-Clause License. The models used for post-hoc watermarking are loaded from Hugging Face and are subject to their respective licenses.

Citation

If you use Text Seal in your research, please cite:

@article{sander2025detecting,
  title={Detecting benchmark contamination through watermarking},
  author={Sander, Tom and Fernandez, Pierre and Mahloujifar, Saeed and Durmus, Alain and Guo, Chuan},
  journal={arXiv preprint arXiv:2502.17259},
  year={2025}
}

@article{fernandez2025how,
  title={How Good is Post-Hoc Watermarking With Language Model Rephrasing?},
  author = {Fernandez, Pierre and Sander, Tom and Elsahar, Hady and Chang, Hongyan and Sou\v{c}ek, Tom\'{a}\v{s} and Lacatusu, Valeriu and Tran, Tuan and Rebuffi, Sylvestre-Alvise and Mourachko, Alexandre},
  year={2025}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textseal-0.0.5.tar.gz (161.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textseal-0.0.5-py3-none-any.whl (178.9 kB view details)

Uploaded Python 3

File details

Details for the file textseal-0.0.5.tar.gz.

File metadata

  • Download URL: textseal-0.0.5.tar.gz
  • Upload date:
  • Size: 161.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for textseal-0.0.5.tar.gz
Algorithm Hash digest
SHA256 4ecf92ebdfa20e14c34d4e65486e5e93b1704aa7c7787371a3ef767e7c302f5b
MD5 4ea2c9a673d96d669d55e1fe272504b0
BLAKE2b-256 60cacabe1a1fcf2cae02e953fba6ecc3aa4fb7365f92b52bbc02655727c2efca

See more details on using hashes here.

File details

Details for the file textseal-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: textseal-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 178.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for textseal-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6a09d87771158920b912edf0aa06fa026914dd60f59a7b47383b78c71d731111
MD5 ad390045a0725340720a9a0173d3609f
BLAKE2b-256 fca64e5e45a491af8a20ddc38ea19c5c81361a3140ff871dae8aaffacef9c033

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page