[feature] Watermarked generation and detection (Gumbel-max algorithm) (#54053)

Signed-off-by: Raphael Rialland <raphael.rialland@mistral.ai>
Co-authored-by: OpenAI Codex <codex@openai.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Raphaël Rialland
2026-09-09 21:00:15 -07:00
committed by GitHub
co-authored by OpenAI Codex mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 8c34a37232
commit ea40bb9e90
32 changed files with 1693 additions and 15 deletions
+2
View File
@@ -60,6 +60,7 @@ from vllm.config.vllm import (
get_layers_from_vllm_config,
set_current_vllm_config,
)
from vllm.config.watermarking import WatermarkConfig
from vllm.config.weight_transfer import WeightTransferConfig
# __all__ should only contain classes and functions.
@@ -146,4 +147,5 @@ __all__ = [
"set_current_vllm_config",
"get_layers_from_vllm_config",
"WeightTransferConfig",
"WatermarkConfig",
]
+36
View File
@@ -51,6 +51,7 @@ from .scheduler import SchedulerConfig
from .speculative import EagleModelTypes, NgramGPUTypes, SpeculativeConfig
from .structured_outputs import StructuredOutputsConfig
from .utils import SupportsHash, config, replace
from .watermarking import WatermarkConfig
from .weight_transfer import WeightTransferConfig
if TYPE_CHECKING:
@@ -381,6 +382,8 @@ class VllmConfig:
"""LoRA configuration."""
speculative_config: SpeculativeConfig | None = None
"""Speculative decoding configuration."""
watermark_config: WatermarkConfig | None = None
"""Text watermarking configuration."""
diffusion_config: DiffusionConfig | None = None
"""Diffusion LLM (dLLM) configuration."""
@@ -662,6 +665,14 @@ class VllmConfig:
@property
def use_v2_model_runner(self) -> bool:
if getattr(self, "watermark_config", None) is not None:
if envs.VLLM_USE_V2_MODEL_RUNNER is False:
logger.info_once(
"Watermarking requires Model Runner V2 and overrides "
"VLLM_USE_V2_MODEL_RUNNER=0."
)
return True
use_v2_model_runner = envs.VLLM_USE_V2_MODEL_RUNNER
if use_v2_model_runner is not None:
return use_v2_model_runner
@@ -1091,6 +1102,30 @@ class VllmConfig:
if not self.use_v2_model_runner:
raise ValueError("trace replay requires Model Runner V2")
def _check_watermarking_unsupported(
self,
*,
beam_search: bool = False,
custom_sampler: bool = False,
) -> None:
watermark_config = getattr(self, "watermark_config", None)
if watermark_config is None:
return
if (
self.speculative_config is not None
and not watermark_config.supports_speculative_decoding
):
raise ValueError(
f"The {watermark_config.algorithm} watermarking algorithm "
"does not support speculative decoding."
)
if beam_search:
raise ValueError("Beam search is not supported with watermarking.")
if custom_sampler:
raise ValueError(
"Model-specific custom samplers are not supported with watermarking."
)
def _resolve_and_verify_engram_config(self) -> None:
"""Resolve legacy offload settings and validate model and parallel configs."""
if self.engram_config is None:
@@ -1124,6 +1159,7 @@ class VllmConfig:
self.try_verify_and_update_config()
self._resolve_and_verify_engram_config()
self._check_watermarking_unsupported()
# Models may have supplied their own DCP defaults above; anything still
# unset falls back to the stock ones.
self.parallel_config.set_dcp_defaults()
+49
View File
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import Literal
from pydantic import Field, model_validator
from typing_extensions import Self
from vllm.config.utils import config
from vllm.logger import init_logger
logger = init_logger(__name__)
WatermarkingAlgorithm = Literal["gumbel"]
WatermarkPRFName = Literal["philox"]
_SPECULATIVE_DECODING_SUPPORT: dict[WatermarkingAlgorithm, bool] = {
"gumbel": False,
}
@config
class WatermarkConfig:
"""Configuration for text watermark generation."""
key: int = Field(ge=0, repr=False, exclude=True)
"""Secret key used to watermark generated text."""
algorithm: WatermarkingAlgorithm = "gumbel"
"""Algorithm used to watermark generated text."""
context_width: int = Field(default=4, ge=1)
"""Number of prior output tokens used by the watermark PRF."""
prf: WatermarkPRFName = "philox"
"""Pseudorandom function used by the watermarking algorithm."""
@model_validator(mode="after")
def validate_key(self) -> Self:
if self.key > 2**64 - 1:
raise ValueError("philox keys must fit in 64 bits")
if self.algorithm == "gumbel":
logger.warning_once(
"Single-key Gumbel-max watermarking may increase the frequency of "
"degenerate generations, including repetition loops.",
scope="global",
)
return self
@property
def supports_speculative_decoding(self) -> bool:
return _SPECULATIVE_DECODING_SUPPORT[self.algorithm]