[watermarking] Dual-key gumbel-max watermarking for speculative decoding support (#56122)
Signed-off-by: Raphael Rialland <raphael.rialland@mistral.ai> Signed-off-by: Simon Veitner <sveitner@redhat.com> Signed-off-by: Tomas Ruiz <tomas.ruiz.te@gmail.com> Co-authored-by: OpenAI Codex <codex@openai.com> Co-authored-by: Simon Veitner <sveitner@redhat.com> Co-authored-by: Tomas Ruiz <tomas.ruiz.te@gmail.com>
This commit is contained in:
co-authored by
OpenAI Codex
Simon Veitner
Tomas Ruiz
parent
1ee4be4dbc
commit
7ee8a6dd01
@@ -1,16 +1,18 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from typing import Literal
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.sampling_params import SamplingParams
|
||||
from vllm.v1.sample.ops.topk_topp_sampler import apply_top_k_top_p
|
||||
from vllm.v1.watermarking.watermarker import Watermarker
|
||||
from vllm.v1.watermarking.watermarker import RandomSampler, Watermarker
|
||||
from vllm.v1.worker.gpu.buffer_utils import UvaBackedTensor
|
||||
from vllm.v1.worker.gpu.sample.gumbel import gumbel_sample
|
||||
from vllm.v1.worker.gpu.sample.sampler import Sampler
|
||||
from vllm.v1.worker.gpu.sample.watermark import repeated_context_mask
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -20,10 +22,14 @@ class GPUWatermarkSampler(Sampler):
|
||||
self,
|
||||
watermarker: Watermarker,
|
||||
*args,
|
||||
deduplicate_contexts: Literal["none", "single_turn", "all"] = "single_turn",
|
||||
deduplicate_contexts_max_history: int | None = 8192,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
self.watermarker = watermarker
|
||||
self.deduplicate_contexts = deduplicate_contexts
|
||||
self.deduplicate_contexts_max_history = deduplicate_contexts_max_history
|
||||
self.watermarking = UvaBackedTensor(
|
||||
self.sampling_states.max_num_reqs, dtype=torch.bool
|
||||
)
|
||||
@@ -72,37 +78,38 @@ class GPUWatermarkSampler(Sampler):
|
||||
|
||||
processed_logits = apply_top_k_top_p(processed_logits, top_k, top_p)
|
||||
contexts = self._get_contexts(expanded_idx_mapping)
|
||||
|
||||
def random_sample(sample_logits: torch.Tensor) -> torch.Tensor:
|
||||
return gumbel_sample(
|
||||
sample_logits,
|
||||
expanded_idx_mapping,
|
||||
self.sampling_states.temperature.gpu,
|
||||
self.sampling_states.seeds.gpu,
|
||||
pos,
|
||||
apply_temperature=False,
|
||||
is_drafting=False,
|
||||
use_fp64=self.use_fp64_gumbel,
|
||||
repeated_contexts = None
|
||||
if self.deduplicate_contexts != "none":
|
||||
repeated_contexts = self._get_repeated_contexts(
|
||||
expanded_idx_mapping, contexts
|
||||
)
|
||||
|
||||
temperatures = self.sampling_states.temperature.gpu[expanded_idx_mapping]
|
||||
needs_mixed_sampling = repeated_contexts is not None or not np.all(enabled)
|
||||
skip_mask = None
|
||||
if needs_mixed_sampling:
|
||||
watermarking = self.watermarking.gpu[expanded_idx_mapping] & (
|
||||
temperatures != 0
|
||||
)
|
||||
if repeated_contexts is not None:
|
||||
watermarking &= ~repeated_contexts
|
||||
skip_mask = ~watermarking
|
||||
|
||||
random_sampler = RandomSampler(
|
||||
expanded_idx_mapping=expanded_idx_mapping,
|
||||
temperatures=self.sampling_states.temperature.gpu,
|
||||
seeds=self.sampling_states.seeds.gpu,
|
||||
positions=pos,
|
||||
use_fp64=self.use_fp64_gumbel,
|
||||
)
|
||||
output = self.watermarker.sample(
|
||||
processed_logits,
|
||||
contexts,
|
||||
random_sample,
|
||||
random_sampler,
|
||||
skip_mask=skip_mask,
|
||||
)
|
||||
temperatures = self.sampling_states.temperature.gpu[expanded_idx_mapping]
|
||||
watermarking = self.watermarking.gpu[expanded_idx_mapping] & (temperatures != 0)
|
||||
if not np.all(enabled):
|
||||
unwatermarked = random_sample(processed_logits)
|
||||
sampled = torch.where(watermarking, output.token_ids, unwatermarked)
|
||||
output_logits = output.logits
|
||||
if output.logits is not processed_logits:
|
||||
output_logits = torch.where(
|
||||
watermarking.unsqueeze(-1), output.logits, processed_logits
|
||||
)
|
||||
else:
|
||||
sampled = output.token_ids
|
||||
output_logits = output.logits
|
||||
sampled = output.token_ids
|
||||
output_logits = output.logits
|
||||
sampled = torch.where(
|
||||
temperatures == 0,
|
||||
processed_logits.argmax(dim=-1),
|
||||
@@ -110,22 +117,78 @@ class GPUWatermarkSampler(Sampler):
|
||||
)
|
||||
return sampled, output_logits
|
||||
|
||||
def _get_contexts(self, expanded_idx_mapping: torch.Tensor) -> torch.Tensor:
|
||||
def _get_repeated_contexts(
|
||||
self,
|
||||
expanded_idx_mapping: torch.Tensor,
|
||||
contexts: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
return repeated_context_mask(
|
||||
self.req_states.all_token_ids.gpu,
|
||||
expanded_idx_mapping,
|
||||
self.req_states.prompt_len.gpu,
|
||||
self.req_states.total_len.gpu,
|
||||
contexts,
|
||||
self.deduplicate_contexts_max_history,
|
||||
include_prompt=self.deduplicate_contexts == "all",
|
||||
skip_partial_context=self.deduplicate_contexts == "all",
|
||||
)
|
||||
|
||||
def _get_contexts(
|
||||
self,
|
||||
expanded_idx_mapping: torch.Tensor,
|
||||
expanded_local_pos: torch.Tensor | None = None,
|
||||
draft_sampled: torch.Tensor | None = None,
|
||||
) -> torch.Tensor:
|
||||
"""Build contexts for flattened verification rows.
|
||||
|
||||
With committed tokens ``[10, 11]``, drafts ``[20, 21]``, and width 2,
|
||||
local positions ``[0, 1, 2]`` use contexts ``[10, 11]``, ``[11, 20]``,
|
||||
and ``[20, 21]``. The first verification row precedes the first draft.
|
||||
"""
|
||||
context_width = self.watermarker.context_width
|
||||
req_indices = expanded_idx_mapping.to(torch.int64)
|
||||
valid_reqs = req_indices >= 0
|
||||
safe_req_indices = req_indices.clamp_min(0)
|
||||
total_lens = self.req_states.total_len.gpu[safe_req_indices].to(torch.int64)
|
||||
prompt_lens = self.req_states.prompt_len.gpu[safe_req_indices].to(torch.int64)
|
||||
if expanded_local_pos is None:
|
||||
expanded_local_pos = torch.zeros_like(req_indices)
|
||||
else:
|
||||
expanded_local_pos = expanded_local_pos.to(torch.int64)
|
||||
offsets = torch.arange(
|
||||
-context_width, 0, dtype=torch.int64, device=req_indices.device
|
||||
)
|
||||
positions = total_lens.unsqueeze(-1) + offsets
|
||||
valid_positions = valid_reqs.unsqueeze(-1) & (
|
||||
positions >= prompt_lens.unsqueeze(-1)
|
||||
positions = (
|
||||
total_lens.unsqueeze(-1) + expanded_local_pos.unsqueeze(-1) + offsets
|
||||
)
|
||||
positions = positions.clamp_min(0)
|
||||
contexts = self.req_states.all_token_ids.gpu[
|
||||
safe_req_indices.unsqueeze(-1), positions
|
||||
committed = positions < total_lens.unsqueeze(-1)
|
||||
valid_committed = (
|
||||
valid_reqs.unsqueeze(-1)
|
||||
& committed
|
||||
& (positions >= prompt_lens.unsqueeze(-1))
|
||||
)
|
||||
committed_contexts = self.req_states.all_token_ids.gpu[
|
||||
safe_req_indices.unsqueeze(-1),
|
||||
positions.clamp(0, self.req_states.all_token_ids.gpu.shape[1] - 1),
|
||||
]
|
||||
return torch.where(valid_positions, contexts, -1)
|
||||
contexts = torch.where(valid_committed, committed_contexts, -1)
|
||||
if draft_sampled is None:
|
||||
return contexts
|
||||
|
||||
row_indices = torch.arange(
|
||||
len(req_indices), dtype=torch.int64, device=req_indices.device
|
||||
)
|
||||
draft_offsets = positions - total_lens.unsqueeze(-1)
|
||||
draft_indices = (
|
||||
row_indices.unsqueeze(-1)
|
||||
- expanded_local_pos.unsqueeze(-1)
|
||||
+ draft_offsets
|
||||
+ 1
|
||||
)
|
||||
valid_drafts = (
|
||||
valid_reqs.unsqueeze(-1)
|
||||
& ~committed
|
||||
& (draft_offsets < expanded_local_pos.unsqueeze(-1))
|
||||
)
|
||||
draft_contexts = draft_sampled[draft_indices.clamp(0, len(draft_sampled) - 1)]
|
||||
return torch.where(valid_drafts, draft_contexts, contexts)
|
||||
|
||||
Reference in New Issue
Block a user