sbw
Stateless Bernoulli Watermarking — A fast, efficient watermark detection library for LLM-generated text.
This package provides watermark detection capabilities. For watermark injection during text generation, see vllm-sbw.
Features
- GPU-accelerated detection using PyTorch and custom Philox PRNG
- Batch processing for efficient detection on multiple texts
- Multiple seeding schemes: selfhash, lefthash, minhash, and additive
- Text normalization with Unicode and homoglyph handling
Installation
pip install sbw
Quick Start
from transformers import AutoTokenizer
from sbw import WatermarkDetector
import torch
# Initialize detector
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
detector = WatermarkDetector(
device=torch.device("cuda"),
tokenizer=tokenizer,
vocab=list(range(len(tokenizer))),
gamma=0.25,
seeding_scheme="selfhash",
hash_key=15485863,
z_threshold=4.0,
)
# Detect watermark
text = "Your text to analyze..."
result = detector.detect(text=text)
print(f"Watermark detected: {result['prediction']}")
print(f"Z-score: {result['z_score']:.2f}")
print(f"P-value: {result['p_value']:.4f}")
print(f"Green fraction: {result['green_fraction']:.2%}")
Batch Detection
For processing multiple texts efficiently:
from sbw import WatermarkBatch
# Create batch processor
batch = WatermarkBatch(
vocab_size=len(tokenizer),
gamma=0.25,
seeding_scheme="selfhash",
hash_key=15485863,
device=torch.device("cuda"),
)
# Process multiple texts
texts = ["Text 1...", "Text 2...", "Text 3..."]
token_ids = [tokenizer.encode(t, add_special_tokens=False) for t in texts]
results = batch.detect_batch(token_ids)
for i, result in enumerate(results):
print(f"Text {i+1}: z={result['z_score']:.2f}, detected={result['prediction']}")
Parameters
Detection Parameters
| Parameter | Description | Default |
|---|---|---|
gamma |
Fraction of vocabulary in the "green" list | 0.25 |
seeding_scheme |
How to derive PRF seed from context | "selfhash" |
hash_key |
Secret key for watermark | 15485863 |
z_threshold |
Z-score threshold for detection | 4.0 |
Seeding Schemes
- selfhash: Uses hash of current token (recommended for robustness)
- lefthash: Uses hash of previous token
- minhash: Minimum hash over context window
- additive: Sum of hashes over context window
API Reference
WatermarkDetector
Main detection class with text normalization and full detection pipeline.
detector = WatermarkDetector(
device: torch.device,
tokenizer: PreTrainedTokenizer,
vocab: List[int],
gamma: float = 0.25,
seeding_scheme: str = "selfhash",
hash_key: int = 15485863,
z_threshold: float = 4.0,
normalizers: List[str] = ["unicode", "homoglyphs", "truecase"],
)
result = detector.detect(text: str) -> Dict[str, Any]
WatermarkBatch
Low-level batch processor for efficient detection without tokenization overhead.
batch = WatermarkBatch(
vocab_size: int,
gamma: float = 0.25,
seeding_scheme: str = "selfhash",
hash_key: int = 15485863,
device: torch.device = None,
)
results = batch.detect_batch(token_ids: List[List[int]]) -> List[Dict]
Related Projects
- vllm-sbw — vLLM integration for watermark injection
- waterpipe — Evaluation pipeline
- flip-dont-shuffle — Paper repository
- lm-watermarking — KGW watermarking (Kirchenbauer et al.)
Citation
@inproceedings{flip-dont-shuffle-2026,
title={Flip, Don't Shuffle: Watermarking LLMs at the Speed of Inference},
author={Ceppi, Simone and Sanchez, Ignacio},
booktitle={Proceedings of EMNLP 2026},
year={2026}
}
License
EUPL-1.2
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sbw-1.0.0.tar.gz.
File metadata
- Download URL: sbw-1.0.0.tar.gz
- Upload date:
- Size: 111.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4b514405389f73aceac6bfd65f602c8fffcceef9c848b845a32298bbfd25873
|
|
| MD5 |
fb595226b20242add9f6934e57f38c95
|
|
| BLAKE2b-256 |
5e6f02781c364e9e0b1a3f7b0c8adba55c83b7640608993f09c97fc42dced256
|
Provenance
The following attestation bundles were made for sbw-1.0.0.tar.gz:
Publisher:
publish.yml on si-mon-jinn/sbw
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sbw-1.0.0.tar.gz -
Subject digest:
e4b514405389f73aceac6bfd65f602c8fffcceef9c848b845a32298bbfd25873 - Sigstore transparency entry: 2615683448
- Sigstore integration time:
-
Permalink:
si-mon-jinn/sbw@07db6888cd9d928ca47bf16065f8055f641538b4 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/si-mon-jinn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@07db6888cd9d928ca47bf16065f8055f641538b4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sbw-1.0.0-py3-none-any.whl.
File metadata
- Download URL: sbw-1.0.0-py3-none-any.whl
- Upload date:
- Size: 116.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02dd97c0580d70c0b47201de737ffdb81f3d2f60faa0d6bea0695fda373fdb22
|
|
| MD5 |
06f50eb5488bff2342fde30cacc0f2a2
|
|
| BLAKE2b-256 |
58c46cafc9c3d0ab3663406a91fe976a87962e7a3f72e869122a7d7b52091fdb
|
Provenance
The following attestation bundles were made for sbw-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on si-mon-jinn/sbw
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sbw-1.0.0-py3-none-any.whl -
Subject digest:
02dd97c0580d70c0b47201de737ffdb81f3d2f60faa0d6bea0695fda373fdb22 - Sigstore transparency entry: 2615683575
- Sigstore integration time:
-
Permalink:
si-mon-jinn/sbw@07db6888cd9d928ca47bf16065f8055f641538b4 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/si-mon-jinn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@07db6888cd9d928ca47bf16065f8055f641538b4 -
Trigger Event:
release
-
Statement type: