[Model] Extend device-side mm normalization to Qwen3VL/Qwen3.5/Qwen4Next (#51289)

Signed-off-by: cjackal <44624812+cjackal@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
cjackal
2026-09-29 11:44:54 +08:00
committed by GitHub
co-authored by mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 07f1ba2ec6
commit 491f44adfa
11 changed files with 87 additions and 25 deletions
@@ -34,6 +34,7 @@ def vqa_ppl_test(
mm_processor_kwargs = {}
processor = AutoProcessor.from_pretrained(model_info.name)
image_token_id = processor.image_token_id
images = []
prompts = []
@@ -99,8 +100,10 @@ def vqa_ppl_test(
for token_data in token_datas[1:]:
assert token_data is not None
assert len(token_data) == 1
token_log_prob = list(token_data.values())[0].logprob
token_log_probs.append(token_log_prob)
token_id, token_log_prob = next(iter(token_data.items()))
if token_id == image_token_id:
continue
token_log_probs.append(token_log_prob.logprob)
neg_log_likelihood = -torch.tensor(
token_log_probs, dtype=torch.float32, device="cpu"
@@ -132,11 +135,12 @@ def vqa_ppl_test(
inputs = inputs.to("cuda")
input_ids = inputs["input_ids"]
outputs = hf_model.model(**inputs, labels=input_ids)
labels = input_ids.masked_fill(input_ids == image_token_id, -100)
outputs = hf_model.model(**inputs, labels=labels)
neg_log_likelihood = outputs.loss
neg_log_likelihood = neg_log_likelihood.to(torch.float32).cpu()
num_loss_tokens = input_ids.shape[1] - 1
num_loss_tokens = int((labels[:, 1:] != -100).sum())
nll_sum += neg_log_likelihood * num_loss_tokens
n_tokens += num_loss_tokens
@@ -8,26 +8,51 @@ from tests.models.utils import GenerateModelInfo
from .ppl_utils import vqa_ppl_test
MODELS = [
GenerateModelInfo("Qwen/Qwen2-VL-2B-Instruct", hf_ppl=41081356.0),
GenerateModelInfo("Qwen/Qwen2.5-VL-3B-Instruct", hf_ppl=18330016.0),
(
GenerateModelInfo("Qwen/Qwen2-VL-2B-Instruct", hf_ppl=9.810060501098633),
28,
{},
),
(
GenerateModelInfo("Qwen/Qwen2.5-VL-3B-Instruct", hf_ppl=22.17719078063965),
28,
{},
),
(
GenerateModelInfo("Qwen/Qwen3-VL-4B-Instruct", hf_ppl=11.916606903076172),
32,
{},
),
(
GenerateModelInfo("Qwen/Qwen3.5-4B", hf_ppl=6.478141784667969),
32,
{"enable_prefix_caching": False},
),
]
mm_processor_kwargs = {
"min_pixels": 28 * 28,
"max_pixels": 1280 * 28 * 28,
}
def _pixel_factor(factor: int):
"""`factor = patch_size * merge_size` for Qwen models"""
return {"min_pixels": factor**2, "max_pixels": 1280 * factor**2}
@pytest.mark.parametrize("model_info", MODELS)
@pytest.mark.parametrize("model_info,factor,extra_kwargs", MODELS)
@pytest.mark.parametrize("mm_device_do_normalize", [True, False])
def test_ppl(
hf_runner, vllm_runner, model_info: GenerateModelInfo, mm_device_do_normalize: bool
hf_runner,
vllm_runner,
model_info: GenerateModelInfo,
factor: int,
extra_kwargs: dict,
mm_device_do_normalize: bool,
):
vqa_ppl_test(
hf_runner,
vllm_runner,
model_info,
vllm_extra_kwargs={"mm_device_do_normalize": mm_device_do_normalize},
mm_processor_kwargs=mm_processor_kwargs,
vllm_extra_kwargs={
"mm_device_do_normalize": mm_device_do_normalize,
**extra_kwargs,
},
mm_processor_kwargs=_pixel_factor(factor),
)