Skip to content

Commit

Permalink
Add SynthID (watermerking by Google DeepMind) (#34350)
Browse files Browse the repository at this point in the history
* Add SynthIDTextWatermarkLogitsProcessor

* esolving comments.

* Resolving comments.

* esolving commits,

* Improving SynthIDWatermark tests.

* switch to PT version

* detector as pretrained model + style

* update training + style

* rebase

* Update logits_process.py

* Improving SynthIDWatermark tests.

* Shift detector training to wikitext negatives and stabilize with lower learning rate.

* Clean up.

* in for 7B

* cleanup

* upport python 3.8.

* README and final cleanup.

* HF Hub upload and initiaze.

* Update requirements for synthid_text.

* Adding SynthIDTextWatermarkDetector.

* Detector testing.

* Documentation changes.

* Copyrights fix.

* Fix detector api.

* ironing out errors

* ironing out errors

* training checks

* make fixup and make fix-copies

* docstrings and add to docs

* copyright

* BC

* test docstrings

* move import

* protect type hints

* top level imports

* watermarking example

* direct imports

* tpr fpr meaning

* process_kwargs

* SynthIDTextWatermarkingConfig docstring

* assert -> exception

* example updates

* no immutable dict (cant be serialized)

* pack fn

* einsum equivalent

* import order

* fix test on gpu

* add detector example

---------

Co-authored-by: Sumedh Ghaisas <[email protected]>
Co-authored-by: Marc Sun <[email protected]>
Co-authored-by: sumedhghaisas2 <[email protected]>
Co-authored-by: raushan <[email protected]>
  • Loading branch information
5 people authored Oct 23, 2024
1 parent e50bf61 commit b0f0c61
Show file tree
Hide file tree
Showing 15 changed files with 2,238 additions and 80 deletions.
18 changes: 18 additions & 0 deletions docs/source/en/internal/generation_utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ generation.
[[autodoc]] SuppressTokensLogitsProcessor
- __call__

[[autodoc]] SynthIDTextWatermarkLogitsProcessor
- __call__

[[autodoc]] TemperatureLogitsWarper
- __call__

Expand Down Expand Up @@ -418,5 +421,20 @@ A [`Constraint`] can be used to force the generation to include specific tokens

## Watermark Utils

[[autodoc]] WatermarkingConfig
- __call__

[[autodoc]] WatermarkDetector
- __call__

[[autodoc]] BayesianDetectorConfig
- __call__

[[autodoc]] BayesianDetectorModel
- __call__

[[autodoc]] SynthIDTextWatermarkingConfig
- __call__

[[autodoc]] SynthIDTextWatermarkDetector
- __call__
2 changes: 0 additions & 2 deletions docs/source/en/main_classes/text_generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ like token streaming.
- validate
- get_generation_mode

[[autodoc]] generation.WatermarkingConfig

## GenerationMixin

[[autodoc]] GenerationMixin
Expand Down
34 changes: 34 additions & 0 deletions examples/research_projects/synthid_text/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SynthID Text

This project showcases the use of SynthIDText for watermarking LLMs. The code shown in this repo also
demostrates the training of the detector for detecting such watermarked text. This detector can be uploaded onto
a private HF hub repo (private for security reasons) and can be initialized again through pretrained model loading also shown in this script.

See our blog post: https://huggingface.co/blog/synthid-text


## Python version

User would need python 3.9 to run this example.

## Installation and running

Once you install transformers you would need to install requirements for this project through requirements.txt provided in this folder.

```
pip install -r requirements.txt
```

## To run the detector training

```
python detector_training.py --model_name=google/gemma-7b-it
```

Check the script for more parameters are are tunable and check out paper at link
https://www.nature.com/articles/s41586-024-08025-4 for more information on these parameters.

## Caveat

Make sure to run the training of the detector and the detection on the same hardware
CPU, GPU or TPU to get consistent results (we use detecterministic randomness which is hardware dependent).
Loading

0 comments on commit b0f0c61

Please sign in to comment.