[Feature][Hunyuan image 3.0] AR + DIT with kv reuse. (#3346)
Signed-off-by: dengyunyang <584797741@qq.com>
This commit is contained in:
1 parent
ce8a7dfd2d
commit
5b61e7f1f1
8 files changed
+1198
-135
No files matched your search
@@ -0,0 +1,440 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Unit tests for ImageKVCacheManager.
|
||||
|
||||
Covers: cache → reuse flow, AR KV injection, CFG (sequential & parallel), SP, cross-request isolation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from contextlib import ExitStack, contextmanager
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
pytestmark = [pytest.mark.core_model, pytest.mark.cpu]
|
||||
|
||||
_TRANSFORMER_MODULE = "vllm_omni.diffusion.models.hunyuan_image3.hunyuan_image3_transformer"
|
||||
|
||||
NUM_HEADS = 4
|
||||
NUM_KV_HEADS = 2
|
||||
HEAD_DIM = 16
|
||||
IMAGE_TOKEN_LEN = 8
|
||||
SCALING = 1.0 / math.sqrt(HEAD_DIM)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Mocks + helpers
|
||||
# ============================================================
|
||||
|
||||
|
||||
class MockAttention(nn.Module):
|
||||
def __init__(self, num_heads, head_size, causal=False, softmax_scale=None, num_kv_heads=None, **kwargs):
|
||||
super().__init__()
|
||||
|
||||
def forward(self, query, key, value, attn_metadata=None, **kwargs):
|
||||
return query
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patched_mgr_env(sp_size=1):
|
||||
target = _TRANSFORMER_MODULE
|
||||
patches = [
|
||||
patch(f"{target}.get_sequence_parallel_world_size", return_value=sp_size),
|
||||
patch(f"{target}.get_sequence_parallel_rank", return_value=0),
|
||||
patch(f"{target}.Attention", MockAttention),
|
||||
]
|
||||
with ExitStack() as stack:
|
||||
for p in patches:
|
||||
stack.enter_context(p)
|
||||
yield
|
||||
|
||||
|
||||
def _make_cache_mgr(image_token_len=IMAGE_TOKEN_LEN, sp_size=1):
|
||||
with patched_mgr_env(sp_size=sp_size):
|
||||
from vllm_omni.diffusion.models.hunyuan_image3.hunyuan_image3_transformer import (
|
||||
ImageKVCacheManager,
|
||||
)
|
||||
|
||||
mgr = ImageKVCacheManager(
|
||||
num_heads=NUM_HEADS,
|
||||
num_kv_heads=NUM_KV_HEADS,
|
||||
head_dim=HEAD_DIM,
|
||||
scaling=SCALING,
|
||||
image_token_len=image_token_len,
|
||||
)
|
||||
return mgr
|
||||
|
||||
|
||||
def _make_known_kv(num_tokens, base=0.0):
|
||||
"""Create key/value with known values. Token i has all elements = base+i / base+i+0.5."""
|
||||
k = torch.full((num_tokens, NUM_KV_HEADS, HEAD_DIM), 0.0)
|
||||
v = torch.full((num_tokens, NUM_KV_HEADS, HEAD_DIM), 0.0)
|
||||
for i in range(num_tokens):
|
||||
k[i] = base + i
|
||||
v[i] = base + i + 0.5
|
||||
return k, v
|
||||
|
||||
|
||||
def _call_mgr(
|
||||
mgr,
|
||||
bs,
|
||||
q_len,
|
||||
seq_len,
|
||||
key_flat,
|
||||
value_flat,
|
||||
first_step=False,
|
||||
uncond_cfg_prefill=False,
|
||||
num_image_tokens=IMAGE_TOKEN_LEN,
|
||||
shard_image_size=None,
|
||||
):
|
||||
query = torch.randn(bs * q_len, NUM_HEADS, HEAD_DIM)
|
||||
attn_mask = torch.zeros(bs, 1, seq_len, seq_len)
|
||||
mgr(
|
||||
query,
|
||||
key_flat,
|
||||
value_flat,
|
||||
attn_mask,
|
||||
query_lens=[q_len] * bs,
|
||||
seq_lens=[seq_len] * bs,
|
||||
first_step=first_step,
|
||||
uncond_cfg_prefill=uncond_cfg_prefill,
|
||||
num_image_tokens=num_image_tokens,
|
||||
shard_image_size=shard_image_size,
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 1: No AR KV — basic cache → reuse
|
||||
# ============================================================
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bs", [1, 2])
|
||||
def test_no_ar_kv(bs):
|
||||
"""
|
||||
No AR KV injected. Tests the basic first_step cache → update reuse path.
|
||||
|
||||
Sequence layout per batch on first_step (q_len=12, IMAGE_TOKEN_LEN=8):
|
||||
[prompt(3) | image(8) | eoi(1)]
|
||||
image_size = IMAGE_TOKEN_LEN + 1 = 9
|
||||
cached_prompt_len = seq_len - image_size = 12 - 9 = 3
|
||||
|
||||
After first_step:
|
||||
image_kv_cache_map stores prompt tokens [0:3] for each batch (total 3*bs tokens flat).
|
||||
|
||||
Update step (q_len=IMAGE_TOKEN_LEN=8, seq_len = cached_prompt(3) + eoi(1) + image(8) = 12):
|
||||
_reuse_prompt_kv produces [cached_prompt(3) | new_image(8) | zero_eoi(1)] per batch.
|
||||
"""
|
||||
mgr = _make_cache_mgr()
|
||||
assert mgr.image_kv_cache_map is None
|
||||
|
||||
# --- first_step ---
|
||||
q_len = 12
|
||||
k_flat, v_flat = _make_known_kv(bs * q_len, base=1.0)
|
||||
|
||||
_call_mgr(mgr, bs, q_len=q_len, seq_len=q_len, key_flat=k_flat, value_flat=v_flat, first_step=True)
|
||||
|
||||
cached_key, cached_value = mgr.image_kv_cache_map
|
||||
# 3 prompt tokens cached per batch
|
||||
assert cached_key.shape[0] == 3 * bs
|
||||
for b in range(bs):
|
||||
flat_offset = b * q_len
|
||||
cache_offset = b * 3
|
||||
assert torch.allclose(cached_key[cache_offset : cache_offset + 3], k_flat[flat_offset : flat_offset + 3])
|
||||
assert torch.allclose(cached_value[cache_offset : cache_offset + 3], v_flat[flat_offset : flat_offset + 3])
|
||||
|
||||
# --- update step ---
|
||||
img_q_len = IMAGE_TOKEN_LEN
|
||||
update_seq_len = 3 + 1 + img_q_len # cached_prompt(3) + eoi(1) + image(8) = 12
|
||||
new_img_k, new_img_v = _make_known_kv(bs * img_q_len, base=50.0)
|
||||
|
||||
key_input = new_img_k.reshape(bs, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
val_input = new_img_v.reshape(bs, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
result_k, result_v = mgr._reuse_prompt_kv(key_input, val_input, update_seq_len, bs=bs)
|
||||
|
||||
assert result_k.shape == (bs, update_seq_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
for b in range(bs):
|
||||
cache_offset = b * 3
|
||||
img_offset = b * img_q_len
|
||||
# Cached prompt preserved
|
||||
assert torch.allclose(result_k[b, :3], cached_key[cache_offset : cache_offset + 3])
|
||||
# New image tokens
|
||||
assert torch.allclose(result_k[b, 3 : 3 + img_q_len], new_img_k[img_offset : img_offset + img_q_len])
|
||||
# Zero eoi at the end
|
||||
assert torch.allclose(result_k[b, -1], torch.zeros(NUM_KV_HEADS, HEAD_DIM))
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 2: AR KV, no CFG
|
||||
# ============================================================
|
||||
|
||||
|
||||
@pytest.mark.parametrize("sp_size", [1, 2])
|
||||
def test_ar_kv_no_cfg(sp_size):
|
||||
"""
|
||||
AR KV injected, bs=1, no CFG. Tests AR prefix prepend + cache + reuse.
|
||||
|
||||
sp_size=1:
|
||||
first_step input: q_len=12, ar_len=5, seq_len=17
|
||||
Sequence layout: [ar(5) | prompt(3) | image(8) | eoi(1)]
|
||||
image_size = IMAGE_TOKEN_LEN + 1 = 9
|
||||
cached_prompt_len = seq_len - image_size = 17 - 9 = 8 (= ar(5) + prompt(3))
|
||||
|
||||
Update step: q_len=8, seq_len = cached_prompt(8) + eoi(1) + image(8) = 17
|
||||
Result: [cached(8) | new_image(8) | zero_eoi(1)]
|
||||
|
||||
sp_size=2:
|
||||
shard_image_size = 4 (passed externally, simulating SP sharding)
|
||||
cached_prompt_len = seq_len - shard_image_size = 17 - 4 = 13 (= ar(5) + prompt(8))
|
||||
Note: with SP, more of the sequence is treated as "prompt" because
|
||||
image tokens are sharded across ranks.
|
||||
|
||||
Update step: q_len=4 (shard_image_size), seq_len = cached_prompt(13) + shard_image(4) = 17
|
||||
SP path returns only cached prompt [bs, cached_prompt_len, ...] (no eoi concat).
|
||||
|
||||
Both sp_size cases test _cache_prompt_kv and _reuse_prompt_kv directly to avoid
|
||||
needing full SP infrastructure mocks in __call__.
|
||||
"""
|
||||
mgr = _make_cache_mgr(sp_size=sp_size)
|
||||
ar_len = 5
|
||||
ar_k, ar_v = _make_known_kv(ar_len, base=100.0)
|
||||
mgr._injected_ar_kv = [(ar_k.clone(), ar_v.clone())]
|
||||
|
||||
# --- first_step: call _cache_prompt_kv directly ---
|
||||
bs, q_len = 1, 12
|
||||
seq_len = q_len + ar_len # 17
|
||||
k_raw, v_raw = _make_known_kv(q_len, base=1.0)
|
||||
key_4d = k_raw.reshape(1, q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
val_4d = v_raw.reshape(1, q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
|
||||
shard_image_size = 4 if sp_size > 1 else None
|
||||
mgr.image_kv_cache_map = None
|
||||
mgr._cache_prompt_kv(key_4d, val_4d, seq_len, shard_image_size)
|
||||
|
||||
cached_key, cached_value = mgr.image_kv_cache_map
|
||||
if sp_size == 1:
|
||||
# cached = ar(5) + prompt(3) = 8
|
||||
expected_cached_len = 8
|
||||
else:
|
||||
# cached = seq_len - shard_image_size = 17 - 4 = 13
|
||||
expected_cached_len = seq_len - shard_image_size
|
||||
|
||||
assert cached_key.shape[0] == expected_cached_len
|
||||
# AR KV always at the front
|
||||
assert torch.allclose(cached_key[:ar_len], ar_k)
|
||||
assert torch.allclose(cached_value[:ar_len], ar_v)
|
||||
# Prompt tokens follow AR
|
||||
prompt_cached = expected_cached_len - ar_len
|
||||
assert torch.allclose(cached_key[ar_len : ar_len + prompt_cached], k_raw[:prompt_cached])
|
||||
# AR KV consumed
|
||||
assert mgr._injected_ar_kv is None
|
||||
|
||||
# --- update step: call _reuse_prompt_kv directly ---
|
||||
if sp_size == 1:
|
||||
img_q_len = IMAGE_TOKEN_LEN
|
||||
update_seq_len = expected_cached_len + 1 + img_q_len # 8 + 1 + 8 = 17
|
||||
new_img_k, new_img_v = _make_known_kv(img_q_len, base=50.0)
|
||||
|
||||
key_input = new_img_k.reshape(1, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
val_input = new_img_v.reshape(1, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
result_k, result_v = mgr._reuse_prompt_kv(key_input, val_input, update_seq_len, bs=1)
|
||||
|
||||
assert result_k.shape == (1, update_seq_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
# AR + prompt preserved
|
||||
assert torch.allclose(result_k[0, :ar_len], ar_k)
|
||||
assert torch.allclose(result_k[0, ar_len : ar_len + prompt_cached], k_raw[:prompt_cached])
|
||||
# New image tokens
|
||||
assert torch.allclose(result_k[0, expected_cached_len : expected_cached_len + img_q_len], new_img_k)
|
||||
# Zero eoi
|
||||
assert torch.allclose(result_k[0, -1], torch.zeros(NUM_KV_HEADS, HEAD_DIM))
|
||||
else:
|
||||
# SP path: _reuse_prompt_kv returns only cached prompt (no image concat)
|
||||
img_q_len = shard_image_size # 4
|
||||
update_seq_len = expected_cached_len + img_q_len # 13 + 4 = 17
|
||||
new_img_k, new_img_v = _make_known_kv(img_q_len, base=50.0)
|
||||
|
||||
key_input = new_img_k.reshape(1, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
val_input = new_img_v.reshape(1, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
result_k, result_v = mgr._reuse_prompt_kv(
|
||||
key_input,
|
||||
val_input,
|
||||
update_seq_len,
|
||||
bs=bs,
|
||||
shard_image_size=img_q_len,
|
||||
)
|
||||
|
||||
# SP returns only the cached prompt portion
|
||||
assert result_k.shape == (1, expected_cached_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
assert torch.allclose(result_k[0, :ar_len], ar_k)
|
||||
assert torch.allclose(result_k[0, ar_len : ar_len + prompt_cached], k_raw[:prompt_cached])
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 3: AR KV + CFG (sequential & parallel)
|
||||
# ============================================================
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cfg_parallel,bs", [(False, 2), (True, 1)])
|
||||
def test_ar_kv_with_cfg(cfg_parallel, bs):
|
||||
"""
|
||||
AR KV + CFG. Tests uncond_cfg_prefill → first_step → update.
|
||||
|
||||
Common setup:
|
||||
positive_reuse_len = 10, negative_reuse_len = 6, neg_uncond_cfg_q_len = 4
|
||||
AR KV: 10 tokens (base=100)
|
||||
|
||||
Sequential CFG (cfg_parallel=False, bs=2):
|
||||
uncond_cfg_prefill (bs=1):
|
||||
Builds neg AR KV = [shared_prefix(6) from pos_ar | neg_prefill_tokens(4)]
|
||||
→ _injected_ar_kv becomes [(pos_ar(10), pos_av(10)), (neg_k(10), neg_v(10))]
|
||||
|
||||
first_step (bs=2, q_len=12, seq_len=22):
|
||||
Batch 0 (pos): [pos_ar(10) | prompt(3) | image(8) | eoi(1)]
|
||||
Batch 1 (neg): [neg_ar(10) | prompt(3) | image(8) | eoi(1)]
|
||||
cached_prompt_len per batch = 22 - 9 = 13
|
||||
Total cached = 13 * 2 = 26
|
||||
|
||||
Update (bs=2, seq_len = 13 + 1 + 8 = 22):
|
||||
Result per batch: [cached(13) | new_image(8) | zero_eoi(1)]
|
||||
|
||||
CFG Parallel (cfg_parallel=True, bs=1):
|
||||
This rank handles only the negative branch.
|
||||
uncond_cfg_prefill (bs=1):
|
||||
Same as above: builds neg AR KV.
|
||||
Then we keep only the negative entry (simulating _keep_negative_kv_only).
|
||||
→ _injected_ar_kv = [(neg_k(10), neg_v(10))]
|
||||
|
||||
first_step (bs=1, q_len=12, seq_len=22):
|
||||
[neg_ar(10) | prompt(3) | image(8) | eoi(1)]
|
||||
cached_prompt_len = 22 - 9 = 13
|
||||
|
||||
Update (bs=1, seq_len = 13 + 1 + 8 = 22):
|
||||
Result: [cached(13) | new_image(8) | zero_eoi(1)]
|
||||
"""
|
||||
positive_reuse_len = 10
|
||||
negative_reuse_len = 6
|
||||
neg_uncond_cfg_q_len = positive_reuse_len - negative_reuse_len # 4
|
||||
|
||||
mgr = _make_cache_mgr()
|
||||
pos_ar_k, pos_ar_v = _make_known_kv(positive_reuse_len, base=100.0)
|
||||
mgr._injected_ar_kv = [(pos_ar_k.clone(), pos_ar_v.clone())]
|
||||
|
||||
# --- uncond_cfg_prefill ---
|
||||
neg_k, neg_v = _make_known_kv(neg_uncond_cfg_q_len, base=200.0)
|
||||
prefill_seq_len = negative_reuse_len + neg_uncond_cfg_q_len # 6 + 4 = 10
|
||||
|
||||
_call_mgr(
|
||||
mgr,
|
||||
bs=1,
|
||||
q_len=neg_uncond_cfg_q_len,
|
||||
seq_len=prefill_seq_len,
|
||||
key_flat=neg_k,
|
||||
value_flat=neg_v,
|
||||
first_step=True,
|
||||
uncond_cfg_prefill=True,
|
||||
num_image_tokens=0,
|
||||
)
|
||||
|
||||
# After prefill: _injected_ar_kv = [(pos), (neg)]
|
||||
assert len(mgr._injected_ar_kv) == 2
|
||||
neg_ar_k, neg_ar_v = mgr._injected_ar_kv[1]
|
||||
# neg AR KV = [shared_prefix(6) from pos | neg_prefill(4)], total 10
|
||||
assert neg_ar_k.shape[0] == positive_reuse_len
|
||||
assert torch.allclose(neg_ar_k[:negative_reuse_len], pos_ar_k[:negative_reuse_len])
|
||||
assert torch.allclose(neg_ar_k[negative_reuse_len:], neg_k)
|
||||
|
||||
# --- simulate cfg_parallel: keep only negative ---
|
||||
if cfg_parallel:
|
||||
mgr._injected_ar_kv = [mgr._injected_ar_kv[1]]
|
||||
|
||||
# --- first_step ---
|
||||
q_len = 12
|
||||
seq_len = q_len + positive_reuse_len # 22
|
||||
k_flat, v_flat = _make_known_kv(bs * q_len, base=1.0)
|
||||
|
||||
_call_mgr(mgr, bs, q_len=q_len, seq_len=seq_len, key_flat=k_flat, value_flat=v_flat, first_step=True)
|
||||
|
||||
cached_key, cached_value = mgr.image_kv_cache_map
|
||||
cached_prompt_len_per_batch = seq_len - (IMAGE_TOKEN_LEN + 1) # 22 - 9 = 13
|
||||
assert cached_key.shape[0] == cached_prompt_len_per_batch * bs
|
||||
assert mgr._injected_ar_kv is None
|
||||
|
||||
if not cfg_parallel:
|
||||
# bs=2: batch 0 = pos, batch 1 = neg
|
||||
# Batch 0: pos_ar(10) + prompt(3)
|
||||
assert torch.allclose(cached_key[:positive_reuse_len], pos_ar_k)
|
||||
assert torch.allclose(cached_key[positive_reuse_len:13], k_flat[:3])
|
||||
# Batch 1: neg_ar(10) + prompt(3)
|
||||
assert torch.allclose(cached_key[13:23], neg_ar_k)
|
||||
assert torch.allclose(cached_key[23:26], k_flat[q_len : q_len + 3])
|
||||
else:
|
||||
# bs=1: only neg branch
|
||||
assert torch.allclose(cached_key[:positive_reuse_len], neg_ar_k)
|
||||
assert torch.allclose(cached_key[positive_reuse_len:13], k_flat[:3])
|
||||
|
||||
# --- update step ---
|
||||
img_q_len = IMAGE_TOKEN_LEN
|
||||
update_seq_len = cached_prompt_len_per_batch + 1 + img_q_len # 13 + 1 + 8 = 22
|
||||
new_img_k, new_img_v = _make_known_kv(bs * img_q_len, base=50.0)
|
||||
|
||||
key_input = new_img_k.reshape(bs, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
val_input = new_img_v.reshape(bs, img_q_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
result_k, result_v = mgr._reuse_prompt_kv(key_input, val_input, update_seq_len, bs=bs)
|
||||
|
||||
assert result_k.shape == (bs, update_seq_len, NUM_KV_HEADS, HEAD_DIM)
|
||||
for b in range(bs):
|
||||
cache_offset = b * cached_prompt_len_per_batch
|
||||
# Cached prompt preserved
|
||||
assert torch.allclose(
|
||||
result_k[b, :cached_prompt_len_per_batch],
|
||||
cached_key[cache_offset : cache_offset + cached_prompt_len_per_batch],
|
||||
)
|
||||
# New image tokens
|
||||
img_offset = b * img_q_len
|
||||
assert torch.allclose(
|
||||
result_k[b, cached_prompt_len_per_batch : cached_prompt_len_per_batch + img_q_len],
|
||||
new_img_k[img_offset : img_offset + img_q_len],
|
||||
)
|
||||
# Zero eoi
|
||||
assert torch.allclose(result_k[b, -1], torch.zeros(NUM_KV_HEADS, HEAD_DIM))
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 4: Cross-request isolation
|
||||
# ============================================================
|
||||
|
||||
|
||||
def test_cross_request_isolation():
|
||||
"""
|
||||
Verify leftover image_kv_cache_map from a previous request is NOT treated as AR KV.
|
||||
|
||||
Setup: mgr has stale cache from a prior request (9 tokens, base=999).
|
||||
New request: first_step with q_len=12, no AR KV.
|
||||
|
||||
Expected: stale cache is overwritten. New cache = prompt tokens from current request.
|
||||
The stale values (999.x) must NOT appear in the new cache.
|
||||
"""
|
||||
mgr = _make_cache_mgr()
|
||||
|
||||
# Simulate leftover from previous request
|
||||
leftover_k, leftover_v = _make_known_kv(9, base=999.0)
|
||||
mgr.image_kv_cache_map = (leftover_k, leftover_v)
|
||||
assert mgr._injected_ar_kv is None
|
||||
|
||||
# New request first_step
|
||||
bs, seq_len = 1, 12
|
||||
k_flat, v_flat = _make_known_kv(bs * seq_len, base=1.0)
|
||||
|
||||
_call_mgr(mgr, bs, q_len=seq_len, seq_len=seq_len, key_flat=k_flat, value_flat=v_flat, first_step=True)
|
||||
|
||||
cached_key, cached_value = mgr.image_kv_cache_map
|
||||
# cached_prompt_len = 12 - 9 = 3
|
||||
assert cached_key.shape[0] == 3
|
||||
# Must be from current request, not stale
|
||||
assert torch.allclose(cached_key[:3], k_flat[:3])
|
||||
assert torch.allclose(cached_value[:3], v_flat[:3])
|
||||
# Stale values must not be present
|
||||
assert not torch.any(cached_key >= 999.0)
|
||||
@@ -142,6 +142,40 @@ def compute_image_ssim_psnr(
|
||||
return ssim_value, psnr_value
|
||||
|
||||
|
||||
class CLIPScorer:
|
||||
def __init__(self, model_name_or_path: str = "openai/clip-vit-base-patch16"):
|
||||
from transformers import CLIPModel, CLIPProcessor
|
||||
|
||||
self._model = CLIPModel.from_pretrained(model_name_or_path)
|
||||
self._processor = CLIPProcessor.from_pretrained(model_name_or_path)
|
||||
self._model.eval()
|
||||
|
||||
def score(self, image: Image.Image, text: str) -> float:
|
||||
inputs = self._processor(text=[text], images=[image], return_tensors="pt", padding=True)
|
||||
with torch.no_grad():
|
||||
outputs = self._model(**inputs)
|
||||
img_emb = outputs.image_embeds
|
||||
txt_emb = outputs.text_embeds
|
||||
img_emb = img_emb / img_emb.norm(p=2, dim=-1, keepdim=True)
|
||||
txt_emb = txt_emb / txt_emb.norm(p=2, dim=-1, keepdim=True)
|
||||
similarity = (img_emb * txt_emb).sum(dim=-1)
|
||||
return float(similarity.item() * 100)
|
||||
|
||||
def assert_score(self, *, model_name: str, image: Image.Image, text: str, threshold: float) -> None:
|
||||
value = self.score(image, text)
|
||||
print(f"{model_name} CLIP score:")
|
||||
print(f" CLIPScore: value={value:.4f}, threshold>={threshold:.4f}, higher_is_better=True")
|
||||
assert value >= threshold, (
|
||||
f"CLIP score below threshold for {model_name}: got {value:.4f}, expected >= {threshold:.4f}."
|
||||
)
|
||||
|
||||
|
||||
def _pil_to_clip_tensor(image: Image.Image) -> torch.Tensor:
|
||||
array = np.asarray(image.convert("RGB"), dtype=np.uint8)
|
||||
tensor = torch.from_numpy(array).permute(2, 0, 1).unsqueeze(0)
|
||||
return tensor
|
||||
|
||||
|
||||
def _pil_to_batched_tensor(image: Image.Image, *, compare_mode: str) -> torch.Tensor:
|
||||
array = np.asarray(image.convert(compare_mode), dtype=np.float32) / 255.0
|
||||
tensor = torch.from_numpy(array).permute(2, 0, 1).unsqueeze(0)
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import gc
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
import yaml
|
||||
from PIL import Image
|
||||
|
||||
from tests.e2e.accuracy.helpers import CLIPScorer, assert_similarity, model_output_dir
|
||||
from tests.helpers.runtime import OmniRunner
|
||||
from vllm_omni.diffusion.models.hunyuan_image3.prompt_utils import build_prompt_tokens
|
||||
|
||||
pytestmark = [pytest.mark.full_model, pytest.mark.diffusion]
|
||||
|
||||
MODEL_NAME = "tencent/HunyuanImage-3.0-Instruct"
|
||||
SEED = 42
|
||||
NUM_INFERENCE_STEPS = 50
|
||||
GUIDANCE_SCALE = 0
|
||||
HEIGHT, WIDTH = 1024, 1024
|
||||
PSNR_THRESHOLD = 0.0
|
||||
SSIM_THRESHOLD = 0.0
|
||||
CLIP_SCORE_THRESHOLD = 20.0
|
||||
PROMPT = "A brown and white dog is running on the grass."
|
||||
|
||||
# fmt: off
|
||||
_BASE_CONFIG = {
|
||||
"stage_args": [
|
||||
{
|
||||
"stage_id": 0, "stage_type": "llm",
|
||||
"runtime": {"process": True, "devices": "0,1", "max_batch_size": 1, "requires_multimodal_data": True},
|
||||
"engine_args": {
|
||||
"model_stage": "AR", "model_arch": "HunyuanImage3ForCausalMM",
|
||||
"worker_cls": "vllm_omni.worker.gpu_ar_worker.GPUARWorker",
|
||||
"scheduler_cls": "vllm_omni.core.sched.omni_ar_scheduler.OmniARScheduler",
|
||||
"gpu_memory_utilization": 0.95, "enforce_eager": True, "trust_remote_code": True,
|
||||
"engine_output_type": "latent", "enable_prefix_caching": False,
|
||||
"max_num_batched_tokens": 32768, "tensor_parallel_size": 2, "pipeline_parallel_size": 1,
|
||||
"hf_overrides": {"rope_parameters": {"mrope_section": [0, 32, 32], "rope_type": "default"}},
|
||||
},
|
||||
"is_comprehension": False, "final_output": True, "final_output_type": "text",
|
||||
"default_sampling_params": {
|
||||
"temperature": 0.0, "top_p": 1, "top_k": -1, "max_tokens": 8192,
|
||||
"stop_token_ids": [128025], "detokenize": True, "skip_special_tokens": False,
|
||||
},
|
||||
"output_connectors": {"to_stage_1": "rdma_connector"},
|
||||
},
|
||||
{
|
||||
"stage_id": 1, "stage_type": "diffusion",
|
||||
"runtime": {"process": True, "devices": "2,3", "max_batch_size": 1, "requires_multimodal_data": True},
|
||||
"engine_args": {
|
||||
"model_stage": "dit", "model_arch": "HunyuanImage3ForCausalMM",
|
||||
"enforce_eager": True, "trust_remote_code": True, "distributed_executor_backend": "mp",
|
||||
"parallel_config": {"tensor_parallel_size": 2, "enable_expert_parallel": True},
|
||||
},
|
||||
"engine_input_source": [0],
|
||||
"custom_process_input_func": "vllm_omni.model_executor.stage_input_processors.hunyuan_image3.ar2diffusion",
|
||||
"final_output": True, "final_output_type": "image",
|
||||
"default_sampling_params": {"num_inference_steps": NUM_INFERENCE_STEPS, "guidance_scale": GUIDANCE_SCALE},
|
||||
"input_connectors": {"from_stage_0": "rdma_connector"},
|
||||
},
|
||||
],
|
||||
"runtime": {
|
||||
"enabled": True,
|
||||
"connectors": {"rdma_connector": {
|
||||
"name": "MooncakeTransferEngineConnector",
|
||||
"extra": {"host": "auto", "zmq_port": 50051, "protocol": "rdma", "device_name": "", "memory_pool_size": 4294967296, "memory_pool_device": "cpu"},
|
||||
}},
|
||||
"edges": [{"from": 0, "to": 1}],
|
||||
},
|
||||
}
|
||||
# fmt: on
|
||||
|
||||
|
||||
def _make_config(enable_kv_reuse: bool, path: Path) -> None:
|
||||
config = copy.deepcopy(_BASE_CONFIG)
|
||||
config["stage_args"][0]["engine_args"]["omni_kv_config"] = {"need_send_cache": enable_kv_reuse}
|
||||
config["stage_args"][1]["engine_args"]["omni_kv_config"] = {"need_recv_cache": enable_kv_reuse}
|
||||
path.write_text(yaml.dump(config, default_flow_style=False, sort_keys=False))
|
||||
|
||||
|
||||
def _run(stage_config_path: str, output_path: Path) -> tuple[Image.Image, str, float]:
|
||||
from transformers import AutoTokenizer
|
||||
|
||||
from vllm_omni.inputs.data import OmniDiffusionSamplingParams, OmniPromptType
|
||||
from vllm_omni.platforms import current_omni_platform
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
||||
token_ids = build_prompt_tokens(PROMPT, tokenizer, task="it2i_recaption", sys_type="en_unified")
|
||||
|
||||
with OmniRunner(MODEL_NAME, stage_configs_path=stage_config_path) as runner:
|
||||
params_list = list(runner.omni.default_sampling_params_list)
|
||||
for sp in params_list:
|
||||
if isinstance(sp, OmniDiffusionSamplingParams):
|
||||
sp.num_inference_steps = NUM_INFERENCE_STEPS
|
||||
sp.guidance_scale = GUIDANCE_SCALE
|
||||
sp.seed = SEED
|
||||
sp.generator = torch.Generator(device=current_omni_platform.device_type or "cuda").manual_seed(SEED)
|
||||
|
||||
prompts: list[OmniPromptType] = [
|
||||
{
|
||||
"prompt_token_ids": token_ids,
|
||||
"prompt": PROMPT,
|
||||
"use_system_prompt": "en_unified",
|
||||
"height": HEIGHT,
|
||||
"width": WIDTH,
|
||||
}
|
||||
]
|
||||
t0 = time.perf_counter()
|
||||
outputs = list(runner.omni.generate(prompts=prompts, sampling_params_list=params_list))
|
||||
elapsed = time.perf_counter() - t0
|
||||
|
||||
assert outputs, "Pipeline produced no outputs"
|
||||
images = None
|
||||
cot_text = ""
|
||||
for out in outputs:
|
||||
ro = getattr(out, "request_output", None)
|
||||
if ro and getattr(ro, "outputs", None):
|
||||
txt = "".join(getattr(o, "text", "") or "" for o in ro.outputs)
|
||||
if txt:
|
||||
cot_text = txt
|
||||
|
||||
imgs = getattr(out, "images", None)
|
||||
if not imgs and ro and hasattr(ro, "images"):
|
||||
imgs = ro.images
|
||||
if imgs:
|
||||
images = imgs
|
||||
|
||||
assert images, "Pipeline output had no images"
|
||||
|
||||
image = images[0].convert("RGB")
|
||||
image.save(output_path)
|
||||
gc.collect()
|
||||
if torch.accelerator.is_available():
|
||||
torch.accelerator.empty_cache()
|
||||
return image, cot_text, elapsed
|
||||
|
||||
|
||||
@pytest.mark.skipif(torch.accelerator.device_count() < 4, reason="Needs 4+ GPUs (2 AR + 2 DiT)")
|
||||
def test_text_to_image_alignment(accuracy_artifact_root: Path) -> None:
|
||||
"""KV reuse ON vs OFF: same pipeline, same seed → PSNR >= 40 dB."""
|
||||
output_dir = model_output_dir(accuracy_artifact_root, MODEL_NAME + "-kv-reuse")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
tmp = Path(tmpdir)
|
||||
_make_config(False, tmp / "off.yaml")
|
||||
image_no_reuse_1, cot_no_reuse_1, time_no_reuse_1 = _run(
|
||||
str(tmp / "off.yaml"), output_dir / "without_kv_reuse_1.png"
|
||||
)
|
||||
image_no_reuse_2, cot_no_reuse_2, time_no_reuse_2 = _run(
|
||||
str(tmp / "off.yaml"), output_dir / "without_kv_reuse_2.png"
|
||||
)
|
||||
_make_config(True, tmp / "on.yaml")
|
||||
image_reuse, cot_reuse, time_reuse = _run(str(tmp / "on.yaml"), output_dir / "with_kv_reuse.png")
|
||||
|
||||
print("\n--- End-to-end time ---")
|
||||
print(f" WITHOUT KV reuse 1: {time_no_reuse_1:.2f}s")
|
||||
print(f" WITHOUT KV reuse 2: {time_no_reuse_2:.2f}s")
|
||||
print(f" WITH KV reuse: {time_reuse:.2f}s")
|
||||
|
||||
(output_dir / "cot_with_kv_reuse.txt").write_text(cot_reuse, encoding="utf-8")
|
||||
(output_dir / "cot_without_kv_reuse_1.txt").write_text(cot_no_reuse_1, encoding="utf-8")
|
||||
(output_dir / "cot_without_kv_reuse_2.txt").write_text(cot_no_reuse_2, encoding="utf-8")
|
||||
|
||||
print(f"\n--- CoT WITH KV reuse (len={len(cot_reuse)}) ---\n{cot_reuse}")
|
||||
print(f"\n--- CoT WITHOUT KV reuse 1 (len={len(cot_no_reuse_1)}) ---\n{cot_no_reuse_1}")
|
||||
print(f"\n--- CoT WITHOUT KV reuse 2 (len={len(cot_no_reuse_2)}) ---\n{cot_no_reuse_2}")
|
||||
|
||||
clip_scorer = CLIPScorer()
|
||||
clip_scorer.assert_score(
|
||||
model_name=f"{MODEL_NAME} with-reuse",
|
||||
image=image_reuse,
|
||||
text=PROMPT,
|
||||
threshold=CLIP_SCORE_THRESHOLD,
|
||||
)
|
||||
clip_scorer.assert_score(
|
||||
model_name=f"{MODEL_NAME} without-reuse 1",
|
||||
image=image_no_reuse_1,
|
||||
text=PROMPT,
|
||||
threshold=CLIP_SCORE_THRESHOLD,
|
||||
)
|
||||
clip_scorer.assert_score(
|
||||
model_name=f"{MODEL_NAME} without-reuse 2",
|
||||
image=image_no_reuse_2,
|
||||
text=PROMPT,
|
||||
threshold=CLIP_SCORE_THRESHOLD,
|
||||
)
|
||||
|
||||
assert_similarity(
|
||||
model_name=f"{MODEL_NAME} non-reuse run1 vs run2",
|
||||
vllm_image=image_no_reuse_1,
|
||||
diffusers_image=image_no_reuse_2,
|
||||
ssim_threshold=SSIM_THRESHOLD,
|
||||
psnr_threshold=PSNR_THRESHOLD,
|
||||
width=WIDTH,
|
||||
height=HEIGHT,
|
||||
)
|
||||
|
||||
assert_similarity(
|
||||
model_name=f"{MODEL_NAME} KV-reuse vs no-reuse",
|
||||
vllm_image=image_reuse,
|
||||
diffusers_image=image_no_reuse_1,
|
||||
ssim_threshold=SSIM_THRESHOLD,
|
||||
psnr_threshold=PSNR_THRESHOLD,
|
||||
width=WIDTH,
|
||||
height=HEIGHT,
|
||||
)
|
||||
Reference in new issue
Block a user