longcat ¶
Native LongCat Video DiT implementation using FastVideo conventions.
Classes¶
fastvideo.models.dits.longcat.CaptionEmbedder ¶
CaptionEmbedder(caption_channels: int = 4096, hidden_size: int = 4096, text_tokens_zero_pad: bool = True, dtype: dtype | None = None)
Bases: Module
Caption embedding with MLP projection and optional text compaction.
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.CaptionEmbedder.forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder_hidden_states | Tensor | [B, N_text, C_text] or [B, 1, N_text, C_text] | required |
encoder_attention_mask | Tensor | None | [B, N_text] or [B, 1, 1, N_text] | None |
Returns: y: [B, N_text, C] - standard padded representation (like other models)
Source code in fastvideo/models/dits/longcat.py
fastvideo.models.dits.longcat.FinalLayer ¶
FinalLayer(hidden_size: int, out_channels: int, adaln_tembed_dim: int, patch_size: tuple[int, int, int], dtype: dtype | None = None)
Bases: Module
Final output projection with AdaLN modulation.
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.FinalLayer.forward ¶
forward(x: Tensor, t: Tensor, latent_shape: tuple) -> Tensor
Returns: [B, N, out_channels * patch_size^3]
Source code in fastvideo/models/dits/longcat.py
fastvideo.models.dits.longcat.LongCatCrossAttention ¶
LongCatCrossAttention(dim: int, num_heads: int, config: LongCatVideoConfig, dtype: dtype | None = None)
Bases: Module
Cross-attention for text conditioning (standard implementation like other models).
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.LongCatCrossAttention.forward ¶
forward(x: Tensor, context: Tensor, latent_shape: tuple = None, num_cond_latents: int = 0, **kwargs) -> Tensor
Forward pass for cross-attention (standard implementation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | Image tokens [B, N_img, C] | required |
context | Tensor | Text tokens [B, N_text, C] (standard padded format) | required |
latent_shape | tuple | (T, H, W) - needed for calculating num_cond_tokens | None |
num_cond_latents | int | Number of conditioning latent frames (for I2V) | 0 |
For I2V mode (num_cond_latents > 0): - Conditioned tokens get ZERO cross-attention output - Only noise tokens get cross-attention with text
Source code in fastvideo/models/dits/longcat.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
fastvideo.models.dits.longcat.LongCatSelfAttention ¶
LongCatSelfAttention(dim: int, num_heads: int, config: LongCatVideoConfig, dtype: dtype | None = None)
Bases: Module
Self-attention with 3D RoPE support and optional BSA.
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.LongCatSelfAttention.forward ¶
forward(x: Tensor, latent_shape: tuple, num_cond_latents: int = 0, return_kv: bool = False, **kwargs) -> Tensor | tuple
Forward pass with 3D RoPE and optional BSA.
For I2V mode (num_cond_latents > 0): - Conditioned tokens only attend to themselves - Noise tokens attend to ALL tokens (cond + noise)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_kv | bool | If True, return (output, (k_cache, v_cache)) for KV caching | False |
Source code in fastvideo/models/dits/longcat.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
fastvideo.models.dits.longcat.LongCatSelfAttention.forward_with_kv_cache ¶
forward_with_kv_cache(x: Tensor, latent_shape: tuple, num_cond_latents: int, kv_cache: tuple) -> Tensor
Forward using cached K/V from conditioning frames.
x contains only NOISE tokens. kv_cache contains pre-computed K/V for CONDITIONING tokens.
CRITICAL: RoPE positions for noise tokens must start AFTER conditioning. We achieve this by padding Q with dummy tokens for conditioning positions, applying RoPE to the full sequence, then extracting only noise token Q.
Source code in fastvideo/models/dits/longcat.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
fastvideo.models.dits.longcat.LongCatSwiGLUFFN ¶
Bases: Module
SwiGLU feed-forward network using FastVideo's ReplicatedLinear.
FFN(x) = down(gate(x) * SiLU(up(x)))
Source code in fastvideo/models/dits/longcat.py
fastvideo.models.dits.longcat.LongCatTransformer3DModel ¶
LongCatTransformer3DModel(config: LongCatVideoConfig, hf_config: dict[str, Any])
Bases: BaseDiT
Native LongCat Video Transformer using FastVideo layers.
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.LongCatTransformer3DModel.disable_bsa ¶
fastvideo.models.dits.longcat.LongCatTransformer3DModel.enable_bsa ¶
fastvideo.models.dits.longcat.LongCatTransformer3DModel.forward ¶
forward(hidden_states: Tensor, encoder_hidden_states: Tensor | list[Tensor], timestep: LongTensor, encoder_attention_mask: Tensor | None = None, encoder_hidden_states_image: Tensor | list[Tensor] | None = None, guidance: float | None = None, num_cond_latents: int = 0, return_kv: bool = False, kv_cache_dict: dict | None = None, skip_crs_attn: bool = False, offload_kv_cache: bool = False, **kwargs) -> Tensor | tuple[Tensor, dict]
Forward pass with FastVideo parameter ordering.
This follows FastVideo convention:
(hidden_states, encoder_hidden_states, timestep)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_cond_latents | int | For I2V, number of conditioning latent frames. These frames are treated as "clean" (timestep=0) and use split attention behavior. | 0 |
return_kv | bool | If True, return (output, kv_cache_dict) | False |
kv_cache_dict | dict | None | Pre-computed K/V cache {block_idx: (k, v)} | None |
skip_crs_attn | bool | If True, skip cross-attention (for cache init) | False |
offload_kv_cache | bool | If True, move cache to CPU after compute | False |
Source code in fastvideo/models/dits/longcat.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 | |
fastvideo.models.dits.longcat.LongCatTransformer3DModel.unpatchify ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | [B, N, C] where C = T_p * H_p * W_p * C_out | required |
Returns: [B, C_out, T, H, W]
Source code in fastvideo/models/dits/longcat.py
fastvideo.models.dits.longcat.LongCatTransformerBlock ¶
LongCatTransformerBlock(hidden_size: int, num_heads: int, mlp_ratio: int, adaln_tembed_dim: int, config: LongCatVideoConfig, dtype: dtype | None = None)
Bases: Module
Single-stream transformer block with: - AdaLN modulation (FP32) - Self-attention - Cross-attention - SwiGLU FFN
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.LongCatTransformerBlock.forward ¶
forward(x: Tensor, context: Tensor, t: Tensor, latent_shape: tuple, num_cond_latents: int = 0, return_kv: bool = False, kv_cache: tuple | None = None, skip_crs_attn: bool = False, **kwargs) -> Tensor | tuple
Forward pass with AdaLN modulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_cond_latents | int | For I2V, number of conditioning latent frames. These frames use split attention behavior. | 0 |
return_kv | bool | If True, return (x, (k_cache, v_cache)) | False |
kv_cache | tuple | None | Pre-computed K/V from conditioning frames | None |
skip_crs_attn | bool | If True, skip cross-attention (used during cache init) | False |
Source code in fastvideo/models/dits/longcat.py
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
fastvideo.models.dits.longcat.PatchEmbed3D ¶
PatchEmbed3D(patch_size: tuple[int, int, int] = (1, 2, 2), in_channels: int = 16, embed_dim: int = 4096)
Bases: Module
3D patch embedding using Conv3d.
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.PatchEmbed3D.forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | [B, C, T, H, W] | required |
Returns: [B, N, C] where N = (T/pt) * (H/ph) * (W/pw)
Source code in fastvideo/models/dits/longcat.py
fastvideo.models.dits.longcat.TimestepEmbedder ¶
TimestepEmbedder(frequency_embedding_size: int = 256, adaln_tembed_dim: int = 512, dtype: dtype | None = None)
Bases: Module
Sinusoidal timestep embedding + MLP projection.
Source code in fastvideo/models/dits/longcat.py
Methods:¶
fastvideo.models.dits.longcat.TimestepEmbedder.forward ¶
forward(t: Tensor, latent_shape: tuple | None = None) -> Tensor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t | Tensor | [B] or [B, T] timesteps | required |
latent_shape | tuple | None | (T, H, W) for temporal expansion | None |
Returns: [B, T, C]
Source code in fastvideo/models/dits/longcat.py
fastvideo.models.dits.longcat.TimestepEmbedder.timestep_embedding staticmethod ¶
Create sinusoidal timestep embeddings.
Source code in fastvideo/models/dits/longcat.py
Functions:¶
fastvideo.models.dits.longcat.modulate_fp32 ¶
Apply modulation in FP32 for numerical stability.
Converts inputs to FP32 for the modulation operation, then casts back.