diff --git a/unirl/models/hunyuan_image3/ar.py b/unirl/models/hunyuan_image3/ar.py index 000e78b55..ef36235b3 100644 --- a/unirl/models/hunyuan_image3/ar.py +++ b/unirl/models/hunyuan_image3/ar.py @@ -2,6 +2,7 @@ from __future__ import annotations +import sys from dataclasses import dataclass from dataclasses import field as dc_field from typing import Any, Dict, List, Optional, Tuple @@ -99,8 +100,13 @@ def init_state( batch_size = int(input_ids.shape[0]) prompt_len = int(input_ids.shape[1]) - past_kv_initial = self._build_kv_cache( - transformer, batch_size=batch_size, max_cache_len=prompt_len + int(max_new_tokens) + cache_cls = sys.modules[type(transformer).__module__].HunyuanStaticCache + past_kv_initial = cache_cls( + config=transformer.config, + batch_size=batch_size, + max_cache_len=prompt_len + int(max_new_tokens), + dtype=torch.bfloat16, + dynamic=True, ) cond_vit = conditions.cond_vit @@ -214,29 +220,6 @@ def step( return token_id, log_prob, state - @staticmethod - def _build_kv_cache(transformer, *, batch_size: int, max_cache_len: int): - """Pre-build a ``HunyuanStaticCache`` for the AR loop.""" - import sys as _sys - - upstream_mod = _sys.modules.get(type(transformer).__module__) - cache_cls = getattr(upstream_mod, "HunyuanStaticCache", None) - if cache_cls is None: - return None - config = getattr(transformer, "config", None) - if config is None: - return None - try: - return cache_cls( - config=config, - batch_size=batch_size, - max_cache_len=max_cache_len, - dtype=torch.bfloat16, - dynamic=True, - ) - except Exception: # noqa: BLE001 -- fall back to HF default cache - return None - class HunyuanImage3ARStage(ARStage[HunyuanImage3ARConditions]): """Rollout-level AR stage: ``HunyuanImage3ARConditions → TextSegment``."""