A library for watermarking large language models via a sliding window frequency constraint through constrained decoding.
Project description
Watermarking Language Models via a Frequency Based Constrained Decoding Mechanism
Constrained Decoding has often been used as a technique to ensure robust structured outputs by language models, but in this project we explore its usage in the setting of watermarking language model responses as an inference-time technique. A user can ensure that each sliding window of a given size has a maximum frequency of a certain token adhering to user input. Users can simply create dictionaries of constraints on token frequencies, and language model responses will adhere to these in any given sliding window in the response.
Installation
You can get started with this project by cloning the repository or installing the package via pip.
Clone the Repository
git clone https://github.com/ajuneja23/constrainedFrequencyDecodingWatermark.git
cd constrainedFrequencyDecodingWatermark
Install via pip
pip install constraint-watermark
Usage
After installation, you can use the constrained decoding mechanism to watermark language model responses. Create dictionaries of constraints on token frequencies, and the language model will adhere to these constraints in any given sliding window in the response.
Example
Here is a simple example to demonstrate how to use the constrained watermarking:
from constraint-watermark import ConstrainedDecoder
# Define your constraints
constraints = {
"token1": 2,
"token2": 1,
# Add more tokens and their frequency constraints if needed
}
# Initialize the constrained decoder
decoder = ConstrainedDecoder(
model_name="gpt2", # Replace with your model name or path
constraints_dict=constraints,
device="cpu", # Replace with "cuda" or "mps" if available
window_size=50, # Set the window size if needed
)
# Generate a response with constraints
response = decoder.decode(prompt="Your input text here", max_new_toks=50)
print(response)
Contributing
We welcome contributions to improve this project. Please fork the repository and submit a pull request.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
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 constraint-watermark-0.1.0.tar.gz.
File metadata
- Download URL: constraint-watermark-0.1.0.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3708ecf018c402f8e31ed5892c109cfa0536c281316197e7da92f87df69e350
|
|
| MD5 |
76294ac700ebb85fc81abfb788bcdc24
|
|
| BLAKE2b-256 |
c281737828cf2b6e1525707ff64c9710b844b300239f29b1aeb8bfe085b2ef13
|
File details
Details for the file constraint_watermark-0.1.0-py3-none-any.whl.
File metadata
- Download URL: constraint_watermark-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54466725931c21a451e0c24662cf216a9947924b407b1efb205cd700c79c3905
|
|
| MD5 |
7256dbf2291869f25ad49623ff09ede4
|
|
| BLAKE2b-256 |
9b7932980acaeb304737d130c27219f9cf5dc2a9d53b92514a42d38172a349bf
|