[Perf] DiffusionGemma: constrained reads over the request's logprob_token_ids (#58216)
Signed-off-by: Matt Mastracci <matthew@mastracci.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Misha Goin <mgoin64@gmail.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
Lucas Wilkinson
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Misha Goin
parent
92e9c6bf13
commit
ab3de6edf2
@@ -145,6 +145,29 @@ def test_diffusion_rejects_bad_extra_args(extra_args: dict, match: str):
|
||||
_verify_diffusion(SamplingParams(extra_args=extra_args))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("flag", [True, 1])
|
||||
def test_diffusion_constrained_needs_logprob_token_ids(flag):
|
||||
bad = SamplingParams(extra_args={"diffusion_constrained": flag})
|
||||
with pytest.raises(VLLMValidationError, match="needs logprob_token_ids"):
|
||||
_verify_diffusion(bad)
|
||||
|
||||
ok = SamplingParams(
|
||||
logprob_token_ids=[3, 5], extra_args={"diffusion_constrained": flag}
|
||||
)
|
||||
_verify_diffusion(ok)
|
||||
|
||||
# An unset or false flag needs no ids.
|
||||
_verify_diffusion(SamplingParams(extra_args={"diffusion_constrained": False}))
|
||||
_verify_diffusion(SamplingParams(extra_args={"diffusion_constrained": 0}))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["yes", 2, [1]])
|
||||
def test_diffusion_constrained_must_be_a_bool(value):
|
||||
params = SamplingParams(extra_args={"diffusion_constrained": value})
|
||||
with pytest.raises(VLLMValidationError, match="must be a boolean"):
|
||||
_verify_diffusion(params)
|
||||
|
||||
|
||||
def test_diffusion_seed_canvas_must_fill_the_canvas():
|
||||
params = SamplingParams(extra_args={"diffusion_seed_canvas": [0] * 7})
|
||||
with pytest.raises(VLLMValidationError, match="exactly 8 ids, got 7"):
|
||||
|
||||
@@ -8,9 +8,11 @@ import pytest
|
||||
import torch
|
||||
|
||||
from vllm.model_executor.models.diffusion_gemma import (
|
||||
_MASKED_LOGIT,
|
||||
DiffusionGemmaRequestStates,
|
||||
_compiled_sample_step,
|
||||
_concat_logprob_stashes,
|
||||
_mask_rows_to_allowed,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.v1.outputs import LogprobsTensors
|
||||
@@ -317,3 +319,50 @@ def test_read_emits_at_convergence_while_generation_waits_for_commit(width, step
|
||||
sampled, counts = _denoise_once(states, [0], width=width)
|
||||
assert counts.tolist() == [width]
|
||||
assert torch.equal(sampled[0], states.argmax_canvas[0, :width].int())
|
||||
|
||||
|
||||
def test_batch_allowed_needs_one_shared_set():
|
||||
states = _states()
|
||||
states.constrained[0] = (1, 2, 3)
|
||||
states.constrained[1] = (4, 5)
|
||||
|
||||
# Mixed sets, or a slot with no set, fall back to per-row masking.
|
||||
assert states.batch_allowed([0, 1]) is None
|
||||
assert states.batch_allowed([0, 2]) is None
|
||||
assert states.batch_allowed([]) is None
|
||||
|
||||
shared = states.batch_allowed([0])
|
||||
assert shared.tolist() == [1, 2, 3]
|
||||
assert shared.dtype == torch.int64
|
||||
assert states.batch_allowed([0, 0]) is shared
|
||||
assert states.allowed_tensor((1, 2, 3)) is shared
|
||||
|
||||
|
||||
def test_mask_rows_to_allowed_masks_only_constrained_rows():
|
||||
logits = torch.randn(5, VOCAB, device="cuda")
|
||||
before = logits.clone()
|
||||
first = torch.tensor([3, 7], device="cuda")
|
||||
third = torch.tensor([0], device="cuda")
|
||||
|
||||
out = _mask_rows_to_allowed(logits, [0, 2, 3], [2, 1, 2], [first, None, third])
|
||||
|
||||
assert out is not logits
|
||||
assert torch.equal(logits, before)
|
||||
# Masked columns hold the finite sentinel, so entropy stays finite.
|
||||
kept = out > _MASKED_LOGIT
|
||||
assert torch.isfinite(out).all()
|
||||
assert kept[0:2].sum(dim=1).tolist() == [2, 2]
|
||||
assert kept[0:2][:, first].all()
|
||||
assert torch.equal(out[0:2][:, first], before[0:2][:, first])
|
||||
assert torch.equal(out[2], before[2])
|
||||
assert kept[3:5].sum(dim=1).tolist() == [1, 1]
|
||||
assert torch.equal(out[3:5][:, third], before[3:5][:, third])
|
||||
# The masked row's softmax is the distribution renormalized over the set.
|
||||
torch.testing.assert_close(
|
||||
out[0].softmax(dim=-1)[first], before[0, first].softmax(dim=-1)
|
||||
)
|
||||
|
||||
|
||||
def test_mask_rows_to_allowed_is_a_no_op_without_constrained_rows():
|
||||
logits = torch.randn(3, VOCAB, device="cuda")
|
||||
assert _mask_rows_to_allowed(logits, [0, 1], [1, 2], [None, None]) is logits
|
||||
|
||||
Reference in New Issue
Block a user