[Feat] Support teacache for SenseNova-U1 (#4164)
Signed-off-by: hanzheli <hanzheli@kuaishou.com> Signed-off-by: princepride <wangzhipeng628@gmail.com> Signed-off-by: 汪志鹏 <wangzhipeng628@gmail.com> Co-authored-by: princepride <wangzhipeng628@gmail.com>
This commit is contained in:
1 parent
bb8614595d
commit
9a105bf90d
7 files changed
+130
-11
No files matched your search
@@ -1187,6 +1187,68 @@ def extract_flux_context(
|
||||
)
|
||||
|
||||
|
||||
def extract_sensenova_u1_context(
|
||||
module: nn.Module,
|
||||
input_ids: torch.Tensor | None = None,
|
||||
indexes: torch.Tensor | None = None,
|
||||
attention_mask: dict[str, torch.Tensor] | torch.Tensor | None = None,
|
||||
past_key_values: Any | None = None,
|
||||
inputs_embeds: torch.Tensor | None = None,
|
||||
use_cache: bool | None = None,
|
||||
embed_only: bool = False,
|
||||
compute_logits: bool = True,
|
||||
**kwargs: Any,
|
||||
) -> CacheContext:
|
||||
"""Extract cache context for SenseNovaU1ForCausalLM denoising forwards."""
|
||||
from vllm_omni.diffusion.models.sensenova_u1.sensenova_u1_transformer import (
|
||||
SenseNovaU1CausalLMOutput,
|
||||
)
|
||||
|
||||
layer_kwargs = dict(kwargs)
|
||||
layer_kwargs.pop("cache_dit_skip", None)
|
||||
image_gen_indicators = layer_kwargs.pop("image_gen_indicators", None)
|
||||
exist_und = (~image_gen_indicators).any().item()
|
||||
exist_gen = image_gen_indicators.any().item()
|
||||
causal_mask_mapping = attention_mask
|
||||
|
||||
first_layer = module.model.layers[0]
|
||||
modulated_input = first_layer.input_layernorm_mot_gen(inputs_embeds)
|
||||
|
||||
def run_transformer_blocks():
|
||||
h = inputs_embeds
|
||||
for layer in module.model.layers:
|
||||
h = layer(
|
||||
h,
|
||||
image_gen_indicators=image_gen_indicators,
|
||||
exist_und=exist_und,
|
||||
exist_gen=exist_gen,
|
||||
indexes=indexes,
|
||||
attention_mask=causal_mask_mapping,
|
||||
past_key_values=past_key_values,
|
||||
**layer_kwargs,
|
||||
)
|
||||
return (h,)
|
||||
|
||||
def postprocess(h: torch.Tensor) -> SenseNovaU1CausalLMOutput:
|
||||
h = module.model.norm_mot_gen(h)
|
||||
|
||||
logits = module.logits_processor(module.lm_head, h) if compute_logits else None
|
||||
return SenseNovaU1CausalLMOutput(
|
||||
logits=logits,
|
||||
past_key_values=past_key_values if use_cache else None,
|
||||
hidden_states=h,
|
||||
)
|
||||
|
||||
return CacheContext(
|
||||
modulated_input=modulated_input,
|
||||
hidden_states=inputs_embeds,
|
||||
encoder_hidden_states=None,
|
||||
temb=modulated_input,
|
||||
run_transformer_blocks=run_transformer_blocks,
|
||||
postprocess=postprocess,
|
||||
)
|
||||
|
||||
|
||||
# Registry for model-specific extractors
|
||||
# Key: Transformer class name
|
||||
# Value: extractor function with signature (module, *args, **kwargs) -> CacheContext
|
||||
@@ -1202,6 +1264,7 @@ EXTRACTOR_REGISTRY: dict[str, Callable] = {
|
||||
"Flux2Transformer2DModel": extract_flux2_context,
|
||||
"LongCatImageTransformer2DModel": extract_longcat_context,
|
||||
"FluxTransformer2DModel": extract_flux_context,
|
||||
"SenseNovaU1ForCausalLM": extract_sensenova_u1_context,
|
||||
# Future models:
|
||||
# "CogVideoXTransformer3DModel": extract_cogvideox_context,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user