Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions unirl/models/hunyuan_image3/ar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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``."""
Expand Down
Loading