Official repository for LTX-Video
Python diffusion-modelsditimage-to-videoimage-to-video-generationtext-to-videotext-to-video-generation
STG: Support skipping entire transformer blocks
Yoav HaCohen278c43553106
diff --git a/inference.py b/inference.pyindex 704ba6a..cac8110 100644--- a/inference.py+++ b/inference.py@@ -177,31 +177,32 @@ def main():"--guidance_scale",type=float,default=3,- help="Guidance scale for the pipeline",+ help="Guidance scale.",)parser.add_argument("--stg_scale",type=float,default=1,- help="Spatiotemporal guidance scale for the pipeline. 0 to disable STG.",+ help="Spatiotemporal guidance scale. 0 to disable STG.",)parser.add_argument("--stg_rescale",type=float,default=0.7,- help="Spatiotemporal guidance rescaling scale for the pipeline. 1 to disable rescale.",+ help="Spatiotemporal guidance rescaling scale. 1 to disable rescale.",)parser.add_argument("--stg_mode",type=str,- default="stg_a",- help="Spatiotemporal guidance mode for the pipeline. Can be either stg_a or stg_r.",+ default="attention",+ help="Spatiotemporal guidance mode. "+ "It can be one of 'attention' (default), 'residual', or 'transformer_block'.",)parser.add_argument("--stg_skip_layers",type=str,default="19",- help="Attention layers to skip for spatiotemporal guidance. Comma separated list of integers.",+ help="Layers to block for spatiotemporal guidance. Comma separated list of integers.",)parser.add_argument("--image_cond_noise_scale",@@ -348,11 +349,16 @@ def main():# Set spatiotemporal guidanceskip_block_list = [int(x.strip()) for x in args.stg_skip_layers.split(",")]- skip_layer_strategy = (- SkipLayerStrategy.Attention- if args.stg_mode.lower() == "stg_a"- else SkipLayerStrategy.Residual- )+ if args.stg_mode.lower() == "stg_a" or args.stg_mode.lower() == "attention":+ skip_layer_strategy = SkipLayerStrategy.Attention+ elif args.stg_mode.lower() == "stg_r" or args.stg_mode.lower() == "residual":+ skip_layer_strategy = SkipLayerStrategy.Residual+ elif (+ args.stg_mode.lower() == "stg_t" or args.stg_mode.lower() == "transformer_block"+ ):+ skip_layer_strategy = SkipLayerStrategy.TransformerBlock+ else:+ raise ValueError(f"Invalid spatiotemporal guidance mode: {args.stg_mode}")# Use submodels for the pipelinesubmodel_dict = {diff --git a/ltx_video/models/transformers/attention.py b/ltx_video/models/transformers/attention.pyindex 96ed251..6eeac94 100644--- a/ltx_video/models/transformers/attention.py+++ b/ltx_video/models/transformers/attention.py@@ -219,6 +219,8 @@ class BasicTransformerBlock(nn.Module):# 0. Self-Attentionbatch_size = hidden_states.shape[0]+ original_hidden_states = hidden_states+norm_hidden_states = self.norm1(hidden_states)# Apply ada_norm_single@@ -308,6 +310,15 @@ class BasicTransformerBlock(nn.Module):if hidden_states.ndim == 4:hidden_states = hidden_states.squeeze(1)+ if (+ skip_layer_mask is not None+ and skip_layer_strategy == SkipLayerStrategy.TransformerBlock+ ):+ skip_layer_mask = skip_layer_mask.view(-1, 1, 1)+ hidden_states = hidden_states * skip_layer_mask + original_hidden_states * (+ 1.0 - skip_layer_mask+ )+return hidden_statesdiff --git a/ltx_video/utils/skip_layer_strategy.py b/ltx_video/utils/skip_layer_strategy.pyindex fd4c133..39638b5 100644--- a/ltx_video/utils/skip_layer_strategy.py+++ b/ltx_video/utils/skip_layer_strategy.py@@ -4,3 +4,4 @@ from enum import Enum, autoclass SkipLayerStrategy(Enum):Attention = auto()Residual = auto()+ TransformerBlock = auto()