Reference implementation of the CHORD method. This repository contains the core method modules only — enough to understand and build on the approach.
CHORD watermarks autoregressive (AR) image generators at the token
level without touching the generator's weights. At every decoding step
t, a lightweight signal network T maps a dual-stream context —
the L2-normalized mean codebook embedding of the previous chunk of
C tokens, concatenated with the embedding of the immediately
preceding token — together with the position index to a soft,
vocabulary-sized signal w_t = tanh(k2 · T_raw) ∈ (-1, 1)^V. The raw
logits are tilted by δ · w_t before sampling. Verification re-encodes
the suspect image with the shared VQ tokenizer, deterministically
replays the same signal from the observed token stream, and scores the
sequence by the mean signal value at the observed tokens. Because the
conditioning is a smoothed, chunk-level function of the content
itself, the signal survives value-preserving edits and re-tokenization
drift while remaining image-adaptive rather than static.
chord/
├── network.py # ChordSignalNet: the signal network T (residual MLP)
├── context.py # chunk / local context construction, keyed start embedding
├── generate.py # ChordBiaser: per-step logit bias for a frozen AR generator
├── verify.py # ChordDetector: re-tokenize, replay, score; TPR@FPR helper
├── keys.py # multi-key support via keyed vocabulary permutation
└── losses.py # training objectives (Eq. 5): L_wm, L_sm, L_pc, L_cal
import torch
from chord import ChordSignalNet, ChordBiaser, ChordDetector, key_permutation
V, d, N = 16384, 8, 256 # codebook size, embedding dim, sequence length
codebook = vq_model.quantize.embedding.weight.data # (V, d), shared tokenizer
T = ChordSignalNet(codebook_dim=d, vocab_size=V, max_positions=N, key=1234)
T.load_state_dict(torch.load("T.pt")) # trained signal network
perm = key_permutation(key=1234, vocab_size=V)
# --- generation: add delta * w_t to the raw logits at every step -------------
biaser = ChordBiaser(T, codebook, delta=4.0, chunk_size=32, key_perm=perm)
for t in range(N):
logits = ar_generator_step(...) # (B, V) raw logits from the frozen AR model
logits = logits + biaser.bias(t)
z_t = sample(logits) # top-k / top-p / temperature — unchanged
biaser.append(z_t)
# --- verification ------------------------------------------------------------
detector = ChordDetector(T, codebook, chunk_size=32, key_perm=perm)
scores = detector.score_tokens(vq_encode(suspect_images)) # (B,) mean signalT depends only on the tokenizer's codebook, not on the generator's
parameter count, so the same network transfers across generator scales
that share a tokenizer.
losses.pydefines the training objectives; the end-to-end training loop (frozen-generator rollout and differentiable re-tokenization) is not included in this release.- No pretrained checkpoints, datasets, or evaluation/attack scripts are included, so paper experiments are not directly reproducible from this repository alone.
- Python ≥ 3.9, PyTorch ≥ 2.0
MIT — see LICENSE.