[watermarking] add context deduplication support to speculative decoding (#56807)

Signed-off-by: Raphael Rialland <raphael.rialland@mistral.ai>
Signed-off-by: Simon Veitner <sveitner@redhat.com>
Co-authored-by: Simon Veitner <sveitner@redhat.com>
Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com>
This commit is contained in:
Raphaël Rialland
2026-09-30 12:22:10 -04:00
committed by GitHub
co-authored by Simon Veitner Lucas Wilkinson
parent 73a5831127
commit 0f8b398158
17 changed files with 1379 additions and 88 deletions
@@ -203,6 +203,182 @@ def test_watermarked_recovery_uses_target_key():
assert sampled[0, 0] == expected.token_ids[0]
def test_repeated_draft_context_does_not_change_acceptance():
torch.manual_seed(0)
num_trials = 8
# A draft distribution of its own, so the drafts are not accepted outright.
target_logits = torch.randn(64, device="cuda")
draft_logits = torch.randn(64, device="cuda")
inputs = _build_rejection_sample_inputs(
target_logits,
draft_logits,
num_speculative_steps=1,
temperature=1.0,
num_trials=num_trials,
)
expected, expected_num_sampled = rejection_sample(**inputs, num_speculative_steps=1)
sampled, num_sampled = watermarked_rejection_sample(
**inputs,
num_speculative_steps=1,
contexts=torch.tensor([[10, 11], [11, 12]], device="cuda").repeat(
num_trials, 1
),
watermarking=torch.ones(num_trials, dtype=torch.bool, device="cuda"),
watermarking_skip_mask=torch.tensor([True, False], device="cuda").repeat(
num_trials
),
watermarker=GumbelWatermarker(key=42, context_width=2),
)
# Some drafts must actually be rejected, or the ordinary-residual branch
# the skip mask selects is never reached.
assert (expected_num_sampled == 1).any()
torch.testing.assert_close(num_sampled, expected_num_sampled, rtol=0, atol=0)
torch.testing.assert_close(sampled[:, :1], expected[:, :1], rtol=0, atol=0)
def test_repeated_context_skip_mask_is_read_per_row_in_a_block():
"""A block of three drafts, accepted at row 0 and rejected at row 1.
The skip mask selects the sampler per row, so the row the target recovers
at decides which key draws the recovery token.
"""
num_speculative_steps = 3
target_logits = torch.tensor([float("-inf"), 0.0, 1.0, 2.0], device="cuda")
draft_logits = torch.tensor([30.0, -30.0, -30.0, -30.0], device="cuda")
inputs = _build_rejection_sample_inputs(
target_logits,
draft_logits,
num_speculative_steps=num_speculative_steps,
temperature=1.0,
num_trials=1,
)
# Row 0 drafts a token the target gives 0.67 and the draft e**-60, so it is
# accepted; row 1 drafts a token the target gives no mass, so it is not.
inputs["draft_sampled"][1] = 3
inputs["draft_sampled"][2] = 0
contexts = torch.tensor(
[[10, 11], [11, 3], [3, 0], [0, 1]], dtype=torch.int64, device="cuda"
)
expected, expected_num_sampled = rejection_sample(
**inputs, num_speculative_steps=num_speculative_steps
)
assert expected_num_sampled.item() == 2
assert expected[0, 0].item() == 3
watermarker = GumbelWatermarker(key=42, context_width=2)
skipped, skipped_num_sampled = watermarked_rejection_sample(
**inputs,
num_speculative_steps=num_speculative_steps,
contexts=contexts,
watermarking=torch.tensor([True], device="cuda"),
watermarking_skip_mask=torch.tensor([False, True, False, False], device="cuda"),
watermarker=watermarker,
)
assert skipped_num_sampled.item() == 2
torch.testing.assert_close(skipped[:, :2], expected[:, :2], rtol=0, atol=0)
watermarked, watermarked_num_sampled = watermarked_rejection_sample(
**inputs,
num_speculative_steps=num_speculative_steps,
contexts=contexts,
watermarking=torch.tensor([True], device="cuda"),
watermarking_skip_mask=torch.zeros(
num_speculative_steps + 1, dtype=torch.bool, device="cuda"
),
watermarker=watermarker,
)
# The residual equals the target to within e**-60, so the recovery token is
# the target key's draw on the rejected row's context.
recovery = GumbelWatermarker(key=42, context_width=2).sample(
target_logits.unsqueeze(0), contexts[1:2], lambda _: None
)
assert watermarked_num_sampled.item() == 2
assert watermarked[0, 0].item() == 3
assert watermarked[0, 1] == recovery.token_ids[0]
def test_repeated_recovery_context_uses_ordinary_residual_sample():
target_logits = torch.tensor([float("-inf"), 0.0, 1.0, 2.0], device="cuda")
draft_logits = torch.tensor([100.0, -100.0, -100.0, -100.0], device="cuda")
inputs = _build_rejection_sample_inputs(
target_logits,
draft_logits,
num_speculative_steps=1,
temperature=1.0,
num_trials=1,
)
inputs["draft_sampled"][1] = 0
expected, expected_num_sampled = rejection_sample(**inputs, num_speculative_steps=1)
sampled, num_sampled = watermarked_rejection_sample(
**inputs,
num_speculative_steps=1,
contexts=torch.tensor([[10, 11], [11, 0]], device="cuda"),
watermarking=torch.tensor([True], device="cuda"),
watermarking_skip_mask=torch.tensor([True, False], device="cuda"),
watermarker=GumbelWatermarker(key=42, context_width=2),
)
torch.testing.assert_close(num_sampled, expected_num_sampled, rtol=0, atol=0)
torch.testing.assert_close(sampled[:, :1], expected[:, :1], rtol=0, atol=0)
def test_repeated_bonus_context_uses_ordinary_target_sample():
target_logits = torch.randn(64, device="cuda")
inputs = _build_rejection_sample_inputs(
target_logits,
target_logits,
num_speculative_steps=1,
temperature=1.0,
num_trials=1,
)
expected, expected_num_sampled = rejection_sample(**inputs, num_speculative_steps=1)
sampled, num_sampled = watermarked_rejection_sample(
**inputs,
num_speculative_steps=1,
contexts=torch.tensor([[10, 11], [11, 12]], device="cuda"),
watermarking=torch.tensor([True], device="cuda"),
watermarking_skip_mask=torch.tensor([False, True], device="cuda"),
watermarker=GumbelWatermarker(key=42, context_width=2),
)
torch.testing.assert_close(num_sampled, expected_num_sampled, rtol=0, atol=0)
torch.testing.assert_close(sampled[:, :2], expected[:, :2], rtol=0, atol=0)
def test_repeated_context_does_not_change_unwatermarked_or_greedy_rows():
target_logits = torch.randn(64, device="cuda")
inputs = _build_rejection_sample_inputs(
target_logits,
target_logits,
num_speculative_steps=1,
temperature=1.0,
num_trials=2,
)
inputs["temperature"][1] = 0
expected, expected_num_sampled = rejection_sample(**inputs, num_speculative_steps=1)
sampled, num_sampled = watermarked_rejection_sample(
**inputs,
num_speculative_steps=1,
contexts=torch.tensor([[10, 11], [11, 12], [20, 21], [21, 22]], device="cuda"),
watermarking=torch.tensor([False, True], device="cuda"),
watermarking_skip_mask=torch.ones(4, dtype=torch.bool, device="cuda"),
watermarker=GumbelWatermarker(key=42, context_width=2),
)
torch.testing.assert_close(num_sampled, expected_num_sampled, rtol=0, atol=0)
for row, length in enumerate(num_sampled):
torch.testing.assert_close(
sampled[row, : int(length)], expected[row, : int(length)], rtol=0, atol=0
)
def test_watermarked_recovery_supports_smaller_draft_vocabulary():
target_logits = torch.tensor([float("-inf"), 0.0, 1.0, 2.0, 100.0], device="cuda")
draft_logits = torch.tensor([100.0, -100.0, -100.0, -100.0], device="cuda")